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