]> Pileus Git - ~andy/gtk/blob - gtk/gtkpaned.c
CVS is doing its broken pipe thing, this is more of the previous commit
[~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 "gtkpaned.h"
28
29 enum {
30   ARG_0,
31   ARG_HANDLE_SIZE
32 };
33
34 static void    gtk_paned_class_init (GtkPanedClass  *klass);
35 static void    gtk_paned_init       (GtkPaned       *paned);
36 static void    gtk_paned_set_arg    (GtkObject      *object,
37                                      GtkArg         *arg,
38                                      guint           arg_id);
39 static void    gtk_paned_get_arg    (GtkObject      *object,
40                                      GtkArg         *arg,
41                                      guint           arg_id);
42 static void    gtk_paned_realize    (GtkWidget      *widget);
43 static void    gtk_paned_map        (GtkWidget      *widget);
44 static void    gtk_paned_unmap      (GtkWidget      *widget);
45 static void    gtk_paned_unrealize  (GtkWidget      *widget);
46 static gint    gtk_paned_expose     (GtkWidget      *widget,
47                                      GdkEventExpose *event);
48 static void    gtk_paned_add        (GtkContainer   *container,
49                                      GtkWidget      *widget);
50 static void    gtk_paned_remove     (GtkContainer   *container,
51                                      GtkWidget      *widget);
52 static void    gtk_paned_forall     (GtkContainer   *container,
53                                      gboolean        include_internals,
54                                      GtkCallback     callback,
55                                      gpointer        callback_data);
56 static GtkType gtk_paned_child_type (GtkContainer   *container);
57
58 static GtkContainerClass *parent_class = NULL;
59
60
61 GtkType
62 gtk_paned_get_type (void)
63 {
64   static GtkType paned_type = 0;
65   
66   if (!paned_type)
67     {
68       static const GtkTypeInfo paned_info =
69       {
70         "GtkPaned",
71         sizeof (GtkPaned),
72         sizeof (GtkPanedClass),
73         (GtkClassInitFunc) gtk_paned_class_init,
74         (GtkObjectInitFunc) gtk_paned_init,
75         /* reserved_1 */ NULL,
76         /* reserved_2 */ NULL,
77         (GtkClassInitFunc) NULL,
78       };
79
80       paned_type = gtk_type_unique (GTK_TYPE_CONTAINER, &paned_info);
81     }
82   
83   return paned_type;
84 }
85
86 static void
87 gtk_paned_class_init (GtkPanedClass *class)
88 {
89   GtkObjectClass *object_class;
90   GtkWidgetClass *widget_class;
91   GtkContainerClass *container_class;
92
93   object_class = (GtkObjectClass *) class;
94   widget_class = (GtkWidgetClass *) class;
95   container_class = (GtkContainerClass *) class;
96
97   parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
98
99   object_class->set_arg = gtk_paned_set_arg;
100   object_class->get_arg = gtk_paned_get_arg;
101
102   widget_class->realize = gtk_paned_realize;
103   widget_class->map = gtk_paned_map;
104   widget_class->unmap = gtk_paned_unmap;
105   widget_class->unrealize = gtk_paned_unrealize;
106   widget_class->expose_event = gtk_paned_expose;
107   
108   container_class->add = gtk_paned_add;
109   container_class->remove = gtk_paned_remove;
110   container_class->forall = gtk_paned_forall;
111   container_class->child_type = gtk_paned_child_type;
112
113   gtk_object_add_arg_type("GtkPaned::handle_size", GTK_TYPE_UINT,
114                           GTK_ARG_READWRITE, ARG_HANDLE_SIZE);
115 }
116
117 static GtkType
118 gtk_paned_child_type (GtkContainer *container)
119 {
120   if (!GTK_PANED (container)->child1 || !GTK_PANED (container)->child2)
121     return GTK_TYPE_WIDGET;
122   else
123     return GTK_TYPE_NONE;
124 }
125
126 static void
127 gtk_paned_init (GtkPaned *paned)
128 {
129   GTK_WIDGET_UNSET_FLAGS (paned, GTK_NO_WINDOW);
130   
131   paned->child1 = NULL;
132   paned->child2 = NULL;
133   paned->handle = NULL;
134   paned->xor_gc = NULL;
135   paned->cursor_type = GDK_CROSS;
136   
137   paned->handle_width = 5;
138   paned->handle_height = 5;
139   paned->handle_size = 5;
140   paned->position_set = FALSE;
141   paned->last_allocation = -1;
142   paned->in_drag = FALSE;
143   
144   paned->handle_xpos = -1;
145   paned->handle_ypos = -1;
146 }
147
148 static void
149 gtk_paned_set_arg (GtkObject *object,
150                    GtkArg    *arg,
151                    guint      arg_id)
152 {
153   GtkPaned *paned = GTK_PANED (object);
154   
155   switch (arg_id)
156     {
157     case ARG_HANDLE_SIZE:
158       gtk_paned_set_handle_size (paned, GTK_VALUE_UINT (*arg));
159       break;
160     default:
161       break;
162     }
163 }
164
165 static void
166 gtk_paned_get_arg (GtkObject *object,
167                    GtkArg    *arg,
168                    guint      arg_id)
169 {
170   GtkPaned *paned = GTK_PANED (object);
171
172   switch (arg_id)
173     {
174     case ARG_HANDLE_SIZE:
175       GTK_VALUE_UINT (*arg) = paned->handle_size;
176       break;
177     default:
178       arg->type = GTK_TYPE_INVALID;
179       break;
180     }
181 }
182
183 static void
184 gtk_paned_realize (GtkWidget *widget)
185 {
186   GtkPaned *paned;
187   GdkWindowAttr attributes;
188   gint attributes_mask;
189
190   g_return_if_fail (widget != NULL);
191   g_return_if_fail (GTK_IS_PANED (widget));
192
193   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
194   paned = GTK_PANED (widget);
195
196   attributes.x = widget->allocation.x;
197   attributes.y = widget->allocation.y;
198   attributes.width = widget->allocation.width;
199   attributes.height = widget->allocation.height;
200   attributes.window_type = GDK_WINDOW_CHILD;
201   attributes.wclass = GDK_INPUT_OUTPUT;
202   attributes.visual = gtk_widget_get_visual (widget);
203   attributes.colormap = gtk_widget_get_colormap (widget);
204   attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
205   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
206
207   widget->window = gdk_window_new (gtk_widget_get_parent_window(widget),
208                                    &attributes, attributes_mask);
209   gdk_window_set_user_data (widget->window, paned);
210
211   attributes.x = paned->handle_xpos;
212   attributes.y = paned->handle_ypos;
213   attributes.width = paned->handle_width;
214   attributes.height = paned->handle_height;
215   attributes.cursor = gdk_cursor_new (paned->cursor_type);
216   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
217                             GDK_BUTTON_RELEASE_MASK |
218                             GDK_POINTER_MOTION_MASK |
219                             GDK_POINTER_MOTION_HINT_MASK);
220   attributes_mask |= GDK_WA_CURSOR;
221
222   paned->handle = gdk_window_new (widget->window,
223                                   &attributes, attributes_mask);
224   gdk_window_set_user_data (paned->handle, paned);
225   gdk_cursor_destroy (attributes.cursor);
226
227   widget->style = gtk_style_attach (widget->style, widget->window);
228
229   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
230   gtk_style_set_background (widget->style, paned->handle, GTK_STATE_NORMAL);
231
232   gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
233
234   gdk_window_show (paned->handle);
235 }
236
237 static void
238 gtk_paned_map (GtkWidget *widget)
239 {
240   GtkPaned *paned;
241
242   g_return_if_fail (widget != NULL);
243   g_return_if_fail (GTK_IS_PANED (widget));
244
245   GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
246   paned = GTK_PANED (widget);
247
248   if (paned->child1 &&
249       GTK_WIDGET_VISIBLE (paned->child1) &&
250       !GTK_WIDGET_MAPPED (paned->child1))
251     gtk_widget_map (paned->child1);
252   if (paned->child2 &&
253       GTK_WIDGET_VISIBLE (paned->child2) &&
254       !GTK_WIDGET_MAPPED (paned->child2))
255     gtk_widget_map (paned->child2);
256
257   gdk_window_show (widget->window);
258 }
259
260 static void
261 gtk_paned_unmap (GtkWidget *widget)
262 {
263   g_return_if_fail (widget != NULL);
264   g_return_if_fail (GTK_IS_PANED (widget));
265
266   GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
267
268   gdk_window_hide (widget->window);
269 }
270
271 static void
272 gtk_paned_unrealize (GtkWidget *widget)
273 {
274   GtkPaned *paned;
275
276   g_return_if_fail (widget != NULL);
277   g_return_if_fail (GTK_IS_PANED (widget));
278
279   paned = GTK_PANED (widget);
280
281   if (paned->xor_gc)
282     {
283       gdk_gc_destroy (paned->xor_gc);
284       paned->xor_gc = NULL;
285     }
286
287   if (paned->handle)
288     {
289       gdk_window_set_user_data (paned->handle, NULL);
290       gdk_window_destroy (paned->handle);
291       paned->handle = NULL;
292     }
293
294   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
295     (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
296 }
297
298
299
300 static gint
301 gtk_paned_expose (GtkWidget      *widget,
302                   GdkEventExpose *event)
303 {
304   GtkPaned *paned;
305   GdkEventExpose child_event;
306
307   g_return_val_if_fail (widget != NULL, FALSE);
308   g_return_val_if_fail (GTK_IS_PANED (widget), FALSE);
309   g_return_val_if_fail (event != NULL, FALSE);
310
311   if (GTK_WIDGET_DRAWABLE (widget))
312     {
313       paned = GTK_PANED (widget);
314
315       if (event->window != paned->handle)
316         {
317           child_event = *event;
318
319           if (paned->child1 &&
320               GTK_WIDGET_NO_WINDOW (paned->child1) &&
321               gtk_widget_intersect (paned->child1, &event->area, &child_event.area))
322             gtk_widget_event (paned->child1, (GdkEvent *) &child_event);
323
324           if (paned->child2 &&
325               GTK_WIDGET_NO_WINDOW (paned->child2) &&
326               gtk_widget_intersect (paned->child2, &event->area, &child_event.area))
327             gtk_widget_event (paned->child2, (GdkEvent *) &child_event);
328         }
329     }
330
331   return FALSE;
332 }
333
334 void
335 gtk_paned_add1 (GtkPaned  *paned,
336                 GtkWidget *widget)
337 {
338   gtk_paned_pack1 (paned, widget, FALSE, TRUE);
339 }
340
341 void
342 gtk_paned_add2 (GtkPaned  *paned,
343                 GtkWidget *widget)
344 {
345   gtk_paned_pack2 (paned, widget, TRUE, TRUE);
346 }
347
348 void
349 gtk_paned_pack1 (GtkPaned  *paned,
350                  GtkWidget *child,
351                  gboolean   resize,
352                  gboolean   shrink)
353 {
354   g_return_if_fail (paned != NULL);
355   g_return_if_fail (GTK_IS_PANED (paned));
356   g_return_if_fail (GTK_IS_WIDGET (child));
357
358   if (!paned->child1)
359     {
360       paned->child1 = child;
361       paned->child1_resize = resize;
362       paned->child1_shrink = shrink;
363
364       gtk_widget_set_parent (child, GTK_WIDGET (paned));
365
366       if (GTK_WIDGET_REALIZED (child->parent))
367         gtk_widget_realize (child);
368
369       if (GTK_WIDGET_VISIBLE (child->parent) && GTK_WIDGET_VISIBLE (child))
370         {
371           if (GTK_WIDGET_MAPPED (child->parent))
372             gtk_widget_map (child);
373
374           gtk_widget_queue_resize (child);
375         }
376     }
377 }
378
379 void
380 gtk_paned_pack2 (GtkPaned  *paned,
381                  GtkWidget *child,
382                  gboolean   resize,
383                  gboolean   shrink)
384 {
385   g_return_if_fail (paned != NULL);
386   g_return_if_fail (GTK_IS_PANED (paned));
387   g_return_if_fail (GTK_IS_WIDGET (child));
388
389   if (!paned->child2)
390     {
391       paned->child2 = child;
392       paned->child2_resize = resize;
393       paned->child2_shrink = shrink;
394
395       gtk_widget_set_parent (child, GTK_WIDGET (paned));
396
397       if (GTK_WIDGET_REALIZED (child->parent))
398         gtk_widget_realize (child);
399
400       if (GTK_WIDGET_VISIBLE (child->parent) && GTK_WIDGET_VISIBLE (child))
401         {
402           if (GTK_WIDGET_MAPPED (child->parent))
403             gtk_widget_map (child);
404
405           gtk_widget_queue_resize (child);
406         }
407     }
408 }
409
410
411 static void
412 gtk_paned_add (GtkContainer *container,
413                GtkWidget    *widget)
414 {
415   GtkPaned *paned;
416
417   g_return_if_fail (container != NULL);
418   g_return_if_fail (GTK_IS_PANED (container));
419   g_return_if_fail (widget != NULL);
420
421   paned = GTK_PANED (container);
422
423   if (!paned->child1)
424     gtk_paned_add1 (GTK_PANED (container), widget);
425   else if (!paned->child2)
426     gtk_paned_add2 (GTK_PANED (container), widget);
427 }
428
429 static void
430 gtk_paned_remove (GtkContainer *container,
431                   GtkWidget    *widget)
432 {
433   GtkPaned *paned;
434   gboolean was_visible;
435
436   g_return_if_fail (container != NULL);
437   g_return_if_fail (GTK_IS_PANED (container));
438   g_return_if_fail (widget != NULL);
439
440   paned = GTK_PANED (container);
441   was_visible = GTK_WIDGET_VISIBLE (widget);
442
443   if (paned->child1 == widget)
444     {
445       gtk_widget_unparent (widget);
446
447       paned->child1 = NULL;
448
449       if (was_visible && GTK_WIDGET_VISIBLE (container))
450         gtk_widget_queue_resize (GTK_WIDGET (container));
451     }
452   else if (paned->child2 == widget)
453     {
454       gtk_widget_unparent (widget);
455
456       paned->child2 = NULL;
457
458       if (was_visible && GTK_WIDGET_VISIBLE (container))
459         gtk_widget_queue_resize (GTK_WIDGET (container));
460     }
461 }
462
463 static void
464 gtk_paned_forall (GtkContainer *container,
465                   gboolean      include_internals,
466                   GtkCallback   callback,
467                   gpointer      callback_data)
468 {
469   GtkPaned *paned;
470
471   g_return_if_fail (container != NULL);
472   g_return_if_fail (GTK_IS_PANED (container));
473   g_return_if_fail (callback != NULL);
474
475   paned = GTK_PANED (container);
476
477   if (paned->child1)
478     (*callback) (paned->child1, callback_data);
479   if (paned->child2)
480     (*callback) (paned->child2, callback_data);
481 }
482
483 /**
484  * gtk_paned_get_position:
485  * @paned: a #GtkPaned widget
486  * 
487  * Obtains the position of the divider between the two panes.
488  * 
489  * Return value: position of the divider
490  **/
491 gint
492 gtk_paned_get_position (GtkPaned  *paned)
493 {
494   g_return_val_if_fail (paned != NULL, 0);
495   g_return_val_if_fail (GTK_IS_PANED (paned), 0);
496
497   return paned->child1_size;
498 }
499
500 /**
501  * gtk_paned_set_position:
502  * @paned: a #GtkPaned widget
503  * @position: pixel position of divider
504  * 
505  * Sets the position of the divider between the two panes.
506  **/
507 void
508 gtk_paned_set_position (GtkPaned *paned,
509                         gint      position)
510 {
511   g_return_if_fail (paned != NULL);
512   g_return_if_fail (GTK_IS_PANED (paned));
513
514   if (position >= 0)
515     {
516       /* We don't clamp here - the assumption is that
517        * if the total allocation changes at the same time
518        * as the position, the position set is with reference
519        * to the new total size. If only the position changes,
520        * then clamping will occur in gtk_paned_compute_position()
521        */
522       paned->child1_size = position;
523       paned->position_set = TRUE;
524     }
525   else
526     {
527       paned->position_set = FALSE;
528     }
529
530   gtk_widget_queue_resize (GTK_WIDGET (paned));
531 }
532
533 void
534 gtk_paned_set_handle_size (GtkPaned *paned,
535                            guint16   size)
536 {
537   g_return_if_fail (paned != NULL);
538   g_return_if_fail (GTK_IS_PANED (paned));
539
540   gtk_widget_queue_resize (GTK_WIDGET (paned));
541
542   paned->handle_size = size;
543 }
544
545 void
546 gtk_paned_compute_position(GtkPaned *paned,
547                            gint      allocation,
548                            gint      child1_req,
549                            gint      child2_req)
550 {
551   g_return_if_fail (paned != NULL);
552   g_return_if_fail (GTK_IS_PANED (paned));
553
554   paned->min_position = paned->child1_shrink ? 0 : child1_req;
555
556   paned->max_position = allocation;
557   if (!paned->child2_shrink)
558     paned->max_position = MAX (1, paned->max_position - child2_req);
559
560   if (!paned->position_set)
561     {
562       if (paned->child1_resize && !paned->child2_resize)
563         paned->child1_size = MAX (1, allocation - child2_req);
564       else if (!paned->child1_resize && paned->child2_resize)
565         paned->child1_size = child1_req;
566       else if (child1_req + child2_req != 0)
567         paned->child1_size = allocation * ((gdouble)child1_req / (child1_req + child2_req));
568       else
569         paned->child1_size = allocation * 0.5;
570     }
571   else
572     {
573       /* If the position was set before the initial allocation.
574        * (paned->last_allocation <= 0) just clamp it and leave it.
575        */
576       if (paned->last_allocation > 0)
577         {
578           if (paned->child1_resize && !paned->child2_resize)
579             paned->child1_size += allocation - paned->last_allocation;
580           else if (!(!paned->child1_resize && paned->child2_resize))
581             paned->child1_size = allocation * ((gdouble) paned->child1_size / (paned->last_allocation));
582         }
583     }
584
585   paned->child1_size = CLAMP (paned->child1_size,
586                               paned->min_position,
587                               paned->max_position);
588
589   paned->last_allocation = allocation;
590 }