feat: add gtk3 examples
This commit is contained in:
parent
896aef45e2
commit
c284ea6baf
|
@ -0,0 +1,38 @@
|
||||||
|
#include <gtk/gtk.h>
|
||||||
|
|
||||||
|
static void activate (GtkApplication* app, gpointer user_data)
|
||||||
|
{
|
||||||
|
GtkWidget *window;
|
||||||
|
|
||||||
|
/*create new object and store it at the pointer*/
|
||||||
|
window = gtk_application_window_new (app);
|
||||||
|
|
||||||
|
/*set window title*/
|
||||||
|
/*cast pointer using the macro GTK_WINDOW() instead of C cast*/
|
||||||
|
gtk_window_set_title (GTK_WINDOW (window), "Window Title");
|
||||||
|
|
||||||
|
/*set window size*/
|
||||||
|
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
|
||||||
|
|
||||||
|
/*make window visible*/
|
||||||
|
gtk_widget_show_all (window);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*create a GtkApplication object and run it*/
|
||||||
|
int main (int argc, char **argv)
|
||||||
|
{
|
||||||
|
GtkApplication *app;
|
||||||
|
int status;
|
||||||
|
|
||||||
|
/*initialise pointer with app id (name) and without flags*/
|
||||||
|
app = gtk_application_new ("de.swingbe.gtk.example0", G_APPLICATION_FLAGS_NONE);
|
||||||
|
|
||||||
|
/*connect the active signal with the active function defined above*/
|
||||||
|
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
|
||||||
|
|
||||||
|
/*launch app and pass through arguments*/
|
||||||
|
status = g_application_run (G_APPLICATION (app), argc, argv);
|
||||||
|
g_object_unref (app);
|
||||||
|
|
||||||
|
return status;
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
CC ?= gcc
|
||||||
|
PKGCONFIG = $(shell which pkg-config)
|
||||||
|
CFLAGS = $(shell $(PKGCONFIG) --cflags gtk+-3.0)
|
||||||
|
LIBS = $(shell $(PKGCONFIG) --libs gtk+-3.0)
|
||||||
|
|
||||||
|
SRC = exampleapp.c
|
||||||
|
|
||||||
|
OBJS = $(SRC:.c=.o)
|
||||||
|
|
||||||
|
all: exampleapp
|
||||||
|
|
||||||
|
%.o: %.c
|
||||||
|
|
||||||
|
$(CC) -c -o $(@F) $(CFLAGS) $<
|
||||||
|
|
||||||
|
exampleapp: $(OBJS)
|
||||||
|
|
||||||
|
$(CC) -o $(@F) $(OBJS) $(LIBS)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
|
||||||
|
rm -f $(OBJS)
|
||||||
|
rm -f exampleapp
|
|
@ -0,0 +1,5 @@
|
||||||
|
* build
|
||||||
|
|
||||||
|
```
|
||||||
|
gcc `pkg-config --cflags gtk+-3.0` -o example-1 example-1.c `pkg-config --libs gtk+-3.0`
|
||||||
|
```
|
|
@ -0,0 +1,58 @@
|
||||||
|
#include <gtk/gtk.h>
|
||||||
|
|
||||||
|
static void print_hello (GtkWidget *widget,
|
||||||
|
gpointer data)
|
||||||
|
{
|
||||||
|
/*print in a terminal*/
|
||||||
|
g_print ("Clicked\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
activate (GtkApplication *app,
|
||||||
|
gpointer user_data)
|
||||||
|
{
|
||||||
|
GtkWidget *window;
|
||||||
|
GtkWidget *button;
|
||||||
|
|
||||||
|
/*controlling button size and layout by storing it inside a box*/
|
||||||
|
GtkWidget *button_box;
|
||||||
|
|
||||||
|
window = gtk_application_window_new (app);
|
||||||
|
gtk_window_set_title (GTK_WINDOW (window), "Title");
|
||||||
|
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
|
||||||
|
|
||||||
|
/*create and assigen box with orientation*/
|
||||||
|
button_box = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
|
||||||
|
|
||||||
|
/*add button box to window*/
|
||||||
|
gtk_container_add (GTK_CONTAINER (window), button_box);
|
||||||
|
|
||||||
|
/*initialise button*/
|
||||||
|
button = gtk_button_new_with_label ("Button");
|
||||||
|
|
||||||
|
/*connect button with callback function with NULL as input*/
|
||||||
|
g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
|
||||||
|
|
||||||
|
/*connect button with destroy callback to destroy the window instead of the button*/
|
||||||
|
g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
|
||||||
|
|
||||||
|
/*add button to window*/
|
||||||
|
gtk_container_add (GTK_CONTAINER (button_box), button);
|
||||||
|
|
||||||
|
gtk_widget_show_all (window);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main (int argc,
|
||||||
|
char **argv)
|
||||||
|
{
|
||||||
|
GtkApplication *app;
|
||||||
|
int status;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
CC ?= gcc
|
||||||
|
PKGCONFIG = $(shell which pkg-config)
|
||||||
|
CFLAGS = $(shell $(PKGCONFIG) --cflags gtk+-3.0)
|
||||||
|
LIBS = $(shell $(PKGCONFIG) --libs gtk+-3.0)
|
||||||
|
|
||||||
|
SRC = exampleapp.c
|
||||||
|
|
||||||
|
OBJS = $(SRC:.c=.o)
|
||||||
|
|
||||||
|
all: exampleapp
|
||||||
|
|
||||||
|
%.o: %.c
|
||||||
|
|
||||||
|
$(CC) -c -o $(@F) $(CFLAGS) $<
|
||||||
|
|
||||||
|
exampleapp: $(OBJS)
|
||||||
|
|
||||||
|
$(CC) -o $(@F) $(OBJS) $(LIBS)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
|
||||||
|
rm -f $(OBJS)
|
||||||
|
rm -f exampleapp
|
|
@ -0,0 +1,5 @@
|
||||||
|
* build
|
||||||
|
|
||||||
|
```
|
||||||
|
gcc `pkg-config --cflags gtk+-3.0` -o example-1 example-1.c `pkg-config --libs gtk+-3.0`
|
||||||
|
```
|
|
@ -0,0 +1,75 @@
|
||||||
|
#include <gtk/gtk.h>
|
||||||
|
|
||||||
|
static void
|
||||||
|
print_hello (GtkWidget *widget,
|
||||||
|
gpointer data)
|
||||||
|
{
|
||||||
|
g_print ("Hello World\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
activate (GtkApplication *app,
|
||||||
|
gpointer user_data)
|
||||||
|
{
|
||||||
|
GtkWidget *window;
|
||||||
|
GtkWidget *grid;
|
||||||
|
GtkWidget *button;
|
||||||
|
|
||||||
|
/* create a new window, and set its title */
|
||||||
|
window = gtk_application_window_new (app);
|
||||||
|
gtk_window_set_title (GTK_WINDOW (window), "Window");
|
||||||
|
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
|
||||||
|
|
||||||
|
/* Here we construct the container that is going pack our buttons */
|
||||||
|
grid = gtk_grid_new ();
|
||||||
|
|
||||||
|
/* Pack the container in the window */
|
||||||
|
gtk_container_add (GTK_CONTAINER (window), grid);
|
||||||
|
|
||||||
|
button = gtk_button_new_with_label ("Button 1");
|
||||||
|
g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
|
||||||
|
|
||||||
|
/* Place the first button in the grid cell (0, 0), and make it fill
|
||||||
|
* just 1 cell horizontally and vertically (ie no spanning)
|
||||||
|
*/
|
||||||
|
gtk_grid_attach (GTK_GRID (grid), button, 0, 0, 1, 1);
|
||||||
|
|
||||||
|
button = gtk_button_new_with_label ("Button 2");
|
||||||
|
g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
|
||||||
|
|
||||||
|
/* Place the second button in the grid cell (1, 0), and make it fill
|
||||||
|
* just 1 cell horizontally and vertically (ie no spanning)
|
||||||
|
*/
|
||||||
|
gtk_grid_attach (GTK_GRID (grid), button, 1, 0, 1, 1);
|
||||||
|
|
||||||
|
button = gtk_button_new_with_label ("Quit");
|
||||||
|
g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
|
||||||
|
|
||||||
|
/* Place the Quit button in the grid cell (0, 1), and make it
|
||||||
|
* span 2 columns.
|
||||||
|
*/
|
||||||
|
gtk_grid_attach (GTK_GRID (grid), button, 0, 1, 2, 1);
|
||||||
|
|
||||||
|
/* Now that we are done packing our widgets, we show them all
|
||||||
|
* in one go, by calling gtk_widget_show_all() on the window.
|
||||||
|
* This call recursively calls gtk_widget_show() on all widgets
|
||||||
|
* that are contained in the window, directly or indirectly.
|
||||||
|
*/
|
||||||
|
gtk_widget_show_all (window);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main (int argc,
|
||||||
|
char **argv)
|
||||||
|
{
|
||||||
|
GtkApplication *app;
|
||||||
|
int status;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
CC ?= gcc
|
||||||
|
PKGCONFIG = $(shell which pkg-config)
|
||||||
|
CFLAGS = $(shell $(PKGCONFIG) --cflags gtk+-3.0)
|
||||||
|
LIBS = $(shell $(PKGCONFIG) --libs gtk+-3.0)
|
||||||
|
|
||||||
|
SRC = exampleapp.c
|
||||||
|
|
||||||
|
OBJS = $(SRC:.c=.o)
|
||||||
|
|
||||||
|
all: exampleapp
|
||||||
|
|
||||||
|
%.o: %.c
|
||||||
|
|
||||||
|
$(CC) -c -o $(@F) $(CFLAGS) $<
|
||||||
|
|
||||||
|
exampleapp: $(OBJS)
|
||||||
|
|
||||||
|
$(CC) -o $(@F) $(OBJS) $(LIBS)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
|
||||||
|
rm -f $(OBJS)
|
||||||
|
rm -f exampleapp
|
|
@ -0,0 +1,5 @@
|
||||||
|
* build
|
||||||
|
|
||||||
|
```
|
||||||
|
gcc `pkg-config --cflags gtk+-3.0` -o example-2 example-2.c `pkg-config --libs gtk+-3.0`
|
||||||
|
```
|
|
@ -0,0 +1,45 @@
|
||||||
|
<interface>
|
||||||
|
<object id="window" class="GtkWindow">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="title">Grid</property>
|
||||||
|
<property name="border-width">10</property>
|
||||||
|
<child>
|
||||||
|
<object id="grid" class="GtkGrid">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<child>
|
||||||
|
<object id="button1" class="GtkButton">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="label">Button 1</property>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="left-attach">0</property>
|
||||||
|
<property name="top-attach">0</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object id="button2" class="GtkButton">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="label">Button 2</property>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="left-attach">1</property>
|
||||||
|
<property name="top-attach">0</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object id="quit" class="GtkButton">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="label">Quit</property>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="left-attach">0</property>
|
||||||
|
<property name="top-attach">1</property>
|
||||||
|
<property name="width">2</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
</interface>
|
|
@ -0,0 +1,46 @@
|
||||||
|
#include <gtk/gtk.h>
|
||||||
|
|
||||||
|
static void
|
||||||
|
print_hello (GtkWidget *widget,
|
||||||
|
gpointer data)
|
||||||
|
{
|
||||||
|
g_print ("Hello World\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main (int argc,
|
||||||
|
char *argv[])
|
||||||
|
{
|
||||||
|
GtkBuilder *builder;
|
||||||
|
GObject *window;
|
||||||
|
GObject *button;
|
||||||
|
GError *error = NULL;
|
||||||
|
|
||||||
|
gtk_init (&argc, &argv);
|
||||||
|
|
||||||
|
/* Construct a GtkBuilder instance and load our UI description */
|
||||||
|
builder = gtk_builder_new ();
|
||||||
|
if (gtk_builder_add_from_file (builder, "builder.ui", &error) == 0)
|
||||||
|
{
|
||||||
|
g_printerr ("Error loading file: %s\n", error->message);
|
||||||
|
g_clear_error (&error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Connect signal handlers to the constructed widgets. */
|
||||||
|
window = gtk_builder_get_object (builder, "window");
|
||||||
|
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
|
||||||
|
|
||||||
|
button = gtk_builder_get_object (builder, "button1");
|
||||||
|
g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
|
||||||
|
|
||||||
|
button = gtk_builder_get_object (builder, "button2");
|
||||||
|
g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
|
||||||
|
|
||||||
|
button = gtk_builder_get_object (builder, "quit");
|
||||||
|
g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL);
|
||||||
|
|
||||||
|
gtk_main ();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
CC ?= gcc
|
||||||
|
PKGCONFIG = $(shell which pkg-config)
|
||||||
|
CFLAGS = $(shell $(PKGCONFIG) --cflags gtk+-3.0)
|
||||||
|
LIBS = $(shell $(PKGCONFIG) --libs gtk+-3.0)
|
||||||
|
|
||||||
|
SRC = exampleapp.c
|
||||||
|
|
||||||
|
OBJS = $(SRC:.c=.o)
|
||||||
|
|
||||||
|
all: exampleapp
|
||||||
|
|
||||||
|
%.o: %.c
|
||||||
|
|
||||||
|
$(CC) -c -o $(@F) $(CFLAGS) $<
|
||||||
|
|
||||||
|
exampleapp: $(OBJS)
|
||||||
|
|
||||||
|
$(CC) -o $(@F) $(OBJS) $(LIBS)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
|
||||||
|
rm -f $(OBJS)
|
||||||
|
rm -f exampleapp
|
|
@ -0,0 +1,7 @@
|
||||||
|
* build manually
|
||||||
|
|
||||||
|
```
|
||||||
|
gcc `pkg-config --cflags gtk+-3.0` -o example-3 example-3.c `pkg-config --libs gtk+-3.0`
|
||||||
|
```
|
||||||
|
|
||||||
|
* or build using ```make``` instruction
|
Loading…
Reference in New Issue