Skip to content
This repository was archived by the owner on Mar 18, 2022. It is now read-only.

Commit bacc3ef

Browse files
committed
added examples on c
1 parent 9c45de0 commit bacc3ef

25 files changed

+1219
-46
lines changed

example/c/cairo/Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
CC := clang
2+
3+
CFLAGS = `pkg-config --cflags gtk+-3.0`
4+
LIBS = `pkg-config --libs gtk+-3.0`
5+
6+
src = main.c
7+
obj = cairo_paint
8+
targets = cairo_paint
9+
10+
all: $(targets)
11+
12+
cairo_paint: $(src)
13+
$(CC) -o $(obj) $(src) $(CFLAGS) $(LIBS)
14+
15+
clean:
16+
rm -f $(targets) *.o *.*~

example/c/cairo/main.c

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#include <gtk/gtk.h>
2+
3+
static cairo_surface_t *s_cairo_surface = NULL;
4+
5+
static void clear_surface ()
6+
{
7+
cairo_t *cr = cairo_create (s_cairo_surface);
8+
9+
cairo_set_source_rgb (cr, 1, 1, 1);
10+
cairo_paint (cr);
11+
12+
cairo_destroy (cr);
13+
}
14+
15+
static void draw_brush (GtkWidget *widget, gdouble x, gdouble y)
16+
{
17+
#define BRUSH_SIZE 10
18+
19+
cairo_t *cr = cairo_create (s_cairo_surface);
20+
21+
cairo_rectangle (cr, x - BRUSH_SIZE / 2, y - BRUSH_SIZE / 2, BRUSH_SIZE, BRUSH_SIZE);
22+
cairo_fill (cr);
23+
cairo_destroy (cr);
24+
25+
gtk_widget_queue_draw_area (widget, x - BRUSH_SIZE / 2, y - BRUSH_SIZE / 2, BRUSH_SIZE, BRUSH_SIZE);
26+
}
27+
28+
static gboolean configure_event_cb (GtkWidget *widget, GdkEventConfigure *event, gpointer data)
29+
{
30+
if (s_cairo_surface != NULL)
31+
cairo_surface_destroy (s_cairo_surface);
32+
33+
s_cairo_surface = gdk_window_create_similar_surface (
34+
gtk_widget_get_window (widget),
35+
CAIRO_CONTENT_COLOR,
36+
gtk_widget_get_allocated_width (widget),
37+
gtk_widget_get_allocated_height (widget));
38+
39+
clear_surface ();
40+
41+
return TRUE;
42+
}
43+
44+
static gboolean cb_draw (GtkWidget *widget, cairo_t *cr, gpointer data)
45+
{
46+
cairo_set_source_surface (cr, s_cairo_surface, 0, 0);
47+
cairo_paint (cr);
48+
49+
return TRUE;
50+
}
51+
52+
static gboolean cb_button_press_event (GtkWidget *widget, GdkEventButton *event, gpointer data)
53+
{
54+
if (s_cairo_surface == NULL)
55+
return FALSE;
56+
57+
if (event->button == GDK_BUTTON_PRIMARY)
58+
draw_brush (widget, event->x, event->y);
59+
else if (event->button == GDK_BUTTON_SECONDARY) {
60+
clear_surface ();
61+
gtk_widget_queue_draw (widget);
62+
}
63+
64+
return TRUE;
65+
}
66+
67+
static gboolean cb_motion_notify_event (GtkWidget *widget, GdkEventMotion *event, gpointer data)
68+
{
69+
if (s_cairo_surface == NULL)
70+
return FALSE;
71+
72+
if (event->state & GDK_BUTTON1_MASK)
73+
draw_brush (widget, event->x, event->y);
74+
75+
return TRUE;
76+
}
77+
78+
static void cb_window_destory ()
79+
{
80+
if (s_cairo_surface != NULL)
81+
cairo_surface_destroy (s_cairo_surface);
82+
}
83+
84+
static void cb_application_activate (GtkApplication* app, gpointer user_data)
85+
{
86+
GtkBuilder* builder = gtk_builder_new ();
87+
g_return_if_fail (builder != NULL);
88+
89+
GError* error = NULL;
90+
if (!gtk_builder_add_from_file (builder, "./main.glade", &error)) {
91+
if (error) {
92+
g_error ("Failed to load: %s", error->message);
93+
g_error_free (error);
94+
return ;
95+
}
96+
}
97+
98+
GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (gtk_builder_get_object (builder, "application_window"));
99+
g_warn_if_fail (window != NULL);
100+
g_object_set (window, "application", app, NULL);
101+
gtk_window_set_title (GTK_WINDOW (window), "2D графика Краска с Каиром");
102+
g_signal_connect (window, "destroy", G_CALLBACK (cb_window_destory), NULL);
103+
104+
{
105+
GtkWidget *drawing_area = GTK_WIDGET (gtk_builder_get_object (builder, "drawing_area"));
106+
g_warn_if_fail (drawing_area != NULL);
107+
108+
g_signal_connect (drawing_area, "draw", G_CALLBACK (cb_draw), NULL);
109+
g_signal_connect (drawing_area, "configure-event", G_CALLBACK (configure_event_cb), NULL);
110+
g_signal_connect (drawing_area, "motion-notify-event", G_CALLBACK (cb_motion_notify_event), NULL);
111+
g_signal_connect (drawing_area, "button-press-event", G_CALLBACK (cb_button_press_event), NULL);
112+
113+
gtk_widget_set_events (drawing_area, gtk_widget_get_events (drawing_area)
114+
| GDK_BUTTON_PRESS_MASK
115+
| GDK_POINTER_MOTION_MASK);
116+
}
117+
118+
gtk_widget_show_all (GTK_WIDGET (window));
119+
g_object_unref (builder);
120+
}
121+
122+
int main (int argc, char **argv)
123+
{
124+
GtkApplication *app;
125+
int status;
126+
127+
app = gtk_application_new ("org.gtk3.cairo", G_APPLICATION_FLAGS_NONE);
128+
g_signal_connect (app, "activate", G_CALLBACK (cb_application_activate), NULL);
129+
status = g_application_run (G_APPLICATION (app), argc, argv);
130+
g_object_unref (app);
131+
132+
return status;
133+
}

