Skip to content

Commit

Permalink
change to Gtk4 (#2) (#5)
Browse files Browse the repository at this point in the history
* Gtk4 final

---------

Co-authored-by: marsalien7
Co-authored-by: Martin
  • Loading branch information
marsman7 authored Mar 27, 2024
1 parent 4a32314 commit 343c0dd
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 63 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
8 changes: 8 additions & 0 deletions GITHELP.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# GIT Help

Marker : feature-keybord branch

Noch eine Änderung

## Strategie



## First steps

git init
Expand Down
116 changes: 67 additions & 49 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,85 +1,103 @@
#include <gtk/gtk.h>
// 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;
}
14 changes: 3 additions & 11 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion settings.json
Original file line number Diff line number Diff line change
@@ -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
]
}

0 comments on commit 343c0dd

Please sign in to comment.