fa2c4dbe |
1 | |
f0d936c0 |
2 | #include "Drawing.h" |
76a67e8a |
3 | #include <gtk/gtk.h> |
4 | #include <gdk/gdk.h> |
f0d936c0 |
5 | |
6 | /***************************************************************************** |
7 | * Drawing functions * |
8 | *****************************************************************************/ |
9 | |
10 | typedef enum |
11 | { |
12 | RED, |
13 | GREEN, |
14 | BLUE, |
15 | WHITE, |
16 | BLACK |
17 | |
18 | } ControlFlowColors; |
19 | |
20 | /* Vector of unallocated colors */ |
21 | static GdkColor CF_Colors [] = |
22 | { |
23 | { 0, 0xffff, 0x0000, 0x0000 }, // RED |
24 | { 0, 0x0000, 0xffff, 0x0000 }, // GREEN |
25 | { 0, 0x0000, 0x0000, 0xffff }, // BLUE |
26 | { 0, 0xffff, 0xffff, 0xffff }, // WHITE |
27 | { 0, 0x0000, 0x0000, 0x0000 } // BLACK |
28 | }; |
29 | |
30 | |
76a67e8a |
31 | struct _Drawing_t { |
32 | GtkWidget *Drawing_Area_V; |
33 | GdkPixmap *Pixmap; |
f0d936c0 |
34 | |
76a67e8a |
35 | gint height, width, depth; |
f0d936c0 |
36 | |
76a67e8a |
37 | }; |
f0d936c0 |
38 | |
847b479d |
39 | void test_draw(Drawing_t *Drawing) |
40 | { |
41 | GdkRectangle update_rect; |
42 | // GdkColor color = { 0, 65535, 65535, 65535 }; |
f0d936c0 |
43 | |
847b479d |
44 | // gdk_colormap_alloc_color(gdk_rgb_get_cmap(), &color, 0, 1); |
45 | |
46 | // GdkGC *gc = |
47 | // Drawing->Drawing_Area_V-> |
48 | // style->fg_gc[GTK_WIDGET_STATE (Drawing->Drawing_Area_V)]; |
49 | // gdk_gc_set_foreground(gc, &color); |
50 | update_rect.x = 50; |
51 | update_rect.y = 50; |
52 | update_rect.width = 1000; |
53 | update_rect.height = 1000; |
54 | gdk_draw_rectangle (Drawing->Pixmap, |
55 | Drawing->Drawing_Area_V->style->black_gc, |
56 | TRUE, |
57 | 50, 50, |
58 | 1000, |
59 | 1000); |
60 | |
61 | |
62 | //Drawing_draw_line(Drawing, 10, 10, 50, 10, |
63 | // Drawing->Drawing_Area_V->style->black_gc); |
64 | gtk_widget_draw (Drawing->Drawing_Area_V, &update_rect); |
65 | |
66 | // Drawing_Refresh( Drawing, 0, 0, 30, 30); |
67 | } |
68 | |
69 | void Drawing_Data_Request(Drawing_t *Drawing, |
70 | GdkPixmap *Pixmap, |
71 | gint x, gint y, |
72 | gint width, |
73 | gint height) |
74 | { |
75 | gdk_draw_rectangle (Pixmap, |
76 | Drawing->Drawing_Area_V->style->white_gc, |
77 | TRUE, |
78 | x, y, |
79 | width, // do not overlap |
80 | height); |
81 | |
82 | Drawing_draw_line(Drawing, Pixmap, 10, 10, 50, 10, |
83 | Drawing->Drawing_Area_V->style->black_gc); |
84 | |
85 | } |
86 | |
87 | /* Callbacks */ |
88 | |
89 | |
90 | /* Create a new backing pixmap of the appropriate size */ |
91 | static gboolean |
92 | configure_event( GtkWidget *widget, GdkEventConfigure *event, |
93 | gpointer user_data) |
f0d936c0 |
94 | { |
847b479d |
95 | Drawing_t *Drawing = (Drawing_t*)user_data; |
f0d936c0 |
96 | |
847b479d |
97 | GdkPixmap *Pixmap = gdk_pixmap_new(widget->window, |
98 | widget->allocation.width, |
99 | widget->allocation.height, |
100 | -1); |
101 | |
102 | if(Drawing->Pixmap == NULL) |
103 | { |
104 | Drawing->Pixmap = gdk_pixmap_new(widget->window, |
105 | widget->allocation.width, |
106 | widget->allocation.height, |
107 | -1); |
108 | Drawing->width = widget->allocation.width; |
109 | Drawing->height = widget->allocation.height; |
110 | |
111 | /* Initial data request */ |
112 | Drawing_Data_Request(Drawing, Drawing->Pixmap, 0, 0, |
113 | widget->allocation.width, |
114 | widget->allocation.height); |
115 | |
116 | } |
117 | // /* Draw empty background */ |
118 | // gdk_draw_rectangle (Pixmap, |
119 | // widget->style->black_gc, |
120 | // TRUE, |
121 | // 0, 0, |
122 | // widget->allocation.width, |
123 | // widget->allocation.height); |
124 | |
125 | /* Copy old data to new pixmap */ |
126 | gdk_draw_drawable (Pixmap, |
127 | widget->style->white_gc, |
128 | Drawing->Pixmap, |
129 | 0, 0, |
130 | 0, 0, |
131 | -1, -1); |
132 | |
133 | /* Request data for missing space */ |
134 | Drawing_Data_Request(Drawing, Pixmap, Drawing->width, 0, |
135 | widget->allocation.width - Drawing->width, |
136 | widget->allocation.height); |
137 | Drawing_Data_Request(Drawing, Pixmap, 0, Drawing->height, |
138 | Drawing->width, |
139 | widget->allocation.height - Drawing->height); |
140 | |
141 | // gdk_draw_rectangle (Pixmap, |
142 | // widget->style->white_gc, |
143 | // TRUE, |
144 | // Drawing->width, 0, |
145 | // widget->allocation.width - |
146 | // Drawing->width, |
147 | // widget->allocation.height); |
148 | |
149 | // gdk_draw_rectangle (Pixmap, |
150 | // widget->style->white_gc, |
151 | // TRUE, |
152 | // 0, Drawing->height, |
153 | // Drawing->width, // do not overlap |
154 | // widget->allocation.height - |
155 | // Drawing->height); |
156 | |
157 | |
158 | |
159 | g_critical("drawing configure event"); |
160 | |
161 | |
162 | if (Drawing->Pixmap) |
163 | gdk_pixmap_unref(Drawing->Pixmap); |
164 | |
165 | Drawing->Pixmap = Pixmap; |
166 | Drawing->width = widget->allocation.width; |
167 | Drawing->height = widget->allocation.height; |
168 | |
169 | return TRUE; |
170 | } |
171 | |
172 | |
173 | /* Redraw the screen from the backing pixmap */ |
174 | static gboolean |
175 | expose_event( GtkWidget *widget, GdkEventExpose *event, gpointer user_data ) |
176 | { |
177 | Drawing_t *Drawing = (Drawing_t*)user_data; |
178 | g_critical("drawing expose event"); |
179 | |
180 | gdk_draw_pixmap(widget->window, |
181 | widget->style->fg_gc[GTK_WIDGET_STATE (widget)], |
182 | Drawing->Pixmap, |
183 | event->area.x, event->area.y, |
184 | event->area.x, event->area.y, |
185 | event->area.width, event->area.height); |
186 | |
187 | return FALSE; |
188 | } |
189 | |
190 | Drawing_t *Drawing_construct(void) |
191 | { |
76a67e8a |
192 | Drawing_t *Drawing = g_new(Drawing_t, 1); |
f0d936c0 |
193 | |
194 | Drawing->Drawing_Area_V = gtk_drawing_area_new (); |
847b479d |
195 | |
196 | //gtk_widget_set_size_request(Drawing->Drawing_Area_V->window, 50, 50); |
f0d936c0 |
197 | g_object_set_data_full( |
198 | G_OBJECT(Drawing->Drawing_Area_V), |
76a67e8a |
199 | "Link_Drawing_Data", |
f0d936c0 |
200 | Drawing, |
fa2c4dbe |
201 | (GDestroyNotify)Drawing_destroy); |
f0d936c0 |
202 | |
847b479d |
203 | //gtk_widget_modify_bg( Drawing->Drawing_Area_V, |
204 | // GTK_STATE_NORMAL, |
205 | // &CF_Colors[BLACK]); |
206 | |
207 | //gdk_window_get_geometry(Drawing->Drawing_Area_V->window, |
208 | // NULL, NULL, |
209 | // &(Drawing->width), |
210 | // &(Drawing->height), |
211 | // -1); |
212 | |
213 | //Drawing->Pixmap = gdk_pixmap_new( |
214 | // Drawing->Drawing_Area_V->window, |
215 | // Drawing->width, |
216 | // Drawing->height, |
217 | // Drawing->depth); |
218 | |
219 | Drawing->Pixmap = NULL; |
220 | |
221 | // Drawing->Pixmap = gdk_pixmap_new(Drawing->Drawing_Area_V->window, |
222 | // Drawing->Drawing_Area_V->allocation.width, |
223 | // Drawing->Drawing_Area_V->allocation.height, |
224 | // -1); |
225 | |
226 | |
227 | g_signal_connect (G_OBJECT(Drawing->Drawing_Area_V), |
228 | "configure_event", |
229 | G_CALLBACK (configure_event), |
230 | (gpointer)Drawing); |
231 | |
232 | g_signal_connect (G_OBJECT(Drawing->Drawing_Area_V), |
233 | "expose_event", |
234 | G_CALLBACK (expose_event), |
235 | (gpointer)Drawing); |
236 | |
f0d936c0 |
237 | return Drawing; |
238 | } |
239 | |
240 | void Drawing_destroy(Drawing_t *Drawing) |
241 | { |
242 | |
76a67e8a |
243 | // Do not unref here, Drawing_t destroyed by it's widget. |
244 | //g_object_unref( G_OBJECT(Drawing->Drawing_Area_V)); |
f0d936c0 |
245 | |
246 | g_free(Drawing); |
247 | } |
248 | |
76a67e8a |
249 | GtkWidget *Drawing_getWidget(Drawing_t *Drawing) |
250 | { |
251 | return Drawing->Drawing_Area_V; |
252 | } |
253 | |
f0d936c0 |
254 | /* get_time_from_pixels |
255 | * |
256 | * Get the time interval from window time and pixels, and pixels requested. This |
257 | * function uses TimeMul, which should only be used if the float value is lower |
258 | * that 4, and here it's always lower than 1, so it's ok. |
259 | */ |
fa2c4dbe |
260 | void convert_pixels_to_time( |
261 | Drawing_t *Drawing, |
262 | guint x, |
263 | LttTime *window_time_begin, |
264 | LttTime *window_time_end, |
76a67e8a |
265 | LttTime *time) |
f0d936c0 |
266 | { |
fa2c4dbe |
267 | LttTime window_time_interval; |
f0d936c0 |
268 | |
76a67e8a |
269 | TimeSub(window_time_interval, *window_time_end, *window_time_begin); |
f0d936c0 |
270 | |
271 | |
fa2c4dbe |
272 | TimeMul(*time, window_time_interval, |
273 | (x/(float)Drawing->width)); |
76a67e8a |
274 | TimeAdd(*time, *window_time_begin, *time); |
f0d936c0 |
275 | |
fa2c4dbe |
276 | } |
277 | |
278 | |
279 | |
280 | void convert_time_to_pixels( |
281 | LttTime window_time_begin, |
282 | LttTime window_time_end, |
283 | LttTime time, |
284 | Drawing_t *Drawing, |
76a67e8a |
285 | guint *x) |
fa2c4dbe |
286 | { |
287 | LttTime window_time_interval; |
76a67e8a |
288 | float interval_float, time_float; |
fa2c4dbe |
289 | |
290 | TimeSub(window_time_interval, window_time_end, window_time_begin); |
291 | |
76a67e8a |
292 | TimeSub(time, time, window_time_begin); |
fa2c4dbe |
293 | |
76a67e8a |
294 | interval_float = (window_time_interval.tv_sec * NANSECOND_CONST) |
295 | + window_time_interval.tv_nsec; |
296 | time_float = (time.tv_sec * NANSECOND_CONST) |
297 | + time.tv_nsec; |
298 | |
299 | *x = (guint)(time_float/interval_float * Drawing->width); |
f0d936c0 |
300 | |
301 | } |
302 | |
847b479d |
303 | void Drawing_Refresh ( Drawing_t *Drawing, |
304 | guint x, guint y, |
305 | guint width, guint height) |
306 | { |
307 | gdk_draw_drawable( |
308 | Drawing->Drawing_Area_V->window, |
309 | Drawing->Drawing_Area_V-> |
310 | style->fg_gc[GTK_WIDGET_STATE (Drawing->Drawing_Area_V)], |
311 | GDK_DRAWABLE(Drawing->Pixmap), |
312 | x, y, |
313 | x, y, |
314 | width, height); |
315 | } |
316 | |
317 | |
318 | void Drawing_draw_line( Drawing_t *Drawing, |
319 | GdkPixmap *Pixmap, |
320 | guint x1, guint y1, |
321 | guint x2, guint y2, |
322 | GdkGC *GC) |
323 | { |
324 | gdk_draw_line (Pixmap, |
325 | GC, |
326 | x1, y1, x2, y2); |
327 | } |
328 | |
329 | |
fa2c4dbe |
330 | |
331 | |
76a67e8a |
332 | void Drawing_Resize(Drawing_t *Drawing, guint h, guint w) |
f0d936c0 |
333 | { |
f0d936c0 |
334 | Drawing->height = h ; |
76a67e8a |
335 | Drawing->width = w ; |
f0d936c0 |
336 | |
76a67e8a |
337 | gtk_widget_set_size_request ( Drawing->Drawing_Area_V, |
338 | Drawing->width, |
f0d936c0 |
339 | Drawing->height); |
340 | |
341 | |
342 | } |
847b479d |
343 | |
344 | |