]> www.fi.muni.cz Git - evince.git/blob - shell/ev-window.c
Initial revision
[evince.git] / shell / ev-window.c
1 /* this file is part of evince, a gnome document viewer
2  *
3  *  Copyright (C) 2004 Martin Kretzschmar
4  *
5  *  Author:
6  *    Martin Kretzschmar <martink@gnome.org>
7  *
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.
12  *
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.
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 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "ev-window.h"
28
29 #include <glib.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>
44
45 #include <string.h>
46
47 #include <ev-macros.h>
48 #include <ev-stock-icons.h>
49
50 #include "ev-application.h"
51
52 enum {
53         PROP_0,
54         PROP_ATTRIBUTE
55 };
56
57 enum {
58         SIGNAL,
59         N_SIGNALS
60 };
61
62 struct _EvWindowPrivate {
63         GtkWidget *main_box;
64         GtkWidget *bonobo_widget;
65         GtkUIManager *ui_manager;
66         GtkWidget *statusbar;
67         guint help_message_cid;
68 };
69
70 #if 0
71 /* enable these to add support for signals */
72 static guint ev_window_signals [N_SIGNALS] = { 0 };
73 #endif
74
75 G_DEFINE_TYPE (EvWindow, ev_window, GTK_TYPE_WINDOW)
76
77 #define EV_WINDOW_GET_PRIVATE(object) \
78         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_WINDOW, EvWindowPrivate))
79
80 #if 0
81 const char *
82 ev_window_get_attribute (EvWindow *self)
83 {
84         g_return_val_if_fail (self != NULL && EV_IS_WINDOW (self), NULL);
85         
86         return self->priv->attribute;
87 }
88
89 void
90 ev_window_set_attribute (EvWindow* self, const char *attribute)
91 {
92         g_assert (self != NULL && EV_IS_WINDOW (self));
93         g_assert (attribute != NULL);
94
95         if (self->priv->attribute != NULL) {
96                 g_free (self->priv->attribute);
97         }
98
99         self->priv->attribute = g_strdup (attribute);
100
101         g_object_notify (G_OBJECT (self), "attribute");
102 }
103
104 static void
105 ev_window_get_property (GObject *object, guint prop_id, GValue *value,
106                         GParamSpec *param_spec)
107 {
108         EvWindow *self;
109
110         self = EV_WINDOW (object);
111
112         switch (prop_id) {
113         case PROP_ATTRIBUTE:
114                 g_value_set_string (value, self->priv->attribute);
115                 break;
116         default:
117                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
118                                                    prop_id,
119                                                    param_spec);
120                 break;
121         }
122 }
123
124 static void
125 ev_window_set_property (GObject *object, guint prop_id, const GValue *value,
126                         GParamSpec *param_spec)
127 {
128         EvWindow *self;
129         
130         self = EV_WINDOW (object);
131         
132         switch (prop_id) {
133         case PROP_ATTRIBUTE:
134                 ev_window_set_attribute (self, g_value_get_string (value));
135                 break;
136         default:
137                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
138                                                    prop_id,
139                                                    param_spec);
140                 break;
141         }
142 }
143 #endif
144
145 gboolean
146 ev_window_is_empty (const EvWindow *ev_window)
147 {
148         g_return_val_if_fail (EV_IS_WINDOW (ev_window), FALSE);
149         
150         return ev_window->priv->bonobo_widget == NULL;
151 }
152
153 void
154 ev_window_open (EvWindow *ev_window, const char *uri)
155 {
156 #if 0
157         char *mime_type;
158         BonoboObject *bonobo_control;
159         CORBA_Environment ev;
160         Bonobo_PersistFile pf;
161
162         mime_type = gnome_vfs_get_mime_type (uri);
163
164         g_return_if_fail (mime_type != NULL); /* FIXME set error */
165
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);
172                 goto finally;
173         } else if (!strcmp (mime_type, "application/x-bzip")) {
174                 g_message ("Cannot open bzip2-compressed file %s.", uri);
175                 goto finally;
176         } else {
177                 g_warning ("Don't know how to open %s file %s.",
178                            mime_type, uri); /* FIXME set error */
179                 goto finally;
180         }
181
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,
186                             TRUE, TRUE, 0);
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);
195
196 finally:
197         g_free (mime_type);
198 #endif
199 }
200
201 static void
202 ev_window_cmd_file_open (GtkAction *action, EvWindow *ev_window)
203 {
204         ev_application_open (EV_APP, NULL);
205 }
206
207 static void
208 ev_window_cmd_file_close_window (GtkAction *action, EvWindow *ev_window)
209 {
210         g_return_if_fail (EV_IS_WINDOW (ev_window));
211
212         gtk_widget_destroy (GTK_WIDGET (ev_window));
213 }
214
215 static void
216 ev_window_cmd_help_about (GtkAction *action, EvWindow *ev_window)
217 {
218         const char *authors[] = {
219                 N_("Many..."),
220                 NULL
221         };
222
223         const char *documenters[] = {
224                 N_("Not so many..."),
225                 NULL
226         };
227
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")
240         };
241
242         char *license_trans;
243
244 #ifdef ENABLE_NLS
245         const char **p;
246
247         for (p = authors; *p; ++p)
248                 *p = _(*p);
249
250         for (p = documenters; *p; ++p)
251                 *p = _(*p);
252 #endif
253
254         license_trans = g_strconcat (_(license[0]), "\n", _(license[1]), "\n",
255                                      _(license[2]), "\n", NULL);
256
257         gtk_show_about_dialog (
258                 GTK_WINDOW (ev_window),
259                 "name", _("Evince"),
260                 "version", VERSION,
261                 "copyright",
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."),
266                 "authors", authors,
267                 "documenters", documenters,
268                 "translator-credits", _("translator-credits"),
269                 NULL);
270
271         g_free (license_trans);
272 }
273
274 static void
275 ev_window_view_toolbar_cb (GtkAction *action, EvWindow *ev_window)
276 {
277         g_object_set (
278                 G_OBJECT (gtk_ui_manager_get_widget (
279                                   ev_window->priv->ui_manager,
280                                   "/ToolBar")),
281                 "visible",
282                 gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)),
283                 NULL);
284 }
285
286 static void
287 ev_window_view_statusbar_cb (GtkAction *action, EvWindow *ev_window)
288 {
289         g_object_set (
290                 ev_window->priv->statusbar,
291                 "visible",
292                 gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)),
293                 NULL);
294 }
295
296 static void
297 menu_item_select_cb (GtkMenuItem *proxy, EvWindow *ev_window)
298 {
299         GtkAction *action;
300         char *message;
301
302         action = g_object_get_data (G_OBJECT (proxy), "gtk-action");
303         g_return_if_fail (action != NULL);
304         
305         g_object_get (G_OBJECT (action), "tooltip", &message, NULL);
306         if (message) {
307                 gtk_statusbar_push (GTK_STATUSBAR (ev_window->priv->statusbar),
308                                     ev_window->priv->help_message_cid, message);
309                 g_free (message);
310         }
311 }
312
313 static void
314 menu_item_deselect_cb (GtkMenuItem *proxy, EvWindow *ev_window)
315 {
316         gtk_statusbar_pop (GTK_STATUSBAR (ev_window->priv->statusbar),
317                            ev_window->priv->help_message_cid);
318 }
319
320 static void
321 connect_proxy_cb (GtkUIManager *ui_manager, GtkAction *action,
322                   GtkWidget *proxy, EvWindow *ev_window)
323 {
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),
329                                   ev_window);
330         }
331 }
332
333 static void
334 disconnect_proxy_cb (GtkUIManager *ui_manager, GtkAction *action,
335                      GtkWidget *proxy, EvWindow *ev_window)
336 {
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);
342         }
343 }
344
345 static void
346 ev_window_dispose (GObject *object)
347 {
348         EvWindowPrivate *priv;
349
350         g_return_if_fail (object != NULL && EV_IS_WINDOW (object));
351
352         priv = EV_WINDOW (object)->priv;
353
354         EV_UNREF (priv->ui_manager);
355
356         EV_CALL_VIRTUAL (
357                 G_OBJECT_CLASS (ev_window_parent_class), dispose, (object));
358 }
359
360 static void
361 ev_window_finalize (GObject *object)
362 {
363         g_return_if_fail (object != NULL && EV_IS_WINDOW (object));
364
365         EV_WINDOW (object)->priv = NULL;
366
367         EV_CALL_VIRTUAL (
368                 G_OBJECT_CLASS (ev_window_parent_class), finalize, (object));
369 }
370
371 static void
372 ev_window_class_init (EvWindowClass *ev_window_class)
373 {
374         GObjectClass *g_object_class;
375
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;
379
380         g_type_class_add_private (g_object_class, sizeof (EvWindowPrivate));
381
382 #if 0
383         /* setting up signal system */
384         ev_window_class->signal = ev_window_signal;
385
386         ev_window_signals [SIGNAL] = g_signal_new (
387                 "signal",
388                 EV_TYPE_WINDOW,
389                 G_SIGNAL_RUN_LAST,
390                 G_STRUCT_OFFSET (EvWindowClass,
391                                  signal),
392                 NULL,
393                 NULL,
394                 g_cclosure_marshal_VOID__STRING,
395                 G_TYPE_NONE,
396                 0);
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;
400
401         g_object_class_install_property (
402                 g_object_class,
403                 PROP_ATTRIBUTE,
404                 g_param_spec_string ("attribute",
405                                      "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",
410                                      "default_value",
411                                      G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
412 #endif
413 }
414
415 /* Normal items */
416 static GtkActionEntry entries[] = {
417         { "File", NULL, N_("_File") },
418         { "View", NULL, N_("_View") },
419         { "Help", NULL, N_("_Help") },
420
421         /* File menu */
422         { "FileOpen", GTK_STOCK_OPEN, N_("_Open"), "<control>O",
423           N_("Open a file"),
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) },
428
429         /* Help menu */
430         { "HelpAbout", STOCK_ABOUT, N_("_About"), NULL,
431           N_("Display credits for the document viewer creators"),
432           G_CALLBACK (ev_window_cmd_help_about) },
433 };
434
435 /* Toggle items */
436 static GtkToggleActionEntry toggle_entries[] = {
437         /* View Menu */
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 },
444 };
445
446 static void
447 ev_window_init (EvWindow *ev_window)
448 {
449         GtkActionGroup *action_group;
450         GtkAccelGroup *accel_group;
451         GError *error = NULL;
452         GtkWidget *menubar;
453         GtkWidget *toolbar;
454
455         ev_window->priv = EV_WINDOW_GET_PRIVATE (ev_window);
456
457         gtk_window_set_title (GTK_WINDOW (ev_window), _("Evince"));
458
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);
462
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),
469                                              ev_window);
470
471         ev_window->priv->ui_manager = gtk_ui_manager_new ();
472         gtk_ui_manager_insert_action_group (ev_window->priv->ui_manager,
473                                             action_group, 0);
474
475         accel_group =
476                 gtk_ui_manager_get_accel_group (ev_window->priv->ui_manager);
477         gtk_window_add_accel_group (GTK_WINDOW (ev_window), accel_group);
478
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);
483
484         if (!gtk_ui_manager_add_ui_from_file (ev_window->priv->ui_manager,
485                                               DATADIR"/evince-ui.xml",
486                                               &error)) {
487                 g_message ("building menus failed: %s", error->message);
488                 g_error_free (error);
489         }
490
491         menubar = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
492                                              "/MainMenu");
493         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), menubar,
494                             FALSE, FALSE, 0);
495
496         toolbar = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
497                                              "/ToolBar");
498         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), toolbar,
499                             FALSE, FALSE, 0);
500
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,
505                           FALSE, TRUE, 0);
506         ev_window->priv->help_message_cid = gtk_statusbar_get_context_id
507                 (GTK_STATUSBAR (ev_window->priv->statusbar), "help_message");
508
509 }