New attempt at getting constraints work
[wxWidgets.git] / src / gtk1 / 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) &&
331 (child->height > 1) &&
332 !(GTK_WIDGET_REALIZED(widget) && GTK_IS_NOTEBOOK(widget)) )
333 {
334 child_allocation.x = child->x;
335 child_allocation.y = child->y;
336 child_allocation.width = MAX( child->width, 1 );
337 child_allocation.height = MAX( child->height, 1 );
338
339 /* work around for GTK bug when moving widgets outside
340 the X window -> do NOT move them entirely outside */
341 if (child_allocation.y + child_allocation.height < 0)
342 child_allocation.y = -child_allocation.height;
343 if (child_allocation.x + child_allocation.width < 0)
344 child_allocation.x = -child_allocation.width;
345
346 gtk_widget_size_allocate (widget, &child_allocation);
347 }
348 else
349 {
350 gtk_widget_queue_resize (GTK_WIDGET (myfixed));
351 }
352 }
353 break;
354 }
355 }
356 }
357
358 static void
359 gtk_myfixed_map (GtkWidget *widget)
360 {
361 GtkMyFixed *myfixed;
362 GtkMyFixedChild *child;
363 GList *children;
364
365 g_return_if_fail (widget != NULL);
366 g_return_if_fail (GTK_IS_MYFIXED (widget));
367
368 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
369 myfixed = GTK_MYFIXED (widget);
370
371 children = myfixed->children;
372 while (children)
373 {
374 child = children->data;
375 children = children->next;
376
377 if (GTK_WIDGET_VISIBLE (child->widget) && !GTK_WIDGET_MAPPED (child->widget))
378 gtk_widget_map (child->widget);
379 }
380
381 gdk_window_show (widget->window);
382 }
383
384 #if (GTK_MINOR_VERSION == 0)
385 static void
386 gtk_myfixed_unmap (GtkWidget *widget)
387 {
388 g_return_if_fail (widget != NULL);
389 g_return_if_fail (GTK_IS_MYFIXED (widget));
390
391 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
392 }
393 #endif
394
395 static void
396 gtk_myfixed_realize (GtkWidget *widget)
397 {
398 GtkMyFixed *myfixed;
399 GdkWindowAttr attributes;
400 gint attributes_mask;
401
402 g_return_if_fail (widget != NULL);
403 g_return_if_fail (GTK_IS_MYFIXED (widget));
404
405 myfixed = GTK_MYFIXED (widget);
406
407 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
408
409 attributes.window_type = GDK_WINDOW_CHILD;
410
411 #if (GTK_MINOR_VERSION > 0)
412 attributes.x = widget->allocation.x;
413 attributes.y = widget->allocation.y;
414 attributes.width = widget->allocation.width;
415 attributes.height = widget->allocation.height;
416
417 if (myfixed->shadow_type != GTK_SHADOW_NONE)
418 {
419 attributes.x += 2;
420 attributes.y += 2;
421 attributes.width -= 4;
422 attributes.height -= 4;
423 }
424
425 if (attributes.width < 2) attributes.width = 2;
426 if (attributes.height < 2) attributes.height = 2;
427 #else
428 attributes.x = widget->allocation.x;
429 attributes.y = widget->allocation.y;
430 attributes.width = 32000;
431 attributes.height = 32000;
432 #endif
433 attributes.wclass = GDK_INPUT_OUTPUT;
434 attributes.visual = gtk_widget_get_visual (widget);
435 attributes.colormap = gtk_widget_get_colormap (widget);
436 attributes.event_mask = gtk_widget_get_events (widget);
437 attributes.event_mask |=
438 GDK_EXPOSURE_MASK |
439 GDK_POINTER_MOTION_MASK |
440 GDK_POINTER_MOTION_HINT_MASK |
441 GDK_BUTTON_MOTION_MASK |
442 GDK_BUTTON1_MOTION_MASK |
443 GDK_BUTTON2_MOTION_MASK |
444 GDK_BUTTON3_MOTION_MASK |
445 GDK_BUTTON_PRESS_MASK |
446 GDK_BUTTON_RELEASE_MASK |
447 GDK_KEY_PRESS_MASK |
448 GDK_KEY_RELEASE_MASK |
449 GDK_ENTER_NOTIFY_MASK |
450 GDK_LEAVE_NOTIFY_MASK |
451 GDK_FOCUS_CHANGE_MASK;
452 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
453
454 widget->window = gdk_window_new( gtk_widget_get_parent_window (widget), &attributes,
455 attributes_mask);
456 gdk_window_set_user_data (widget->window, widget);
457
458 widget->style = gtk_style_attach (widget->style, widget->window);
459 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
460 }
461
462 static void
463 gtk_myfixed_size_request (GtkWidget *widget,
464 GtkRequisition *requisition)
465 {
466 GtkMyFixed *myfixed;
467 GtkMyFixedChild *child;
468 GList *children;
469 GtkRequisition child_requisition;
470
471 g_return_if_fail (widget != NULL);
472 g_return_if_fail (GTK_IS_MYFIXED (widget));
473 g_return_if_fail (requisition != NULL);
474
475 myfixed = GTK_MYFIXED (widget);
476
477 children = myfixed->children;
478 while (children)
479 {
480 child = children->data;
481 children = children->next;
482
483 if (GTK_WIDGET_VISIBLE (child->widget))
484 {
485 gtk_widget_size_request (child->widget, &child_requisition);
486 }
487 }
488
489 /* request very little, I'm not sure if requesting nothing
490 will always have positive effects on stability... */
491 requisition->width = 2;
492 requisition->height = 2;
493 }
494
495 static void
496 gtk_myfixed_size_allocate (GtkWidget *widget,
497 GtkAllocation *allocation)
498 {
499 GtkMyFixed *myfixed;
500 gint border;
501 GtkMyFixedChild *child;
502 GtkAllocation child_allocation;
503 GList *children;
504
505 g_return_if_fail (widget != NULL);
506 g_return_if_fail (GTK_IS_MYFIXED(widget));
507 g_return_if_fail (allocation != NULL);
508
509 myfixed = GTK_MYFIXED (widget);
510
511 widget->allocation = *allocation;
512 #if (GTK_MINOR_VERSION > 0)
513 if (myfixed->shadow_type == GTK_SHADOW_NONE)
514 border = 0;
515 else
516 border = 2;
517 #else
518 border = 0;
519 #endif
520
521 if (GTK_WIDGET_REALIZED (widget))
522 {
523 gdk_window_move_resize( widget->window,
524 allocation->x+border, allocation->y+border,
525 #if (GTK_MINOR_VERSION > 0)
526 allocation->width-border*2, allocation->height-border*2
527 #else
528 32000, 32000
529 #endif
530 );
531 }
532
533 children = myfixed->children;
534 while (children)
535 {
536 child = children->data;
537 children = children->next;
538
539 /* please look at the text in wxWindow::DoSetSize() on why the
540 test GTK_WIDGET_REALIZED() has to be here */
541 if (GTK_WIDGET_VISIBLE (child->widget))
542 {
543 /* if (GTK_IS_NOTEBOOK(child->widget) && !GTK_WIDGET_REALIZED(child->widget))
544 {
545 gtk_widget_queue_resize( child->widget );
546 }
547 else */
548 {
549 child_allocation.x = child->x;
550 child_allocation.y = child->y;
551 child_allocation.width = MAX( child->width, 1 );
552 child_allocation.height = MAX( child->height, 1 );
553
554 /* work around for GTK bug when moving widgets outside
555 the X window -> do NOT move them entirely outside */
556 if (child_allocation.y + child_allocation.height < 0)
557 child_allocation.y = -child_allocation.height;
558 if (child_allocation.x + child_allocation.width < 0)
559 child_allocation.x = -child_allocation.width;
560
561 gtk_widget_size_allocate (child->widget, &child_allocation);
562 }
563 }
564 }
565 }
566
567 static void
568 gtk_myfixed_paint (GtkWidget *widget,
569 GdkRectangle *area)
570 {
571 g_return_if_fail (widget != NULL);
572 g_return_if_fail (GTK_IS_MYFIXED (widget));
573 g_return_if_fail (area != NULL);
574
575 if (GTK_WIDGET_DRAWABLE (widget))
576 gdk_window_clear_area (widget->window,
577 area->x, area->y,
578 area->width, area->height);
579 }
580
581 static void
582 gtk_myfixed_draw (GtkWidget *widget,
583 GdkRectangle *area)
584 {
585 GtkMyFixed *myfixed;
586 GtkMyFixedChild *child;
587 GdkRectangle child_area;
588 GList *children;
589
590 g_return_if_fail (widget != NULL);
591 g_return_if_fail (GTK_IS_MYFIXED (widget));
592
593 if (GTK_WIDGET_DRAWABLE (widget))
594 {
595 myfixed = GTK_MYFIXED (widget);
596 gtk_myfixed_paint (widget, area);
597
598 children = myfixed->children;
599 while (children)
600 {
601 child = children->data;
602 children = children->next;
603
604 if (gtk_widget_intersect (child->widget, area, &child_area))
605 gtk_widget_draw (child->widget, &child_area);
606 }
607 }
608 }
609
610 static gint
611 gtk_myfixed_expose (GtkWidget *widget,
612 GdkEventExpose *event)
613 {
614 GtkMyFixed *myfixed;
615 GtkMyFixedChild *child;
616 GdkEventExpose child_event;
617 GList *children;
618
619 g_return_val_if_fail (widget != NULL, FALSE);
620 g_return_val_if_fail (GTK_IS_MYFIXED (widget), FALSE);
621 g_return_val_if_fail (event != NULL, FALSE);
622
623 if (GTK_WIDGET_DRAWABLE (widget))
624 {
625 myfixed = GTK_MYFIXED (widget);
626
627 child_event = *event;
628
629 children = myfixed->children;
630 while (children)
631 {
632 child = children->data;
633 children = children->next;
634
635 if (GTK_WIDGET_NO_WINDOW (child->widget) &&
636 gtk_widget_intersect (child->widget, &event->area,
637 &child_event.area))
638 gtk_widget_event (child->widget, (GdkEvent*) &child_event);
639 }
640 }
641
642 return FALSE;
643 }
644
645 static void
646 gtk_myfixed_add (GtkContainer *container,
647 GtkWidget *widget)
648 {
649 g_return_if_fail (container != NULL);
650 g_return_if_fail (GTK_IS_MYFIXED (container));
651 g_return_if_fail (widget != NULL);
652
653 gtk_myfixed_put (GTK_MYFIXED (container), widget, 0, 0, 20, 20 );
654 }
655
656 static void
657 gtk_myfixed_remove (GtkContainer *container,
658 GtkWidget *widget)
659 {
660 GtkMyFixed *myfixed;
661 GtkMyFixedChild *child;
662 GList *children;
663
664 g_return_if_fail (container != NULL);
665 g_return_if_fail (GTK_IS_MYFIXED (container));
666 g_return_if_fail (widget != NULL);
667
668 myfixed = GTK_MYFIXED (container);
669
670 children = myfixed->children;
671 while (children)
672 {
673 child = children->data;
674
675 if (child->widget == widget)
676 {
677 gboolean was_visible = GTK_WIDGET_VISIBLE (widget);
678
679 gtk_widget_unparent (widget);
680
681 myfixed->children = g_list_remove_link (myfixed->children, children);
682 g_list_free (children);
683 g_free (child);
684
685 if (was_visible && GTK_WIDGET_VISIBLE (container))
686 gtk_widget_queue_resize (GTK_WIDGET (container));
687
688 break;
689 }
690
691 children = children->next;
692 }
693 }
694
695 static void
696 gtk_myfixed_foreach (GtkContainer *container,
697 #if (GTK_MINOR_VERSION > 0)
698 gboolean include_internals,
699 #endif
700 GtkCallback callback,
701 gpointer callback_data)
702 {
703 GtkMyFixed *myfixed;
704 GtkMyFixedChild *child;
705 GList *children;
706
707 g_return_if_fail (container != NULL);
708 g_return_if_fail (GTK_IS_MYFIXED (container));
709 g_return_if_fail (callback != NULL);
710
711 myfixed = GTK_MYFIXED (container);
712
713 children = myfixed->children;
714 while (children)
715 {
716 child = children->data;
717 children = children->next;
718
719 (* callback) (child->widget, callback_data);
720 }
721 }
722
723
724 #ifdef __cplusplus
725 }
726 #endif /* __cplusplus */
727