]> www.fi.muni.cz Git - evince.git/blob - shell/ev-window.c
Fix bugs in fullscreen code.
[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
275                         update_action_sensitivity (ev_window);
276                 
277                 } else {
278                         g_assert (error != NULL);
279                         g_object_unref (document);
280                         unable_to_load (ev_window, error->message);
281                         g_error_free (error);
282                 }
283         } else {
284                 char *error_message;
285
286                 error_message = g_strdup_printf (_("Unhandled MIME type: '%s'"),
287                                                  mime_type);
288                 unable_to_load (ev_window, error_message);
289                 g_free (error_message);
290         }
291
292         g_free (mime_type);
293 }
294
295 static void
296 ev_window_cmd_file_open (GtkAction *action, EvWindow *ev_window)
297 {
298         ev_application_open (EV_APP, NULL);
299 }
300
301 static void
302 ev_window_cmd_file_print (GtkAction *action, EvWindow *ev_window)
303 {
304         g_return_if_fail (EV_IS_WINDOW (ev_window));
305         /* FIXME */
306 }
307
308 static void
309 ev_window_cmd_file_close_window (GtkAction *action, EvWindow *ev_window)
310 {
311         g_return_if_fail (EV_IS_WINDOW (ev_window));
312
313         gtk_widget_destroy (GTK_WIDGET (ev_window));
314 }
315
316 static void
317 ev_window_cmd_edit_find (GtkAction *action, EvWindow *ev_window)
318 {
319         g_return_if_fail (EV_IS_WINDOW (ev_window));
320
321         gtk_widget_show (ev_window->priv->find_bar);
322
323         if (ev_window->priv->exit_fullscreen_popup) 
324                 update_fullscreen_popup (ev_window);
325         
326         egg_find_bar_grab_focus (EGG_FIND_BAR (ev_window->priv->find_bar));
327 }
328
329 static void
330 ev_window_cmd_edit_copy (GtkAction *action, EvWindow *ev_window)
331 {
332         g_return_if_fail (EV_IS_WINDOW (ev_window));
333
334         /* FIXME */
335 }
336
337 static void
338 update_fullscreen_popup (EvWindow *window)
339 {
340         GtkWidget *popup = window->priv->exit_fullscreen_popup;
341         int popup_width, popup_height;
342         GdkRectangle screen_rect;
343
344         g_return_if_fail (popup != NULL);
345
346         if (!popup)
347                 return;
348         
349         popup_width = popup->requisition.width;
350         popup_height = popup->requisition.height;
351
352         /* FIXME multihead */
353         gdk_screen_get_monitor_geometry (gdk_screen_get_default (),
354                         gdk_screen_get_monitor_at_window
355                         (gdk_screen_get_default (),
356                          GTK_WIDGET (window)->window),
357                          &screen_rect);
358
359         if (GTK_WIDGET_VISIBLE (window->priv->find_bar)) {
360                 GtkRequisition req;
361
362                 gtk_widget_size_request (window->priv->find_bar, &req);
363                 
364                 screen_rect.height -= req.height;
365         }
366         
367         if (gtk_widget_get_direction (popup) == GTK_TEXT_DIR_RTL)
368         {
369                 gtk_window_move (GTK_WINDOW (popup),
370                                  screen_rect.x + screen_rect.width - popup_width,
371                                  screen_rect.height - popup_height);
372         }
373         else
374         {
375                 gtk_window_move (GTK_WINDOW (popup),
376                                 screen_rect.x, screen_rect.height - popup_height);
377         }
378 }
379
380 static void
381 screen_size_changed_cb (GdkScreen *screen,
382                         EvWindow *window)
383 {
384         update_fullscreen_popup (window);
385 }
386
387 static void
388 destroy_exit_fullscreen_popup (EvWindow *window)
389 {
390         if (window->priv->exit_fullscreen_popup != NULL)
391         {
392                 /* FIXME multihead */
393                 g_signal_handlers_disconnect_by_func
394                         (gdk_screen_get_default (),
395                          G_CALLBACK (screen_size_changed_cb), window);
396
397                 gtk_widget_destroy (window->priv->exit_fullscreen_popup);
398                 window->priv->exit_fullscreen_popup = NULL;
399         }
400 }
401
402 static void
403 exit_fullscreen_button_clicked_cb (GtkWidget *button, EvWindow *window)
404 {
405         GtkAction *action;
406
407         action = gtk_action_group_get_action (window->priv->action_group, "ViewFullscreen");
408         g_return_if_fail (action != NULL);
409
410         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), FALSE);
411 }
412
413 static void
414 fullscreen_popup_size_request_cb (GtkWidget *popup, GtkRequisition *req, EvWindow *window)
415 {
416         update_fullscreen_popup (window);
417 }
418
419 static void
420 ev_window_fullscreen (EvWindow *window)
421 {
422         GtkWidget *popup, *button, *icon, *label, *hbox, *main_menu;
423
424         window->priv->fullscreen_mode = TRUE;
425
426         popup = gtk_window_new (GTK_WINDOW_POPUP);
427         window->priv->exit_fullscreen_popup = popup;
428
429         button = gtk_button_new ();
430         g_signal_connect (button, "clicked",
431                           G_CALLBACK (exit_fullscreen_button_clicked_cb),
432                           window);
433         gtk_widget_show (button);
434         gtk_container_add (GTK_CONTAINER (popup), button);
435
436         hbox = gtk_hbox_new (FALSE, 2);
437         gtk_widget_show (hbox);
438         gtk_container_add (GTK_CONTAINER (button), hbox);
439
440         icon = gtk_image_new_from_stock (GTK_STOCK_QUIT, GTK_ICON_SIZE_BUTTON);
441         gtk_widget_show (icon);
442         gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);
443
444         label = gtk_label_new (_("Exit Fullscreen"));
445         gtk_widget_show (label);
446         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
447
448         gtk_window_set_resizable (GTK_WINDOW (popup), FALSE);
449
450         /* FIXME multihead */
451         g_signal_connect (gdk_screen_get_default (), "size-changed",
452                           G_CALLBACK (screen_size_changed_cb), window);
453         g_signal_connect (popup, "size_request",
454                           G_CALLBACK (fullscreen_popup_size_request_cb), window);
455
456         main_menu = gtk_ui_manager_get_widget (window->priv->ui_manager, "/MainMenu");
457         gtk_widget_hide (main_menu);
458         gtk_widget_hide (window->priv->statusbar);
459         
460         update_fullscreen_popup (window);
461
462         gtk_widget_show (popup);
463 }
464
465 static void
466 ev_window_unfullscreen (EvWindow *window)
467 {
468         GtkWidget *main_menu;
469         
470         window->priv->fullscreen_mode = FALSE;
471
472         main_menu = gtk_ui_manager_get_widget (window->priv->ui_manager, "/MainMenu");
473         gtk_widget_show (main_menu);
474         gtk_widget_show (window->priv->statusbar);
475         
476         destroy_exit_fullscreen_popup (window);
477 }
478  
479 static void
480 ev_window_cmd_view_fullscreen (GtkAction *action, EvWindow *ev_window)
481 {
482         gboolean fullscreen;
483         
484         g_return_if_fail (EV_IS_WINDOW (ev_window));
485
486         fullscreen = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
487
488         if (fullscreen) {
489                 gtk_window_fullscreen (GTK_WINDOW (ev_window));
490         } else {
491                 gtk_window_unfullscreen (GTK_WINDOW (ev_window));
492         }
493 }
494
495 static gboolean
496 ev_window_state_event_cb (GtkWidget *widget, GdkEventWindowState *event, EvWindow *window)
497 {
498         if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
499         {
500                 GtkActionGroup *action_group;
501                 GtkAction *action;
502                 gboolean fullscreen;
503
504                 fullscreen = event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN;
505
506                 if (fullscreen)
507                 {
508                         ev_window_fullscreen (window);
509                 }
510                 else
511                 {
512                         ev_window_unfullscreen (window);
513                 }
514
515                 action_group = window->priv->action_group;
516
517                 action = gtk_action_group_get_action (action_group, "ViewFullscreen");
518                 g_signal_handlers_block_by_func
519                         (action, G_CALLBACK (ev_window_cmd_view_fullscreen), window);
520                 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), fullscreen);
521                 g_signal_handlers_unblock_by_func
522                         (action, G_CALLBACK (ev_window_cmd_view_fullscreen), window);
523
524         }
525
526         return FALSE;
527 }
528
529 static gboolean
530 ev_window_focus_out_cb (GtkWidget *widget, GdkEventFocus *event, EvWindow *ev_window)
531 {
532         gtk_window_unfullscreen (GTK_WINDOW (ev_window));
533         
534         return FALSE;
535 }
536
537
538 static void
539 ev_window_cmd_view_zoom_in (GtkAction *action, EvWindow *ev_window)
540 {
541         g_return_if_fail (EV_IS_WINDOW (ev_window));
542
543         ev_view_zoom_in (EV_VIEW (ev_window->priv->view));
544 }
545
546 static void
547 ev_window_cmd_view_zoom_out (GtkAction *action, EvWindow *ev_window)
548 {
549         g_return_if_fail (EV_IS_WINDOW (ev_window));
550
551         ev_view_zoom_out (EV_VIEW (ev_window->priv->view));
552 }
553
554 static void
555 ev_window_cmd_view_normal_size (GtkAction *action, EvWindow *ev_window)
556 {
557         g_return_if_fail (EV_IS_WINDOW (ev_window));
558
559         ev_view_normal_size (EV_VIEW (ev_window->priv->view));
560 }
561
562 static void
563 ev_window_cmd_view_best_fit (GtkAction *action, EvWindow *ev_window)
564 {
565         g_return_if_fail (EV_IS_WINDOW (ev_window));
566
567         ev_view_best_fit (EV_VIEW (ev_window->priv->view));
568 }
569
570 static void
571 ev_window_cmd_view_page_width (GtkAction *action, EvWindow *ev_window)
572 {
573         g_return_if_fail (EV_IS_WINDOW (ev_window));
574
575         ev_view_fit_width (EV_VIEW (ev_window->priv->view));
576 }
577
578 static void
579 ev_window_cmd_go_back (GtkAction *action, EvWindow *ev_window)
580 {
581         g_return_if_fail (EV_IS_WINDOW (ev_window));
582
583         /* FIXME */
584 }
585
586 static void
587 ev_window_cmd_go_forward (GtkAction *action, EvWindow *ev_window)
588 {
589         g_return_if_fail (EV_IS_WINDOW (ev_window));
590
591         /* FIXME */
592 }
593
594 static void
595 ev_window_cmd_go_previous_page (GtkAction *action, EvWindow *ev_window)
596 {
597         g_return_if_fail (EV_IS_WINDOW (ev_window));
598
599         ev_view_set_page (EV_VIEW (ev_window->priv->view),
600                           ev_view_get_page (EV_VIEW (ev_window->priv->view)) - 1);
601 }
602
603 static void
604 ev_window_cmd_go_next_page (GtkAction *action, EvWindow *ev_window)
605 {
606         g_return_if_fail (EV_IS_WINDOW (ev_window));
607
608         ev_view_set_page (EV_VIEW (ev_window->priv->view),
609                           ev_view_get_page (EV_VIEW (ev_window->priv->view)) + 1);
610 }
611
612 static void
613 ev_window_cmd_go_first_page (GtkAction *action, EvWindow *ev_window)
614 {
615         g_return_if_fail (EV_IS_WINDOW (ev_window));
616
617         ev_view_set_page (EV_VIEW (ev_window->priv->view), 1);
618 }
619
620 static void
621 ev_window_cmd_go_last_page (GtkAction *action, EvWindow *ev_window)
622 {
623         g_return_if_fail (EV_IS_WINDOW (ev_window));
624
625         ev_view_set_page (EV_VIEW (ev_window->priv->view), G_MAXINT);
626 }
627
628 static void
629 ev_window_cmd_help_contents (GtkAction *action, EvWindow *ev_window)
630 {
631         g_return_if_fail (EV_IS_WINDOW (ev_window));
632
633         /* FIXME */
634 }
635
636 static void
637 ev_window_cmd_help_about (GtkAction *action, EvWindow *ev_window)
638 {
639         const char *authors[] = {
640                 N_("Many..."),
641                 NULL
642         };
643
644         const char *documenters[] = {
645                 N_("Not so many..."),
646                 NULL
647         };
648
649         const char *license[] = {
650                 N_("Evince is free software; you can redistribute it and/or modify\n"
651                    "it under the terms of the GNU General Public License as published by\n"
652                    "the Free Software Foundation; either version 2 of the License, or\n"
653                    "(at your option) any later version.\n"),            
654                 N_("Evince is distributed in the hope that it will be useful,\n"
655                    "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
656                    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
657                    "GNU General Public License for more details.\n"),
658                 N_("You should have received a copy of the GNU General Public License\n"
659                    "along with Evince; if not, write to the Free Software Foundation, Inc.,\n"
660                    "59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n")
661         };
662
663         char *license_trans;
664
665 #ifdef ENABLE_NLS
666         const char **p;
667
668         for (p = authors; *p; ++p)
669                 *p = _(*p);
670
671         for (p = documenters; *p; ++p)
672                 *p = _(*p);
673 #endif
674
675         license_trans = g_strconcat (_(license[0]), "\n", _(license[1]), "\n",
676                                      _(license[2]), "\n", NULL);
677
678         gtk_show_about_dialog (
679                 GTK_WINDOW (ev_window),
680                 "name", _("Evince"),
681                 "version", VERSION,
682                 "copyright",
683                 _("\xc2\xa9 1996-2004 The Evince authors"),
684                 "license", license_trans,
685                 "website", "http://www.gnome.org/projects/evince",
686                 "comments", _("PostScript and PDF File Viewer."),
687                 "authors", authors,
688                 "documenters", documenters,
689                 "translator-credits", _("translator-credits"),
690                 NULL);
691
692         g_free (license_trans);
693 }
694
695 static void
696 ev_window_view_toolbar_cb (GtkAction *action, EvWindow *ev_window)
697 {
698         g_object_set (
699                 G_OBJECT (gtk_ui_manager_get_widget (
700                                   ev_window->priv->ui_manager,
701                                   "/ToolBar")),
702                 "visible",
703                 gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)),
704                 NULL);
705 }
706
707 static void
708 ev_window_view_statusbar_cb (GtkAction *action, EvWindow *ev_window)
709 {
710         g_object_set (
711                 ev_window->priv->statusbar,
712                 "visible",
713                 gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)),
714                 NULL);
715 }
716
717 static void
718 ev_window_view_sidebar_cb (GtkAction *action, EvWindow *ev_window)
719 {
720         /* FIXME */
721 }
722
723 static void
724 menu_item_select_cb (GtkMenuItem *proxy, EvWindow *ev_window)
725 {
726         GtkAction *action;
727         char *message;
728
729         action = g_object_get_data (G_OBJECT (proxy), "gtk-action");
730         g_return_if_fail (action != NULL);
731         
732         g_object_get (G_OBJECT (action), "tooltip", &message, NULL);
733         if (message) {
734                 gtk_statusbar_push (GTK_STATUSBAR (ev_window->priv->statusbar),
735                                     ev_window->priv->help_message_cid, message);
736                 g_free (message);
737         }
738 }
739
740 static void
741 menu_item_deselect_cb (GtkMenuItem *proxy, EvWindow *ev_window)
742 {
743         gtk_statusbar_pop (GTK_STATUSBAR (ev_window->priv->statusbar),
744                            ev_window->priv->help_message_cid);
745 }
746
747 static void
748 connect_proxy_cb (GtkUIManager *ui_manager, GtkAction *action,
749                   GtkWidget *proxy, EvWindow *ev_window)
750 {
751         if (GTK_IS_MENU_ITEM (proxy)) {
752                 g_signal_connect (proxy, "select",
753                                   G_CALLBACK (menu_item_select_cb), ev_window);
754                 g_signal_connect (proxy, "deselect",
755                                   G_CALLBACK (menu_item_deselect_cb),
756                                   ev_window);
757         }
758 }
759
760 static void
761 disconnect_proxy_cb (GtkUIManager *ui_manager, GtkAction *action,
762                      GtkWidget *proxy, EvWindow *ev_window)
763 {
764         if (GTK_IS_MENU_ITEM (proxy)) {
765                 g_signal_handlers_disconnect_by_func
766                         (proxy, G_CALLBACK (menu_item_select_cb), ev_window);
767                 g_signal_handlers_disconnect_by_func
768                         (proxy, G_CALLBACK (menu_item_deselect_cb), ev_window);
769         }
770 }
771
772 static void
773 view_page_changed_cb (EvView   *view,
774                       EvWindow *ev_window)
775 {
776         update_action_sensitivity (ev_window);
777 }
778
779 static void
780 find_bar_previous_cb (EggFindBar *find_bar,
781                       EvWindow   *ev_window)
782 {
783         /* FIXME - highlight previous result */
784         g_printerr ("Find Previous\n");
785
786 }
787
788 static void
789 find_bar_next_cb (EggFindBar *find_bar,
790                   EvWindow   *ev_window)
791 {
792         /* FIXME - highlight next result */
793         g_printerr ("Find Next\n");
794 }
795
796 static void
797 find_bar_close_cb (EggFindBar *find_bar,
798                    EvWindow   *ev_window)
799 {
800         gtk_widget_hide (ev_window->priv->find_bar);
801
802         if (ev_window->priv->exit_fullscreen_popup)
803                 update_fullscreen_popup (ev_window);
804 }
805
806 static void
807 find_bar_search_changed_cb (EggFindBar *find_bar,
808                             GParamSpec *param,
809                             EvWindow   *ev_window)
810 {
811         gboolean case_sensitive;
812         gboolean visible;
813         const char *search_string;
814
815         g_return_if_fail (EV_IS_WINDOW (ev_window));
816         
817         /* Either the string or case sensitivity could have changed,
818          * we connect this callback to both. We also connect it
819          * to ::visible so when the find bar is hidden, we should
820          * pretend the search string is NULL/""
821          */
822
823         case_sensitive = egg_find_bar_get_case_sensitive (find_bar);
824         visible = GTK_WIDGET_VISIBLE (find_bar);
825         search_string = egg_find_bar_get_search_string (find_bar);
826         
827 #if 0
828         g_printerr ("search for '%s'\n", search_string ? search_string : "(nil)");
829 #endif
830
831         /* We don't require begin/end find calls to be matched up, it's really
832          * start_find and cancel_any_find_that_may_not_be_finished
833          */
834         if (visible && search_string) {
835                 ev_document_begin_find (ev_window->priv->document, search_string, case_sensitive);
836         } else {
837                 ev_document_end_find (ev_window->priv->document);
838         }
839 }
840
841 static void
842 ev_window_dispose (GObject *object)
843 {
844         EvWindowPrivate *priv;
845
846         g_return_if_fail (object != NULL && EV_IS_WINDOW (object));
847
848         priv = EV_WINDOW (object)->priv;
849
850         if (priv->ui_manager) {
851                 g_object_unref (priv->ui_manager);
852                 priv->ui_manager = NULL;
853         }
854
855         if (priv->action_group) {
856                 g_object_unref (priv->action_group);
857                 priv->action_group = NULL;
858         }
859         
860         G_OBJECT_CLASS (parent_class)->dispose (object);
861 }
862
863 static void
864 ev_window_class_init (EvWindowClass *ev_window_class)
865 {
866         GObjectClass *g_object_class;
867
868         parent_class = g_type_class_peek_parent (ev_window_class);
869
870         g_object_class = G_OBJECT_CLASS (ev_window_class);
871         g_object_class->dispose = ev_window_dispose;
872
873         g_type_class_add_private (g_object_class, sizeof (EvWindowPrivate));
874
875 #if 0
876         /* setting up signal system */
877         ev_window_class->signal = ev_window_signal;
878
879         ev_window_signals [SIGNAL] = g_signal_new (
880                 "signal",
881                 EV_TYPE_WINDOW,
882                 G_SIGNAL_RUN_LAST,
883                 G_STRUCT_OFFSET (EvWindowClass,
884                                  signal),
885                 NULL,
886                 NULL,
887                 g_cclosure_marshal_VOID__STRING,
888                 G_TYPE_NONE,
889                 0);
890         /* setting up property system */
891         g_object_class->set_property = ev_window_set_property;
892         g_object_class->get_property = ev_window_get_property;
893
894         g_object_class_install_property (
895                 g_object_class,
896                 PROP_ATTRIBUTE,
897                 g_param_spec_string ("attribute",
898                                      "Attribute",
899                                      "A simple unneccessary attribute that "
900                                      "does nothing special except being a "
901                                      "demonstration for the correct implem"
902                                      "entation of a GObject property",
903                                      "default_value",
904                                      G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
905 #endif
906 }
907
908 /* Normal items */
909 static GtkActionEntry entries[] = {
910         { "File", NULL, N_("_File") },
911         { "Edit", NULL, N_("_Edit") },
912         { "View", NULL, N_("_View") },
913         { "Go", NULL, N_("_Go") },
914         { "Help", NULL, N_("_Help") },
915
916         /* File menu */
917         { "FileOpen", GTK_STOCK_OPEN, N_("_Open"), "<control>O",
918           N_("Open a file"),
919           G_CALLBACK (ev_window_cmd_file_open) },
920         { "FilePrint", GTK_STOCK_PRINT, N_("_Print"), "<control>P",
921           N_("Print this document"),
922           G_CALLBACK (ev_window_cmd_file_print) },
923         { "FileCloseWindow", GTK_STOCK_CLOSE, N_("_Close"), "<control>W",
924           N_("Close this window"),
925           G_CALLBACK (ev_window_cmd_file_close_window) },
926
927         /* Edit menu */
928         { "EditCopy", GTK_STOCK_COPY, N_("_Copy"), "<control>C",
929           N_("Copy text from the document"),
930           G_CALLBACK (ev_window_cmd_edit_copy) },
931         
932         { "EditFind", GTK_STOCK_FIND, N_("_Find"), "<control>F",
933           N_("Find a word or phrase in the document"),
934           G_CALLBACK (ev_window_cmd_edit_find) },
935
936         /* View menu */
937         { "ViewZoomIn", GTK_STOCK_ZOOM_IN, N_("Zoom _In"), "<control>plus",
938           N_("Enlarge the document"),
939           G_CALLBACK (ev_window_cmd_view_zoom_in) },
940         { "ViewZoomOut", GTK_STOCK_ZOOM_OUT, N_("Zoom _Out"), "<control>minus",
941           N_("Shrink the document"),
942           G_CALLBACK (ev_window_cmd_view_zoom_out) },
943         { "ViewNormalSize", GTK_STOCK_ZOOM_100, N_("_Normal Size"), "<control>0",
944           N_("Zoom to the normal size"),
945           G_CALLBACK (ev_window_cmd_view_normal_size) },
946         { "ViewBestFit", GTK_STOCK_ZOOM_FIT, N_("_Best Fit"), NULL,
947           N_("Zoom to fit the document to the current window"),
948           G_CALLBACK (ev_window_cmd_view_best_fit) },
949         { "ViewPageWidth", EV_STOCK_ZOOM_FIT_WIDTH, N_("Fit Page _Width"), NULL,
950           N_("Zoom to fit the width of the current window "),
951           G_CALLBACK (ev_window_cmd_view_page_width) },
952
953         /* Go menu */
954         { "GoBack", GTK_STOCK_GO_BACK, N_("_Back"), "<mod1>Left",
955           N_("Go to the page viewed before this one"),
956           G_CALLBACK (ev_window_cmd_go_back) },
957         { "GoForward", GTK_STOCK_GO_FORWARD, N_("Fo_rward"), "<mod1>Right",
958           N_("Go to the page viewed before this one"),
959           G_CALLBACK (ev_window_cmd_go_forward) },
960         { "GoPreviousPage", GTK_STOCK_GO_BACK, N_("_Previous Page"), "<control>Page_Up",
961           N_("Go to the previous page"),
962           G_CALLBACK (ev_window_cmd_go_previous_page) },
963         { "GoNextPage", GTK_STOCK_GO_FORWARD, N_("_Next Page"), "<control>Page_Down",
964           N_("Go to the next page"),
965           G_CALLBACK (ev_window_cmd_go_next_page) },
966         { "GoFirstPage", GTK_STOCK_GOTO_FIRST, N_("_First Page"), "<control>Home",
967           N_("Go to the first page"),
968           G_CALLBACK (ev_window_cmd_go_first_page) },        
969         { "GoLastPage", GTK_STOCK_GOTO_LAST, N_("_Last Page"), "<control>End",
970           N_("Go to the last page"),
971           G_CALLBACK (ev_window_cmd_go_last_page) },
972         
973         /* Help menu */
974         { "HelpContents", GTK_STOCK_HELP, N_("_Contents"), NULL,
975           N_("Display help for the viewer application"),
976           G_CALLBACK (ev_window_cmd_help_contents) },
977         
978         { "HelpAbout", GTK_STOCK_ABOUT, N_("_About"), NULL,
979           N_("Display credits for the document viewer creators"),
980           G_CALLBACK (ev_window_cmd_help_about) },
981 };
982
983 /* Toggle items */
984 static GtkToggleActionEntry toggle_entries[] = {
985         /* View Menu */
986         { "ViewToolbar", NULL, N_("_Toolbar"), "<shift><control>T",
987           N_("Show or hide toolbar"),
988           G_CALLBACK (ev_window_view_toolbar_cb), TRUE },
989         { "ViewStatusbar", NULL, N_("_Statusbar"), NULL,
990           N_("Show or hide statusbar"),
991           G_CALLBACK (ev_window_view_statusbar_cb), TRUE },
992         { "ViewSidebar", NULL, N_("Side_bar"), "F9",
993           N_("Show or hide sidebar"),
994           G_CALLBACK (ev_window_view_sidebar_cb), FALSE },
995         { "ViewFullscreen", NULL, N_("_Fullscreen"), "F11",
996           N_("Expand the window to fill the screen"),
997           G_CALLBACK (ev_window_cmd_view_fullscreen) },
998 };
999
1000 static void
1001 ev_window_init (EvWindow *ev_window)
1002 {
1003         GtkActionGroup *action_group;
1004         GtkAccelGroup *accel_group;
1005         GError *error = NULL;
1006         GtkWidget *scrolled_window;
1007         GtkWidget *menubar;
1008         GtkWidget *toolbar;
1009         GtkWidget *sidebar_widget;
1010
1011         ev_window->priv = EV_WINDOW_GET_PRIVATE (ev_window);
1012
1013         gtk_window_set_title (GTK_WINDOW (ev_window), _("Document Viewer"));
1014
1015         ev_window->priv->main_box = gtk_vbox_new (FALSE, 0);
1016         gtk_container_add (GTK_CONTAINER (ev_window), ev_window->priv->main_box);
1017         gtk_widget_show (ev_window->priv->main_box);
1018         
1019         action_group = gtk_action_group_new ("MenuActions");
1020         ev_window->priv->action_group = action_group;
1021         gtk_action_group_set_translation_domain (action_group, NULL);
1022         gtk_action_group_add_actions (action_group, entries,
1023                                       G_N_ELEMENTS (entries), ev_window);
1024         gtk_action_group_add_toggle_actions (action_group, toggle_entries,
1025                                              G_N_ELEMENTS (toggle_entries),
1026                                              ev_window);
1027
1028         ev_window->priv->ui_manager = gtk_ui_manager_new ();
1029         gtk_ui_manager_insert_action_group (ev_window->priv->ui_manager,
1030                                             action_group, 0);
1031
1032         accel_group =
1033                 gtk_ui_manager_get_accel_group (ev_window->priv->ui_manager);
1034         gtk_window_add_accel_group (GTK_WINDOW (ev_window), accel_group);
1035
1036         g_signal_connect (ev_window->priv->ui_manager, "connect_proxy",
1037                           G_CALLBACK (connect_proxy_cb), ev_window);
1038         g_signal_connect (ev_window->priv->ui_manager, "disconnect_proxy",
1039                           G_CALLBACK (disconnect_proxy_cb), ev_window);
1040
1041         if (!gtk_ui_manager_add_ui_from_file (ev_window->priv->ui_manager,
1042                                               DATADIR"/evince-ui.xml",
1043                                               &error)) {
1044                 g_message ("building menus failed: %s", error->message);
1045                 g_error_free (error);
1046         }
1047
1048         menubar = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
1049                                              "/MainMenu");
1050         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), menubar,
1051                             FALSE, FALSE, 0);
1052
1053         toolbar = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
1054                                              "/ToolBar");
1055         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), toolbar,
1056                             FALSE, FALSE, 0);
1057
1058         /* Add the main area */
1059         ev_window->priv->hpaned = gtk_hpaned_new ();
1060         gtk_widget_show (ev_window->priv->hpaned);
1061         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), ev_window->priv->hpaned,
1062                             TRUE, TRUE, 0);
1063
1064         ev_window->priv->sidebar = ev_sidebar_new ();
1065         gtk_widget_show (ev_window->priv->sidebar);
1066         gtk_paned_add1 (GTK_PANED (ev_window->priv->hpaned),
1067                         ev_window->priv->sidebar);
1068
1069         /* Stub sidebar, for now */
1070         sidebar_widget = ev_sidebar_bookmarks_new ();
1071         gtk_widget_show (sidebar_widget);
1072         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
1073                              "bookmarks",
1074                              _("Bookmarks"),
1075                              sidebar_widget);
1076
1077         sidebar_widget = ev_sidebar_thumbnails_new ();
1078         gtk_widget_show (sidebar_widget);
1079         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
1080                              "thumbnails",
1081                              _("Thumbnails"),
1082                              sidebar_widget);
1083         
1084         scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1085         gtk_widget_show (scrolled_window);
1086         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
1087                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1088
1089         gtk_paned_add2 (GTK_PANED (ev_window->priv->hpaned),
1090                         scrolled_window);
1091
1092         ev_window->priv->view = ev_view_new ();
1093         gtk_widget_show (ev_window->priv->view);
1094         gtk_container_add (GTK_CONTAINER (scrolled_window),
1095                            ev_window->priv->view);
1096         g_signal_connect (ev_window->priv->view,
1097                           "page-changed",
1098                           G_CALLBACK (view_page_changed_cb),
1099                           ev_window);
1100
1101         ev_window->priv->statusbar = gtk_statusbar_new ();
1102         gtk_widget_show (ev_window->priv->statusbar);
1103         gtk_box_pack_end (GTK_BOX (ev_window->priv->main_box),
1104                           ev_window->priv->statusbar,
1105                           FALSE, TRUE, 0);
1106         ev_window->priv->help_message_cid = gtk_statusbar_get_context_id
1107                 (GTK_STATUSBAR (ev_window->priv->statusbar), "help_message");
1108
1109         ev_window->priv->find_bar = egg_find_bar_new ();
1110         gtk_box_pack_end (GTK_BOX (ev_window->priv->main_box),
1111                           ev_window->priv->find_bar,
1112                           FALSE, TRUE, 0);
1113         
1114         /* Connect to find bar signals */
1115         g_signal_connect (ev_window->priv->find_bar,
1116                           "previous",
1117                           G_CALLBACK (find_bar_previous_cb),
1118                           ev_window);
1119         g_signal_connect (ev_window->priv->find_bar,
1120                           "next",
1121                           G_CALLBACK (find_bar_next_cb),
1122                           ev_window);
1123         g_signal_connect (ev_window->priv->find_bar,
1124                           "close",
1125                           G_CALLBACK (find_bar_close_cb),
1126                           ev_window);
1127         g_signal_connect (ev_window->priv->find_bar,
1128                           "notify::search-string",
1129                           G_CALLBACK (find_bar_search_changed_cb),
1130                           ev_window);
1131         g_signal_connect (ev_window->priv->find_bar,
1132                           "notify::case-sensitive",
1133                           G_CALLBACK (find_bar_search_changed_cb),
1134                           ev_window);
1135         g_signal_connect (ev_window->priv->find_bar,
1136                           "notify::visible",
1137                           G_CALLBACK (find_bar_search_changed_cb),
1138                           ev_window);
1139
1140         g_signal_connect (ev_window, "window-state-event",
1141                           G_CALLBACK (ev_window_state_event_cb),
1142                           ev_window);
1143         g_signal_connect (ev_window, "focus_out_event",
1144                           G_CALLBACK (ev_window_focus_out_cb),
1145                           ev_window);
1146         
1147         /* Give focus to the scrolled window */
1148         gtk_widget_grab_focus (scrolled_window);
1149         
1150         update_action_sensitivity (ev_window);
1151