example/c/cairo/main.glade

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- Generated with glade 3.22.1 -->
3+
<interface>
4+
<requires lib="gtk+" version="3.20"/>
5+
<object class="GtkApplicationWindow" id="application_window">
6+
<property name="can_focus">False</property>
7+
<property name="width_request">480</property>
8+
<property name="height_request">240</property>
9+
<child>
10+
<placeholder/>
11+
</child>
12+
<child>
13+
<object class="GtkBox">
14+
<property name="visible">True</property>
15+
<property name="can_focus">False</property>
16+
<property name="orientation">vertical</property>
17+
<child>
18+
<object class="GtkDrawingArea" id="drawing_area">
19+
<property name="visible">True</property>
20+
<property name="can_focus">False</property>
21+
</object>
22+
<packing>
23+
<property name="expand">True</property>
24+
<property name="fill">True</property>
25+
<property name="position">0</property>
26+
</packing>
27+
</child>
28+
</object>
29+
</child>
30+
</object>
31+
</interface>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <gtk/gtk.h>
2+
#include "GtkListBoxItem.h"
3+
4+
/**
5+
*[refs]: https://developer.gnome.org/gtk3/stable/GtkWidget.html
6+
* Building composite widgets from template XML
7+
*/
8+
G_DEFINE_TYPE_WITH_PRIVATE (GtkListBoxItem, gtk_listbox_item, GTK_TYPE_LIST_BOX_ROW);
9+
10+
static void gtk_listbox_item_class_init (GtkListBoxItemClass *klass)
11+
{
12+
gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS(klass),
13+
"/gtk/examples/custom-widget/GtkListBoxItem.glade");
14+
15+
gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS(klass),
16+
GtkListBoxItem, label_no);
17+
gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS(klass),
18+
GtkListBoxItem, label_body);
19+
}
20+
21+
static void gtk_listbox_item_init (GtkListBoxItem *self)
22+
{
23+
self->priv = (GtkListBoxItemPrivate *) gtk_listbox_item_get_instance_private (GTK_LISTBOX_ITEM (self));
24+
gtk_widget_init_template (GTK_WIDGET (self));
25+
}
26+
27+
GtkListBoxItem *gtk_listbox_item_new (gint index, const gchar *str)
28+
{
29+
GtkListBoxItem *row =
30+
GTK_LISTBOX_ITEM (g_object_new (gtk_listbox_item_get_type (), NULL));
31+
32+
g_warn_if_fail (row != NULL);
33+
g_warn_if_fail (row->priv != NULL);
34+
g_warn_if_fail (row->priv->label_body != NULL);
35+
36+
{
37+
gchar idx[16];
38+
memset (idx, 0x0, 16);
39+
sprintf (idx, "%d", index);
40+
gtk_label_set_text (row->priv->label_no, idx);
41+
}
42+
43+
gtk_label_set_text (row->priv->label_body, str);
44+
45+
return row;
46+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef __GTK_LISTBOX_ITEM_H__
2+
#define __GTK_LISTBOX_ITEM_H__
3+
4+
#include <gtk/gtk.h>
5+
6+
#define GTK_TYPE_LISTBOX_ITEM\
7+
(gtk_listbox_item_get_type())
8+
#define GTK_LISTBOX_ITEM(listbox_row)\
9+
(G_TYPE_CHECK_INSTANCE_CAST((listbox_row), GTK_TYPE_LISTBOX_ITEM, GtkListBoxItem))
10+
#define GTK_LISTBOX_ITEM_CLASS(klass)\
11+
(G_TYPE_CHECK_CLASS_CAST((klass), GTK_TYPE_LISTBOX_ITEM, GtkListBoxItemClass))
12+
13+
GType gtk_listbox_item_get_type (void) G_GNUC_CONST;
14+
15+
typedef struct {
16+
GtkListBoxRowClass parent_class;
17+
} GtkListBoxItemClass;
18+
19+
typedef struct {
20+
GtkLabel *label_no;
21+
GtkLabel *label_body;
22+
} GtkListBoxItemPrivate;
23+
24+
typedef struct {
25+
GtkListBoxRow parent;
26+
GtkListBoxItemPrivate *priv;
27+
} GtkListBoxItem;
28+
29+
extern GtkListBoxItem *gtk_listbox_item_new (gint index, const gchar *str);
30+
31+
#endif // __GTK_LISTBOX_ITEM_H__

example/c/custom-widget/Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
CC := clang
2+
3+
CFLAGS = `pkg-config --cflags gtk+-3.0`
4+
LIBS = `pkg-config --libs gtk+-3.0`
5+
6+
targets = custom_widget
7+
obj = custom_widget
8+
src = custom_widget.c GtkListBoxItem.c resources.c
9+
10+
all: $(targets)
11+
12+
custom_widget: $(src)
13+
$(CC) -o $(obj) $(src) $(CFLAGS) $(LIBS)
14+
15+
clean:
16+
rm -f $(targets) *.o *.*~
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <gtk/gtk.h>
2+
#include "GtkListBoxItem.h"
3+
4+
static gboolean cb_item_selected (GtkListBox *listbox, GtkListBoxItem *row, gpointer user_data)
5+
{
6+
if (row != NULL) {
7+
const gint index = gtk_list_box_row_get_index (GTK_LIST_BOX_ROW (row));
8+
g_print ("item selected -> No.%d %s\n", index, gtk_label_get_text (row->priv->label_body));
9+
}
10+
11+
return TRUE;
12+
}
13+
14+
static void cb_app_activate (GtkApplication* app, gpointer user_data)
15+
{
16+
GtkBuilder* builder = gtk_builder_new ();
17+
g_return_if_fail (builder != NULL);
18+
19+
GError* error = NULL;
20+
if (!gtk_builder_add_from_file (builder, "./res/main.glade", &error)) {
21+
if (error) {
22+
g_error ("Failed to load: %s", error->message);
23+
g_error_free (error);
24+
return ;
25+
}
26+
}
27+
28+
GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (gtk_builder_get_object (builder, "application_window"));
29+
g_warn_if_fail (window != NULL);
30+
g_object_set (window, "application", app, NULL);
31+
32+
GtkWidget *listbox = GTK_WIDGET (gtk_builder_get_object (builder, "listbox"));
33+
g_warn_if_fail (listbox != NULL);
34+
35+
g_signal_connect (listbox, "row-selected", G_CALLBACK (cb_item_selected), NULL);
36+
37+
for (int i = 0; i < 10; i++) {
38+
static const gchar items[][256] = {
39+
"суши",
40+
"стейк",
41+
"Карри и рис",
42+
"гамбургер",
43+
"Удон",
44+
"Жареная курица",
45+
"гречиха",
46+
"Жареный рис",
47+
"Жареный рис",
48+
"Миска с морепродуктами",
49+
};
50+
GtkListBoxItem *item = gtk_listbox_item_new (i, items[i]);
51+
gtk_container_add (GTK_CONTAINER (listbox), GTK_WIDGET (item));
52+
}
53+
54+
gtk_widget_show_all (GTK_WIDGET (window));
55+
g_object_unref (builder);
56+
}
57+
58+
int main (int argc, char **argv)
59+
{
60+
GtkApplication *app;
61+
int status;
62+
63+
app = gtk_application_new ("org.gtk3.custom-widget", G_APPLICATION_FLAGS_NONE);
64+
g_signal_connect (app, "activate", G_CALLBACK (cb_app_activate), NULL);
65+
status = g_application_run (G_APPLICATION (app), argc, argv);
66+
g_object_unref (app);
67+
68+
return status;
69+
}
70+

0 commit comments

Comments
 (0)