3 * Copyright (C) 2002-2009 Jim Evins <evins@snaught.com>.
5 * This file is part of gLabels.
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.
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.
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/>.
25 #include <glib/gi18n.h>
29 #include "ui-commands.h"
30 #include "file-util.h"
31 #include "xml-label.h"
34 #include "units-util.h"
39 /*===========================================================================*/
40 /* Private macros and constants. */
41 /*===========================================================================*/
43 #define DEFAULT_WINDOW_WIDTH 788
44 #define DEFAULT_WINDOW_HEIGHT 600
46 #define CURSOR_INFO_WIDTH 150
47 #define ZOOM_INFO_WIDTH 50
50 /*===========================================================================*/
52 /*===========================================================================*/
54 static GList *window_list = NULL;
57 /*===========================================================================*/
58 /* Local function prototypes */
59 /*===========================================================================*/
61 static void gl_window_finalize (GObject *object);
62 static void gl_window_destroy (GtkObject *gtk_object);
64 static void set_window_title (glWindow *window,
67 static gboolean window_delete_event_cb (glWindow *window,
71 static void selection_changed_cb (glLabel *label,
74 static void context_menu_activate_cb (glView *view,
76 guint32 activate_time,
79 static void zoom_changed_cb (glView *view,
83 static void pointer_moved_cb (glView *view,
88 static void pointer_exit_cb (glView *view,
91 static void name_changed_cb (glLabel *label,
94 static void modified_changed_cb (glLabel *label,
97 static void clipboard_changed_cb (GtkClipboard *clipboard,
101 static void focus_widget_changed_cb(GtkWindow *gtk_window,
105 static void set_copy_paste_sensitivity (glWindow *window,
106 GtkWidget *focus_widget);
109 /****************************************************************************/
110 /* Boilerplate Object stuff. */
111 /****************************************************************************/
112 G_DEFINE_TYPE (glWindow, gl_window, GTK_TYPE_WINDOW);
116 gl_window_class_init (glWindowClass *class)
118 GObjectClass *object_class = G_OBJECT_CLASS (class);
119 GtkObjectClass *gtk_object_class = GTK_OBJECT_CLASS (class);
121 gl_debug (DEBUG_WINDOW, "START");
123 gl_window_parent_class = g_type_class_peek_parent (class);
125 object_class->finalize = gl_window_finalize;
127 gtk_object_class->destroy = gl_window_destroy;
129 gl_debug (DEBUG_WINDOW, "END");
134 gl_window_init (glWindow *window)
138 GtkWidget *status_hbox;
140 gl_debug (DEBUG_WINDOW, "START");
142 vbox1 = gtk_vbox_new (FALSE, 0);
143 gtk_container_add (GTK_CONTAINER (window), vbox1);
145 window->ui = ui = gl_ui_new (window);
146 gtk_box_pack_start (GTK_BOX (vbox1),
147 gtk_ui_manager_get_widget (ui, "/MenuBar"),
149 gtk_box_pack_start (GTK_BOX (vbox1),
150 gtk_ui_manager_get_widget (ui, "/MainToolBar"),
152 gtk_box_pack_start (GTK_BOX (vbox1),
153 gtk_ui_manager_get_widget (ui, "/DrawingToolBar"),
156 window->hbox = gtk_hbox_new (FALSE, 0);
157 gtk_box_pack_start (GTK_BOX (vbox1), window->hbox, TRUE, TRUE, 0);
159 window->sidebar = GL_UI_SIDEBAR (gl_ui_sidebar_new ());
160 gtk_box_pack_end (GTK_BOX (window->hbox), GTK_WIDGET (window->sidebar), FALSE, FALSE, 0);
162 window->property_bar = GL_UI_PROPERTY_BAR (gl_ui_property_bar_new ());
163 gtk_box_pack_start (GTK_BOX (vbox1), GTK_WIDGET (window->property_bar), FALSE, FALSE, 0);
165 status_hbox = gtk_hbox_new (FALSE, 0);
166 gtk_box_pack_start (GTK_BOX (vbox1), status_hbox, FALSE, FALSE, 0);
168 window->status_bar = gtk_statusbar_new ();
169 gtk_statusbar_set_has_resize_grip (GTK_STATUSBAR (window->status_bar), FALSE);
170 gtk_box_pack_start (GTK_BOX (status_hbox),
173 window->zoom_info = gtk_label_new (NULL);
174 gtk_widget_set_size_request (window->zoom_info, ZOOM_INFO_WIDTH, -1);
175 window->zoom_info_frame = gtk_frame_new (NULL);
176 gtk_frame_set_shadow_type (GTK_FRAME(window->zoom_info_frame), GTK_SHADOW_IN);
177 gtk_container_add (GTK_CONTAINER(window->zoom_info_frame), window->zoom_info);
178 gtk_widget_show_all (window->zoom_info_frame);
179 gtk_box_pack_end (GTK_BOX (status_hbox),
180 window->zoom_info_frame,
183 window->cursor_info = gtk_label_new (NULL);
184 gtk_widget_set_size_request (window->cursor_info, CURSOR_INFO_WIDTH, -1);
185 window->cursor_info_frame = gtk_frame_new (NULL);
186 gtk_frame_set_shadow_type (GTK_FRAME(window->cursor_info_frame), GTK_SHADOW_IN);
187 gtk_container_add (GTK_CONTAINER(window->cursor_info_frame), window->cursor_info);
188 gtk_widget_show_all (window->cursor_info_frame);
189 gtk_box_pack_end (GTK_BOX (status_hbox),
190 window->cursor_info_frame,
193 gtk_window_set_default_size (GTK_WINDOW (window),
194 DEFAULT_WINDOW_WIDTH,
195 DEFAULT_WINDOW_HEIGHT);
197 g_signal_connect (G_OBJECT(window), "delete-event",
198 G_CALLBACK(window_delete_event_cb), NULL);
200 window->menu_tips_context_id =
201 gtk_statusbar_get_context_id (GTK_STATUSBAR (window->status_bar), "menu_tips");
203 window->print_settings = NULL;
204 window->merge_dialog = NULL;
205 window->context_menu = GTK_MENU (gtk_ui_manager_get_widget (ui, "/ContextMenu"));
206 window->empty_selection_context_menu =
207 GTK_MENU (gtk_ui_manager_get_widget (ui, "/EmptySelectionContextMenu"));
211 window_list = g_list_append (window_list, window);
213 gl_debug (DEBUG_WINDOW, "END");
218 gl_window_finalize (GObject *object)
220 gl_debug (DEBUG_WINDOW, "START");
222 g_return_if_fail (object != NULL);
223 g_return_if_fail (GL_IS_WINDOW (object));
225 G_OBJECT_CLASS (gl_window_parent_class)->finalize (object);
227 gl_debug (DEBUG_WINDOW, "END");
232 gl_window_destroy (GtkObject *gtk_object)
235 GtkClipboard *clipboard;
237 gl_debug (DEBUG_WINDOW, "START");
239 g_return_if_fail (gtk_object != NULL);
240 g_return_if_fail (GL_IS_WINDOW (gtk_object));
242 window = GL_WINDOW (gtk_object);
243 window_list = g_list_remove (window_list, window);
246 gl_ui_unref(window->ui);
252 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (window),
253 GDK_SELECTION_CLIPBOARD);
255 g_signal_handlers_disconnect_by_func (G_OBJECT (clipboard),
256 G_CALLBACK (clipboard_changed_cb),
259 g_object_unref (window->label);
262 if (GTK_OBJECT_CLASS (gl_window_parent_class)->destroy) {
263 GTK_OBJECT_CLASS (gl_window_parent_class)->destroy (gtk_object);
266 gl_debug (DEBUG_WINDOW, "END");
270 /****************************************************************************/
271 /** Create a glabels window. */
272 /****************************************************************************/
278 gl_debug (DEBUG_WINDOW, "START");
280 window = g_object_new (GL_TYPE_WINDOW,
281 "title", _("(none) - gLabels"),
284 gl_debug (DEBUG_WINDOW, "window=%p", window);
285 gl_debug (DEBUG_WINDOW, "view=%p", window->view);
287 gl_debug (DEBUG_WINDOW, "END");
289 return GTK_WIDGET(window);
293 /****************************************************************************/
294 /** Create a glabels window from a label. */
295 /****************************************************************************/
297 gl_window_new_from_label (glLabel *label)
301 gl_debug (DEBUG_WINDOW, "START");
303 window = GL_WINDOW (gl_window_new ());
305 gl_window_set_label (window, label);
307 gl_debug (DEBUG_WINDOW, "END");
309 return GTK_WIDGET(window);
313 /****************************************************************************/
314 /** Create a glabels window from a glabels file. */
315 /****************************************************************************/
317 gl_window_new_from_file (const gchar *filename)
322 glXMLLabelStatus status;
324 gl_debug (DEBUG_WINDOW, "START");
326 window = GL_WINDOW (gl_window_new ());
328 abs_filename = gl_file_util_make_absolute (filename);
329 label = gl_xml_label_open (abs_filename, &status);
330 g_free (abs_filename);
332 gl_window_set_label (window, label);
334 gl_debug (DEBUG_WINDOW, "END");
336 return GTK_WIDGET(window);
340 /****************************************************************************/
341 /** Is window empty? */
342 /****************************************************************************/
344 gl_window_is_empty (glWindow *window)
346 g_return_val_if_fail (GL_IS_WINDOW (window), FALSE);
348 gl_debug (DEBUG_WINDOW, "return %d", (window->view == NULL) );
349 return ( window->view == NULL );
353 /****************************************************************************/
354 /** Create view from label and place in window. */
355 /****************************************************************************/
357 gl_window_set_label (glWindow *window,
361 GtkClipboard *clipboard;
362 GtkWidget *focus_widget;
364 gl_debug (DEBUG_WINDOW, "START");
366 g_return_if_fail (GL_IS_WINDOW (window));
367 g_return_if_fail (GL_IS_LABEL (label));
369 window->label = g_object_ref (label);
371 gl_label_clear_modified (label);
373 set_window_title (window, label);
375 if ( window->view != NULL ) {
376 gtk_widget_destroy (window->view);
380 window->view = gl_view_new (label);
381 gtk_box_pack_start (GTK_BOX (window->hbox), window->view, TRUE, TRUE, 0);
383 gtk_widget_show_all (window->view);
385 gl_view_zoom_to_fit (GL_VIEW(window->view));
387 if (gl_prefs_model_get_grid_visible (gl_prefs)) {
388 gl_view_show_grid (GL_VIEW(window->view));
390 gl_view_hide_grid (GL_VIEW(window->view));
393 if (gl_prefs_model_get_markup_visible (gl_prefs)) {
394 gl_view_show_markup (GL_VIEW(window->view));
396 gl_view_hide_markup (GL_VIEW(window->view));
399 gl_ui_update_all (window->ui, GL_VIEW(window->view));
401 gl_ui_property_bar_set_label (window->property_bar, window->label);
402 gl_ui_sidebar_set_label (window->sidebar, window->label);
404 string = g_strdup_printf ("%3.0f%%",
405 100.0*gl_view_get_zoom (GL_VIEW(window->view)));
406 gtk_label_set_text (GTK_LABEL(window->zoom_info), string);
410 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (window),
411 GDK_SELECTION_CLIPBOARD);
414 g_signal_connect (G_OBJECT(window->label), "selection_changed",
415 G_CALLBACK(selection_changed_cb), window);
417 g_signal_connect (G_OBJECT(window->view), "context_menu_activate",
418 G_CALLBACK(context_menu_activate_cb), window);
420 g_signal_connect (G_OBJECT(window->view), "zoom_changed",
421 G_CALLBACK(zoom_changed_cb), window);
423 g_signal_connect (G_OBJECT(window->view), "pointer_moved",
424 G_CALLBACK(pointer_moved_cb), window);
426 g_signal_connect (G_OBJECT(window->view), "pointer_exit",
427 G_CALLBACK(pointer_exit_cb), window);
429 g_signal_connect (G_OBJECT(label), "name_changed",
430 G_CALLBACK(name_changed_cb), window);
432 g_signal_connect (G_OBJECT(label), "modified_changed",
433 G_CALLBACK(modified_changed_cb), window);
435 g_signal_connect (G_OBJECT(clipboard), "owner_change",
436 G_CALLBACK(clipboard_changed_cb), window);
438 g_signal_connect (G_OBJECT(window), "set_focus",
439 G_CALLBACK(focus_widget_changed_cb), window);
441 /* Initialize "Paste" sensitivity. */
442 focus_widget = gtk_window_get_focus (GTK_WINDOW (window));
443 set_copy_paste_sensitivity (window, focus_widget);
445 gl_debug (DEBUG_WINDOW, "END");
449 /****************************************************************************/
450 /** Return list of glabels windows. */
451 /****************************************************************************/
453 gl_window_get_window_list (void)
455 gl_debug (DEBUG_WINDOW, "");
460 /*---------------------------------------------------------------------------*/
461 /** PRIVATE. Set window title based on name and state of label. */
462 /*---------------------------------------------------------------------------*/
464 set_window_title (glWindow *window,
469 gl_debug (DEBUG_WINDOW, "START");
471 g_return_if_fail (window && GL_IS_WINDOW (window));
472 g_return_if_fail (label && GL_IS_LABEL (label));
474 name = gl_label_get_short_name (label);
475 g_return_if_fail (name != NULL);
477 if (gl_label_is_modified (label)) {
478 title = g_strdup_printf ("%s %s - gLabels",
479 name, _("(modified)"));
481 title = g_strdup_printf ("%s - gLabels", name);
484 gtk_window_set_title (GTK_WINDOW(window), title);
489 gl_debug (DEBUG_WINDOW, "END");
493 /*-------------------------------------------------------------------------*/
494 /** PRIVATE. Window "delete-event" callback. */
495 /*-------------------------------------------------------------------------*/
497 window_delete_event_cb (glWindow *window,
501 gl_debug (DEBUG_WINDOW, "START");
503 g_return_val_if_fail (window && GL_IS_WINDOW (window), TRUE);
505 gl_file_close (window);
507 gl_debug (DEBUG_WINDOW, "END");
513 /*---------------------------------------------------------------------------*/
514 /** PRIVATE. View "selection state changed" callback. */
515 /*---------------------------------------------------------------------------*/
517 selection_changed_cb (glLabel *label,
520 gl_debug (DEBUG_WINDOW, "START");
522 g_return_if_fail (label && GL_IS_LABEL (label));
523 g_return_if_fail (window && GL_IS_WINDOW (window));
525 gl_ui_update_selection_verbs (window->ui, GL_VIEW (window->view), TRUE);
527 gl_debug (DEBUG_WINDOW, "END");
531 /*---------------------------------------------------------------------------*/
532 /** PRIVATE. View "context menu activate" callback. */
533 /*---------------------------------------------------------------------------*/
535 context_menu_activate_cb (glView *view,
537 guint32 activate_time,
540 gl_debug (DEBUG_WINDOW, "START");
542 g_return_if_fail (view && GL_IS_VIEW (view));
543 g_return_if_fail (window && GL_IS_WINDOW (window));
545 if (gl_label_is_selection_empty (view->label)) {
547 gtk_menu_popup (GTK_MENU (window->empty_selection_context_menu),
548 NULL, NULL, NULL, NULL, button, activate_time);
552 gtk_menu_popup (GTK_MENU (window->context_menu),
553 NULL, NULL, NULL, NULL, button, activate_time);
557 gl_debug (DEBUG_WINDOW, "END");
561 /*---------------------------------------------------------------------------*/
562 /** PRIVATE. View "zoom state changed" callback. */
563 /*---------------------------------------------------------------------------*/
565 zoom_changed_cb (glView *view,
571 gl_debug (DEBUG_WINDOW, "START");
573 g_return_if_fail (view && GL_IS_VIEW (view));
574 g_return_if_fail (window && GL_IS_WINDOW (window));
576 string = g_strdup_printf ("%3.0f%%", 100.0*zoom);
577 gtk_label_set_text (GTK_LABEL(window->zoom_info), string);
580 gl_ui_update_zoom_verbs (window->ui, view);
582 gl_debug (DEBUG_WINDOW, "END");
586 /*---------------------------------------------------------------------------*/
587 /** PRIVATE. View "pointer moved" callback. */
588 /*---------------------------------------------------------------------------*/
590 pointer_moved_cb (glView *view,
597 gdouble units_per_point;
598 gint units_precision;
600 gl_debug (DEBUG_WINDOW, "START");
602 g_return_if_fail (view && GL_IS_VIEW (view));
603 g_return_if_fail (window && GL_IS_WINDOW (window));
605 units = gl_prefs_model_get_units (gl_prefs);
606 units_per_point = lgl_units_get_units_per_point (units);
607 units_precision = gl_units_util_get_precision (units);
609 string = g_strdup_printf ("%.*f, %.*f",
610 units_precision, x*units_per_point,
611 units_precision, y*units_per_point);
612 gtk_label_set_text (GTK_LABEL(window->cursor_info), string);
615 gl_debug (DEBUG_WINDOW, "END");
619 /*---------------------------------------------------------------------------*/
620 /** PRIVATE. View "pointer exit" callback. */
621 /*---------------------------------------------------------------------------*/
623 pointer_exit_cb (glView *view,
626 gl_debug (DEBUG_WINDOW, "START");
628 g_return_if_fail (view && GL_IS_VIEW (view));
629 g_return_if_fail (window && GL_IS_WINDOW (window));
631 gtk_label_set_text (GTK_LABEL(window->cursor_info), "");
633 gl_debug (DEBUG_WINDOW, "END");
637 /*---------------------------------------------------------------------------*/
638 /** PRIVATE. Label "name changed" callback. */
639 /*---------------------------------------------------------------------------*/
641 name_changed_cb (glLabel *label,
644 gl_debug (DEBUG_WINDOW, "START");
646 g_return_if_fail (label && GL_IS_LABEL (label));
647 g_return_if_fail (window && GL_IS_WINDOW (window));
649 set_window_title (window, label);
651 gl_debug (DEBUG_WINDOW, "END");
655 /*---------------------------------------------------------------------------*/
656 /** PRIVATE. Label "modified state changed" callback. */
657 /*---------------------------------------------------------------------------*/
659 modified_changed_cb (glLabel *label,
662 gl_debug (DEBUG_WINDOW, "START");
664 g_return_if_fail (label && GL_IS_LABEL (label));
665 g_return_if_fail (window && GL_IS_WINDOW (window));
667 set_window_title (window, label);
669 gl_ui_update_modified_verbs (window->ui, label);
671 gl_debug (DEBUG_WINDOW, "END");
675 /*---------------------------------------------------------------------------*/
676 /** PRIVATE. Clipboard "owner change" callback. */
677 /*---------------------------------------------------------------------------*/
679 clipboard_changed_cb (GtkClipboard *clipboard,
683 GtkWidget *focus_widget;
685 gl_debug (DEBUG_WINDOW, "START");
687 g_return_if_fail (window && GL_IS_WINDOW (window));
689 focus_widget = gtk_window_get_focus (GTK_WINDOW (window));
690 set_copy_paste_sensitivity (window, focus_widget);
692 gl_debug (DEBUG_WINDOW, "END");
696 /*---------------------------------------------------------------------------*/
697 /** PRIVATE. Window "set-focus" callback. */
698 /*---------------------------------------------------------------------------*/
700 focus_widget_changed_cb (GtkWindow *gtk_window,
704 gl_debug (DEBUG_WINDOW, "START");
706 g_return_if_fail (window && GL_IS_WINDOW (window));
710 gl_debug (DEBUG_WINDOW, "SET-FOCUS %x %s\n",
712 G_OBJECT_TYPE_NAME (widget));
714 set_copy_paste_sensitivity (window, widget);
717 gl_debug (DEBUG_WINDOW, "END");
721 /*---------------------------------------------------------------------------*/
722 /** PRIVATE. Set paste sensitivity. */
723 /*---------------------------------------------------------------------------*/
725 set_copy_paste_sensitivity (glWindow *window,
726 GtkWidget *focus_widget)
728 gl_debug (DEBUG_WINDOW, "START");
730 g_return_if_fail (window && GL_IS_WINDOW (window));
732 if ( focus_widget == GL_VIEW(window->view)->canvas )
734 gl_ui_update_selection_verbs (window->ui, GL_VIEW (window->view), TRUE);
736 gl_ui_update_paste_verbs (window->ui, gl_label_can_paste (window->label));
740 gl_ui_update_selection_verbs (window->ui, GL_VIEW (window->view), FALSE);
741 gl_ui_update_paste_verbs (window->ui, FALSE);
744 gl_debug (DEBUG_WINDOW, "END");
749 * Local Variables: -- emacs
751 * c-basic-offset: 8 -- emacs
752 * tab-width: 8 -- emacs
753 * indent-tabs-mode: nil -- emacs