]> www.fi.muni.cz Git - evince.git/blob - shell/ev-window.c
Fix size toggle buttons behavior
[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, 2005 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-page-action.h"
33 #include "ev-sidebar.h"
34 #include "ev-sidebar-links.h"
35 #include "ev-sidebar-thumbnails.h"
36 #include "ev-view.h"
37 #include "ev-page-view.h"
38 #include "ev-password.h"
39 #include "ev-password-view.h"
40 #include "ev-print-job.h"
41 #include "ev-document-find.h"
42 #include "ev-document-security.h"
43 #include "eggfindbar.h"
44
45 #include "pdf-document.h"
46 #include "pixbuf-document.h"
47 #include "ps-document.h"
48
49 #include <glib/gi18n.h>
50 #include <gtk/gtk.h>
51
52 #include <libgnomevfs/gnome-vfs-mime-utils.h>
53 #include <libgnomevfs/gnome-vfs-uri.h>
54 #include <libgnomeprintui/gnome-print-dialog.h>
55
56 #include <gconf/gconf-client.h>
57
58 #include <string.h>
59
60 #include "ev-application.h"
61 #include "ev-stock-icons.h"
62
63 typedef enum {
64         EV_SIZING_BEST_FIT,
65         EV_SIZING_FIT_WIDTH,
66         EV_SIZING_FREE,
67 } EvSizingMode;
68
69 typedef enum {
70         PAGE_MODE_SINGLE_PAGE,
71         PAGE_MODE_CONTINUOUS_PAGE,
72         PAGE_MODE_PASSWORD,
73 } EvWindowPageMode;
74
75 typedef enum {
76         EV_CHROME_MENUBAR       = 1 << 0,
77         EV_CHROME_TOOLBAR       = 1 << 1,
78         EV_CHROME_SIDEBAR       = 1 << 2,
79         EV_CHROME_FINDBAR       = 1 << 3,
80         EV_CHROME_STATUSBAR     = 1 << 4,
81         EV_CHROME_NORMAL        = EV_CHROME_MENUBAR | EV_CHROME_TOOLBAR | EV_CHROME_SIDEBAR | EV_CHROME_STATUSBAR
82 } EvChrome;
83
84 struct _EvWindowPrivate {
85         GtkWidget *main_box;
86         GtkWidget *menubar;
87         GtkWidget *toolbar_dock;
88         GtkWidget *toolbar;
89         GtkWidget *hpaned;
90         GtkWidget *sidebar;
91         GtkWidget *thumbs_sidebar;
92         GtkWidget *find_bar;
93         GtkWidget *scrolled_window;
94         GtkWidget *view;
95         GtkWidget *page_view;
96         GtkWidget *password_view;
97         GtkActionGroup *action_group;
98         GtkUIManager *ui_manager;
99         GtkWidget *statusbar;
100         guint help_message_cid;
101         guint view_message_cid;
102         GtkWidget *fullscreen_toolbar;
103         GtkWidget *fullscreen_popup;
104         char *uri;
105
106         EvDocument *document;
107
108         EvWindowPageMode page_mode;
109         /* These members are used temporarily when in PAGE_MODE_PASSWORD */
110         EvDocument *password_document;
111         GtkWidget *password_dialog;
112         char *password_uri;
113
114         EvChrome chrome;
115         gboolean fullscreen_mode;
116         EvSizingMode sizing_mode;
117         GSource *fullscreen_timeout_source;
118 };
119
120 static GtkTargetEntry ev_drop_types[] = {
121         { "text/uri-list", 0, 0 }
122 };
123
124
125 #define EV_WINDOW_GET_PRIVATE(object) \
126         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_WINDOW, EvWindowPrivate))
127
128 #define PAGE_SELECTOR_ACTION    "PageSelector"
129
130 #define GCONF_CHROME_TOOLBAR    "/apps/evince/show_toolbar"
131 #define GCONF_CHROME_SIDEBAR    "/apps/evince/show_sidebar"
132 #define GCONF_CHROME_STATUSBAR  "/apps/evince/show_statusbar"
133
134 static void     ev_window_update_fullscreen_popup (EvWindow         *window);
135 static void     ev_window_set_page_mode           (EvWindow         *window,
136                                                    EvWindowPageMode  page_mode);
137 static gboolean start_loading_document            (EvWindow         *ev_window,
138                                                    EvDocument       *document,
139                                                    const char       *uri);
140 static void     ev_window_set_sizing_mode         (EvWindow         *ev_window,
141                                                    EvSizingMode      sizing_mode);
142
143 G_DEFINE_TYPE (EvWindow, ev_window, GTK_TYPE_WINDOW)
144
145 static void
146 set_action_sensitive (EvWindow   *ev_window,
147                       const char *name,
148                       gboolean    sensitive)
149 {
150         GtkAction *action = gtk_action_group_get_action (ev_window->priv->action_group,
151                                                          name);
152         gtk_action_set_sensitive (action, sensitive);
153 }
154
155 static void
156 update_action_sensitivity (EvWindow *ev_window)
157 {
158         EvDocument *document;
159         EvWindowPageMode page_mode;
160         EvView *view;
161
162         document = ev_window->priv->document;
163         page_mode = ev_window->priv->page_mode;
164
165         view = EV_VIEW (ev_window->priv->view);
166
167         /* File menu */
168         /* "FileOpen": always sensitive */
169         set_action_sensitive (ev_window, "FileSaveAs", document!=NULL);
170         set_action_sensitive (ev_window, "FilePrint", document!=NULL);
171         /* "FileCloseWindow": always sensitive */
172
173         /* Edit menu */
174         set_action_sensitive (ev_window, "EditCopy", document!=NULL);
175         set_action_sensitive (ev_window, "EditSelectAll", document!=NULL);
176
177         if (document)
178                 set_action_sensitive (ev_window, "EditFind", EV_IS_DOCUMENT_FIND (document));
179         else
180                 set_action_sensitive (ev_window, "EditFind", FALSE);
181
182         /* View menu */
183         set_action_sensitive (ev_window, "ViewZoomIn", document!=NULL);
184         set_action_sensitive (ev_window, "ViewZoomOut", document!=NULL);
185         set_action_sensitive (ev_window, "ViewNormalSize", document!=NULL);
186         set_action_sensitive (ev_window, "ViewBestFit", document!=NULL);
187         set_action_sensitive (ev_window, "ViewPageWidth", document!=NULL);
188
189         /* Go menu */
190         if (document) {
191                 int n_pages;
192                 int page;
193
194                 page = ev_view_get_page (EV_VIEW (ev_window->priv->view));
195                 n_pages = ev_document_get_n_pages (document);
196
197                 set_action_sensitive (ev_window, "GoPreviousPage", page > 1);
198                 set_action_sensitive (ev_window, "GoNextPage", page < n_pages);
199                 set_action_sensitive (ev_window, "GoFirstPage", page > 1);
200                 set_action_sensitive (ev_window, "GoLastPage", page < n_pages);
201         } else {
202                 set_action_sensitive (ev_window, "GoFirstPage", FALSE);
203                 set_action_sensitive (ev_window, "GoPreviousPage", FALSE);
204                 set_action_sensitive (ev_window, "GoNextPage", FALSE);
205                 set_action_sensitive (ev_window, "GoLastPage", FALSE);
206         }
207
208         /* Page View radio group */
209         if (document) {
210                 set_action_sensitive (ev_window, "SinglePage", page_mode != PAGE_MODE_PASSWORD);
211                 set_action_sensitive (ev_window, "ContinuousPage", page_mode != PAGE_MODE_PASSWORD);
212         } else {
213                 set_action_sensitive (ev_window, "SinglePage", FALSE);
214                 set_action_sensitive (ev_window, "ContinuousPage", FALSE);
215         }
216         /* Help menu */
217         /* "HelpContents": always sensitive */
218         /* "HelpAbout": always sensitive */
219
220         /* Toolbar-specific actions: */
221         set_action_sensitive (ev_window, PAGE_SELECTOR_ACTION, document!=NULL);
222 }
223
224 static void
225 set_widget_visibility (GtkWidget *widget, gboolean visible)
226 {
227         if (visible)
228                 gtk_widget_show (widget);
229         else
230                 gtk_widget_hide (widget);
231 }
232
233 static void
234 update_chrome_visibility (EvWindow *window)
235 {
236         EvWindowPrivate *priv = window->priv;
237         gboolean menubar, toolbar, sidebar, findbar, statusbar, fullscreen_toolbar;
238
239         menubar = (priv->chrome & EV_CHROME_MENUBAR) != 0 && !priv->fullscreen_mode;
240         toolbar = (priv->chrome & EV_CHROME_TOOLBAR) != 0 && !priv->fullscreen_mode;
241         sidebar = (priv->chrome & EV_CHROME_SIDEBAR) != 0 && !priv->fullscreen_mode;
242         fullscreen_toolbar = (priv->chrome & EV_CHROME_TOOLBAR) != 0;
243         statusbar = (priv->chrome & EV_CHROME_STATUSBAR) != 0 && !priv->fullscreen_mode;
244         findbar = (priv->chrome & EV_CHROME_FINDBAR) != 0;
245
246         set_widget_visibility (priv->menubar, menubar);
247         set_widget_visibility (priv->toolbar_dock, toolbar);
248         set_widget_visibility (priv->sidebar, sidebar);
249         set_widget_visibility (priv->find_bar, findbar);
250         set_widget_visibility (priv->statusbar, statusbar);
251         set_widget_visibility (priv->fullscreen_toolbar, fullscreen_toolbar);
252
253         if (priv->fullscreen_popup != NULL) {
254                 set_widget_visibility (priv->fullscreen_popup, priv->fullscreen_mode);
255         }
256 }
257
258 static void
259 update_chrome_flag (EvWindow *window, EvChrome flag, const char *pref, gboolean active)
260 {
261         EvWindowPrivate *priv = window->priv;
262         GConfClient *client;
263         
264         if (active) {
265                 priv->chrome |= flag;
266         }
267         else {
268                 priv->chrome &= ~flag;
269         }
270
271         if (pref != NULL) {
272                 client = gconf_client_get_default ();
273                 gconf_client_set_bool (client, pref, active, NULL);
274                 g_object_unref (client);
275         }
276
277         update_chrome_visibility (window);
278 }
279
280 static void
281 ev_window_cmd_view_best_fit (GtkAction *action, EvWindow *ev_window)
282 {
283         if (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action))) {
284                 ev_window_set_sizing_mode (ev_window, EV_SIZING_BEST_FIT);
285         } else {
286                 ev_window_set_sizing_mode (ev_window, EV_SIZING_FREE);
287         }
288 }
289
290 static void
291 ev_window_cmd_view_page_width (GtkAction *action, EvWindow *ev_window)
292 {
293         if (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action))) {
294                 ev_window_set_sizing_mode (ev_window, EV_SIZING_FIT_WIDTH);
295         } else {
296                 ev_window_set_sizing_mode (ev_window, EV_SIZING_FREE);
297         }
298 }
299
300 static void
301 update_sizing_buttons (EvWindow *window)
302 {
303         GtkActionGroup *action_group = window->priv->action_group;
304         GtkAction *action;
305         gboolean best_fit, page_width;
306
307         switch (window->priv->sizing_mode) {
308         case EV_SIZING_BEST_FIT:
309                 best_fit = TRUE;
310                 page_width = FALSE;
311                 break;
312         case EV_SIZING_FIT_WIDTH:
313                 best_fit = FALSE;
314                 page_width = TRUE;
315                 break;
316         case EV_SIZING_FREE:
317                 best_fit = page_width = FALSE;
318                 break;
319         }
320
321         action = gtk_action_group_get_action (action_group, "ViewBestFit");
322         g_signal_handlers_block_by_func
323                 (action, G_CALLBACK (ev_window_cmd_view_best_fit), window);
324         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), best_fit);
325         g_signal_handlers_unblock_by_func
326                 (action, G_CALLBACK (ev_window_cmd_view_best_fit), window);
327
328         action = gtk_action_group_get_action (action_group, "ViewPageWidth");   
329         g_signal_handlers_block_by_func
330                 (action, G_CALLBACK (ev_window_cmd_view_page_width), window);
331         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), page_width);
332         g_signal_handlers_unblock_by_func
333                 (action, G_CALLBACK (ev_window_cmd_view_page_width), window);
334 }
335
336 void
337 ev_window_open_page (EvWindow *ev_window, int page)
338 {
339         ev_view_set_page (EV_VIEW (ev_window->priv->view), page);
340 }
341
342 void
343 ev_window_open_link (EvWindow *ev_window, EvLink *link)
344 {
345         ev_view_go_to_link (EV_VIEW (ev_window->priv->view), link);
346 }
347
348 gboolean
349 ev_window_is_empty (const EvWindow *ev_window)
350 {
351         g_return_val_if_fail (EV_IS_WINDOW (ev_window), FALSE);
352
353         return ev_window->priv->document == NULL;
354 }
355
356 static void
357 unable_to_load (EvWindow   *ev_window,
358                 const char *error_message)
359 {
360         GtkWidget *dialog;
361
362         dialog = gtk_message_dialog_new (GTK_WINDOW (ev_window),
363                                          GTK_DIALOG_DESTROY_WITH_PARENT,
364                                          GTK_MESSAGE_ERROR,
365                                          GTK_BUTTONS_CLOSE,
366                                          _("Unable to open document"));
367         gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
368                                                   "%s", error_message);
369         gtk_dialog_run (GTK_DIALOG (dialog));
370         gtk_widget_destroy (dialog);
371 }
372
373 /* Would be nice to have this in gdk-pixbuf */
374 static gboolean
375 mime_type_supported_by_gdk_pixbuf (const gchar *mime_type)
376 {
377         GSList *formats, *list;
378         gboolean retval = FALSE;
379
380         formats = gdk_pixbuf_get_formats ();
381
382         list = formats;
383         while (list) {
384                 GdkPixbufFormat *format = list->data;
385                 int i;
386                 gchar **mime_types;
387
388                 if (gdk_pixbuf_format_is_disabled (format))
389                         continue;
390
391                 mime_types = gdk_pixbuf_format_get_mime_types (format);
392
393                 for (i = 0; mime_types[i] != NULL; i++) {
394                         if (strcmp (mime_types[i], mime_type) == 0) {
395                                 retval = TRUE;
396                                 break;
397                         }
398                 }
399
400                 if (retval)
401                         break;
402
403                 list = list->next;
404         }
405
406         g_slist_free (formats);
407
408         return retval;
409 }
410
411 static void
412 update_window_title (EvDocument *document, GParamSpec *pspec, EvWindow *ev_window)
413 {
414         char *title = NULL;
415         char *doc_title = NULL;
416         gboolean password_needed;
417
418         password_needed = (ev_window->priv->password_document != NULL);
419
420         if (document) {
421                 doc_title = ev_document_get_title (document);
422
423                 /* Make sure we get a valid title back */
424                 if (doc_title) {
425                         if (doc_title[0] == '\000' ||
426                             !g_utf8_validate (doc_title, -1, NULL)) {
427                                 g_free (doc_title);
428                                 doc_title = NULL;
429                         }
430                 }
431         }
432
433         if (doc_title) {
434                 char *p;
435
436                 for (p = doc_title; *p; ++p) {
437                         /* an '\n' byte is always ASCII, no need for UTF-8 special casing */
438                         if (*p == '\n')
439                                 *p = ' ';
440                 }
441         }
442
443         if (doc_title == NULL && ev_window->priv->uri) {
444                 doc_title = g_path_get_basename (ev_window->priv->uri);
445         }
446
447         if (password_needed) {
448                 if (doc_title == NULL) {
449                         title = g_strdup (_("Document Viewer - Password Required"));
450                 } else {
451                         title = g_strdup_printf (_("%s - Password Required"), doc_title);
452                 }
453         } else {
454                 if (doc_title == NULL) {
455                         title = g_strdup (_("Document Viewer"));
456                 } else {
457                         title = g_strdup (doc_title);
458                 }
459         }
460
461         gtk_window_set_title (GTK_WINDOW (ev_window), title);
462
463         g_free (doc_title);
464         g_free (title);
465 }
466
467 static void
468 update_total_pages (EvWindow *ev_window)
469 {
470         GtkAction *action;
471         int pages;
472
473         pages = ev_document_get_n_pages (ev_window->priv->document);
474         action = gtk_action_group_get_action
475                 (ev_window->priv->action_group, PAGE_SELECTOR_ACTION);
476         ev_page_action_set_total_pages (EV_PAGE_ACTION (action), pages);
477 }
478
479 /* This function assumes that ev_window just had ev_window->document set.
480  */
481 static void
482 ev_window_setup_document (EvWindow *ev_window)
483 {
484         EvDocument *document;
485         EvView *view = EV_VIEW (ev_window->priv->view);
486         EvSidebar *sidebar = EV_SIDEBAR (ev_window->priv->sidebar);
487
488         document = ev_window->priv->document;
489
490         g_signal_connect_object (G_OBJECT (document),
491                                  "notify::title",
492                                  G_CALLBACK (update_window_title),
493                                  ev_window, 0);
494
495         ev_window_set_page_mode (ev_window, PAGE_MODE_SINGLE_PAGE);
496
497         ev_sidebar_set_document (sidebar, document);
498         ev_view_set_document (view, document);
499
500         update_window_title (document, NULL, ev_window);
501         update_total_pages (ev_window);
502         update_action_sensitivity (ev_window);
503 }
504
505 static void
506 password_dialog_response (GtkWidget *password_dialog,
507                           gint       response_id,
508                           EvWindow  *ev_window)
509 {
510         char *password;
511         
512         if (response_id == GTK_RESPONSE_OK) {
513                 EvDocument *document;
514                 gchar *uri;
515
516                 password = ev_password_dialog_get_password (password_dialog);
517                 if (password)
518                         ev_document_security_set_password (EV_DOCUMENT_SECURITY (ev_window->priv->password_document),
519                                                            password);
520                 g_free (password);
521
522                 document = ev_window->priv->password_document;
523                 uri = ev_window->priv->password_uri;
524
525                 ev_window->priv->password_document = NULL;
526                 ev_window->priv->password_uri = NULL;
527
528                 if (start_loading_document (ev_window, document, uri)) {
529                         gtk_widget_destroy (password_dialog);
530                 }
531
532                 g_object_unref (document);
533                 g_free (uri);
534
535                 return;
536         }
537
538         gtk_widget_set_sensitive (ev_window->priv->password_view, TRUE);
539         gtk_widget_destroy (password_dialog);
540 }
541
542 /* Called either by start_loading_document or by the "unlock" callback on the
543  * password_view page.  It assumes that ev_window->priv->password_* has been set
544  * correctly.  These are cleared by password_dialog_response() */
545
546 static void
547 ev_window_popup_password_dialog (EvWindow *ev_window)
548 {
549         g_assert (ev_window->priv->password_document);
550         g_assert (ev_window->priv->password_uri);
551
552         gtk_widget_set_sensitive (ev_window->priv->password_view, FALSE);
553
554         update_window_title (ev_window->priv->password_document, NULL, ev_window);
555         if (ev_window->priv->password_dialog == NULL) {
556                 gchar *file_name;
557
558                 file_name = g_path_get_basename (ev_window->priv->password_uri);
559                 ev_window->priv->password_dialog =
560                         ev_password_dialog_new (GTK_WIDGET (ev_window), file_name);
561                 g_object_add_weak_pointer (G_OBJECT (ev_window->priv->password_dialog),
562                                            (gpointer *) &(ev_window->priv->password_dialog));
563                 g_signal_connect (ev_window->priv->password_dialog,
564                                   "response",
565                                   G_CALLBACK (password_dialog_response),
566                                   ev_window);
567                 g_free (file_name);
568                 gtk_widget_show (ev_window->priv->password_dialog);
569         } else {
570                 ev_password_dialog_set_bad_pass (ev_window->priv->password_dialog);
571         }
572 }
573
574 /* This wil try to load the document.  It might be called multiple times on the
575  * same document by the password dialog.
576  *
577  * Since the flow of the error dialog is very confusing, we assume that both
578  * document and uri will go away after this function is called, and thus we need
579  * to ref/dup them.  Additionally, it needs to clear
580  * ev_window->priv->password_{uri,document}, and thus people who call this
581  * function should _not_ necessarily expect those to exist after being
582  * called. */
583 static gboolean
584 start_loading_document (EvWindow   *ev_window,
585                         EvDocument *document,
586                         const char *uri)
587 {
588         gboolean result;
589         GError *error = NULL;
590
591         g_assert (document);
592         g_assert (document != ev_window->priv->document);
593         g_assert (uri);
594         if (ev_window->priv->password_document) {
595                 g_object_unref (ev_window->priv->password_document);
596                 ev_window->priv->password_document = NULL;
597         }
598         if (ev_window->priv->password_uri) {
599                 g_free (ev_window->priv->password_uri);
600                 ev_window->priv->password_uri = NULL;
601         }
602
603         result = ev_document_load (document, uri, &error);
604
605         /* Success! */
606         if (result) {
607                 if (ev_window->priv->document)
608                         g_object_unref (ev_window->priv->document);
609                 ev_window->priv->document = g_object_ref (document);
610                 ev_window_setup_document (ev_window);
611
612                 return TRUE;
613         }
614
615         /* unable to load the document */
616         g_assert (error != NULL);
617
618         if (error->domain == EV_DOCUMENT_ERROR &&
619             error->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
620                 char *file_name;
621
622                 ev_window->priv->password_document = g_object_ref (document);
623                 ev_window->priv->password_uri = g_strdup (uri);
624
625                 file_name = g_path_get_basename (uri);
626                 ev_password_view_set_file_name (EV_PASSWORD_VIEW (ev_window->priv->password_view),
627                                                 file_name);
628                 g_free (file_name);
629                 ev_window_set_page_mode (ev_window, PAGE_MODE_PASSWORD);
630
631                 ev_window_popup_password_dialog (ev_window);
632         } else {
633                 unable_to_load (ev_window, error->message);
634         }
635         g_error_free (error);
636
637         return FALSE;
638 }
639
640 static gboolean
641 is_file_supported (const gchar *mime_type)
642 {
643         static char *supported_types [] = {
644                 "application/pdf",
645                 "application/postscript",
646                 "application/x-gzpostscript",
647                 "image/x-eps",
648                 NULL
649         };
650         gint   i;
651         
652         g_return_val_if_fail (mime_type != NULL, FALSE);
653
654         if (mime_type_supported_by_gdk_pixbuf (mime_type))
655                 return TRUE;
656         
657         for (i = 0; supported_types[i] != NULL; i++) {
658                 if (g_ascii_strcasecmp (mime_type, supported_types[i]) == 0)
659                         return TRUE;
660         }
661         
662         return FALSE;
663 }
664
665 void
666 ev_window_open (EvWindow *ev_window, const char *uri)
667 {
668         EvDocument *document = NULL;
669         char *mime_type;
670
671         g_free (ev_window->priv->uri);
672         ev_window->priv->uri = g_strdup (uri);
673
674         mime_type = gnome_vfs_get_mime_type (uri);
675
676         if (mime_type == NULL)
677                 document = NULL;
678         else if (!strcmp (mime_type, "application/pdf"))
679                 document = g_object_new (PDF_TYPE_DOCUMENT, NULL);
680         else if (!strcmp (mime_type, "application/postscript") ||
681                  !strcmp (mime_type, "application/x-gzpostscript") ||
682                  !strcmp (mime_type, "image/x-eps"))
683                 document = g_object_new (PS_TYPE_DOCUMENT, NULL);
684         else if (mime_type_supported_by_gdk_pixbuf (mime_type))
685                 document = g_object_new (PIXBUF_TYPE_DOCUMENT, NULL);
686
687         if (document) {
688                 start_loading_document (ev_window, document, uri);
689                 /* See the comment on start_loading_document on ref counting.
690                  * As the password dialog flow is confusing, we're very explicit
691                  * on ref counting. */
692                 g_object_unref (document);
693         } else {
694                 char *error_message;
695
696                 error_message = g_strdup_printf (_("Unhandled MIME type: '%s'"),
697                                                  mime_type?mime_type:"<Unknown MIME Type>");
698                 unable_to_load (ev_window, error_message);
699                 g_free (error_message);
700         }
701
702         g_free (mime_type);
703 }
704
705 static void
706 ev_window_open_uri_list (EvWindow *ev_window, GList *uri_list)
707 {
708         GList *list;
709         gchar *uri, *mime_type;
710         
711         g_return_if_fail (uri_list != NULL);
712         
713         list = uri_list;
714         while (list) {
715                 uri = gnome_vfs_uri_to_string (list->data, GNOME_VFS_URI_HIDE_NONE);
716                 mime_type = gnome_vfs_get_mime_type (uri);
717                 
718                 if (is_file_supported (mime_type)) {
719                         if (ev_window_is_empty (EV_WINDOW (ev_window))) {
720                                 ev_window_open (ev_window, uri);
721                                 
722                                 gtk_widget_show (GTK_WIDGET (ev_window));
723                         } else {
724                                 EvWindow *new_window;
725                                 
726                                 new_window = ev_application_new_window (EV_APP);
727                                 ev_window_open (new_window, uri);
728                                 
729                                 gtk_widget_show (GTK_WIDGET (new_window));
730                         }
731                 }
732
733                 g_free (mime_type);
734                 g_free (uri);
735
736                 list = g_list_next (list);
737         }
738 }
739
740 static void
741 ev_window_cmd_file_open (GtkAction *action, EvWindow *ev_window)
742 {
743         ev_application_open (EV_APP, NULL);
744 }
745
746 /* FIXME
747 static gboolean
748 overwrite_existing_file (GtkWindow *window, const gchar *file_name)
749 {
750         GtkWidget *msgbox;
751         gchar *utf8_file_name;
752         AtkObject *obj;
753         gint ret;
754
755         utf8_file_name = g_filename_to_utf8 (file_name, -1, NULL, NULL, NULL);
756         msgbox = gtk_message_dialog_new (
757                 window,
758                 (GtkDialogFlags)GTK_DIALOG_DESTROY_WITH_PARENT,
759                 GTK_MESSAGE_QUESTION,
760                 GTK_BUTTONS_NONE,
761                 _("A file named \"%s\" already exists."),
762                 utf8_file_name);
763         g_free (utf8_file_name);
764
765         gtk_message_dialog_format_secondary_text (
766                 GTK_MESSAGE_DIALOG (msgbox),
767                 _("Do you want to replace it with the one you are saving?"));
768
769         gtk_dialog_add_button (GTK_DIALOG (msgbox),
770                                GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
771
772         gtk_dialog_add_button (GTK_DIALOG (msgbox),
773                                _("_Replace"), GTK_RESPONSE_YES);
774
775         gtk_dialog_set_default_response (GTK_DIALOG (msgbox),
776                                          GTK_RESPONSE_CANCEL);
777
778         obj = gtk_widget_get_accessible (msgbox);
779
780         if (GTK_IS_ACCESSIBLE (obj))
781                 atk_object_set_name (obj, _("Question"));
782
783         ret = gtk_dialog_run (GTK_DIALOG (msgbox));
784         gtk_widget_destroy (msgbox);
785
786         return (ret == GTK_RESPONSE_YES);
787 }
788 */
789
790 static void
791 save_error_dialog (GtkWindow *window, const gchar *file_name)
792 {
793         GtkWidget *error_dialog;
794
795         error_dialog = gtk_message_dialog_new (
796                 window,
797                 (GtkDialogFlags)GTK_DIALOG_DESTROY_WITH_PARENT,
798                 GTK_MESSAGE_ERROR,
799                 GTK_BUTTONS_CLOSE,
800                 _("The file could not be saved as \"%s\"."),
801                 file_name);
802
803         /* Easy way to make the text bold while keeping the string
804          * above free from pango markup (FIXME ?) */
805         gtk_message_dialog_format_secondary_text (
806                 GTK_MESSAGE_DIALOG (error_dialog), " ");
807
808         gtk_dialog_run (GTK_DIALOG (error_dialog));
809         gtk_widget_destroy (error_dialog);
810 }
811
812 static void
813 ev_window_cmd_save_as (GtkAction *action, EvWindow *ev_window)
814 {
815         GtkWidget *fc;
816         GtkFileFilter *pdf_filter, *all_filter;
817         gchar *uri = NULL;
818
819         fc = gtk_file_chooser_dialog_new (
820                 _("Save a Copy"),
821                 NULL, GTK_FILE_CHOOSER_ACTION_SAVE,
822                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
823                 GTK_STOCK_SAVE, GTK_RESPONSE_OK,
824                 NULL);
825         gtk_window_set_modal (GTK_WINDOW (fc), TRUE);
826
827         pdf_filter = gtk_file_filter_new ();
828         gtk_file_filter_set_name (pdf_filter, _("PDF Documents"));
829         gtk_file_filter_add_mime_type (pdf_filter, "application/pdf");
830         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (fc), pdf_filter);
831
832         all_filter = gtk_file_filter_new ();
833         gtk_file_filter_set_name (all_filter, _("All Files"));
834         gtk_file_filter_add_pattern (all_filter, "*");
835         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (fc), all_filter);
836         gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (fc), pdf_filter);
837
838         gtk_dialog_set_default_response (GTK_DIALOG (fc), GTK_RESPONSE_OK);
839
840         gtk_widget_show (fc);
841
842         while (gtk_dialog_run (GTK_DIALOG (fc)) == GTK_RESPONSE_OK) {
843                 uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (fc));
844
845 /* FIXME
846                 if (g_file_test (uri, G_FILE_TEST_EXISTS) &&
847                     !overwrite_existing_file (GTK_WINDOW (fc), uri))
848                                 continue;
849 */
850
851                 if (ev_document_save (ev_window->priv->document, uri, NULL))
852                         break;
853                 else
854                         save_error_dialog (GTK_WINDOW (fc), uri);
855         }
856         gtk_widget_destroy (fc);
857 }
858
859 static gboolean
860 using_postscript_printer (GnomePrintConfig *config)
861 {
862         const guchar *driver;
863         const guchar *transport;
864
865         driver = gnome_print_config_get (
866                 config, (const guchar *)"Settings.Engine.Backend.Driver");
867
868         transport = gnome_print_config_get (
869                 config, (const guchar *)"Settings.Transport.Backend");
870
871         if (driver) {
872                 if (!strcmp ((const gchar *)driver, "gnome-print-ps"))
873                         return TRUE;
874                 else
875                         return FALSE;
876         } else  if (transport) {
877                 if (!strcmp ((const gchar *)transport, "CUPS"))
878                         return TRUE;
879         }
880
881         return FALSE;
882 }
883
884 static void
885 ev_window_print (EvWindow *ev_window)
886 {
887         GnomePrintConfig *config;
888         GnomePrintJob *job;
889         GtkWidget *print_dialog;
890         EvPrintJob *print_job = NULL;
891
892         g_return_if_fail (EV_IS_WINDOW (ev_window));
893         g_return_if_fail (ev_window->priv->document != NULL);
894
895         config = gnome_print_config_default ();
896         job = gnome_print_job_new (config);
897
898         print_dialog = gnome_print_dialog_new (job, _("Print"),
899                                                (GNOME_PRINT_DIALOG_RANGE |
900                                                 GNOME_PRINT_DIALOG_COPIES));
901         gtk_dialog_set_response_sensitive (GTK_DIALOG (print_dialog),
902                                            GNOME_PRINT_DIALOG_RESPONSE_PREVIEW,
903                                            FALSE);
904
905         while (TRUE) {
906                 int response;
907                 response = gtk_dialog_run (GTK_DIALOG (print_dialog));
908
909                 if (response != GNOME_PRINT_DIALOG_RESPONSE_PRINT)
910                         break;
911
912                 /* FIXME: Change this when we have the first backend
913                  * that can print more than postscript
914                  */
915                 if (!using_postscript_printer (config)) {
916                         GtkWidget *dialog;
917
918                         dialog = gtk_message_dialog_new (
919                                 GTK_WINDOW (print_dialog), GTK_DIALOG_MODAL,
920                                 GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
921                                 _("Printing is not supported on this printer."));
922                         gtk_message_dialog_format_secondary_text (
923                                 GTK_MESSAGE_DIALOG (dialog),
924                                 _("You were trying to print to a printer using the \"%s\" driver. This program requires a PostScript printer driver."),
925                                 gnome_print_config_get (
926                                         config, "Settings.Engine.Backend.Driver"));
927                         gtk_dialog_run (GTK_DIALOG (dialog));
928                         gtk_widget_destroy (dialog);
929
930                         continue;
931                 }
932
933                 print_job = g_object_new (EV_TYPE_PRINT_JOB,
934                                           "gnome_print_job", job,
935                                           "document", ev_window->priv->document,
936                                           "print_dialog", print_dialog,
937                                           NULL);
938                 break;
939         }
940
941         gtk_widget_destroy (print_dialog);
942
943         if (print_job != NULL) {
944                 ev_print_job_print (print_job, GTK_WINDOW (ev_window));
945                 g_object_unref (print_job);
946         }
947 }
948
949 static void
950 ev_window_cmd_file_print (GtkAction *action, EvWindow *ev_window)
951 {
952         ev_window_print (ev_window);
953 }
954
955 static void
956 ev_window_cmd_file_close_window (GtkAction *action, EvWindow *ev_window)
957 {
958         g_return_if_fail (EV_IS_WINDOW (ev_window));
959
960         gtk_widget_destroy (GTK_WIDGET (ev_window));
961 }
962
963 static void
964 find_not_supported_dialog (EvWindow   *ev_window)
965 {
966         GtkWidget *dialog;
967
968         /* If you change this so it isn't modal, be sure you don't
969          * allow multiple copies of the dialog...
970          */
971
972         dialog = gtk_message_dialog_new (GTK_WINDOW (ev_window),
973                                          GTK_DIALOG_DESTROY_WITH_PARENT,
974                                          GTK_MESSAGE_ERROR,
975                                          GTK_BUTTONS_CLOSE,
976                                          _("The \"Find\" feature will not work with this document"));
977         gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
978                                                   _("Searching for text is only supported for PDF documents."));
979         gtk_dialog_run (GTK_DIALOG (dialog));
980         gtk_widget_destroy (dialog);
981 }
982
983 static void
984 ev_window_cmd_edit_select_all (GtkAction *action, EvWindow *ev_window)
985 {
986         g_return_if_fail (EV_IS_WINDOW (ev_window));
987
988         ev_view_select_all (EV_VIEW (ev_window->priv->view));
989 }
990
991 static void
992 ev_window_cmd_edit_find (GtkAction *action, EvWindow *ev_window)
993 {
994         g_return_if_fail (EV_IS_WINDOW (ev_window));
995
996         if (ev_window->priv->document == NULL) {
997                 g_printerr ("We should have set the Find menu item insensitive since there's no document\n");
998         } else if (!EV_IS_DOCUMENT_FIND (ev_window->priv->document)) {
999                 find_not_supported_dialog (ev_window);
1000         } else {
1001                 update_chrome_flag (ev_window, EV_CHROME_FINDBAR, NULL, TRUE);
1002
1003                 egg_find_bar_grab_focus (EGG_FIND_BAR (ev_window->priv->find_bar));
1004         }
1005 }
1006
1007 static void
1008 ev_window_cmd_edit_copy (GtkAction *action, EvWindow *ev_window)
1009 {
1010         g_return_if_fail (EV_IS_WINDOW (ev_window));
1011
1012         ev_view_copy (EV_VIEW (ev_window->priv->view));
1013 }
1014
1015 static void
1016 ev_window_update_fullscreen_popup (EvWindow *window)
1017 {
1018         GtkWidget *popup = window->priv->fullscreen_popup;
1019         int popup_width, popup_height;
1020         GdkRectangle screen_rect;
1021         gboolean toolbar;
1022
1023         g_return_if_fail (popup != NULL);
1024
1025         toolbar = (window->priv->chrome & EV_CHROME_TOOLBAR) != 0;
1026         popup_width = popup->requisition.width;
1027         popup_height = popup->requisition.height;
1028
1029         /* FIXME multihead */
1030         gdk_screen_get_monitor_geometry (gdk_screen_get_default (),
1031                         gdk_screen_get_monitor_at_window
1032                         (gdk_screen_get_default (),
1033                          GTK_WIDGET (window)->window),
1034                          &screen_rect);
1035         if (toolbar) {
1036                 gtk_widget_set_size_request (popup,
1037                                              screen_rect.width,
1038                                              -1);
1039                 gtk_window_move (GTK_WINDOW (popup),
1040                                  screen_rect.x,
1041                                  screen_rect.y);
1042
1043         } else {
1044                 if (gtk_widget_get_direction (popup) == GTK_TEXT_DIR_RTL)
1045                 {
1046                         gtk_window_move (GTK_WINDOW (popup),
1047                                          screen_rect.x,
1048                                          screen_rect.y);
1049                 } else {
1050                         gtk_window_move (GTK_WINDOW (popup),
1051                                          screen_rect.x + screen_rect.width - popup_width,
1052                                          screen_rect.y);
1053                 }
1054         }
1055 }
1056
1057 static void
1058 screen_size_changed_cb (GdkScreen *screen,
1059                         EvWindow *window)
1060 {
1061         ev_window_update_fullscreen_popup (window);
1062 }
1063
1064 static void
1065 destroy_fullscreen_popup (EvWindow *window)
1066 {
1067         if (window->priv->fullscreen_popup != NULL)
1068         {
1069                 /* FIXME multihead */
1070                 g_signal_handlers_disconnect_by_func
1071                         (gdk_screen_get_default (),
1072                          G_CALLBACK (screen_size_changed_cb), window);
1073
1074                 gtk_widget_destroy (window->priv->fullscreen_popup);
1075                 window->priv->fullscreen_popup = NULL;
1076         }
1077 }
1078
1079 static void
1080 exit_fullscreen_button_clicked_cb (GtkWidget *button, EvWindow *window)
1081 {
1082         GtkAction *action;
1083
1084         action = gtk_action_group_get_action (window->priv->action_group, "ViewFullscreen");
1085         g_return_if_fail (action != NULL);
1086
1087         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), FALSE);
1088 }
1089
1090 static void
1091 fullscreen_popup_size_request_cb (GtkWidget *popup, GtkRequisition *req, EvWindow *window)
1092 {
1093         ev_window_update_fullscreen_popup (window);
1094 }
1095
1096 static gboolean
1097 fullscreen_timeout_cb (gpointer data)
1098 {
1099         EvWindow *window = EV_WINDOW (data);
1100
1101         g_object_set (window->priv->fullscreen_popup, "visible", FALSE, NULL);
1102         ev_view_hide_cursor (EV_VIEW (window->priv->view));
1103         window->priv->fullscreen_timeout_source = NULL;
1104
1105         return FALSE;
1106 }
1107
1108 static void
1109 fullscreen_set_timeout (EvWindow *window)
1110 {
1111         GSource *source;
1112
1113         if (window->priv->fullscreen_timeout_source != NULL)
1114                 g_source_destroy (window->priv->fullscreen_timeout_source);
1115
1116         source = g_timeout_source_new (1000);
1117         g_source_set_callback (source, fullscreen_timeout_cb, window, NULL);
1118         g_source_attach (source, NULL);
1119         window->priv->fullscreen_timeout_source = source;
1120 }
1121
1122 static void
1123 fullscreen_clear_timeout (EvWindow *window)
1124 {
1125         if (window->priv->fullscreen_timeout_source != NULL)
1126                 g_source_destroy (window->priv->fullscreen_timeout_source);
1127
1128         window->priv->fullscreen_timeout_source = NULL;
1129         ev_view_show_cursor (EV_VIEW (window->priv->view));
1130 }
1131
1132 static gboolean
1133 fullscreen_motion_notify_cb (GtkWidget *widget,
1134                              GdkEventMotion *event,
1135                              gpointer user_data)
1136 {
1137         EvWindow *window = EV_WINDOW (user_data);
1138
1139         if (!GTK_WIDGET_VISIBLE (window->priv->fullscreen_popup)) {
1140                 g_object_set (window->priv->fullscreen_popup, "visible", TRUE, NULL);
1141                 ev_view_show_cursor (EV_VIEW (window->priv->view));
1142         }
1143
1144         fullscreen_set_timeout (window);
1145
1146         return FALSE;
1147 }
1148
1149 static gboolean
1150 fullscreen_leave_notify_cb (GtkWidget *widget,
1151                             GdkEventCrossing *event,
1152                             gpointer user_data)
1153 {
1154         EvWindow *window = EV_WINDOW (user_data);
1155
1156         fullscreen_clear_timeout (window);
1157
1158         return FALSE;
1159 }
1160
1161 static GtkWidget *
1162 ev_window_get_exit_fullscreen_button (EvWindow *window)
1163 {
1164         GtkWidget *button, *icon, *label, *hbox;
1165
1166         button = gtk_button_new ();
1167         g_signal_connect (button, "clicked",
1168                           G_CALLBACK (exit_fullscreen_button_clicked_cb),
1169                           window);
1170         gtk_widget_show (button);
1171
1172         hbox = gtk_hbox_new (FALSE, 2);
1173         gtk_widget_show (hbox);
1174         gtk_container_add (GTK_CONTAINER (button), hbox);
1175
1176         icon = gtk_image_new_from_stock (EV_STOCK_LEAVE_FULLSCREEN, GTK_ICON_SIZE_BUTTON);
1177         gtk_widget_show (icon);
1178         gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);
1179
1180         label = gtk_label_new (_("Leave Fullscreen"));
1181         gtk_widget_show (label);
1182         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
1183
1184         return button;
1185 }
1186
1187 static GtkWidget *
1188 ev_window_create_fullscreen_popup (EvWindow *window)
1189 {
1190         GtkWidget *popup;
1191         GtkWidget *hbox;
1192         GtkWidget *button;
1193
1194         popup = gtk_window_new (GTK_WINDOW_POPUP);
1195         hbox = gtk_hbox_new (FALSE, 0);
1196         button = ev_window_get_exit_fullscreen_button (window);
1197
1198         gtk_container_add (GTK_CONTAINER (popup), hbox);
1199         gtk_box_pack_start (GTK_BOX (hbox), window->priv->fullscreen_toolbar,
1200                             TRUE, TRUE, 0);
1201         gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
1202
1203         gtk_widget_show (button);
1204         gtk_widget_show (hbox);
1205
1206         gtk_window_set_resizable (GTK_WINDOW (popup), FALSE);
1207
1208         /* FIXME multihead */
1209         g_signal_connect (gdk_screen_get_default (), "size-changed",
1210                           G_CALLBACK (screen_size_changed_cb), window);
1211         g_signal_connect (popup, "size_request",
1212                           G_CALLBACK (fullscreen_popup_size_request_cb), window);
1213
1214         return popup;
1215 }
1216
1217 static void
1218 ev_window_fullscreen (EvWindow *window)
1219 {
1220         window->priv->fullscreen_mode = TRUE;
1221
1222         if (window->priv->fullscreen_popup == NULL)
1223                 window->priv->fullscreen_popup
1224                         = ev_window_create_fullscreen_popup (window);
1225         update_chrome_visibility (window);
1226
1227         g_object_set (G_OBJECT (window->priv->scrolled_window),
1228                       "shadow-type", GTK_SHADOW_NONE,
1229                       NULL);
1230
1231         g_signal_connect (window->priv->view,
1232                           "motion-notify-event",
1233                           G_CALLBACK (fullscreen_motion_notify_cb),
1234                           window);
1235         g_signal_connect (window->priv->view,
1236                           "leave-notify-event",
1237                           G_CALLBACK (fullscreen_leave_notify_cb),
1238                           window);
1239         fullscreen_set_timeout (window);
1240
1241         gtk_widget_grab_focus (window->priv->view);
1242
1243         ev_window_update_fullscreen_popup (window);
1244 }
1245
1246 static void
1247 ev_window_unfullscreen (EvWindow *window)
1248 {
1249         window->priv->fullscreen_mode = FALSE;
1250
1251         g_object_set (G_OBJECT (window->priv->scrolled_window),
1252                       "shadow-type", GTK_SHADOW_IN,
1253                       NULL);
1254
1255         fullscreen_clear_timeout (window);
1256
1257         g_signal_handlers_disconnect_by_func (window->priv->view,
1258                                               (gpointer) fullscreen_motion_notify_cb,
1259                                               window);
1260
1261 //      destroy_fullscreen_popup (window);
1262
1263         update_chrome_visibility (window);
1264 }
1265
1266 static void
1267 ev_window_cmd_view_fullscreen (GtkAction *action, EvWindow *ev_window)
1268 {
1269         gboolean fullscreen;
1270
1271         g_return_if_fail (EV_IS_WINDOW (ev_window));
1272
1273         fullscreen = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
1274
1275         if (fullscreen) {
1276                 gtk_window_fullscreen (GTK_WINDOW (ev_window));
1277         } else {
1278                 gtk_window_unfullscreen (GTK_WINDOW (ev_window));
1279         }
1280 }
1281
1282 static gboolean
1283 ev_window_state_event_cb (GtkWidget *widget, GdkEventWindowState *event, EvWindow *window)
1284 {
1285         if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
1286         {
1287                 GtkActionGroup *action_group;
1288                 GtkAction *action;
1289                 gboolean fullscreen;
1290
1291                 fullscreen = event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN;
1292
1293                 if (fullscreen)
1294                 {
1295                         ev_window_fullscreen (window);
1296                 }
1297                 else
1298                 {
1299                         ev_window_unfullscreen (window);
1300                 }
1301
1302                 action_group = window->priv->action_group;
1303
1304                 action = gtk_action_group_get_action (action_group, "ViewFullscreen");
1305                 g_signal_handlers_block_by_func
1306                         (action, G_CALLBACK (ev_window_cmd_view_fullscreen), window);
1307                 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), fullscreen);
1308                 g_signal_handlers_unblock_by_func
1309                         (action, G_CALLBACK (ev_window_cmd_view_fullscreen), window);
1310
1311         }
1312
1313         return FALSE;
1314 }
1315
1316 static gboolean
1317 ev_window_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
1318 {
1319         EvWindow *window = EV_WINDOW (widget);
1320         EvWindowPrivate *priv = window->priv;
1321
1322         if (priv->fullscreen_mode)
1323         {
1324                 gtk_widget_show (priv->fullscreen_popup);
1325         }
1326
1327         return GTK_WIDGET_CLASS (ev_window_parent_class)->focus_in_event (widget, event);
1328 }
1329
1330 static gboolean
1331 ev_window_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
1332 {
1333         EvWindow *window = EV_WINDOW (widget);
1334         EvWindowPrivate *priv = window->priv;
1335
1336         if (priv->fullscreen_mode)
1337         {
1338                 gtk_widget_hide (priv->fullscreen_popup);
1339         }
1340
1341         return GTK_WIDGET_CLASS (ev_window_parent_class)->focus_out_event (widget, event);
1342 }
1343
1344 static void
1345 ev_window_set_page_mode (EvWindow         *window,
1346                          EvWindowPageMode  page_mode)
1347 {
1348         GtkWidget *child = NULL;
1349         GtkWidget *real_child;
1350
1351         if (window->priv->page_mode == page_mode)
1352                 return;
1353
1354         window->priv->page_mode = page_mode;
1355
1356         switch (page_mode) {
1357         case PAGE_MODE_SINGLE_PAGE:
1358                 child = window->priv->view;
1359                 break;
1360         case PAGE_MODE_PASSWORD:
1361                 child = window->priv->password_view;
1362                 break;
1363         case PAGE_MODE_CONTINUOUS_PAGE:
1364                 child = window->priv->page_view;
1365                 break;
1366         default:
1367                 g_assert_not_reached ();
1368         }
1369
1370         real_child = gtk_bin_get_child (GTK_BIN (window->priv->scrolled_window));
1371         if (child != real_child) {
1372                 gtk_container_remove (GTK_CONTAINER (window->priv->scrolled_window),
1373                                       real_child);
1374                 gtk_container_add (GTK_CONTAINER (window->priv->scrolled_window),
1375                                    child);
1376         }
1377         update_action_sensitivity (window);
1378 }
1379
1380 static void
1381 ev_window_cmd_view_zoom_in (GtkAction *action, EvWindow *ev_window)
1382 {
1383         g_return_if_fail (EV_IS_WINDOW (ev_window));
1384
1385         ev_window_set_sizing_mode (ev_window, EV_SIZING_FREE);
1386
1387         ev_view_zoom_in (EV_VIEW (ev_window->priv->view));
1388 }
1389
1390 static void
1391 ev_window_cmd_view_zoom_out (GtkAction *action, EvWindow *ev_window)
1392 {
1393         g_return_if_fail (EV_IS_WINDOW (ev_window));
1394
1395         ev_window_set_sizing_mode (ev_window, EV_SIZING_FREE);
1396
1397         ev_view_zoom_out (EV_VIEW (ev_window->priv->view));
1398 }
1399
1400 static void
1401 ev_window_cmd_view_normal_size (GtkAction *action, EvWindow *ev_window)
1402 {
1403         g_return_if_fail (EV_IS_WINDOW (ev_window));
1404
1405         ev_view_set_size (EV_VIEW (ev_window->priv->view), -1, -1);
1406 }
1407
1408 static void
1409 ev_window_cmd_go_previous_page (GtkAction *action, EvWindow *ev_window)
1410 {
1411         g_return_if_fail (EV_IS_WINDOW (ev_window));
1412
1413         ev_view_set_page (EV_VIEW (ev_window->priv->view),
1414                           ev_view_get_page (EV_VIEW (ev_window->priv->view)) - 1);
1415 }
1416
1417 static void
1418 ev_window_cmd_go_next_page (GtkAction *action, EvWindow *ev_window)
1419 {
1420         g_return_if_fail (EV_IS_WINDOW (ev_window));
1421
1422         ev_view_set_page (EV_VIEW (ev_window->priv->view),
1423                           ev_view_get_page (EV_VIEW (ev_window->priv->view)) + 1);
1424 }
1425
1426 static void
1427 ev_window_cmd_go_first_page (GtkAction *action, EvWindow *ev_window)
1428 {
1429         g_return_if_fail (EV_IS_WINDOW (ev_window));
1430
1431         ev_view_set_page (EV_VIEW (ev_window->priv->view), 1);
1432 }
1433
1434 static void
1435 ev_window_cmd_go_last_page (GtkAction *action, EvWindow *ev_window)
1436 {
1437         g_return_if_fail (EV_IS_WINDOW (ev_window));
1438
1439         ev_view_set_page (EV_VIEW (ev_window->priv->view), G_MAXINT);
1440 }
1441
1442 static void
1443 ev_window_cmd_help_contents (GtkAction *action, EvWindow *ev_window)
1444 {
1445         g_return_if_fail (EV_IS_WINDOW (ev_window));
1446
1447         /* FIXME */
1448 }
1449
1450 static void
1451 ev_window_cmd_leave_fullscreen (GtkAction *action, EvWindow *window)
1452 {
1453         gtk_window_unfullscreen (GTK_WINDOW (window));
1454 }
1455
1456 static void
1457 size_allocate_cb (GtkWidget     *scrolled_window,
1458                   GtkAllocation *allocation,
1459                   EvWindow      *ev_window)
1460 {
1461         int width, height;
1462         GtkRequisition vsb_requisition;
1463         int scrollbar_spacing;
1464
1465         width = ev_window->priv->scrolled_window->allocation.width;
1466         height = ev_window->priv->scrolled_window->allocation.height;
1467
1468         /* the scrolled window has a GTK_SHADOW_IN */
1469         width -= 2 * ev_window->priv->view->style->xthickness;
1470         height -= 2 * ev_window->priv->view->style->ythickness;
1471
1472         if (ev_window->priv->sizing_mode == EV_SIZING_BEST_FIT) {
1473                 ev_view_set_size (EV_VIEW (ev_window->priv->view),
1474                                   MAX (1, width), MAX (1, height));
1475         } else if (ev_window->priv->sizing_mode == EV_SIZING_FIT_WIDTH) {
1476                 gtk_widget_size_request (GTK_SCROLLED_WINDOW (ev_window->priv->scrolled_window)->vscrollbar,
1477                                          &vsb_requisition);
1478                 gtk_widget_style_get (ev_window->priv->scrolled_window,
1479                                       "scrollbar_spacing", &scrollbar_spacing,
1480                                       NULL);
1481                 ev_view_set_size (EV_VIEW (ev_window->priv->view),
1482                                   width - vsb_requisition.width - scrollbar_spacing, -1);
1483         }
1484 }
1485
1486 static void
1487 ev_window_set_sizing_mode (EvWindow     *ev_window,
1488                            EvSizingMode  sizing_mode)
1489 {
1490         GtkWidget *scrolled_window;
1491
1492         if (ev_window->priv->sizing_mode == sizing_mode)
1493                 return;
1494
1495         scrolled_window = ev_window->priv->scrolled_window;
1496         ev_window->priv->sizing_mode = sizing_mode;
1497
1498         g_signal_handlers_disconnect_by_func (scrolled_window, size_allocate_cb, ev_window);
1499
1500         switch (sizing_mode) {
1501         case EV_SIZING_BEST_FIT:
1502                 g_object_set (G_OBJECT (scrolled_window),
1503                               "hscrollbar-policy", GTK_POLICY_NEVER,
1504                               "vscrollbar-policy", GTK_POLICY_NEVER,
1505                               NULL);
1506                 g_signal_connect (scrolled_window, "size-allocate",
1507                                   G_CALLBACK (size_allocate_cb),
1508                                   ev_window);
1509                 break;
1510         case EV_SIZING_FIT_WIDTH:
1511                 g_object_set (G_OBJECT (scrolled_window),
1512                               "hscrollbar-policy", GTK_POLICY_NEVER,
1513                               "vscrollbar-policy", GTK_POLICY_AUTOMATIC,
1514                               NULL);
1515                 g_signal_connect (scrolled_window, "size-allocate",
1516                                   G_CALLBACK (size_allocate_cb),
1517                                   ev_window);
1518                 break;
1519         case EV_SIZING_FREE:
1520                 g_object_set (G_OBJECT (scrolled_window),
1521                               "hscrollbar-policy", GTK_POLICY_AUTOMATIC,
1522                               "vscrollbar-policy", GTK_POLICY_AUTOMATIC,
1523                               NULL);
1524                 break;
1525         }
1526
1527         update_sizing_buttons (ev_window);
1528 }
1529
1530 static void
1531 ev_window_cmd_help_about (GtkAction *action, EvWindow *ev_window)
1532 {
1533         const char *authors[] = {
1534                 N_("Many..."),
1535                 NULL
1536         };
1537
1538         const char *documenters[] = {
1539                 N_("Not so many..."),
1540                 NULL
1541         };
1542
1543         const char *license[] = {
1544                 N_("Evince is free software; you can redistribute it and/or modify\n"
1545                    "it under the terms of the GNU General Public License as published by\n"
1546                    "the Free Software Foundation; either version 2 of the License, or\n"
1547                    "(at your option) any later version.\n"),
1548                 N_("Evince is distributed in the hope that it will be useful,\n"
1549                    "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1550                    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
1551                    "GNU General Public License for more details.\n"),
1552                 N_("You should have received a copy of the GNU General Public License\n"
1553                    "along with Evince; if not, write to the Free Software Foundation, Inc.,\n"
1554                    "59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n")
1555         };
1556
1557         char *license_trans;
1558
1559 #ifdef ENABLE_NLS
1560         const char **p;
1561
1562         for (p = authors; *p; ++p)
1563                 *p = _(*p);
1564
1565         for (p = documenters; *p; ++p)
1566                 *p = _(*p);
1567 #endif
1568
1569         license_trans = g_strconcat (_(license[0]), "\n", _(license[1]), "\n",
1570                                      _(license[2]), "\n", NULL);
1571
1572         gtk_show_about_dialog (
1573                 GTK_WINDOW (ev_window),
1574                 "name", _("Evince"),
1575                 "version", VERSION,
1576                 "copyright",
1577                 _("\xc2\xa9 1996-2004 The Evince authors"),
1578                 "license", license_trans,
1579                 "website", "http://www.gnome.org/projects/evince",
1580                 "comments", _("PostScript and PDF File Viewer."),
1581                 "authors", authors,
1582                 "documenters", documenters,
1583                 "translator-credits", _("translator-credits"),
1584                 NULL);
1585
1586         g_free (license_trans);
1587 }
1588
1589 static void
1590 ev_window_view_toolbar_cb (GtkAction *action, EvWindow *ev_window)
1591 {
1592         update_chrome_flag (ev_window, EV_CHROME_TOOLBAR,
1593                             GCONF_CHROME_TOOLBAR,
1594                             gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
1595 }
1596
1597 static void
1598 ev_window_view_statusbar_cb (GtkAction *action, EvWindow *ev_window)
1599 {
1600         update_chrome_flag (ev_window, EV_CHROME_STATUSBAR,
1601                             GCONF_CHROME_STATUSBAR,
1602                             gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
1603 }
1604
1605 static void
1606 ev_window_view_sidebar_cb (GtkAction *action, EvWindow *ev_window)
1607 {
1608         update_chrome_flag (ev_window, EV_CHROME_SIDEBAR,
1609                             GCONF_CHROME_SIDEBAR,
1610                             gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
1611 }
1612
1613 static void
1614 ev_window_sidebar_visibility_changed_cb (EvSidebar *ev_sidebar, GParamSpec *pspec,
1615                                          EvWindow   *ev_window)
1616 {
1617         GtkAction *action;
1618         gboolean visible;
1619
1620         visible = GTK_WIDGET_VISIBLE (ev_sidebar);
1621
1622         /* In fullscreen mode the sidebar is not visible,
1623          * but we don't want to update the chrome
1624          */
1625         if (ev_window->priv->fullscreen_mode)
1626                 return;
1627         
1628         action = gtk_action_group_get_action (ev_window->priv->action_group, "ViewSidebar");
1629         
1630         g_signal_handlers_block_by_func
1631                 (action, G_CALLBACK (ev_window_view_sidebar_cb), ev_window);
1632         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), visible);
1633         g_signal_handlers_unblock_by_func
1634                 (action, G_CALLBACK (ev_window_view_sidebar_cb), ev_window);
1635
1636         update_chrome_flag (ev_window, EV_CHROME_SIDEBAR,
1637                             GCONF_CHROME_SIDEBAR, visible);
1638 }
1639
1640 static void
1641 menu_item_select_cb (GtkMenuItem *proxy, EvWindow *ev_window)
1642 {
1643         GtkAction *action;
1644         char *message;
1645
1646         action = g_object_get_data (G_OBJECT (proxy), "gtk-action");
1647         g_return_if_fail (action != NULL);
1648
1649         g_object_get (G_OBJECT (action), "tooltip", &message, NULL);
1650         if (message) {
1651                 gtk_statusbar_push (GTK_STATUSBAR (ev_window->priv->statusbar),
1652                                     ev_window->priv->help_message_cid, message);
1653                 g_free (message);
1654         }
1655 }
1656
1657 static void
1658 menu_item_deselect_cb (GtkMenuItem *proxy, EvWindow *ev_window)
1659 {
1660         gtk_statusbar_pop (GTK_STATUSBAR (ev_window->priv->statusbar),
1661                            ev_window->priv->help_message_cid);
1662 }
1663
1664 static void
1665 connect_proxy_cb (GtkUIManager *ui_manager, GtkAction *action,
1666                   GtkWidget *proxy, EvWindow *ev_window)
1667 {
1668         if (GTK_IS_MENU_ITEM (proxy)) {
1669                 g_signal_connect (proxy, "select",
1670                                   G_CALLBACK (menu_item_select_cb), ev_window);
1671                 g_signal_connect (proxy, "deselect",
1672                                   G_CALLBACK (menu_item_deselect_cb),
1673                                   ev_window);
1674         }
1675 }
1676
1677 static void
1678 disconnect_proxy_cb (GtkUIManager *ui_manager, GtkAction *action,
1679                      GtkWidget *proxy, EvWindow *ev_window)
1680 {
1681         if (GTK_IS_MENU_ITEM (proxy)) {
1682                 g_signal_handlers_disconnect_by_func
1683                         (proxy, G_CALLBACK (menu_item_select_cb), ev_window);
1684                 g_signal_handlers_disconnect_by_func
1685                         (proxy, G_CALLBACK (menu_item_deselect_cb), ev_window);
1686         }
1687 }
1688
1689 static void
1690 update_current_page (EvWindow *ev_window,
1691                      EvView   *view)
1692 {
1693         int page;
1694         GtkAction *action;
1695         EvSidebarThumbnails *thumbs;
1696
1697         thumbs = EV_SIDEBAR_THUMBNAILS (ev_window->priv->thumbs_sidebar);
1698         ev_sidebar_thumbnails_select_page (thumbs, ev_view_get_page (view));
1699
1700         action = gtk_action_group_get_action
1701                 (ev_window->priv->action_group, PAGE_SELECTOR_ACTION);
1702
1703         page = ev_view_get_page (EV_VIEW (ev_window->priv->view));
1704         ev_page_action_set_current_page (EV_PAGE_ACTION (action), page);
1705 }
1706
1707 static void
1708 view_page_changed_cb (EvView   *view,
1709                       EvWindow *ev_window)
1710 {
1711         update_current_page (ev_window, view);
1712         update_action_sensitivity (ev_window);
1713 }
1714
1715 static void
1716 view_status_changed_cb (EvView     *view,
1717                         GParamSpec *pspec,
1718                         EvWindow   *ev_window)
1719 {
1720         const char *message;
1721
1722         gtk_statusbar_pop (GTK_STATUSBAR (ev_window->priv->statusbar),
1723                            ev_window->priv->view_message_cid);
1724
1725         message = ev_view_get_status (view);
1726         if (message) {
1727                 gtk_statusbar_push (GTK_STATUSBAR (ev_window->priv->statusbar),
1728                                     ev_window->priv->view_message_cid, message);
1729         }
1730 }
1731
1732 static void
1733 view_find_status_changed_cb (EvView     *view,
1734                              GParamSpec *pspec,
1735                              EvWindow   *ev_window)
1736 {
1737         const char *text;
1738
1739         text = ev_view_get_find_status (view);
1740         egg_find_bar_set_status_text (EGG_FIND_BAR (ev_window->priv->find_bar),
1741                                       text);
1742 }
1743
1744 static void
1745 find_bar_previous_cb (EggFindBar *find_bar,
1746                       EvWindow   *ev_window)
1747 {
1748         ev_view_find_previous (EV_VIEW (ev_window->priv->view));
1749 }
1750
1751 static void
1752 find_bar_next_cb (EggFindBar *find_bar,
1753                   EvWindow   *ev_window)
1754 {
1755         ev_view_find_next (EV_VIEW (ev_window->priv->view));
1756 }
1757
1758 static void
1759 find_bar_close_cb (EggFindBar *find_bar,
1760                    EvWindow   *ev_window)
1761 {
1762         update_chrome_flag (ev_window, EV_CHROME_FINDBAR, NULL, FALSE);
1763 }
1764
1765 static void
1766 ev_window_page_mode_cb (GtkRadioAction *action,
1767                         GtkRadioAction *activated_action,
1768                         EvWindow       *window)
1769 {
1770         int mode;
1771
1772         mode = gtk_radio_action_get_current_value (action);
1773
1774         g_assert (mode == PAGE_MODE_CONTINUOUS_PAGE ||
1775                   mode == PAGE_MODE_SINGLE_PAGE);
1776
1777         ev_window_set_page_mode (window, (EvWindowPageMode) mode);
1778 }
1779
1780 static void
1781 find_bar_search_changed_cb (EggFindBar *find_bar,
1782                             GParamSpec *param,
1783                             EvWindow   *ev_window)
1784 {
1785         gboolean case_sensitive;
1786         gboolean visible;
1787         const char *search_string;
1788
1789         g_return_if_fail (EV_IS_WINDOW (ev_window));
1790
1791         /* Either the string or case sensitivity could have changed,
1792          * we connect this callback to both. We also connect it
1793          * to ::visible so when the find bar is hidden, we should
1794          * pretend the search string is NULL/""
1795          */
1796
1797         case_sensitive = egg_find_bar_get_case_sensitive (find_bar);
1798         visible = GTK_WIDGET_VISIBLE (find_bar);
1799         search_string = egg_find_bar_get_search_string (find_bar);
1800
1801 #if 0
1802         g_printerr ("search for '%s'\n", search_string ? search_string : "(nil)");
1803 #endif
1804
1805         if (ev_window->priv->document &&
1806             EV_IS_DOCUMENT_FIND (ev_window->priv->document)) {
1807                 if (visible && search_string) {
1808                         ev_document_find_begin (EV_DOCUMENT_FIND (ev_window->priv->document), search_string, case_sensitive);
1809                 } else {
1810                         ev_document_find_cancel (EV_DOCUMENT_FIND (ev_window->priv->document));
1811                         egg_find_bar_set_status_text (EGG_FIND_BAR (ev_window->priv->find_bar),
1812                                                       NULL);
1813                         gtk_widget_queue_draw (GTK_WIDGET (ev_window->priv->view));
1814                 }
1815         }
1816 }
1817
1818 static void
1819 ev_window_dispose (GObject *object)
1820 {
1821         EvWindow *window = EV_WINDOW (object);
1822         EvWindowPrivate *priv = window->priv;
1823
1824         if (priv->ui_manager) {
1825                 g_object_unref (priv->ui_manager);
1826                 priv->ui_manager = NULL;
1827         }
1828
1829         if (priv->action_group) {
1830                 g_object_unref (priv->action_group);
1831                 priv->action_group = NULL;
1832         }
1833
1834         if (priv->document) {
1835                 g_object_unref (priv->document);
1836                 priv->document = NULL;
1837         }
1838
1839         if (priv->view) {
1840                 g_object_unref (priv->view);
1841                 priv->view = NULL;
1842         }
1843
1844         if (priv->page_view) {
1845                 g_object_unref (priv->page_view);
1846                 priv->page_view = NULL;
1847         }
1848
1849         if (priv->password_document) {
1850                 g_object_unref (priv->password_document);
1851                 priv->password_document = NULL;
1852         }
1853         
1854         if (priv->password_uri) {
1855                 g_free (priv->password_uri);
1856                 priv->password_uri = NULL;
1857         }
1858
1859         destroy_fullscreen_popup (window);
1860
1861         G_OBJECT_CLASS (ev_window_parent_class)->dispose (object);
1862 }
1863
1864 static void
1865 ev_window_class_init (EvWindowClass *ev_window_class)
1866 {
1867         GObjectClass *g_object_class = G_OBJECT_CLASS (ev_window_class);
1868         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (ev_window_class);
1869
1870         g_object_class->dispose = ev_window_dispose;
1871
1872         widget_class->focus_in_event = ev_window_focus_in_event;
1873         widget_class->focus_out_event = ev_window_focus_out_event;
1874
1875         g_type_class_add_private (g_object_class, sizeof (EvWindowPrivate));
1876 }
1877
1878 /* Normal items */
1879 static GtkActionEntry entries[] = {
1880         { "File", NULL, N_("_File") },
1881         { "Edit", NULL, N_("_Edit") },
1882         { "View", NULL, N_("_View") },
1883         { "Go", NULL, N_("_Go") },
1884         { "Help", NULL, N_("_Help") },
1885
1886         /* File menu */
1887         { "FileOpen", GTK_STOCK_OPEN, NULL, "<control>O",
1888           N_("Open an existing document"),
1889           G_CALLBACK (ev_window_cmd_file_open) },
1890         { "FileSaveAs", GTK_STOCK_SAVE_AS, N_("_Save a Copy..."), NULL,
1891           N_("Save the current document with a new filename"),
1892           G_CALLBACK (ev_window_cmd_save_as) },
1893         { "FilePrint", GTK_STOCK_PRINT, N_("Print..."), "<control>P",
1894           N_("Print this document"),
1895           G_CALLBACK (ev_window_cmd_file_print) },
1896         { "FileCloseWindow", GTK_STOCK_CLOSE, NULL, "<control>W",
1897           N_("Close this window"),
1898           G_CALLBACK (ev_window_cmd_file_close_window) },
1899
1900         /* Edit menu */
1901         { "EditCopy", GTK_STOCK_COPY, NULL, "<control>C",
1902           N_("Copy text from the document"),
1903           G_CALLBACK (ev_window_cmd_edit_copy) },
1904         { "EditSelectAll", NULL, N_("Select _All"), "<control>A",
1905           N_("Select the entire page"),
1906           G_CALLBACK (ev_window_cmd_edit_select_all) },
1907         { "EditFind", GTK_STOCK_FIND, NULL, "<control>F",
1908           N_("Find a word or phrase in the document"),
1909           G_CALLBACK (ev_window_cmd_edit_find) },
1910
1911         /* View menu */
1912         { "ViewZoomIn", GTK_STOCK_ZOOM_IN, NULL, "<control>plus",
1913           N_("Enlarge the document"),
1914           G_CALLBACK (ev_window_cmd_view_zoom_in) },
1915         { "ViewZoomOut", GTK_STOCK_ZOOM_OUT, NULL, "<control>minus",
1916           N_("Shrink the document"),
1917           G_CALLBACK (ev_window_cmd_view_zoom_out) },
1918         { "ViewNormalSize", GTK_STOCK_ZOOM_100, NULL, "<control>0",
1919           N_("Reset the zoom level to the default value"),
1920           G_CALLBACK (ev_window_cmd_view_normal_size) },
1921
1922         /* Go menu */
1923         { "GoPreviousPage", GTK_STOCK_GO_BACK, N_("_Previous Page"), "Page_Up",
1924           N_("Go to the previous page"),
1925           G_CALLBACK (ev_window_cmd_go_previous_page) },
1926         { "GoNextPage", GTK_STOCK_GO_FORWARD, N_("_Next Page"), "Page_Down",
1927           N_("Go to the next page"),
1928           G_CALLBACK (ev_window_cmd_go_next_page) },
1929         { "GoFirstPage", GTK_STOCK_GOTO_FIRST, N_("_First Page"), "<control>Home",
1930           N_("Go to the first page"),
1931           G_CALLBACK (ev_window_cmd_go_first_page) },
1932         { "GoLastPage", GTK_STOCK_GOTO_LAST, N_("_Last Page"), "<control>End",
1933           N_("Go to the last page"),
1934           G_CALLBACK (ev_window_cmd_go_last_page) },
1935
1936         /* Help menu */
1937         { "HelpContents", GTK_STOCK_HELP, N_("_Contents"), NULL,
1938           N_("Display help for the viewer application"),
1939           G_CALLBACK (ev_window_cmd_help_contents) },
1940
1941         { "HelpAbout", GTK_STOCK_ABOUT, N_("_About"), NULL,
1942           N_("Display credits for the document viewer creators"),
1943           G_CALLBACK (ev_window_cmd_help_about) },
1944
1945         /* Toolbar-only */
1946         { "LeaveFullscreen", EV_STOCK_LEAVE_FULLSCREEN, N_("Leave Fullscreen"), "Escape",
1947           N_("Leave fullscreen mode"),
1948           G_CALLBACK (ev_window_cmd_leave_fullscreen) }
1949 };
1950
1951 /* Toggle items */
1952 static GtkToggleActionEntry toggle_entries[] = {
1953         /* View Menu */
1954         { "ViewToolbar", NULL, N_("_Toolbar"), "<shift><control>T",
1955           N_("Show or hide the toolbar"),
1956           G_CALLBACK (ev_window_view_toolbar_cb), TRUE },
1957         { "ViewStatusbar", NULL, N_("_Statusbar"), NULL,
1958           N_("Show or hide the statusbar"),
1959           G_CALLBACK (ev_window_view_statusbar_cb), TRUE },
1960         { "ViewSidebar", NULL, N_("Side _pane"), "F9",
1961           N_("Show or hide the side pane"),
1962           G_CALLBACK (ev_window_view_sidebar_cb), TRUE },
1963         { "ViewFullscreen", NULL, N_("_Fullscreen"), "F11",
1964           N_("Expand the window to fill the screen"),
1965           G_CALLBACK (ev_window_cmd_view_fullscreen) },
1966         { "ViewBestFit", EV_STOCK_ZOOM_PAGE, N_("_Best Fit"), NULL,
1967           N_("Make the current document fill the window"),
1968           G_CALLBACK (ev_window_cmd_view_best_fit) },
1969         { "ViewPageWidth", EV_STOCK_ZOOM_WIDTH, N_("Fit Page _Width"), NULL,
1970           N_("Make the current document fill the window width"),
1971           G_CALLBACK (ev_window_cmd_view_page_width) },
1972 };
1973
1974 static GtkRadioActionEntry page_view_entries[] = {
1975         { "SinglePage", GTK_STOCK_DND, N_("Single"), NULL,
1976           N_("Show the document one page at a time"),
1977           PAGE_MODE_SINGLE_PAGE },
1978         { "ContinuousPage", GTK_STOCK_DND_MULTIPLE, N_("Multi"), NULL,
1979           N_("Show the full document at once"),
1980           PAGE_MODE_CONTINUOUS_PAGE }
1981 };
1982
1983 static void
1984 goto_page_cb (GtkAction *action, int page_number, EvWindow *ev_window)
1985 {
1986         EvView *view = EV_VIEW (ev_window->priv->view);
1987
1988         if (ev_view_get_page (view) != page_number) {
1989                 ev_view_set_page (view, page_number);
1990         }
1991 }
1992
1993 static void
1994 drag_data_received_cb (GtkWidget *widget, GdkDragContext *context,
1995                        gint x, gint y, GtkSelectionData *selection_data,
1996                        guint info, guint time, gpointer gdata)
1997 {
1998         GList    *uri_list = NULL;
1999
2000         uri_list = gnome_vfs_uri_list_parse ((gchar *) selection_data->data);
2001
2002         if (uri_list) {
2003                 ev_window_open_uri_list (EV_WINDOW (widget), uri_list);
2004                 
2005                 gnome_vfs_uri_list_free (uri_list);
2006
2007                 gtk_drag_finish (context, TRUE, FALSE, time);
2008         }
2009 }
2010
2011 static void
2012 register_custom_actions (EvWindow *window, GtkActionGroup *group)
2013 {
2014         GtkAction *action;
2015
2016         action = g_object_new (EV_TYPE_PAGE_ACTION,
2017                                "name", PAGE_SELECTOR_ACTION,
2018                                "label", _("Page"),
2019                                "tooltip", _("Select Page"),
2020                                NULL);
2021         g_signal_connect (action, "goto_page",
2022                           G_CALLBACK (goto_page_cb), window);
2023         gtk_action_group_add_action (group, action);
2024         g_object_unref (action);
2025 }
2026
2027 static void
2028 set_action_properties (GtkActionGroup *action_group)
2029 {
2030         GtkAction *action;
2031
2032         action = gtk_action_group_get_action (action_group, "GoPreviousPage");
2033         /*translators: this is the label for toolbar button*/
2034         g_object_set (action, "short_label", _("Previous"), NULL);
2035         g_object_set (action, "is-important", TRUE, NULL);
2036         action = gtk_action_group_get_action (action_group, "GoNextPage");
2037         /*translators: this is the label for toolbar button*/
2038         g_object_set (action, "is-important", TRUE, NULL);
2039         g_object_set (action, "short_label", _("Next"), NULL);
2040         action = gtk_action_group_get_action (action_group, "ViewPageWidth");
2041         /*translators: this is the label for toolbar button*/
2042         g_object_set (action, "short_label", _("Fit Width"), NULL);
2043         action = gtk_action_group_get_action (action_group, "ViewZoomIn");
2044
2045         action = gtk_action_group_get_action (action_group, "LeaveFullscreen");
2046         g_object_set (action, "is-important", TRUE, NULL);
2047 }
2048
2049 static void
2050 set_chrome_actions (EvWindow *window)
2051 {
2052         EvWindowPrivate *priv = window->priv;
2053         GtkActionGroup *action_group = priv->action_group;
2054         GtkAction *action;
2055
2056         action= gtk_action_group_get_action (action_group, "ViewToolbar");
2057         g_signal_handlers_block_by_func
2058                 (action, G_CALLBACK (ev_window_view_toolbar_cb), window);
2059         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
2060                                       (priv->chrome & EV_CHROME_TOOLBAR) != 0);
2061         g_signal_handlers_unblock_by_func
2062                 (action, G_CALLBACK (ev_window_view_toolbar_cb), window);
2063
2064         action= gtk_action_group_get_action (action_group, "ViewSidebar");
2065         g_signal_handlers_block_by_func
2066                 (action, G_CALLBACK (ev_window_view_sidebar_cb), window);
2067         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
2068                                       (priv->chrome & EV_CHROME_SIDEBAR) != 0);
2069         g_signal_handlers_unblock_by_func
2070                 (action, G_CALLBACK (ev_window_view_sidebar_cb), window);
2071
2072         action= gtk_action_group_get_action (action_group, "ViewStatusbar");
2073         g_signal_handlers_block_by_func
2074                 (action, G_CALLBACK (ev_window_view_statusbar_cb), window);
2075         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
2076                                       (priv->chrome & EV_CHROME_STATUSBAR) != 0);
2077         g_signal_handlers_unblock_by_func
2078                 (action, G_CALLBACK (ev_window_view_statusbar_cb), window);
2079 }
2080
2081 static EvChrome
2082 load_chrome (void)
2083 {
2084         EvChrome chrome = EV_CHROME_NORMAL;
2085         GConfClient *client;
2086         GConfValue *value;
2087
2088         client = gconf_client_get_default ();
2089
2090         value = gconf_client_get (client, GCONF_CHROME_TOOLBAR, NULL);
2091         if (value != NULL) {
2092                 if (value->type == GCONF_VALUE_BOOL && !gconf_value_get_bool (value)) {
2093                         chrome &= ~EV_CHROME_TOOLBAR;
2094                 }
2095                 gconf_value_free (value);
2096         }
2097
2098         value = gconf_client_get (client, GCONF_CHROME_SIDEBAR, NULL);
2099         if (value != NULL) {
2100                 if (value->type == GCONF_VALUE_BOOL && !gconf_value_get_bool (value)) {
2101                         chrome &= ~EV_CHROME_SIDEBAR;
2102                 }
2103                 gconf_value_free (value);
2104         }
2105
2106         value = gconf_client_get (client, GCONF_CHROME_STATUSBAR, NULL);
2107         if (value != NULL) {
2108                 if (value->type == GCONF_VALUE_BOOL && !gconf_value_get_bool (value)) {
2109                         chrome &= ~EV_CHROME_STATUSBAR;
2110                 }
2111                 gconf_value_free (value);
2112         }
2113
2114         g_object_unref (client);
2115
2116         return chrome;
2117 }
2118
2119 static void
2120 ev_window_init (EvWindow *ev_window)
2121 {
2122         GtkActionGroup *action_group;
2123         GtkAccelGroup *accel_group;
2124         GError *error = NULL;
2125         GtkWidget *sidebar_widget, *toolbar_dock;
2126
2127         ev_window->priv = EV_WINDOW_GET_PRIVATE (ev_window);
2128
2129         ev_window->priv->page_mode = PAGE_MODE_SINGLE_PAGE;
2130         update_window_title (NULL, NULL, ev_window);
2131
2132         ev_window->priv->main_box = gtk_vbox_new (FALSE, 0);
2133         gtk_container_add (GTK_CONTAINER (ev_window), ev_window->priv->main_box);
2134         gtk_widget_show (ev_window->priv->main_box);
2135
2136         action_group = gtk_action_group_new ("MenuActions");
2137         ev_window->priv->action_group = action_group;
2138         gtk_action_group_set_translation_domain (action_group, NULL);
2139         gtk_action_group_add_actions (action_group, entries,
2140                                       G_N_ELEMENTS (entries), ev_window);
2141         gtk_action_group_add_toggle_actions (action_group, toggle_entries,
2142                                              G_N_ELEMENTS (toggle_entries),
2143                                              ev_window);
2144         gtk_action_group_add_radio_actions (action_group, page_view_entries,
2145                                             G_N_ELEMENTS (page_view_entries),
2146                                             ev_window->priv->page_mode,
2147                                             G_CALLBACK (ev_window_page_mode_cb),
2148                                             ev_window);
2149         set_action_properties (action_group);
2150         register_custom_actions (ev_window, action_group);
2151
2152         ev_window->priv->ui_manager = gtk_ui_manager_new ();
2153         gtk_ui_manager_insert_action_group (ev_window->priv->ui_manager,
2154                                             action_group, 0);
2155
2156         accel_group =
2157                 gtk_ui_manager_get_accel_group (ev_window->priv->ui_manager);
2158         gtk_window_add_accel_group (GTK_WINDOW (ev_window), accel_group);
2159
2160         g_signal_connect (ev_window->priv->ui_manager, "connect_proxy",
2161                           G_CALLBACK (connect_proxy_cb), ev_window);
2162         g_signal_connect (ev_window->priv->ui_manager, "disconnect_proxy",
2163                           G_CALLBACK (disconnect_proxy_cb), ev_window);
2164
2165         if (!gtk_ui_manager_add_ui_from_file (ev_window->priv->ui_manager,
2166                                               DATADIR"/evince-ui.xml",
2167                                               &error)) {
2168                 g_message ("building menus failed: %s", error->message);
2169                 g_error_free (error);
2170         }
2171
2172         ev_window->priv->menubar =
2173                  gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
2174                                             "/MainMenu");
2175         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box),
2176                             ev_window->priv->menubar,
2177                             FALSE, FALSE, 0);
2178
2179         /* This sucks, but there is no way to have a draw=no, expand=true separator
2180          * in a GtkUIManager-built toolbar. So, just add another toolbar.
2181          * See gtk+ bug 166489.
2182          */
2183         toolbar_dock = ev_window->priv->toolbar_dock = gtk_hbox_new (FALSE, 0);
2184         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), toolbar_dock,
2185                             FALSE, FALSE, 0);
2186         gtk_widget_show (toolbar_dock);
2187
2188         ev_window->priv->toolbar =
2189                 gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
2190                                            "/ToolBar");
2191         gtk_box_pack_start (GTK_BOX (toolbar_dock), ev_window->priv->toolbar,
2192                             TRUE, TRUE, 0);
2193         gtk_widget_show (ev_window->priv->toolbar);
2194
2195         ev_window->priv->fullscreen_toolbar =
2196                 gtk_ui_manager_get_widget (ev_window->priv->ui_manager, "/LeaveFullscreenToolbar");
2197         gtk_toolbar_set_show_arrow (GTK_TOOLBAR (ev_window->priv->fullscreen_toolbar), TRUE);
2198         gtk_toolbar_set_style (GTK_TOOLBAR (ev_window->priv->fullscreen_toolbar), GTK_TOOLBAR_BOTH_HORIZ);
2199
2200         /* Add the main area */
2201         ev_window->priv->hpaned = gtk_hpaned_new ();
2202         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), ev_window->priv->hpaned,
2203                             TRUE, TRUE, 0);     
2204         ev_window->priv->sidebar = ev_sidebar_new ();
2205         g_signal_connect (ev_window->priv->sidebar,
2206                           "notify::visible",
2207                           G_CALLBACK (ev_window_sidebar_visibility_changed_cb),
2208                           ev_window);
2209         gtk_paned_pack1 (GTK_PANED (ev_window->priv->hpaned),
2210                          ev_window->priv->sidebar, FALSE, FALSE);
2211         gtk_widget_show (ev_window->priv->hpaned);
2212
2213         /* Stub sidebar, for now */
2214         sidebar_widget = ev_sidebar_links_new ();
2215         gtk_widget_show (sidebar_widget);
2216         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
2217                              "index",
2218                              _("Index"),
2219                              sidebar_widget);
2220
2221         ev_window->priv->thumbs_sidebar = ev_sidebar_thumbnails_new ();
2222         gtk_widget_show (ev_window->priv->thumbs_sidebar);
2223         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
2224                              "thumbnails",
2225                              _("Thumbnails"),
2226                              ev_window->priv->thumbs_sidebar);
2227
2228         ev_window->priv->scrolled_window =
2229                 GTK_WIDGET (g_object_new (GTK_TYPE_SCROLLED_WINDOW,
2230                                           "shadow-type", GTK_SHADOW_IN,
2231                                           NULL));
2232         gtk_widget_show (ev_window->priv->scrolled_window);
2233
2234         gtk_paned_add2 (GTK_PANED (ev_window->priv->hpaned),
2235                         ev_window->priv->scrolled_window);
2236
2237         ev_window->priv->view = ev_view_new ();
2238         //ev_window->priv->page_view = ev_page_view_new ();
2239         ev_window->priv->password_view = ev_password_view_new ();
2240         g_signal_connect_swapped (ev_window->priv->password_view,
2241                                   "unlock",
2242                                   G_CALLBACK (ev_window_popup_password_dialog),
2243                                   ev_window);
2244         gtk_widget_show (ev_window->priv->view);
2245         //gtk_widget_show (ev_window->priv->page_view);
2246         gtk_widget_show (ev_window->priv->password_view);
2247
2248         /* We own a ref on these widgets, as we can swap them in and out */
2249         g_object_ref (ev_window->priv->view);
2250         //g_object_ref (ev_window->priv->page_view);
2251         g_object_ref (ev_window->priv->password_view);
2252
2253         gtk_container_add (GTK_CONTAINER (ev_window->priv->scrolled_window),
2254                            ev_window->priv->view);
2255         g_signal_connect (ev_window->priv->view,
2256                           "page-changed",
2257                           G_CALLBACK (view_page_changed_cb),
2258                           ev_window);
2259         g_signal_connect (ev_window->priv->view,
2260                           "notify::find-status",
2261                           G_CALLBACK (view_find_status_changed_cb),
2262                           ev_window);
2263         g_signal_connect (ev_window->priv->view,
2264                           "notify::status",
2265                           G_CALLBACK (view_status_changed_cb),
2266                           ev_window);
2267
2268         ev_window->priv->statusbar = gtk_statusbar_new ();
2269         gtk_box_pack_end (GTK_BOX (ev_window->priv->main_box),
2270                           ev_window->priv->statusbar,
2271                           FALSE, TRUE, 0);
2272         ev_window->priv->help_message_cid = gtk_statusbar_get_context_id
2273                 (GTK_STATUSBAR (ev_window->priv->statusbar), "help_message");
2274         ev_window->priv->view_message_cid = gtk_statusbar_get_context_id
2275                 (GTK_STATUSBAR (ev_window->priv->statusbar), "view_message");
2276
2277         ev_window->priv->find_bar = egg_find_bar_new ();
2278         gtk_box_pack_end (GTK_BOX (ev_window->priv->main_box),
2279                           ev_window->priv->find_bar,
2280                           FALSE, TRUE, 0);
2281
2282         ev_window->priv->chrome = load_chrome ();
2283         set_chrome_actions (ev_window);
2284         update_chrome_visibility (ev_window);
2285
2286         /* Connect to find bar signals */
2287         g_signal_connect (ev_window->priv->find_bar,
2288                           "previous",
2289                           G_CALLBACK (find_bar_previous_cb),
2290                           ev_window);
2291         g_signal_connect (ev_window->priv->find_bar,
2292                           "next",
2293                           G_CALLBACK (find_bar_next_cb),
2294                           ev_window);
2295         g_signal_connect (ev_window->priv->find_bar,
2296                           "close",
2297                           G_CALLBACK (find_bar_close_cb),
2298                           ev_window);
2299         g_signal_connect (ev_window->priv->find_bar,
2300                           "notify::search-string",
2301                           G_CALLBACK (find_bar_search_changed_cb),
2302                           ev_window);
2303         g_signal_connect (ev_window->priv->find_bar,
2304                           "notify::case-sensitive",
2305                           G_CALLBACK (find_bar_search_changed_cb),
2306                           ev_window);
2307         g_signal_connect (ev_window->priv->find_bar,
2308                           "notify::visible",
2309                           G_CALLBACK (find_bar_search_changed_cb),
2310                           ev_window);
2311
2312         g_signal_connect (ev_window, "window-state-event",
2313                           G_CALLBACK (ev_window_state_event_cb),
2314                           ev_window);
2315
2316         /* Give focus to the scrolled window */
2317         gtk_widget_grab_focus (ev_window->priv->scrolled_window);
2318
2319         /* Drag and Drop */
2320         gtk_drag_dest_unset (GTK_WIDGET (ev_window));
2321         gtk_drag_dest_set (GTK_WIDGET (ev_window), GTK_DEST_DEFAULT_ALL, ev_drop_types,
2322                            sizeof (ev_drop_types) / sizeof (ev_drop_types[0]),
2323                            GDK_ACTION_COPY);
2324         g_signal_connect (G_OBJECT (ev_window), "drag_data_received",
2325                           G_CALLBACK (drag_data_received_cb), NULL);
2326
2327         /* Set it to something random to force a change */
2328         ev_window->priv->sizing_mode = EV_SIZING_FREE;
2329         ev_window_set_sizing_mode (ev_window, EV_SIZING_BEST_FIT);
2330         update_action_sensitivity (ev_window);
2331 }