]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/win_gtk.c
Implemented tearoff menus, please see my posting to wxwin-developers.
[wxWidgets.git] / src / gtk / win_gtk.c
1 /* ///////////////////////////////////////////////////////////////////////////
2 // Name: win_gtk.c
3 // Purpose: native GTK+ widget for wxWindows
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////// */
9
10 #include "wx/gtk/win_gtk.h"
11 #include "gtk/gtksignal.h"
12 #include "gtk/gtknotebook.h"
13
14 #ifdef __cplusplus
15 extern "C" {
16 #endif /* __cplusplus */
17
18 static void gtk_myfixed_class_init (GtkMyFixedClass *klass);
19 static void gtk_myfixed_init (GtkMyFixed *myfixed);
20 static void gtk_myfixed_map (GtkWidget *widget);
21 #if (GTK_MINOR_VERSION == 0)
22 static void gtk_myfixed_unmap (GtkWidget *widget);
23 #endif
24 static void gtk_myfixed_realize (GtkWidget *widget);
25 static void gtk_myfixed_size_request (GtkWidget *widget,
26 GtkRequisition *requisition);
27 static void gtk_myfixed_size_allocate (GtkWidget *widget,
28 GtkAllocation *allocation);
29 static void gtk_myfixed_paint (GtkWidget *widget,
30 GdkRectangle *area);
31 static void gtk_myfixed_draw (GtkWidget *widget,
32 GdkRectangle *area);
33 static gint gtk_myfixed_expose (GtkWidget *widget,
34 GdkEventExpose *event);
35 static void gtk_myfixed_add (GtkContainer *container,
36 GtkWidget *widget);
37 static void gtk_myfixed_remove (GtkContainer *container,
38 GtkWidget *widget);
39 static void gtk_myfixed_foreach (GtkContainer *container,
40 #if (GTK_MINOR_VERSION > 0)
41 gboolean include_internals,
42 #endif
43 GtkCallback callback,
44 gpointer callback_data);
45 #if (GTK_MINOR_VERSION > 0)
46 static GtkType gtk_myfixed_child_type (GtkContainer *container);
47 #endif
48
49 #if (GTK_MINOR_VERSION > 0)
50 static void gtk_myfixed_scroll_set_adjustments (GtkMyFixed *myfixed,
51 GtkAdjustment *hadj,
52 GtkAdjustment *vadj);
53 #endif
54
55
56
57 static GtkContainerClass *parent_class = NULL;
58
59 guint
60 gtk_myfixed_get_type ()
61 {
62 static guint myfixed_type = 0;
63
64 if (!myfixed_type)
65 {
66 GtkTypeInfo myfixed_info =
67 {
68 "GtkMyFixed",
69 sizeof (GtkMyFixed),
70 sizeof (GtkMyFixedClass),
71 (GtkClassInitFunc) gtk_myfixed_class_init,
72 (GtkObjectInitFunc) gtk_myfixed_init,
73 #if (GTK_MINOR_VERSION > 0)
74 /* reserved_1 */ NULL,
75 /* reserved_2 */ NULL,
76 (GtkClassInitFunc) NULL,
77 #else
78 (GtkArgSetFunc) NULL,
79 (GtkArgGetFunc) NULL,
80 #endif
81 };
82 myfixed_type = gtk_type_unique (gtk_container_get_type (), &myfixed_info);
83 }
84
85 return myfixed_type;
86 }
87
88 static void
89 gtk_myfixed_class_init (GtkMyFixedClass *klass)
90 {
91 GtkObjectClass *object_class;
92 GtkWidgetClass *widget_class;
93 GtkContainerClass *container_class;
94
95 object_class = (GtkObjectClass*) klass;
96 widget_class = (GtkWidgetClass*) klass;
97 container_class = (GtkContainerClass*) klass;
98
99 #if (GTK_MINOR_VERSION > 0)
100 parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
101 #else
102 parent_class = gtk_type_class (gtk_container_get_type ());
103 #endif
104
105 widget_class->map = gtk_myfixed_map;
106 #if (GTK_MINOR_VERSION == 0)
107 widget_class->unmap = gtk_myfixed_unmap;
108 #endif
109 widget_class->realize = gtk_myfixed_realize;
110 widget_class->size_request = gtk_myfixed_size_request;
111 widget_class->size_allocate = gtk_myfixed_size_allocate;
112 widget_class->draw = gtk_myfixed_draw;
113 widget_class->expose_event = gtk_myfixed_expose;
114
115 container_class->add = gtk_myfixed_add;
116 container_class->remove = gtk_myfixed_remove;
117 #if (GTK_MINOR_VERSION > 0)
118 container_class->forall = gtk_myfixed_foreach;
119 #else
120 container_class->foreach = gtk_myfixed_foreach;
121 #endif
122
123 #if (GTK_MINOR_VERSION > 0)
124 container_class->child_type = gtk_myfixed_child_type;
125 #endif
126
127 #if (GTK_MINOR_VERSION > 0)
128 klass->set_scroll_adjustments = gtk_myfixed_scroll_set_adjustments;
129
130 widget_class->set_scroll_adjustments_signal =
131 gtk_signal_new ("set_scroll_adjustments",
132 GTK_RUN_LAST,
133 object_class->type,
134 GTK_SIGNAL_OFFSET (GtkMyFixedClass, set_scroll_adjustments),
135 gtk_marshal_NONE__POINTER_POINTER,
136 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
137 #endif
138 }
139
140 #if (GTK_MINOR_VERSION > 0)
141 static GtkType
142 gtk_myfixed_child_type (GtkContainer *container)
143 {
144 return GTK_TYPE_WIDGET;
145 }
146 #endif
147
148 static void
149 gtk_myfixed_init (GtkMyFixed *myfixed)
150 {
151 GTK_WIDGET_UNSET_FLAGS (myfixed, GTK_NO_WINDOW);
152
153 #if (GTK_MINOR_VERSION == 0)
154 GTK_WIDGET_SET_FLAGS (myfixed, GTK_BASIC);
155 #endif
156
157 #if (GTK_MINOR_VERSION > 0)
158 myfixed->shadow_type = GTK_SHADOW_NONE;
159 #endif
160
161 myfixed->children = NULL;
162 }
163
164 GtkWidget*
165 gtk_myfixed_new ()
166 {
167 GtkMyFixed *myfixed;
168
169 myfixed = gtk_type_new (gtk_myfixed_get_type ());
170
171 return GTK_WIDGET (myfixed);
172 }
173
174 #if (GTK_MINOR_VERSION > 0)
175 void
176 gtk_myfixed_scroll_set_adjustments (GtkMyFixed *myfixed,
177 GtkAdjustment *hadj,
178 GtkAdjustment *vadj)
179 {
180 /* OK, this is embarassing, but this function has to be here */
181 }
182
183 void
184 gtk_myfixed_set_shadow_type (GtkMyFixed *myfixed,
185 GtkShadowType type)
186 {
187 g_return_if_fail (myfixed != NULL);
188 g_return_if_fail (GTK_IS_MYFIXED (myfixed));
189
190 if ((GtkShadowType) myfixed->shadow_type != type)
191 {
192 myfixed->shadow_type = type;
193
194 if (GTK_WIDGET_VISIBLE (myfixed))
195 {
196 gtk_widget_size_allocate (GTK_WIDGET (myfixed), &(GTK_WIDGET (myfixed)->allocation));
197 gtk_widget_queue_draw (GTK_WIDGET (myfixed));
198 }
199 }
200 }
201 #endif
202
203 void
204 gtk_myfixed_put (GtkMyFixed *myfixed,
205 GtkWidget *widget,
206 gint16 x,
207 gint16 y,
208 gint16 width,
209 gint16 height)
210 {
211 GtkMyFixedChild *child_info;
212
213 g_return_if_fail (myfixed != NULL);
214 g_return_if_fail (GTK_IS_MYFIXED (myfixed));
215 g_return_if_fail (widget != NULL);
216
217 child_info = g_new (GtkMyFixedChild, 1);
218 child_info->widget = widget;
219 child_info->x = x;
220 child_info->y = y;
221 child_info->width = width;
222 child_info->height = height;
223
224 gtk_widget_set_parent (widget, GTK_WIDGET (myfixed));
225
226 myfixed->children = g_list_append (myfixed->children, child_info);
227
228 if (GTK_WIDGET_REALIZED (myfixed))
229 gtk_widget_realize (widget);
230
231 if (GTK_WIDGET_VISIBLE (myfixed) && GTK_WIDGET_VISIBLE (widget))
232 {
233 if (GTK_WIDGET_MAPPED (myfixed))
234 gtk_widget_map (widget);
235
236 gtk_widget_queue_resize (GTK_WIDGET (myfixed));
237 }
238 }
239
240 void
241 gtk_myfixed_move (GtkMyFixed *myfixed,
242 GtkWidget *widget,
243 gint16 x,
244 gint16 y)
245 {
246 GtkMyFixedChild *child;
247 GList *children;
248
249 g_return_if_fail (myfixed != NULL);
250 g_return_if_fail (GTK_IS_MYFIXED (myfixed));
251 g_return_if_fail (widget != NULL);
252
253 children = myfixed->children;
254 while (children)
255 {
256 child = children->data;
257 children = children->next;
258
259 if (child->widget == widget)
260 {
261 gtk_myfixed_set_size( myfixed, widget, x, y, child->width, child->height );
262 break;
263 }
264 }
265 }
266
267 void
268 gtk_myfixed_resize (GtkMyFixed *myfixed,
269 GtkWidget *widget,
270 gint16 width,
271 gint16 height)
272 {
273 GtkMyFixedChild *child;
274 GList *children;
275
276 g_return_if_fail (myfixed != NULL);
277 g_return_if_fail (GTK_IS_MYFIXED (myfixed));
278 g_return_if_fail (widget != NULL);
279
280 children = myfixed->children;
281 while (children)
282 {
283 child = children->data;
284 children = children->next;
285
286 if (child->widget == widget)
287 {
288 gtk_myfixed_set_size( myfixed, widget, child->x, child->y, width, height );
289 break;
290 }
291 }
292 }
293
294 void
295 gtk_myfixed_set_size (GtkMyFixed *myfixed,
296 GtkWidget *widget,
297 gint16 x,
298 gint16 y,
299 gint16 width,
300 gint16 height)
301 {
302 GtkMyFixedChild *child;
303 GList *children;
304 GtkAllocation child_allocation;
305
306 g_return_if_fail (myfixed != NULL);
307 g_return_if_fail (GTK_IS_MYFIXED (myfixed));
308 g_return_if_fail (widget != NULL);
309
310 children = myfixed->children;
311 while (children)
312 {
313 child = children->data;
314 children = children->next;
315
316 if (child->widget == widget)
317 {
318 if ((child->x == x) &&
319 (child->y == y) &&
320 (child->width == width) &&
321 (child->height == height)) return;
322
323 child->x = x;
324 child->y = y;
325 child->width = width;
326 child->height = height;
327
328 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (myfixed))
329 {
330 if ((child->width > 1) && (child->height > 1) && (GTK_WIDGET_REALIZED(widget)))
331 {
332 child_allocation.x = child->x;
333 child_allocation.y = child->y;
334 child_allocation.width = MAX( child->width, 1 );
335 child_allocation.height = MAX( child->height, 1 );
336
337 /* work around for GTK bug when moving widgets outside
338 the X window -> do NOT move them entirely outside */
339 if (child_allocation.y + child_allocation.height < 0)
340 child_allocation.y = -child_allocation.height;
341 if (child_allocation.x + child_allocation.width < 0)
342 child_allocation.x = -child_allocation.width;
343
344 gtk_widget_size_allocate (widget, &child_allocation);
345 }
346 else
347 {
348 gtk_widget_queue_resize (GTK_WIDGET (myfixed));
349 }
350 }
351 break;
352 }
353 }
354 }
355
356 static void
357 gtk_myfixed_map (GtkWidget *widget)
358 {
359 GtkMyFixed *myfixed;
360 GtkMyFixedChild *child;
361 GList *children;
362
363 g_return_if_fail (widget != NULL);
364 g_return_if_fail (GTK_IS_MYFIXED (widget));
365
366 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
367 myfixed = GTK_MYFIXED (widget);
368
369 children = myfixed->children;
370 while (children)
371 {
372 child = children->data;
373 children = children->next;
374
375 if (GTK_WIDGET_VISIBLE (child->widget) && !GTK_WIDGET_MAPPED (child->widget))
376 gtk_widget_map (child->widget);
377 }
378
379 gdk_window_show (widget->window);
380 }
381
382 #if (GTK_MINOR_VERSION == 0)
383 static void
384 gtk_myfixed_unmap (GtkWidget *widget)
385 {
386 g_return_if_fail (widget != NULL);
387 g_return_if_fail (GTK_IS_MYFIXED (widget));
388
389 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
390 }
391 #endif
392
393 static void
394 gtk_myfixed_realize (GtkWidget *widget)
395 {
396 GtkMyFixed *myfixed;
397 GdkWindowAttr attributes;
398 gint attributes_mask;
399
400 g_return_if_fail (widget != NULL);
401 g_return_if_fail (GTK_IS_MYFIXED (widget));
402
403 myfixed = GTK_MYFIXED (widget);
404
405 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
406
407 attributes.window_type = GDK_WINDOW_CHILD;
408
409 #if (GTK_MINOR_VERSION > 0)
410 attributes.x = widget->allocation.x;
411 attributes.y = widget->allocation.y;
412 attributes.width = widget->allocation.width;
413 attributes.height = widget->allocation.height;
414
415 if (myfixed->shadow_type != GTK_SHADOW_NONE)
416 {
417 attributes.x += 2;
418 attributes.y += 2;
419 attributes.width -= 4;
420 attributes.height -= 4;
421 }
422
423 if (attributes.width < 2) attributes.width = 2;
424 if (attributes.height < 2) attributes.height = 2;
425 #else
426 attributes.x = widget->allocation.x;
427 attributes.y = widget->allocation.y;
428 attributes.width = 32000;
429 attributes.height = 32000;
430 #endif
431 attributes.wclass = GDK_INPUT_OUTPUT;
432 attributes.visual = gtk_widget_get_visual (widget);
433 attributes.colormap = gtk_widget_get_colormap (widget);
434 attributes.event_mask = gtk_widget_get_events (widget);
435 attributes.event_mask |=
436 GDK_EXPOSURE_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 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
451
452 widget->window = gdk_window_new( gtk_widget_get_parent_window (widget), &attributes,
453 attributes_mask);
454 gdk_window_set_user_data (widget->window, widget);
455
456 widget->style = gtk_style_attach (widget->style, widget->window);
457 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
458 }
459
460 static void
461 gtk_myfixed_size_request (GtkWidget *widget,
462 GtkRequisition *requisition)
463 {
464 GtkMyFixed *myfixed;
465 GtkMyFixedChild *child;
466 GList *children;
467 GtkRequisition child_requisition;
468
469 g_return_if_fail (widget != NULL);
470 g_return_if_fail (GTK_IS_MYFIXED (widget));
471 g_return_if_fail (requisition != NULL);
472
473 myfixed = GTK_MYFIXED (widget);
474
475 children = myfixed->children;
476 while (children)
477 {
478 child = children->data;
479 children = children->next;
480
481 if (GTK_WIDGET_VISIBLE (child->widget))
482 {
483 gtk_widget_size_request (child->widget, &child_requisition);
484 }
485 }
486
487 /* request very little, I'm not sure if requesting nothing
488 will always have positive effects on stability... */
489 requisition->width = 2;
490 requisition->height = 2;
491 }
492
493 static void
494 gtk_myfixed_size_allocate (GtkWidget *widget,
495 GtkAllocation *allocation)
496 {
497 GtkMyFixed *myfixed;
498 gint border;
499 GtkMyFixedChild *child;
500 GtkAllocation child_allocation;
501 GList *children;
502
503 g_return_if_fail (widget != NULL);
504 g_return_if_fail (GTK_IS_MYFIXED(widget));
505 g_return_if_fail (allocation != NULL);
506
507 myfixed = GTK_MYFIXED (widget);
508
509 widget->allocation = *allocation;
510 #if (GTK_MINOR_VERSION > 0)
511 if (myfixed->shadow_type == GTK_SHADOW_NONE)
512 border = 0;
513 else
514 border = 2;
515 #else
516 border = 0;
517 #endif
518
519 if (GTK_WIDGET_REALIZED (widget))
520 {
521 gdk_window_move_resize( widget->window,
522 allocation->x+border, allocation->y+border,
523 #if (GTK_MINOR_VERSION > 0)
524 allocation->width-border*2, allocation->height-border*2
525 #else
526 32000, 32000
527 #endif
528 );
529 }
530
531 children = myfixed->children;
532 while (children)
533 {
534 child = children->data;
535 children = children->next;
536
537 /* please look at the text in wxWindow::DoSetSize() on why the
538 test GTK_WIDGET_REALIZED() has to be here */
539 if (GTK_WIDGET_VISIBLE (child->widget))
540 {
541 /* if (GTK_IS_NOTEBOOK(child->widget) && !GTK_WIDGET_REALIZED(child->widget))
542 {
543 gtk_widget_queue_resize( child->widget );
544 }
545 else */
546 {
547 child_allocation.x = child->x;
548 child_allocation.y = child->y;
549 child_allocation.width = MAX( child->width, 1 );
550 child_allocation.height = MAX( child->height, 1 );
551
552 /* work around for GTK bug when moving widgets outside
553 the X window -> do NOT move them entirely outside */
554 if (child_allocation.y + child_allocation.height < 0)
555 child_allocation.y = -child_allocation.height;
556 if (child_allocation.x + child_allocation.width < 0)
557 child_allocation.x = -child_allocation.width;
558
559 gtk_widget_size_allocate (child->widget, &child_allocation);
560 }
561 }
562 }
563 }
564
565 static void
566 gtk_myfixed_paint (GtkWidget *widget,
567 GdkRectangle *area)
568 {
569 g_return_if_fail (widget != NULL);
570 g_return_if_fail (GTK_IS_MYFIXED (widget));
571 g_return_if_fail (area != NULL);
572
573 if (GTK_WIDGET_DRAWABLE (widget))
574 gdk_window_clear_area (widget->window,
575 area->x, area->y,
576 area->width, area->height);
577 }
578
579 static void
580 gtk_myfixed_draw (GtkWidget *widget,
581 GdkRectangle *area)
582 {
583 GtkMyFixed *myfixed;
584 GtkMyFixedChild *child;
585 GdkRectangle child_area;
586 GList *children;
587
588 g_return_if_fail (widget != NULL);
589 g_return_if_fail (GTK_IS_MYFIXED (widget));
590
591 if (GTK_WIDGET_DRAWABLE (widget))
592 {
593 myfixed = GTK_MYFIXED (widget);
594 gtk_myfixed_paint (widget, area);
595
596 children = myfixed->children;
597 while (children)
598 {
599 child = children->data;
600 children = children->next;
601
602 if (gtk_widget_intersect (child->widget, area, &child_area))
603 gtk_widget_draw (child->widget, &child_area);
604 }
605 }
606 }
607
608 static gint
609 gtk_myfixed_expose (GtkWidget *widget,
610 GdkEventExpose *event)
611 {
612 GtkMyFixed *myfixed;
613 GtkMyFixedChild *child;
614 GdkEventExpose child_event;
615 GList *children;
616
617 g_return_val_if_fail (widget != NULL, FALSE);
618 g_return_val_if_fail (GTK_IS_MYFIXED (widget), FALSE);
619 g_return_val_if_fail (event != NULL, FALSE);
620
621 if (GTK_WIDGET_DRAWABLE (widget))
622 {
623 myfixed = GTK_MYFIXED (widget);
624
625 child_event = *event;
626
627 children = myfixed->children;
628 while (children)
629 {
630 child = children->data;
631 children = children->next;
632
633 if (GTK_WIDGET_NO_WINDOW (child->widget) &&
634 gtk_widget_intersect (child->widget, &event->area,
635 &child_event.area))
636 gtk_widget_event (child->widget, (GdkEvent*) &child_event);
637 }
638 }
639
640 return FALSE;
641 }
642
643 static void
644 gtk_myfixed_add (GtkContainer *container,
645 GtkWidget *widget)
646 {
647 g_return_if_fail (container != NULL);
648 g_return_if_fail (GTK_IS_MYFIXED (container));
649 g_return_if_fail (widget != NULL);
650
651 gtk_myfixed_put (GTK_MYFIXED (container), widget, 0, 0, 20, 20 );
652 }
653
654 static void
655 gtk_myfixed_remove (GtkContainer *container,
656 GtkWidget *widget)
657 {
658 GtkMyFixed *myfixed;
659 GtkMyFixedChild *child;
660 GList *children;
661
662 g_return_if_fail (container != NULL);
663 g_return_if_fail (GTK_IS_MYFIXED (container));
664 g_return_if_fail (widget != NULL);
665
666 myfixed = GTK_MYFIXED (container);
667
668 children = myfixed->children;
669 while (children)
670 {
671 child = children->data;
672
673 if (child->widget == widget)
674 {
675 gboolean was_visible = GTK_WIDGET_VISIBLE (widget);
676
677 gtk_widget_unparent (widget);
678
679 myfixed->children = g_list_remove_link (myfixed->children, children);
680 g_list_free (children);
681 g_free (child);
682
683 if (was_visible && GTK_WIDGET_VISIBLE (container))
684 gtk_widget_queue_resize (GTK_WIDGET (container));
685
686 break;
687 }
688
689 children = children->next;
690 }
691 }
692
693 static void
694 gtk_myfixed_foreach (GtkContainer *container,
695 #if (GTK_MINOR_VERSION > 0)
696 gboolean include_internals,
697 #endif
698 GtkCallback callback,
699 gpointer callback_data)
700 {
701 GtkMyFixed *myfixed;
702 GtkMyFixedChild *child;
703 GList *children;
704
705 g_return_if_fail (container != NULL);
706 g_return_if_fail (GTK_IS_MYFIXED (container));
707 g_return_if_fail (callback != NULL);
708
709 myfixed = GTK_MYFIXED (container);
710
711 children = myfixed->children;
712 while (children)
713 {
714 child = children->data;
715 children = children->next;
716
717 (* callback) (child->widget, callback_data);
718 }
719 }
720
721
722 #ifdef __cplusplus
723 }
724 #endif /* __cplusplus */
725