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