Skip to content

Commit 343c0dd

Browse files
authored
change to Gtk4 (#2) (#5)
* Gtk4 final --------- Co-authored-by: marsalien7 Co-authored-by: Martin
1 parent 4a32314 commit 343c0dd

File tree

5 files changed

+83
-63
lines changed

5 files changed

+83
-63
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
GIT Testprojekt
44

55
## Change log
6-
Version Branch Änderung
7-
0.0.2 main Gtk Grundgerüst
6+
Version Branch Änderung
7+
0.0.2 main Gtk Grundgerüst
8+
0.0.4 feature-gtk4 Umstellung auf gtk4
9+
'apt install libgtk-4-1 libgtk-4-dev libgtk-4-bin libgtk-4-common'

GITHELP.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# GIT Help
22

3+
Marker : feature-keybord branch
4+
5+
Noch eine Änderung
6+
7+
## Strategie
8+
9+
10+
311
## First steps
412

513
git init

main.c

Lines changed: 67 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,103 @@
11
#include <gtk/gtk.h>
2+
// https://docs.gtk.org/gtk4/
3+
4+
#define BUTTON_NUM 20
25

36
GtkWidget *window;
47
GtkWidget *entry;
58
GtkWidget *button;
69
GtkWidget *grid;
7-
GtkWidget *buttons[10];
10+
GtkWidget *buttons[BUTTON_NUM];
811
GtkWidget *box;
912

13+
const char *buttonlabels[BUTTON_NUM] = {"C","/","*","-","7","8","9","+","4","5","6","(","1","2","3",")"," ","0",",","="};
14+
1015
// Funktion zum Behandeln des Button-Klicks
11-
void button_clicked(GtkWidget *button, gpointer data) {
12-
const char *text = gtk_entry_get_text(GTK_ENTRY(data));
16+
void button_clicked(GtkWidget *button, gpointer data)
17+
{
18+
const char *text = gtk_editable_get_text(GTK_EDITABLE(data));
1319
printf("Der eingegebene Text ist: %s\n", text);
1420
}
1521

1622
// Funktion zum Behandeln der Ziffern-Buttons
17-
void number_button_clicked(GtkWidget *button, gpointer data) {
18-
const char *number = gtk_button_get_label(GTK_BUTTON(button));
19-
gtk_entry_set_text(GTK_ENTRY(data), number);
20-
// gtk_entry_append_text(GTK_ENTRY(data), number);
23+
void number_button_clicked(GtkWidget *button, gpointer data)
24+
{
25+
const char *label = gtk_button_get_label(GTK_BUTTON(button));
26+
char button_char = *label;
27+
switch (button_char) {
28+
case 'C':
29+
// gtk_entry_set_text (GTK_ENTRY(data), "");
30+
gtk_editable_set_text(GTK_EDITABLE(data), "");
31+
break;
32+
default:
33+
GtkEntryBuffer *buffer = gtk_entry_get_buffer (GTK_ENTRY(data));
34+
gtk_entry_buffer_insert_text (buffer, -1, label, -1);
35+
}
2136
}
2237

23-
int main(int argc, char *argv[]) {
24-
gtk_init(&argc, &argv);
25-
38+
static void activate (GtkApplication *app, gpointer user_data)
39+
{
2640
// Erstellen Sie ein Fenster
27-
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
28-
gtk_window_set_title(GTK_WINDOW(window), "Hello World");
29-
gtk_window_set_default_size(GTK_WINDOW(window), 300, 100);
41+
window = gtk_application_window_new (app);
42+
gtk_window_set_title(GTK_WINDOW(window), "Hello World Gtk4");
43+
gtk_window_set_default_size(GTK_WINDOW(window), 320, 100);
3044

31-
// Erstellen Sie ein Eingabefeld
45+
// Erstellt ein Eingabefeld
3246
entry = gtk_entry_new();
3347

34-
// Erstellen Sie einen Button
48+
// Erstellt einen Button
3549
button = gtk_button_new_with_label("Eingabe ausgeben");
36-
37-
// Verbinden Sie den Button-Klick mit der Funktion button_clicked
50+
51+
// Verbindt den Button-Klick mit der Funktion button_clicked
3852
g_signal_connect(button, "clicked", G_CALLBACK(button_clicked), entry);
3953

40-
// Verbinden Sie den delete-event-Handler mit gtk_main_quit()
41-
g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);
42-
43-
/**/
44-
45-
// Erstellen Sie ein GtkGrid-Widget für den Tastenblock
54+
// Erstellt ein GtkGrid-Widget für den Tastenblock
4655
grid = gtk_grid_new();
4756
gtk_grid_set_column_spacing(GTK_GRID(grid), 5);
4857
gtk_grid_set_row_spacing(GTK_GRID(grid), 5);
4958

50-
// Erstellen Sie 10 Buttons für die Ziffern 0-9
51-
for (int i = 0; i < 10; i++) {
52-
buttons[i] = gtk_button_new_with_label(g_strdup_printf("%d", i));
59+
for (int i = 0; i < BUTTON_NUM; i++) {
60+
// Erstellen der Buttons
61+
buttons[i] = gtk_button_new_with_label(buttonlabels[i]);
5362
g_signal_connect(buttons[i], "clicked", G_CALLBACK(number_button_clicked), entry);
54-
}
5563

56-
// Fügen Sie die Buttons in das Grid-Widget ein
57-
int row = 0, column = 0;
58-
for (int i = 0; i < 10; i++) {
59-
gtk_grid_attach(GTK_GRID(grid), buttons[i], column, row, 1, 1);
60-
if (column == 2) {
61-
column = 0;
62-
row++;
63-
} else {
64-
column++;
65-
}
64+
// Fügt die Buttons in das Grid-Widget ein
65+
gtk_grid_attach(GTK_GRID(grid), buttons[i], i % 4, i / 4, 1, 1);
6666
}
6767

68-
/**/
6968
// Erstellen Sie einen Box-Container und fügen Sie das Eingabefeld, den Button und den Tastenblock hinzu
70-
box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
71-
gtk_box_pack_start(GTK_BOX(box), entry, TRUE, TRUE, 0);
72-
gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
73-
gtk_box_pack_start(GTK_BOX(box), grid, TRUE, TRUE, 0);
69+
box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
70+
gtk_box_append(GTK_BOX(box), entry);
71+
gtk_widget_set_margin_top(entry, 10);
72+
gtk_widget_set_margin_start(entry, 10);
73+
gtk_widget_set_margin_end(entry, 10);
74+
75+
gtk_box_append(GTK_BOX(box), grid);
76+
gtk_widget_set_halign(grid, GTK_ALIGN_CENTER);
77+
gtk_widget_set_margin_top(grid, 10);
78+
gtk_widget_set_margin_bottom(grid, 10);
79+
80+
gtk_box_append(GTK_BOX(box), button);
81+
gtk_widget_set_margin_start(button, 10);
82+
gtk_widget_set_margin_end(button, 10);
83+
gtk_widget_set_margin_bottom(button, 10);
7484

7585
// Fügen Sie den Box-Container zum Fenster hinzu
76-
gtk_container_add(GTK_CONTAINER(window), box);
86+
gtk_window_set_child (GTK_WINDOW (window), box);
87+
88+
gtk_window_present (GTK_WINDOW (window));
89+
}
7790

78-
// Zeigen Sie alle Widgets an
79-
gtk_widget_show_all(window);
8091

81-
// Starten Sie die GTK+-Hauptschleife
82-
gtk_main();
92+
int main (int argc, char **argv)
93+
{
94+
GtkApplication *app;
95+
int status;
8396

84-
return 0;
85-
}
97+
app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE);
98+
g_signal_connect(app, "activate", G_CALLBACK (activate), NULL);
99+
status = g_application_run (G_APPLICATION (app), argc, argv);
100+
g_object_unref (app);
101+
102+
return status;
103+
}

makefile

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,19 @@ HDRS = \
1212

1313
# Bibliotheken
1414
LIBS = \
15-
-lgtk3
15+
-lgtk4
1616

1717
# Objektdateien
1818
OBJS = $(SRCS:.c=.o)
1919

2020
# Flagge für GTK+-Entwicklung
21-
GTK3_CFLAGS = -Wall `pkg-config --cflags glib-2.0` \
22-
-I/usr/include/gtk-3.0 \
23-
-I/usr/include/glib-2.0 \
24-
-I/usr/include/pango-1.0 \
25-
-I/usr/include/harfbuzz \
26-
-I/usr/include/cairo \
27-
-I/usr/include/gdk-pixbuf-2.0 \
28-
-I/usr/include/atk-1.0
21+
GTK3_CFLAGS = -Wall `pkg-config --cflags gtk4`
2922

3023
# Flagge für C-Compiler
3124
CFLAGS = $(GTK3_CFLAGS)
3225

3326
# Flagge für Linker
34-
LDFLAGS = `pkg-config --libs gtk+-3.0` -L/usr/local/lib -L/usr/lib
35-
#LDFLAGS = `pkg-config --libs glib-2.0` -L/usr/local/lib -L/usr/lib
27+
LDFLAGS = `pkg-config --libs gtk4` -L/usr/local/lib -L/usr/lib
3628

3729
# Programmname
3830
PROGRAM = $(PROJECT)

settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"c_cpp.includePath": [
3-
"/usr/include/gtk-3.0" // Ersetzen Sie diesen Pfad nach Bedarf
3+
"/usr/include/gtk-4.0" // Ersetzen Sie diesen Pfad nach Bedarf
44
]
55
}

0 commit comments

Comments
 (0)