]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/win_gtk.c
const-ness warnings in non-Unicode again.
[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
13 #ifdef __cplusplus
14 extern "C" {
15 #endif /* __cplusplus */
16
17 static void gtk_myfixed_class_init (GtkMyFixedClass *klass);
18 static void gtk_myfixed_init (GtkMyFixed *myfixed);
19 static void gtk_myfixed_map (GtkWidget *widget);
20 #if (GTK_MINOR_VERSION == 0)
21 static void gtk_myfixed_unmap (GtkWidget *widget);
22 #endif
23 static void gtk_myfixed_realize (GtkWidget *widget);
24 static void gtk_myfixed_size_request (GtkWidget *widget,
25 GtkRequisition *requisition);
26 static void gtk_myfixed_size_allocate (GtkWidget *widget,
27 GtkAllocation *allocation);
28 static void gtk_myfixed_paint (GtkWidget *widget,
29 GdkRectangle *area);
30 static void gtk_myfixed_draw (GtkWidget *widget,
31 GdkRectangle *area);
32 static gint gtk_myfixed_expose (GtkWidget *widget,
33 GdkEventExpose *event);
34 static void gtk_myfixed_add (GtkContainer *container,
35 GtkWidget *widget);
36 static void gtk_myfixed_remove (GtkContainer *container,
37 GtkWidget *widget);
38 static void gtk_myfixed_foreach (GtkContainer *container,
39 #if (GTK_MINOR_VERSION > 0)
40 gboolean include_internals,
41 #endif
42 GtkCallback callback,
43 gpointer callback_data);
44 #if (GTK_MINOR_VERSION > 0)
45 static GtkType gtk_myfixed_child_type (GtkContainer *container);
46 #endif
47
48 #if (GTK_MINOR_VERSION > 0)
49 static void gtk_myfixed_scroll_set_adjustments (GtkMyFixed *myfixed,
50 GtkAdjustment *hadj,
51 GtkAdjustment *vadj);
52 #endif
53
54
55
56 static GtkContainerClass *parent_class = NULL;
57
58 guint
59 gtk_myfixed_get_type ()
60 {
61 static guint myfixed_type = 0;
62
63 if (!myfixed_type)
64 {
65 GtkTypeInfo myfixed_info =
66 {
67 "GtkMyFixed",
68 sizeof (GtkMyFixed),
69 sizeof (GtkMyFixedClass),
70 (GtkClassInitFunc) gtk_myfixed_class_init,
71 (GtkObjectInitFunc) gtk_myfixed_init,
72 #if (GTK_MINOR_VERSION > 0)
73 /* reserved_1 */ NULL,
74 /* reserved_2 */ NULL,
75 (GtkClassInitFunc) NULL,
76 #else
77 (GtkArgSetFunc) NULL,
78 (GtkArgGetFunc) NULL,
79 #endif
80 };
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 gtk_myfixed_scroll_set_adjustments (GtkMyFixed *myfixed,
176 GtkAdjustment *hadj,
177 GtkAdjustment *vadj)
178 {
179 /* OK, this is embarassing, but this function has to be here */
180 }
181
182 void
183 gtk_myfixed_set_shadow_type (GtkMyFixed *myfixed,
184 GtkShadowType type)
185 {
186 g_return_if_fail (myfixed != NULL);
187 g_return_if_fail (GTK_IS_MYFIXED (myfixed));
188
189 if ((GtkShadowType) myfixed->shadow_type != type)
190 {
191 myfixed->shadow_type = type;
192
193 if (GTK_WIDGET_VISIBLE (myfixed))
194 {
195 gtk_widget_size_allocate (GTK_WIDGET (myfixed), &(GTK_WIDGET (myfixed)->allocation));
196 gtk_widget_queue_draw (GTK_WIDGET (myfixed));
197 }
198 }
199 }
200 #endif
201
202 void
203 gtk_myfixed_put (GtkMyFixed *myfixed,
204 GtkWidget *widget,
205 gint16 x,
206 gint16 y)
207 {
208 GtkMyFixedChild *child_info;
209
210 g_return_if_fail (myfixed != NULL);
211 g_return_if_fail (GTK_IS_MYFIXED (myfixed));
212 g_return_if_fail (widget != NULL);
213
214 child_info = g_new (GtkMyFixedChild, 1);
215 child_info->widget = widget;
216 child_info->x = x;
217 child_info->y = y;
218
219 gtk_widget_set_parent (widget, GTK_WIDGET (myfixed));
220
221 myfixed->children = g_list_append (myfixed->children, child_info);
222
223 if (GTK_WIDGET_REALIZED (myfixed) && !GTK_WIDGET_REALIZED (widget))
224 gtk_widget_realize (widget);
225
226 if (GTK_WIDGET_MAPPED (myfixed) && !GTK_WIDGET_MAPPED (widget))
227 gtk_widget_map (widget);
228
229 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (myfixed))
230 gtk_widget_queue_resize (GTK_WIDGET (myfixed));
231 }
232
233 void
234 gtk_myfixed_move (GtkMyFixed *myfixed,
235 GtkWidget *widget,
236 gint16 x,
237 gint16 y)
238 {
239 GtkMyFixedChild *child;
240 GList *children;
241
242 g_return_if_fail (myfixed != NULL);
243 g_return_if_fail (GTK_IS_MYFIXED (myfixed));
244 g_return_if_fail (widget != NULL);
245
246 children = myfixed->children;
247 while (children)
248 {
249 child = children->data;
250 children = children->next;
251
252 if (child->widget == widget)
253 {
254 if ((child->x == x) && (child->y == y)) return;
255
256 child->x = x;
257 child->y = y;
258
259 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (myfixed))
260 gtk_widget_queue_resize (GTK_WIDGET (myfixed));
261
262 break;
263 }
264 }
265 }
266
267 static void
268 gtk_myfixed_map (GtkWidget *widget)
269 {
270 GtkMyFixed *myfixed;
271 GtkMyFixedChild *child;
272 GList *children;
273
274 g_return_if_fail (widget != NULL);
275 g_return_if_fail (GTK_IS_MYFIXED (widget));
276
277 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
278 myfixed = GTK_MYFIXED (widget);
279
280 gdk_window_show (widget->window);
281
282 children = myfixed->children;
283 while (children)
284 {
285 child = children->data;
286 children = children->next;
287
288 if (GTK_WIDGET_VISIBLE (child->widget) &&
289 !GTK_WIDGET_MAPPED (child->widget))
290 gtk_widget_map (child->widget);
291 }
292 }
293
294 #if (GTK_MINOR_VERSION == 0)
295 static void
296 gtk_myfixed_unmap (GtkWidget *widget)
297 {
298 g_return_if_fail (widget != NULL);
299 g_return_if_fail (GTK_IS_MYFIXED (widget));
300
301 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
302 }
303 #endif
304
305 static void
306 gtk_myfixed_realize (GtkWidget *widget)
307 {
308 GtkMyFixed *myfixed;
309 GdkWindowAttr attributes;
310 gint attributes_mask;
311
312 g_return_if_fail (widget != NULL);
313 g_return_if_fail (GTK_IS_MYFIXED (widget));
314
315 myfixed = GTK_MYFIXED (widget);
316
317 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
318
319 attributes.window_type = GDK_WINDOW_CHILD;
320
321 #if (GTK_MINOR_VERSION > 0)
322 if (myfixed->shadow_type != GTK_SHADOW_NONE)
323 {
324 attributes.x = 2;
325 attributes.y = 2;
326 }
327 else
328 {
329 attributes.x = 0;
330 attributes.y = 0;
331 }
332 attributes.width = MAX (1, (gint)widget->allocation.width - attributes.x * 2 );
333 attributes.height = MAX (1, (gint)widget->allocation.height - attributes.y * 2 );
334 #else
335 attributes.x = widget->allocation.x;
336 attributes.y = widget->allocation.y;
337 attributes.width = 32000;
338 attributes.height = 32000;
339 #endif
340 attributes.wclass = GDK_INPUT_OUTPUT;
341 attributes.visual = gtk_widget_get_visual (widget);
342 attributes.colormap = gtk_widget_get_colormap (widget);
343 attributes.event_mask = gtk_widget_get_events (widget);
344 attributes.event_mask |=
345 GDK_EXPOSURE_MASK |
346 GDK_POINTER_MOTION_MASK |
347 GDK_POINTER_MOTION_HINT_MASK |
348 GDK_BUTTON_MOTION_MASK |
349 GDK_BUTTON1_MOTION_MASK |
350 GDK_BUTTON2_MOTION_MASK |
351 GDK_BUTTON3_MOTION_MASK |
352 GDK_BUTTON_PRESS_MASK |
353 GDK_BUTTON_RELEASE_MASK |
354 GDK_KEY_PRESS_MASK |
355 GDK_KEY_RELEASE_MASK |
356 GDK_ENTER_NOTIFY_MASK |
357 GDK_LEAVE_NOTIFY_MASK |
358 GDK_FOCUS_CHANGE_MASK;
359
360 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
361
362 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes,
363 attributes_mask);
364 gdk_window_set_user_data (widget->window, widget);
365
366 widget->style = gtk_style_attach (widget->style, widget->window);
367 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
368 }
369
370 static void
371 gtk_myfixed_size_request (GtkWidget *widget,
372 GtkRequisition *requisition)
373 {
374 GtkMyFixed *myfixed;
375 GtkMyFixedChild *child;
376 GList *children;
377
378 g_return_if_fail (widget != NULL);
379 g_return_if_fail (GTK_IS_MYFIXED (widget));
380 g_return_if_fail (requisition != NULL);
381
382 myfixed = GTK_MYFIXED (widget);
383
384 requisition->width = 0;
385 requisition->height = 0;
386
387 children = myfixed->children;
388 while (children)
389 {
390 child = children->data;
391 children = children->next;
392
393 if (GTK_WIDGET_VISIBLE (child->widget))
394 {
395 gtk_widget_size_request (child->widget, &child->widget->requisition);
396 }
397 }
398 }
399
400 static void
401 gtk_myfixed_size_allocate (GtkWidget *widget,
402 GtkAllocation *allocation)
403 {
404 GtkMyFixed *myfixed;
405 gint border;
406 GtkMyFixedChild *child;
407 GtkAllocation child_allocation;
408 GList *children;
409
410 g_return_if_fail (widget != NULL);
411 g_return_if_fail (GTK_IS_MYFIXED(widget));
412 g_return_if_fail (allocation != NULL);
413
414 myfixed = GTK_MYFIXED (widget);
415
416 #if (GTK_MINOR_VERSION > 0)
417 if (myfixed->shadow_type == GTK_SHADOW_NONE)
418 border = 0;
419 else
420 border = 2;
421 #else
422 border = 0;
423 #endif
424
425 widget->allocation = *allocation;
426 if (GTK_WIDGET_REALIZED (widget))
427 {
428 gdk_window_move_resize( widget->window,
429 allocation->x+border, allocation->y+border,
430 #if (GTK_MINOR_VERSION > 0)
431 allocation->width-border*2, allocation->height-border*2
432 #else
433 32000, 32000
434 #endif
435 );
436 }
437
438 children = myfixed->children;
439 while (children)
440 {
441 child = children->data;
442 children = children->next;
443
444 if (GTK_WIDGET_VISIBLE (child->widget))
445 {
446 child_allocation.x = child->x;
447 child_allocation.y = child->y;
448 child_allocation.width = child->widget->requisition.width;
449 child_allocation.height = child->widget->requisition.height;
450 gtk_widget_size_allocate (child->widget, &child_allocation);
451 }
452 }
453 }
454
455 static void
456 gtk_myfixed_paint (GtkWidget *widget,
457 GdkRectangle *area)
458 {
459 g_return_if_fail (widget != NULL);
460 g_return_if_fail (GTK_IS_MYFIXED (widget));
461 g_return_if_fail (area != NULL);
462
463 if (GTK_WIDGET_DRAWABLE (widget))
464 gdk_window_clear_area (widget->window,
465 area->x, area->y,
466 area->width, area->height);
467 }
468
469 static void
470 gtk_myfixed_draw (GtkWidget *widget,
471 GdkRectangle *area)
472 {
473 GtkMyFixed *myfixed;
474 GtkMyFixedChild *child;
475 GdkRectangle child_area;
476 GList *children;
477
478 g_return_if_fail (widget != NULL);
479 g_return_if_fail (GTK_IS_MYFIXED (widget));
480
481 if (GTK_WIDGET_DRAWABLE (widget))
482 {
483 myfixed = GTK_MYFIXED (widget);
484 gtk_myfixed_paint (widget, area);
485
486 children = myfixed->children;
487 while (children)
488 {
489 child = children->data;
490 children = children->next;
491
492 if (gtk_widget_intersect (child->widget, area, &child_area))
493 gtk_widget_draw (child->widget, &child_area);
494 }
495 }
496 }
497
498 static gint
499 gtk_myfixed_expose (GtkWidget *widget,
500 GdkEventExpose *event)
501 {
502 GtkMyFixed *myfixed;
503 GtkMyFixedChild *child;
504 GdkEventExpose child_event;
505 GList *children;
506
507 g_return_val_if_fail (widget != NULL, FALSE);
508 g_return_val_if_fail (GTK_IS_MYFIXED (widget), FALSE);
509 g_return_val_if_fail (event != NULL, FALSE);
510
511 if (GTK_WIDGET_DRAWABLE (widget))
512 {
513 myfixed = GTK_MYFIXED (widget);
514
515 child_event = *event;
516
517 children = myfixed->children;
518 while (children)
519 {
520 child = children->data;
521 children = children->next;
522
523 if (GTK_WIDGET_NO_WINDOW (child->widget) &&
524 gtk_widget_intersect (child->widget, &event->area,
525 &child_event.area))
526 gtk_widget_event (child->widget, (GdkEvent*) &child_event);
527 }
528 }
529
530 return FALSE;
531 }
532
533 static void
534 gtk_myfixed_add (GtkContainer *container,
535 GtkWidget *widget)
536 {
537 g_return_if_fail (container != NULL);
538 g_return_if_fail (GTK_IS_MYFIXED (container));
539 g_return_if_fail (widget != NULL);
540
541 gtk_myfixed_put (GTK_MYFIXED (container), widget, 0, 0);
542 }
543
544 static void
545 gtk_myfixed_remove (GtkContainer *container,
546 GtkWidget *widget)
547 {
548 GtkMyFixed *myfixed;
549 GtkMyFixedChild *child;
550 GList *children;
551
552 g_return_if_fail (container != NULL);
553 g_return_if_fail (GTK_IS_MYFIXED (container));
554 g_return_if_fail (widget != NULL);
555
556 myfixed = GTK_MYFIXED (container);
557
558 children = myfixed->children;
559 while (children)
560 {
561 child = children->data;
562
563 if (child->widget == widget)
564 {
565 gtk_widget_unparent (widget);
566
567 myfixed->children = g_list_remove_link (myfixed->children, children);
568 g_list_free (children);
569 g_free (child);
570
571 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (container))
572 gtk_widget_queue_resize (GTK_WIDGET (container));
573
574 break;
575 }
576
577 children = children->next;
578 }
579 }
580
581 static void
582 gtk_myfixed_foreach (GtkContainer *container,
583 #if (GTK_MINOR_VERSION > 0)
584 gboolean include_internals,
585 #endif
586 GtkCallback callback,
587 gpointer callback_data)
588 {
589 GtkMyFixed *myfixed;
590 GtkMyFixedChild *child;
591 GList *children;
592
593 g_return_if_fail (container != NULL);
594 g_return_if_fail (GTK_IS_MYFIXED (container));
595 g_return_if_fail (callback != NULL);
596
597 myfixed = GTK_MYFIXED (container);
598
599 children = myfixed->children;
600 while (children)
601 {
602 child = children->data;
603 children = children->next;
604
605 (* callback) (child->widget, callback_data);
606 }
607 }
608
609
610 #ifdef __cplusplus
611 }
612 #endif /* __cplusplus */
613