]> www.fi.muni.cz Git - evince.git/blob - shell/ev-window.c
4e1cc65008d6a503f58a4e411971fff9e3ca90b6
[evince.git] / shell / ev-window.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /* this file is part of evince, a gnome document viewer
3  *
4  *  Copyright (C) 2004 Martin Kretzschmar
5  *  Copyright (C) 2004 Red Hat, Inc.
6  *  Copyright (C) 2000, 2001, 2002, 2003, 2004 Marco Pesenti Gritti
7  *  Copyright (C) 2003, 2004 Christian Persch
8  *
9  *  Author:
10  *    Martin Kretzschmar <martink@gnome.org>
11  *
12  * Evince is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * Evince is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include "ev-window.h"
32 #include "ev-sidebar.h"
33 #include "ev-sidebar-bookmarks.h"
34 #include "ev-sidebar-thumbnails.h"
35 #include "ev-view.h"
36 #include "eggfindbar.h"
37
38 #include "pdf-document.h"
39 #include "pixbuf-document.h"
40 #include "gtkgs.h"
41
42 #include <glib/gi18n.h>
43 #include <gtk/gtk.h>
44 #include <libgnomevfs/gnome-vfs-mime-utils.h>
45
46 #include <string.h>
47
48 #include "ev-application.h"
49 #include "ev-stock-icons.h"
50
51 enum {
52         PROP_0,
53         PROP_ATTRIBUTE
54 };
55
56 enum {
57         SIGNAL,
58         N_SIGNALS
59 };
60
61 struct _EvWindowPrivate {
62         GtkWidget *main_box;
63         GtkWidget *hpaned;
64         GtkWidget *sidebar;
65         GtkWidget *find_bar;
66         GtkWidget *view;
67         GtkActionGroup *action_group;
68         GtkUIManager *ui_manager;
69         GtkWidget *statusbar;
70         guint help_message_cid;
71         GtkWidget *exit_fullscreen_popup;
72
73         EvDocument *document;
74
75         gboolean fullscreen_mode;
76 };
77
78 #if 0
79 /* enable these to add support for signals */
80 static guint ev_window_signals [N_SIGNALS] = { 0 };
81 #endif
82
83 static void update_fullscreen_popup (EvWindow *window);
84
85 static GObjectClass *parent_class = NULL;
86
87 G_DEFINE_TYPE (EvWindow, ev_window, GTK_TYPE_WINDOW)
88
89 #define EV_WINDOW_GET_PRIVATE(object) \
90         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_WINDOW, EvWindowPrivate))
91
92 #if 0
93 const char *
94 ev_window_get_attribute (EvWindow *self)
95 {
96         g_return_val_if_fail (self != NULL && EV_IS_WINDOW (self), NULL);
97         
98         return self->priv->attribute;
99 }
100
101 void
102 ev_window_set_attribute (EvWindow* self, const char *attribute)
103 {
104         g_assert (self != NULL && EV_IS_WINDOW (self));
105         g_assert (attribute != NULL);
106
107         if (self->priv->attribute != NULL) {
108                 g_free (self->priv->attribute);
109         }
110
111         self->priv->attribute = g_strdup (attribute);
112
113         g_object_notify (G_OBJECT (self), "attribute");
114 }
115
116 static void
117 ev_window_get_property (GObject *object, guint prop_id, GValue *value,
118                         GParamSpec *param_spec)
119 {
120         EvWindow *self;
121
122         self = EV_WINDOW (object);
123
124         switch (prop_id) {
125         case PROP_ATTRIBUTE:
126                 g_value_set_string (value, self->priv->attribute);
127                 break;
128         default:
129                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
130                                                    prop_id,
131                                                    param_spec);
132                 break;
133         }
134 }
135
136 static void
137 ev_window_set_property (GObject *object, guint prop_id, const GValue *value,
138                         GParamSpec *param_spec)
139 {
140         EvWindow *self;
141         
142         self = EV_WINDOW (object);
143         
144         switch (prop_id) {
145         case PROP_ATTRIBUTE:
146                 ev_window_set_attribute (self, g_value_get_string (value));
147                 break;
148         default:
149                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
150                                                    prop_id,
151                                                    param_spec);
152                 break;
153         }
154 }
155 #endif
156
157 static void
158 set_action_sensitive (EvWindow   *ev_window,
159                       const char *name,
160                       gboolean    sensitive)
161 {
162         GtkAction *action = gtk_action_group_get_action (ev_window->priv->action_group,
163                                                          name);
164         gtk_action_set_sensitive (action, sensitive);
165 }
166
167 static void
168 update_action_sensitivity (EvWindow *ev_window)
169 {
170         int n_pages;
171         int page;
172
173         if (ev_window->priv->document)
174                 n_pages = ev_document_get_n_pages (ev_window->priv->document);
175         else
176                 n_pages = 1;
177
178         page = ev_view_get_page (EV_VIEW (ev_window->priv->view));
179
180         set_action_sensitive (ev_window, "GoFirstPage", page > 1);
181         set_action_sensitive (ev_window, "GoPreviousPage", page > 1);
182         set_action_sensitive (ev_window, "GoNextPage", page < n_pages);
183         set_action_sensitive (ev_window, "GoLastPage", page < n_pages);
184 }
185
186 gboolean
187 ev_window_is_empty (const EvWindow *ev_window)
188 {
189         g_return_val_if_fail (EV_IS_WINDOW (ev_window), FALSE);
190         
191         return ev_window->priv->document == NULL;
192 }
193
194 static void
195 unable_to_load (EvWindow   *ev_window,
196                 const char *error_message)
197 {
198         GtkWidget *dialog;
199
200         dialog = gtk_message_dialog_new (GTK_WINDOW (ev_window),
201                                          GTK_DIALOG_DESTROY_WITH_PARENT,
202                                          GTK_MESSAGE_ERROR,
203                                          GTK_BUTTONS_CLOSE,
204                                          _("Unable to open document"));
205         gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
206                                                   "%s", error_message);
207         gtk_dialog_run (GTK_DIALOG (dialog));
208         gtk_widget_destroy (dialog);
209 }
210
211 /* Would be nice to have this in gdk-pixbuf */
212 static gboolean
213 mime_type_supported_by_gdk_pixbuf (const gchar *mime_type)
214 {
215         GSList *formats, *list;
216         gboolean retval = FALSE;
217         
218         formats = gdk_pixbuf_get_formats ();
219         
220         list = formats;
221         while (list) {
222                 GdkPixbufFormat *format = list->data;
223                 int i;
224                 gchar **mime_types;
225                 
226                 if (gdk_pixbuf_format_is_disabled (format))
227                         continue;
228
229                 mime_types = gdk_pixbuf_format_get_mime_types (format);
230                 
231                 for (i = 0; mime_types[i] != NULL; i++) {
232                         if (strcmp (mime_types[i], mime_type) == 0) {
233                                 retval = TRUE;
234                                 break;
235                         }
236                 }
237                 
238                 if (retval)
239                         break;
240                 
241                 list = list->next;
242         }
243         
244         g_slist_free (formats);
245
246         return retval;
247 }
248
249 void
250 ev_window_open (EvWindow *ev_window, const char *uri)
251 {
252         EvDocument *document = NULL;
253         char *mime_type;
254
255         mime_type = gnome_vfs_get_mime_type (uri);
256
257         if (!strcmp (mime_type, "application/pdf"))
258                 document = g_object_new (PDF_TYPE_DOCUMENT, NULL);
259         else if (!strcmp (mime_type, "application/postscript"))
260                 document = g_object_new (GTK_GS_TYPE, NULL);
261         else if (mime_type_supported_by_gdk_pixbuf (mime_type))
262                 document = g_object_new (PIXBUF_TYPE_DOCUMENT, NULL);
263         
264         if (document) {
265                 GError *error = NULL;
266
267                 if (ev_document_load (document, uri, &error)) {
268                         if (ev_window->priv->document)
269                                 g_object_unref (ev_window->priv->document);
270                         ev_window->priv->document = document;
271
272                         ev_view_set_document (EV_VIEW (ev_window->priv->view),
273                                               document);
274                         ev_sidebar_set_document (EV_SIDEBAR (ev_window->priv->sidebar),
275                                                  document);
276
277                         update_action_sensitivity (ev_window);
278                 
279                 } else {
280                         g_assert (error != NULL);
281                         g_object_unref (document);
282                         unable_to_load (ev_window, error->message);
283                         g_error_free (error);
284                 }
285         } else {
286                 char *error_message;
287
288                 error_message = g_strdup_printf (_("Unhandled MIME type: '%s'"),
289                                                  mime_type);
290                 unable_to_load (ev_window, error_message);
291                 g_free (error_message);
292         }
293
294         g_free (mime_type);
295 }
296
297 static void
298 ev_window_cmd_file_open (GtkAction *action, EvWindow *ev_window)
299 {
300         ev_application_open (EV_APP, NULL);
301 }
302
303 static void
304 ev_window_cmd_file_print (GtkAction *action, EvWindow *ev_window)
305 {
306         g_return_if_fail (EV_IS_WINDOW (ev_window));
307         /* FIXME */
308 }
309
310 static void
311 ev_window_cmd_file_close_window (GtkAction *action, EvWindow *ev_window)
312 {
313         g_return_if_fail (EV_IS_WINDOW (ev_window));
314
315         gtk_widget_destroy (GTK_WIDGET (ev_window));
316 }
317
318 static void
319 ev_window_cmd_edit_find (GtkAction *action, EvWindow *ev_window)
320 {
321         g_return_if_fail (EV_IS_WINDOW (ev_window));
322
323         gtk_widget_show (ev_window->priv->find_bar);
324
325         if (ev_window->priv->exit_fullscreen_popup) 
326                 update_fullscreen_popup (ev_window);
327         
328         egg_find_bar_grab_focus (EGG_FIND_BAR (ev_window->priv->find_bar));
329 }
330
331 static void
332 ev_window_cmd_edit_copy (GtkAction *action, EvWindow *ev_window)
333 {
334         g_return_if_fail (EV_IS_WINDOW (ev_window));
335
336         /* FIXME */
337 }
338
339 static void
340 update_fullscreen_popup (EvWindow *window)
341 {
342         GtkWidget *popup = window->priv->exit_fullscreen_popup;
343         int popup_width, popup_height;
344         GdkRectangle screen_rect;
345
346         g_return_if_fail (popup != NULL);
347
348         if (!popup)
349                 return;
350         
351         popup_width = popup->requisition.width;
352         popup_height = popup->requisition.height;
353
354         /* FIXME multihead */
355         gdk_screen_get_monitor_geometry (gdk_screen_get_default (),
356                         gdk_screen_get_monitor_at_window
357                         (gdk_screen_get_default (),
358                          GTK_WIDGET (window)->window),
359                          &screen_rect);
360
361         if (GTK_WIDGET_VISIBLE (window->priv->find_bar)) {
362                 GtkRequisition req;
363
364                 gtk_widget_size_request (window->priv->find_bar, &req);
365                 
366                 screen_rect.height -= req.height;
367         }
368         
369         if (gtk_widget_get_direction (popup) == GTK_TEXT_DIR_RTL)
370         {
371                 gtk_window_move (GTK_WINDOW (popup),
372                                  screen_rect.x + screen_rect.width - popup_width,
373                                  screen_rect.height - popup_height);
374         }
375         else
376         {
377                 gtk_window_move (GTK_WINDOW (popup),
378                                 screen_rect.x, screen_rect.height - popup_height);
379         }
380 }
381
382 static void
383 screen_size_changed_cb (GdkScreen *screen,
384                         EvWindow *window)
385 {
386         update_fullscreen_popup (window);
387 }
388
389 static void
390 destroy_exit_fullscreen_popup (EvWindow *window)
391 {
392         if (window->priv->exit_fullscreen_popup != NULL)
393         {
394                 /* FIXME multihead */
395                 g_signal_handlers_disconnect_by_func
396                         (gdk_screen_get_default (),
397                          G_CALLBACK (screen_size_changed_cb), window);
398
399                 gtk_widget_destroy (window->priv->exit_fullscreen_popup);
400                 window->priv->exit_fullscreen_popup = NULL;
401         }
402 }
403
404 static void
405 exit_fullscreen_button_clicked_cb (GtkWidget *button, EvWindow *window)
406 {
407         GtkAction *action;
408
409         action = gtk_action_group_get_action (window->priv->action_group, "ViewFullscreen");
410         g_return_if_fail (action != NULL);
411
412         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), FALSE);
413 }
414
415 static void
416 fullscreen_popup_size_request_cb (GtkWidget *popup, GtkRequisition *req, EvWindow *window)
417 {
418         update_fullscreen_popup (window);
419 }
420
421 static void
422 ev_window_fullscreen (EvWindow *window)
423 {
424         GtkWidget *popup, *button, *icon, *label, *hbox, *main_menu;
425
426         window->priv->fullscreen_mode = TRUE;
427
428         popup = gtk_window_new (GTK_WINDOW_POPUP);
429         window->priv->exit_fullscreen_popup = popup;
430
431         button = gtk_button_new ();
432         g_signal_connect (button, "clicked",
433                           G_CALLBACK (exit_fullscreen_button_clicked_cb),
434                           window);
435         gtk_widget_show (button);
436         gtk_container_add (GTK_CONTAINER (popup), button);
437
438         hbox = gtk_hbox_new (FALSE, 2);
439         gtk_widget_show (hbox);
440         gtk_container_add (GTK_CONTAINER (button), hbox);
441
442         icon = gtk_image_new_from_stock (GTK_STOCK_QUIT, GTK_ICON_SIZE_BUTTON);
443         gtk_widget_show (icon);
444         gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);
445
446         label = gtk_label_new (_("Exit Fullscreen"));
447         gtk_widget_show (label);
448         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
449
450         gtk_window_set_resizable (GTK_WINDOW (popup), FALSE);
451
452         /* FIXME multihead */
453         g_signal_connect (gdk_screen_get_default (), "size-changed",
454                           G_CALLBACK (screen_size_changed_cb), window);
455         g_signal_connect (popup, "size_request",
456                           G_CALLBACK (fullscreen_popup_size_request_cb), window);
457
458         main_menu = gtk_ui_manager_get_widget (window->priv->ui_manager, "/MainMenu");
459         gtk_widget_hide (main_menu);
460         gtk_widget_hide (window->priv->statusbar);
461         
462         update_fullscreen_popup (window);
463
464         gtk_widget_show (popup);
465 }
466
467 static void
468 ev_window_unfullscreen (EvWindow *window)
469 {
470         GtkWidget *main_menu;
471         
472         window->priv->fullscreen_mode = FALSE;
473
474         main_menu = gtk_ui_manager_get_widget (window->priv->ui_manager, "/MainMenu");
475         gtk_widget_show (main_menu);
476         gtk_widget_show (window->priv->statusbar);
477         
478         destroy_exit_fullscreen_popup (window);
479 }
480  
481 static void
482 ev_window_cmd_view_fullscreen (GtkAction *action, EvWindow *ev_window)
483 {
484         gboolean fullscreen;
485         
486         g_return_if_fail (EV_IS_WINDOW (ev_window));
487
488         fullscreen = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
489
490         if (fullscreen) {
491                 gtk_window_fullscreen (GTK_WINDOW (ev_window));
492         } else {
493                 gtk_window_unfullscreen (GTK_WINDOW (ev_window));
494         }
495 }
496
497 static gboolean
498 ev_window_state_event_cb (GtkWidget *widget, GdkEventWindowState *event, EvWindow *window)
499 {
500         if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
501         {
502                 GtkActionGroup *action_group;
503                 GtkAction *action;
504                 gboolean fullscreen;
505
506                 fullscreen = event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN;
507
508                 if (fullscreen)
509                 {
510                         ev_window_fullscreen (window);
511                 }
512                 else
513                 {
514                         ev_window_unfullscreen (window);
515                 }
516
517                 action_group = window->priv->action_group;
518
519                 action = gtk_action_group_get_action (action_group, "ViewFullscreen");
520                 g_signal_handlers_block_by_func
521                         (action, G_CALLBACK (ev_window_cmd_view_fullscreen), window);
522                 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), fullscreen);
523                 g_signal_handlers_unblock_by_func
524                         (action, G_CALLBACK (ev_window_cmd_view_fullscreen), window);
525
526         }
527
528         return FALSE;
529 }
530
531 static gboolean
532 ev_window_focus_out_cb (GtkWidget *widget, GdkEventFocus *event, EvWindow *ev_window)
533 {
534         gtk_window_unfullscreen (GTK_WINDOW (ev_window));
535         
536         return FALSE;
537 }
538
539
540 static void
541 ev_window_cmd_view_zoom_in (GtkAction *action, EvWindow *ev_window)
542 {
543         g_return_if_fail (EV_IS_WINDOW (ev_window));
544
545         ev_view_zoom_in (EV_VIEW (ev_window->priv->view));
546 }
547
548 static void
549 ev_window_cmd_view_zoom_out (GtkAction *action, EvWindow *ev_window)
550 {
551         g_return_if_fail (EV_IS_WINDOW (ev_window));
552
553         ev_view_zoom_out (EV_VIEW (ev_window->priv->view));
554 }
555
556 static void
557 ev_window_cmd_view_normal_size (GtkAction *action, EvWindow *ev_window)
558 {
559         g_return_if_fail (EV_IS_WINDOW (ev_window));
560
561         ev_view_normal_size (EV_VIEW (ev_window->priv->view));
562 }
563
564 static void
565 ev_window_cmd_view_best_fit (GtkAction *action, EvWindow *ev_window)
566 {
567         g_return_if_fail (EV_IS_WINDOW (ev_window));
568
569         ev_view_best_fit (EV_VIEW (ev_window->priv->view));
570 }
571
572 static void
573 ev_window_cmd_view_page_width (GtkAction *action, EvWindow *ev_window)
574 {
575         g_return_if_fail (EV_IS_WINDOW (ev_window));
576
577         ev_view_fit_width (EV_VIEW (ev_window->priv->view));
578 }
579
580 static void
581 ev_window_cmd_go_back (GtkAction *action, EvWindow *ev_window)
582 {
583         g_return_if_fail (EV_IS_WINDOW (ev_window));
584
585         /* FIXME */
586 }
587
588 static void
589 ev_window_cmd_go_forward (GtkAction *action, EvWindow *ev_window)
590 {
591         g_return_if_fail (EV_IS_WINDOW (ev_window));
592
593         /* FIXME */
594 }
595
596 static void
597 ev_window_cmd_go_previous_page (GtkAction *action, EvWindow *ev_window)
598 {
599         g_return_if_fail (EV_IS_WINDOW (ev_window));
600
601         ev_view_set_page (EV_VIEW (ev_window->priv->view),
602                           ev_view_get_page (EV_VIEW (ev_window->priv->view)) - 1);
603 }
604
605 static void
606 ev_window_cmd_go_next_page (GtkAction *action, EvWindow *ev_window)
607 {
608         g_return_if_fail (EV_IS_WINDOW (ev_window));
609
610         ev_view_set_page (EV_VIEW (ev_window->priv->view),
611                           ev_view_get_page (EV_VIEW (ev_window->priv->view)) + 1);
612 }
613
614 static void
615 ev_window_cmd_go_first_page (GtkAction *action, EvWindow *ev_window)
616 {
617         g_return_if_fail (EV_IS_WINDOW (ev_window));
618
619         ev_view_set_page (EV_VIEW (ev_window->priv->view), 1);
620 }
621
622 static void
623 ev_window_cmd_go_last_page (GtkAction *action, EvWindow *ev_window)
624 {
625         g_return_if_fail (EV_IS_WINDOW (ev_window));
626
627         ev_view_set_page (EV_VIEW (ev_window->priv->view), G_MAXINT);
628 }
629
630 static void
631 ev_window_cmd_help_contents (GtkAction *action, EvWindow *ev_window)
632 {
633         g_return_if_fail (EV_IS_WINDOW (ev_window));
634
635         /* FIXME */
636 }
637
638 static void
639 ev_window_cmd_help_about (GtkAction *action, EvWindow *ev_window)
640 {
641         const char *authors[] = {
642                 N_("Many..."),
643                 NULL
644         };
645
646         const char *documenters[] = {
647                 N_("Not so many..."),
648                 NULL
649         };
650
651         const char *license[] = {
652                 N_("Evince is free software; you can redistribute it and/or modify\n"
653                    "it under the terms of the GNU General Public License as published by\n"
654                    "the Free Software Foundation; either version 2 of the License, or\n"
655                    "(at your option) any later version.\n"),            
656                 N_("Evince is distributed in the hope that it will be useful,\n"
657                    "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
658                    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
659                    "GNU General Public License for more details.\n"),
660                 N_("You should have received a copy of the GNU General Public License\n"
661                    "along with Evince; if not, write to the Free Software Foundation, Inc.,\n"
662                    "59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n")
663         };
664
665         char *license_trans;
666
667 #ifdef ENABLE_NLS
668         const char **p;
669
670         for (p = authors; *p; ++p)
671                 *p = _(*p);
672
673         for (p = documenters; *p; ++p)
674                 *p = _(*p);
675 #endif
676
677         license_trans = g_strconcat (_(license[0]), "\n", _(license[1]), "\n",
678                                      _(license[2]), "\n", NULL);
679
680         gtk_show_about_dialog (
681                 GTK_WINDOW (ev_window),
682                 "name", _("Evince"),
683                 "version", VERSION,
684                 "copyright",
685                 _("\xc2\xa9 1996-2004 The Evince authors"),
686                 "license", license_trans,
687                 "website", "http://www.gnome.org/projects/evince",
688                 "comments", _("PostScript and PDF File Viewer."),
689                 "authors", authors,
690                 "documenters", documenters,
691                 "translator-credits", _("translator-credits"),
692                 NULL);
693
694         g_free (license_trans);
695 }
696
697 static void
698 ev_window_view_toolbar_cb (GtkAction *action, EvWindow *ev_window)
699 {
700         g_object_set (
701                 G_OBJECT (gtk_ui_manager_get_widget (
702                                   ev_window->priv->ui_manager,
703                                   "/ToolBar")),
704                 "visible",
705                 gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)),
706                 NULL);
707 }
708
709 static void
710 ev_window_view_statusbar_cb (GtkAction *action, EvWindow *ev_window)
711 {
712         g_object_set (
713                 ev_window->priv->statusbar,
714                 "visible",
715                 gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)),
716                 NULL);
717 }
718
719 static void
720 ev_window_view_sidebar_cb (GtkAction *action, EvWindow *ev_window)
721 {
722         /* FIXME */
723 }
724
725 static void
726 menu_item_select_cb (GtkMenuItem *proxy, EvWindow *ev_window)
727 {
728         GtkAction *action;
729         char *message;
730
731         action = g_object_get_data (G_OBJECT (proxy), "gtk-action");
732         g_return_if_fail (action != NULL);
733         
734         g_object_get (G_OBJECT (action), "tooltip", &message, NULL);
735         if (message) {
736                 gtk_statusbar_push (GTK_STATUSBAR (ev_window->priv->statusbar),
737                                     ev_window->priv->help_message_cid, message);
738                 g_free (message);
739         }
740 }
741
742 static void
743 menu_item_deselect_cb (GtkMenuItem *proxy, EvWindow *ev_window)
744 {
745         gtk_statusbar_pop (GTK_STATUSBAR (ev_window->priv->statusbar),
746                            ev_window->priv->help_message_cid);
747 }
748
749 static void
750 connect_proxy_cb (GtkUIManager *ui_manager, GtkAction *action,
751                   GtkWidget *proxy, EvWindow *ev_window)
752 {
753         if (GTK_IS_MENU_ITEM (proxy)) {
754                 g_signal_connect (proxy, "select",
755                                   G_CALLBACK (menu_item_select_cb), ev_window);
756                 g_signal_connect (proxy, "deselect",
757                                   G_CALLBACK (menu_item_deselect_cb),
758                                   ev_window);
759         }
760 }
761
762 static void
763 disconnect_proxy_cb (GtkUIManager *ui_manager, GtkAction *action,
764                      GtkWidget *proxy, EvWindow *ev_window)
765 {
766         if (GTK_IS_MENU_ITEM (proxy)) {
767                 g_signal_handlers_disconnect_by_func
768                         (proxy, G_CALLBACK (menu_item_select_cb), ev_window);
769                 g_signal_handlers_disconnect_by_func
770                         (proxy, G_CALLBACK (menu_item_deselect_cb), ev_window);
771         }
772 }
773
774 static void
775 view_page_changed_cb (EvView   *view,
776                       EvWindow *ev_window)
777 {
778         update_action_sensitivity (ev_window);
779 }
780
781 static void
782 find_bar_previous_cb (EggFindBar *find_bar,
783                       EvWindow   *ev_window)
784 {
785         /* FIXME - highlight previous result */
786         g_printerr ("Find Previous\n");
787
788 }
789
790 static void
791 find_bar_next_cb (EggFindBar *find_bar,
792                   EvWindow   *ev_window)
793 {
794         /* FIXME - highlight next result */
795         g_printerr ("Find Next\n");
796 }
797
798 static void
799 find_bar_close_cb (EggFindBar *find_bar,
800                    EvWindow   *ev_window)
801 {
802         gtk_widget_hide (ev_window->priv->find_bar);
803
804         if (ev_window->priv->exit_fullscreen_popup)
805                 update_fullscreen_popup (ev_window);
806 }
807
808 static void
809 find_bar_search_changed_cb (EggFindBar *find_bar,
810                             GParamSpec *param,
811                             EvWindow   *ev_window)
812 {
813         gboolean case_sensitive;
814         gboolean visible;
815         const char *search_string;
816
817         g_return_if_fail (EV_IS_WINDOW (ev_window));
818         
819         /* Either the string or case sensitivity could have changed,
820          * we connect this callback to both. We also connect it
821          * to ::visible so when the find bar is hidden, we should
822          * pretend the search string is NULL/""
823          */
824
825         case_sensitive = egg_find_bar_get_case_sensitive (find_bar);
826         visible = GTK_WIDGET_VISIBLE (find_bar);
827         search_string = egg_find_bar_get_search_string (find_bar);
828         
829 #if 0
830         g_printerr ("search for '%s'\n", search_string ? search_string : "(nil)");
831 #endif
832
833         /* We don't require begin/end find calls to be matched up, it's really
834          * start_find and cancel_any_find_that_may_not_be_finished
835          */
836         if (visible && search_string) {
837                 ev_document_begin_find (ev_window->priv->document, search_string, case_sensitive);
838         } else {
839                 ev_document_end_find (ev_window->priv->document);
840         }
841 }
842
843 static void
844 ev_window_dispose (GObject *object)
845 {
846         EvWindowPrivate *priv;
847
848         g_return_if_fail (object != NULL && EV_IS_WINDOW (object));
849
850         priv = EV_WINDOW (object)->priv;
851
852         if (priv->ui_manager) {
853                 g_object_unref (priv->ui_manager);
854                 priv->ui_manager = NULL;
855         }
856
857         if (priv->action_group) {
858                 g_object_unref (priv->action_group);
859                 priv->action_group = NULL;
860         }
861         
862         G_OBJECT_CLASS (parent_class)->dispose (object);
863 }
864
865 static void
866 ev_window_class_init (EvWindowClass *ev_window_class)
867 {
868         GObjectClass *g_object_class;
869
870         parent_class = g_type_class_peek_parent (ev_window_class);
871
872         g_object_class = G_OBJECT_CLASS (ev_window_class);
873         g_object_class->dispose = ev_window_dispose;
874
875         g_type_class_add_private (g_object_class, sizeof (EvWindowPrivate));
876
877 #if 0
878         /* setting up signal system */
879         ev_window_class->signal = ev_window_signal;
880
881         ev_window_signals [SIGNAL] = g_signal_new (
882                 "signal",
883                 EV_TYPE_WINDOW,
884                 G_SIGNAL_RUN_LAST,
885                 G_STRUCT_OFFSET (EvWindowClass,
886                                  signal),
887                 NULL,
888                 NULL,
889                 g_cclosure_marshal_VOID__STRING,
890                 G_TYPE_NONE,
891                 0);
892         /* setting up property system */
893         g_object_class->set_property = ev_window_set_property;
894         g_object_class->get_property = ev_window_get_property;
895
896         g_object_class_install_property (
897                 g_object_class,
898                 PROP_ATTRIBUTE,
899                 g_param_spec_string ("attribute",
900                                      "Attribute",
901                                      "A simple unneccessary attribute that "
902                                      "does nothing special except being a "
903                                      "demonstration for the correct implem"
904                                      "entation of a GObject property",
905                                      "default_value",
906                                      G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
907 #endif
908 }
909
910 /* Normal items */
911 static GtkActionEntry entries[] = {
912         { "File", NULL, N_("_File") },
913         { "Edit", NULL, N_("_Edit") },
914         { "View", NULL, N_("_View") },
915         { "Go", NULL, N_("_Go") },
916         { "Help", NULL, N_("_Help") },
917
918         /* File menu */
919         { "FileOpen", GTK_STOCK_OPEN, N_("_Open"), "<control>O",
920           N_("Open a file"),
921           G_CALLBACK (ev_window_cmd_file_open) },
922         { "FilePrint", GTK_STOCK_PRINT, N_("_Print"), "<control>P",
923           N_("Print this document"),
924           G_CALLBACK (ev_window_cmd_file_print) },
925         { "FileCloseWindow", GTK_STOCK_CLOSE, N_("_Close"), "<control>W",
926           N_("Close this window"),
927           G_CALLBACK (ev_window_cmd_file_close_window) },
928
929         /* Edit menu */
930         { "EditCopy", GTK_STOCK_COPY, N_("_Copy"), "<control>C",
931           N_("Copy text from the document"),
932           G_CALLBACK (ev_window_cmd_edit_copy) },
933         
934         { "EditFind", GTK_STOCK_FIND, N_("_Find"), "<control>F",
935           N_("Find a word or phrase in the document"),
936           G_CALLBACK (ev_window_cmd_edit_find) },
937
938         /* View menu */
939         { "ViewZoomIn", GTK_STOCK_ZOOM_IN, N_("Zoom _In"), "<control>plus",
940           N_("Enlarge the document"),
941           G_CALLBACK (ev_window_cmd_view_zoom_in) },
942         { "ViewZoomOut", GTK_STOCK_ZOOM_OUT, N_("Zoom _Out"), "<control>minus",
943           N_("Shrink the document"),
944           G_CALLBACK (ev_window_cmd_view_zoom_out) },
945         { "ViewNormalSize", GTK_STOCK_ZOOM_100, N_("_Normal Size"), "<control>0",
946           N_("Zoom to the normal size"),
947           G_CALLBACK (ev_window_cmd_view_normal_size) },
948         { "ViewBestFit", GTK_STOCK_ZOOM_FIT, N_("_Best Fit"), NULL,
949           N_("Zoom to fit the document to the current window"),
950           G_CALLBACK (ev_window_cmd_view_best_fit) },
951         { "ViewPageWidth", EV_STOCK_ZOOM_FIT_WIDTH, N_("Fit Page _Width"), NULL,
952           N_("Zoom to fit the width of the current window "),
953           G_CALLBACK (ev_window_cmd_view_page_width) },
954
955         /* Go menu */
956         { "GoBack", GTK_STOCK_GO_BACK, N_("_Back"), "<mod1>Left",
957           N_("Go to the page viewed before this one"),
958           G_CALLBACK (ev_window_cmd_go_back) },
959         { "GoForward", GTK_STOCK_GO_FORWARD, N_("Fo_rward"), "<mod1>Right",
960           N_("Go to the page viewed before this one"),
961           G_CALLBACK (ev_window_cmd_go_forward) },
962         { "GoPreviousPage", GTK_STOCK_GO_BACK, N_("_Previous Page"), "<control>Page_Up",
963           N_("Go to the previous page"),
964           G_CALLBACK (ev_window_cmd_go_previous_page) },
965         { "GoNextPage", GTK_STOCK_GO_FORWARD, N_("_Next Page"), "<control>Page_Down",
966           N_("Go to the next page"),
967           G_CALLBACK (ev_window_cmd_go_next_page) },
968         { "GoFirstPage", GTK_STOCK_GOTO_FIRST, N_("_First Page"), "<control>Home",
969           N_("Go to the first page"),
970           G_CALLBACK (ev_window_cmd_go_first_page) },        
971         { "GoLastPage", GTK_STOCK_GOTO_LAST, N_("_Last Page"), "<control>End",
972           N_("Go to the last page"),
973           G_CALLBACK (ev_window_cmd_go_last_page) },
974         
975         /* Help menu */
976         { "HelpContents", GTK_STOCK_HELP, N_("_Contents"), NULL,
977           N_("Display help for the viewer application"),
978           G_CALLBACK (ev_window_cmd_help_contents) },
979         
980         { "HelpAbout", GTK_STOCK_ABOUT, N_("_About"), NULL,
981           N_("Display credits for the document viewer creators"),
982           G_CALLBACK (ev_window_cmd_help_about) },
983 };
984
985 /* Toggle items */
986 static GtkToggleActionEntry toggle_entries[] = {
987         /* View Menu */
988         { "ViewToolbar", NULL, N_("_Toolbar"), "<shift><control>T",
989           N_("Show or hide toolbar"),
990           G_CALLBACK (ev_window_view_toolbar_cb), TRUE },
991         { "ViewStatusbar", NULL, N_("_Statusbar"), NULL,
992           N_("Show or hide statusbar"),
993           G_CALLBACK (ev_window_view_statusbar_cb), TRUE },
994         { "ViewSidebar", NULL, N_("Side_bar"), "F9",
995           N_("Show or hide sidebar"),
996           G_CALLBACK (ev_window_view_sidebar_cb), FALSE },
997         { "ViewFullscreen", NULL, N_("_Fullscreen"), "F11",
998           N_("Expand the window to fill the screen"),
999           G_CALLBACK (ev_window_cmd_view_fullscreen) },
1000 };
1001
1002 static void
1003 ev_window_init (EvWindow *ev_window)
1004 {
1005         GtkActionGroup *action_group;
1006         GtkAccelGroup *accel_group;
1007         GError *error = NULL;
1008         GtkWidget *scrolled_window;
1009         GtkWidget *menubar;
1010         GtkWidget *toolbar;
1011         GtkWidget *sidebar_widget;
1012
1013         ev_window->priv = EV_WINDOW_GET_PRIVATE (ev_window);
1014
1015         gtk_window_set_title (GTK_WINDOW (ev_window), _("Document Viewer"));
1016
1017         ev_window->priv->main_box = gtk_vbox_new (FALSE, 0);
1018         gtk_container_add (GTK_CONTAINER (ev_window), ev_window->priv->main_box);
1019         gtk_widget_show (ev_window->priv->main_box);
1020         
1021         action_group = gtk_action_group_new ("MenuActions");
1022         ev_window->priv->action_group = action_group;
1023         gtk_action_group_set_translation_domain (action_group, NULL);
1024         gtk_action_group_add_actions (action_group, entries,
1025                                       G_N_ELEMENTS (entries), ev_window);
1026         gtk_action_group_add_toggle_actions (action_group, toggle_entries,
1027                                              G_N_ELEMENTS (toggle_entries),
1028                                              ev_window);
1029
1030         ev_window->priv->ui_manager = gtk_ui_manager_new ();
1031         gtk_ui_manager_insert_action_group (ev_window->priv->ui_manager,
1032                                             action_group, 0);
1033
1034         accel_group =
1035                 gtk_ui_manager_get_accel_group (ev_window->priv->ui_manager);
1036         gtk_window_add_accel_group (GTK_WINDOW (ev_window), accel_group);
1037
1038         g_signal_connect (ev_window->priv->ui_manager, "connect_proxy",
1039                           G_CALLBACK (connect_proxy_cb), ev_window);
1040         g_signal_connect (ev_window->priv->ui_manager, "disconnect_proxy",
1041                           G_CALLBACK (disconnect_proxy_cb), ev_window);
1042
1043         if (!gtk_ui_manager_add_ui_from_file (ev_window->priv->ui_manager,
1044                                               DATADIR"/evince-ui.xml",
1045                                               &error)) {
1046                 g_message ("building menus failed: %s", error->message);
1047                 g_error_free (error);
1048         }
1049
1050         menubar = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
1051                                              "/MainMenu");
1052         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), menubar,
1053                             FALSE, FALSE, 0);
1054
1055         toolbar = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
1056                                              "/ToolBar");
1057         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), toolbar,
1058                             FALSE, FALSE, 0);
1059
1060         /* Add the main area */
1061         ev_window->priv->hpaned = gtk_hpaned_new ();
1062         gtk_widget_show (ev_window->priv->hpaned);
1063         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), ev_window->priv->hpaned,
1064                             TRUE, TRUE, 0);
1065
1066         ev_window->priv->sidebar = ev_sidebar_new ();
1067         gtk_widget_show (ev_window->priv->sidebar);
1068         gtk_paned_add1 (GTK_PANED (ev_window->priv->hpaned),
1069                         ev_window->priv->sidebar);
1070
1071         /* Stub sidebar, for now */
1072         sidebar_widget = ev_sidebar_bookmarks_new ();
1073         gtk_widget_show (sidebar_widget);
1074         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
1075                              "bookmarks",
1076                              _("Bookmarks"),
1077                              sidebar_widget);
1078
1079         sidebar_widget = ev_sidebar_thumbnails_new ();
1080         gtk_widget_show (sidebar_widget);
1081         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
1082                              "thumbnails",
1083                              _("Thumbnails"),
1084                              sidebar_widget);
1085         
1086         scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1087         gtk_widget_show (scrolled_window);
1088         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
1089                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1090
1091         gtk_paned_add2 (GTK_PANED (ev_window->priv->hpaned),
1092                         scrolled_window);
1093
1094         ev_window->priv->view = ev_view_new ();
1095         gtk_widget_show (ev_window->priv->view);
1096         gtk_container_add (GTK_CONTAINER (scrolled_window),
1097                            ev_window->priv->view);
1098         g_signal_connect (ev_window->priv->view,
1099                           "page-changed",
1100                           G_CALLBACK (view_page_changed_cb),
1101                           ev_window);
1102
1103         ev_window->priv->statusbar = gtk_statusbar_new ();
1104         gtk_widget_show (ev_window->priv->statusbar);
1105         gtk_box_pack_end (GTK_BOX (ev_window->priv->main_box),
1106                           ev_window->priv->statusbar,
1107                           FALSE, TRUE, 0);
1108         ev_window->priv->help_message_cid = gtk_statusbar_get_context_id
1109                 (GTK_STATUSBAR (ev_window->priv->statusbar), "help_message");
1110
1111         ev_window->priv->find_bar = egg_find_bar_new ();
1112         gtk_box_pack_end (GTK_BOX (ev_window->priv->main_box),
1113                           ev_window->priv->find_bar,
1114                           FALSE, TRUE, 0);
1115         
1116         /* Connect to find bar signals */
1117         g_signal_connect (ev_window->priv->find_bar,
1118                           "previous",
1119                           G_CALLBACK (find_bar_previous_cb),
1120                           ev_window);
1121         g_signal_connect (ev_window->priv->find_bar,
1122                           "next",
1123                           G_CALLBACK (find_bar_next_cb),
1124                           ev_window);
1125         g_signal_connect (ev_window->priv->find_bar,
1126                           "close",
1127                           G_CALLBACK (find_bar_close_cb),
1128                           ev_window);
1129         g_signal_connect (ev_window->priv->find_bar,
1130                           "notify::search-string",
1131                           G_CALLBACK (find_bar_search_changed_cb),
1132                           ev_window);
1133         g_signal_connect (ev_window->priv->find_bar,
1134                           "notify::case-sensitive",
1135                           G_CALLBACK (find_bar_search_changed_cb),
1136                           ev_window);
1137         g_signal_connect (ev_window->priv->find_bar,
1138                           "notify::visible",
1139                           G_CALLBACK (find_bar_search_changed_cb),
1140                           ev_window);
1141
1142         g_signal_connect (ev_window, "window-state-event",
1143                           G_CALLBACK (ev_window_state_event_cb),
1144                           ev_window);
1145         g_signal_connect (ev_window, "focus_out_event",
1146                           G_CALLBACK (ev_window_focus_out_cb),
1147                           ev_window);
1148         
1149         /* Give focus to the scrolled window */
1150         gtk_widget_grab_focus (scrolled_window);
1151         
1152         update_action_sensitivity (ev_window);
1153