]> git.sur5r.net Git - glabels/blob - glabels2/src/hig.c
When creating origins for labels, y is now sorted in reverse.
[glabels] / glabels2 / src / hig.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  hig.c:  HIG inspired dialog and layout tools
5  *
6  *  Copyright (C) 2001  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 <glib.h>
26
27 #include "hig.h"
28
29 /*========================================================*/
30 /* Private macros and constants.                          */
31 /*========================================================*/
32 #define HIG_DIALOG_BORDER        12
33 #define HIG_DIALOG_VBOX_SPACING  18
34 #define HIG_DIALOG_ACTION_BORDER  0
35
36 #define HIG_GENERAL_SPACING       6
37
38 /*===========================================*/
39 /* Private globals                           */
40 /*===========================================*/
41 static GtkDialogClass *hig_dialog_parent_class;
42 static GtkVBoxClass   *hig_category_parent_class;
43
44
45 /*===========================================*/
46 /* Local function prototypes                 */
47 /*===========================================*/
48
49 static void       gl_hig_dialog_class_init   (glHigDialogClass *class);
50 static void       gl_hig_dialog_init         (glHigDialog *hig_dialog);
51 static void       gl_hig_dialog_finalize     (GObject *object);
52
53 static void       add_buttons_valist         (glHigDialog *dialog,
54                                               const gchar *first_button_text,
55                                               va_list      args);
56
57 static void       gl_hig_category_class_init (glHigCategoryClass *class);
58 static void       gl_hig_category_init       (glHigCategory *hig_category);
59 static void       gl_hig_category_finalize   (GObject *object);
60
61 \f
62 /****************************************************************************/
63 /* Boilerplate Dialog Object stuff.                                         */
64 /****************************************************************************/
65 guint
66 gl_hig_dialog_get_type (void)
67 {
68         static guint hig_dialog_type = 0;
69
70         if (!hig_dialog_type) {
71                 GTypeInfo hig_dialog_info = {
72                         sizeof (glHigDialogClass),
73                         NULL,
74                         NULL,
75                         (GClassInitFunc) gl_hig_dialog_class_init,
76                         NULL,
77                         NULL,
78                         sizeof (glHigDialog),
79                         0,
80                         (GInstanceInitFunc) gl_hig_dialog_init,
81                 };
82
83                 hig_dialog_type =
84                     g_type_register_static (gtk_dialog_get_type (),
85                                             "glHigDialog",
86                                             &hig_dialog_info, 0);
87         }
88
89         return hig_dialog_type;
90 }
91
92 static void
93 gl_hig_dialog_class_init (glHigDialogClass *class)
94 {
95         GObjectClass *object_class = (GObjectClass *) class;
96
97         hig_dialog_parent_class = g_type_class_peek_parent (class);
98
99         object_class->finalize = gl_hig_dialog_finalize;
100 }
101
102 static void
103 gl_hig_dialog_init (glHigDialog *hig_dialog)
104 {
105         gtk_container_set_border_width (GTK_CONTAINER(hig_dialog),
106                                         HIG_DIALOG_BORDER);
107
108         hig_dialog->vbox = gtk_vbox_new (FALSE, HIG_DIALOG_VBOX_SPACING);
109         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(hig_dialog)->vbox),
110                             hig_dialog->vbox, FALSE, FALSE, 0);
111         gtk_box_set_spacing (GTK_BOX(GTK_DIALOG(hig_dialog)->vbox),
112                              HIG_DIALOG_VBOX_SPACING);
113 }
114
115 static void
116 gl_hig_dialog_finalize (GObject *object)
117 {
118         glHigDialog *hig_dialog;
119
120         g_return_if_fail (object != NULL);
121         g_return_if_fail (GL_IS_HIG_DIALOG (object));
122
123         hig_dialog = GL_HIG_DIALOG (object);
124
125         G_OBJECT_CLASS (hig_dialog_parent_class)->finalize (object);
126 }
127
128
129 /****************************************************************************/
130 /* Create a dialog that attempts to be HIG compliant.                       */
131 /****************************************************************************/
132 GtkWidget* gl_hig_dialog_new (void)
133 {
134         GtkWidget    *dialog;
135
136         dialog = g_object_new (gl_hig_dialog_get_type (), NULL);
137
138         return dialog;
139 }
140
141
142
143 /****************************************************************************/
144 /* Create a dialog that attempts to be HIG compliant with buttons.          */
145 /****************************************************************************/
146 GtkWidget *gl_hig_dialog_new_with_buttons (const gchar    *title,
147                                            GtkWindow      *parent,
148                                            GtkDialogFlags  flags,
149                                            const gchar    *first_button_text,
150                                            ...)
151 {
152         GtkWidget    *dialog;
153         va_list       args;
154   
155         /* Create bare dialog */
156         dialog = g_object_new (gl_hig_dialog_get_type (), NULL);
157
158         /* Title */
159         gtk_window_set_title (GTK_WINDOW(dialog), title);
160
161         /* Parent */
162         gtk_window_set_transient_for (GTK_WINDOW(dialog), parent);
163
164         /* Flags */
165         if ( flags & GTK_DIALOG_MODAL ) {
166                 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
167         }
168         if ( flags & GTK_DIALOG_DESTROY_WITH_PARENT ) {
169                 gtk_window_set_destroy_with_parent (GTK_WINDOW(dialog), TRUE);
170         }
171
172         /* Buttons */
173         va_start (args, first_button_text);
174         add_buttons_valist (GL_HIG_DIALOG(dialog), first_button_text, args);
175         va_end (args);
176
177
178         return dialog;
179 }
180
181 /*---------------------------------------------------------------------------*/
182 /* PRIVATE.  Add buttons to dialog from va_list.                             */
183 /*---------------------------------------------------------------------------*/
184 static void
185 add_buttons_valist(glHigDialog    *dialog,
186                    const gchar    *first_button_text,
187                    va_list         args)
188 {
189   const gchar* text;
190   gint response_id;
191
192   g_return_if_fail (GL_IS_HIG_DIALOG (dialog));
193   
194   if (first_button_text == NULL)
195     return;
196   
197   text = first_button_text;
198   response_id = va_arg (args, gint);
199
200   while (text != NULL)
201     {
202       gtk_dialog_add_button (GTK_DIALOG(dialog), text, response_id);
203
204       text = va_arg (args, gchar*);
205       if (text == NULL)
206         break;
207       response_id = va_arg (args, int);
208     }
209 }
210
211 \f
212 /****************************************************************************/
213 /* Boilerplate Category Object stuff.                                       */
214 /****************************************************************************/
215 guint
216 gl_hig_category_get_type (void)
217 {
218         static guint hig_category_type = 0;
219
220         if (!hig_category_type) {
221                 GTypeInfo hig_category_info = {
222                         sizeof (glHigCategoryClass),
223                         NULL,
224                         NULL,
225                         (GClassInitFunc) gl_hig_category_class_init,
226                         NULL,
227                         NULL,
228                         sizeof (glHigCategory),
229                         0,
230                         (GInstanceInitFunc) gl_hig_category_init,
231                 };
232
233                 hig_category_type =
234                     g_type_register_static (gtk_vbox_get_type (),
235                                             "glHigCategory",
236                                             &hig_category_info, 0);
237         }
238
239         return hig_category_type;
240 }
241
242 static void
243 gl_hig_category_class_init (glHigCategoryClass *class)
244 {
245         GObjectClass *object_class = (GObjectClass *) class;
246
247         hig_category_parent_class = g_type_class_peek_parent (class);
248
249         object_class->finalize = gl_hig_category_finalize;
250 }
251
252 static void
253 gl_hig_category_init (glHigCategory *hig_category)
254 {
255         GtkWidget *hbox;
256
257         gtk_box_set_spacing (GTK_BOX(hig_category), HIG_GENERAL_SPACING);
258
259         /* 1st row: Label */
260         hig_category->label = gtk_label_new ("");
261         gtk_label_set_use_markup (GTK_LABEL(hig_category->label), TRUE);
262         gtk_misc_set_alignment (GTK_MISC(hig_category->label), 0.0, 0.0);
263         gtk_box_pack_start (GTK_BOX(hig_category),
264                             hig_category->label, FALSE, FALSE, 0);
265         
266         /* 2nd row: HBOX */
267         hbox = gtk_hbox_new (FALSE, HIG_GENERAL_SPACING);
268         gtk_box_pack_start (GTK_BOX(hig_category), hbox, FALSE, FALSE, 0);
269
270         /* 2nd row, Column 1: Indentation spacing */
271         gtk_box_pack_start (GTK_BOX(hbox),
272                             gtk_label_new ("    "), FALSE, FALSE, 0);
273
274         /* 2nd row, Column 2: User area (inner vbox) */
275         hig_category->vbox = gtk_vbox_new (FALSE, HIG_GENERAL_SPACING);
276         gtk_box_pack_start (GTK_BOX(hbox),
277                             hig_category->vbox, FALSE, FALSE, 0);
278 }
279
280 static void
281 gl_hig_category_finalize (GObject *object)
282 {
283         glHigCategory *hig_category;
284
285         g_return_if_fail (object != NULL);
286         g_return_if_fail (GL_IS_HIG_CATEGORY (object));
287
288         hig_category = GL_HIG_CATEGORY (object);
289
290         G_OBJECT_CLASS (hig_category_parent_class)->finalize (object);
291 }
292
293
294 /****************************************************************************/
295 /* Create a category layout container that attempts to be HIG compliant.    */
296 /****************************************************************************/
297 GtkWidget* gl_hig_category_new (const gchar *header)
298 {
299         GtkWidget    *category;
300         gchar        *marked_up_header;
301
302         category = g_object_new (gl_hig_category_get_type (), NULL);
303
304         marked_up_header = g_strdup_printf ("<span weight=\"bold\">%s</span>",
305                                             header);
306         gtk_label_set_text (GTK_LABEL(GL_HIG_CATEGORY(category)->label),
307                             marked_up_header);
308         g_free (marked_up_header);
309
310         gtk_label_set_use_markup (GTK_LABEL(GL_HIG_CATEGORY(category)->label),
311                                   TRUE);
312
313         return category;
314 }
315