From 343c0ddc88f815a3952c444384d1d07b3b182aa0 Mon Sep 17 00:00:00 2001 From: Martin <79449686+marsman7@users.noreply.github.com> Date: Wed, 27 Mar 2024 15:44:18 +0100 Subject: [PATCH] change to Gtk4 (#2) (#5) * Gtk4 final --------- Co-authored-by: marsalien7 Co-authored-by: Martin --- CHANGELOG.md | 6 ++- GITHELP.md | 8 ++++ main.c | 116 +++++++++++++++++++++++++++++--------------------- makefile | 14 ++---- settings.json | 2 +- 5 files changed, 83 insertions(+), 63 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f052f1..5f8b3eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,5 +3,7 @@ GIT Testprojekt ## Change log -Version Branch Änderung -0.0.2 main Gtk Grundgerüst +Version Branch Änderung +0.0.2 main Gtk Grundgerüst +0.0.4 feature-gtk4 Umstellung auf gtk4 +'apt install libgtk-4-1 libgtk-4-dev libgtk-4-bin libgtk-4-common' diff --git a/GITHELP.md b/GITHELP.md index ce74b66..114ba87 100644 --- a/GITHELP.md +++ b/GITHELP.md @@ -1,5 +1,13 @@ # GIT Help +Marker : feature-keybord branch + +Noch eine Änderung + +## Strategie + + + ## First steps git init diff --git a/main.c b/main.c index 5c57300..c292f60 100644 --- a/main.c +++ b/main.c @@ -1,85 +1,103 @@ #include +// https://docs.gtk.org/gtk4/ + +#define BUTTON_NUM 20 GtkWidget *window; GtkWidget *entry; GtkWidget *button; GtkWidget *grid; -GtkWidget *buttons[10]; +GtkWidget *buttons[BUTTON_NUM]; GtkWidget *box; +const char *buttonlabels[BUTTON_NUM] = {"C","/","*","-","7","8","9","+","4","5","6","(","1","2","3",")"," ","0",",","="}; + // Funktion zum Behandeln des Button-Klicks -void button_clicked(GtkWidget *button, gpointer data) { - const char *text = gtk_entry_get_text(GTK_ENTRY(data)); +void button_clicked(GtkWidget *button, gpointer data) +{ + const char *text = gtk_editable_get_text(GTK_EDITABLE(data)); printf("Der eingegebene Text ist: %s\n", text); } // Funktion zum Behandeln der Ziffern-Buttons -void number_button_clicked(GtkWidget *button, gpointer data) { - const char *number = gtk_button_get_label(GTK_BUTTON(button)); - gtk_entry_set_text(GTK_ENTRY(data), number); -// gtk_entry_append_text(GTK_ENTRY(data), number); +void number_button_clicked(GtkWidget *button, gpointer data) +{ + const char *label = gtk_button_get_label(GTK_BUTTON(button)); + char button_char = *label; + switch (button_char) { + case 'C': +// gtk_entry_set_text (GTK_ENTRY(data), ""); + gtk_editable_set_text(GTK_EDITABLE(data), ""); + break; + default: + GtkEntryBuffer *buffer = gtk_entry_get_buffer (GTK_ENTRY(data)); + gtk_entry_buffer_insert_text (buffer, -1, label, -1); + } } -int main(int argc, char *argv[]) { - gtk_init(&argc, &argv); - +static void activate (GtkApplication *app, gpointer user_data) +{ // Erstellen Sie ein Fenster - window = gtk_window_new(GTK_WINDOW_TOPLEVEL); - gtk_window_set_title(GTK_WINDOW(window), "Hello World"); - gtk_window_set_default_size(GTK_WINDOW(window), 300, 100); + window = gtk_application_window_new (app); + gtk_window_set_title(GTK_WINDOW(window), "Hello World Gtk4"); + gtk_window_set_default_size(GTK_WINDOW(window), 320, 100); - // Erstellen Sie ein Eingabefeld + // Erstellt ein Eingabefeld entry = gtk_entry_new(); - // Erstellen Sie einen Button + // Erstellt einen Button button = gtk_button_new_with_label("Eingabe ausgeben"); - - // Verbinden Sie den Button-Klick mit der Funktion button_clicked + + // Verbindt den Button-Klick mit der Funktion button_clicked g_signal_connect(button, "clicked", G_CALLBACK(button_clicked), entry); - // Verbinden Sie den delete-event-Handler mit gtk_main_quit() - g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL); - -/**/ - - // Erstellen Sie ein GtkGrid-Widget für den Tastenblock + // Erstellt ein GtkGrid-Widget für den Tastenblock grid = gtk_grid_new(); gtk_grid_set_column_spacing(GTK_GRID(grid), 5); gtk_grid_set_row_spacing(GTK_GRID(grid), 5); - // Erstellen Sie 10 Buttons für die Ziffern 0-9 - for (int i = 0; i < 10; i++) { - buttons[i] = gtk_button_new_with_label(g_strdup_printf("%d", i)); + for (int i = 0; i < BUTTON_NUM; i++) { + // Erstellen der Buttons + buttons[i] = gtk_button_new_with_label(buttonlabels[i]); g_signal_connect(buttons[i], "clicked", G_CALLBACK(number_button_clicked), entry); - } - // Fügen Sie die Buttons in das Grid-Widget ein - int row = 0, column = 0; - for (int i = 0; i < 10; i++) { - gtk_grid_attach(GTK_GRID(grid), buttons[i], column, row, 1, 1); - if (column == 2) { - column = 0; - row++; - } else { - column++; - } + // Fügt die Buttons in das Grid-Widget ein + gtk_grid_attach(GTK_GRID(grid), buttons[i], i % 4, i / 4, 1, 1); } -/**/ // Erstellen Sie einen Box-Container und fügen Sie das Eingabefeld, den Button und den Tastenblock hinzu - box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); - gtk_box_pack_start(GTK_BOX(box), entry, TRUE, TRUE, 0); - gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0); - gtk_box_pack_start(GTK_BOX(box), grid, TRUE, TRUE, 0); + box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); + gtk_box_append(GTK_BOX(box), entry); + gtk_widget_set_margin_top(entry, 10); + gtk_widget_set_margin_start(entry, 10); + gtk_widget_set_margin_end(entry, 10); + + gtk_box_append(GTK_BOX(box), grid); + gtk_widget_set_halign(grid, GTK_ALIGN_CENTER); + gtk_widget_set_margin_top(grid, 10); + gtk_widget_set_margin_bottom(grid, 10); + + gtk_box_append(GTK_BOX(box), button); + gtk_widget_set_margin_start(button, 10); + gtk_widget_set_margin_end(button, 10); + gtk_widget_set_margin_bottom(button, 10); // Fügen Sie den Box-Container zum Fenster hinzu - gtk_container_add(GTK_CONTAINER(window), box); + gtk_window_set_child (GTK_WINDOW (window), box); + + gtk_window_present (GTK_WINDOW (window)); +} - // Zeigen Sie alle Widgets an - gtk_widget_show_all(window); - // Starten Sie die GTK+-Hauptschleife - gtk_main(); +int main (int argc, char **argv) +{ + GtkApplication *app; + int status; - return 0; -} + app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE); + g_signal_connect(app, "activate", G_CALLBACK (activate), NULL); + status = g_application_run (G_APPLICATION (app), argc, argv); + g_object_unref (app); + + return status; +} \ No newline at end of file diff --git a/makefile b/makefile index d9d1c9d..0f75365 100644 --- a/makefile +++ b/makefile @@ -12,27 +12,19 @@ HDRS = \ # Bibliotheken LIBS = \ - -lgtk3 + -lgtk4 # Objektdateien OBJS = $(SRCS:.c=.o) # Flagge für GTK+-Entwicklung -GTK3_CFLAGS = -Wall `pkg-config --cflags glib-2.0` \ - -I/usr/include/gtk-3.0 \ - -I/usr/include/glib-2.0 \ - -I/usr/include/pango-1.0 \ - -I/usr/include/harfbuzz \ - -I/usr/include/cairo \ - -I/usr/include/gdk-pixbuf-2.0 \ - -I/usr/include/atk-1.0 +GTK3_CFLAGS = -Wall `pkg-config --cflags gtk4` # Flagge für C-Compiler CFLAGS = $(GTK3_CFLAGS) # Flagge für Linker -LDFLAGS = `pkg-config --libs gtk+-3.0` -L/usr/local/lib -L/usr/lib -#LDFLAGS = `pkg-config --libs glib-2.0` -L/usr/local/lib -L/usr/lib +LDFLAGS = `pkg-config --libs gtk4` -L/usr/local/lib -L/usr/lib # Programmname PROGRAM = $(PROJECT) diff --git a/settings.json b/settings.json index c401ec2..6f4ac5a 100644 --- a/settings.json +++ b/settings.json @@ -1,5 +1,5 @@ { "c_cpp.includePath": [ - "/usr/include/gtk-3.0" // Ersetzen Sie diesen Pfad nach Bedarf + "/usr/include/gtk-4.0" // Ersetzen Sie diesen Pfad nach Bedarf ] } \ No newline at end of file