]> code.delx.au - gnu-emacs/blob - src/emacsgtkfixed.c
Merge from emacs-23; up to 2010-06-15T03:34:12Z!rgm@gnu.org.
[gnu-emacs] / src / emacsgtkfixed.c
1 /* A Gtk Widget that inherits GtkFixed, but can be shrinked.
2
3 Copyright (C) 2011 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "emacsgtkfixed.h"
21
22
23 struct _EmacsFixedPrivate
24 {
25 int minwidth, minheight;
26 };
27
28
29 static void emacs_fixed_get_preferred_width (GtkWidget *widget,
30 gint *minimum,
31 gint *natural);
32 static void emacs_fixed_get_preferred_height (GtkWidget *widget,
33 gint *minimum,
34 gint *natural);
35 G_DEFINE_TYPE (EmacsFixed, emacs_fixed, GTK_TYPE_FIXED)
36
37 static void
38 emacs_fixed_class_init (EmacsFixedClass *klass)
39 {
40 GtkWidgetClass *widget_class;
41 GtkFixedClass *fixed_class;
42
43 widget_class = (GtkWidgetClass*) klass;
44 fixed_class = (GtkFixedClass*) klass;
45
46 widget_class->get_preferred_width = emacs_fixed_get_preferred_width;
47 widget_class->get_preferred_height = emacs_fixed_get_preferred_height;
48 g_type_class_add_private (klass, sizeof (EmacsFixedPrivate));
49 }
50
51 static GType
52 emacs_fixed_child_type (GtkFixed *container)
53 {
54 return GTK_TYPE_WIDGET;
55 }
56
57 static void
58 emacs_fixed_init (EmacsFixed *fixed)
59 {
60 fixed->priv = G_TYPE_INSTANCE_GET_PRIVATE (fixed, EMACS_TYPE_FIXED,
61 EmacsFixedPrivate);
62 fixed->priv->minwidth = fixed->priv->minheight = 0;
63 }
64
65 /**
66 * emacs_fixed_new:
67 *
68 * Creates a new #EmacsFixed.
69 *
70 * Returns: a new #EmacsFixed.
71 */
72 GtkWidget*
73 emacs_fixed_new (void)
74 {
75 return g_object_new (EMACS_TYPE_FIXED, NULL);
76 }
77
78 static GtkWidgetClass *
79 get_parent_class (EmacsFixed *fixed)
80 {
81 EmacsFixedClass *klass = EMACS_FIXED_GET_CLASS (fixed);
82 GtkFixedClass *parent_class = g_type_class_peek_parent (klass);
83 return (GtkWidgetClass*) parent_class;
84 }
85
86 static void
87 emacs_fixed_get_preferred_width (GtkWidget *widget,
88 gint *minimum,
89 gint *natural)
90 {
91 EmacsFixed *fixed = EMACS_FIXED (widget);
92 EmacsFixedPrivate *priv = fixed->priv;
93 GtkWidgetClass *widget_class = get_parent_class (fixed);
94 widget_class->get_preferred_width (widget, minimum, natural);
95 if (minimum) *minimum = priv->minwidth;
96 }
97
98 static void
99 emacs_fixed_get_preferred_height (GtkWidget *widget,
100 gint *minimum,
101 gint *natural)
102 {
103 EmacsFixed *fixed = EMACS_FIXED (widget);
104 EmacsFixedPrivate *priv = fixed->priv;
105 GtkWidgetClass *widget_class = get_parent_class (fixed);
106 widget_class->get_preferred_height (widget, minimum, natural);
107 if (minimum) *minimum = priv->minheight;
108 }
109
110 void
111 emacs_fixed_set_min_size (EmacsFixed *widget, int width, int height)
112 {
113 EmacsFixedPrivate *priv = widget->priv;
114 GtkWidgetClass *widget_class = get_parent_class (widget);
115 int mw, nw, mh, nh;
116
117 widget_class->get_preferred_height (GTK_WIDGET (widget), &mh, &nh);
118 widget_class->get_preferred_width (GTK_WIDGET (widget), &mw, &nw);
119
120 /* Gtk complains if min size is less than natural size. */
121 if (width <= nw) priv->minwidth = width;
122 if (height <= nh) priv->minheight = height;
123 }