]> git.sur5r.net Git - glabels/blob - glabels2/src/wdgt-line.c
Initial revision
[glabels] / glabels2 / src / wdgt-line.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  wdgt_line.c:  line properties widget module
5  *
6  *  Copyright (C) 2001-2002  Jim Evins <evins@snaught.com>.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22
23 #include <config.h>
24
25 #include "wdgt-line.h"
26 #include "marshal.h"
27
28 #include "debug.h"
29
30 #define RED(x)   ( ((x)>>24) & 0xff )
31 #define GREEN(x) ( ((x)>>16) & 0xff )
32 #define BLUE(x)  ( ((x)>>8)  & 0xff )
33 #define ALPHA(x) (  (x)      & 0xff )
34
35 /*===========================================*/
36 /* Private types                             */
37 /*===========================================*/
38
39 enum {
40         CHANGED,
41         LAST_SIGNAL
42 };
43
44 typedef void (*glWdgtLineSignal) (GObject * object, gpointer data);
45
46 /*===========================================*/
47 /* Private globals                           */
48 /*===========================================*/
49
50 static GObjectClass *parent_class;
51
52 static gint wdgt_line_signals[LAST_SIGNAL] = { 0 };
53
54 /*===========================================*/
55 /* Local function prototypes                 */
56 /*===========================================*/
57
58 static void gl_wdgt_line_class_init (glWdgtLineClass * class);
59 static void gl_wdgt_line_instance_init (glWdgtLine * line);
60 static void gl_wdgt_line_finalize (GObject * object);
61 static void gl_wdgt_line_construct (glWdgtLine * line, gchar * label);
62 static void changed_cb (glWdgtLine * line);
63 \f
64 /*================================================================*/
65 /* Boilerplate Object stuff.                                      */
66 /*================================================================*/
67 guint
68 gl_wdgt_line_get_type (void)
69 {
70         static guint wdgt_line_type = 0;
71
72         if (!wdgt_line_type) {
73                 GTypeInfo wdgt_line_info = {
74                         sizeof (glWdgtLineClass),
75                         NULL,
76                         NULL,
77                         (GClassInitFunc) gl_wdgt_line_class_init,
78                         NULL,
79                         NULL,
80                         sizeof (glWdgtLine),
81                         0,
82                         (GInstanceInitFunc) gl_wdgt_line_instance_init,
83                 };
84
85                 wdgt_line_type =
86                     g_type_register_static (gtk_vbox_get_type (),
87                                             "glWdgtLine",
88                                             &wdgt_line_info, 0);
89         }
90
91         return wdgt_line_type;
92 }
93
94 static void
95 gl_wdgt_line_class_init (glWdgtLineClass * class)
96 {
97         GObjectClass *object_class;
98
99         object_class = (GObjectClass *) class;
100
101         parent_class = gtk_type_class (gtk_vbox_get_type ());
102
103         object_class->finalize = gl_wdgt_line_finalize;
104
105         wdgt_line_signals[CHANGED] =
106             g_signal_new ("changed",
107                           G_OBJECT_CLASS_TYPE(object_class),
108                           G_SIGNAL_RUN_LAST,
109                           G_STRUCT_OFFSET (glWdgtLineClass, changed),
110                           NULL, NULL,
111                           gl_marshal_VOID__VOID,
112                           G_TYPE_NONE, 0);
113
114 }
115
116 static void
117 gl_wdgt_line_instance_init (glWdgtLine * line)
118 {
119         line->width_spin = NULL;
120         line->color_picker = NULL;
121         line->units_label = NULL;
122 }
123
124 static void
125 gl_wdgt_line_finalize (GObject * object)
126 {
127         glWdgtLine *line;
128         glWdgtLineClass *class;
129
130         g_return_if_fail (object != NULL);
131         g_return_if_fail (GL_IS_WDGT_LINE (object));
132
133         line = GL_WDGT_LINE (object);
134
135         G_OBJECT_CLASS (parent_class)->finalize (object);
136 }
137
138 GtkWidget *
139 gl_wdgt_line_new (gchar * label)
140 {
141         glWdgtLine *line;
142
143         line = g_object_new (gl_wdgt_line_get_type (), NULL);
144
145         gl_wdgt_line_construct (line, label);
146
147         return GTK_WIDGET (line);
148 }
149 \f
150 /*============================================================*/
151 /* Construct composite widget.                                */
152 /*============================================================*/
153 static void
154 gl_wdgt_line_construct (glWdgtLine * line,
155                         gchar * label)
156 {
157         GtkWidget *wvbox, *wframe, *wtable, *wlabel;
158         GtkObject *adjust;
159
160         wvbox = GTK_WIDGET (line);
161
162         wframe = gtk_frame_new (label);
163         gtk_box_pack_start (GTK_BOX (wvbox), wframe, FALSE, FALSE, 0);
164
165         wtable = gtk_table_new (2, 3, TRUE);
166         gtk_container_set_border_width (GTK_CONTAINER (wtable), 10);
167         gtk_table_set_row_spacings (GTK_TABLE (wtable), 5);
168         gtk_table_set_col_spacings (GTK_TABLE (wtable), 5);
169         gtk_container_add (GTK_CONTAINER (wframe), wtable);
170
171         /* Line Width Label */
172         wlabel = gtk_label_new (_("Width:"));
173         gtk_misc_set_alignment (GTK_MISC (wlabel), 0, 0.5);
174         gtk_label_set_justify (GTK_LABEL (wlabel), GTK_JUSTIFY_RIGHT);
175         gtk_table_attach_defaults (GTK_TABLE (wtable), wlabel, 0, 1, 0, 1);
176         /* Line Width widget */
177         adjust = gtk_adjustment_new (1.0, 0.25, 4.0, 0.25, 1.0, 1.0);
178         line->width_spin =
179             gtk_spin_button_new (GTK_ADJUSTMENT (adjust), 0.25, 2);
180         g_signal_connect_swapped (G_OBJECT (line->width_spin), "changed",
181                                   G_CALLBACK (changed_cb),
182                                   G_OBJECT (line));
183         gtk_table_attach_defaults (GTK_TABLE (wtable), line->width_spin, 1, 2,
184                                    0, 1);
185         /* Line Width units */
186         line->units_label = gtk_label_new (_("points"));
187         gtk_misc_set_alignment (GTK_MISC (line->units_label), 0, 0.5);
188         gtk_table_attach_defaults (GTK_TABLE (wtable), line->units_label,
189                                    2, 3, 0, 1);
190
191         /* Line Color Label */
192         wlabel = gtk_label_new (_("Color:"));
193         gtk_misc_set_alignment (GTK_MISC (wlabel), 0, 0.5);
194         gtk_table_attach_defaults (GTK_TABLE (wtable), wlabel, 0, 1, 1, 2);
195         /* Line Color picker widget */
196         line->color_picker = gnome_color_picker_new ();
197         g_signal_connect_swapped (G_OBJECT (line->color_picker), "color_set",
198                                   G_CALLBACK (changed_cb),
199                                   G_OBJECT (line));
200         gtk_table_attach_defaults (GTK_TABLE (wtable), line->color_picker, 1, 3,
201                                    1, 2);
202
203 }
204
205 /*--------------------------------------------------------------------------*/
206 /* PRIVATE.  Callback for when any control in the widget has changed.       */
207 /*--------------------------------------------------------------------------*/
208 static void
209 changed_cb (glWdgtLine * line)
210 {
211         /* Emit our "changed" signal */
212         g_signal_emit (G_OBJECT (line), wdgt_line_signals[CHANGED], 0);
213 }
214 \f
215 /*====================================================================*/
216 /* query values from controls.                                        */
217 /*====================================================================*/
218 void
219 gl_wdgt_line_get_params (glWdgtLine * line,
220                          gdouble * width,
221                          guint * color)
222 {
223         guint8 r, g, b, a;
224
225         *width =
226             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
227                                               (line->width_spin));
228
229         gnome_color_picker_get_i8 (GNOME_COLOR_PICKER (line->color_picker),
230                                    &r, &g, &b, &a);
231         *color = GNOME_CANVAS_COLOR_A (r, g, b, a);
232 }
233
234 /*====================================================================*/
235 /* fill in values and ranges for controls.                            */
236 /*====================================================================*/
237 void
238 gl_wdgt_line_set_params (glWdgtLine * line,
239                          gdouble width,
240                          guint color)
241 {
242         gtk_spin_button_set_value (GTK_SPIN_BUTTON (line->width_spin), width);
243
244         gnome_color_picker_set_i8 (GNOME_COLOR_PICKER (line->color_picker),
245                                    RED (color), GREEN (color), BLUE (color),
246                                    ALPHA (color));
247 }