]> git.sur5r.net Git - glabels/blob - glabels2/src/rotate-label-button.c
2009-09-17 Jim Evins <evins@snaught.com>
[glabels] / glabels2 / src / rotate-label-button.c
1 /*
2  *  rotate-label-button.c
3  *  Copyright (C) 2001-2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
7  *  gLabels 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  *  gLabels 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 gLabels.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22
23 #include "rotate-label-button.h"
24
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27 #include <math.h>
28
29 #include <libglabels/libglabels.h>
30 #include "mini-label-preview.h"
31 #include "hig.h"
32 #include "marshal.h"
33
34 #include "debug.h"
35
36
37 /*========================================================*/
38 /* Private macros and constants.                          */
39 /*========================================================*/
40
41 #define SIZE 48
42
43
44 /*===========================================*/
45 /* Private types                             */
46 /*===========================================*/
47
48 struct _glRotateLabelButtonPrivate {
49
50         GtkWidget    *no_rotate_radio;
51         GtkWidget    *rotate_radio;
52         GtkWidget    *no_rotate_preview;
53         GtkWidget    *rotate_preview;
54 };
55
56 enum {
57         CHANGED,
58         LAST_SIGNAL
59 };
60
61
62 /*===========================================*/
63 /* Private globals                           */
64 /*===========================================*/
65
66 static gint rotate_label_button_signals[LAST_SIGNAL] = { 0 };
67
68
69 /*===========================================*/
70 /* Local function prototypes                 */
71 /*===========================================*/
72
73 static void gl_rotate_label_button_finalize    (GObject                *object);
74
75 static void toggled_cb                         (GtkToggleButton        *toggle,
76                                                 gpointer                user_data);
77
78
79 /****************************************************************************/
80 /* Boilerplate Object stuff.                                                */
81 /****************************************************************************/
82 G_DEFINE_TYPE (glRotateLabelButton, gl_rotate_label_button, GTK_TYPE_HBOX);
83
84
85 static void
86 gl_rotate_label_button_class_init (glRotateLabelButtonClass *class)
87 {
88         GObjectClass *object_class = G_OBJECT_CLASS (class);
89
90         gl_rotate_label_button_parent_class = g_type_class_peek_parent (class);
91
92         object_class->finalize = gl_rotate_label_button_finalize;
93
94         rotate_label_button_signals[CHANGED] =
95             g_signal_new ("changed",
96                           G_OBJECT_CLASS_TYPE(object_class),
97                           G_SIGNAL_RUN_LAST,
98                           G_STRUCT_OFFSET (glRotateLabelButtonClass, changed),
99                           NULL, NULL,
100                           gl_marshal_VOID__VOID,
101                           G_TYPE_NONE, 0);
102
103 }
104
105
106 static void
107 gl_rotate_label_button_init (glRotateLabelButton *this)
108 {
109         GtkWidget *vbox;
110         GtkWidget *label;
111
112         this->priv = g_new0 (glRotateLabelButtonPrivate, 1);
113
114         gtk_container_set_border_width (GTK_CONTAINER (this), GL_HIG_PAD2);
115
116         this->priv->no_rotate_radio = gtk_radio_button_new (NULL);
117         gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (this->priv->no_rotate_radio),
118                                     FALSE);
119         vbox = gtk_vbox_new (FALSE, 1);
120         this->priv->no_rotate_preview = gl_mini_label_preview_new (SIZE, SIZE);
121         gtk_box_pack_start (GTK_BOX (vbox), this->priv->no_rotate_preview, FALSE, FALSE, 0);
122         label = gtk_label_new (_("Normal"));
123         gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
124         gtk_container_add (GTK_CONTAINER (this->priv->no_rotate_radio), vbox);
125
126
127         this->priv->rotate_radio    = gtk_radio_button_new_from_widget (GTK_RADIO_BUTTON (this->priv->no_rotate_radio));
128         gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (this->priv->rotate_radio),
129                                     FALSE);
130         vbox = gtk_vbox_new (FALSE, 1);
131         this->priv->rotate_preview    = gl_mini_label_preview_new (SIZE, SIZE);
132         gtk_box_pack_start (GTK_BOX (vbox), this->priv->rotate_preview, FALSE, FALSE, 0);
133         label = gtk_label_new (_("Rotated"));
134         gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
135         gtk_container_add (GTK_CONTAINER (this->priv->rotate_radio), vbox);
136
137         gtk_box_pack_start (GTK_BOX (this),
138                             this->priv->no_rotate_radio,
139                             FALSE, FALSE, GL_HIG_PAD1);
140         gtk_box_pack_start (GTK_BOX (this),
141                             this->priv->rotate_radio,
142                             FALSE, FALSE, GL_HIG_PAD1);
143         
144         /* Connect signals to controls */
145         g_signal_connect (G_OBJECT (this->priv->no_rotate_radio),
146                           "toggled",
147                           G_CALLBACK (toggled_cb), this);
148         g_signal_connect (G_OBJECT (this->priv->rotate_radio),
149                           "toggled",
150                           G_CALLBACK (toggled_cb), this);
151 }
152
153
154 static void
155 gl_rotate_label_button_finalize (GObject *object)
156 {
157         glRotateLabelButton      *this = GL_ROTATE_LABEL_BUTTON (object);
158
159         g_return_if_fail (object != NULL);
160         g_return_if_fail (GL_IS_ROTATE_LABEL_BUTTON (object));
161
162         g_free (this->priv);
163
164         G_OBJECT_CLASS (gl_rotate_label_button_parent_class)->finalize (object);
165 }
166
167
168 GtkWidget *
169 gl_rotate_label_button_new (void)
170 {
171         glRotateLabelButton *this;
172
173         this = g_object_new (gl_rotate_label_button_get_type (), NULL);
174
175         return GTK_WIDGET (this);
176 }
177
178
179 /*--------------------------------------------------------------------------*/
180 /* PRIVATE.  modify widget due to change of check button                    */
181 /*--------------------------------------------------------------------------*/
182 static void
183 toggled_cb (GtkToggleButton *toggle,
184             gpointer         user_data)
185 {
186
187         /* Emit our "changed" signal */
188         g_signal_emit (G_OBJECT (user_data),
189                        rotate_label_button_signals[CHANGED], 0);
190
191 }
192
193
194 /****************************************************************************/
195 /* query state of widget.                                                   */
196 /****************************************************************************/
197 gboolean
198 gl_rotate_label_button_get_state (glRotateLabelButton *this)
199 {
200         return
201             gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
202                                           (this->priv->rotate_radio));
203 }
204
205 /****************************************************************************/
206 /* set state of widget.                                                     */
207 /****************************************************************************/
208 void
209 gl_rotate_label_button_set_state (glRotateLabelButton *this,
210                                   gboolean             state)
211 {
212         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
213                                       (this->priv->rotate_radio), state);
214 }
215
216 /****************************************************************************/
217 /* set template for widget.                                                 */
218 /****************************************************************************/
219 void
220 gl_rotate_label_button_set_template_name (glRotateLabelButton *this,
221                                           gchar               *name)
222 {
223         lglTemplate               *template;
224         const lglTemplateFrame    *frame;
225         gdouble                    raw_w, raw_h;
226
227         if (name == NULL)
228         {
229                 gtk_widget_set_sensitive (this->priv->no_rotate_radio, FALSE);
230                 gtk_widget_set_sensitive (this->priv->rotate_radio, FALSE);
231         }
232         else
233         {
234                 template = lgl_db_lookup_template_from_name (name);
235                 frame = (lglTemplateFrame *)template->frames->data;
236
237                 gl_mini_label_preview_set_by_name (GL_MINI_LABEL_PREVIEW (this->priv->no_rotate_preview),
238                                                    name, FALSE);
239                 gl_mini_label_preview_set_by_name (GL_MINI_LABEL_PREVIEW (this->priv->rotate_preview),
240                                                    name, TRUE);
241
242                 lgl_template_frame_get_size (frame, &raw_w, &raw_h);
243                 gtk_widget_set_sensitive (this->priv->no_rotate_radio,
244                                           (raw_w != raw_h));
245                 gtk_widget_set_sensitive (this->priv->rotate_radio,
246                                           (raw_w != raw_h));
247
248                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
249                                               (this->priv->no_rotate_radio), TRUE);
250                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
251                                               (this->priv->rotate_radio), FALSE);
252         }
253
254 }
255
256
257
258 /*
259  * Local Variables:       -- emacs
260  * mode: C                -- emacs
261  * c-basic-offset: 8      -- emacs
262  * tab-width: 8           -- emacs
263  * indent-tabs-mode: nil  -- emacs
264  * End:                   -- emacs
265  */