]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/win_gtk.c
Move the wxDL_ macros to wxDynLibrary
[wxWidgets.git] / src / gtk / win_gtk.c
1 /* ///////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/win_gtk.c
3 // Purpose: Native GTK+ widget for wxWidgets, based on GtkLayout and
4 // GtkFixed. It makes use of the gravity window property and
5 // therefore does not work with GTK 1.0.
6 // Author: Robert Roebling
7 // Id: $Id$
8 // Copyright: (c) 1998 Robert Roebling
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////// */
11
12 #ifdef VMS
13 #define XCheckIfEvent XCHECKIFEVENT
14 #endif
15
16 #include "wx/platform.h"
17 #include "wx/gtk/win_gtk.h"
18
19 #ifdef __cplusplus
20 extern "C" {
21 #endif /* __cplusplus */
22
23 typedef struct _GtkPizzaChild GtkPizzaChild;
24 typedef struct _GtkPizzaClass GtkPizzaClass;
25 typedef struct _GtkPizzaAdjData GtkPizzaAdjData;
26
27 struct _GtkPizzaClass
28 {
29 GtkContainerClass parent_class;
30
31 void (*set_scroll_adjustments) (GtkPizza *pizza,
32 GtkAdjustment *hadjustment,
33 GtkAdjustment *vadjustment);
34 };
35
36 struct _GtkPizzaChild
37 {
38 GtkWidget *widget;
39 gint x;
40 gint y;
41 };
42
43 struct _GtkPizzaAdjData
44 {
45 gint dx;
46 gint dy;
47 };
48
49 static void gtk_pizza_class_init (GtkPizzaClass *klass);
50 static void gtk_pizza_init (GtkPizza *pizza);
51
52 static void gtk_pizza_realize (GtkWidget *widget);
53 static void gtk_pizza_unrealize (GtkWidget *widget);
54
55 static void gtk_pizza_map (GtkWidget *widget);
56
57 static void gtk_pizza_size_request (GtkWidget *widget,
58 GtkRequisition *requisition);
59 static void gtk_pizza_size_allocate (GtkWidget *widget,
60 GtkAllocation *allocation);
61 static void gtk_pizza_style_set (GtkWidget *widget,
62 GtkStyle *previous_style);
63 static void gtk_pizza_add (GtkContainer *container,
64 GtkWidget *widget);
65 static void gtk_pizza_remove (GtkContainer *container,
66 GtkWidget *widget);
67 static void gtk_pizza_forall (GtkContainer *container,
68 gboolean include_internals,
69 GtkCallback callback,
70 gpointer callback_data);
71
72 static void gtk_pizza_allocate_child (GtkPizza *pizza,
73 GtkPizzaChild *child);
74
75 static GtkType gtk_pizza_child_type (GtkContainer *container);
76
77 static void gtk_pizza_scroll_set_adjustments (GtkPizza *pizza,
78 GtkAdjustment *hadj,
79 GtkAdjustment *vadj);
80
81 static GtkWidgetClass* pizza_parent_class;
82
83 GtkType
84 gtk_pizza_get_type ()
85 {
86 static GtkType pizza_type = 0;
87
88 if (!pizza_type)
89 {
90 const GTypeInfo pizza_info =
91 {
92 sizeof (GtkPizzaClass),
93 NULL, /* base_init */
94 NULL, /* base_finalize */
95 (GClassInitFunc) gtk_pizza_class_init,
96 NULL, /* class_finalize */
97 NULL, /* class_data */
98 sizeof (GtkPizza),
99 16, /* n_preallocs */
100 (GInstanceInitFunc) gtk_pizza_init,
101 NULL
102 };
103 pizza_type = g_type_register_static (GTK_TYPE_CONTAINER, "GtkPizza", &pizza_info, (GTypeFlags)0);
104 }
105
106 return pizza_type;
107 }
108
109 /* Marshaller needed for set_scroll_adjustments signal,
110 generated with GLib-2.4.6 glib-genmarshal */
111 #define g_marshal_value_peek_object(v) g_value_get_object (v)
112 static void
113 g_cclosure_user_marshal_VOID__OBJECT_OBJECT (GClosure *closure,
114 GValue *return_value,
115 guint n_param_values,
116 const GValue *param_values,
117 gpointer invocation_hint,
118 gpointer marshal_data)
119 {
120 typedef void (*GMarshalFunc_VOID__OBJECT_OBJECT) (gpointer data1,
121 gpointer arg_1,
122 gpointer arg_2,
123 gpointer data2);
124 register GMarshalFunc_VOID__OBJECT_OBJECT callback;
125 register GCClosure *cc = (GCClosure*) closure;
126 register gpointer data1, data2;
127
128 g_return_if_fail (n_param_values == 3);
129
130 if (G_CCLOSURE_SWAP_DATA (closure))
131 {
132 data1 = closure->data;
133 data2 = g_value_peek_pointer (param_values + 0);
134 }
135 else
136 {
137 data1 = g_value_peek_pointer (param_values + 0);
138 data2 = closure->data;
139 }
140 callback = (GMarshalFunc_VOID__OBJECT_OBJECT) (marshal_data ? marshal_data : cc->callback);
141
142 callback (data1,
143 g_marshal_value_peek_object (param_values + 1),
144 g_marshal_value_peek_object (param_values + 2),
145 data2);
146 }
147
148 static void
149 gtk_pizza_class_init (GtkPizzaClass *klass)
150 {
151 GtkObjectClass *object_class;
152 GtkWidgetClass *widget_class;
153 GtkContainerClass *container_class;
154
155 object_class = (GtkObjectClass*) klass;
156 widget_class = (GtkWidgetClass*) klass;
157 container_class = (GtkContainerClass*) klass;
158 pizza_parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
159
160 widget_class->map = gtk_pizza_map;
161 widget_class->realize = gtk_pizza_realize;
162 widget_class->unrealize = gtk_pizza_unrealize;
163 widget_class->size_request = gtk_pizza_size_request;
164 widget_class->size_allocate = gtk_pizza_size_allocate;
165 widget_class->style_set = gtk_pizza_style_set;
166
167 container_class->add = gtk_pizza_add;
168 container_class->remove = gtk_pizza_remove;
169 container_class->forall = gtk_pizza_forall;
170
171 container_class->child_type = gtk_pizza_child_type;
172
173 klass->set_scroll_adjustments = gtk_pizza_scroll_set_adjustments;
174
175 widget_class->set_scroll_adjustments_signal =
176 g_signal_new(
177 "set_scroll_adjustments",
178 G_TYPE_FROM_CLASS(object_class),
179 G_SIGNAL_RUN_LAST,
180 G_STRUCT_OFFSET(GtkPizzaClass, set_scroll_adjustments),
181 NULL,
182 NULL,
183 g_cclosure_user_marshal_VOID__OBJECT_OBJECT,
184 G_TYPE_NONE,
185 2,
186 GTK_TYPE_ADJUSTMENT,
187 GTK_TYPE_ADJUSTMENT);
188 }
189
190 static GtkType
191 gtk_pizza_child_type (GtkContainer *container)
192 {
193 return GTK_TYPE_WIDGET;
194 }
195
196 static void
197 gtk_pizza_init (GtkPizza *pizza)
198 {
199 GTK_WIDGET_SET_FLAGS (pizza, GTK_CAN_FOCUS);
200 GTK_WIDGET_UNSET_FLAGS (pizza, GTK_NO_WINDOW);
201
202 pizza->children = NULL;
203
204 pizza->bin_window = NULL;
205
206 pizza->m_xoffset = 0;
207 pizza->m_yoffset = 0;
208 }
209
210 GtkWidget*
211 gtk_pizza_new ()
212 {
213 GtkPizza *pizza;
214
215 pizza = g_object_new (gtk_pizza_get_type (), NULL);
216
217 pizza->m_noscroll = FALSE;
218
219 return GTK_WIDGET (pizza);
220 }
221
222 GtkWidget*
223 gtk_pizza_new_no_scroll ()
224 {
225 GtkPizza *pizza;
226
227 pizza = g_object_new (gtk_pizza_get_type (), NULL);
228
229 pizza->m_noscroll = TRUE;
230
231 return GTK_WIDGET (pizza);
232 }
233
234 gint gtk_pizza_get_xoffset (GtkPizza *pizza)
235 {
236 g_return_val_if_fail ( (pizza != NULL), -1 );
237 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza)), -1 );
238
239 return pizza->m_xoffset;
240 }
241
242 gint gtk_pizza_get_yoffset (GtkPizza *pizza)
243 {
244 g_return_val_if_fail ( (pizza != NULL), -1 );
245 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza)), -1 );
246
247 return pizza->m_yoffset;
248 }
249
250 void gtk_pizza_set_xoffset (GtkPizza *pizza, gint xoffset)
251 {
252 g_return_if_fail (pizza != NULL);
253 g_return_if_fail (GTK_IS_PIZZA (pizza));
254
255 pizza->m_xoffset = xoffset;
256 // do something
257 }
258
259 void gtk_pizza_set_yoffset (GtkPizza *pizza, gint yoffset)
260 {
261 g_return_if_fail (pizza != NULL);
262 g_return_if_fail (GTK_IS_PIZZA (pizza));
263
264 pizza->m_xoffset = yoffset;
265 // do something
266 }
267
268 gint gtk_pizza_get_rtl_offset (GtkPizza *pizza)
269 {
270 gint border;
271
272 g_return_val_if_fail ( (pizza != NULL), 0 );
273 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza)), 0 );
274
275 if (!pizza->bin_window) return 0;
276
277 border = pizza->container.border_width;
278
279 return GTK_WIDGET(pizza)->allocation.width - border*2;
280 }
281
282
283 static void
284 gtk_pizza_scroll_set_adjustments (GtkPizza *pizza,
285 GtkAdjustment *hadj,
286 GtkAdjustment *vadj)
287 {
288 /* We handle scrolling in the wxScrolledWindow, not here. */
289 }
290
291 void
292 gtk_pizza_put (GtkPizza *pizza,
293 GtkWidget *widget,
294 gint x,
295 gint y,
296 gint width,
297 gint height)
298 {
299 GtkPizzaChild *child_info;
300
301 g_return_if_fail (pizza != NULL);
302 g_return_if_fail (GTK_IS_PIZZA (pizza));
303 g_return_if_fail (widget != NULL);
304
305 child_info = g_new (GtkPizzaChild, 1);
306
307 child_info->widget = widget;
308 child_info->x = x;
309 child_info->y = y;
310
311 pizza->children = g_list_append (pizza->children, child_info);
312
313 if (GTK_WIDGET_REALIZED (pizza))
314 gtk_widget_set_parent_window (widget, pizza->bin_window);
315
316 gtk_widget_set_parent (widget, GTK_WIDGET (pizza));
317
318 gtk_widget_set_size_request( widget, width, height );
319 if (GTK_WIDGET_REALIZED (pizza))
320 gtk_pizza_allocate_child (pizza, child_info);
321 }
322
323 void
324 gtk_pizza_set_size (GtkPizza *pizza,
325 GtkWidget *widget,
326 gint x,
327 gint y,
328 gint width,
329 gint height)
330 {
331 GtkPizzaChild *child;
332 GList *children;
333
334 g_return_if_fail (pizza != NULL);
335 g_return_if_fail (GTK_IS_PIZZA (pizza));
336 g_return_if_fail (widget != NULL);
337
338 #ifndef WX_WARN_ILLEGAL_SETSIZE
339 /* this really shouldn't happen -- but it does, a lot, right now and we
340 can't pass negative values to gtk_widget_set_size_request() without getting
341 a warning printed out, so filter them out here */
342 if ( width < 0 )
343 width = 0;
344 if ( height < 0 )
345 height = 0;
346 #endif
347
348 children = pizza->children;
349 while (children)
350 {
351 child = children->data;
352 children = children->next;
353
354 if (child->widget == widget)
355 {
356 if (child->x != x || child->y != y)
357 {
358 child->x = x;
359 child->y = y;
360 gtk_widget_queue_resize(widget);
361 }
362
363 gtk_widget_set_size_request (widget, width, height);
364
365 return;
366 }
367 }
368 }
369
370 static void
371 gtk_pizza_map (GtkWidget *widget)
372 {
373 GtkPizza *pizza;
374 GtkPizzaChild *child;
375 GList *children;
376
377 g_return_if_fail (widget != NULL);
378 g_return_if_fail (GTK_IS_PIZZA (widget));
379
380 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
381 pizza = GTK_PIZZA (widget);
382
383 children = pizza->children;
384 while (children)
385 {
386 child = children->data;
387 children = children->next;
388
389 if ( GTK_WIDGET_VISIBLE (child->widget) &&
390 !GTK_WIDGET_MAPPED (child->widget) )
391 {
392 gtk_widget_map (child->widget);
393 }
394 }
395
396 gdk_window_show (widget->window);
397 gdk_window_show (pizza->bin_window);
398 }
399
400 static void
401 gtk_pizza_realize (GtkWidget *widget)
402 {
403 GtkPizza *pizza;
404 GdkWindowAttr attributes;
405 gint attributes_mask;
406 GtkPizzaChild *child;
407 GList *children;
408 int border;
409 int w, h;
410
411 g_return_if_fail (widget != NULL);
412 g_return_if_fail (GTK_IS_PIZZA (widget));
413
414 pizza = GTK_PIZZA (widget);
415 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
416
417 attributes.window_type = GDK_WINDOW_CHILD;
418
419 attributes.x = widget->allocation.x;
420 attributes.y = widget->allocation.y;
421 attributes.width = widget->allocation.width;
422 attributes.height = widget->allocation.height;
423
424 /* minimal size */
425 if (attributes.width < 2) attributes.width = 2;
426 if (attributes.height < 2) attributes.height = 2;
427
428 border = pizza->container.border_width;
429 w = attributes.width - 2 * border;
430 h = attributes.height - 2 * border;
431 if (w < 2) w = 2;
432 if (h < 2) h = 2;
433
434 if (!pizza->m_noscroll)
435 {
436 attributes.x += border;
437 attributes.y += border;
438 attributes.width = w;
439 attributes.height = h;
440 }
441
442 attributes.wclass = GDK_INPUT_OUTPUT;
443 attributes.visual = gtk_widget_get_visual (widget);
444 attributes.colormap = gtk_widget_get_colormap (widget);
445 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
446 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
447
448 widget->window = gdk_window_new(gtk_widget_get_parent_window (widget),
449 &attributes, attributes_mask);
450 gdk_window_set_user_data (widget->window, widget);
451
452 attributes.x = 0;
453 attributes.y = 0;
454 if (pizza->m_noscroll)
455 {
456 attributes.x = border;
457 attributes.y = border;
458 attributes.width = w;
459 attributes.height = h;
460 }
461
462 attributes.event_mask = gtk_widget_get_events (widget);
463 attributes.event_mask |= GDK_EXPOSURE_MASK |
464 GDK_SCROLL_MASK |
465 GDK_POINTER_MOTION_MASK |
466 GDK_POINTER_MOTION_HINT_MASK |
467 GDK_BUTTON_MOTION_MASK |
468 GDK_BUTTON1_MOTION_MASK |
469 GDK_BUTTON2_MOTION_MASK |
470 GDK_BUTTON3_MOTION_MASK |
471 GDK_BUTTON_PRESS_MASK |
472 GDK_BUTTON_RELEASE_MASK |
473 GDK_KEY_PRESS_MASK |
474 GDK_KEY_RELEASE_MASK |
475 GDK_ENTER_NOTIFY_MASK |
476 GDK_LEAVE_NOTIFY_MASK |
477 GDK_FOCUS_CHANGE_MASK;
478
479 pizza->bin_window = gdk_window_new(widget->window,
480 &attributes, attributes_mask);
481 gdk_window_set_user_data (pizza->bin_window, widget);
482
483 widget->style = gtk_style_attach (widget->style, widget->window);
484 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
485 gtk_style_set_background (widget->style, pizza->bin_window, GTK_STATE_NORMAL );
486
487 /*
488 gdk_window_set_back_pixmap( widget->window, NULL, FALSE );
489 gdk_window_set_back_pixmap( pizza->bin_window, NULL, FALSE );
490 */
491
492 /* cannot be done before realisation */
493 children = pizza->children;
494 while (children)
495 {
496 child = children->data;
497 children = children->next;
498
499 gtk_widget_set_parent_window (child->widget, pizza->bin_window);
500 }
501 }
502
503 static void
504 gtk_pizza_unrealize (GtkWidget *widget)
505 {
506 GtkPizza *pizza;
507
508 g_return_if_fail (widget != NULL);
509 g_return_if_fail (GTK_IS_PIZZA (widget));
510
511 pizza = GTK_PIZZA (widget);
512
513 gdk_window_set_user_data (pizza->bin_window, NULL);
514 gdk_window_destroy (pizza->bin_window);
515 pizza->bin_window = NULL;
516
517 if (pizza_parent_class->unrealize)
518 pizza_parent_class->unrealize(widget);
519 }
520
521 static void
522 gtk_pizza_size_request (GtkWidget *widget,
523 GtkRequisition *requisition)
524 {
525 GtkPizza *pizza;
526 GtkPizzaChild *child;
527 GList *children;
528 GtkRequisition child_requisition;
529
530 g_return_if_fail (widget != NULL);
531 g_return_if_fail (GTK_IS_PIZZA (widget));
532 g_return_if_fail (requisition != NULL);
533
534 pizza = GTK_PIZZA (widget);
535
536 children = pizza->children;
537 while (children)
538 {
539 child = children->data;
540 children = children->next;
541
542 if (GTK_WIDGET_VISIBLE (child->widget))
543 {
544 gtk_widget_size_request (child->widget, &child_requisition);
545 }
546 }
547
548 /* request very little, I'm not sure if requesting nothing
549 will always have positive effects on stability... */
550 requisition->width = 2;
551 requisition->height = 2;
552 }
553
554 static void
555 gtk_pizza_size_allocate (GtkWidget *widget,
556 GtkAllocation *allocation)
557 {
558 GtkPizza *pizza;
559 gint border;
560 gint x,y,w,h;
561 GtkPizzaChild *child;
562 GList *children;
563 gboolean only_resize;
564
565 g_return_if_fail (widget != NULL);
566 g_return_if_fail (GTK_IS_PIZZA(widget));
567 g_return_if_fail (allocation != NULL);
568
569 pizza = GTK_PIZZA (widget);
570
571 only_resize = ((widget->allocation.x == allocation->x) &&
572 (widget->allocation.y == allocation->y));
573 widget->allocation = *allocation;
574
575 if (GTK_WIDGET_REALIZED(widget))
576 {
577 border = pizza->container.border_width;
578
579 x = allocation->x + border;
580 y = allocation->y + border;
581 w = allocation->width - border*2;
582 h = allocation->height - border*2;
583 if (w < 0)
584 w = 0;
585 if (h < 0)
586 h = 0;
587
588 if (pizza->m_noscroll)
589 {
590 if (only_resize)
591 gdk_window_resize( widget->window, allocation->width, allocation->height );
592 else
593 gdk_window_move_resize( widget->window, allocation->x, allocation->y,
594 allocation->width, allocation->height );
595
596 gdk_window_move_resize( pizza->bin_window, border, border, w, h );
597 }
598 else
599 {
600 if (only_resize)
601 gdk_window_resize( widget->window, w, h );
602 else
603 gdk_window_move_resize( widget->window, x, y, w, h );
604
605 gdk_window_resize( pizza->bin_window, w, h );
606 }
607 }
608
609 children = pizza->children;
610 while (children)
611 {
612 child = children->data;
613 children = children->next;
614
615 gtk_pizza_allocate_child (pizza, child);
616 }
617 }
618
619 static void
620 gtk_pizza_style_set(GtkWidget *widget, GtkStyle *previous_style)
621 {
622 if (GTK_WIDGET_REALIZED(widget))
623 {
624 gtk_style_set_background(widget->style, widget->window, GTK_STATE_NORMAL);
625 gtk_style_set_background(widget->style, GTK_PIZZA(widget)->bin_window, GTK_STATE_NORMAL );
626 }
627
628 pizza_parent_class->style_set(widget, previous_style);
629 }
630
631 static void
632 gtk_pizza_add (GtkContainer *container,
633 GtkWidget *widget)
634 {
635 g_return_if_fail (container != NULL);
636 g_return_if_fail (GTK_IS_PIZZA (container));
637 g_return_if_fail (widget != NULL);
638
639 gtk_pizza_put (GTK_PIZZA (container), widget, 0, 0, 20, 20 );
640 }
641
642 static void
643 gtk_pizza_remove (GtkContainer *container,
644 GtkWidget *widget)
645 {
646 GtkPizza *pizza;
647 GtkPizzaChild *child;
648 GList *children;
649
650 g_return_if_fail (container != NULL);
651 g_return_if_fail (GTK_IS_PIZZA (container));
652 g_return_if_fail (widget != NULL);
653
654 pizza = GTK_PIZZA (container);
655
656 children = pizza->children;
657 while (children)
658 {
659 child = children->data;
660
661 if (child->widget == widget)
662 {
663 gtk_widget_unparent (widget);
664
665 /* security checks */
666 g_return_if_fail (GTK_IS_WIDGET (widget));
667
668 pizza->children = g_list_remove_link (pizza->children, children);
669 g_list_free (children);
670 g_free (child);
671
672 /* security checks */
673 g_return_if_fail (GTK_IS_WIDGET (widget));
674
675 break;
676 }
677
678 children = children->next;
679 }
680 }
681
682 static void
683 gtk_pizza_forall (GtkContainer *container,
684 gboolean include_internals,
685 GtkCallback callback,
686 gpointer callback_data)
687 {
688 GtkPizza *pizza;
689 GtkPizzaChild *child;
690 GList *children;
691
692 g_return_if_fail (container != NULL);
693 g_return_if_fail (GTK_IS_PIZZA (container));
694 g_return_if_fail (callback != (GtkCallback)NULL);
695
696 pizza = GTK_PIZZA (container);
697
698 children = pizza->children;
699 while (children)
700 {
701 child = children->data;
702 children = children->next;
703
704 (* callback) (child->widget, callback_data);
705 }
706 }
707
708 static void
709 gtk_pizza_allocate_child (GtkPizza *pizza,
710 GtkPizzaChild *child)
711 {
712 GtkAllocation allocation;
713 GtkRequisition requisition;
714
715 allocation.x = child->x - pizza->m_xoffset;
716 allocation.y = child->y - pizza->m_yoffset;
717 gtk_widget_get_child_requisition (child->widget, &requisition);
718 allocation.width = requisition.width;
719 allocation.height = requisition.height;
720
721 if (gtk_widget_get_direction( GTK_WIDGET(pizza) ) == GTK_TEXT_DIR_RTL)
722 {
723 /* reverse horizontal placement */
724 gint offset,border;
725
726 offset = GTK_WIDGET(pizza)->allocation.width;
727 border = pizza->container.border_width;
728 offset -= border*2;
729
730 allocation.x = offset - child->x - allocation.width + pizza->m_xoffset;
731 }
732
733 gtk_widget_size_allocate (child->widget, &allocation);
734 }
735
736 void
737 gtk_pizza_scroll (GtkPizza *pizza, gint dx, gint dy)
738 {
739 GList *tmp_list;
740
741 pizza->m_xoffset += dx;
742 pizza->m_yoffset += dy;
743
744 if (pizza->bin_window)
745 gdk_window_scroll( pizza->bin_window, -dx, -dy );
746
747 tmp_list = pizza->children;
748 while (tmp_list)
749 {
750 GtkPizzaChild *child = tmp_list->data;
751 tmp_list = tmp_list->next;
752
753 GtkAllocation alloc = child->widget->allocation;
754 alloc.x -= dx;
755 alloc.y -= dy;
756 gtk_widget_size_allocate( child->widget, &alloc );
757 }
758 }
759
760 #ifdef __cplusplus
761 }
762 #endif /* __cplusplus */