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, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GTK+ Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23  */
24 
25 /**
26  * SECTION:gtkfixed
27  * @Short_description: A container which allows you to position
28  * widgets at fixed coordinates
29  * @Title: GtkFixed
30  * @See_also: #GtkLayout
31  *
32  * The #GtkFixed widget is a container which can place child widgets
33  * at fixed positions and with fixed sizes, given in pixels. #GtkFixed
34  * performs no automatic layout management.
35  *
36  * For most applications, you should not use this container! It keeps
37  * you from having to learn about the other GTK+ containers, but it
38  * results in broken applications.  With #GtkFixed, the following
39  * things will result in truncated text, overlapping widgets, and
40  * other display bugs:
41  *
42  * - Themes, which may change widget sizes.
43  *
44  * - Fonts other than the one you used to write the app will of course
45  *   change the size of widgets containing text; keep in mind that
46  *   users may use a larger font because of difficulty reading the
47  *   default, or they may be using a different OS that provides different fonts.
48  *
49  * - Translation of text into other languages changes its size. Also,
50  *   display of non-English text will use a different font in many
51  *   cases.
52  *
53  * In addition, #GtkFixed does not pay attention to text direction and thus may
54  * produce unwanted results if your app is run under right-to-left languages
55  * such as Hebrew or Arabic. That is: normally GTK+ will order containers
56  * appropriately for the text direction, e.g. to put labels to the right of the
57  * thing they label when using an RTL language, but it can’t do that with
58  * #GtkFixed. So if you need to reorder widgets depending on the text direction,
59  * you would need to manually detect it and adjust child positions accordingly.
60  *
61  * Finally, fixed positioning makes it kind of annoying to add/remove
62  * GUI elements, since you have to reposition all the other
63  * elements. This is a long-term maintenance problem for your
64  * application.
65  *
66  * If you know none of these things are an issue for your application,
67  * and prefer the simplicity of #GtkFixed, by all means use the
68  * widget. But you should be aware of the tradeoffs.
69  *
70  * See also #GtkLayout, which shares the ability to perform fixed positioning
71  * of child widgets and additionally adds custom drawing and scrollability.
72  */
73 
74 #include "config.h"
75 
76 #include "gtkfixed.h"
77 
78 #include "gtkwidgetprivate.h"
79 #include "gtkprivate.h"
80 #include "gtkintl.h"
81 
82 
83 struct _GtkFixedPrivate
84 {
85   GList *children;
86 };
87 
88 enum {
89   CHILD_PROP_0,
90   CHILD_PROP_X,
91   CHILD_PROP_Y
92 };
93 
94 static void gtk_fixed_realize       (GtkWidget        *widget);
95 static void gtk_fixed_get_preferred_width  (GtkWidget *widget,
96                                             gint      *minimum,
97                                             gint      *natural);
98 static void gtk_fixed_get_preferred_height (GtkWidget *widget,
99                                             gint      *minimum,
100                                             gint      *natural);
101 static void gtk_fixed_size_allocate (GtkWidget        *widget,
102                                      GtkAllocation    *allocation);
103 static void gtk_fixed_style_updated (GtkWidget        *widget);
104 static gboolean gtk_fixed_draw      (GtkWidget        *widget,
105                                      cairo_t          *cr);
106 static void gtk_fixed_add           (GtkContainer     *container,
107                                      GtkWidget        *widget);
108 static void gtk_fixed_remove        (GtkContainer     *container,
109                                      GtkWidget        *widget);
110 static void gtk_fixed_forall        (GtkContainer     *container,
111                                      gboolean          include_internals,
112                                      GtkCallback       callback,
113                                      gpointer          callback_data);
114 static GType gtk_fixed_child_type   (GtkContainer     *container);
115 
116 static void gtk_fixed_set_child_property (GtkContainer *container,
117                                           GtkWidget    *child,
118                                           guint         property_id,
119                                           const GValue *value,
120                                           GParamSpec   *pspec);
121 static void gtk_fixed_get_child_property (GtkContainer *container,
122                                           GtkWidget    *child,
123                                           guint         property_id,
124                                           GValue       *value,
125                                           GParamSpec   *pspec);
126 
G_DEFINE_TYPE_WITH_PRIVATE(GtkFixed,gtk_fixed,GTK_TYPE_CONTAINER)127 G_DEFINE_TYPE_WITH_PRIVATE (GtkFixed, gtk_fixed, GTK_TYPE_CONTAINER)
128 
129 static void
130 gtk_fixed_class_init (GtkFixedClass *class)
131 {
132   GtkWidgetClass *widget_class;
133   GtkContainerClass *container_class;
134 
135   widget_class = (GtkWidgetClass*) class;
136   container_class = (GtkContainerClass*) class;
137 
138   widget_class->realize = gtk_fixed_realize;
139   widget_class->get_preferred_width = gtk_fixed_get_preferred_width;
140   widget_class->get_preferred_height = gtk_fixed_get_preferred_height;
141   widget_class->size_allocate = gtk_fixed_size_allocate;
142   widget_class->draw = gtk_fixed_draw;
143   widget_class->style_updated = gtk_fixed_style_updated;
144 
145   container_class->add = gtk_fixed_add;
146   container_class->remove = gtk_fixed_remove;
147   container_class->forall = gtk_fixed_forall;
148   container_class->child_type = gtk_fixed_child_type;
149   container_class->set_child_property = gtk_fixed_set_child_property;
150   container_class->get_child_property = gtk_fixed_get_child_property;
151   gtk_container_class_handle_border_width (container_class);
152 
153   gtk_container_class_install_child_property (container_class,
154                                               CHILD_PROP_X,
155                                               g_param_spec_int ("x",
156                                                                 P_("X position"),
157                                                                 P_("X position of child widget"),
158                                                                 G_MININT, G_MAXINT, 0,
159                                                                 GTK_PARAM_READWRITE));
160 
161   gtk_container_class_install_child_property (container_class,
162                                               CHILD_PROP_Y,
163                                               g_param_spec_int ("y",
164                                                                 P_("Y position"),
165                                                                 P_("Y position of child widget"),
166                                                                 G_MININT, G_MAXINT, 0,
167                                                                 GTK_PARAM_READWRITE));
168 }
169 
170 static GType
gtk_fixed_child_type(GtkContainer * container)171 gtk_fixed_child_type (GtkContainer *container)
172 {
173   return GTK_TYPE_WIDGET;
174 }
175 
176 static void
gtk_fixed_init(GtkFixed * fixed)177 gtk_fixed_init (GtkFixed *fixed)
178 {
179   fixed->priv = gtk_fixed_get_instance_private (fixed);
180 
181   gtk_widget_set_has_window (GTK_WIDGET (fixed), FALSE);
182 
183   fixed->priv->children = NULL;
184 }
185 
186 /**
187  * gtk_fixed_new:
188  *
189  * Creates a new #GtkFixed.
190  *
191  * Returns: a new #GtkFixed.
192  */
193 GtkWidget*
gtk_fixed_new(void)194 gtk_fixed_new (void)
195 {
196   return g_object_new (GTK_TYPE_FIXED, NULL);
197 }
198 
199 static GtkFixedChild*
get_child(GtkFixed * fixed,GtkWidget * widget)200 get_child (GtkFixed  *fixed,
201            GtkWidget *widget)
202 {
203   GtkFixedPrivate *priv = fixed->priv;
204   GList *children;
205 
206   for (children = priv->children; children; children = children->next)
207     {
208       GtkFixedChild *child;
209 
210       child = children->data;
211 
212       if (child->widget == widget)
213         return child;
214     }
215 
216   return NULL;
217 }
218 
219 /**
220  * gtk_fixed_put:
221  * @fixed: a #GtkFixed.
222  * @widget: the widget to add.
223  * @x: the horizontal position to place the widget at.
224  * @y: the vertical position to place the widget at.
225  *
226  * Adds a widget to a #GtkFixed container at the given position.
227  */
228 void
gtk_fixed_put(GtkFixed * fixed,GtkWidget * widget,gint x,gint y)229 gtk_fixed_put (GtkFixed  *fixed,
230                GtkWidget *widget,
231                gint       x,
232                gint       y)
233 {
234   GtkFixedPrivate *priv;
235   GtkFixedChild *child_info;
236 
237   g_return_if_fail (GTK_IS_FIXED (fixed));
238   g_return_if_fail (GTK_IS_WIDGET (widget));
239   g_return_if_fail (_gtk_widget_get_parent (widget) == NULL);
240 
241   priv = fixed->priv;
242 
243   child_info = g_new (GtkFixedChild, 1);
244   child_info->widget = widget;
245   child_info->x = x;
246   child_info->y = y;
247 
248   gtk_widget_set_parent (widget, GTK_WIDGET (fixed));
249 
250   priv->children = g_list_append (priv->children, child_info);
251 }
252 
253 static void
gtk_fixed_move_internal(GtkFixed * fixed,GtkFixedChild * child,gint x,gint y)254 gtk_fixed_move_internal (GtkFixed      *fixed,
255                          GtkFixedChild *child,
256                          gint           x,
257                          gint           y)
258 {
259   g_return_if_fail (GTK_IS_FIXED (fixed));
260   g_return_if_fail (gtk_widget_get_parent (child->widget) == GTK_WIDGET (fixed));
261 
262   gtk_widget_freeze_child_notify (child->widget);
263 
264   if (child->x != x)
265     {
266       child->x = x;
267       gtk_widget_child_notify (child->widget, "x");
268     }
269 
270   if (child->y != y)
271     {
272       child->y = y;
273       gtk_widget_child_notify (child->widget, "y");
274     }
275 
276   gtk_widget_thaw_child_notify (child->widget);
277 
278   if (gtk_widget_get_visible (child->widget) &&
279       gtk_widget_get_visible (GTK_WIDGET (fixed)))
280     gtk_widget_queue_resize (GTK_WIDGET (fixed));
281 }
282 
283 /**
284  * gtk_fixed_move:
285  * @fixed: a #GtkFixed.
286  * @widget: the child widget.
287  * @x: the horizontal position to move the widget to.
288  * @y: the vertical position to move the widget to.
289  *
290  * Moves a child of a #GtkFixed container to the given position.
291  */
292 void
gtk_fixed_move(GtkFixed * fixed,GtkWidget * widget,gint x,gint y)293 gtk_fixed_move (GtkFixed  *fixed,
294                 GtkWidget *widget,
295                 gint       x,
296                 gint       y)
297 {
298   gtk_fixed_move_internal (fixed, get_child (fixed, widget), x, y);
299 }
300 
301 static void
gtk_fixed_set_child_property(GtkContainer * container,GtkWidget * child,guint property_id,const GValue * value,GParamSpec * pspec)302 gtk_fixed_set_child_property (GtkContainer *container,
303                               GtkWidget    *child,
304                               guint         property_id,
305                               const GValue *value,
306                               GParamSpec   *pspec)
307 {
308   GtkFixed *fixed = GTK_FIXED (container);
309   GtkFixedChild *fixed_child;
310 
311   fixed_child = get_child (fixed, child);
312 
313   switch (property_id)
314     {
315     case CHILD_PROP_X:
316       gtk_fixed_move_internal (fixed,
317                                fixed_child,
318                                g_value_get_int (value),
319                                fixed_child->y);
320       break;
321     case CHILD_PROP_Y:
322       gtk_fixed_move_internal (fixed,
323                                fixed_child,
324                                fixed_child->x,
325                                g_value_get_int (value));
326       break;
327     default:
328       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
329       break;
330     }
331 }
332 
333 static void
gtk_fixed_get_child_property(GtkContainer * container,GtkWidget * child,guint property_id,GValue * value,GParamSpec * pspec)334 gtk_fixed_get_child_property (GtkContainer *container,
335                               GtkWidget    *child,
336                               guint         property_id,
337                               GValue       *value,
338                               GParamSpec   *pspec)
339 {
340   GtkFixedChild *fixed_child;
341 
342   fixed_child = get_child (GTK_FIXED (container), child);
343 
344   switch (property_id)
345     {
346     case CHILD_PROP_X:
347       g_value_set_int (value, fixed_child->x);
348       break;
349     case CHILD_PROP_Y:
350       g_value_set_int (value, fixed_child->y);
351       break;
352     default:
353       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
354       break;
355     }
356 }
357 
358 static void
set_background(GtkWidget * widget)359 set_background (GtkWidget *widget)
360 {
361   if (gtk_widget_get_realized (widget))
362     {
363       /* We still need to call gtk_style_context_set_background() here for
364        * GtkFixed, since subclasses like EmacsFixed depend on the X window
365        * background to be set.
366        * This should be revisited next time we have a major API break.
367        */
368       G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
369       gtk_style_context_set_background (gtk_widget_get_style_context (widget),
370                                         gtk_widget_get_window (widget));
371       G_GNUC_END_IGNORE_DEPRECATIONS;
372     }
373 }
374 
375 static void
gtk_fixed_style_updated(GtkWidget * widget)376 gtk_fixed_style_updated (GtkWidget *widget)
377 {
378   GTK_WIDGET_CLASS (gtk_fixed_parent_class)->style_updated (widget);
379 
380   set_background (widget);
381 }
382 
383 static void
gtk_fixed_realize(GtkWidget * widget)384 gtk_fixed_realize (GtkWidget *widget)
385 {
386   GtkAllocation allocation;
387   GdkWindow *window;
388   GdkWindowAttr attributes;
389   gint attributes_mask;
390 
391   if (!gtk_widget_get_has_window (widget))
392     GTK_WIDGET_CLASS (gtk_fixed_parent_class)->realize (widget);
393   else
394     {
395       gtk_widget_set_realized (widget, TRUE);
396 
397       gtk_widget_get_allocation (widget, &allocation);
398 
399       attributes.window_type = GDK_WINDOW_CHILD;
400       attributes.x = allocation.x;
401       attributes.y = allocation.y;
402       attributes.width = allocation.width;
403       attributes.height = allocation.height;
404       attributes.wclass = GDK_INPUT_OUTPUT;
405       attributes.visual = gtk_widget_get_visual (widget);
406       attributes.event_mask = gtk_widget_get_events (widget);
407       attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK;
408 
409       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
410 
411       window = gdk_window_new (gtk_widget_get_parent_window (widget),
412                                &attributes, attributes_mask);
413       gtk_widget_set_window (widget, window);
414       gtk_widget_register_window (widget, window);
415 
416       set_background (widget);
417     }
418 }
419 
420 static void
gtk_fixed_get_preferred_width(GtkWidget * widget,gint * minimum,gint * natural)421 gtk_fixed_get_preferred_width (GtkWidget *widget,
422                                gint      *minimum,
423                                gint      *natural)
424 {
425   GtkFixed *fixed = GTK_FIXED (widget);
426   GtkFixedPrivate *priv = fixed->priv;
427   GtkFixedChild *child;
428   GList *children;
429   gint child_min, child_nat;
430 
431   *minimum = 0;
432   *natural = 0;
433 
434   for (children = priv->children; children; children = children->next)
435     {
436       child = children->data;
437 
438       if (!gtk_widget_get_visible (child->widget))
439         continue;
440 
441       gtk_widget_get_preferred_width (child->widget, &child_min, &child_nat);
442 
443       *minimum = MAX (*minimum, child->x + child_min);
444       *natural = MAX (*natural, child->x + child_nat);
445     }
446 }
447 
448 static void
gtk_fixed_get_preferred_height(GtkWidget * widget,gint * minimum,gint * natural)449 gtk_fixed_get_preferred_height (GtkWidget *widget,
450                                 gint      *minimum,
451                                 gint      *natural)
452 {
453   GtkFixed *fixed = GTK_FIXED (widget);
454   GtkFixedPrivate *priv = fixed->priv;
455   GtkFixedChild *child;
456   GList *children;
457   gint child_min, child_nat;
458 
459   *minimum = 0;
460   *natural = 0;
461 
462   for (children = priv->children; children; children = children->next)
463     {
464       child = children->data;
465 
466       if (!gtk_widget_get_visible (child->widget))
467         continue;
468 
469       gtk_widget_get_preferred_height (child->widget, &child_min, &child_nat);
470 
471       *minimum = MAX (*minimum, child->y + child_min);
472       *natural = MAX (*natural, child->y + child_nat);
473     }
474 }
475 
476 static void
gtk_fixed_size_allocate(GtkWidget * widget,GtkAllocation * allocation)477 gtk_fixed_size_allocate (GtkWidget     *widget,
478                          GtkAllocation *allocation)
479 {
480   GtkFixed *fixed = GTK_FIXED (widget);
481   GtkFixedPrivate *priv = fixed->priv;
482   GtkFixedChild *child;
483   GtkAllocation child_allocation;
484   GtkRequisition child_requisition;
485   GList *children;
486 
487   gtk_widget_set_allocation (widget, allocation);
488 
489   if (gtk_widget_get_has_window (widget))
490     {
491       if (gtk_widget_get_realized (widget))
492         gdk_window_move_resize (gtk_widget_get_window (widget),
493                                 allocation->x,
494                                 allocation->y,
495                                 allocation->width,
496                                 allocation->height);
497     }
498 
499   for (children = priv->children; children; children = children->next)
500     {
501       child = children->data;
502 
503       if (!gtk_widget_get_visible (child->widget))
504         continue;
505 
506       gtk_widget_get_preferred_size (child->widget, &child_requisition, NULL);
507       child_allocation.x = child->x;
508       child_allocation.y = child->y;
509 
510       if (!gtk_widget_get_has_window (widget))
511         {
512           child_allocation.x += allocation->x;
513           child_allocation.y += allocation->y;
514         }
515 
516       child_allocation.width = child_requisition.width;
517       child_allocation.height = child_requisition.height;
518       gtk_widget_size_allocate (child->widget, &child_allocation);
519     }
520 }
521 
522 static void
gtk_fixed_add(GtkContainer * container,GtkWidget * widget)523 gtk_fixed_add (GtkContainer *container,
524                GtkWidget    *widget)
525 {
526   gtk_fixed_put (GTK_FIXED (container), widget, 0, 0);
527 }
528 
529 static void
gtk_fixed_remove(GtkContainer * container,GtkWidget * widget)530 gtk_fixed_remove (GtkContainer *container,
531                   GtkWidget    *widget)
532 {
533   GtkFixed *fixed = GTK_FIXED (container);
534   GtkFixedPrivate *priv = fixed->priv;
535   GtkFixedChild *child;
536   GtkWidget *widget_container = GTK_WIDGET (container);
537   GList *children;
538 
539   for (children = priv->children; children; children = children->next)
540     {
541       child = children->data;
542 
543       if (child->widget == widget)
544         {
545           gboolean was_visible = gtk_widget_get_visible (widget);
546 
547           gtk_widget_unparent (widget);
548 
549           priv->children = g_list_remove_link (priv->children, children);
550           g_list_free (children);
551           g_free (child);
552 
553           if (was_visible && gtk_widget_get_visible (widget_container))
554             gtk_widget_queue_resize (widget_container);
555 
556           break;
557         }
558     }
559 }
560 
561 static void
gtk_fixed_forall(GtkContainer * container,gboolean include_internals,GtkCallback callback,gpointer callback_data)562 gtk_fixed_forall (GtkContainer *container,
563                   gboolean      include_internals,
564                   GtkCallback   callback,
565                   gpointer      callback_data)
566 {
567   GtkFixed *fixed = GTK_FIXED (container);
568   GtkFixedPrivate *priv = fixed->priv;
569   GtkFixedChild *child;
570   GList *children;
571 
572   children = priv->children;
573   while (children)
574     {
575       child = children->data;
576       children = children->next;
577 
578       (* callback) (child->widget, callback_data);
579     }
580 }
581 
582 static gboolean
gtk_fixed_draw(GtkWidget * widget,cairo_t * cr)583 gtk_fixed_draw (GtkWidget *widget,
584                 cairo_t   *cr)
585 {
586   GtkFixed *fixed = GTK_FIXED (widget);
587   GtkFixedPrivate *priv = fixed->priv;
588   GtkFixedChild *child;
589   GList *list;
590 
591   for (list = priv->children;
592        list;
593        list = list->next)
594     {
595       child = list->data;
596 
597       gtk_container_propagate_draw (GTK_CONTAINER (fixed),
598                                     child->widget,
599                                     cr);
600     }
601 
602   return FALSE;
603 }
604 
605