use a single window, with tabs
[empathy.git] / src / polari-fixed-size-frame.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3  * Copyright (C) 2013 Red Hat, Inc.
4  *
5  * This program is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published
7  * by the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.";
17  */
18
19 #include "polari-fixed-size-frame.h"
20
21 struct _PolariFixedSizeFramePrivate {
22   int width;
23   int height;
24 };
25
26 enum
27 {
28   PROP_0,
29
30   PROP_WIDTH,
31   PROP_HEIGHT,
32
33   LAST_PROP
34 };
35
36 static GParamSpec *props[LAST_PROP];
37
38 static void
39 polari_fixed_size_frame_buildable_init (GtkBuildableIface *iface);
40
41 G_DEFINE_TYPE_WITH_CODE (PolariFixedSizeFrame, polari_fixed_size_frame,
42                          GTK_TYPE_FRAME,
43                          G_ADD_PRIVATE (PolariFixedSizeFrame)
44                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
45                                                 polari_fixed_size_frame_buildable_init))
46
47 static void
48 polari_fixed_size_frame_buildable_init (GtkBuildableIface *iface)
49 {
50 }
51
52 static void
53 queue_redraw (PolariFixedSizeFrame *self)
54 {
55   GtkWidget *child = gtk_bin_get_child (GTK_BIN (self));
56
57   if (child)
58     gtk_widget_queue_resize (child);
59
60   gtk_widget_queue_draw (GTK_WIDGET (self));
61 }
62
63 static void
64 polari_fixed_size_frame_set_width (PolariFixedSizeFrame *self,
65                                        int                       width)
66 {
67   if (self->priv->width != width)
68     {
69       self->priv->width = width;
70       g_object_notify_by_pspec (G_OBJECT (self), props[PROP_WIDTH]);
71
72       queue_redraw (self);
73     }
74 }
75
76 static void
77 polari_fixed_size_frame_set_height (PolariFixedSizeFrame *self,
78                                         int                       height)
79 {
80   if (self->priv->height != height)
81     {
82       self->priv->height = height;
83       g_object_notify_by_pspec (G_OBJECT (self), props[PROP_HEIGHT]);
84
85       queue_redraw (self);
86     }
87 }
88
89 static void
90 polari_fixed_size_frame_set_property (GObject      *object,
91                                           guint         prop_id,
92                                           const GValue *value,
93                                           GParamSpec   *pspec)
94 {
95   PolariFixedSizeFrame *self = POLARI_FIXED_SIZE_FRAME (object);
96
97   switch (prop_id)
98     {
99     case PROP_WIDTH:
100       polari_fixed_size_frame_set_width(self, g_value_get_int (value));
101       break;
102     case PROP_HEIGHT:
103       polari_fixed_size_frame_set_height(self, g_value_get_int (value));
104       break;
105     default:
106       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
107     }
108 }
109
110 static void
111 polari_fixed_size_frame_get_property (GObject    *object,
112                                           guint       prop_id,
113                                           GValue     *value,
114                                           GParamSpec *pspec)
115 {
116   PolariFixedSizeFrame *self = POLARI_FIXED_SIZE_FRAME (object);
117
118   switch (prop_id)
119     {
120     case PROP_WIDTH:
121         g_value_set_int (value, self->priv->width);
122       break;
123     case PROP_HEIGHT:
124         g_value_set_int (value, self->priv->height);
125       break;
126     default:
127       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
128     }
129 }
130
131 static void
132 polari_fixed_size_frame_get_preferred_width (GtkWidget *widget,
133                                                  int       *minimum_size,
134                                                  int       *natural_size)
135 {
136   PolariFixedSizeFrame *self = POLARI_FIXED_SIZE_FRAME (widget);
137
138   if (self->priv->width == -1)
139     {
140       GTK_WIDGET_CLASS (polari_fixed_size_frame_parent_class)->get_preferred_width (widget, minimum_size, natural_size);
141     }
142   else
143     {
144       *minimum_size = *natural_size = self->priv->width;
145     }
146 }
147
148 static void
149 polari_fixed_size_frame_get_preferred_height (GtkWidget *widget,
150                                                   int       *minimum_size,
151                                                   int       *natural_size)
152 {
153   PolariFixedSizeFrame *self = POLARI_FIXED_SIZE_FRAME (widget);
154
155   if (self->priv->height == -1)
156     {
157       GTK_WIDGET_CLASS (polari_fixed_size_frame_parent_class)->get_preferred_height (widget, minimum_size, natural_size);
158     }
159   else
160     {
161       *minimum_size = *natural_size = self->priv->height;
162     }
163 }
164
165 static void
166 polari_fixed_size_frame_class_init (PolariFixedSizeFrameClass *klass)
167 {
168   GObjectClass *object_class = G_OBJECT_CLASS (klass);
169   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
170   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
171
172   object_class->get_property = polari_fixed_size_frame_get_property;
173   object_class->set_property = polari_fixed_size_frame_set_property;
174   widget_class->get_preferred_width =
175     polari_fixed_size_frame_get_preferred_width;
176   widget_class->get_preferred_height =
177     polari_fixed_size_frame_get_preferred_height;
178   gtk_container_class_handle_border_width (container_class);
179
180   props[PROP_WIDTH] =
181     g_param_spec_int ("width",
182                       "Width",
183                       "Fixed width of the widget, or -1 to use the child's "
184                       "width",
185                       -1,
186                       G_MAXINT,
187                       -1,
188                       G_PARAM_READWRITE);
189
190   props[PROP_HEIGHT] =
191     g_param_spec_int ("height",
192                       "Height",
193                       "Fixed height of the widget, or -1 to use the child's "
194                       "height",
195                       -1,
196                       G_MAXINT,
197                       -1,
198                       G_PARAM_READWRITE);
199
200   g_object_class_install_properties (object_class, LAST_PROP, props);
201 }
202
203 static void
204 polari_fixed_size_frame_init (PolariFixedSizeFrame *self)
205 {
206   self->priv = polari_fixed_size_frame_get_instance_private (self),
207   self->priv->width = -1;
208   self->priv->height = -1;
209 }