-
Notifications
You must be signed in to change notification settings - Fork 1
/
gtktest.c
203 lines (162 loc) · 5.19 KB
/
gtktest.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <gtk/gtk.h>
#include <math.h>
#include <windows.h>
typedef struct _components {
GtkWidget *window;
GtkWidget *previous;
GtkWidget *next;
GtkWidget *graphNavigationPanel;
GtkWidget *mainPanel;
GtkWidget *drawingArea;
GdkPixmap *pixMap;
} components;
static components this;
static void next_handler( GtkWidget *widget,
gpointer data )
{
g_print ("next %s \n", (gchar *) data);
}
static void previous_handler( GtkWidget *widget,
gpointer data )
{
g_print ("previous %s \n", (gchar *) data);
}
static gboolean delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
return FALSE;
}
/* Another callback */
static void destroy( GtkWidget *widget,
gpointer data )
{
gtk_main_quit ();
}
static void createWindow() {
this.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_container_set_border_width (GTK_CONTAINER (this.window), 10);
gtk_window_set_title (GTK_WINDOW (this.window), "Graph Window");
g_signal_connect (G_OBJECT (this.window), "delete_event",
G_CALLBACK (delete_event), NULL);
g_signal_connect (G_OBJECT (this.window), "destroy",
G_CALLBACK (destroy), NULL);
}
static void layoutWidgets() {
/* Create the graph navigation panel and add it to the window. */
this.graphNavigationPanel = gtk_hbox_new (FALSE, 0);
this.mainPanel = gtk_vbox_new(FALSE, 0);
gtk_container_add (GTK_CONTAINER (this.window), this.mainPanel);
/* Add the draw-able area to the main panel. */
gtk_box_pack_start (GTK_BOX(this.mainPanel), this.drawingArea, TRUE, TRUE, 0);
}
static void show() {
gtk_widget_show (this.drawingArea);
gtk_widget_show (this.mainPanel);
gtk_widget_show (this.window);
}
/* Draw a dot */
static void draw_brush (GtkWidget *widget, gdouble x, gdouble y)
{
GdkRectangle update_rect;
update_rect.x = x - 2;
update_rect.y = y - 2;
update_rect.width = 4;
update_rect.height = 4;
gdk_draw_arc (this.pixMap,
widget->style->black_gc,
TRUE,
update_rect.x, update_rect.y,
update_rect.width, update_rect.height,
10,
10);
gtk_widget_queue_draw_area (widget,
update_rect.x, update_rect.y,
update_rect.width,
update_rect.height);
}
/* Redraw the screen from the backing pixmap */
static gboolean expose_event( GtkWidget *widget, GdkEventExpose *event )
{
gdk_draw_drawable(widget->window,
widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
this.pixMap,
event->area.x, event->area.y,
event->area.x, event->area.y,
event->area.width, event->area.height);
Sleep(50);
#include "drawthis.c"
return FALSE;
}
/* Create a new backing pixmap of the appropriate size */
static gboolean configure_event( GtkWidget *widget, GdkEventConfigure *event )
{
if (this.pixMap)
g_object_unref(this.pixMap);
this.pixMap = gdk_pixmap_new(widget->window,
widget->allocation.width,
widget->allocation.height,
-1);
gdk_draw_rectangle (this.pixMap,
widget->style->white_gc,
TRUE,
0, 0,
widget->allocation.width,
widget->allocation.height);
return TRUE;
}
static gboolean button_press_event( GtkWidget *widget, GdkEventButton *event )
{
//do nada
//if (event->button == 1 && this.pixMap != NULL) {
//draw_brush (widget, (event->x)-50, (event->y)-50);
//}
return TRUE;
}
static gboolean
motion_notify_event( GtkWidget *widget, GdkEventMotion *event )
{
//do nix
/*
int x, y;
GdkModifierType state;
if (event->is_hint)
gdk_window_get_pointer (event->window, &x, &y, &state);
else
{
x = event->x;
y = event->y;
state = event->state;
}
if (state & GDK_BUTTON1_MASK && this.pixMap != NULL)
draw_brush (widget, x+50, y+50);
*/
return TRUE;
}
static void createDrawingArea() {
this.drawingArea = gtk_drawing_area_new();
gtk_signal_connect (GTK_OBJECT (this.drawingArea), "expose_event",
(GtkSignalFunc) expose_event, NULL);
gtk_signal_connect (GTK_OBJECT(this.drawingArea),"configure_event",
(GtkSignalFunc) configure_event, NULL);
gtk_signal_connect (GTK_OBJECT (this.drawingArea), "motion_notify_event",
(GtkSignalFunc) motion_notify_event, NULL);
gtk_signal_connect (GTK_OBJECT (this.drawingArea), "button_press_event",
(GtkSignalFunc) button_press_event, NULL);
gtk_widget_set_events (this.drawingArea, GDK_EXPOSURE_MASK
| GDK_LEAVE_NOTIFY_MASK
| GDK_BUTTON_PRESS_MASK
| GDK_POINTER_MOTION_MASK
| GDK_POINTER_MOTION_HINT_MASK);
gtk_widget_set_size_request(this.drawingArea, 800, 500);
}
int main( int argc,char *argv[])
{
gtk_init (&argc, &argv);
createWindow();
createDrawingArea();
layoutWidgets();
show();
gtk_main ();
return 0;
}