1 /* ///////////////////////////////////////////////////////////////////////////
3 // Purpose: native GTK+ widget for wxWindows
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////// */
10 #include "wx/gtk/win_gtk.h"
11 #include "gtk/gtksignal.h"
12 #include "gtk/gtknotebook.h"
13 #include "gtk/gtktable.h"
17 #endif /* __cplusplus */
19 static void gtk_myfixed_class_init (GtkMyFixedClass
*klass
);
20 static void gtk_myfixed_init (GtkMyFixed
*myfixed
);
21 static void gtk_myfixed_map (GtkWidget
*widget
);
22 #if (GTK_MINOR_VERSION == 0)
23 static void gtk_myfixed_unmap (GtkWidget
*widget
);
25 static void gtk_myfixed_realize (GtkWidget
*widget
);
26 static void gtk_myfixed_size_request (GtkWidget
*widget
,
27 GtkRequisition
*requisition
);
28 static void gtk_myfixed_size_allocate (GtkWidget
*widget
,
29 GtkAllocation
*allocation
);
30 static void gtk_myfixed_paint (GtkWidget
*widget
,
32 static void gtk_myfixed_draw (GtkWidget
*widget
,
34 static gint
gtk_myfixed_expose (GtkWidget
*widget
,
35 GdkEventExpose
*event
);
36 static void gtk_myfixed_add (GtkContainer
*container
,
38 static void gtk_myfixed_remove (GtkContainer
*container
,
40 static void gtk_myfixed_foreach (GtkContainer
*container
,
41 #if (GTK_MINOR_VERSION > 0)
42 gboolean include_internals
,
45 gpointer callback_data
);
46 #if (GTK_MINOR_VERSION > 0)
47 static GtkType
gtk_myfixed_child_type (GtkContainer
*container
);
50 #if (GTK_MINOR_VERSION > 0)
51 static void gtk_myfixed_scroll_set_adjustments (GtkMyFixed
*myfixed
,
58 static GtkContainerClass
*parent_class
= NULL
;
61 gtk_myfixed_get_type ()
63 static guint myfixed_type
= 0;
67 GtkTypeInfo myfixed_info
=
71 sizeof (GtkMyFixedClass
),
72 (GtkClassInitFunc
) gtk_myfixed_class_init
,
73 (GtkObjectInitFunc
) gtk_myfixed_init
,
74 #if (GTK_MINOR_VERSION > 0)
75 /* reserved_1 */ NULL
,
76 /* reserved_2 */ NULL
,
77 (GtkClassInitFunc
) NULL
,
83 myfixed_type
= gtk_type_unique (gtk_container_get_type (), &myfixed_info
);
90 gtk_myfixed_class_init (GtkMyFixedClass
*klass
)
92 GtkObjectClass
*object_class
;
93 GtkWidgetClass
*widget_class
;
94 GtkContainerClass
*container_class
;
96 object_class
= (GtkObjectClass
*) klass
;
97 widget_class
= (GtkWidgetClass
*) klass
;
98 container_class
= (GtkContainerClass
*) klass
;
100 #if (GTK_MINOR_VERSION > 0)
101 parent_class
= gtk_type_class (GTK_TYPE_CONTAINER
);
103 parent_class
= gtk_type_class (gtk_container_get_type ());
106 widget_class
->map
= gtk_myfixed_map
;
107 #if (GTK_MINOR_VERSION == 0)
108 widget_class
->unmap
= gtk_myfixed_unmap
;
110 widget_class
->realize
= gtk_myfixed_realize
;
111 widget_class
->size_request
= gtk_myfixed_size_request
;
112 widget_class
->size_allocate
= gtk_myfixed_size_allocate
;
113 widget_class
->draw
= gtk_myfixed_draw
;
114 widget_class
->expose_event
= gtk_myfixed_expose
;
116 container_class
->add
= gtk_myfixed_add
;
117 container_class
->remove
= gtk_myfixed_remove
;
118 #if (GTK_MINOR_VERSION > 0)
119 container_class
->forall
= gtk_myfixed_foreach
;
121 container_class
->foreach
= gtk_myfixed_foreach
;
124 #if (GTK_MINOR_VERSION > 0)
125 container_class
->child_type
= gtk_myfixed_child_type
;
128 #if (GTK_MINOR_VERSION > 0)
129 klass
->set_scroll_adjustments
= gtk_myfixed_scroll_set_adjustments
;
131 widget_class
->set_scroll_adjustments_signal
=
132 gtk_signal_new ("set_scroll_adjustments",
135 GTK_SIGNAL_OFFSET (GtkMyFixedClass
, set_scroll_adjustments
),
136 gtk_marshal_NONE__POINTER_POINTER
,
137 GTK_TYPE_NONE
, 2, GTK_TYPE_ADJUSTMENT
, GTK_TYPE_ADJUSTMENT
);
141 #if (GTK_MINOR_VERSION > 0)
143 gtk_myfixed_child_type (GtkContainer
*container
)
145 return GTK_TYPE_WIDGET
;
150 gtk_myfixed_init (GtkMyFixed
*myfixed
)
152 GTK_WIDGET_UNSET_FLAGS (myfixed
, GTK_NO_WINDOW
);
154 #if (GTK_MINOR_VERSION == 0)
155 GTK_WIDGET_SET_FLAGS (myfixed
, GTK_BASIC
);
158 #if (GTK_MINOR_VERSION > 0)
159 myfixed
->shadow_type
= GTK_SHADOW_NONE
;
162 myfixed
->children
= NULL
;
170 myfixed
= gtk_type_new (gtk_myfixed_get_type ());
172 return GTK_WIDGET (myfixed
);
175 #if (GTK_MINOR_VERSION > 0)
177 gtk_myfixed_scroll_set_adjustments (GtkMyFixed
*myfixed
,
181 /* OK, this is embarassing, but this function has to be here */
185 gtk_myfixed_set_shadow_type (GtkMyFixed
*myfixed
,
188 g_return_if_fail (myfixed
!= NULL
);
189 g_return_if_fail (GTK_IS_MYFIXED (myfixed
));
191 if ((GtkShadowType
) myfixed
->shadow_type
!= type
)
193 myfixed
->shadow_type
= type
;
195 if (GTK_WIDGET_VISIBLE (myfixed
))
197 gtk_widget_size_allocate (GTK_WIDGET (myfixed
), &(GTK_WIDGET (myfixed
)->allocation
));
198 gtk_widget_queue_draw (GTK_WIDGET (myfixed
));
205 gtk_myfixed_put (GtkMyFixed
*myfixed
,
212 GtkMyFixedChild
*child_info
;
214 g_return_if_fail (myfixed
!= NULL
);
215 g_return_if_fail (GTK_IS_MYFIXED (myfixed
));
216 g_return_if_fail (widget
!= NULL
);
218 child_info
= g_new (GtkMyFixedChild
, 1);
219 child_info
->widget
= widget
;
222 child_info
->width
= width
;
223 child_info
->height
= height
;
225 gtk_widget_set_parent (widget
, GTK_WIDGET (myfixed
));
227 myfixed
->children
= g_list_append (myfixed
->children
, child_info
);
229 if (GTK_WIDGET_REALIZED (myfixed
))
230 gtk_widget_realize (widget
);
232 if (GTK_WIDGET_VISIBLE (myfixed
) && GTK_WIDGET_VISIBLE (widget
))
234 if (GTK_WIDGET_MAPPED (myfixed
))
235 gtk_widget_map (widget
);
237 gtk_widget_queue_resize (GTK_WIDGET (myfixed
));
242 gtk_myfixed_move (GtkMyFixed
*myfixed
,
247 GtkMyFixedChild
*child
;
250 g_return_if_fail (myfixed
!= NULL
);
251 g_return_if_fail (GTK_IS_MYFIXED (myfixed
));
252 g_return_if_fail (widget
!= NULL
);
254 children
= myfixed
->children
;
257 child
= children
->data
;
258 children
= children
->next
;
260 if (child
->widget
== widget
)
262 gtk_myfixed_set_size( myfixed
, widget
, x
, y
, child
->width
, child
->height
);
269 gtk_myfixed_resize (GtkMyFixed
*myfixed
,
274 GtkMyFixedChild
*child
;
277 g_return_if_fail (myfixed
!= NULL
);
278 g_return_if_fail (GTK_IS_MYFIXED (myfixed
));
279 g_return_if_fail (widget
!= NULL
);
281 children
= myfixed
->children
;
284 child
= children
->data
;
285 children
= children
->next
;
287 if (child
->widget
== widget
)
289 gtk_myfixed_set_size( myfixed
, widget
, child
->x
, child
->y
, width
, height
);
296 gtk_myfixed_set_size (GtkMyFixed
*myfixed
,
303 GtkMyFixedChild
*child
;
305 GtkAllocation child_allocation
;
307 g_return_if_fail (myfixed
!= NULL
);
308 g_return_if_fail (GTK_IS_MYFIXED (myfixed
));
309 g_return_if_fail (widget
!= NULL
);
311 children
= myfixed
->children
;
314 child
= children
->data
;
315 children
= children
->next
;
317 if (child
->widget
== widget
)
319 if ((child
->x
== x
) &&
321 (child
->width
== width
) &&
322 (child
->height
== height
)) return;
326 child
->width
= width
;
327 child
->height
= height
;
329 if (GTK_WIDGET_VISIBLE (widget
) && GTK_WIDGET_VISIBLE (myfixed
))
331 if ( ((child
->width
> 1) && (child
->height
> 1) && (GTK_WIDGET_REALIZED(widget
))) ||
332 /* I don't know why GtkTable is different */
333 GTK_IS_TABLE(widget
))
335 child_allocation
.x
= child
->x
;
336 child_allocation
.y
= child
->y
;
337 child_allocation
.width
= MAX( child
->width
, 1 );
338 child_allocation
.height
= MAX( child
->height
, 1 );
340 /* work around for GTK bug when moving widgets outside
341 the X window -> do NOT move them entirely outside */
342 if (child_allocation
.y
+ child_allocation
.height
< 0)
343 child_allocation
.y
= -child_allocation
.height
;
344 if (child_allocation
.x
+ child_allocation
.width
< 0)
345 child_allocation
.x
= -child_allocation
.width
;
347 gtk_widget_size_allocate (widget
, &child_allocation
);
351 gtk_widget_queue_resize (GTK_WIDGET (myfixed
));
360 gtk_myfixed_map (GtkWidget
*widget
)
363 GtkMyFixedChild
*child
;
366 g_return_if_fail (widget
!= NULL
);
367 g_return_if_fail (GTK_IS_MYFIXED (widget
));
369 GTK_WIDGET_SET_FLAGS (widget
, GTK_MAPPED
);
370 myfixed
= GTK_MYFIXED (widget
);
372 children
= myfixed
->children
;
375 child
= children
->data
;
376 children
= children
->next
;
378 if (GTK_WIDGET_VISIBLE (child
->widget
) && !GTK_WIDGET_MAPPED (child
->widget
))
379 gtk_widget_map (child
->widget
);
382 gdk_window_show (widget
->window
);
385 #if (GTK_MINOR_VERSION == 0)
387 gtk_myfixed_unmap (GtkWidget
*widget
)
389 g_return_if_fail (widget
!= NULL
);
390 g_return_if_fail (GTK_IS_MYFIXED (widget
));
392 GTK_WIDGET_UNSET_FLAGS (widget
, GTK_MAPPED
);
397 gtk_myfixed_realize (GtkWidget
*widget
)
400 GdkWindowAttr attributes
;
401 gint attributes_mask
;
403 g_return_if_fail (widget
!= NULL
);
404 g_return_if_fail (GTK_IS_MYFIXED (widget
));
406 myfixed
= GTK_MYFIXED (widget
);
408 GTK_WIDGET_SET_FLAGS (widget
, GTK_REALIZED
);
410 attributes
.window_type
= GDK_WINDOW_CHILD
;
412 #if (GTK_MINOR_VERSION > 0)
413 attributes
.x
= widget
->allocation
.x
;
414 attributes
.y
= widget
->allocation
.y
;
415 attributes
.width
= widget
->allocation
.width
;
416 attributes
.height
= widget
->allocation
.height
;
418 if (myfixed
->shadow_type
!= GTK_SHADOW_NONE
)
422 attributes
.width
-= 4;
423 attributes
.height
-= 4;
426 if (attributes
.width
< 2) attributes
.width
= 2;
427 if (attributes
.height
< 2) attributes
.height
= 2;
429 attributes
.x
= widget
->allocation
.x
;
430 attributes
.y
= widget
->allocation
.y
;
431 attributes
.width
= 32000;
432 attributes
.height
= 32000;
434 attributes
.wclass
= GDK_INPUT_OUTPUT
;
435 attributes
.visual
= gtk_widget_get_visual (widget
);
436 attributes
.colormap
= gtk_widget_get_colormap (widget
);
437 attributes
.event_mask
= gtk_widget_get_events (widget
);
438 attributes
.event_mask
|=
440 GDK_POINTER_MOTION_MASK
|
441 GDK_POINTER_MOTION_HINT_MASK
|
442 GDK_BUTTON_MOTION_MASK
|
443 GDK_BUTTON1_MOTION_MASK
|
444 GDK_BUTTON2_MOTION_MASK
|
445 GDK_BUTTON3_MOTION_MASK
|
446 GDK_BUTTON_PRESS_MASK
|
447 GDK_BUTTON_RELEASE_MASK
|
449 GDK_KEY_RELEASE_MASK
|
450 GDK_ENTER_NOTIFY_MASK
|
451 GDK_LEAVE_NOTIFY_MASK
|
452 GDK_FOCUS_CHANGE_MASK
;
453 attributes_mask
= GDK_WA_X
| GDK_WA_Y
| GDK_WA_VISUAL
| GDK_WA_COLORMAP
;
455 widget
->window
= gdk_window_new( gtk_widget_get_parent_window (widget
), &attributes
,
457 gdk_window_set_user_data (widget
->window
, widget
);
459 widget
->style
= gtk_style_attach (widget
->style
, widget
->window
);
460 gtk_style_set_background (widget
->style
, widget
->window
, GTK_STATE_NORMAL
);
464 gtk_myfixed_size_request (GtkWidget
*widget
,
465 GtkRequisition
*requisition
)
468 GtkMyFixedChild
*child
;
470 GtkRequisition child_requisition
;
472 g_return_if_fail (widget
!= NULL
);
473 g_return_if_fail (GTK_IS_MYFIXED (widget
));
474 g_return_if_fail (requisition
!= NULL
);
476 myfixed
= GTK_MYFIXED (widget
);
478 children
= myfixed
->children
;
481 child
= children
->data
;
482 children
= children
->next
;
484 if (GTK_WIDGET_VISIBLE (child
->widget
))
486 gtk_widget_size_request (child
->widget
, &child_requisition
);
490 /* request very little, I'm not sure if requesting nothing
491 will always have positive effects on stability... */
492 requisition
->width
= 2;
493 requisition
->height
= 2;
497 gtk_myfixed_size_allocate (GtkWidget
*widget
,
498 GtkAllocation
*allocation
)
502 GtkMyFixedChild
*child
;
503 GtkAllocation child_allocation
;
506 g_return_if_fail (widget
!= NULL
);
507 g_return_if_fail (GTK_IS_MYFIXED(widget
));
508 g_return_if_fail (allocation
!= NULL
);
510 myfixed
= GTK_MYFIXED (widget
);
512 widget
->allocation
= *allocation
;
513 #if (GTK_MINOR_VERSION > 0)
514 if (myfixed
->shadow_type
== GTK_SHADOW_NONE
)
522 if (GTK_WIDGET_REALIZED (widget
))
524 gdk_window_move_resize( widget
->window
,
525 allocation
->x
+border
, allocation
->y
+border
,
526 #if (GTK_MINOR_VERSION > 0)
527 allocation
->width
-border
*2, allocation
->height
-border
*2
534 children
= myfixed
->children
;
537 child
= children
->data
;
538 children
= children
->next
;
540 /* please look at the text in wxWindow::DoSetSize() on why the
541 test GTK_WIDGET_REALIZED() has to be here */
542 if (GTK_WIDGET_VISIBLE (child
->widget
))
544 /* if (GTK_IS_NOTEBOOK(child->widget) && !GTK_WIDGET_REALIZED(child->widget))
546 gtk_widget_queue_resize( child->widget );
550 child_allocation
.x
= child
->x
;
551 child_allocation
.y
= child
->y
;
552 child_allocation
.width
= MAX( child
->width
, 1 );
553 child_allocation
.height
= MAX( child
->height
, 1 );
555 /* work around for GTK bug when moving widgets outside
556 the X window -> do NOT move them entirely outside */
557 if (child_allocation
.y
+ child_allocation
.height
< 0)
558 child_allocation
.y
= -child_allocation
.height
;
559 if (child_allocation
.x
+ child_allocation
.width
< 0)
560 child_allocation
.x
= -child_allocation
.width
;
562 gtk_widget_size_allocate (child
->widget
, &child_allocation
);
569 gtk_myfixed_paint (GtkWidget
*widget
,
572 g_return_if_fail (widget
!= NULL
);
573 g_return_if_fail (GTK_IS_MYFIXED (widget
));
574 g_return_if_fail (area
!= NULL
);
576 if (GTK_WIDGET_DRAWABLE (widget
))
577 gdk_window_clear_area (widget
->window
,
579 area
->width
, area
->height
);
583 gtk_myfixed_draw (GtkWidget
*widget
,
587 GtkMyFixedChild
*child
;
588 GdkRectangle child_area
;
591 g_return_if_fail (widget
!= NULL
);
592 g_return_if_fail (GTK_IS_MYFIXED (widget
));
594 if (GTK_WIDGET_DRAWABLE (widget
))
596 myfixed
= GTK_MYFIXED (widget
);
597 gtk_myfixed_paint (widget
, area
);
599 children
= myfixed
->children
;
602 child
= children
->data
;
603 children
= children
->next
;
605 if (gtk_widget_intersect (child
->widget
, area
, &child_area
))
606 gtk_widget_draw (child
->widget
, &child_area
);
612 gtk_myfixed_expose (GtkWidget
*widget
,
613 GdkEventExpose
*event
)
616 GtkMyFixedChild
*child
;
617 GdkEventExpose child_event
;
620 g_return_val_if_fail (widget
!= NULL
, FALSE
);
621 g_return_val_if_fail (GTK_IS_MYFIXED (widget
), FALSE
);
622 g_return_val_if_fail (event
!= NULL
, FALSE
);
624 if (GTK_WIDGET_DRAWABLE (widget
))
626 myfixed
= GTK_MYFIXED (widget
);
628 child_event
= *event
;
630 children
= myfixed
->children
;
633 child
= children
->data
;
634 children
= children
->next
;
636 if (GTK_WIDGET_NO_WINDOW (child
->widget
) &&
637 gtk_widget_intersect (child
->widget
, &event
->area
,
639 gtk_widget_event (child
->widget
, (GdkEvent
*) &child_event
);
647 gtk_myfixed_add (GtkContainer
*container
,
650 g_return_if_fail (container
!= NULL
);
651 g_return_if_fail (GTK_IS_MYFIXED (container
));
652 g_return_if_fail (widget
!= NULL
);
654 gtk_myfixed_put (GTK_MYFIXED (container
), widget
, 0, 0, 20, 20 );
658 gtk_myfixed_remove (GtkContainer
*container
,
662 GtkMyFixedChild
*child
;
665 g_return_if_fail (container
!= NULL
);
666 g_return_if_fail (GTK_IS_MYFIXED (container
));
667 g_return_if_fail (widget
!= NULL
);
669 myfixed
= GTK_MYFIXED (container
);
671 children
= myfixed
->children
;
674 child
= children
->data
;
676 if (child
->widget
== widget
)
678 gboolean was_visible
= GTK_WIDGET_VISIBLE (widget
);
680 gtk_widget_unparent (widget
);
682 myfixed
->children
= g_list_remove_link (myfixed
->children
, children
);
683 g_list_free (children
);
686 if (was_visible
&& GTK_WIDGET_VISIBLE (container
))
687 gtk_widget_queue_resize (GTK_WIDGET (container
));
692 children
= children
->next
;
697 gtk_myfixed_foreach (GtkContainer
*container
,
698 #if (GTK_MINOR_VERSION > 0)
699 gboolean include_internals
,
701 GtkCallback callback
,
702 gpointer callback_data
)
705 GtkMyFixedChild
*child
;
708 g_return_if_fail (container
!= NULL
);
709 g_return_if_fail (GTK_IS_MYFIXED (container
));
710 g_return_if_fail (callback
!= NULL
);
712 myfixed
= GTK_MYFIXED (container
);
714 children
= myfixed
->children
;
717 child
= children
->data
;
718 children
= children
->next
;
720 (* callback
) (child
->widget
, callback_data
);
727 #endif /* __cplusplus */