]> Pileus Git - ~andy/gtk/blob - gtk/gtkpaned.c
Fix a typo. Fix a typo.
[~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   g_return_if_fail (GTK_IS_PANED (widget));
227
228   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
229   paned = GTK_PANED (widget);
230
231   widget->window = gtk_widget_get_parent_window (widget);
232   gdk_window_ref (widget->window);
233   
234   attributes.window_type = GDK_WINDOW_CHILD;
235   attributes.wclass = GDK_INPUT_ONLY;
236   attributes.x = paned->handle_pos.x;
237   attributes.y = paned->handle_pos.y;
238   attributes.width = paned->handle_pos.width;
239   attributes.height = paned->handle_pos.height;
240   attributes.cursor = gdk_cursor_new (paned->cursor_type);
241   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
242                             GDK_BUTTON_RELEASE_MASK |
243                             GDK_POINTER_MOTION_MASK |
244                             GDK_POINTER_MOTION_HINT_MASK);
245   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_CURSOR;
246
247   paned->handle = gdk_window_new (widget->window,
248                                   &attributes, attributes_mask);
249   gdk_window_set_user_data (paned->handle, paned);
250   gdk_cursor_destroy (attributes.cursor);
251
252   widget->style = gtk_style_attach (widget->style, widget->window);
253
254   if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
255       paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
256     gdk_window_show (paned->handle);
257 }
258
259 static void
260 gtk_paned_unrealize (GtkWidget *widget)
261 {
262   GtkPaned *paned;
263
264   g_return_if_fail (GTK_IS_PANED (widget));
265
266   paned = GTK_PANED (widget);
267
268   if (paned->xor_gc)
269     {
270       gdk_gc_destroy (paned->xor_gc);
271       paned->xor_gc = NULL;
272     }
273
274   if (paned->handle)
275     {
276       gdk_window_set_user_data (paned->handle, NULL);
277       gdk_window_destroy (paned->handle);
278       paned->handle = NULL;
279     }
280
281   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
282     (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
283 }
284
285 static void
286 gtk_paned_map (GtkWidget *widget)
287 {
288   GtkPaned *paned = GTK_PANED (widget);
289   
290   g_return_if_fail (GTK_IS_PANED (widget));
291
292   gdk_window_show (paned->handle);
293
294   GTK_WIDGET_CLASS (parent_class)->map (widget);
295 }
296
297 static void
298 gtk_paned_unmap (GtkWidget *widget)
299 {
300   GtkPaned *paned = GTK_PANED (widget);
301     
302   g_return_if_fail (GTK_IS_PANED (widget));
303
304   gdk_window_hide (paned->handle);
305
306   GTK_WIDGET_CLASS (parent_class)->unmap (widget);
307 }
308
309 static gint
310 gtk_paned_expose (GtkWidget      *widget,
311                   GdkEventExpose *event)
312 {
313   GtkPaned *paned;
314
315   g_return_val_if_fail (GTK_IS_PANED (widget), FALSE);
316
317   if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
318     {
319       GdkRegion *region;
320
321       paned = GTK_PANED (widget);
322       
323       region = gdk_region_rectangle (&paned->handle_pos);
324       gdk_region_intersect (region, event->region);
325
326       if (!gdk_region_empty (region))
327         {
328           GdkRectangle clip;
329
330           gdk_region_get_clipbox (region, &clip);
331           
332           gtk_paint_handle (widget->style, widget->window,
333                             GTK_STATE_NORMAL, GTK_SHADOW_NONE,
334                             &clip, widget, "paned",
335                             paned->handle_pos.x, paned->handle_pos.y,
336                             paned->handle_pos.width, paned->handle_pos.height,
337                             paned->orientation);
338         }
339
340       gdk_region_destroy (region);
341       
342       /* Chain up to draw children */
343       GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
344     }
345
346   return FALSE;
347 }
348
349 void
350 gtk_paned_add1 (GtkPaned  *paned,
351                 GtkWidget *widget)
352 {
353   gtk_paned_pack1 (paned, widget, FALSE, TRUE);
354 }
355
356 void
357 gtk_paned_add2 (GtkPaned  *paned,
358                 GtkWidget *widget)
359 {
360   gtk_paned_pack2 (paned, widget, TRUE, TRUE);
361 }
362
363 void
364 gtk_paned_pack1 (GtkPaned  *paned,
365                  GtkWidget *child,
366                  gboolean   resize,
367                  gboolean   shrink)
368 {
369   g_return_if_fail (GTK_IS_PANED (paned));
370   g_return_if_fail (GTK_IS_WIDGET (child));
371
372   if (!paned->child1)
373     {
374       paned->child1 = child;
375       paned->child1_resize = resize;
376       paned->child1_shrink = shrink;
377
378       gtk_widget_set_parent (child, GTK_WIDGET (paned));
379     }
380 }
381
382 void
383 gtk_paned_pack2 (GtkPaned  *paned,
384                  GtkWidget *child,
385                  gboolean   resize,
386                  gboolean   shrink)
387 {
388   g_return_if_fail (GTK_IS_PANED (paned));
389   g_return_if_fail (GTK_IS_WIDGET (child));
390
391   if (!paned->child2)
392     {
393       paned->child2 = child;
394       paned->child2_resize = resize;
395       paned->child2_shrink = shrink;
396
397       gtk_widget_set_parent (child, GTK_WIDGET (paned));
398     }
399 }
400
401
402 static void
403 gtk_paned_add (GtkContainer *container,
404                GtkWidget    *widget)
405 {
406   GtkPaned *paned;
407
408   g_return_if_fail (GTK_IS_PANED (container));
409   g_return_if_fail (widget != NULL);
410
411   paned = GTK_PANED (container);
412
413   if (!paned->child1)
414     gtk_paned_add1 (GTK_PANED (container), widget);
415   else if (!paned->child2)
416     gtk_paned_add2 (GTK_PANED (container), widget);
417 }
418
419 static void
420 gtk_paned_remove (GtkContainer *container,
421                   GtkWidget    *widget)
422 {
423   GtkPaned *paned;
424   gboolean was_visible;
425
426   g_return_if_fail (GTK_IS_PANED (container));
427   g_return_if_fail (widget != NULL);
428
429   paned = GTK_PANED (container);
430   was_visible = GTK_WIDGET_VISIBLE (widget);
431
432   if (paned->child1 == widget)
433     {
434       gtk_widget_unparent (widget);
435
436       paned->child1 = NULL;
437
438       if (was_visible && GTK_WIDGET_VISIBLE (container))
439         gtk_widget_queue_resize (GTK_WIDGET (container));
440     }
441   else if (paned->child2 == widget)
442     {
443       gtk_widget_unparent (widget);
444
445       paned->child2 = NULL;
446
447       if (was_visible && GTK_WIDGET_VISIBLE (container))
448         gtk_widget_queue_resize (GTK_WIDGET (container));
449     }
450 }
451
452 static void
453 gtk_paned_forall (GtkContainer *container,
454                   gboolean      include_internals,
455                   GtkCallback   callback,
456                   gpointer      callback_data)
457 {
458   GtkPaned *paned;
459
460   g_return_if_fail (GTK_IS_PANED (container));
461   g_return_if_fail (callback != NULL);
462
463   paned = GTK_PANED (container);
464
465   if (paned->child1)
466     (*callback) (paned->child1, callback_data);
467   if (paned->child2)
468     (*callback) (paned->child2, callback_data);
469 }
470
471 /**
472  * gtk_paned_get_position:
473  * @paned: a #GtkPaned widget
474  * 
475  * Obtains the position of the divider between the two panes.
476  * 
477  * Return value: position of the divider
478  **/
479 gint
480 gtk_paned_get_position (GtkPaned  *paned)
481 {
482   g_return_val_if_fail (GTK_IS_PANED (paned), 0);
483
484   return paned->child1_size;
485 }
486
487 /**
488  * gtk_paned_set_position:
489  * @paned: a #GtkPaned widget
490  * @position: pixel position of divider, a negative value means that the position
491  *            is unset.
492  * 
493  * Sets the position of the divider between the two panes.
494  **/
495 void
496 gtk_paned_set_position (GtkPaned *paned,
497                         gint      position)
498 {
499   GObject *object;
500   
501   g_return_if_fail (GTK_IS_PANED (paned));
502
503   object = G_OBJECT (paned);
504   
505   if (position >= 0)
506     {
507       /* We don't clamp here - the assumption is that
508        * if the total allocation changes at the same time
509        * as the position, the position set is with reference
510        * to the new total size. If only the position changes,
511        * then clamping will occur in gtk_paned_compute_position()
512        */
513
514       paned->child1_size = position;
515       paned->position_set = TRUE;
516     }
517   else
518     {
519       paned->position_set = FALSE;
520     }
521
522   g_object_freeze_notify (object);
523   g_object_notify (object, "position");
524   g_object_notify (object, "position_set");
525   g_object_thaw_notify (object);
526
527   gtk_widget_queue_resize (GTK_WIDGET (paned));
528 }
529
530 void
531 gtk_paned_compute_position (GtkPaned *paned,
532                             gint      allocation,
533                             gint      child1_req,
534                             gint      child2_req)
535 {
536   gint old_position;
537   
538   g_return_if_fail (GTK_IS_PANED (paned));
539
540   old_position = paned->child1_size;
541
542   paned->min_position = paned->child1_shrink ? 0 : child1_req;
543
544   paned->max_position = allocation;
545   if (!paned->child2_shrink)
546     paned->max_position = MAX (1, paned->max_position - child2_req);
547
548   if (!paned->position_set)
549     {
550       if (paned->child1_resize && !paned->child2_resize)
551         paned->child1_size = MAX (1, allocation - child2_req);
552       else if (!paned->child1_resize && paned->child2_resize)
553         paned->child1_size = child1_req;
554       else if (child1_req + child2_req != 0)
555         paned->child1_size = allocation * ((gdouble)child1_req / (child1_req + child2_req));
556       else
557         paned->child1_size = allocation * 0.5;
558     }
559   else
560     {
561       /* If the position was set before the initial allocation.
562        * (paned->last_allocation <= 0) just clamp it and leave it.
563        */
564       if (paned->last_allocation > 0)
565         {
566           if (paned->child1_resize && !paned->child2_resize)
567             paned->child1_size += allocation - paned->last_allocation;
568           else if (!(!paned->child1_resize && paned->child2_resize))
569             paned->child1_size = allocation * ((gdouble) paned->child1_size / (paned->last_allocation));
570         }
571     }
572
573   paned->child1_size = CLAMP (paned->child1_size,
574                               paned->min_position,
575                               paned->max_position);
576
577   if (paned->child1_size != old_position)
578     g_object_notify (G_OBJECT (paned), "position");
579
580   paned->last_allocation = allocation;
581 }