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