Added GPL copyrights to my files, MD
[lttv.git] / ltt / branches / poly / lttv / modules / gui / controlflow / drawing.c
1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Mathieu Desnoyers
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
16 * MA 02111-1307, USA.
17 */
18
19 #include <gtk/gtk.h>
20 #include <gdk/gdk.h>
21
22 #include <lttv/processTrace.h>
23 #include <lttv/gtkTraceSet.h>
24 #include <lttv/hook.h>
25
26 #include "drawing.h"
27 #include "cfv.h"
28 #include "cfv-private.h"
29 #include "eventhooks.h"
30
31 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
32 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
33
34 /*****************************************************************************
35 * drawing functions *
36 *****************************************************************************/
37
38 //FIXME Colors will need to be dynamic. Graphic context part not done so far.
39 typedef enum
40 {
41 RED,
42 GREEN,
43 BLUE,
44 WHITE,
45 BLACK
46
47 } ControlFlowColors;
48
49 /* Vector of unallocated colors */
50 static GdkColor CF_Colors [] =
51 {
52 { 0, 0xffff, 0x0000, 0x0000 }, // RED
53 { 0, 0x0000, 0xffff, 0x0000 }, // GREEN
54 { 0, 0x0000, 0x0000, 0xffff }, // BLUE
55 { 0, 0xffff, 0xffff, 0xffff }, // WHITE
56 { 0, 0x0000, 0x0000, 0x0000 } // BLACK
57 };
58
59
60 /* Function responsible for updating the exposed area.
61 * It must call processTrace() to ask for this update.
62 * Note : this function cannot clear the background, because it may
63 * erase drawing already present (SAFETY).
64 */
65 void drawing_data_request(Drawing_t *drawing,
66 GdkPixmap **pixmap,
67 gint x, gint y,
68 gint width,
69 gint height)
70 {
71 if(width < 0) return ;
72 if(height < 0) return ;
73 ControlFlowData *control_flow_data =
74 (ControlFlowData*)g_object_get_data(
75 G_OBJECT(
76 drawing->drawing_area),
77 "control_flow_data");
78
79 LttTime start, end;
80 LttTime window_end = ltt_time_add(control_flow_data->time_window.time_width,
81 control_flow_data->time_window.start_time);
82
83 g_critical("req : window_end : %u, %u", window_end.tv_sec,
84 window_end.tv_nsec);
85
86 g_critical("req : time width : %u, %u", control_flow_data->time_window.time_width.tv_sec,
87 control_flow_data->time_window.time_width.tv_nsec);
88
89 g_critical("x is : %i, x+width is : %i", x, x+width);
90
91 convert_pixels_to_time(drawing->drawing_area->allocation.width, x,
92 &control_flow_data->time_window.start_time,
93 &window_end,
94 &start);
95
96 convert_pixels_to_time(drawing->drawing_area->allocation.width, x + width,
97 &control_flow_data->time_window.start_time,
98 &window_end,
99 &end);
100
101 LttvTracesetContext * tsc =
102 get_traceset_context(control_flow_data->mw);
103
104 //send_test_process(
105 //guicontrolflow_get_process_list(drawing->control_flow_data),
106 //drawing);
107 //send_test_drawing(
108 //guicontrolflow_get_process_list(drawing->control_flow_data),
109 //drawing, *pixmap, x, y, width, height);
110
111 // Let's call processTrace() !!
112 EventRequest event_request; // Variable freed at the end of the function.
113 event_request.control_flow_data = control_flow_data;
114 event_request.time_begin = start;
115 event_request.time_end = end;
116 event_request.x_begin = x;
117 event_request.x_end = x+width;
118
119 g_critical("req : start : %u, %u", event_request.time_begin.tv_sec,
120 event_request.time_begin.tv_nsec);
121
122 g_critical("req : end : %u, %u", event_request.time_end.tv_sec,
123 event_request.time_end.tv_nsec);
124
125 LttvHooks *event = lttv_hooks_new();
126 LttvHooks *after_event = lttv_hooks_new();
127 LttvHooks *after_traceset = lttv_hooks_new();
128 lttv_hooks_add(after_traceset, after_data_request, &event_request);
129 lttv_hooks_add(event, draw_event_hook, &event_request);
130 //Modified by xiangxiu: state update hooks are added by the main window
131 //state_add_event_hooks_api(control_flow_data->mw);
132 lttv_hooks_add(after_event, draw_after_hook, &event_request);
133
134 lttv_process_traceset_seek_time(tsc, start);
135 // FIXME : would like to place the after_traceset hook after the traceset,
136 // but the traceset context state is not valid anymore.
137 lttv_traceset_context_add_hooks(tsc,
138 // NULL, after_traceset, NULL, NULL, NULL, NULL,
139 NULL, NULL, NULL, NULL, NULL, NULL,
140 NULL, after_traceset, NULL, event, after_event);
141 lttv_process_traceset(tsc, end, G_MAXULONG);
142 //after_data_request((void*)&event_request,(void*)tsc);
143 lttv_traceset_context_remove_hooks(tsc,
144 //NULL, after_traceset, NULL, NULL, NULL, NULL,
145 NULL, NULL, NULL, NULL, NULL, NULL,
146 NULL, after_traceset, NULL, event, after_event);
147 //Modified by xiangxiu: state update hooks are removed by the main window
148 //state_remove_event_hooks_api(control_flow_data->mw);
149
150 lttv_hooks_destroy(after_traceset);
151 lttv_hooks_destroy(event);
152 lttv_hooks_destroy(after_event);
153
154
155 }
156
157 /* Callbacks */
158
159
160 /* Create a new backing pixmap of the appropriate size */
161 /* As the scaling will always change, it's of no use to copy old
162 * pixmap.
163 */
164 static gboolean
165 configure_event( GtkWidget *widget, GdkEventConfigure *event,
166 gpointer user_data)
167 {
168 Drawing_t *drawing = (Drawing_t*)user_data;
169
170
171 /* First, get the new time interval of the main window */
172 /* we assume (see documentation) that the main window
173 * has updated the time interval before this configure gets
174 * executed.
175 */
176 get_time_window(drawing->control_flow_data->mw,
177 &drawing->control_flow_data->time_window);
178
179 /* New pixmap, size of the configure event */
180 //GdkPixmap *pixmap = gdk_pixmap_new(widget->window,
181 // widget->allocation.width + SAFETY,
182 // widget->allocation.height + SAFETY,
183 // -1);
184
185 g_critical("drawing configure event");
186 g_critical("New draw size : %i by %i",widget->allocation.width, widget->allocation.height);
187
188
189 if (drawing->pixmap)
190 gdk_pixmap_unref(drawing->pixmap);
191
192 /* If no old pixmap present */
193 //if(drawing->pixmap == NULL)
194 {
195 drawing->pixmap = gdk_pixmap_new(
196 widget->window,
197 widget->allocation.width + SAFETY,
198 widget->allocation.height + SAFETY,
199 //ProcessList_get_height
200 // (GuiControlFlow_get_process_list(drawing->control_flow_data)),
201 -1);
202 drawing->width = widget->allocation.width;
203 drawing->height = widget->allocation.height;
204
205
206 // Clear the image
207 gdk_draw_rectangle (drawing->pixmap,
208 widget->style->white_gc,
209 TRUE,
210 0, 0,
211 widget->allocation.width+SAFETY,
212 widget->allocation.height+SAFETY);
213
214 //g_info("init data request");
215
216
217 /* Initial data request */
218 // Do not need to ask for data of 1 pixel : not synchronized with
219 // main window time at this moment.
220 drawing_data_request(drawing, &drawing->pixmap, 0, 0,
221 widget->allocation.width,
222 widget->allocation.height);
223
224 drawing->width = widget->allocation.width;
225 drawing->height = widget->allocation.height;
226
227 return TRUE;
228
229
230
231 }
232 #ifdef NOTUSE
233 // /* Draw empty background */
234 // gdk_draw_rectangle (pixmap,
235 // widget->style->black_gc,
236 // TRUE,
237 // 0, 0,
238 // widget->allocation.width,
239 // widget->allocation.height);
240
241 /* Copy old data to new pixmap */
242 gdk_draw_drawable (pixmap,
243 widget->style->white_gc,
244 drawing->pixmap,
245 0, 0,
246 0, 0,
247 -1, -1);
248
249 if (drawing->pixmap)
250 gdk_pixmap_unref(drawing->pixmap);
251
252 drawing->pixmap = pixmap;
253
254 // Clear the bottom part of the image (SAFETY)
255 gdk_draw_rectangle (pixmap,
256 widget->style->white_gc,
257 TRUE,
258 0, drawing->height+SAFETY,
259 drawing->width+SAFETY, // do not overlap
260 (widget->allocation.height) - drawing->height);
261
262 // Clear the right part of the image (SAFETY)
263 gdk_draw_rectangle (pixmap,
264 widget->style->white_gc,
265 TRUE,
266 drawing->width+SAFETY, 0,
267 (widget->allocation.width) - drawing->width, // do not overlap
268 drawing->height+SAFETY);
269
270 /* Clear the backgound for data request, but not SAFETY */
271 gdk_draw_rectangle (pixmap,
272 drawing->drawing_area->style->white_gc,
273 TRUE,
274 drawing->width + SAFETY, 0,
275 widget->allocation.width - drawing->width, // do not overlap
276 widget->allocation.height+SAFETY);
277
278 /* Request data for missing space */
279 g_info("missing data request");
280 drawing_data_request(drawing, &pixmap, drawing->width, 0,
281 widget->allocation.width - drawing->width,
282 widget->allocation.height);
283
284 drawing->width = widget->allocation.width;
285 drawing->height = widget->allocation.height;
286
287 return TRUE;
288 #endif //NOTUSE
289 }
290
291
292 /* Redraw the screen from the backing pixmap */
293 static gboolean
294 expose_event( GtkWidget *widget, GdkEventExpose *event, gpointer user_data )
295 {
296 Drawing_t *drawing = (Drawing_t*)user_data;
297 ControlFlowData *control_flow_data =
298 (ControlFlowData*)g_object_get_data(
299 G_OBJECT(widget),
300 "control_flow_data");
301
302 g_critical("drawing expose event");
303
304 guint x=0;
305 LttTime* current_time =
306 guicontrolflow_get_current_time(control_flow_data);
307
308 LttTime window_end = ltt_time_add(control_flow_data->time_window.time_width,
309 control_flow_data->time_window.start_time);
310
311 convert_time_to_pixels(
312 control_flow_data->time_window.start_time,
313 window_end,
314 *current_time,
315 widget->allocation.width,
316 &x);
317
318 gdk_draw_pixmap(widget->window,
319 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
320 drawing->pixmap,
321 event->area.x, event->area.y,
322 event->area.x, event->area.y,
323 event->area.width, event->area.height);
324
325 if(x >= event->area.x && x <= event->area.x+event->area.width)
326 {
327 GdkGC *gc = gdk_gc_new(control_flow_data->drawing->pixmap);
328 gdk_gc_copy(gc, widget->style->black_gc);
329
330 drawing_draw_line(NULL, widget->window,
331 x, event->area.y,
332 x, event->area.y+event->area.height,
333 gc);
334 gdk_gc_unref(gc);
335 }
336 return FALSE;
337 }
338
339 /* mouse click */
340 static gboolean
341 button_press_event( GtkWidget *widget, GdkEventButton *event, gpointer user_data )
342 {
343 ControlFlowData *control_flow_data =
344 (ControlFlowData*)g_object_get_data(
345 G_OBJECT(widget),
346 "control_flow_data");
347 Drawing_t *drawing = control_flow_data->drawing;
348
349
350 g_critical("click");
351 if(event->button == 1)
352 {
353 LttTime time;
354
355 LttTime window_end = ltt_time_add(control_flow_data->time_window.time_width,
356 control_flow_data->time_window.start_time);
357
358
359 /* left mouse button click */
360 g_critical("x click is : %f", event->x);
361
362 convert_pixels_to_time(widget->allocation.width, (guint)event->x,
363 &control_flow_data->time_window.start_time,
364 &window_end,
365 &time);
366
367 set_current_time(control_flow_data->mw, &time);
368
369 }
370
371 return FALSE;
372 }
373
374
375
376
377 Drawing_t *drawing_construct(ControlFlowData *control_flow_data)
378 {
379 Drawing_t *drawing = g_new(Drawing_t, 1);
380
381 drawing->drawing_area = gtk_drawing_area_new ();
382 drawing->control_flow_data = control_flow_data;
383
384 drawing->pango_layout =
385 gtk_widget_create_pango_layout(drawing->drawing_area, NULL);
386
387 //gtk_widget_set_size_request(drawing->drawing_area->window, 50, 50);
388 g_object_set_data_full(
389 G_OBJECT(drawing->drawing_area),
390 "Link_drawing_Data",
391 drawing,
392 (GDestroyNotify)drawing_destroy);
393
394 //gtk_widget_modify_bg( drawing->drawing_area,
395 // GTK_STATE_NORMAL,
396 // &CF_Colors[BLACK]);
397
398 //gdk_window_get_geometry(drawing->drawing_area->window,
399 // NULL, NULL,
400 // &(drawing->width),
401 // &(drawing->height),
402 // -1);
403
404 //drawing->pixmap = gdk_pixmap_new(
405 // drawing->drawing_area->window,
406 // drawing->width,
407 // drawing->height,
408 // drawing->depth);
409
410 drawing->pixmap = NULL;
411
412 // drawing->pixmap = gdk_pixmap_new(drawing->drawing_area->window,
413 // drawing->drawing_area->allocation.width,
414 // drawing->drawing_area->allocation.height,
415 // -1);
416
417 gtk_widget_add_events(drawing->drawing_area, GDK_BUTTON_PRESS_MASK);
418
419 g_signal_connect (G_OBJECT(drawing->drawing_area),
420 "configure_event",
421 G_CALLBACK (configure_event),
422 (gpointer)drawing);
423
424 g_signal_connect (G_OBJECT(drawing->drawing_area),
425 "expose_event",
426 G_CALLBACK (expose_event),
427 (gpointer)drawing);
428
429 g_signal_connect (G_OBJECT(drawing->drawing_area),
430 "button-press-event",
431 G_CALLBACK (button_press_event),
432 (gpointer)drawing);
433
434
435 return drawing;
436 }
437
438 void drawing_destroy(Drawing_t *drawing)
439 {
440
441 // Do not unref here, Drawing_t destroyed by it's widget.
442 //g_object_unref( G_OBJECT(drawing->drawing_area));
443
444 g_free(drawing->pango_layout);
445 g_free(drawing);
446 }
447
448 GtkWidget *drawing_get_widget(Drawing_t *drawing)
449 {
450 return drawing->drawing_area;
451 }
452
453 /* convert_pixels_to_time
454 *
455 * Convert from window pixel and time interval to an absolute time.
456 */
457 void convert_pixels_to_time(
458 gint width,
459 guint x,
460 LttTime *window_time_begin,
461 LttTime *window_time_end,
462 LttTime *time)
463 {
464 LttTime window_time_interval;
465
466 window_time_interval = ltt_time_sub(*window_time_end,
467 *window_time_begin);
468 *time = ltt_time_mul(window_time_interval, (x/(float)width));
469 *time = ltt_time_add(*window_time_begin, *time);
470 }
471
472
473
474 void convert_time_to_pixels(
475 LttTime window_time_begin,
476 LttTime window_time_end,
477 LttTime time,
478 int width,
479 guint *x)
480 {
481 LttTime window_time_interval;
482 float interval_float, time_float;
483
484 window_time_interval = ltt_time_sub(window_time_end,window_time_begin);
485
486 time = ltt_time_sub(time, window_time_begin);
487
488 interval_float = ltt_time_to_double(window_time_interval);
489 time_float = ltt_time_to_double(time);
490
491 *x = (guint)(time_float/interval_float * width);
492
493 }
494
495 void drawing_refresh ( Drawing_t *drawing,
496 guint x, guint y,
497 guint width, guint height)
498 {
499 g_info("Drawing.c : drawing_refresh %u, %u, %u, %u", x, y, width, height);
500 GdkRectangle update_rect;
501
502 gdk_draw_drawable(
503 drawing->drawing_area->window,
504 drawing->drawing_area->
505 style->fg_gc[GTK_WIDGET_STATE (drawing->drawing_area)],
506 GDK_DRAWABLE(drawing->pixmap),
507 x, y,
508 x, y,
509 width, height);
510
511 update_rect.x = 0 ;
512 update_rect.y = 0 ;
513 update_rect.width = drawing->width;
514 update_rect.height = drawing->height ;
515 gtk_widget_draw( drawing->drawing_area, &update_rect);
516
517 }
518
519
520 void drawing_draw_line( Drawing_t *drawing,
521 GdkPixmap *pixmap,
522 guint x1, guint y1,
523 guint x2, guint y2,
524 GdkGC *GC)
525 {
526 gdk_draw_line (pixmap,
527 GC,
528 x1, y1, x2, y2);
529 }
530
531
532
533
534 void drawing_resize(Drawing_t *drawing, guint h, guint w)
535 {
536 drawing->height = h ;
537 drawing->width = w ;
538
539 gtk_widget_set_size_request ( drawing->drawing_area,
540 drawing->width,
541 drawing->height);
542
543
544 }
545
546
547 /* Insert a square corresponding to a new process in the list */
548 /* Applies to whole drawing->width */
549 void drawing_insert_square(Drawing_t *drawing,
550 guint y,
551 guint height)
552 {
553 //GdkRectangle update_rect;
554
555 /* Allocate a new pixmap with new height */
556 GdkPixmap *pixmap = gdk_pixmap_new(drawing->drawing_area->window,
557 drawing->width + SAFETY,
558 drawing->height + height + SAFETY,
559 -1);
560
561 /* Copy the high region */
562 gdk_draw_drawable (pixmap,
563 drawing->drawing_area->style->black_gc,
564 drawing->pixmap,
565 0, 0,
566 0, 0,
567 drawing->width + SAFETY, y);
568
569
570
571
572 /* add an empty square */
573 gdk_draw_rectangle (pixmap,
574 drawing->drawing_area->style->white_gc,
575 TRUE,
576 0, y,
577 drawing->width + SAFETY, // do not overlap
578 height);
579
580
581
582 /* copy the bottom of the region */
583 gdk_draw_drawable (pixmap,
584 drawing->drawing_area->style->black_gc,
585 drawing->pixmap,
586 0, y,
587 0, y + height,
588 drawing->width+SAFETY, drawing->height - y + SAFETY);
589
590
591
592
593 if (drawing->pixmap)
594 gdk_pixmap_unref(drawing->pixmap);
595
596 drawing->pixmap = pixmap;
597
598 drawing->height+=height;
599
600 /* Rectangle to update, from new drawing dimensions */
601 //update_rect.x = 0 ;
602 //update_rect.y = y ;
603 //update_rect.width = drawing->width;
604 //update_rect.height = drawing->height - y ;
605 //gtk_widget_draw( drawing->drawing_area, &update_rect);
606 }
607
608
609 /* Remove a square corresponding to a removed process in the list */
610 void drawing_remove_square(Drawing_t *drawing,
611 guint y,
612 guint height)
613 {
614 //GdkRectangle update_rect;
615
616 /* Allocate a new pixmap with new height */
617 GdkPixmap *pixmap = gdk_pixmap_new(
618 drawing->drawing_area->window,
619 drawing->width + SAFETY,
620 drawing->height - height + SAFETY,
621 -1);
622
623 /* Copy the high region */
624 gdk_draw_drawable (pixmap,
625 drawing->drawing_area->style->black_gc,
626 drawing->pixmap,
627 0, 0,
628 0, 0,
629 drawing->width + SAFETY, y);
630
631
632
633 /* Copy up the bottom of the region */
634 gdk_draw_drawable (pixmap,
635 drawing->drawing_area->style->black_gc,
636 drawing->pixmap,
637 0, y + height,
638 0, y,
639 drawing->width, drawing->height - y - height + SAFETY);
640
641
642 if (drawing->pixmap)
643 gdk_pixmap_unref(drawing->pixmap);
644
645 drawing->pixmap = pixmap;
646
647 drawing->height-=height;
648
649 /* Rectangle to update, from new drawing dimensions */
650 //update_rect.x = 0 ;
651 //update_rect.y = y ;
652 //update_rect.width = drawing->width;
653 //update_rect.height = drawing->height - y ;
654 //gtk_widget_draw( drawing->drawing_area, &update_rect);
655 }
656
657
This page took 0.042395 seconds and 4 git commands to generate.