]> Pileus Git - ~andy/gtk/blob - gtk/gtkpaned.c
Remove excess calls to g_return_if_fail from static and virtual functions.
[~andy/gtk] / gtk / gtkpaned.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "gtkintl.h"
28 #include "gtkpaned.h"
29
30 enum {
31   PROP_0,
32   PROP_POSITION,
33   PROP_POSITION_SET
34 };
35
36 static void    gtk_paned_class_init   (GtkPanedClass  *klass);
37 static void    gtk_paned_init         (GtkPaned       *paned);
38 static void    gtk_paned_set_property (GObject        *object,
39                                        guint           prop_id,
40                                        const GValue   *value,
41                                        GParamSpec     *pspec);
42 static void    gtk_paned_get_property (GObject        *object,
43                                        guint           prop_id,
44                                        GValue         *value,
45                                        GParamSpec     *pspec);
46 static void    gtk_paned_realize      (GtkWidget      *widget);
47 static void    gtk_paned_unrealize    (GtkWidget      *widget);
48 static void    gtk_paned_map          (GtkWidget      *widget);
49 static void    gtk_paned_unmap        (GtkWidget      *widget);
50 static gint    gtk_paned_expose       (GtkWidget      *widget,
51                                        GdkEventExpose *event);
52 static void    gtk_paned_add          (GtkContainer   *container,
53                                        GtkWidget      *widget);
54 static void    gtk_paned_remove       (GtkContainer   *container,
55                                        GtkWidget      *widget);
56 static void    gtk_paned_forall       (GtkContainer   *container,
57                                        gboolean        include_internals,
58                                        GtkCallback     callback,
59                                        gpointer        callback_data);
60 static GtkType gtk_paned_child_type   (GtkContainer   *container);
61
62 static GtkContainerClass *parent_class = NULL;
63
64
65 GtkType
66 gtk_paned_get_type (void)
67 {
68   static GtkType paned_type = 0;
69   
70   if (!paned_type)
71     {
72       static const GtkTypeInfo paned_info =
73       {
74         "GtkPaned",
75         sizeof (GtkPaned),
76         sizeof (GtkPanedClass),
77         (GtkClassInitFunc) gtk_paned_class_init,
78         (GtkObjectInitFunc) gtk_paned_init,
79         /* reserved_1 */ NULL,
80         /* reserved_2 */ NULL,
81         (GtkClassInitFunc) NULL,
82       };
83
84       paned_type = gtk_type_unique (GTK_TYPE_CONTAINER, &paned_info);
85     }
86   
87   return paned_type;
88 }
89
90 static void
91 gtk_paned_class_init (GtkPanedClass *class)
92 {
93   GObjectClass *object_class;
94   GtkWidgetClass *widget_class;
95   GtkContainerClass *container_class;
96
97   object_class = (GObjectClass *) class;
98   widget_class = (GtkWidgetClass *) class;
99   container_class = (GtkContainerClass *) class;
100
101   parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
102
103   object_class->set_property = gtk_paned_set_property;
104   object_class->get_property = gtk_paned_get_property;
105
106   widget_class->realize = gtk_paned_realize;
107   widget_class->unrealize = gtk_paned_unrealize;
108   widget_class->map = gtk_paned_map;
109   widget_class->unmap = gtk_paned_unmap;
110   widget_class->expose_event = gtk_paned_expose;
111   
112   container_class->add = gtk_paned_add;
113   container_class->remove = gtk_paned_remove;
114   container_class->forall = gtk_paned_forall;
115   container_class->child_type = gtk_paned_child_type;
116
117   g_object_class_install_property (object_class,
118                                    PROP_POSITION,
119                                    g_param_spec_int ("position",
120                                                      _("Position"),
121                                                      _("Position of paned separator in pixels (0 means all the way to the left/top)"),
122                                                      0,
123                                                      G_MAXINT,
124                                                      0,
125                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
126   g_object_class_install_property (object_class,
127                                    PROP_POSITION_SET,
128                                    g_param_spec_boolean ("position_set",
129                                                          _("Position Set"),
130                                                          _("TRUE if the Position property should be used"),
131                                                          FALSE,
132                                                          G_PARAM_READABLE | G_PARAM_WRITABLE));
133                                    
134   gtk_widget_class_install_style_property (widget_class,
135                                            g_param_spec_int ("handle_size",
136                                                              _("Handle Size"),
137                                                              _("Width of handle"),
138                                                              0,
139                                                              G_MAXINT,
140                                                              5,
141                                                              G_PARAM_READABLE));
142 }
143
144 static GtkType
145 gtk_paned_child_type (GtkContainer *container)
146 {
147   if (!GTK_PANED (container)->child1 || !GTK_PANED (container)->child2)
148     return GTK_TYPE_WIDGET;
149   else
150     return GTK_TYPE_NONE;
151 }
152
153 static void
154 gtk_paned_init (GtkPaned *paned)
155 {
156   GTK_WIDGET_SET_FLAGS (paned, GTK_NO_WINDOW);
157   
158   paned->child1 = NULL;
159   paned->child2 = NULL;
160   paned->handle = NULL;
161   paned->xor_gc = NULL;
162   paned->cursor_type = GDK_CROSS;
163   
164   paned->handle_pos.width = 5;
165   paned->handle_pos.height = 5;
166   paned->position_set = FALSE;
167   paned->last_allocation = -1;
168   paned->in_drag = FALSE;
169   
170   paned->handle_pos.x = -1;
171   paned->handle_pos.y = -1;
172 }
173
174 static void
175 gtk_paned_set_property (GObject        *object,
176                         guint           prop_id,
177                         const GValue   *value,
178                         GParamSpec     *pspec)
179 {
180   GtkPaned *paned = GTK_PANED (object);
181   
182   switch (prop_id)
183     {
184     case PROP_POSITION:
185       gtk_paned_set_position (paned, g_value_get_int (value));
186       break;
187     case PROP_POSITION_SET:
188       paned->position_set = g_value_get_boolean (value);
189       gtk_widget_queue_resize (GTK_WIDGET (paned));
190       break;
191     default:
192       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
193       break;
194     }
195 }
196
197 static void
198 gtk_paned_get_property (GObject        *object,
199                         guint           prop_id,
200                         GValue         *value,
201                         GParamSpec     *pspec)
202 {
203   GtkPaned *paned = GTK_PANED (object);
204   
205   switch (prop_id)
206     {
207     case PROP_POSITION:
208       g_value_set_int (value, paned->child1_size);
209       break;
210     case PROP_POSITION_SET:
211       g_value_set_boolean (value, paned->position_set);
212       break;
213     default:
214       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215       break;
216     }
217 }
218
219 static void
220 gtk_paned_realize (GtkWidget *widget)
221 {
222   GtkPaned *paned;
223   GdkWindowAttr attributes;
224   gint attributes_mask;
225
226   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
227   paned = GTK_PANED (widget);
228
229   widget->window = gtk_widget_get_parent_window (widget);
230   gdk_window_ref (widget->window);
231   
232   attributes.window_type = GDK_WINDOW_CHILD;
233   attributes.wclass = GDK_INPUT_ONLY;
234   attributes.x = paned->handle_pos.x;
235   attributes.y = paned->handle_pos.y;
236   attributes.width = paned->handle_pos.width;
237   attributes.height = paned->handle_pos.height;
238   attributes.cursor = gdk_cursor_new (paned->cursor_type);
239   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
240                             GDK_BUTTON_RELEASE_MASK |
241                             GDK_POINTER_MOTION_MASK |
242                             GDK_POINTER_MOTION_HINT_MASK);
243   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_CURSOR;
244
245   paned->handle = gdk_window_new (widget->window,
246                                   &attributes, attributes_mask);
247   gdk_window_set_user_data (paned->handle, paned);
248   gdk_cursor_destroy (attributes.cursor);
249
250   widget->style = gtk_style_attach (widget->style, widget->window);
251
252   if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
253       paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
254     gdk_window_show (paned->handle);
255 }
256
257 static void
258 gtk_paned_unrealize (GtkWidget *widget)
259 {
260   GtkPaned *paned = GTK_PANED (widget);
261
262   if (paned->xor_gc)
263     {
264       gdk_gc_destroy (paned->xor_gc);
265       paned->xor_gc = NULL;
266     }
267
268   if (paned->handle)
269     {
270       gdk_window_set_user_data (paned->handle, NULL);
271       gdk_window_destroy (paned->handle);
272       paned->handle = NULL;
273     }
274
275   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
276     (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
277 }
278
279 static void
280 gtk_paned_map (GtkWidget *widget)
281 {
282   GtkPaned *paned = GTK_PANED (widget);
283
284   gdk_window_show (paned->handle);
285
286   GTK_WIDGET_CLASS (parent_class)->map (widget);
287 }
288
289 static void
290 gtk_paned_unmap (GtkWidget *widget)
291 {
292   GtkPaned *paned = GTK_PANED (widget);
293     
294   gdk_window_hide (paned->handle);
295
296   GTK_WIDGET_CLASS (parent_class)->unmap (widget);
297 }
298
299 static gint
300 gtk_paned_expose (GtkWidget      *widget,
301                   GdkEventExpose *event)
302 {
303   GtkPaned *paned = GTK_PANED (widget);
304
305   if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget) &&
306       paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
307       paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
308     {
309       GdkRegion *region;
310
311       region = gdk_region_rectangle (&paned->handle_pos);
312       gdk_region_intersect (region, event->region);
313
314       if (!gdk_region_empty (region))
315         {
316           GdkRectangle clip;
317
318           gdk_region_get_clipbox (region, &clip);
319           
320           gtk_paint_handle (widget->style, widget->window,
321                             GTK_STATE_NORMAL, GTK_SHADOW_NONE,
322                             &clip, widget, "paned",
323                             paned->handle_pos.x, paned->handle_pos.y,
324                             paned->handle_pos.width, paned->handle_pos.height,
325                             paned->orientation);
326         }
327
328       gdk_region_destroy (region);
329     }
330   
331   /* Chain up to draw children */
332   GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
333
334   return FALSE;
335 }
336
337 void
338 gtk_paned_add1 (GtkPaned  *paned,
339                 GtkWidget *widget)
340 {
341   gtk_paned_pack1 (paned, widget, FALSE, TRUE);
342 }
343
344 void
345 gtk_paned_add2 (GtkPaned  *paned,
346                 GtkWidget *widget)
347 {
348   gtk_paned_pack2 (paned, widget, TRUE, TRUE);
349 }
350
351 void
352 gtk_paned_pack1 (GtkPaned  *paned,
353                  GtkWidget *child,
354                  gboolean   resize,
355                  gboolean   shrink)
356 {
357   g_return_if_fail (GTK_IS_PANED (paned));
358   g_return_if_fail (GTK_IS_WIDGET (child));
359
360   if (!paned->child1)
361     {
362       paned->child1 = child;
363       paned->child1_resize = resize;
364       paned->child1_shrink = shrink;
365
366       gtk_widget_set_parent (child, GTK_WIDGET (paned));
367     }
368 }
369
370 void
371 gtk_paned_pack2 (GtkPaned  *paned,
372                  GtkWidget *child,
373                  gboolean   resize,
374                  gboolean   shrink)
375 {
376   g_return_if_fail (GTK_IS_PANED (paned));
377   g_return_if_fail (GTK_IS_WIDGET (child));
378
379   if (!paned->child2)
380     {
381       paned->child2 = child;
382       paned->child2_resize = resize;
383       paned->child2_shrink = shrink;
384
385       gtk_widget_set_parent (child, GTK_WIDGET (paned));
386     }
387 }
388
389
390 static void
391 gtk_paned_add (GtkContainer *container,
392                GtkWidget    *widget)
393 {
394   GtkPaned *paned;
395
396   g_return_if_fail (GTK_IS_PANED (container));
397
398   paned = GTK_PANED (container);
399
400   if (!paned->child1)
401     gtk_paned_add1 (paned, widget);
402   else if (!paned->child2)
403     gtk_paned_add2 (paned, widget);
404 }
405
406 static void
407 gtk_paned_remove (GtkContainer *container,
408                   GtkWidget    *widget)
409 {
410   GtkPaned *paned;
411   gboolean was_visible;
412
413   paned = GTK_PANED (container);
414   was_visible = GTK_WIDGET_VISIBLE (widget);
415
416   if (paned->child1 == widget)
417     {
418       gtk_widget_unparent (widget);
419
420       paned->child1 = NULL;
421
422       if (was_visible && GTK_WIDGET_VISIBLE (container))
423         gtk_widget_queue_resize (GTK_WIDGET (container));
424     }
425   else if (paned->child2 == widget)
426     {
427       gtk_widget_unparent (widget);
428
429       paned->child2 = NULL;
430
431       if (was_visible && GTK_WIDGET_VISIBLE (container))
432         gtk_widget_queue_resize (GTK_WIDGET (container));
433     }
434 }
435
436 static void
437 gtk_paned_forall (GtkContainer *container,
438                   gboolean      include_internals,
439                   GtkCallback   callback,
440                   gpointer      callback_data)
441 {
442   GtkPaned *paned;
443
444   g_return_if_fail (callback != NULL);
445
446   paned = GTK_PANED (container);
447
448   if (paned->child1)
449     (*callback) (paned->child1, callback_data);
450   if (paned->child2)
451     (*callback) (paned->child2, callback_data);
452 }
453
454 /**
455  * gtk_paned_get_position:
456  * @paned: a #GtkPaned widget
457  * 
458  * Obtains the position of the divider between the two panes.
459  * 
460  * Return value: position of the divider
461  **/
462 gint
463 gtk_paned_get_position (GtkPaned  *paned)
464 {
465   g_return_val_if_fail (GTK_IS_PANED (paned), 0);
466
467   return paned->child1_size;
468 }
469
470 /**
471  * gtk_paned_set_position:
472  * @paned: a #GtkPaned widget
473  * @position: pixel position of divider, a negative value means that the position
474  *            is unset.
475  * 
476  * Sets the position of the divider between the two panes.
477  **/
478 void
479 gtk_paned_set_position (GtkPaned *paned,
480                         gint      position)
481 {
482   GObject *object;
483   
484   g_return_if_fail (GTK_IS_PANED (paned));
485
486   object = G_OBJECT (paned);
487   
488   if (position >= 0)
489     {
490       /* We don't clamp here - the assumption is that
491        * if the total allocation changes at the same time
492        * as the position, the position set is with reference
493        * to the new total size. If only the position changes,
494        * then clamping will occur in gtk_paned_compute_position()
495        */
496
497       paned->child1_size = position;
498       paned->position_set = TRUE;
499     }
500   else
501     {
502       paned->position_set = FALSE;
503     }
504
505   g_object_freeze_notify (object);
506   g_object_notify (object, "position");
507   g_object_notify (object, "position_set");
508   g_object_thaw_notify (object);
509
510   gtk_widget_queue_resize (GTK_WIDGET (paned));
511 }
512
513 void
514 gtk_paned_compute_position (GtkPaned *paned,
515                             gint      allocation,
516                             gint      child1_req,
517                             gint      child2_req)
518 {
519   gint old_position;
520   
521   g_return_if_fail (GTK_IS_PANED (paned));
522
523   old_position = paned->child1_size;
524
525   paned->min_position = paned->child1_shrink ? 0 : child1_req;
526
527   paned->max_position = allocation;
528   if (!paned->child2_shrink)
529     paned->max_position = MAX (1, paned->max_position - child2_req);
530
531   if (!paned->position_set)
532     {
533       if (paned->child1_resize && !paned->child2_resize)
534         paned->child1_size = MAX (1, allocation - child2_req);
535       else if (!paned->child1_resize && paned->child2_resize)
536         paned->child1_size = child1_req;
537       else if (child1_req + child2_req != 0)
538         paned->child1_size = allocation * ((gdouble)child1_req / (child1_req + child2_req));
539       else
540         paned->child1_size = allocation * 0.5;
541     }
542   else
543     {
544       /* If the position was set before the initial allocation.
545        * (paned->last_allocation <= 0) just clamp it and leave it.
546        */
547       if (paned->last_allocation > 0)
548         {
549           if (paned->child1_resize && !paned->child2_resize)
550             paned->child1_size += allocation - paned->last_allocation;
551           else if (!(!paned->child1_resize && paned->child2_resize))
552             paned->child1_size = allocation * ((gdouble) paned->child1_size / (paned->last_allocation));
553         }
554     }
555
556   paned->child1_size = CLAMP (paned->child1_size,
557                               paned->min_position,
558                               paned->max_position);
559
560   if (paned->child1_size != old_position)
561     g_object_notify (G_OBJECT (paned), "position");
562
563   paned->last_allocation = allocation;
564 }