*: add source from drawing example and using gtk builder
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
parent
bfe6f06b5a
commit
ddfb728687
4 changed files with 296 additions and 0 deletions
12
Makefile
12
Makefile
|
@ -3,6 +3,18 @@ CFLAGS ?= -Wall -g $(shell pkg-config --cflags gtk4)
|
||||||
LDFLAGS ?= $(shell pkg-config --libs gtk4)
|
LDFLAGS ?= $(shell pkg-config --libs gtk4)
|
||||||
CLEANFILES :=
|
CLEANFILES :=
|
||||||
|
|
||||||
|
all: main example-3 example-4
|
||||||
|
|
||||||
|
example-3: example-3.c
|
||||||
|
gcc $(CFLAGS) -o $@ $< $(LDFLAGS)
|
||||||
|
|
||||||
|
CLEANFILES += example-3
|
||||||
|
|
||||||
|
example-4: example-4.c
|
||||||
|
gcc $(CFLAGS) -o $@ $< $(LDFLAGS)
|
||||||
|
|
||||||
|
CLEANFILES += example-4
|
||||||
|
|
||||||
main: main.c
|
main: main.c
|
||||||
gcc $(CFLAGS) -o $@ $< $(LDFLAGS)
|
gcc $(CFLAGS) -o $@ $< $(LDFLAGS)
|
||||||
|
|
||||||
|
|
38
builder.ui
Normal file
38
builder.ui
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<interface>
|
||||||
|
<object id="window" class="GtkWindow">
|
||||||
|
<property name="title">Grid</property>
|
||||||
|
<child>
|
||||||
|
<object id="grid" class="GtkGrid">
|
||||||
|
<child>
|
||||||
|
<object id="button1" class="GtkButton">
|
||||||
|
<property name="label">Button 1</property>
|
||||||
|
<layout>
|
||||||
|
<property name="column">0</property>
|
||||||
|
<property name="row">0</property>
|
||||||
|
</layout>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object id="button2" class="GtkButton">
|
||||||
|
<property name="label">Button 2</property>
|
||||||
|
<layout>
|
||||||
|
<property name="column">1</property>
|
||||||
|
<property name="row">0</property>
|
||||||
|
</layout>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object id="quit" class="GtkButton">
|
||||||
|
<property name="label">Quit</property>
|
||||||
|
<layout>
|
||||||
|
<property name="column">0</property>
|
||||||
|
<property name="row">1</property>
|
||||||
|
<property name="column-span">2</property>
|
||||||
|
</layout>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
</interface>
|
187
example-3.c
Normal file
187
example-3.c
Normal file
|
@ -0,0 +1,187 @@
|
||||||
|
#include <gtk/gtk.h>
|
||||||
|
|
||||||
|
/* Surface to store current scribbles */
|
||||||
|
static cairo_surface_t *surface = NULL;
|
||||||
|
|
||||||
|
static void
|
||||||
|
clear_surface (void)
|
||||||
|
{
|
||||||
|
cairo_t *cr;
|
||||||
|
|
||||||
|
cr = cairo_create (surface);
|
||||||
|
|
||||||
|
cairo_set_source_rgb (cr, 1, 1, 1);
|
||||||
|
cairo_paint (cr);
|
||||||
|
|
||||||
|
cairo_destroy (cr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Create a new surface of the appropriate size to store our scribbles */
|
||||||
|
static void
|
||||||
|
resize_cb (GtkWidget *widget,
|
||||||
|
int width,
|
||||||
|
int height,
|
||||||
|
gpointer data)
|
||||||
|
{
|
||||||
|
if (surface)
|
||||||
|
{
|
||||||
|
cairo_surface_destroy (surface);
|
||||||
|
surface = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (gtk_native_get_surface (gtk_widget_get_native (widget)))
|
||||||
|
{
|
||||||
|
surface = gdk_surface_create_similar_surface (gtk_native_get_surface (gtk_widget_get_native (widget)),
|
||||||
|
CAIRO_CONTENT_COLOR,
|
||||||
|
gtk_widget_get_width (widget),
|
||||||
|
gtk_widget_get_height (widget));
|
||||||
|
|
||||||
|
/* Initialize the surface to white */
|
||||||
|
clear_surface ();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Redraw the screen from the surface. Note that the draw
|
||||||
|
* callback receives a ready-to-be-used cairo_t that is already
|
||||||
|
* clipped to only draw the exposed areas of the widget
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
draw_cb (GtkDrawingArea *drawing_area,
|
||||||
|
cairo_t *cr,
|
||||||
|
int width,
|
||||||
|
int height,
|
||||||
|
gpointer data)
|
||||||
|
{
|
||||||
|
cairo_set_source_surface (cr, surface, 0, 0);
|
||||||
|
cairo_paint (cr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Draw a rectangle on the surface at the given position */
|
||||||
|
static void
|
||||||
|
draw_brush (GtkWidget *widget,
|
||||||
|
double x,
|
||||||
|
double y)
|
||||||
|
{
|
||||||
|
cairo_t *cr;
|
||||||
|
|
||||||
|
/* Paint to the surface, where we store our state */
|
||||||
|
cr = cairo_create (surface);
|
||||||
|
|
||||||
|
cairo_rectangle (cr, x - 3, y - 3, 6, 6);
|
||||||
|
cairo_fill (cr);
|
||||||
|
|
||||||
|
cairo_destroy (cr);
|
||||||
|
|
||||||
|
/* Now invalidate the drawing area. */
|
||||||
|
gtk_widget_queue_draw (widget);
|
||||||
|
}
|
||||||
|
|
||||||
|
static double start_x;
|
||||||
|
static double start_y;
|
||||||
|
|
||||||
|
static void
|
||||||
|
drag_begin (GtkGestureDrag *gesture,
|
||||||
|
double x,
|
||||||
|
double y,
|
||||||
|
GtkWidget *area)
|
||||||
|
{
|
||||||
|
start_x = x;
|
||||||
|
start_y = y;
|
||||||
|
|
||||||
|
draw_brush (area, x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
drag_update (GtkGestureDrag *gesture,
|
||||||
|
double x,
|
||||||
|
double y,
|
||||||
|
GtkWidget *area)
|
||||||
|
{
|
||||||
|
draw_brush (area, start_x + x, start_y + y);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
drag_end (GtkGestureDrag *gesture,
|
||||||
|
double x,
|
||||||
|
double y,
|
||||||
|
GtkWidget *area)
|
||||||
|
{
|
||||||
|
draw_brush (area, start_x + x, start_y + y);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
pressed (GtkGestureClick *gesture,
|
||||||
|
int n_press,
|
||||||
|
double x,
|
||||||
|
double y,
|
||||||
|
GtkWidget *area)
|
||||||
|
{
|
||||||
|
clear_surface ();
|
||||||
|
gtk_widget_queue_draw (area);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
close_window (void)
|
||||||
|
{
|
||||||
|
if (surface)
|
||||||
|
cairo_surface_destroy (surface);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
activate (GtkApplication *app,
|
||||||
|
gpointer user_data)
|
||||||
|
{
|
||||||
|
GtkWidget *window;
|
||||||
|
GtkWidget *frame;
|
||||||
|
GtkWidget *drawing_area;
|
||||||
|
GtkGesture *drag;
|
||||||
|
GtkGesture *press;
|
||||||
|
|
||||||
|
window = gtk_application_window_new (app);
|
||||||
|
gtk_window_set_title (GTK_WINDOW (window), "Drawing Area");
|
||||||
|
|
||||||
|
g_signal_connect (window, "destroy", G_CALLBACK (close_window), NULL);
|
||||||
|
|
||||||
|
frame = gtk_frame_new (NULL);
|
||||||
|
gtk_window_set_child (GTK_WINDOW (window), frame);
|
||||||
|
|
||||||
|
drawing_area = gtk_drawing_area_new ();
|
||||||
|
/* set a minimum size */
|
||||||
|
gtk_widget_set_size_request (drawing_area, 100, 100);
|
||||||
|
|
||||||
|
gtk_frame_set_child (GTK_FRAME (frame), drawing_area);
|
||||||
|
|
||||||
|
gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (drawing_area), draw_cb, NULL, NULL);
|
||||||
|
|
||||||
|
g_signal_connect_after (drawing_area, "resize", G_CALLBACK (resize_cb), NULL);
|
||||||
|
|
||||||
|
drag = gtk_gesture_drag_new ();
|
||||||
|
gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (drag), GDK_BUTTON_PRIMARY);
|
||||||
|
gtk_widget_add_controller (drawing_area, GTK_EVENT_CONTROLLER (drag));
|
||||||
|
g_signal_connect (drag, "drag-begin", G_CALLBACK (drag_begin), drawing_area);
|
||||||
|
g_signal_connect (drag, "drag-update", G_CALLBACK (drag_update), drawing_area);
|
||||||
|
g_signal_connect (drag, "drag-end", G_CALLBACK (drag_end), drawing_area);
|
||||||
|
|
||||||
|
press = gtk_gesture_click_new ();
|
||||||
|
gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (press), GDK_BUTTON_SECONDARY);
|
||||||
|
gtk_widget_add_controller (drawing_area, GTK_EVENT_CONTROLLER (press));
|
||||||
|
|
||||||
|
g_signal_connect (press, "pressed", G_CALLBACK (pressed), drawing_area);
|
||||||
|
|
||||||
|
gtk_window_present (GTK_WINDOW (window));
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main (int argc,
|
||||||
|
char **argv)
|
||||||
|
{
|
||||||
|
GtkApplication *app;
|
||||||
|
int status;
|
||||||
|
|
||||||
|
app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
|
||||||
|
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
|
||||||
|
status = g_application_run (G_APPLICATION (app), argc, argv);
|
||||||
|
g_object_unref (app);
|
||||||
|
|
||||||
|
return status;
|
||||||
|
}
|
59
example-4.c
Normal file
59
example-4.c
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
#include <gtk/gtk.h>
|
||||||
|
#include <glib/gstdio.h>
|
||||||
|
|
||||||
|
static void
|
||||||
|
print_hello (GtkWidget *widget,
|
||||||
|
gpointer data)
|
||||||
|
{
|
||||||
|
g_print ("Hello World\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
quit_cb (GtkWindow *window)
|
||||||
|
{
|
||||||
|
gtk_window_close (window);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
activate (GtkApplication *app,
|
||||||
|
gpointer user_data)
|
||||||
|
{
|
||||||
|
/* Construct a GtkBuilder instance and load our UI description */
|
||||||
|
GtkBuilder *builder = gtk_builder_new ();
|
||||||
|
gtk_builder_add_from_file (builder, "builder.ui", NULL);
|
||||||
|
|
||||||
|
/* Connect signal handlers to the constructed widgets. */
|
||||||
|
GObject *window = gtk_builder_get_object (builder, "window");
|
||||||
|
gtk_window_set_application (GTK_WINDOW (window), app);
|
||||||
|
|
||||||
|
GObject *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_swapped (button, "clicked", G_CALLBACK (quit_cb), window);
|
||||||
|
|
||||||
|
gtk_widget_set_visible (GTK_WIDGET (window), TRUE);
|
||||||
|
|
||||||
|
/* We do not need the builder any more */
|
||||||
|
g_object_unref (builder);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main (int argc,
|
||||||
|
char *argv[])
|
||||||
|
{
|
||||||
|
#ifdef GTK_SRCDIR
|
||||||
|
g_chdir (GTK_SRCDIR);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
GtkApplication *app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
|
||||||
|
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
|
||||||
|
|
||||||
|
int status = g_application_run (G_APPLICATION (app), argc, argv);
|
||||||
|
g_object_unref (app);
|
||||||
|
|
||||||
|
return status;
|
||||||
|
}
|
Loading…
Reference in a new issue