]> www.fi.muni.cz Git - evince.git/blob - shell/ev-window.c
*** empty log message ***
[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-application.h"
48
49 enum {
50         PROP_0,
51         PROP_ATTRIBUTE
52 };
53
54 enum {
55         SIGNAL,
56         N_SIGNALS
57 };
58
59 struct _EvWindowPrivate {
60         GtkWidget *main_box;
61         GtkWidget *bonobo_widget;
62         GtkUIManager *ui_manager;
63         GtkWidget *statusbar;
64         guint help_message_cid;
65 };
66
67 #if 0
68 /* enable these to add support for signals */
69 static guint ev_window_signals [N_SIGNALS] = { 0 };
70 #endif
71
72 static GObjectClass *parent_class = NULL;
73
74 G_DEFINE_TYPE (EvWindow, ev_window, GTK_TYPE_WINDOW)
75
76 #define EV_WINDOW_GET_PRIVATE(object) \
77         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_WINDOW, EvWindowPrivate))
78
79 #if 0
80 const char *
81 ev_window_get_attribute (EvWindow *self)
82 {
83         g_return_val_if_fail (self != NULL && EV_IS_WINDOW (self), NULL);
84         
85         return self->priv->attribute;
86 }
87
88 void
89 ev_window_set_attribute (EvWindow* self, const char *attribute)
90 {
91         g_assert (self != NULL && EV_IS_WINDOW (self));
92         g_assert (attribute != NULL);
93
94         if (self->priv->attribute != NULL) {
95                 g_free (self->priv->attribute);
96         }
97
98         self->priv->attribute = g_strdup (attribute);
99
100         g_object_notify (G_OBJECT (self), "attribute");
101 }
102
103 static void
104 ev_window_get_property (GObject *object, guint prop_id, GValue *value,
105                         GParamSpec *param_spec)
106 {
107         EvWindow *self;
108
109         self = EV_WINDOW (object);
110
111         switch (prop_id) {
112         case PROP_ATTRIBUTE:
113                 g_value_set_string (value, self->priv->attribute);
114                 break;
115         default:
116                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
117                                                    prop_id,
118                                                    param_spec);
119                 break;
120         }
121 }
122
123 static void
124 ev_window_set_property (GObject *object, guint prop_id, const GValue *value,
125                         GParamSpec *param_spec)
126 {
127         EvWindow *self;
128         
129         self = EV_WINDOW (object);
130         
131         switch (prop_id) {
132         case PROP_ATTRIBUTE:
133                 ev_window_set_attribute (self, g_value_get_string (value));
134                 break;
135         default:
136                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
137                                                    prop_id,
138                                                    param_spec);
139                 break;
140         }
141 }
142 #endif
143
144 gboolean
145 ev_window_is_empty (const EvWindow *ev_window)
146 {
147         g_return_val_if_fail (EV_IS_WINDOW (ev_window), FALSE);
148         
149         return ev_window->priv->bonobo_widget == NULL;
150 }
151
152 void
153 ev_window_open (EvWindow *ev_window, const char *uri)
154 {
155 #if 0
156         char *mime_type;
157         BonoboObject *bonobo_control;
158         CORBA_Environment ev;
159         Bonobo_PersistFile pf;
160
161         mime_type = gnome_vfs_get_mime_type (uri);
162
163         g_return_if_fail (mime_type != NULL); /* FIXME set error */
164
165         if (!strcmp (mime_type, "application/pdf")) {
166                 bonobo_control = create_gpdf_control ();
167         } else if (!strcmp (mime_type, "application/postscript")) {
168                 bonobo_control = create_ggv_control ();
169         } else if (!strcmp (mime_type, "application/x-gzip")) {
170                 g_message ("Cannot open gzip-compressed file %s.", uri);
171                 goto finally;
172         } else if (!strcmp (mime_type, "application/x-bzip")) {
173                 g_message ("Cannot open bzip2-compressed file %s.", uri);
174                 goto finally;
175         } else {
176                 g_warning ("Don't know how to open %s file %s.",
177                            mime_type, uri); /* FIXME set error */
178                 goto finally;
179         }
180
181         ev_window->priv->bonobo_widget = bonobo_widget_new_control_from_objref (
182                 bonobo_object_corba_objref (bonobo_control), CORBA_OBJECT_NIL);
183         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box),
184                             ev_window->priv->bonobo_widget,
185                             TRUE, TRUE, 0);
186         CORBA_exception_init (&ev);
187         pf = bonobo_object_query_interface (
188                 bonobo_control, "IDL:Bonobo/PersistFile:1.0", &ev);
189         Bonobo_PersistFile_load (pf, uri, &ev);
190         gtk_widget_show (ev_window->priv->bonobo_widget);
191         bonobo_object_release_unref (pf, &ev);
192         bonobo_object_unref (bonobo_control);
193         CORBA_exception_free (&ev);
194
195 finally:
196         g_free (mime_type);
197 #endif
198 }
199
200 static void
201 ev_window_cmd_file_open (GtkAction *action, EvWindow *ev_window)
202 {
203         ev_application_open (EV_APP, NULL);
204 }
205
206 static void
207 ev_window_cmd_file_close_window (GtkAction *action, EvWindow *ev_window)
208 {
209         g_return_if_fail (EV_IS_WINDOW (ev_window));
210
211         gtk_widget_destroy (GTK_WIDGET (ev_window));
212 }
213
214 static void
215 ev_window_cmd_help_about (GtkAction *action, EvWindow *ev_window)
216 {
217         const char *authors[] = {
218                 N_("Many..."),
219                 NULL
220         };
221
222         const char *documenters[] = {
223                 N_("Not so many..."),
224                 NULL
225         };
226
227         const char *license[] = {
228                 N_("Evince is free software; you can redistribute it and/or modify\n"
229                    "it under the terms of the GNU General Public License as published by\n"
230                    "the Free Software Foundation; either version 2 of the License, or\n"
231                    "(at your option) any later version.\n"),            
232                 N_("Evince is distributed in the hope that it will be useful,\n"
233                    "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
234                    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
235                    "GNU General Public License for more details.\n"),
236                 N_("You should have received a copy of the GNU General Public License\n"
237                    "along with Evince; if not, write to the Free Software Foundation, Inc.,\n"
238                    "59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n")
239         };
240
241         char *license_trans;
242
243 #ifdef ENABLE_NLS
244         const char **p;
245
246         for (p = authors; *p; ++p)
247                 *p = _(*p);
248
249         for (p = documenters; *p; ++p)
250                 *p = _(*p);
251 #endif
252
253         license_trans = g_strconcat (_(license[0]), "\n", _(license[1]), "\n",
254                                      _(license[2]), "\n", NULL);
255
256         gtk_show_about_dialog (
257                 GTK_WINDOW (ev_window),
258                 "name", _("Evince"),
259                 "version", VERSION,
260                 "copyright",
261                 _("\xc2\xa9 1996-2004 The Evince authors"),
262                 "license", license_trans,
263                 "website", "http://www.gnome.org/projects/evince",
264                 "comments", _("PostScript and PDF File Viewer."),
265                 "authors", authors,
266                 "documenters", documenters,
267                 "translator-credits", _("translator-credits"),
268                 NULL);
269
270         g_free (license_trans);
271 }
272
273 static void
274 ev_window_view_toolbar_cb (GtkAction *action, EvWindow *ev_window)
275 {
276         g_object_set (
277                 G_OBJECT (gtk_ui_manager_get_widget (
278                                   ev_window->priv->ui_manager,
279                                   "/ToolBar")),
280                 "visible",
281                 gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)),
282                 NULL);
283 }
284
285 static void
286 ev_window_view_statusbar_cb (GtkAction *action, EvWindow *ev_window)
287 {
288         g_object_set (
289                 ev_window->priv->statusbar,
290                 "visible",
291                 gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)),
292                 NULL);
293 }
294
295 static void
296 menu_item_select_cb (GtkMenuItem *proxy, EvWindow *ev_window)
297 {
298         GtkAction *action;
299         char *message;
300
301         action = g_object_get_data (G_OBJECT (proxy), "gtk-action");
302         g_return_if_fail (action != NULL);
303         
304         g_object_get (G_OBJECT (action), "tooltip", &message, NULL);
305         if (message) {
306                 gtk_statusbar_push (GTK_STATUSBAR (ev_window->priv->statusbar),
307                                     ev_window->priv->help_message_cid, message);
308                 g_free (message);
309         }
310 }
311
312 static void
313 menu_item_deselect_cb (GtkMenuItem *proxy, EvWindow *ev_window)
314 {
315         gtk_statusbar_pop (GTK_STATUSBAR (ev_window->priv->statusbar),
316                            ev_window->priv->help_message_cid);
317 }
318
319 static void
320 connect_proxy_cb (GtkUIManager *ui_manager, GtkAction *action,
321                   GtkWidget *proxy, EvWindow *ev_window)
322 {
323         if (GTK_IS_MENU_ITEM (proxy)) {
324                 g_signal_connect (proxy, "select",
325                                   G_CALLBACK (menu_item_select_cb), ev_window);
326                 g_signal_connect (proxy, "deselect",
327                                   G_CALLBACK (menu_item_deselect_cb),
328                                   ev_window);
329         }
330 }
331
332 static void
333 disconnect_proxy_cb (GtkUIManager *ui_manager, GtkAction *action,
334                      GtkWidget *proxy, EvWindow *ev_window)
335 {
336         if (GTK_IS_MENU_ITEM (proxy)) {
337                 g_signal_handlers_disconnect_by_func
338                         (proxy, G_CALLBACK (menu_item_select_cb), ev_window);
339                 g_signal_handlers_disconnect_by_func
340                         (proxy, G_CALLBACK (menu_item_deselect_cb), ev_window);
341         }
342 }
343
344 static void
345 ev_window_dispose (GObject *object)
346 {
347         EvWindowPrivate *priv;
348
349         g_return_if_fail (object != NULL && EV_IS_WINDOW (object));
350
351         priv = EV_WINDOW (object)->priv;
352
353         g_object_unref (priv->ui_manager);
354
355         G_OBJECT_CLASS (parent_class)->dispose (object);
356 }
357
358 static void
359 ev_window_class_init (EvWindowClass *ev_window_class)
360 {
361         GObjectClass *g_object_class;
362
363         parent_class = g_type_class_peek_parent (ev_window_class);
364
365         g_object_class = G_OBJECT_CLASS (ev_window_class);
366         g_object_class->dispose = ev_window_dispose;
367
368         g_type_class_add_private (g_object_class, sizeof (EvWindowPrivate));
369
370 #if 0
371         /* setting up signal system */
372         ev_window_class->signal = ev_window_signal;
373
374         ev_window_signals [SIGNAL] = g_signal_new (
375                 "signal",
376                 EV_TYPE_WINDOW,
377                 G_SIGNAL_RUN_LAST,
378                 G_STRUCT_OFFSET (EvWindowClass,
379                                  signal),
380                 NULL,
381                 NULL,
382                 g_cclosure_marshal_VOID__STRING,
383                 G_TYPE_NONE,
384                 0);
385         /* setting up property system */
386         g_object_class->set_property = ev_window_set_property;
387         g_object_class->get_property = ev_window_get_property;
388
389         g_object_class_install_property (
390                 g_object_class,
391                 PROP_ATTRIBUTE,
392                 g_param_spec_string ("attribute",
393                                      "Attribute",
394                                      "A simple unneccessary attribute that "
395                                      "does nothing special except being a "
396                                      "demonstration for the correct implem"
397                                      "entation of a GObject property",
398                                      "default_value",
399                                      G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
400 #endif
401 }
402
403 /* Normal items */
404 static GtkActionEntry entries[] = {
405         { "File", NULL, N_("_File") },
406         { "View", NULL, N_("_View") },
407         { "Help", NULL, N_("_Help") },
408
409         /* File menu */
410         { "FileOpen", GTK_STOCK_OPEN, N_("_Open"), "<control>O",
411           N_("Open a file"),
412           G_CALLBACK (ev_window_cmd_file_open) },
413         { "FileCloseWindow", GTK_STOCK_CLOSE, N_("_Close"), "<control>W",
414           N_("Close this window"),
415           G_CALLBACK (ev_window_cmd_file_close_window) },
416
417         /* Help menu */
418         { "HelpAbout", NULL, N_("_About"), NULL,
419           N_("Display credits for the document viewer creators"),
420           G_CALLBACK (ev_window_cmd_help_about) },
421 };
422
423 /* Toggle items */
424 static GtkToggleActionEntry toggle_entries[] = {
425         /* View Menu */
426         { "ViewToolbar", NULL, N_("_Toolbar"), "<shift><control>T",
427           N_("Show or hide toolbar"),
428           G_CALLBACK (ev_window_view_toolbar_cb), TRUE },
429         { "ViewStatusbar", NULL, N_("_Statusbar"), NULL,
430           N_("Show or hide statusbar"),
431           G_CALLBACK (ev_window_view_statusbar_cb), TRUE },
432 };
433
434 static void
435 ev_window_init (EvWindow *ev_window)
436 {
437         GtkActionGroup *action_group;
438         GtkAccelGroup *accel_group;
439         GError *error = NULL;
440         GtkWidget *menubar;
441         GtkWidget *toolbar;
442
443         ev_window->priv = EV_WINDOW_GET_PRIVATE (ev_window);
444
445         gtk_window_set_title (GTK_WINDOW (ev_window), _("Evince"));
446
447         ev_window->priv->main_box = gtk_vbox_new (FALSE, 0);
448         gtk_container_add (GTK_CONTAINER (ev_window), ev_window->priv->main_box);
449         gtk_widget_show (ev_window->priv->main_box);
450
451         action_group = gtk_action_group_new ("MenuActions");
452         gtk_action_group_set_translation_domain (action_group, NULL);
453         gtk_action_group_add_actions (action_group, entries,
454                                       G_N_ELEMENTS (entries), ev_window);
455         gtk_action_group_add_toggle_actions (action_group, toggle_entries,
456                                              G_N_ELEMENTS (toggle_entries),
457                                              ev_window);
458
459         ev_window->priv->ui_manager = gtk_ui_manager_new ();
460         gtk_ui_manager_insert_action_group (ev_window->priv->ui_manager,
461                                             action_group, 0);
462
463         accel_group =
464                 gtk_ui_manager_get_accel_group (ev_window->priv->ui_manager);
465         gtk_window_add_accel_group (GTK_WINDOW (ev_window), accel_group);
466
467         g_signal_connect (ev_window->priv->ui_manager, "connect_proxy",
468                           G_CALLBACK (connect_proxy_cb), ev_window);
469         g_signal_connect (ev_window->priv->ui_manager, "disconnect_proxy",
470                           G_CALLBACK (disconnect_proxy_cb), ev_window);
471
472         if (!gtk_ui_manager_add_ui_from_file (ev_window->priv->ui_manager,
473                                               DATADIR"/evince-ui.xml",
474                                               &error)) {
475                 g_message ("building menus failed: %s", error->message);
476                 g_error_free (error);
477         }
478
479         menubar = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
480                                              "/MainMenu");
481         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), menubar,
482                             FALSE, FALSE, 0);
483
484         toolbar = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
485                                              "/ToolBar");
486         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), toolbar,
487                             FALSE, FALSE, 0);
488
489         ev_window->priv->statusbar = gtk_statusbar_new ();
490         gtk_widget_show (ev_window->priv->statusbar);
491         gtk_box_pack_end (GTK_BOX (ev_window->priv->main_box),
492                           ev_window->priv->statusbar,
493                           FALSE, TRUE, 0);
494         ev_window->priv->help_message_cid = gtk_statusbar_get_context_id
495                 (GTK_STATUSBAR (ev_window->priv->statusbar), "help_message");
496
497 }