]> git.sur5r.net Git - glabels/blob - glabels2/src/window.c
Initial status bar code -- only the zoom_info area implemented.
[glabels] / glabels2 / src / window.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  window.c:  a gLabels app window
5  *
6  *  Copyright (C) 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 "ui.h"
26 #include "window.h"
27 #include "util.h"
28 #include "xml-label.h"
29
30 #include "debug.h"
31
32 /*============================================================================*/
33 /* Private macros and constants.                                              */
34 /*============================================================================*/
35
36 #define DEFAULT_WINDOW_WIDTH  500
37 #define DEFAULT_WINDOW_HEIGHT 375
38
39 #define CURSOR_INFO_WIDTH     150
40 #define ZOOM_INFO_WIDTH        50
41
42 /*============================================================================*/
43 /* Private globals                                                            */
44 /*============================================================================*/
45 static BonoboWindowClass *parent_class;
46
47 static GList *window_list = NULL;
48
49
50 /*============================================================================*/
51 /* Local function prototypes                                                  */
52 /*============================================================================*/
53
54 static void     gl_window_class_init   (glWindowClass *class);
55 static void     gl_window_init         (glWindow      *window);
56 static void     gl_window_finalize     (GObject       *object);
57 static void     gl_window_destroy      (GtkObject     *gtk_object);
58
59 static void     set_window_title       (glWindow *window,
60                                         glLabel  *label);
61
62 static gboolean window_delete_event_cb (glWindow      *window,
63                                         GdkEvent      *event,
64                                         gpointer       user_data);
65
66 static void     selection_changed_cb   (glView        *view,
67                                         glWindow      *window);
68
69 static void     zoom_changed_cb        (glView   *view,
70                                         gdouble  zoom,
71                                         glWindow *window);
72
73 static void     name_changed_cb        (glLabel       *label,
74                                         glWindow      *window);
75
76 static void     modified_changed_cb    (glLabel       *label,
77                                         glWindow      *window);
78
79 \f
80 /****************************************************************************/
81 /* Boilerplate Object stuff.                                                */
82 /****************************************************************************/
83 guint
84 gl_window_get_type (void)
85 {
86         static guint window_type = 0;
87
88         if (!window_type) {
89                 GTypeInfo window_info = {
90                         sizeof (glWindowClass),
91                         NULL,
92                         NULL,
93                         (GClassInitFunc) gl_window_class_init,
94                         NULL,
95                         NULL,
96                         sizeof (glWindow),
97                         0,
98                         (GInstanceInitFunc) gl_window_init,
99                 };
100
101                 window_type =
102                     g_type_register_static (bonobo_window_get_type (),
103                                             "glWindow",
104                                             &window_info, 0);
105         }
106
107         return window_type;
108 }
109
110 static void
111 gl_window_class_init (glWindowClass *class)
112 {
113         GObjectClass   *object_class     = (GObjectClass *) class;
114         GtkObjectClass *gtk_object_class = (GtkObjectClass *) class;
115
116         gl_debug (DEBUG_WINDOW, "START");
117
118         parent_class = g_type_class_peek_parent (class);
119
120         object_class->finalize = gl_window_finalize;
121
122         gtk_object_class->destroy = gl_window_destroy;
123
124         gl_debug (DEBUG_WINDOW, "END");
125 }
126
127 static void
128 gl_window_init (glWindow *window)
129 {
130         BonoboUIContainer *ui_container;
131         BonoboUIComponent *ui_component;
132
133         gl_debug (DEBUG_WINDOW, "START");
134
135         ui_container = bonobo_window_get_ui_container(BONOBO_WINDOW(window));
136         ui_component = bonobo_ui_component_new_default ();
137         bonobo_ui_component_set_container (ui_component,
138                                            BONOBO_OBJREF (ui_container),
139                                            NULL);
140
141         window->cursor_info = gtk_label_new (NULL);
142         gtk_widget_set_size_request (window->cursor_info, CURSOR_INFO_WIDTH, -1);
143         window->cursor_info_frame = gtk_frame_new (NULL);
144         gtk_frame_set_shadow_type (GTK_FRAME(window->cursor_info_frame), GTK_SHADOW_IN);
145         gtk_container_add (GTK_CONTAINER(window->cursor_info_frame), window->cursor_info);
146         gtk_widget_show_all (window->cursor_info_frame);
147
148         window->zoom_info = gtk_label_new (NULL);
149         gtk_widget_set_size_request (window->zoom_info, ZOOM_INFO_WIDTH, -1);
150         window->zoom_info_frame = gtk_frame_new (NULL);
151         gtk_frame_set_shadow_type (GTK_FRAME(window->zoom_info_frame), GTK_SHADOW_IN);
152         gtk_container_add (GTK_CONTAINER(window->zoom_info_frame), window->zoom_info);
153         gtk_widget_show_all (window->zoom_info_frame);
154
155         gl_ui_init (ui_component,
156                     BONOBO_WINDOW (window),
157                     window->cursor_info_frame,
158                     window->zoom_info_frame);
159
160         gtk_window_set_default_size (GTK_WINDOW (window),
161                                      DEFAULT_WINDOW_WIDTH,
162                                      DEFAULT_WINDOW_HEIGHT);
163
164         g_signal_connect (G_OBJECT(window), "delete-event",
165                           G_CALLBACK(window_delete_event_cb), NULL);
166         
167         window->uic  = ui_component;
168         window->view = NULL;
169
170         window_list = g_list_append (window_list, window);
171
172         gl_debug (DEBUG_WINDOW, "END");
173 }
174
175 static void
176 gl_window_finalize (GObject *object)
177 {
178         glWindow *window;
179
180         gl_debug (DEBUG_WINDOW, "START");
181
182         g_return_if_fail (object != NULL);
183         g_return_if_fail (GL_IS_WINDOW (object));
184
185         window = GL_WINDOW (object);
186
187         G_OBJECT_CLASS (parent_class)->finalize (object);
188
189         gl_debug (DEBUG_WINDOW, "END");
190 }
191
192 static void
193 gl_window_destroy (GtkObject *gtk_object)
194 {
195         glWindow *window;
196
197         gl_debug (DEBUG_WINDOW, "START");
198
199         g_return_if_fail (gtk_object != NULL);
200         g_return_if_fail (GL_IS_WINDOW (gtk_object));
201
202         window = GL_WINDOW (gtk_object);
203         window_list = g_list_remove (window_list, window);
204
205         if (GTK_OBJECT_CLASS (parent_class)->destroy) {
206                 GTK_OBJECT_CLASS (parent_class)->destroy (gtk_object);
207         }
208
209         gl_debug (DEBUG_WINDOW, "END");
210 }
211
212
213 /****************************************************************************/
214 /* Create an app window.                                                    */
215 /****************************************************************************/
216 GtkWidget *
217 gl_window_new (void)
218 {
219         glWindow *window;
220
221         gl_debug (DEBUG_WINDOW, "START");
222
223         window = g_object_new (gl_window_get_type (),
224                                "win_name", "glabels",
225                                "title",    _("(none) - glabels"),
226                                NULL);
227
228         gl_debug (DEBUG_WINDOW, "window=%p", window);
229         gl_debug (DEBUG_WINDOW, "view=%p", window->view);
230
231         gl_debug (DEBUG_WINDOW, "END");
232
233         return GTK_WIDGET(window);
234 }
235
236 /****************************************************************************/
237 /* Create an app window from a label.                                       */
238 /****************************************************************************/
239 GtkWidget*
240 gl_window_new_from_label (glLabel *label)
241 {
242         glWindow *window;
243
244         gl_debug (DEBUG_WINDOW, "START");
245
246         window = GL_WINDOW (gl_window_new ());
247
248         gl_window_set_label (window, label);
249
250         gl_debug (DEBUG_WINDOW, "END");
251
252         return GTK_WIDGET(window);
253 }
254
255 /****************************************************************************/
256 /* Create an app window from a file.                                        */
257 /****************************************************************************/
258 GtkWidget*
259 gl_window_new_from_file (const gchar *filename)
260 {
261         glWindow         *window;
262         glLabel          *label;
263         gchar            *abs_filename;
264         glXMLLabelStatus  status;
265
266         gl_debug (DEBUG_WINDOW, "START");
267
268         window = GL_WINDOW (gl_window_new ());
269
270         abs_filename = gl_util_make_absolute (filename);
271         label = gl_xml_label_open (filename, &status);
272         g_free (abs_filename);
273
274         gl_window_set_label (window, label);
275
276         gl_debug (DEBUG_WINDOW, "END");
277
278         return GTK_WIDGET(window);
279 }
280
281 /****************************************************************************/
282 /* Is window empty?                                                         */
283 /****************************************************************************/
284 gboolean
285 gl_window_is_empty (glWindow    *window)
286 {
287         g_return_val_if_fail (GL_IS_WINDOW (window), FALSE);
288
289         gl_debug (DEBUG_WINDOW, "return %d", (window->view == NULL) );
290         return ( window->view == NULL );
291 }
292
293 /****************************************************************************/
294 /* Create view from label and place in window.                              */
295 /****************************************************************************/
296 void
297 gl_window_set_label (glWindow    *window,
298                      glLabel     *label)
299 {
300         gchar *string;
301
302         gl_debug (DEBUG_WINDOW, "START");
303
304         g_return_if_fail (GL_IS_WINDOW (window));
305         g_return_if_fail (GL_IS_LABEL (label));
306
307         gl_label_clear_modified (label);
308
309         set_window_title (window, label);
310
311         if ( window->view != NULL ) {
312                 gtk_widget_destroy (window->view);
313                 window->view = NULL;
314         }
315
316         window->view = gl_view_new (label);
317         bonobo_window_set_contents (BONOBO_WINDOW(window), window->view);
318
319         gtk_widget_show_all (window->view);
320
321         gl_ui_update_all (window->uic, GL_VIEW(window->view));
322
323         string = g_strdup_printf ("%3.0f%%",
324                                   100.0*gl_view_get_zoom (GL_VIEW(window->view)));
325         gtk_label_set_text (GTK_LABEL(window->zoom_info), string);
326         g_free (string);
327
328         g_signal_connect (G_OBJECT(window->view), "selection_changed",
329                           G_CALLBACK(selection_changed_cb), window);
330
331         g_signal_connect (G_OBJECT(window->view), "zoom_changed",
332                           G_CALLBACK(zoom_changed_cb), window);
333
334         g_signal_connect (G_OBJECT(label), "name_changed",
335                           G_CALLBACK(name_changed_cb), window);
336
337         g_signal_connect (G_OBJECT(label), "modified_changed",
338                           G_CALLBACK(modified_changed_cb), window);
339
340         gl_debug (DEBUG_WINDOW, "END");
341 }
342
343 /****************************************************************************/
344 /* Return list of app windows.                                              */
345 /****************************************************************************/
346 const GList *
347 gl_window_get_window_list (void)
348 {
349         gl_debug (DEBUG_WINDOW, "");
350         return window_list;
351 }
352
353 /*---------------------------------------------------------------------------*/
354 /* PRIVATE.  Set window title based on name and state of label.              */
355 /*---------------------------------------------------------------------------*/
356 static void 
357 set_window_title (glWindow *window,
358                   glLabel  *label)
359 {
360         gchar *name, *title;
361
362         gl_debug (DEBUG_WINDOW, "START");
363
364         g_return_if_fail (window && GL_IS_WINDOW (window));
365         g_return_if_fail (label && GL_IS_LABEL (label));
366
367         name = gl_label_get_short_name (label);
368         g_return_if_fail (name != NULL);
369
370         if (gl_label_is_modified (label)) {
371                 title = g_strdup_printf ("%s %s - glabels",
372                                          name, _("(modified)"));
373         } else {
374                 title = g_strdup_printf ("%s - glabels", name);
375         }
376
377         gtk_window_set_title (GTK_WINDOW(window), title);
378
379         g_free (name);
380         g_free (title);
381
382         gl_debug (DEBUG_WINDOW, "END");
383 }
384
385 /*-------------------------------------------------------------------------*/
386 /* PRIVATE.  Window "delete-event" callback.                               */
387 /*-------------------------------------------------------------------------*/
388 static gboolean
389 window_delete_event_cb (glWindow      *window,
390                         GdkEvent      *event,
391                         gpointer       user_data)
392 {
393         gl_debug (DEBUG_WINDOW, "START");
394
395         g_return_val_if_fail (window && GL_IS_WINDOW (window), TRUE);
396
397         gl_file_close (window);
398
399         gl_debug (DEBUG_WINDOW, "END");
400
401         return TRUE;
402 }
403
404 /*---------------------------------------------------------------------------*/
405 /* PRIVATE.  View "selection state changed" callback.                        */
406 /*---------------------------------------------------------------------------*/
407 static void 
408 selection_changed_cb (glView   *view,
409                       glWindow *window)
410 {
411         gl_debug (DEBUG_WINDOW, "START");
412
413         g_return_if_fail (view && GL_IS_VIEW (view));
414         g_return_if_fail (window && GL_IS_WINDOW (window));
415
416         gl_ui_update_selection_verbs (window->uic, view);
417
418         gl_debug (DEBUG_WINDOW, "END");
419 }
420
421 /*---------------------------------------------------------------------------*/
422 /* PRIVATE.  View "zoom state changed" callback.                             */
423 /*---------------------------------------------------------------------------*/
424 static void 
425 zoom_changed_cb (glView   *view,
426                  gdouble  zoom,
427                  glWindow *window)
428 {
429         gchar *string;
430
431         gl_debug (DEBUG_WINDOW, "START");
432
433         g_return_if_fail (view && GL_IS_VIEW (view));
434         g_return_if_fail (window && GL_IS_WINDOW (window));
435
436         string = g_strdup_printf ("%3.0f%%", 100.0*zoom);
437         gtk_label_set_text (GTK_LABEL(window->zoom_info), string);
438         g_free (string);
439
440         gl_debug (DEBUG_WINDOW, "END");
441 }
442
443 /*---------------------------------------------------------------------------*/
444 /* PRIVATE.  Label "name changed" callback.                                  */
445 /*---------------------------------------------------------------------------*/
446 static void 
447 name_changed_cb (glLabel  *label,
448                  glWindow *window)
449 {
450         gl_debug (DEBUG_WINDOW, "START");
451
452         g_return_if_fail (label && GL_IS_LABEL (label));
453         g_return_if_fail (window && GL_IS_WINDOW (window));
454
455         set_window_title (window, label);
456
457         gl_debug (DEBUG_WINDOW, "END");
458 }
459
460 /*---------------------------------------------------------------------------*/
461 /* PRIVATE.  Label "modified state changed" callback.                        */
462 /*---------------------------------------------------------------------------*/
463 static void 
464 modified_changed_cb (glLabel  *label,
465                      glWindow *window)
466 {
467         gl_debug (DEBUG_WINDOW, "START");
468
469         g_return_if_fail (label && GL_IS_LABEL (label));
470         g_return_if_fail (window && GL_IS_WINDOW (window));
471
472         set_window_title (window, label);
473
474         gl_ui_update_modified_verbs (window->uic, label);
475
476         gl_debug (DEBUG_WINDOW, "END");
477 }
478