-
Notifications
You must be signed in to change notification settings - Fork 1
/
grid.c
78 lines (63 loc) · 4.07 KB
/
grid.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <gtk/gtk.h>
//compile with gcc -Wall -g grid.c -o grid `pkg-config --cflags --libs gtk+-3.0`
void main_callback(GtkWidget *button, void *callback[]); //these are just the decalarations for the user functions
void button1_callback(); // we will user later in the program there's one for the main callback switch
void button2_callback(); // then one for each button
void button3_callback();
void button4_callback();
int main(int argc, char *argv[])
{
GtkWidget *window; //this declares windows as type GtkWidget*
GtkWidget *grid;
GtkWidget *button;
gtk_init(&argc, &argv); //this initializes gtk
window = gtk_window_new(GTK_WINDOW_TOPLEVEL); //gtk_window_new() sets the widget to type GtkWindow* it takes one parameter which is the window type.
//the 2 window types are GTK_WINDOW_TOPLEVEL and GTK_WINDOW_POPUP.
gtk_window_set_title(GTK_WINDOW(window), "New Window"); //sets a window title. gtk_window_set_title takes the 2 parameters,
//GtkWindow *window and const gchar *title
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); //opens the window in the center of the screen, this function takes 2 parameters,
//the parameters are GtkWindow* and GtkWindowPosition.
gtk_container_set_border_width(GTK_CONTAINER(window), 5); //sets the border width of the window, this takes 2 parameters, the GtkWidget* and the width respectively.
g_signal_connect(G_OBJECT(window), "destroy",G_CALLBACK(gtk_main_quit), NULL); //this line add the callback for the window to be closed when you click the x in the corner
//the function g_signal_connect is defined as g_signal_connect(instance, detailed_signal, c_handler, data)
//you will see more examples of this in coming tutorials
//BUTTONS =D The following for loops are for creating simple buttons
int position = 0;
gchar *buttonarray[4] = {"Button1", "Button2", "Button3", "Button4"}; //array for the button labels
void *callback[] = {button1_callback, button2_callback, button3_callback, button4_callback}; // void pointer array for calling the callbacks
grid = gtk_grid_new(); //sets the table widget as a 4x4 arrangement without autosizing
for (int i=0; i < 2; i++) //for loop for the rows
{
for (int j=0; j < 2; j++) //for loop for the columns
{
button = gtk_button_new_with_label(buttonarray[position]); //sets each button label to the respective button
gtk_grid_attach(GTK_GRID(grid), button, j, i, 1, 1); //sets the defaults for creating each table button
gtk_widget_set_size_request(button, 40, 40); //sets the size of the buttons
main_callback(button, callback[position]); //attaches the button to the respective callback
position++;
}
}
gtk_container_add(GTK_CONTAINER(window), grid); //adds the grid to the window
gtk_widget_show_all(window); //shows all widgets within the GtkWidget *window; this function takes one parameter with the data type GtkWidget
gtk_main(); //gtk main is a void function, this is the main loop of GTK.
}
void main_callback(GtkWidget *button, void *callback[]) //this function is used to attach the callbacks to their respective buttons
{
g_signal_connect(button, "clicked", G_CALLBACK(callback), NULL);//these lines add the callback for the window to be closed when you click the x in the corner
}
void button1_callback() //these functions are void functions, they just print which button is which on the console. g_print is essentially printf.
{
g_print("This is button 1\n");
}
void button2_callback()
{
g_print("This is button 2\n");
}
void button3_callback()
{
g_print("This is button 3\n");
}
void button4_callback()
{
g_print("This is button 4\n");
}