1d4c68aafc3408f68b1b5f3ab274397ecfc39583
[lttv.git] / ltt / branches / poly / lttv / modules / gui / mainWin / src / gtkcustom.c
1 #include <gtk/gtk.h>
2
3 #include <lttv/gtkcustom.h>
4 //#include "gtkintl.h"
5 #include <lttv/mainWindow.h>
6 #include <lttv/gtkTraceSet.h>
7
8 static void gtk_custom_class_init (GtkCustomClass *klass);
9 static void gtk_custom_init (GtkCustom *custom);
10
11
12 static void gtk_custom_size_request (GtkWidget *widget,
13 GtkRequisition *requisition);
14 static void gtk_custom_size_allocate (GtkWidget *widget,
15 GtkAllocation *allocation);
16
17 void gtk_custom_scroll_value_changed (GtkRange *range, gpointer custom);
18
19 GType
20 gtk_custom_get_type (void)
21 {
22 static GType custom_type = 0;
23
24 if (!custom_type)
25 {
26 static const GTypeInfo custom_info =
27 {
28 sizeof (GtkCustomClass),
29 NULL, /* base_init */
30 NULL, /* base_finalize */
31 (GClassInitFunc) gtk_custom_class_init,
32 NULL, /* class_finalize */
33 NULL, /* class_data */
34 sizeof (GtkCustom),
35 0, /* n_preallocs */
36 (GInstanceInitFunc) gtk_custom_init,
37 NULL, /* value_table */
38 };
39
40 custom_type = g_type_register_static (GTK_TYPE_PANED, "GtkCustom",
41 &custom_info, 0);
42 }
43
44 return custom_type;
45 }
46
47 static void
48 gtk_custom_class_init (GtkCustomClass *class)
49 {
50 GtkWidgetClass *widget_class;
51
52 widget_class = (GtkWidgetClass *) class;
53
54 widget_class->size_request = gtk_custom_size_request;
55 widget_class->size_allocate = gtk_custom_size_allocate;
56 }
57
58 static void
59 gtk_custom_init (GtkCustom *custom)
60 {
61 GtkWidget * button;
62
63 GTK_WIDGET_SET_FLAGS (custom, GTK_NO_WINDOW);
64 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (custom), FALSE);
65
66 custom->first_pane = NULL;
67 custom->last_pane = NULL;
68 custom->focused_pane = NULL;
69 custom->num_children = 0;
70
71 custom->vbox = NULL;
72 // custom->scrollWindow = NULL;
73 // custom->viewport = NULL;
74 custom->hscrollbar = NULL;
75 }
76
77
78 GtkWidget* gtk_custom_new ()
79 {
80 return GTK_WIDGET (g_object_new (gtk_custom_get_type (), NULL));
81 }
82
83
84 void gtk_custom_set_focus (GtkWidget * widget, gpointer user_data)
85 {
86 GtkCustom * custom = (GtkCustom*) widget;
87 GtkPaned * pane;
88 if(!custom->first_pane) return;
89
90
91 pane = custom->first_pane;
92 while(1){
93 if((GtkWidget*)pane == (GtkWidget*)user_data){
94 custom->focused_pane = pane;
95 break;
96 }
97 if(pane == custom->last_pane){
98 custom->focused_pane = NULL;
99 break;
100 }
101 pane = (GtkPaned*)pane->child1;
102 }
103 }
104
105 void gtk_custom_set_adjust(GtkCustom * custom, gboolean first_time)
106 {
107 TimeWindow time_window;
108 TimeInterval *time_span;
109 double tmp, start;
110 double range = 0;
111
112 get_time_window(custom->mw,&time_window);
113 if(first_time){
114 time_span = LTTV_TRACESET_CONTEXT(custom->mw->traceset_info->traceset_context)->Time_Span ;
115
116 custom->hadjust->lower = ltt_time_to_double(time_span->startTime) *
117 NANOSECONDS_PER_SECOND;
118 custom->hadjust->value = custom->hadjust->lower;
119 custom->hadjust->upper = ltt_time_to_double(time_span->endTime) *
120 NANOSECONDS_PER_SECOND;
121 }
122
123 /* Page increment of whole visible area */
124 if(custom->hadjust == NULL){
125 g_warning("Insert a viewer first");
126 return;
127 }
128
129 start = ltt_time_to_double(time_window.start_time) * NANOSECONDS_PER_SECOND;
130 tmp = custom->hadjust->upper - custom->hadjust->lower;
131
132 custom->hadjust->page_increment = ltt_time_to_double(
133 time_window.time_width) * NANOSECONDS_PER_SECOND;
134
135 if(custom->hadjust->page_increment >= tmp - range)
136 custom->hadjust->value = custom->hadjust->lower;
137 if(start + custom->hadjust->page_increment >= custom->hadjust->upper - range)
138 custom->hadjust->value = start;
139
140 /* page_size to the whole visible area will take care that the
141 * scroll value + the shown area will never be more than what is
142 * in the trace. */
143 custom->hadjust->page_size = custom->hadjust->page_increment;
144 custom->hadjust->step_increment = custom->hadjust->page_increment / 10;
145
146 gtk_adjustment_changed (custom->hadjust);
147 }
148
149 void gtk_custom_widget_add(GtkCustom * custom, GtkWidget * widget1)
150 {
151 GtkPaned * tmpPane;
152 GtkWidget * w;
153
154 g_return_if_fail(GTK_IS_CUSTOM(custom));
155 g_object_ref(G_OBJECT(widget1));
156
157
158 if(!custom->first_pane){
159 custom->first_pane = (GtkPaned *)gtk_vpaned_new();
160 custom->last_pane = custom->first_pane;
161
162 custom->hscrollbar = gtk_hscrollbar_new (NULL);
163 gtk_widget_show(custom->hscrollbar);
164
165 custom->hadjust = gtk_range_get_adjustment(GTK_RANGE(custom->hscrollbar));
166 gtk_custom_set_adjust(custom, TRUE);
167
168 gtk_range_set_update_policy (GTK_RANGE(custom->hscrollbar),
169 GTK_UPDATE_DISCONTINUOUS);
170 g_signal_connect(G_OBJECT(custom->hscrollbar), "value-changed",
171 G_CALLBACK(gtk_custom_scroll_value_changed), custom);
172
173 custom->vbox = gtk_vbox_new(FALSE,0);
174 gtk_widget_show(custom->vbox);
175
176 // custom->viewport = gtk_viewport_new (NULL,NULL);
177 // gtk_widget_show(custom->viewport);
178
179 // gtk_container_add(GTK_CONTAINER(custom->viewport), (GtkWidget*)custom->vbox);
180 gtk_box_pack_end(GTK_BOX(custom->vbox),(GtkWidget*)custom->hscrollbar,FALSE,FALSE,0);
181 gtk_box_pack_end(GTK_BOX(custom->vbox),(GtkWidget*)custom->first_pane,TRUE,TRUE,0);
182
183 // custom->scrollWindow = gtk_scrolled_window_new (NULL, NULL);
184 // gtk_widget_show(custom->scrollWindow);
185 // gtk_container_add (GTK_CONTAINER (custom->scrollWindow), (GtkWidget*)custom->viewport);
186 // gtk_paned_pack1(GTK_PANED(custom), (GtkWidget*)custom->scrollWindow,FALSE, TRUE);
187
188 gtk_paned_pack1(GTK_PANED(custom), (GtkWidget*)custom->vbox,FALSE, TRUE);
189 }else{
190 tmpPane = custom->last_pane;
191 custom->last_pane = (GtkPaned *)gtk_vpaned_new();
192 gtk_paned_pack1 (tmpPane,(GtkWidget*)custom->last_pane, FALSE,TRUE);
193 }
194 gtk_widget_show((GtkWidget *)custom->last_pane);
195
196 gtk_paned_pack2 (custom->last_pane,widget1, TRUE, TRUE);
197 custom->focused_pane = custom->last_pane;
198 custom->num_children++;
199
200 }
201
202 void gtk_custom_widget_delete(GtkCustom * custom)
203 {
204 GtkPaned * tmp, *prev, *next;
205
206 if(!custom->focused_pane) return;
207
208 tmp = (GtkPaned*)custom->focused_pane->child2; //widget in vpaned
209 g_object_unref(G_OBJECT(tmp));
210
211 if(custom->focused_pane == custom->first_pane &&
212 custom->focused_pane == custom->last_pane){
213 // gtk_container_remove(GTK_CONTAINER(custom),(GtkWidget*)custom->scrollWindow);
214 gtk_container_remove(GTK_CONTAINER(custom),(GtkWidget*)custom->vbox);
215 custom->first_pane = NULL;
216 custom->last_pane = NULL;
217 custom->focused_pane = NULL;
218 }else if(custom->focused_pane == custom->first_pane &&
219 custom->focused_pane != custom->last_pane){
220 next = (GtkPaned*)custom->first_pane->child1;
221 g_object_ref(G_OBJECT(next));
222 gtk_container_remove(GTK_CONTAINER(custom->first_pane),(GtkWidget*)next);
223 gtk_container_remove(GTK_CONTAINER(custom->vbox),(GtkWidget*)custom->first_pane);
224 custom->first_pane = next;
225 gtk_box_pack_end(GTK_BOX(custom->vbox),(GtkWidget*)custom->first_pane,TRUE,TRUE,0);
226 custom->focused_pane = custom->first_pane;
227 g_object_unref(G_OBJECT(next));
228 }else if(custom->focused_pane != custom->first_pane &&
229 custom->focused_pane == custom->last_pane){
230 tmp = custom->last_pane;
231 custom->last_pane = (GtkPaned*)gtk_widget_get_parent((GtkWidget*)custom->last_pane);
232 custom->focused_pane = custom->last_pane;
233 gtk_container_remove(GTK_CONTAINER(custom->last_pane),(GtkWidget*)tmp);
234 }else{
235 tmp = custom->focused_pane;
236 prev = (GtkPaned*)gtk_widget_get_parent((GtkWidget*)tmp);
237 next = (GtkPaned*)tmp->child1;
238 g_object_ref(G_OBJECT(next));
239 gtk_container_remove(GTK_CONTAINER(custom->focused_pane),(GtkWidget*)next);
240 gtk_container_remove(GTK_CONTAINER(prev),(GtkWidget*)custom->focused_pane);
241 gtk_paned_pack1(prev, (GtkWidget*)next, FALSE, TRUE);
242 custom->focused_pane = next;
243 g_object_unref(G_OBJECT(next));
244 }
245
246 custom->num_children--;
247 }
248
249
250 void gtk_custom_widget_move_up(GtkCustom * custom)
251 {
252 GtkWidget* upWidget, *downWidget;
253 GtkPaned * prev,*next, *prevPrev;
254
255 if(custom->last_pane == custom->focused_pane) return;
256
257 // move VPane
258 prev = (GtkPaned*)custom->focused_pane->child1;
259 g_object_ref(G_OBJECT(prev));
260 gtk_container_remove(GTK_CONTAINER(custom->focused_pane),(GtkWidget*)prev);
261
262 if(prev == custom->last_pane){
263 prevPrev = NULL;
264 custom->last_pane = custom->focused_pane;
265 }else{
266 prevPrev = (GtkPaned*)prev->child1;
267 g_object_ref(G_OBJECT(prevPrev));
268 gtk_container_remove(GTK_CONTAINER(prev),(GtkWidget*)prevPrev);
269 }
270
271 g_object_ref(G_OBJECT(custom->focused_pane));
272 if(custom->first_pane == custom->focused_pane){
273 gtk_container_remove(GTK_CONTAINER(custom->vbox),(GtkWidget*)custom->focused_pane);
274 gtk_box_pack_end(GTK_BOX(custom->vbox),(GtkWidget*)prev,TRUE,TRUE,0);
275 custom->first_pane = prev;
276 }else{
277 next = (GtkPaned*)gtk_widget_get_parent((GtkWidget*)custom->focused_pane);
278 gtk_container_remove(GTK_CONTAINER(next),(GtkWidget*)custom->focused_pane);
279 gtk_paned_pack1(GTK_PANED(next), (GtkWidget*)prev, FALSE,TRUE);
280 }
281 gtk_paned_pack1(GTK_PANED(prev),(GtkWidget*)custom->focused_pane, FALSE,TRUE);
282 if(prevPrev)
283 gtk_paned_pack1(GTK_PANED(custom->focused_pane),(GtkWidget*)prevPrev, FALSE,TRUE);
284
285 g_object_unref(G_OBJECT(prev));
286 if(prevPrev) g_object_unref(G_OBJECT(prevPrev));
287 g_object_unref(G_OBJECT(custom->focused_pane));
288 }
289
290
291 void gtk_custom_widget_move_down(GtkCustom * custom)
292 {
293 GtkWidget* upWidget, *downWidget;
294 GtkPaned * prev,*next, *nextNext;
295
296 if(custom->first_pane == custom->focused_pane) return;
297
298 //move VPane
299 next = (GtkPaned*)gtk_widget_get_parent((GtkWidget*)custom->focused_pane);
300 g_object_ref(G_OBJECT(next));
301
302 if(custom->last_pane == custom->focused_pane){
303 prev = NULL;
304 custom->last_pane = next;
305 }else{
306 prev = (GtkPaned*)custom->focused_pane->child1;
307 g_object_ref(G_OBJECT(prev));
308 gtk_container_remove(GTK_CONTAINER(custom->focused_pane),(GtkWidget*)prev);
309 }
310
311 g_object_ref(G_OBJECT(custom->focused_pane));
312 gtk_container_remove(GTK_CONTAINER(next),(GtkWidget*)custom->focused_pane);
313
314 if(next == custom->first_pane){
315 custom->first_pane = custom->focused_pane;
316 gtk_container_remove(GTK_CONTAINER(custom->vbox),(GtkWidget*)next);
317 gtk_box_pack_end(GTK_BOX(custom->vbox),(GtkWidget*)custom->focused_pane,TRUE,TRUE,0);
318 }else{
319 nextNext = (GtkPaned*)gtk_widget_get_parent((GtkWidget*)next);
320 gtk_container_remove(GTK_CONTAINER(nextNext),(GtkWidget*)next);
321 gtk_paned_pack1(nextNext, (GtkWidget*)custom->focused_pane, FALSE, TRUE);
322 }
323 gtk_paned_pack1(custom->focused_pane,(GtkWidget*)next, FALSE,TRUE);
324 if(prev)
325 gtk_paned_pack1(next,(GtkWidget*)prev, FALSE,TRUE);
326
327 if(prev)g_object_unref(G_OBJECT(prev));
328 g_object_unref(G_OBJECT(next));
329 g_object_unref(G_OBJECT(custom->focused_pane));
330 }
331
332 void gtk_custom_scroll_value_changed(GtkRange *range, gpointer custom_arg)
333 {
334 LttTime time;
335 GtkCustom * custom = (GtkCustom*)custom_arg;
336 gdouble value = gtk_range_get_value(range);
337 time = ltt_time_from_double(value / NANOSECONDS_PER_SECOND);
338 set_current_time(custom->mw, &time);
339 g_warning("The current time is second :%d, nanosecond : %d\n", time.tv_sec, time.tv_nsec);
340 }
341
342
343 static void
344 gtk_custom_size_request (GtkWidget *widget,
345 GtkRequisition *requisition)
346 {
347 GtkPaned *paned = GTK_PANED (widget);
348 GtkRequisition child_requisition;
349
350 requisition->width = 0;
351 requisition->height = 0;
352
353 if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1))
354 {
355 gtk_widget_size_request (paned->child1, &child_requisition);
356
357 requisition->height = child_requisition.height;
358 requisition->width = child_requisition.width;
359 }
360
361 if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
362 {
363 gtk_widget_size_request (paned->child2, &child_requisition);
364
365 requisition->width = MAX (requisition->width, child_requisition.width);
366 requisition->height += child_requisition.height;
367 }
368
369 requisition->height += GTK_CONTAINER (paned)->border_width * 2;
370 requisition->width += GTK_CONTAINER (paned)->border_width * 2;
371
372 if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
373 paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
374 {
375 gint handle_size;
376
377 gtk_widget_style_get (widget, "handle_size", &handle_size, NULL);
378 requisition->height += handle_size;
379 }
380
381 }
382
383 static void
384 gtk_custom_size_allocate (GtkWidget *widget,
385 GtkAllocation *allocation)
386 {
387 GtkPaned *paned = GTK_PANED (widget);
388 gint border_width = GTK_CONTAINER (paned)->border_width;
389
390 widget->allocation = *allocation;
391
392 if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
393 paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
394 {
395 GtkRequisition child1_requisition;
396 GtkRequisition child2_requisition;
397 GtkAllocation child1_allocation;
398 GtkAllocation child2_allocation;
399 gint handle_size;
400
401 gtk_widget_style_get (widget, "handle_size", &handle_size, NULL);
402
403 gtk_widget_get_child_requisition (paned->child1, &child1_requisition);
404 gtk_widget_get_child_requisition (paned->child2, &child2_requisition);
405
406 gtk_paned_compute_position (paned,
407 MAX (1, widget->allocation.height
408 - handle_size
409 - 2 * border_width),
410 child1_requisition.height,
411 child2_requisition.height);
412
413 paned->handle_pos.x = widget->allocation.x + border_width;
414 paned->handle_pos.y = widget->allocation.y + paned->child1_size + border_width;
415 paned->handle_pos.width = MAX (1, (gint) widget->allocation.width - 2 * border_width);
416 paned->handle_pos.height = handle_size;
417
418 if (GTK_WIDGET_REALIZED (widget))
419 {
420 if (GTK_WIDGET_MAPPED (widget))
421 gdk_window_show (paned->handle);
422 gdk_window_move_resize (paned->handle,
423 paned->handle_pos.x,
424 paned->handle_pos.y,
425 paned->handle_pos.width,
426 handle_size);
427 }
428
429 child1_allocation.width = child2_allocation.width = MAX (1, (gint) allocation->width - border_width * 2);
430 child1_allocation.height = MAX (1, paned->child1_size);
431 child1_allocation.x = child2_allocation.x = widget->allocation.x + border_width;
432 child1_allocation.y = widget->allocation.y + border_width;
433
434 child2_allocation.y = child1_allocation.y + paned->child1_size + paned->handle_pos.height;
435 child2_allocation.height = MAX (1, widget->allocation.y + widget->allocation.height - child2_allocation.y - border_width);
436
437 if (GTK_WIDGET_MAPPED (widget) &&
438 paned->child1->allocation.height < child1_allocation.height)
439 {
440 gtk_widget_size_allocate (paned->child2, &child2_allocation);
441 gtk_widget_size_allocate (paned->child1, &child1_allocation);
442 }
443 else
444 {
445 gtk_widget_size_allocate (paned->child1, &child1_allocation);
446 gtk_widget_size_allocate (paned->child2, &child2_allocation);
447 }
448 }
449 else
450 {
451 GtkAllocation child_allocation;
452
453 if (GTK_WIDGET_REALIZED (widget))
454 gdk_window_hide (paned->handle);
455
456 if (paned->child1)
457 gtk_widget_set_child_visible (paned->child1, TRUE);
458 if (paned->child2)
459 gtk_widget_set_child_visible (paned->child2, TRUE);
460
461 child_allocation.x = widget->allocation.x + border_width;
462 child_allocation.y = widget->allocation.y + border_width;
463 child_allocation.width = MAX (1, allocation->width - 2 * border_width);
464 child_allocation.height = MAX (1, allocation->height - 2 * border_width);
465
466 if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1))
467 gtk_widget_size_allocate (paned->child1, &child_allocation);
468 else if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
469 gtk_widget_size_allocate (paned->child2, &child_allocation);
470 }
471 }
472
This page took 0.039448 seconds and 4 git commands to generate.