1 /* this file is part of evince, a gnome document viewer
3 * Copyright (C) 2004 Martin Kretzschmar
6 * Martin Kretzschmar <martink@gnome.org>
8 * Evince is free software; you can redistribute it and/or modify it
9 * 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.
13 * Evince is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
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.
27 #include "ev-window.h"
30 #include <glib-object.h>
31 #include <glib/gi18n.h>
32 #include <gtk/gtkaboutdialog.h>
33 #include <gtk/gtkaccelgroup.h>
34 #include <gtk/gtkactiongroup.h>
35 #include <gtk/gtkmain.h>
36 #include <gtk/gtkmenuitem.h>
37 #include <gtk/gtkstatusbar.h>
38 #include <gtk/gtkstock.h>
39 #include <gtk/gtktoggleaction.h>
40 #include <gtk/gtkuimanager.h>
41 #include <gtk/gtkvbox.h>
42 #include <libbonoboui.h>
43 #include <libgnomevfs/gnome-vfs-mime-utils.h>
47 #include <ev-macros.h>
48 #include <ev-stock-icons.h>
50 #include "ev-application.h"
62 struct _EvWindowPrivate {
64 GtkWidget *bonobo_widget;
65 GtkUIManager *ui_manager;
67 guint help_message_cid;
71 /* enable these to add support for signals */
72 static guint ev_window_signals [N_SIGNALS] = { 0 };
75 G_DEFINE_TYPE (EvWindow, ev_window, GTK_TYPE_WINDOW)
77 #define EV_WINDOW_GET_PRIVATE(object) \
78 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_WINDOW, EvWindowPrivate))
82 ev_window_get_attribute (EvWindow *self)
84 g_return_val_if_fail (self != NULL && EV_IS_WINDOW (self), NULL);
86 return self->priv->attribute;
90 ev_window_set_attribute (EvWindow* self, const char *attribute)
92 g_assert (self != NULL && EV_IS_WINDOW (self));
93 g_assert (attribute != NULL);
95 if (self->priv->attribute != NULL) {
96 g_free (self->priv->attribute);
99 self->priv->attribute = g_strdup (attribute);
101 g_object_notify (G_OBJECT (self), "attribute");
105 ev_window_get_property (GObject *object, guint prop_id, GValue *value,
106 GParamSpec *param_spec)
110 self = EV_WINDOW (object);
114 g_value_set_string (value, self->priv->attribute);
117 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
125 ev_window_set_property (GObject *object, guint prop_id, const GValue *value,
126 GParamSpec *param_spec)
130 self = EV_WINDOW (object);
134 ev_window_set_attribute (self, g_value_get_string (value));
137 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
146 ev_window_is_empty (const EvWindow *ev_window)
148 g_return_val_if_fail (EV_IS_WINDOW (ev_window), FALSE);
150 return ev_window->priv->bonobo_widget == NULL;
154 ev_window_open (EvWindow *ev_window, const char *uri)
158 BonoboObject *bonobo_control;
159 CORBA_Environment ev;
160 Bonobo_PersistFile pf;
162 mime_type = gnome_vfs_get_mime_type (uri);
164 g_return_if_fail (mime_type != NULL); /* FIXME set error */
166 if (!strcmp (mime_type, "application/pdf")) {
167 bonobo_control = create_gpdf_control ();
168 } else if (!strcmp (mime_type, "application/postscript")) {
169 bonobo_control = create_ggv_control ();
170 } else if (!strcmp (mime_type, "application/x-gzip")) {
171 g_message ("Cannot open gzip-compressed file %s.", uri);
173 } else if (!strcmp (mime_type, "application/x-bzip")) {
174 g_message ("Cannot open bzip2-compressed file %s.", uri);
177 g_warning ("Don't know how to open %s file %s.",
178 mime_type, uri); /* FIXME set error */
182 ev_window->priv->bonobo_widget = bonobo_widget_new_control_from_objref (
183 bonobo_object_corba_objref (bonobo_control), CORBA_OBJECT_NIL);
184 gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box),
185 ev_window->priv->bonobo_widget,
187 CORBA_exception_init (&ev);
188 pf = bonobo_object_query_interface (
189 bonobo_control, "IDL:Bonobo/PersistFile:1.0", &ev);
190 Bonobo_PersistFile_load (pf, uri, &ev);
191 gtk_widget_show (ev_window->priv->bonobo_widget);
192 bonobo_object_release_unref (pf, &ev);
193 bonobo_object_unref (bonobo_control);
194 CORBA_exception_free (&ev);
202 ev_window_cmd_file_open (GtkAction *action, EvWindow *ev_window)
204 ev_application_open (EV_APP, NULL);
208 ev_window_cmd_file_close_window (GtkAction *action, EvWindow *ev_window)
210 g_return_if_fail (EV_IS_WINDOW (ev_window));
212 gtk_widget_destroy (GTK_WIDGET (ev_window));
216 ev_window_cmd_help_about (GtkAction *action, EvWindow *ev_window)
218 const char *authors[] = {
223 const char *documenters[] = {
224 N_("Not so many..."),
228 const char *license[] = {
229 N_("Evince is free software; you can redistribute it and/or modify\n"
230 "it under the terms of the GNU General Public License as published by\n"
231 "the Free Software Foundation; either version 2 of the License, or\n"
232 "(at your option) any later version.\n"),
233 N_("Evince is distributed in the hope that it will be useful,\n"
234 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
235 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
236 "GNU General Public License for more details.\n"),
237 N_("You should have received a copy of the GNU General Public License\n"
238 "along with Evince; if not, write to the Free Software Foundation, Inc.,\n"
239 "59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n")
247 for (p = authors; *p; ++p)
250 for (p = documenters; *p; ++p)
254 license_trans = g_strconcat (_(license[0]), "\n", _(license[1]), "\n",
255 _(license[2]), "\n", NULL);
257 gtk_show_about_dialog (
258 GTK_WINDOW (ev_window),
262 _("\xc2\xa9 1996-2004 The Evince authors"),
263 "license", license_trans,
264 "website", "http://www.gnome.org/projects/evince",
265 "comments", _("PostScript and PDF File Viewer."),
267 "documenters", documenters,
268 "translator-credits", _("translator-credits"),
271 g_free (license_trans);
275 ev_window_view_toolbar_cb (GtkAction *action, EvWindow *ev_window)
278 G_OBJECT (gtk_ui_manager_get_widget (
279 ev_window->priv->ui_manager,
282 gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)),
287 ev_window_view_statusbar_cb (GtkAction *action, EvWindow *ev_window)
290 ev_window->priv->statusbar,
292 gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)),
297 menu_item_select_cb (GtkMenuItem *proxy, EvWindow *ev_window)
302 action = g_object_get_data (G_OBJECT (proxy), "gtk-action");
303 g_return_if_fail (action != NULL);
305 g_object_get (G_OBJECT (action), "tooltip", &message, NULL);
307 gtk_statusbar_push (GTK_STATUSBAR (ev_window->priv->statusbar),
308 ev_window->priv->help_message_cid, message);
314 menu_item_deselect_cb (GtkMenuItem *proxy, EvWindow *ev_window)
316 gtk_statusbar_pop (GTK_STATUSBAR (ev_window->priv->statusbar),
317 ev_window->priv->help_message_cid);
321 connect_proxy_cb (GtkUIManager *ui_manager, GtkAction *action,
322 GtkWidget *proxy, EvWindow *ev_window)
324 if (GTK_IS_MENU_ITEM (proxy)) {
325 g_signal_connect (proxy, "select",
326 G_CALLBACK (menu_item_select_cb), ev_window);
327 g_signal_connect (proxy, "deselect",
328 G_CALLBACK (menu_item_deselect_cb),
334 disconnect_proxy_cb (GtkUIManager *ui_manager, GtkAction *action,
335 GtkWidget *proxy, EvWindow *ev_window)
337 if (GTK_IS_MENU_ITEM (proxy)) {
338 g_signal_handlers_disconnect_by_func
339 (proxy, G_CALLBACK (menu_item_select_cb), ev_window);
340 g_signal_handlers_disconnect_by_func
341 (proxy, G_CALLBACK (menu_item_deselect_cb), ev_window);
346 ev_window_dispose (GObject *object)
348 EvWindowPrivate *priv;
350 g_return_if_fail (object != NULL && EV_IS_WINDOW (object));
352 priv = EV_WINDOW (object)->priv;
354 EV_UNREF (priv->ui_manager);
357 G_OBJECT_CLASS (ev_window_parent_class), dispose, (object));
361 ev_window_finalize (GObject *object)
363 g_return_if_fail (object != NULL && EV_IS_WINDOW (object));
365 EV_WINDOW (object)->priv = NULL;
368 G_OBJECT_CLASS (ev_window_parent_class), finalize, (object));
372 ev_window_class_init (EvWindowClass *ev_window_class)
374 GObjectClass *g_object_class;
376 g_object_class = G_OBJECT_CLASS (ev_window_class);
377 g_object_class->dispose = ev_window_dispose;
378 g_object_class->finalize = ev_window_finalize;
380 g_type_class_add_private (g_object_class, sizeof (EvWindowPrivate));
383 /* setting up signal system */
384 ev_window_class->signal = ev_window_signal;
386 ev_window_signals [SIGNAL] = g_signal_new (
390 G_STRUCT_OFFSET (EvWindowClass,
394 g_cclosure_marshal_VOID__STRING,
397 /* setting up property system */
398 g_object_class->set_property = ev_window_set_property;
399 g_object_class->get_property = ev_window_get_property;
401 g_object_class_install_property (
404 g_param_spec_string ("attribute",
406 "A simple unneccessary attribute that "
407 "does nothing special except being a "
408 "demonstration for the correct implem"
409 "entation of a GObject property",
411 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
416 static GtkActionEntry entries[] = {
417 { "File", NULL, N_("_File") },
418 { "View", NULL, N_("_View") },
419 { "Help", NULL, N_("_Help") },
422 { "FileOpen", GTK_STOCK_OPEN, N_("_Open"), "<control>O",
424 G_CALLBACK (ev_window_cmd_file_open) },
425 { "FileCloseWindow", GTK_STOCK_CLOSE, N_("_Close"), "<control>W",
426 N_("Close this window"),
427 G_CALLBACK (ev_window_cmd_file_close_window) },
430 { "HelpAbout", STOCK_ABOUT, N_("_About"), NULL,
431 N_("Display credits for the document viewer creators"),
432 G_CALLBACK (ev_window_cmd_help_about) },
436 static GtkToggleActionEntry toggle_entries[] = {
438 { "ViewToolbar", NULL, N_("_Toolbar"), "<shift><control>T",
439 N_("Show or hide toolbar"),
440 G_CALLBACK (ev_window_view_toolbar_cb), TRUE },
441 { "ViewStatusbar", NULL, N_("_Statusbar"), NULL,
442 N_("Show or hide statusbar"),
443 G_CALLBACK (ev_window_view_statusbar_cb), TRUE },
447 ev_window_init (EvWindow *ev_window)
449 GtkActionGroup *action_group;
450 GtkAccelGroup *accel_group;
451 GError *error = NULL;
455 ev_window->priv = EV_WINDOW_GET_PRIVATE (ev_window);
457 gtk_window_set_title (GTK_WINDOW (ev_window), _("Evince"));
459 ev_window->priv->main_box = gtk_vbox_new (FALSE, 0);
460 gtk_container_add (GTK_CONTAINER (ev_window), ev_window->priv->main_box);
461 gtk_widget_show (ev_window->priv->main_box);
463 action_group = gtk_action_group_new ("MenuActions");
464 gtk_action_group_set_translation_domain (action_group, NULL);
465 gtk_action_group_add_actions (action_group, entries,
466 G_N_ELEMENTS (entries), ev_window);
467 gtk_action_group_add_toggle_actions (action_group, toggle_entries,
468 G_N_ELEMENTS (toggle_entries),
471 ev_window->priv->ui_manager = gtk_ui_manager_new ();
472 gtk_ui_manager_insert_action_group (ev_window->priv->ui_manager,
476 gtk_ui_manager_get_accel_group (ev_window->priv->ui_manager);
477 gtk_window_add_accel_group (GTK_WINDOW (ev_window), accel_group);
479 g_signal_connect (ev_window->priv->ui_manager, "connect_proxy",
480 G_CALLBACK (connect_proxy_cb), ev_window);
481 g_signal_connect (ev_window->priv->ui_manager, "disconnect_proxy",
482 G_CALLBACK (disconnect_proxy_cb), ev_window);
484 if (!gtk_ui_manager_add_ui_from_file (ev_window->priv->ui_manager,
485 DATADIR"/evince-ui.xml",
487 g_message ("building menus failed: %s", error->message);
488 g_error_free (error);
491 menubar = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
493 gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), menubar,
496 toolbar = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
498 gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), toolbar,
501 ev_window->priv->statusbar = gtk_statusbar_new ();
502 gtk_widget_show (ev_window->priv->statusbar);
503 gtk_box_pack_end (GTK_BOX (ev_window->priv->main_box),
504 ev_window->priv->statusbar,
506 ev_window->priv->help_message_cid = gtk_statusbar_get_context_id
507 (GTK_STATUSBAR (ev_window->priv->statusbar), "help_message");