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