]> www.fi.muni.cz Git - evince.git/blob - shell/ev-window.c
4671e4edbd0d122e0b421b0f906450acf8795975
[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-types.h"
43 #include "ev-document-find.h"
44 #include "ev-document-security.h"
45 #include "ev-job-queue.h"
46 #include "eggfindbar.h"
47 #include "egg-recent-view-gtk.h"
48 #include "egg-recent-view.h"
49 #include "egg-recent-model.h"
50 #include "egg-toolbar-editor.h"
51 #include "egg-toolbars-model.h"
52 #include "egg-editable-toolbar.h"
53 #include "ephy-zoom.h"
54 #include "ephy-zoom-action.h"
55 #include "ev-application.h"
56 #include "ev-stock-icons.h"
57 #include "ev-file-helpers.h"
58 #include <poppler.h>
59
60 #include <glib/gi18n.h>
61 #include <gtk/gtk.h>
62 #include <gnome.h>
63
64 #include <libgnomevfs/gnome-vfs-uri.h>
65 #include <libgnomevfs/gnome-vfs-utils.h>
66 #include <libgnomevfs/gnome-vfs-ops.h>
67 #include <libgnomeprintui/gnome-print-dialog.h>
68
69 #include <gconf/gconf-client.h>
70
71 #include <string.h>
72
73 typedef enum {
74         PAGE_MODE_DOCUMENT,
75         PAGE_MODE_PASSWORD
76 } EvWindowPageMode;
77
78 typedef enum {
79         EV_CHROME_MENUBAR       = 1 << 0,
80         EV_CHROME_TOOLBAR       = 1 << 1,
81         EV_CHROME_SIDEBAR       = 1 << 2,
82         EV_CHROME_FINDBAR       = 1 << 3,
83         EV_CHROME_STATUSBAR     = 1 << 4,
84         EV_CHROME_NORMAL        = EV_CHROME_MENUBAR | EV_CHROME_TOOLBAR | EV_CHROME_SIDEBAR | EV_CHROME_STATUSBAR
85 } EvChrome;
86
87 struct _EvWindowPrivate {
88         GtkWidget *main_box;
89         GtkWidget *menubar;
90         GtkWidget *toolbar_dock;
91         GtkWidget *toolbar;
92         GtkWidget *hpaned;
93         GtkWidget *sidebar;
94         GtkWidget *find_bar;
95         GtkWidget *scrolled_window;
96         GtkWidget *view;
97         GtkWidget *page_view;
98         GtkWidget *password_view;
99
100         GtkActionGroup *action_group;
101         GtkUIManager *ui_manager;
102
103         gchar *toolbar_file;
104         EggToolbarsModel *toolbar_model;
105
106         GtkWidget *statusbar;
107         guint help_message_cid;
108         guint view_message_cid;
109         GtkWidget *fullscreen_toolbar;
110         GtkWidget *fullscreen_popup;
111         char *uri;
112
113         EvDocument *document;
114         EvPageCache *page_cache;
115
116         EvWindowPageMode page_mode;
117         /* These members are used temporarily when in PAGE_MODE_PASSWORD */
118         EvDocument *password_document;
119         GtkWidget *password_dialog;
120         char *password_uri;
121
122         EvChrome chrome;
123         GSource *fullscreen_timeout_source;
124
125         /* recent file stuff */
126         EggRecentModel *recent_model;
127         EggRecentViewGtk *recent_view;
128 };
129
130 static const GtkTargetEntry ev_drop_types[] = {
131         { "text/uri-list", 0, 0 }
132 };
133
134
135 #define EV_WINDOW_GET_PRIVATE(object) \
136         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_WINDOW, EvWindowPrivate))
137
138 #define PAGE_SELECTOR_ACTION    "PageSelector"
139 #define ZOOM_CONTROL_ACTION     "ViewZoom"
140
141 #define GCONF_CHROME_TOOLBAR    "/apps/evince/show_toolbar"
142 #define GCONF_CHROME_SIDEBAR    "/apps/evince/show_sidebar"
143 #define GCONF_CHROME_STATUSBAR  "/apps/evince/show_statusbar"
144
145 #define GCONF_SIDEBAR_SIZE      "/apps/evince/sidebar_size"
146
147 #define SIDEBAR_DEFAULT_SIZE    132
148
149 static void     ev_window_update_fullscreen_popup       (EvWindow         *window);
150 static void     ev_window_sidebar_visibility_changed_cb (EvSidebar        *ev_sidebar,
151                                                          GParamSpec       *pspec,
152                                                          EvWindow         *ev_window);
153 static void     ev_window_set_page_mode                 (EvWindow         *window,
154                                                          EvWindowPageMode  page_mode);
155 static gboolean start_loading_document                  (EvWindow         *ev_window,
156                                                          EvDocument       *document,
157                                                          const char       *uri);
158 static void     ev_window_sizing_mode_changed_cb        (EvView           *view,
159                                                          GParamSpec       *pspec,
160                                                          EvWindow         *ev_window);
161 static void     ev_window_zoom_changed_cb               (EvView           *view,
162                                                          GParamSpec       *pspec,
163                                                          EvWindow         *ev_window);
164 static void     ev_window_add_recent                    (EvWindow         *window,
165                                                          const char       *filename);
166 static void     ev_window_fullscreen                    (EvWindow         *window);
167 static void     ev_window_unfullscreen                  (EvWindow         *window);
168 static void     ev_window_cmd_view_fullscreen           (GtkAction        *action,
169                                                          EvWindow         *window);
170 static void     ev_window_run_presentation              (EvWindow         *window);
171 static void     ev_window_stop_presentation             (EvWindow         *window);
172 static void     ev_window_cmd_view_presentation         (GtkAction        *action,
173                                                          EvWindow         *window);
174
175
176 G_DEFINE_TYPE (EvWindow, ev_window, GTK_TYPE_WINDOW)
177
178 static void
179 set_action_sensitive (EvWindow   *ev_window,
180                       const char *name,
181                       gboolean    sensitive)
182 {
183         GtkAction *action = gtk_action_group_get_action (ev_window->priv->action_group,
184                                                          name);
185         gtk_action_set_sensitive (action, sensitive);
186 }
187
188 static void
189 update_action_sensitivity (EvWindow *ev_window)
190 {
191         EvView *view;
192         EvDocument *document;
193         EvWindowPageMode page_mode;
194         gboolean sensitive, has_pages = FALSE, has_document;
195         int n_pages = 0, page = -1;
196
197         view = EV_VIEW (ev_window->priv->view);
198         document = ev_window->priv->document;
199         page_mode = ev_window->priv->page_mode;
200         has_document = document != NULL;
201         if (has_document && ev_window->priv->page_cache) {
202                 page = ev_page_cache_get_current_page (ev_window->priv->page_cache);
203                 n_pages = ev_page_cache_get_n_pages (ev_window->priv->page_cache);
204                 has_pages = has_document && n_pages > 0;
205         }
206
207         /* File menu */
208         /* "FileOpen": always sensitive */
209         set_action_sensitive (ev_window, "FileSaveAs", has_document);
210         set_action_sensitive (ev_window, "FilePrint", has_pages);
211         /* "FileCloseWindow": always sensitive */
212
213         /* Edit menu */
214
215         sensitive = has_pages && ev_document_can_get_text (document);
216         set_action_sensitive (ev_window, "EditCopy", sensitive);
217         set_action_sensitive (ev_window, "EditSelectAll", sensitive);
218         set_action_sensitive (ev_window, "EditFind",
219                               has_pages && EV_IS_DOCUMENT_FIND (document));
220         set_action_sensitive (ev_window, "Slash",
221                               has_pages && EV_IS_DOCUMENT_FIND (document));
222         set_action_sensitive (ev_window, "EditFindNext",
223                               ev_view_can_find_next (view));
224
225         /* View menu */
226         set_action_sensitive (ev_window, "ViewContinuous", has_pages);
227         set_action_sensitive (ev_window, "ViewDual", has_pages);
228         set_action_sensitive (ev_window, "ViewZoomIn",
229                               has_pages && ev_view_can_zoom_in (view));
230         set_action_sensitive (ev_window, "ViewZoomOut",
231                               has_pages && ev_view_can_zoom_out (view));
232         set_action_sensitive (ev_window, "ViewBestFit", has_pages);
233         set_action_sensitive (ev_window, "ViewPageWidth", has_pages);
234         set_action_sensitive (ev_window, "ViewReload", has_pages);
235
236         /* Go menu */
237         if (document) {
238                 set_action_sensitive (ev_window, "GoPreviousPage", page > 0);
239                 set_action_sensitive (ev_window, "GoNextPage", page < n_pages - 1);
240                 set_action_sensitive (ev_window, "GoFirstPage", page > 0);
241                 set_action_sensitive (ev_window, "GoLastPage", page < n_pages - 1);
242         } else {
243                 set_action_sensitive (ev_window, "GoFirstPage", FALSE);
244                 set_action_sensitive (ev_window, "GoPreviousPage", FALSE);
245                 set_action_sensitive (ev_window, "GoNextPage", FALSE);
246                 set_action_sensitive (ev_window, "GoLastPage", FALSE);
247         }
248
249         /* Toolbar-specific actions: */
250         set_action_sensitive (ev_window, PAGE_SELECTOR_ACTION, has_pages);
251         set_action_sensitive (ev_window, ZOOM_CONTROL_ACTION,  has_pages);
252
253         if (has_pages && ev_view_get_sizing_mode (view) == EV_SIZING_FREE) {
254                 GtkAction *action;
255                 float      zoom;
256                 float      real_zoom;
257
258                 action = gtk_action_group_get_action (ev_window->priv->action_group, 
259                                                       ZOOM_CONTROL_ACTION);
260
261                 real_zoom = ev_view_get_zoom (EV_VIEW (ev_window->priv->view));
262                 zoom = ephy_zoom_get_nearest_zoom_level (real_zoom);
263
264                 ephy_zoom_action_set_zoom_level (EPHY_ZOOM_ACTION (action), zoom);
265         }
266 }
267
268 static void
269 set_widget_visibility (GtkWidget *widget, gboolean visible)
270 {
271         g_return_if_fail (GTK_IS_WIDGET (widget));
272         
273         if (visible)
274                 gtk_widget_show (widget);
275         else
276                 gtk_widget_hide (widget);
277 }
278
279 static void
280 update_chrome_visibility (EvWindow *window)
281 {
282         EvWindowPrivate *priv = window->priv;
283         gboolean menubar, toolbar, sidebar, findbar, statusbar, fullscreen_toolbar;
284         gboolean fullscreen_mode, presentation, fullscreen;
285
286         presentation = ev_view_get_presentation (EV_VIEW (priv->view));
287         fullscreen = ev_view_get_fullscreen (EV_VIEW (priv->view));
288         fullscreen_mode = fullscreen || presentation;
289
290         menubar = (priv->chrome & EV_CHROME_MENUBAR) != 0 && !fullscreen_mode;
291         toolbar = (priv->chrome & EV_CHROME_TOOLBAR) != 0 && !fullscreen_mode;
292         sidebar = (priv->chrome & EV_CHROME_SIDEBAR) != 0 && !fullscreen_mode;
293         fullscreen_toolbar = (priv->chrome & EV_CHROME_TOOLBAR) != 0;
294         statusbar = (priv->chrome & EV_CHROME_STATUSBAR) != 0 && !fullscreen_mode;
295         findbar = (priv->chrome & EV_CHROME_FINDBAR) != 0;
296
297         set_widget_visibility (priv->menubar, menubar);
298         
299         set_widget_visibility (priv->toolbar_dock, toolbar);
300         set_action_sensitive (window, "EditToolbar", toolbar);
301
302         set_widget_visibility (priv->sidebar, sidebar);
303         set_widget_visibility (priv->find_bar, findbar);
304         set_widget_visibility (priv->statusbar, statusbar);
305         set_widget_visibility (priv->fullscreen_toolbar, fullscreen_toolbar);
306
307         if (priv->fullscreen_popup != NULL) {
308                 set_widget_visibility (priv->fullscreen_popup, fullscreen);
309         }
310 }
311
312 static void
313 update_chrome_flag (EvWindow *window, EvChrome flag, const char *pref, gboolean active)
314 {
315         EvWindowPrivate *priv = window->priv;
316         GConfClient *client;
317         
318         if (active) {
319                 priv->chrome |= flag;
320         }
321         else {
322                 priv->chrome &= ~flag;
323         }
324
325         if (pref != NULL) {
326                 client = gconf_client_get_default ();
327                 gconf_client_set_bool (client, pref, active, NULL);
328                 g_object_unref (client);
329         }
330
331         update_chrome_visibility (window);
332 }
333
334 static void
335 ev_window_cmd_focus_page_selector (GtkAction *act, EvWindow *window)
336 {
337         GtkAction *action;
338         
339         action = gtk_action_group_get_action (window->priv->action_group,
340                                               PAGE_SELECTOR_ACTION);
341         ev_page_action_grab_focus (EV_PAGE_ACTION (action));
342 }
343
344 static void
345 ev_window_cmd_scroll_forward (GtkAction *action, EvWindow *window)
346 {
347         ev_view_scroll (EV_VIEW (window->priv->view), EV_SCROLL_PAGE_FORWARD);
348 }
349
350 static void
351 ev_window_cmd_scroll_backward (GtkAction *action, EvWindow *window)
352 {
353         ev_view_scroll (EV_VIEW (window->priv->view), EV_SCROLL_PAGE_BACKWARD);
354 }
355
356 static void
357 ev_window_cmd_continuous (GtkAction *action, EvWindow *ev_window)
358 {
359         gboolean continuous;
360
361         ev_window_stop_presentation (ev_window);
362         continuous = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
363         g_object_set (G_OBJECT (ev_window->priv->view),
364                       "continuous", continuous,
365                       NULL);
366         update_action_sensitivity (ev_window);
367 }
368
369 static void
370 ev_window_cmd_dual (GtkAction *action, EvWindow *ev_window)
371 {
372         gboolean dual_page;
373
374         ev_window_stop_presentation (ev_window);
375         dual_page = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
376         g_object_set (G_OBJECT (ev_window->priv->view),
377                       "dual-page", dual_page,
378                       NULL);
379         update_action_sensitivity (ev_window);
380 }
381
382 static void
383 ev_window_cmd_view_best_fit (GtkAction *action, EvWindow *ev_window)
384 {
385         ev_window_stop_presentation (ev_window);
386
387         if (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action))) {
388                 ev_view_set_sizing_mode (EV_VIEW (ev_window->priv->view), EV_SIZING_BEST_FIT);
389         } else {
390                 ev_view_set_sizing_mode (EV_VIEW (ev_window->priv->view), EV_SIZING_FREE);
391         }
392         update_action_sensitivity (ev_window);
393 }
394
395 static void
396 ev_window_cmd_view_page_width (GtkAction *action, EvWindow *ev_window)
397 {
398         ev_window_stop_presentation (ev_window);
399
400         if (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action))) {
401                 ev_view_set_sizing_mode (EV_VIEW (ev_window->priv->view), EV_SIZING_FIT_WIDTH);
402         } else {
403                 ev_view_set_sizing_mode (EV_VIEW (ev_window->priv->view), EV_SIZING_FREE);
404         }
405         update_action_sensitivity (ev_window);
406 }
407
408 static void
409 update_sizing_buttons (EvWindow *window)
410 {
411         GtkActionGroup *action_group = window->priv->action_group;
412         GtkAction *action;
413         gboolean best_fit, page_width;
414         EvSizingMode sizing_mode;
415
416         if (window->priv->view == NULL)
417                 return;
418
419         g_object_get (window->priv->view,
420                       "sizing_mode", &sizing_mode,
421                       NULL);
422
423         switch (sizing_mode) {
424         case EV_SIZING_BEST_FIT:
425                 best_fit = TRUE;
426                 page_width = FALSE;
427                 break;
428         case EV_SIZING_FIT_WIDTH:
429                 best_fit = FALSE;
430                 page_width = TRUE;
431                 break;
432
433         default:
434                 best_fit = page_width = FALSE;
435                 break;
436         }
437
438         action = gtk_action_group_get_action (action_group, "ViewBestFit");
439         g_signal_handlers_block_by_func
440                 (action, G_CALLBACK (ev_window_cmd_view_best_fit), window);
441         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), best_fit);
442         g_signal_handlers_unblock_by_func
443                 (action, G_CALLBACK (ev_window_cmd_view_best_fit), window);
444
445         action = gtk_action_group_get_action (action_group, "ViewPageWidth");   
446         g_signal_handlers_block_by_func
447                 (action, G_CALLBACK (ev_window_cmd_view_page_width), window);
448         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), page_width);
449         g_signal_handlers_unblock_by_func
450                 (action, G_CALLBACK (ev_window_cmd_view_page_width), window);
451
452         action = gtk_action_group_get_action (window->priv->action_group, 
453                                               ZOOM_CONTROL_ACTION);     
454         if (best_fit) {
455                 ephy_zoom_action_set_zoom_level (EPHY_ZOOM_ACTION (action), 
456                                                  EPHY_ZOOM_BEST_FIT);
457         } else if (page_width) {
458                 ephy_zoom_action_set_zoom_level (EPHY_ZOOM_ACTION (action), 
459                                                  EPHY_ZOOM_FIT_WIDTH);
460         }
461 }
462
463 void
464 ev_window_open_page_label (EvWindow   *ev_window, 
465                            const char *label)
466 {
467         if (ev_window->priv->page_cache) {
468                 ev_page_cache_set_page_label (ev_window->priv->page_cache, 
469                                               label);
470         }
471 }
472
473 gboolean
474 ev_window_is_empty (const EvWindow *ev_window)
475 {
476         g_return_val_if_fail (EV_IS_WINDOW (ev_window), FALSE);
477
478         return ev_window->priv->document == NULL;
479 }
480
481 static void
482 unable_to_load (EvWindow   *ev_window,
483                 const char *error_message)
484 {
485         GtkWidget *dialog;
486
487         dialog = gtk_message_dialog_new (GTK_WINDOW (ev_window),
488                                          GTK_DIALOG_DESTROY_WITH_PARENT,
489                                          GTK_MESSAGE_ERROR,
490                                          GTK_BUTTONS_CLOSE,
491                                          _("Unable to open document"));
492         gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
493                                                   "%s", error_message);
494         gtk_dialog_run (GTK_DIALOG (dialog));
495         gtk_widget_destroy (dialog);
496 }
497
498 static void
499 update_window_title (EvDocument *document, GParamSpec *pspec, EvWindow *ev_window)
500 {
501         char *title = NULL;
502         char *doc_title = NULL;
503         gboolean password_needed;
504
505         password_needed = (ev_window->priv->password_document != NULL);
506         if (document && ev_window->priv->page_cache) {
507                 doc_title = ev_page_cache_get_title (ev_window->priv->page_cache);
508
509                 /* Make sure we get a valid title back */
510                 if (doc_title) {
511                         if (doc_title[0] == '\000' ||
512                             !g_utf8_validate (doc_title, -1, NULL)) {
513                                 g_free (doc_title);
514                                 doc_title = NULL;
515                         }
516                 }
517         }
518
519         if (doc_title) {
520                 char *p;
521
522                 for (p = doc_title; *p; ++p) {
523                         /* an '\n' byte is always ASCII, no need for UTF-8 special casing */
524                         if (*p == '\n')
525                                 *p = ' ';
526                 }
527         }
528
529         if (doc_title == NULL && ev_window->priv->uri) {
530                 char *basename;
531
532                 basename = g_path_get_basename (ev_window->priv->uri);
533                 doc_title = gnome_vfs_unescape_string_for_display (basename);
534                 g_free (basename);
535         }
536
537         if (password_needed) {
538                 if (doc_title == NULL) {
539                         title = g_strdup (_("Document Viewer - Password Required"));
540                 } else {
541                         title = g_strdup_printf (_("%s - Password Required"), doc_title);
542                 }
543         } else {
544                 if (doc_title == NULL) {
545                         title = g_strdup (_("Document Viewer"));
546                 } else {
547                         title = g_strdup (doc_title);
548                 }
549         }
550
551         gtk_window_set_title (GTK_WINDOW (ev_window), title);
552
553         g_free (doc_title);
554         g_free (title);
555 }
556
557 static void
558 find_changed_cb (EvDocument *document, int page, EvWindow *ev_window)
559 {
560         update_action_sensitivity (ev_window);
561 }
562
563 static void
564 page_changed_cb (EvPageCache *page_cache,
565                  gint         page,
566                  EvWindow    *ev_window)
567 {
568         update_action_sensitivity (ev_window);
569 }
570
571 static void
572 update_document_mode (EvWindow *window, EvDocumentMode mode)
573 {
574         if (mode == EV_DOCUMENT_MODE_FULL_SCREEN) {
575                 ev_window_fullscreen (window);
576         } else if (mode == EV_DOCUMENT_MODE_PRESENTATION) {
577                 ev_window_run_presentation (window);
578         }
579 }
580
581 static void
582 ev_window_setup_document (EvWindow *ev_window)
583 {
584         EvDocumentInfo *info;
585         EvDocument *document;
586         EvView *view = EV_VIEW (ev_window->priv->view);
587         EvSidebar *sidebar = EV_SIDEBAR (ev_window->priv->sidebar);
588         GtkAction *action;
589
590         document = ev_window->priv->document;
591         ev_window->priv->page_cache = ev_document_get_page_cache (ev_window->priv->document);
592         g_signal_connect (ev_window->priv->page_cache, "page-changed", G_CALLBACK (page_changed_cb), ev_window);
593
594         g_signal_connect_object (G_OBJECT (document),
595                                  "notify::title",
596                                  G_CALLBACK (update_window_title),
597                                  ev_window, 0);
598         if (EV_IS_DOCUMENT_FIND (document)) {
599                 g_signal_connect_object (G_OBJECT (document),
600                                          "find_changed",
601                                          G_CALLBACK (find_changed_cb),  
602                                          ev_window, 0);
603         }
604
605         ev_window_set_page_mode (ev_window, PAGE_MODE_DOCUMENT);
606
607         ev_sidebar_set_document (sidebar, document);
608
609         if (ev_page_cache_get_n_pages (ev_window->priv->page_cache) > 0) {
610                 ev_view_set_document (view, document);
611         }
612
613         update_window_title (document, NULL, ev_window);
614         action = gtk_action_group_get_action (ev_window->priv->action_group, PAGE_SELECTOR_ACTION);
615         ev_page_action_set_document (EV_PAGE_ACTION (action), document);
616         update_action_sensitivity (ev_window);
617
618         info = ev_document_get_info (document);
619         update_document_mode (ev_window, info->mode);
620         ev_document_info_free (info);
621 }
622
623 static void
624 password_dialog_response (GtkWidget *password_dialog,
625                           gint       response_id,
626                           EvWindow  *ev_window)
627 {
628         char *password;
629         
630         if (response_id == GTK_RESPONSE_OK) {
631                 EvDocument *document;
632                 gchar *uri;
633
634                 password = ev_password_dialog_get_password (password_dialog);
635                 if (password) {
636                         ev_document_doc_mutex_lock ();
637                         ev_document_security_set_password (EV_DOCUMENT_SECURITY (ev_window->priv->password_document),
638                                                            password);
639                         ev_document_doc_mutex_unlock ();
640                 }
641                 g_free (password);
642
643                 document = ev_window->priv->password_document;
644                 uri = ev_window->priv->password_uri;
645
646                 ev_window->priv->password_document = NULL;
647                 ev_window->priv->password_uri = NULL;
648
649                 if (start_loading_document (ev_window, document, uri)) {
650                         gtk_widget_destroy (password_dialog);
651                 }
652
653                 g_object_unref (document);
654                 g_free (uri);
655
656                 return;
657         }
658
659         gtk_widget_set_sensitive (ev_window->priv->password_view, TRUE);
660         gtk_widget_destroy (password_dialog);
661 }
662
663 /* Called either by start_loading_document or by the "unlock" callback on the
664  * password_view page.  It assumes that ev_window->priv->password_* has been set
665  * correctly.  These are cleared by password_dialog_response() */
666
667 static void
668 ev_window_popup_password_dialog (EvWindow *ev_window)
669 {
670         g_assert (ev_window->priv->password_document);
671         g_assert (ev_window->priv->password_uri);
672
673         gtk_widget_set_sensitive (ev_window->priv->password_view, FALSE);
674
675         update_window_title (ev_window->priv->password_document, NULL, ev_window);
676         if (ev_window->priv->password_dialog == NULL) {
677                 gchar *basename, *file_name;
678
679                 basename = g_path_get_basename (ev_window->priv->password_uri);
680                 file_name = gnome_vfs_unescape_string_for_display (basename);
681                 ev_window->priv->password_dialog =
682                         ev_password_dialog_new (GTK_WIDGET (ev_window), file_name);
683                 g_object_add_weak_pointer (G_OBJECT (ev_window->priv->password_dialog),
684                                            (gpointer *) &(ev_window->priv->password_dialog));
685                 g_signal_connect (ev_window->priv->password_dialog,
686                                   "response",
687                                   G_CALLBACK (password_dialog_response),
688                                   ev_window);
689                 g_free (basename);
690                 g_free (file_name);
691                 gtk_widget_show (ev_window->priv->password_dialog);
692         } else {
693                 ev_password_dialog_set_bad_pass (ev_window->priv->password_dialog);
694         }
695 }
696
697 /* This wil try to load the document.  It might be called multiple times on the
698  * same document by the password dialog.
699  *
700  * Since the flow of the error dialog is very confusing, we assume that both
701  * document and uri will go away after this function is called, and thus we need
702  * to ref/dup them.  Additionally, it needs to clear
703  * ev_window->priv->password_{uri,document}, and thus people who call this
704  * function should _not_ necessarily expect those to exist after being
705  * called. */
706 static gboolean
707 start_loading_document (EvWindow   *ev_window,
708                         EvDocument *document,
709                         const char *uri)
710 {
711         gboolean result;
712         GError *error = NULL;
713
714         g_assert (document);
715         g_assert (document != ev_window->priv->document);
716         g_assert (uri);
717         if (ev_window->priv->password_document) {
718                 g_object_unref (ev_window->priv->password_document);
719                 ev_window->priv->password_document = NULL;
720         }
721         if (ev_window->priv->password_uri) {
722                 g_free (ev_window->priv->password_uri);
723                 ev_window->priv->password_uri = NULL;
724         }
725
726         result = ev_document_load (document, uri, &error);
727
728         /* Success! */
729         if (result) {
730                 if (ev_window->priv->document)
731                         g_object_unref (ev_window->priv->document);
732                 ev_window->priv->document = g_object_ref (document);
733                 ev_window_setup_document (ev_window);
734                 
735                 ev_window_add_recent (ev_window, uri);
736
737                 return TRUE;
738         }
739
740         /* unable to load the document */
741         g_assert (error != NULL);
742
743         if (error->domain == EV_DOCUMENT_ERROR &&
744             error->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
745                 gchar *base_name, *file_name;
746
747                 ev_window->priv->password_document = g_object_ref (document);
748                 ev_window->priv->password_uri = g_strdup (uri);
749
750                 base_name = g_path_get_basename (uri);
751                 file_name = gnome_vfs_unescape_string_for_display (base_name);
752
753                 ev_password_view_set_file_name (EV_PASSWORD_VIEW (ev_window->priv->password_view),
754                                                 file_name);
755                 g_free (file_name);
756                 g_free (base_name);
757                 ev_window_set_page_mode (ev_window, PAGE_MODE_PASSWORD);
758
759                 ev_window_popup_password_dialog (ev_window);
760         } else {
761                 unable_to_load (ev_window, error->message);
762         }
763         g_error_free (error);
764
765         return FALSE;
766 }
767
768 static gboolean
769 sanity_check_uri (EvWindow *window, const char *uri)
770 {
771         gboolean result = FALSE;
772         GnomeVFSURI *vfs_uri;
773         char *err;
774
775         vfs_uri = gnome_vfs_uri_new (uri);
776         if (vfs_uri) {
777                 if (gnome_vfs_uri_exists (vfs_uri)) {
778                         result = TRUE;
779                 }
780         }
781
782         if (!result) {
783                 err = g_strdup_printf (_("The file %s does not exist."), uri);
784                 unable_to_load (window, err);
785                 g_free (err);
786         }
787
788         return result;
789 }
790
791 void
792 ev_window_open (EvWindow *ev_window, const char *uri)
793 {
794         EvDocument *document = NULL;
795         GType document_type;
796         char *mime_type = NULL;
797
798         if (!sanity_check_uri (ev_window, uri)) {
799                 return;
800         }
801
802         g_free (ev_window->priv->uri);
803         ev_window->priv->uri = g_strdup (uri);
804
805         document_type = ev_document_type_lookup (uri, &mime_type);
806         if (document_type != G_TYPE_INVALID) {
807                 document = g_object_new (document_type, NULL);
808         }
809
810         if (document) {
811                 start_loading_document (ev_window, document, uri);
812                 /* See the comment on start_loading_document on ref counting.
813                  * As the password dialog flow is confusing, we're very explicit
814                  * on ref counting. */
815                 g_object_unref (document);
816         } else {
817                 char *error_message;
818
819                 error_message = g_strdup_printf (_("Unhandled MIME type: '%s'"),
820                                                  mime_type?mime_type:"<Unknown MIME Type>");
821                 unable_to_load (ev_window, error_message);
822                 g_free (error_message);
823         }
824
825         g_free (mime_type);
826 }
827
828 static void
829 ev_window_open_uri_list (EvWindow *ev_window, GList *uri_list)
830 {
831         GList *list;
832         gchar *uri;
833         
834         g_return_if_fail (uri_list != NULL);
835         
836         list = uri_list;
837         while (list) {
838                 uri = gnome_vfs_uri_to_string (list->data, GNOME_VFS_URI_HIDE_NONE);
839                 
840                 if (ev_document_type_lookup (uri, NULL) != G_TYPE_INVALID) {
841                         if (ev_window_is_empty (EV_WINDOW (ev_window))) {
842                                 ev_window_open (ev_window, uri);
843                                 
844                                 gtk_widget_show (GTK_WIDGET (ev_window));
845                         } else {
846                                 EvWindow *new_window;
847                                 
848                                 new_window = ev_application_new_window (EV_APP);
849                                 ev_window_open (new_window, uri);
850                                 
851                                 gtk_widget_show (GTK_WIDGET (new_window));
852                         }
853                 }
854
855                 g_free (uri);
856
857                 list = g_list_next (list);
858         }
859 }
860
861 static void
862 ev_window_cmd_file_open (GtkAction *action, EvWindow *ev_window)
863 {
864         ev_application_open (EV_APP, NULL);
865 }
866
867 static void
868 ev_window_cmd_recent_file_activate (EggRecentViewGtk *view, EggRecentItem *item,
869                                     EvWindow *ev_window)
870 {
871         char *uri;
872         GtkWidget *window;
873
874         uri = egg_recent_item_get_uri (item);
875
876         window = GTK_WIDGET (ev_application_get_empty_window (EV_APP));
877         gtk_widget_show (window);
878         ev_window_open (EV_WINDOW (window), uri);
879         
880         g_free (uri);
881 }
882
883 static void
884 ev_window_add_recent (EvWindow *window, const char *filename)
885 {
886         EggRecentItem *item;
887
888         if (strstr (filename, "file:///") == NULL)
889                 return;
890
891         item = egg_recent_item_new_from_uri (filename);
892         egg_recent_item_add_group (item, "Evince");
893         egg_recent_model_add_full (window->priv->recent_model, item);
894 }
895
896 static void
897 ev_window_setup_recent (EvWindow *ev_window)
898 {
899         GtkWidget *menu_item;
900         GtkWidget *menu;
901
902         menu_item = gtk_ui_manager_get_widget (ev_window->priv->ui_manager, "/MainMenu/FileMenu");
903         menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menu_item));
904         menu_item = gtk_ui_manager_get_widget (ev_window->priv->ui_manager, "/MainMenu/FileMenu/RecentFilesMenu");
905
906         g_return_if_fail (menu != NULL);
907         g_return_if_fail (menu_item != NULL);
908
909         /* it would be better if we just filtered by mime-type, but there
910          * doesn't seem to be an easy way to figure out which mime-types we
911          * can handle */
912         ev_window->priv->recent_model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU);
913
914         ev_window->priv->recent_view = egg_recent_view_gtk_new (menu, menu_item);
915         egg_recent_view_gtk_show_icons (EGG_RECENT_VIEW_GTK (ev_window->priv->recent_view), FALSE);
916         egg_recent_model_set_limit (ev_window->priv->recent_model, 5);
917
918         egg_recent_view_set_model (EGG_RECENT_VIEW (ev_window->priv->recent_view),
919                                    ev_window->priv->recent_model);
920
921         egg_recent_model_set_filter_groups (ev_window->priv->recent_model,
922                                             "Evince", NULL);
923
924         egg_recent_view_gtk_set_trailing_sep (ev_window->priv->recent_view, TRUE);
925         
926         g_signal_connect (ev_window->priv->recent_view, "activate",
927                         G_CALLBACK (ev_window_cmd_recent_file_activate), ev_window);
928 }
929
930 /* FIXME
931 static gboolean
932 overwrite_existing_file (GtkWindow *window, const gchar *file_name)
933 {
934         GtkWidget *msgbox;
935         gchar *utf8_file_name;
936         AtkObject *obj;
937         gint ret;
938
939         utf8_file_name = g_filename_to_utf8 (file_name, -1, NULL, NULL, NULL);
940         msgbox = gtk_message_dialog_new (
941                 window,
942                 (GtkDialogFlags)GTK_DIALOG_DESTROY_WITH_PARENT,
943                 GTK_MESSAGE_QUESTION,
944                 GTK_BUTTONS_NONE,
945                 _("A file named \"%s\" already exists."),
946                 utf8_file_name);
947         g_free (utf8_file_name);
948
949         gtk_message_dialog_format_secondary_text (
950                 GTK_MESSAGE_DIALOG (msgbox),
951                 _("Do you want to replace it with the one you are saving?"));
952
953         gtk_dialog_add_button (GTK_DIALOG (msgbox),
954                                GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
955
956         gtk_dialog_add_button (GTK_DIALOG (msgbox),
957                                _("_Replace"), GTK_RESPONSE_YES);
958
959         gtk_dialog_set_default_response (GTK_DIALOG (msgbox),
960                                          GTK_RESPONSE_CANCEL);
961
962         obj = gtk_widget_get_accessible (msgbox);
963
964         if (GTK_IS_ACCESSIBLE (obj))
965                 atk_object_set_name (obj, _("Question"));
966
967         ret = gtk_dialog_run (GTK_DIALOG (msgbox));
968         gtk_widget_destroy (msgbox);
969
970         return (ret == GTK_RESPONSE_YES);
971 }
972 */
973
974 static void
975 save_error_dialog (GtkWindow *window, const gchar *file_name)
976 {
977         GtkWidget *error_dialog;
978
979         error_dialog = gtk_message_dialog_new (
980                 window,
981                 (GtkDialogFlags)GTK_DIALOG_DESTROY_WITH_PARENT,
982                 GTK_MESSAGE_ERROR,
983                 GTK_BUTTONS_CLOSE,
984                 _("The file could not be saved as \"%s\"."),
985                 file_name);
986
987         /* Easy way to make the text bold while keeping the string
988          * above free from pango markup (FIXME ?) */
989         gtk_message_dialog_format_secondary_text (
990                 GTK_MESSAGE_DIALOG (error_dialog), " ");
991
992         gtk_dialog_run (GTK_DIALOG (error_dialog));
993         gtk_widget_destroy (error_dialog);
994 }
995
996 static void
997 ev_window_cmd_save_as (GtkAction *action, EvWindow *ev_window)
998 {
999         GtkWidget *fc;
1000         GtkFileFilter *pdf_filter, *all_filter;
1001         gchar *uri = NULL;
1002         gboolean success;
1003
1004         fc = gtk_file_chooser_dialog_new (
1005                 _("Save a Copy"),
1006                 NULL, GTK_FILE_CHOOSER_ACTION_SAVE,
1007                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1008                 GTK_STOCK_SAVE, GTK_RESPONSE_OK,
1009                 NULL);
1010         gtk_window_set_modal (GTK_WINDOW (fc), TRUE);
1011
1012         pdf_filter = gtk_file_filter_new ();
1013         gtk_file_filter_set_name (pdf_filter, _("PDF Documents"));
1014         gtk_file_filter_add_mime_type (pdf_filter, "application/pdf");
1015         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (fc), pdf_filter);
1016
1017         all_filter = gtk_file_filter_new ();
1018         gtk_file_filter_set_name (all_filter, _("All Files"));
1019         gtk_file_filter_add_pattern (all_filter, "*");
1020         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (fc), all_filter);
1021         gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (fc), pdf_filter);
1022
1023         gtk_dialog_set_default_response (GTK_DIALOG (fc), GTK_RESPONSE_OK);
1024
1025         gtk_widget_show (fc);
1026
1027         while (gtk_dialog_run (GTK_DIALOG (fc)) == GTK_RESPONSE_OK) {
1028                 uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (fc));
1029
1030 /* FIXME
1031                 if (g_file_test (uri, G_FILE_TEST_EXISTS) &&
1032                     !overwrite_existing_file (GTK_WINDOW (fc), uri))
1033                                 continue;
1034 */
1035                 
1036                 ev_document_doc_mutex_lock ();
1037                 success = ev_document_save (ev_window->priv->document, uri, NULL);
1038                 ev_document_doc_mutex_unlock ();
1039
1040                 if (success)
1041                         break;
1042                 else
1043                         save_error_dialog (GTK_WINDOW (fc), uri);
1044         }
1045         gtk_widget_destroy (fc);
1046 }
1047
1048 static gboolean
1049 using_postscript_printer (GnomePrintConfig *config)
1050 {
1051         const guchar *driver;
1052         const guchar *transport;
1053
1054         driver = gnome_print_config_get (
1055                 config, (const guchar *)"Settings.Engine.Backend.Driver");
1056
1057         transport = gnome_print_config_get (
1058                 config, (const guchar *)"Settings.Transport.Backend");
1059
1060         if (driver) {
1061                 if (!strcmp ((const gchar *)driver, "gnome-print-ps"))
1062                         return TRUE;
1063                 else
1064                         return FALSE;
1065         } else  if (transport) {
1066                 if (!strcmp ((const gchar *)transport, "CUPS"))
1067                         return TRUE;
1068                 else if (!strcmp ((const gchar *)transport, "LPD"))
1069                         return TRUE;
1070         }
1071
1072         return FALSE;
1073 }
1074
1075 static void
1076 ev_window_print (EvWindow *window)
1077 {
1078         EvPageCache *page_cache;
1079         int last_page;
1080
1081         page_cache = ev_document_get_page_cache (window->priv->document);
1082         last_page = ev_page_cache_get_n_pages (page_cache);
1083
1084         ev_window_print_range (window, 1, -1);
1085 }
1086
1087 void
1088 ev_window_print_range (EvWindow *ev_window, int first_page, int last_page)
1089 {
1090         GnomePrintConfig *config;
1091         GnomePrintJob *job;
1092         GtkWidget *print_dialog;
1093         gchar *pages_label;
1094         EvPrintJob *print_job = NULL;
1095         EvPageCache *page_cache;
1096
1097         g_return_if_fail (EV_IS_WINDOW (ev_window));
1098         g_return_if_fail (ev_window->priv->document != NULL);
1099
1100         page_cache = ev_document_get_page_cache (ev_window->priv->document);
1101         if (last_page == -1) {
1102                 last_page = ev_page_cache_get_n_pages (page_cache);
1103         }
1104
1105         config = gnome_print_config_default ();
1106         job = gnome_print_job_new (config);
1107
1108         print_dialog = gnome_print_dialog_new (job, (guchar *) _("Print"),
1109                                                (GNOME_PRINT_DIALOG_RANGE |
1110                                                 GNOME_PRINT_DIALOG_COPIES));
1111
1112         pages_label = g_strconcat (_("Pages"), " ", NULL);
1113         gnome_print_dialog_construct_range_page (GNOME_PRINT_DIALOG (print_dialog),
1114                                                  GNOME_PRINT_RANGE_ALL |
1115                                                  GNOME_PRINT_RANGE_RANGE,
1116                                                  first_page, last_page,
1117                                                  NULL, (const guchar *)pages_label);
1118         g_free (pages_label);
1119                                                  
1120         gtk_dialog_set_response_sensitive (GTK_DIALOG (print_dialog),
1121                                            GNOME_PRINT_DIALOG_RESPONSE_PREVIEW,
1122                                            FALSE);
1123
1124         while (TRUE) {
1125                 int response;
1126                 response = gtk_dialog_run (GTK_DIALOG (print_dialog));
1127
1128                 if (response != GNOME_PRINT_DIALOG_RESPONSE_PRINT)
1129                         break;
1130
1131                 /* FIXME: Change this when we have the first backend
1132                  * that can print more than postscript
1133                  */
1134                 if (!using_postscript_printer (config)) {
1135                         GtkWidget *dialog;
1136
1137                         dialog = gtk_message_dialog_new (
1138                                 GTK_WINDOW (print_dialog), GTK_DIALOG_MODAL,
1139                                 GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
1140                                 _("Printing is not supported on this printer."));
1141                         gtk_message_dialog_format_secondary_text (
1142                                 GTK_MESSAGE_DIALOG (dialog),
1143                                 _("You were trying to print to a printer using the \"%s\" driver. This program requires a PostScript printer driver."),
1144                                 gnome_print_config_get (
1145                                         config, (guchar *)"Settings.Engine.Backend.Driver"));
1146                         gtk_dialog_run (GTK_DIALOG (dialog));
1147                         gtk_widget_destroy (dialog);
1148
1149                         continue;
1150                 }
1151
1152                 print_job = g_object_new (EV_TYPE_PRINT_JOB,
1153                                           "gnome_print_job", job,
1154                                           "document", ev_window->priv->document,
1155                                           "print_dialog", print_dialog,
1156                                           NULL);
1157                 break;
1158         }
1159
1160         gtk_widget_destroy (print_dialog);
1161
1162         if (print_job != NULL) {
1163                 ev_print_job_print (print_job, GTK_WINDOW (ev_window));
1164                 g_object_unref (print_job);
1165         }
1166 }
1167
1168 static void
1169 ev_window_cmd_file_print (GtkAction *action, EvWindow *ev_window)
1170 {
1171         ev_window_print (ev_window);
1172 }
1173
1174 static void
1175 ev_window_cmd_file_close_window (GtkAction *action, EvWindow *ev_window)
1176 {
1177         g_return_if_fail (EV_IS_WINDOW (ev_window));
1178
1179         gtk_widget_destroy (GTK_WIDGET (ev_window));
1180 }
1181
1182 static void
1183 find_not_supported_dialog (EvWindow   *ev_window)
1184 {
1185         GtkWidget *dialog;
1186
1187         /* If you change this so it isn't modal, be sure you don't
1188          * allow multiple copies of the dialog...
1189          */
1190
1191         dialog = gtk_message_dialog_new (GTK_WINDOW (ev_window),
1192                                          GTK_DIALOG_DESTROY_WITH_PARENT,
1193                                          GTK_MESSAGE_ERROR,
1194                                          GTK_BUTTONS_CLOSE,
1195                                          _("The \"Find\" feature will not work with this document"));
1196         gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1197                                                   _("Searching for text is only supported for PDF documents."));
1198         gtk_dialog_run (GTK_DIALOG (dialog));
1199         gtk_widget_destroy (dialog);
1200 }
1201
1202 static void
1203 ev_window_cmd_edit_select_all (GtkAction *action, EvWindow *ev_window)
1204 {
1205         g_return_if_fail (EV_IS_WINDOW (ev_window));
1206
1207         ev_view_select_all (EV_VIEW (ev_window->priv->view));
1208 }
1209
1210 static void
1211 ev_window_cmd_edit_find (GtkAction *action, EvWindow *ev_window)
1212 {
1213         g_return_if_fail (EV_IS_WINDOW (ev_window));
1214
1215         if (ev_window->priv->document == NULL) {
1216                 g_printerr ("We should have set the Find menu item insensitive since there's no document\n");
1217         } else if (!EV_IS_DOCUMENT_FIND (ev_window->priv->document)) {
1218                 find_not_supported_dialog (ev_window);
1219         } else {
1220                 update_chrome_flag (ev_window, EV_CHROME_FINDBAR, NULL, TRUE);
1221
1222                 gtk_widget_grab_focus (ev_window->priv->find_bar);
1223         }
1224 }
1225
1226 static void
1227 ev_window_cmd_edit_find_next (GtkAction *action, EvWindow *ev_window)
1228 {
1229         g_return_if_fail (EV_IS_WINDOW (ev_window));
1230
1231         ev_view_find_next (EV_VIEW (ev_window->priv->view));
1232 }
1233
1234 static void
1235 ev_window_cmd_edit_copy (GtkAction *action, EvWindow *ev_window)
1236 {
1237         g_return_if_fail (EV_IS_WINDOW (ev_window));
1238
1239         ev_view_copy (EV_VIEW (ev_window->priv->view));
1240 }
1241
1242 static void
1243 ev_window_update_fullscreen_popup (EvWindow *window)
1244 {
1245         GtkWidget *popup = window->priv->fullscreen_popup;
1246         int popup_width, popup_height;
1247         GdkRectangle screen_rect;
1248         gboolean toolbar;
1249
1250         g_return_if_fail (popup != NULL);
1251
1252         toolbar = (window->priv->chrome & EV_CHROME_TOOLBAR) != 0;
1253         popup_width = popup->requisition.width;
1254         popup_height = popup->requisition.height;
1255
1256         /* FIXME multihead */
1257         gdk_screen_get_monitor_geometry (gdk_screen_get_default (),
1258                         gdk_screen_get_monitor_at_window
1259                         (gdk_screen_get_default (),
1260                          GTK_WIDGET (window)->window),
1261                          &screen_rect);
1262         if (toolbar) {
1263                 gtk_widget_set_size_request (popup,
1264                                              screen_rect.width,
1265                                              -1);
1266                 gtk_window_move (GTK_WINDOW (popup),
1267                                  screen_rect.x,
1268                                  screen_rect.y);
1269
1270         } else {
1271                 if (gtk_widget_get_direction (popup) == GTK_TEXT_DIR_RTL)
1272                 {
1273                         gtk_window_move (GTK_WINDOW (popup),
1274                                          screen_rect.x,
1275                                          screen_rect.y);
1276                 } else {
1277                         gtk_window_move (GTK_WINDOW (popup),
1278                                          screen_rect.x + screen_rect.width - popup_width,
1279                                          screen_rect.y);
1280                 }
1281         }
1282 }
1283
1284 static void
1285 screen_size_changed_cb (GdkScreen *screen,
1286                         EvWindow *window)
1287 {
1288         ev_window_update_fullscreen_popup (window);
1289 }
1290
1291 static void
1292 ev_window_sidebar_position_change_cb (GObject *object, GParamSpec *pspec,
1293                                       EvWindow *ev_window)
1294 {
1295         GConfClient *client;
1296         int sidebar_size;
1297
1298         sidebar_size = gtk_paned_get_position (GTK_PANED (object));
1299
1300         client = gconf_client_get_default ();
1301         gconf_client_set_int (client, GCONF_SIDEBAR_SIZE, sidebar_size, NULL);
1302         g_object_unref (client);
1303 }
1304
1305 static void
1306 destroy_fullscreen_popup (EvWindow *window)
1307 {
1308         if (window->priv->fullscreen_popup != NULL)
1309         {
1310                 gtk_widget_destroy (window->priv->fullscreen_popup);
1311                 window->priv->fullscreen_popup = NULL;
1312         }
1313 }
1314
1315 static void
1316 exit_fullscreen_button_clicked_cb (GtkWidget *button, EvWindow *window)
1317 {
1318         GtkAction *action;
1319
1320         action = gtk_action_group_get_action (window->priv->action_group, "ViewFullscreen");
1321         g_return_if_fail (action != NULL);
1322
1323         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), FALSE);
1324 }
1325
1326 static void
1327 fullscreen_popup_size_request_cb (GtkWidget *popup, GtkRequisition *req, EvWindow *window)
1328 {
1329         ev_window_update_fullscreen_popup (window);
1330 }
1331
1332 static gboolean
1333 fullscreen_timeout_cb (gpointer data)
1334 {
1335         EvWindow *window = EV_WINDOW (data);
1336
1337         g_object_set (window->priv->fullscreen_popup, "visible", FALSE, NULL);
1338         ev_view_hide_cursor (EV_VIEW (window->priv->view));
1339         window->priv->fullscreen_timeout_source = NULL;
1340
1341         return FALSE;
1342 }
1343
1344 static void
1345 fullscreen_set_timeout (EvWindow *window)
1346 {
1347         GSource *source;
1348
1349         if (window->priv->fullscreen_timeout_source != NULL)
1350                 g_source_destroy (window->priv->fullscreen_timeout_source);
1351
1352         source = g_timeout_source_new (1000);
1353         g_source_set_callback (source, fullscreen_timeout_cb, window, NULL);
1354         g_source_attach (source, NULL);
1355         window->priv->fullscreen_timeout_source = source;
1356 }
1357
1358 static void
1359 fullscreen_clear_timeout (EvWindow *window)
1360 {
1361         if (window->priv->fullscreen_timeout_source != NULL)
1362                 g_source_destroy (window->priv->fullscreen_timeout_source);
1363
1364         window->priv->fullscreen_timeout_source = NULL;
1365         ev_view_show_cursor (EV_VIEW (window->priv->view));
1366 }
1367
1368 static gboolean
1369 fullscreen_motion_notify_cb (GtkWidget *widget,
1370                              GdkEventMotion *event,
1371                              gpointer user_data)
1372 {
1373         EvWindow *window = EV_WINDOW (user_data);
1374
1375         if (!GTK_WIDGET_VISIBLE (window->priv->fullscreen_popup)) {
1376                 g_object_set (window->priv->fullscreen_popup, "visible", TRUE, NULL);
1377                 ev_view_show_cursor (EV_VIEW (window->priv->view));
1378         }
1379
1380         fullscreen_set_timeout (window);
1381
1382         return FALSE;
1383 }
1384
1385 static gboolean
1386 fullscreen_leave_notify_cb (GtkWidget *widget,
1387                             GdkEventCrossing *event,
1388                             gpointer user_data)
1389 {
1390         EvWindow *window = EV_WINDOW (user_data);
1391
1392         fullscreen_clear_timeout (window);
1393
1394         return FALSE;
1395 }
1396
1397 static GtkWidget *
1398 ev_window_get_exit_fullscreen_button (EvWindow *window)
1399 {
1400         GtkWidget *button, *icon, *label, *hbox;
1401
1402         button = gtk_button_new ();
1403         g_signal_connect (button, "clicked",
1404                           G_CALLBACK (exit_fullscreen_button_clicked_cb),
1405                           window);
1406         gtk_widget_show (button);
1407
1408         hbox = gtk_hbox_new (FALSE, 2);
1409         gtk_widget_show (hbox);
1410         gtk_container_add (GTK_CONTAINER (button), hbox);
1411
1412         icon = gtk_image_new_from_stock (EV_STOCK_LEAVE_FULLSCREEN, GTK_ICON_SIZE_BUTTON);
1413         gtk_widget_show (icon);
1414         gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);
1415
1416         label = gtk_label_new (_("Leave Fullscreen"));
1417         gtk_widget_show (label);
1418         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
1419
1420         return button;
1421 }
1422
1423 static GtkWidget *
1424 ev_window_create_fullscreen_popup (EvWindow *window)
1425 {
1426         GtkWidget *popup;
1427         GtkWidget *hbox;
1428         GtkWidget *button;
1429
1430         popup = gtk_window_new (GTK_WINDOW_POPUP);
1431         hbox = gtk_hbox_new (FALSE, 0);
1432         button = ev_window_get_exit_fullscreen_button (window);
1433
1434         gtk_container_add (GTK_CONTAINER (popup), hbox);
1435         gtk_box_pack_start (GTK_BOX (hbox), window->priv->fullscreen_toolbar,
1436                             TRUE, TRUE, 0);
1437         gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
1438
1439         gtk_widget_show (button);
1440         gtk_widget_show (hbox);
1441
1442         gtk_window_set_resizable (GTK_WINDOW (popup), FALSE);
1443
1444         /* FIXME multihead */
1445         g_signal_connect_object (gdk_screen_get_default (), "size-changed",
1446                                  G_CALLBACK (screen_size_changed_cb),
1447                                  window, 0);
1448         g_signal_connect_object (popup, "size_request",
1449                                  G_CALLBACK (fullscreen_popup_size_request_cb),
1450                                  window, 0);
1451
1452         return popup;
1453 }
1454
1455
1456 static void
1457 ev_window_update_fullscreen_action (EvWindow *window)
1458 {
1459         GtkAction *action;
1460
1461         action = gtk_action_group_get_action (window->priv->action_group, "ViewFullscreen");
1462         g_signal_handlers_block_by_func
1463                 (action, G_CALLBACK (ev_window_cmd_view_fullscreen), window);
1464         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
1465                                       ev_view_get_fullscreen (EV_VIEW (window->priv->view)));
1466         g_signal_handlers_unblock_by_func
1467                 (action, G_CALLBACK (ev_window_cmd_view_fullscreen), window);
1468 }
1469
1470 static void
1471 ev_window_fullscreen (EvWindow *window)
1472 {
1473         ev_view_set_fullscreen (EV_VIEW (window->priv->view), TRUE);
1474         if (window->priv->fullscreen_popup == NULL)
1475                 window->priv->fullscreen_popup
1476                         = ev_window_create_fullscreen_popup (window);
1477         update_chrome_visibility (window);
1478
1479         g_object_set (G_OBJECT (window->priv->scrolled_window),
1480                       "shadow-type", GTK_SHADOW_NONE,
1481                       NULL);
1482
1483         g_signal_connect (window->priv->view,
1484                           "motion-notify-event",
1485                           G_CALLBACK (fullscreen_motion_notify_cb),
1486                           window);
1487         g_signal_connect (window->priv->view,
1488                           "leave-notify-event",
1489                           G_CALLBACK (fullscreen_leave_notify_cb),
1490                           window);
1491         fullscreen_set_timeout (window);
1492
1493         gtk_widget_grab_focus (window->priv->view);
1494         ev_window_update_fullscreen_action (window);
1495         gtk_window_fullscreen (GTK_WINDOW (window));
1496         ev_window_update_fullscreen_popup (window);
1497 }
1498
1499 static void
1500 ev_window_unfullscreen (EvWindow *window)
1501 {
1502         EvView *view = EV_VIEW (window->priv->view);
1503
1504         ev_view_set_fullscreen (view, FALSE);
1505         g_object_set (G_OBJECT (window->priv->scrolled_window),
1506                       "shadow-type", GTK_SHADOW_IN,
1507                       NULL);
1508
1509         fullscreen_clear_timeout (window);
1510
1511         g_signal_handlers_disconnect_by_func (view,
1512                                               (gpointer) fullscreen_motion_notify_cb,
1513                                               window);
1514         g_signal_handlers_disconnect_by_func (view,
1515                                               (gpointer) fullscreen_leave_notify_cb,
1516                                               window);
1517         ev_window_update_fullscreen_action (window);
1518         gtk_window_unfullscreen (GTK_WINDOW (window));
1519         update_chrome_visibility (window);
1520 }
1521
1522 static void
1523 ev_window_cmd_view_fullscreen (GtkAction *action, EvWindow *window)
1524 {
1525         gboolean fullscreen;
1526
1527         g_return_if_fail (EV_IS_WINDOW (window));
1528         ev_window_stop_presentation (window);
1529
1530         fullscreen = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
1531         if (fullscreen) {
1532                 ev_window_fullscreen (window);
1533         } else {
1534                 ev_window_unfullscreen (window);
1535         }
1536 }
1537
1538 static void
1539 ev_window_update_presentation_action (EvWindow *window)
1540 {
1541         GtkAction *action;
1542
1543         action = gtk_action_group_get_action (window->priv->action_group, "ViewPresentation");
1544         g_signal_handlers_block_by_func
1545                 (action, G_CALLBACK (ev_window_cmd_view_presentation), window);
1546         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
1547                                       ev_view_get_presentation (EV_VIEW (window->priv->view)));
1548         g_signal_handlers_unblock_by_func
1549                 (action, G_CALLBACK (ev_window_cmd_view_presentation), window);
1550 }
1551
1552 static void
1553 ev_window_run_presentation (EvWindow *window)
1554 {
1555         g_object_set (G_OBJECT (window->priv->scrolled_window),
1556                       "shadow-type", GTK_SHADOW_NONE,
1557                       NULL);
1558
1559         gtk_widget_grab_focus (window->priv->view);
1560         ev_view_set_presentation (EV_VIEW (window->priv->view), TRUE);
1561         gtk_window_fullscreen (GTK_WINDOW (window));
1562         ev_window_update_presentation_action (window);
1563         update_chrome_visibility (window);
1564 }
1565
1566 static void
1567 ev_window_stop_presentation (EvWindow *window)
1568 {
1569         if (! ev_view_get_presentation (EV_VIEW (window->priv->view)))
1570                 return;
1571
1572         g_object_set (G_OBJECT (window->priv->scrolled_window),
1573                       "shadow-type", GTK_SHADOW_IN,
1574                       NULL);
1575         ev_view_set_presentation (EV_VIEW (window->priv->view), FALSE);
1576         gtk_window_unfullscreen (GTK_WINDOW (window));
1577         ev_window_update_presentation_action (window);
1578         update_chrome_visibility (window);
1579 }
1580
1581 static void
1582 ev_window_cmd_view_presentation (GtkAction *action, EvWindow *window)
1583 {
1584         gboolean presentation;
1585
1586         g_return_if_fail (EV_IS_WINDOW (window));
1587
1588         presentation = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
1589         if (presentation) {
1590                 ev_window_run_presentation (window);
1591         } else {
1592                 ev_window_stop_presentation (window);
1593         }
1594 }
1595
1596 static gboolean
1597 ev_window_state_event (GtkWidget *widget, GdkEventWindowState *event)
1598 {
1599         EvWindow *window = EV_WINDOW (widget);
1600
1601         if (event->changed_mask & GDK_WINDOW_STATE_MAXIMIZED) {
1602                 gboolean show;
1603
1604                 show = (event->new_window_state & GDK_WINDOW_STATE_MAXIMIZED) == 0;
1605
1606                 gtk_statusbar_set_has_resize_grip (GTK_STATUSBAR (window->priv->statusbar),
1607                                                    show);
1608         }
1609
1610         return FALSE;
1611 }
1612
1613 static gboolean
1614 ev_window_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
1615 {
1616         EvWindow *window = EV_WINDOW (widget);
1617         EvWindowPrivate *priv = window->priv;
1618         gboolean fullscreen;
1619
1620         g_object_get (priv->view,
1621                       "fullscreen", &fullscreen,
1622                       NULL);
1623
1624         if (fullscreen)
1625                 gtk_widget_show (priv->fullscreen_popup);
1626
1627         return GTK_WIDGET_CLASS (ev_window_parent_class)->focus_in_event (widget, event);
1628 }
1629
1630 static gboolean
1631 ev_window_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
1632 {
1633         EvWindow *window = EV_WINDOW (widget);
1634         EvWindowPrivate *priv = window->priv;
1635         gboolean fullscreen;
1636
1637         g_object_get (priv->view,
1638                       "fullscreen", &fullscreen,
1639                       NULL);
1640
1641         if (fullscreen)
1642                 gtk_widget_hide (priv->fullscreen_popup);
1643
1644         return GTK_WIDGET_CLASS (ev_window_parent_class)->focus_out_event (widget, event);
1645 }
1646
1647 static void
1648 ev_window_set_page_mode (EvWindow         *window,
1649                          EvWindowPageMode  page_mode)
1650 {
1651         GtkWidget *child = NULL;
1652         GtkWidget *real_child;
1653
1654         if (window->priv->page_mode == page_mode)
1655                 return;
1656
1657         window->priv->page_mode = page_mode;
1658
1659         switch (page_mode) {
1660         case PAGE_MODE_DOCUMENT:
1661                 child = window->priv->view;
1662                 break;
1663         case PAGE_MODE_PASSWORD:
1664                 child = window->priv->password_view;
1665                 break;
1666         default:
1667                 g_assert_not_reached ();
1668         }
1669
1670         real_child = gtk_bin_get_child (GTK_BIN (window->priv->scrolled_window));
1671         if (child != real_child) {
1672                 gtk_container_remove (GTK_CONTAINER (window->priv->scrolled_window),
1673                                       real_child);
1674                 gtk_container_add (GTK_CONTAINER (window->priv->scrolled_window),
1675                                    child);
1676         }
1677         update_action_sensitivity (window);
1678 }
1679
1680 static void
1681 ev_window_cmd_edit_toolbar_cb (GtkDialog *dialog, gint response, gpointer data)
1682 {
1683         EvWindow *ev_window = EV_WINDOW (data);
1684         egg_editable_toolbar_set_edit_mode
1685                         (EGG_EDITABLE_TOOLBAR (ev_window->priv->toolbar), FALSE);
1686         egg_toolbars_model_save (ev_window->priv->toolbar_model,
1687                                  ev_window->priv->toolbar_file, "1.0");
1688         gtk_widget_destroy (GTK_WIDGET (dialog));
1689 }
1690
1691 static void
1692 ev_window_cmd_edit_toolbar (GtkAction *action, EvWindow *ev_window)
1693 {
1694         GtkWidget *dialog;
1695         GtkWidget *editor;
1696         g_return_if_fail (EV_IS_WINDOW (ev_window));
1697     
1698         dialog = gtk_dialog_new_with_buttons (_("Toolbar editor"), GTK_WINDOW (ev_window), 
1699                                               GTK_DIALOG_DESTROY_WITH_PARENT, 
1700                                               GTK_STOCK_CLOSE,
1701                                               GTK_RESPONSE_CANCEL, 
1702                                               NULL);
1703         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
1704         gtk_window_set_default_size (GTK_WINDOW (dialog), 500, 400);
1705           
1706         editor = egg_toolbar_editor_new (ev_window->priv->ui_manager,
1707                                          ev_window->priv->toolbar_model);
1708         gtk_container_set_border_width (GTK_CONTAINER (editor), 5);
1709         gtk_box_set_spacing (GTK_BOX (EGG_TOOLBAR_EDITOR (editor)), 5);
1710                      
1711         gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), editor);
1712         egg_toolbar_editor_load_actions (EGG_TOOLBAR_EDITOR (editor),
1713                                       DATADIR"/evince-toolbar.xml");
1714
1715         egg_editable_toolbar_set_edit_mode
1716                 (EGG_EDITABLE_TOOLBAR (ev_window->priv->toolbar), TRUE);
1717
1718         gtk_widget_show_all (dialog);
1719       
1720         g_signal_connect (G_OBJECT (dialog), "response",
1721                           G_CALLBACK (ev_window_cmd_edit_toolbar_cb),
1722                           ev_window);
1723 }
1724
1725 static void
1726 ev_window_cmd_view_zoom_in (GtkAction *action, EvWindow *ev_window)
1727 {
1728         g_return_if_fail (EV_IS_WINDOW (ev_window));
1729
1730         ev_view_set_sizing_mode (EV_VIEW (ev_window->priv->view), EV_SIZING_FREE);
1731         ev_view_zoom_in (EV_VIEW (ev_window->priv->view));
1732 }
1733
1734 static void
1735 ev_window_cmd_view_zoom_out (GtkAction *action, EvWindow *ev_window)
1736 {
1737         g_return_if_fail (EV_IS_WINDOW (ev_window));
1738
1739         ev_view_set_sizing_mode (EV_VIEW (ev_window->priv->view), EV_SIZING_FREE);
1740         ev_view_zoom_out (EV_VIEW (ev_window->priv->view));
1741 }
1742
1743 static void
1744 ev_window_cmd_go_previous_page (GtkAction *action, EvWindow *ev_window)
1745 {
1746         g_return_if_fail (EV_IS_WINDOW (ev_window));
1747
1748         ev_page_cache_prev_page (ev_window->priv->page_cache);
1749 }
1750
1751 static void
1752 ev_window_cmd_go_next_page (GtkAction *action, EvWindow *ev_window)
1753 {
1754         g_return_if_fail (EV_IS_WINDOW (ev_window));
1755
1756         ev_page_cache_next_page (ev_window->priv->page_cache);
1757 }
1758
1759 static void
1760 ev_window_cmd_go_first_page (GtkAction *action, EvWindow *ev_window)
1761 {
1762         g_return_if_fail (EV_IS_WINDOW (ev_window));
1763
1764         ev_page_cache_set_current_page (ev_window->priv->page_cache, 0);
1765 }
1766
1767 static void
1768 ev_window_cmd_go_last_page (GtkAction *action, EvWindow *ev_window)
1769 {
1770         int n_pages;
1771
1772         g_return_if_fail (EV_IS_WINDOW (ev_window));
1773
1774         n_pages = ev_page_cache_get_n_pages (ev_window->priv->page_cache);
1775         ev_page_cache_set_current_page (ev_window->priv->page_cache, n_pages - 1);
1776 }
1777
1778 static void
1779 ev_window_cmd_view_reload (GtkAction *action, EvWindow *ev_window)
1780 {
1781         char *uri;
1782         int page;
1783
1784         g_return_if_fail (EV_IS_WINDOW (ev_window));
1785
1786         page = ev_page_cache_get_current_page (ev_window->priv->page_cache);
1787         uri = g_strdup (ev_window->priv->uri);
1788
1789         ev_window_open (ev_window, uri);
1790
1791         /* In case the number of pages in the document has changed. */
1792         page = CLAMP (page, 0, ev_page_cache_get_n_pages (ev_window->priv->page_cache));
1793
1794         ev_page_cache_set_current_page (ev_window->priv->page_cache, page);
1795
1796         g_free (uri);
1797 }
1798
1799 static void
1800 ev_window_cmd_help_contents (GtkAction *action, EvWindow *ev_window)
1801 {
1802         GError *error = NULL;
1803
1804         g_return_if_fail (EV_IS_WINDOW (ev_window));
1805
1806         gnome_help_display ("evince.xml", NULL, &error);
1807
1808         if(error != NULL) {
1809                 g_warning (error->message);
1810                 g_error_free (error);
1811         }
1812 }
1813
1814 static void
1815 ev_window_cmd_leave_fullscreen (GtkAction *action, EvWindow *window)
1816 {
1817         gtk_window_unfullscreen (GTK_WINDOW (window));
1818 }
1819
1820 static void
1821 ev_window_cmd_escape (GtkAction *action, EvWindow *window)
1822 {
1823         GtkWidget *widget;
1824
1825         widget = gtk_window_get_focus (GTK_WINDOW (window));
1826         if (widget && gtk_widget_get_ancestor (widget, EGG_TYPE_FIND_BAR)) {
1827                 update_chrome_flag (window, EV_CHROME_FINDBAR, NULL, FALSE);
1828         } else {
1829                 gboolean fullscreen;
1830                 gboolean presentation;
1831
1832                 g_object_get (window->priv->view,
1833                               "fullscreen", &fullscreen,
1834                               "presentation", &presentation,
1835                               NULL);
1836
1837                 if (fullscreen)
1838                         ev_window_unfullscreen (window);
1839                 if (presentation)
1840                         ev_window_stop_presentation (window);
1841
1842                 if (fullscreen && presentation)
1843                         g_warning ("Both fullscreen and presentation set somehow");
1844         }
1845 }
1846
1847 static void
1848 update_view_size (EvView *view, EvWindow *window)
1849 {
1850         int width, height;
1851         GtkRequisition vsb_requisition;
1852         GtkRequisition hsb_requisition;
1853         int scrollbar_spacing;
1854
1855         /* Calculate the width available for the */
1856         width = window->priv->scrolled_window->allocation.width;
1857         height = window->priv->scrolled_window->allocation.height;
1858
1859         if (gtk_scrolled_window_get_shadow_type (GTK_SCROLLED_WINDOW (window->priv->scrolled_window)) == GTK_SHADOW_IN) {
1860                 width -= 2 * window->priv->view->style->xthickness;
1861                 height -= 2 * window->priv->view->style->ythickness;
1862         }
1863
1864         gtk_widget_size_request (GTK_SCROLLED_WINDOW (window->priv->scrolled_window)->vscrollbar,
1865                                  &vsb_requisition);
1866         gtk_widget_size_request (GTK_SCROLLED_WINDOW (window->priv->scrolled_window)->hscrollbar,
1867                                  &hsb_requisition);
1868         gtk_widget_style_get (window->priv->scrolled_window,
1869                               "scrollbar_spacing", &scrollbar_spacing,
1870                               NULL);
1871
1872         ev_view_set_zoom_for_size (EV_VIEW (window->priv->view),
1873                                    MAX (1, width),
1874                                    MAX (1, height),
1875                                    vsb_requisition.width + scrollbar_spacing,
1876                                    hsb_requisition.height + scrollbar_spacing);
1877 }
1878
1879 static void     
1880 ev_window_sizing_mode_changed_cb (EvView *view, GParamSpec *pspec,
1881                                   EvWindow   *ev_window)
1882 {
1883         GtkWidget *scrolled_window;
1884         EvSizingMode sizing_mode;
1885
1886         g_object_get (ev_window->priv->view,
1887                       "sizing-mode", &sizing_mode,
1888                       NULL);
1889
1890         scrolled_window = ev_window->priv->scrolled_window;
1891
1892         g_signal_handlers_disconnect_by_func (ev_window->priv->view, update_view_size, ev_window);
1893
1894         if (sizing_mode != EV_SIZING_FREE)
1895                 update_view_size (NULL, ev_window);
1896
1897         switch (sizing_mode) {
1898         case EV_SIZING_BEST_FIT:
1899                 g_object_set (G_OBJECT (scrolled_window),
1900                               "hscrollbar-policy", GTK_POLICY_NEVER,
1901                               "vscrollbar-policy", GTK_POLICY_AUTOMATIC,
1902                               NULL);
1903                 g_signal_connect (ev_window->priv->view, "zoom_invalid",
1904                                   G_CALLBACK (update_view_size),
1905                                   ev_window);
1906                 break;
1907         case EV_SIZING_FIT_WIDTH:
1908                 g_object_set (G_OBJECT (scrolled_window),
1909                               "hscrollbar-policy", GTK_POLICY_NEVER,
1910                               "vscrollbar-policy", GTK_POLICY_AUTOMATIC,
1911                               NULL);
1912                 g_signal_connect (ev_window->priv->view, "zoom_invalid",
1913                                   G_CALLBACK (update_view_size),
1914                                   ev_window);
1915                 break;
1916         case EV_SIZING_FREE:
1917                 g_object_set (G_OBJECT (scrolled_window),
1918                               "hscrollbar-policy", GTK_POLICY_AUTOMATIC,
1919                               "vscrollbar-policy", GTK_POLICY_AUTOMATIC,
1920                               NULL);
1921                 break;
1922         }
1923
1924         update_sizing_buttons (ev_window);
1925 }
1926
1927 static void     
1928 ev_window_zoom_changed_cb (EvView *view, GParamSpec *pspec,
1929                            EvWindow   *ev_window)
1930 {
1931         update_action_sensitivity (ev_window);
1932 }
1933
1934 static char *
1935 build_comments_string (void)
1936 {
1937         PopplerBackend backend;
1938         const char *backend_name;
1939         const char *version;
1940
1941         backend = poppler_get_backend ();
1942         version = poppler_get_version ();
1943         switch (backend) {
1944                 case POPPLER_BACKEND_CAIRO:
1945                         backend_name = "cairo";
1946                         break;
1947                 case POPPLER_BACKEND_SPLASH:
1948                         backend_name = "splash";
1949                         break;
1950                 default:
1951                         backend_name = "unknown";
1952                         break;
1953         }
1954
1955         return g_strdup_printf (_("PostScript and PDF File Viewer.\n"
1956                                   "Using poppler %s (%s)"),
1957                                 version, backend_name);
1958 }
1959
1960 static void
1961 ev_window_cmd_help_about (GtkAction *action, EvWindow *ev_window)
1962 {
1963         const char *authors[] = {
1964                 "Martin Kretzschmar <m_kretzschmar@gmx.net>",
1965                 "Jonathan Blandford <jrb@gnome.org>",
1966                 "Marco Pesenti Gritti <marco@gnome.org>",
1967                 "Nickolay V. Shmyrev <nshmyrev@yandex.ru>",
1968                 "Bryan Clark <clarkbw@gnome.org>",
1969                 NULL
1970         };
1971
1972         const char *documenters[] = {
1973                 "Nickolay V. Shmyrev <nshmyrev@yandex.ru>",
1974                 NULL
1975         };
1976
1977         const char *license[] = {
1978                 N_("Evince is free software; you can redistribute it and/or modify\n"
1979                    "it under the terms of the GNU General Public License as published by\n"
1980                    "the Free Software Foundation; either version 2 of the License, or\n"
1981                    "(at your option) any later version.\n"),
1982                 N_("Evince is distributed in the hope that it will be useful,\n"
1983                    "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1984                    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
1985                    "GNU General Public License for more details.\n"),
1986                 N_("You should have received a copy of the GNU General Public License\n"
1987                    "along with Evince; if not, write to the Free Software Foundation, Inc.,\n"
1988                    "59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n")
1989         };
1990
1991         char *license_trans;
1992         char *comments;
1993
1994 #ifdef ENABLE_NLS
1995         const char **p;
1996
1997         for (p = authors; *p; ++p)
1998                 *p = _(*p);
1999
2000         for (p = documenters; *p; ++p)
2001                 *p = _(*p);
2002 #endif
2003
2004         license_trans = g_strconcat (_(license[0]), "\n", _(license[1]), "\n",
2005                                      _(license[2]), "\n", NULL);
2006         comments = build_comments_string ();
2007
2008         gtk_show_about_dialog (
2009                 GTK_WINDOW (ev_window),
2010                 "name", _("Evince"),
2011                 "version", VERSION,
2012                 "copyright",
2013                 _("\xc2\xa9 1996-2004 The Evince authors"),
2014                 "license", license_trans,
2015                 "website", "http://www.gnome.org/projects/evince",
2016                 "comments", comments,
2017                 "authors", authors,
2018                 "documenters", documenters,
2019                 "translator-credits", _("translator-credits"),
2020                 NULL);
2021
2022         g_free (comments);
2023         g_free (license_trans);
2024 }
2025
2026 static void
2027 ev_window_view_toolbar_cb (GtkAction *action, EvWindow *ev_window)
2028 {
2029         update_chrome_flag (ev_window, EV_CHROME_TOOLBAR,
2030                             GCONF_CHROME_TOOLBAR,
2031                             gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
2032 }
2033
2034 static void
2035 ev_window_view_statusbar_cb (GtkAction *action, EvWindow *ev_window)
2036 {
2037         update_chrome_flag (ev_window, EV_CHROME_STATUSBAR,
2038                             GCONF_CHROME_STATUSBAR,
2039                             gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
2040 }
2041
2042 static void
2043 ev_window_view_sidebar_cb (GtkAction *action, EvWindow *ev_window)
2044 {
2045         update_chrome_flag (ev_window, EV_CHROME_SIDEBAR,
2046                             GCONF_CHROME_SIDEBAR,
2047                             gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
2048 }
2049
2050 static void
2051 ev_window_sidebar_visibility_changed_cb (EvSidebar *ev_sidebar, GParamSpec *pspec,
2052                                          EvWindow   *ev_window)
2053 {
2054         GtkAction *action;
2055         gboolean visible, fullscreen;
2056
2057         g_object_get (ev_window->priv->view,
2058                       "fullscreen", &fullscreen,
2059                       NULL);
2060         visible = GTK_WIDGET_VISIBLE (ev_sidebar);
2061
2062         /* In fullscreen mode the sidebar is not visible,
2063          * but we don't want to update the chrome
2064          */
2065         if (fullscreen)
2066                 return;
2067         
2068         action = gtk_action_group_get_action (ev_window->priv->action_group, "ViewSidebar");
2069         
2070         g_signal_handlers_block_by_func
2071                 (action, G_CALLBACK (ev_window_view_sidebar_cb), ev_window);
2072         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), visible);
2073         g_signal_handlers_unblock_by_func
2074                 (action, G_CALLBACK (ev_window_view_sidebar_cb), ev_window);
2075
2076         update_chrome_flag (ev_window, EV_CHROME_SIDEBAR,
2077                             GCONF_CHROME_SIDEBAR, visible);
2078 }
2079
2080 static void
2081 menu_item_select_cb (GtkMenuItem *proxy, EvWindow *ev_window)
2082 {
2083         GtkAction *action;
2084         char *message;
2085
2086         action = g_object_get_data (G_OBJECT (proxy), "gtk-action");
2087         g_return_if_fail (action != NULL);
2088
2089         g_object_get (G_OBJECT (action), "tooltip", &message, NULL);
2090         if (message) {
2091                 gtk_statusbar_push (GTK_STATUSBAR (ev_window->priv->statusbar),
2092                                     ev_window->priv->help_message_cid, message);
2093                 g_free (message);
2094         }
2095 }
2096
2097 static void
2098 menu_item_deselect_cb (GtkMenuItem *proxy, EvWindow *ev_window)
2099 {
2100         gtk_statusbar_pop (GTK_STATUSBAR (ev_window->priv->statusbar),
2101                            ev_window->priv->help_message_cid);
2102 }
2103
2104 static void
2105 connect_proxy_cb (GtkUIManager *ui_manager, GtkAction *action,
2106                   GtkWidget *proxy, EvWindow *ev_window)
2107 {
2108         if (GTK_IS_MENU_ITEM (proxy)) {
2109                 g_signal_connect (proxy, "select",
2110                                   G_CALLBACK (menu_item_select_cb), ev_window);
2111                 g_signal_connect (proxy, "deselect",
2112                                   G_CALLBACK (menu_item_deselect_cb),
2113                                   ev_window);
2114         }
2115 }
2116
2117 static void
2118 disconnect_proxy_cb (GtkUIManager *ui_manager, GtkAction *action,
2119                      GtkWidget *proxy, EvWindow *ev_window)
2120 {
2121         if (GTK_IS_MENU_ITEM (proxy)) {
2122                 g_signal_handlers_disconnect_by_func
2123                         (proxy, G_CALLBACK (menu_item_select_cb), ev_window);
2124                 g_signal_handlers_disconnect_by_func
2125                         (proxy, G_CALLBACK (menu_item_deselect_cb), ev_window);
2126         }
2127 }
2128
2129 static void
2130 view_status_changed_cb (EvView     *view,
2131                         GParamSpec *pspec,
2132                         EvWindow   *ev_window)
2133 {
2134         const char *message;
2135
2136         gtk_statusbar_pop (GTK_STATUSBAR (ev_window->priv->statusbar),
2137                            ev_window->priv->view_message_cid);
2138
2139         message = ev_view_get_status (view);
2140         if (message) {
2141                 gtk_statusbar_push (GTK_STATUSBAR (ev_window->priv->statusbar),
2142                                     ev_window->priv->view_message_cid, message);
2143         }
2144 }
2145
2146 static void
2147 view_find_status_changed_cb (EvView     *view,
2148                              GParamSpec *pspec,
2149                              EvWindow   *ev_window)
2150 {
2151         const char *text;
2152
2153         text = ev_view_get_find_status (view);
2154         egg_find_bar_set_status_text (EGG_FIND_BAR (ev_window->priv->find_bar),
2155                                       text);
2156 }
2157
2158 static void
2159 find_bar_previous_cb (EggFindBar *find_bar,
2160                       EvWindow   *ev_window)
2161 {
2162         ev_view_find_previous (EV_VIEW (ev_window->priv->view));
2163 }
2164
2165 static void
2166 find_bar_next_cb (EggFindBar *find_bar,
2167                   EvWindow   *ev_window)
2168 {
2169         ev_view_find_next (EV_VIEW (ev_window->priv->view));
2170 }
2171
2172 static void
2173 find_bar_close_cb (EggFindBar *find_bar,
2174                    EvWindow   *ev_window)
2175 {
2176         update_chrome_flag (ev_window, EV_CHROME_FINDBAR, NULL, FALSE);
2177 }
2178
2179 static void
2180 find_bar_search_changed_cb (EggFindBar *find_bar,
2181                             GParamSpec *param,
2182                             EvWindow   *ev_window)
2183 {
2184         gboolean case_sensitive;
2185         gboolean visible;
2186         const char *search_string;
2187
2188         g_return_if_fail (EV_IS_WINDOW (ev_window));
2189
2190         /* Either the string or case sensitivity could have changed,
2191          * we connect this callback to both. We also connect it
2192          * to ::visible so when the find bar is hidden, we should
2193          * pretend the search string is NULL/""
2194          */
2195
2196         case_sensitive = egg_find_bar_get_case_sensitive (find_bar);
2197         visible = GTK_WIDGET_VISIBLE (find_bar);
2198         search_string = egg_find_bar_get_search_string (find_bar);
2199
2200 #if 0
2201         g_printerr ("search for '%s'\n", search_string ? search_string : "(nil)");
2202 #endif
2203
2204         if (ev_window->priv->document &&
2205             EV_IS_DOCUMENT_FIND (ev_window->priv->document)) {
2206                 if (visible && search_string && search_string[0]) {
2207                         ev_document_doc_mutex_lock ();
2208                         ev_document_find_begin (EV_DOCUMENT_FIND (ev_window->priv->document), 
2209                                                 ev_page_cache_get_current_page (ev_window->priv->page_cache),
2210                                                 search_string,
2211                                                 case_sensitive);
2212                         ev_document_doc_mutex_unlock ();
2213                 } else {
2214                         ev_document_doc_mutex_lock ();
2215                         ev_document_find_cancel (EV_DOCUMENT_FIND (ev_window->priv->document));
2216                         ev_document_doc_mutex_unlock ();
2217
2218                         egg_find_bar_set_status_text (EGG_FIND_BAR (ev_window->priv->find_bar),
2219                                                       NULL);
2220                         gtk_widget_queue_draw (GTK_WIDGET (ev_window->priv->view));
2221                 }
2222         }
2223 }
2224
2225 static void
2226 zoom_control_changed_cb (EphyZoomAction *action,
2227                          float           zoom,
2228                          EvWindow       *ev_window)
2229 {
2230         EvSizingMode mode;
2231         
2232         g_return_if_fail (EV_IS_WINDOW (ev_window));
2233
2234         if (zoom == EPHY_ZOOM_BEST_FIT) {
2235                 mode = EV_SIZING_BEST_FIT;
2236         } else if (zoom == EPHY_ZOOM_FIT_WIDTH) {
2237                 mode = EV_SIZING_FIT_WIDTH;
2238         } else {
2239                 mode = EV_SIZING_FREE;
2240                 ev_view_set_zoom (EV_VIEW (ev_window->priv->view), zoom, FALSE);
2241         }
2242         
2243         ev_view_set_sizing_mode (EV_VIEW (ev_window->priv->view), mode);
2244 }
2245
2246 static void
2247 ev_window_dispose (GObject *object)
2248 {
2249         EvWindow *window = EV_WINDOW (object);
2250         EvWindowPrivate *priv = window->priv;
2251
2252         if (priv->toolbar_model) {
2253                 g_object_unref (priv->toolbar_model);
2254                 g_free (priv->toolbar_file);
2255                 priv->toolbar_model = NULL;
2256                 priv->toolbar_file = NULL;
2257         }
2258
2259         if (priv->ui_manager) {
2260                 g_object_unref (priv->ui_manager);
2261                 priv->ui_manager = NULL;
2262         }
2263
2264         if (priv->action_group) {
2265                 g_object_unref (priv->action_group);
2266                 priv->action_group = NULL;
2267         }
2268
2269         if (priv->page_cache) {
2270                 g_signal_handlers_disconnect_by_func (priv->page_cache, page_changed_cb, window);
2271                 priv->page_cache = NULL;
2272         }
2273
2274         if (priv->document) {
2275                 g_object_unref (priv->document);
2276                 priv->document = NULL;
2277         }
2278         
2279         if (priv->view) {
2280                 g_object_unref (priv->view);
2281                 priv->view = NULL;
2282         }
2283
2284         if (priv->page_view) {
2285                 g_object_unref (priv->page_view);
2286                 priv->page_view = NULL;
2287         }
2288
2289         if (priv->password_document) {
2290                 g_object_unref (priv->password_document);
2291                 priv->password_document = NULL;
2292         }
2293         
2294         if (priv->password_uri) {
2295                 g_free (priv->password_uri);
2296                 priv->password_uri = NULL;
2297         }
2298
2299         if (priv->find_bar) {
2300                 g_signal_handlers_disconnect_by_func
2301                         (window->priv->find_bar,
2302                          G_CALLBACK (find_bar_close_cb),
2303                          window);
2304                 priv->find_bar = NULL;
2305         }
2306
2307         destroy_fullscreen_popup (window);
2308
2309         G_OBJECT_CLASS (ev_window_parent_class)->dispose (object);
2310 }
2311
2312 static void
2313 ev_window_class_init (EvWindowClass *ev_window_class)
2314 {
2315         GObjectClass *g_object_class = G_OBJECT_CLASS (ev_window_class);
2316         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (ev_window_class);
2317
2318         g_object_class->dispose = ev_window_dispose;
2319
2320         widget_class->window_state_event = ev_window_state_event;
2321         widget_class->focus_in_event = ev_window_focus_in_event;
2322         widget_class->focus_out_event = ev_window_focus_out_event;
2323
2324         g_type_class_add_private (g_object_class, sizeof (EvWindowPrivate));
2325 }
2326
2327 /* Normal items */
2328 static const GtkActionEntry entries[] = {
2329         { "File", NULL, N_("_File") },
2330         { "Edit", NULL, N_("_Edit") },
2331         { "View", NULL, N_("_View") },
2332         { "Go", NULL, N_("_Go") },
2333         { "Help", NULL, N_("_Help") },
2334
2335         /* File menu */
2336         { "FileOpen", GTK_STOCK_OPEN, NULL, "<control>O",
2337           N_("Open an existing document"),
2338           G_CALLBACK (ev_window_cmd_file_open) },
2339         { "FileSaveAs", GTK_STOCK_SAVE_AS, N_("_Save a Copy..."), NULL,
2340           N_("Save the current document with a new filename"),
2341           G_CALLBACK (ev_window_cmd_save_as) },
2342         { "FilePrint", GTK_STOCK_PRINT, N_("Print..."), "<control>P",
2343           N_("Print this document"),
2344           G_CALLBACK (ev_window_cmd_file_print) },
2345         { "FileCloseWindow", GTK_STOCK_CLOSE, NULL, "<control>W",
2346           N_("Close this window"),
2347           G_CALLBACK (ev_window_cmd_file_close_window) },
2348
2349         /* Edit menu */
2350         { "EditCopy", GTK_STOCK_COPY, NULL, "<control>C",
2351           N_("Copy text from the document"),
2352           G_CALLBACK (ev_window_cmd_edit_copy) },
2353         { "EditSelectAll", NULL, N_("Select _All"), "<control>A",
2354           N_("Select the entire page"),
2355           G_CALLBACK (ev_window_cmd_edit_select_all) },
2356         { "EditFind", GTK_STOCK_FIND, NULL, "<control>F",
2357           N_("Find a word or phrase in the document"),
2358           G_CALLBACK (ev_window_cmd_edit_find) },
2359         { "EditFindNext", NULL, N_("Find Ne_xt"), "<control>G",
2360           N_("Find next occurrence of the word or phrase"),
2361           G_CALLBACK (ev_window_cmd_edit_find_next) },
2362         { "EditToolbar", NULL, N_("T_oolbar"), NULL,
2363           N_("Customize the toolbar"),
2364           G_CALLBACK (ev_window_cmd_edit_toolbar) },
2365
2366         /* View menu */
2367         { "ViewZoomIn", GTK_STOCK_ZOOM_IN, NULL, "<control>plus",
2368           N_("Enlarge the document"),
2369           G_CALLBACK (ev_window_cmd_view_zoom_in) },
2370         { "ViewZoomOut", GTK_STOCK_ZOOM_OUT, NULL, "<control>minus",
2371           N_("Shrink the document"),
2372           G_CALLBACK (ev_window_cmd_view_zoom_out) },
2373         { "ViewReload", GTK_STOCK_REFRESH, N_("_Reload"), "<control>R",
2374           N_("Reload the document"),
2375           G_CALLBACK (ev_window_cmd_view_reload) },
2376
2377         /* Go menu */
2378         { "GoPreviousPage", GTK_STOCK_GO_BACK, N_("_Previous Page"), "<control>Page_Up",
2379           N_("Go to the previous page"),
2380           G_CALLBACK (ev_window_cmd_go_previous_page) },
2381         { "GoNextPage", GTK_STOCK_GO_FORWARD, N_("_Next Page"), "<control>Page_Down",
2382           N_("Go to the next page"),
2383           G_CALLBACK (ev_window_cmd_go_next_page) },
2384         { "GoFirstPage", GTK_STOCK_GOTO_FIRST, N_("_First Page"), "<control>Home",
2385           N_("Go to the first page"),
2386           G_CALLBACK (ev_window_cmd_go_first_page) },
2387         { "GoLastPage", GTK_STOCK_GOTO_LAST, N_("_Last Page"), "<control>End",
2388           N_("Go to the last page"),
2389           G_CALLBACK (ev_window_cmd_go_last_page) },
2390
2391         /* Help menu */
2392         { "HelpContents", GTK_STOCK_HELP, N_("_Contents"), "F1",
2393           N_("Display help for the viewer application"),
2394           G_CALLBACK (ev_window_cmd_help_contents) },
2395
2396         { "HelpAbout", GTK_STOCK_ABOUT, N_("_About"), NULL,
2397           N_("Display credits for the document viewer creators"),
2398           G_CALLBACK (ev_window_cmd_help_about) },
2399
2400         /* Toolbar-only */
2401         { "LeaveFullscreen", EV_STOCK_LEAVE_FULLSCREEN, N_("Leave Fullscreen"), NULL,
2402           N_("Leave fullscreen mode"),
2403           G_CALLBACK (ev_window_cmd_leave_fullscreen) },
2404
2405         /* Accellerators */
2406         { "Escape", NULL, "", "Escape", "",
2407           G_CALLBACK (ev_window_cmd_escape) },
2408         { "Slash", GTK_STOCK_FIND, NULL, "slash",
2409           N_("Find a word or phrase in the document"),
2410           G_CALLBACK (ev_window_cmd_edit_find) },
2411         { "PageDown", NULL, "", "Page_Down",
2412           N_("Scroll one page forward"),
2413           G_CALLBACK (ev_window_cmd_scroll_forward) },
2414         { "PageUp", NULL, "", "Page_Up",
2415           N_("Scroll one page backward"),
2416           G_CALLBACK (ev_window_cmd_scroll_backward) },
2417         { "Space", NULL, "", "space",
2418           N_("Scroll one page forward"),
2419           G_CALLBACK (ev_window_cmd_scroll_forward) },
2420         { "ShiftSpace", NULL, "", "<shift>space",
2421           N_("Scroll one page backward"),
2422           G_CALLBACK (ev_window_cmd_scroll_backward) },
2423         { "BackSpace", NULL, "", "BackSpace",
2424           N_("Scroll one page backward"),
2425           G_CALLBACK (ev_window_cmd_scroll_backward) },
2426         { "ShiftBackSpace", NULL, "", "<shift>BackSpace",
2427           N_("Scroll one page forward"),
2428           G_CALLBACK (ev_window_cmd_scroll_forward) },
2429         { "Plus", GTK_STOCK_ZOOM_IN, NULL, "plus",
2430           N_("Enlarge the document"),
2431           G_CALLBACK (ev_window_cmd_view_zoom_in) },
2432         { "Minus", GTK_STOCK_ZOOM_OUT, NULL, "minus",
2433           N_("Shrink the document"),
2434           G_CALLBACK (ev_window_cmd_view_zoom_out) },
2435         { "FocusPageSelector", NULL, "", "<control>l",
2436           N_("Focus the page selector"),
2437           G_CALLBACK (ev_window_cmd_focus_page_selector) }
2438 };
2439
2440 /* Toggle items */
2441 static const GtkToggleActionEntry toggle_entries[] = {
2442         /* View Menu */
2443         { "ViewToolbar", NULL, N_("_Toolbar"), "<shift><control>T",
2444           N_("Show or hide the toolbar"),
2445           G_CALLBACK (ev_window_view_toolbar_cb), TRUE },
2446         { "ViewStatusbar", NULL, N_("_Statusbar"), NULL,
2447           N_("Show or hide the statusbar"),
2448           G_CALLBACK (ev_window_view_statusbar_cb), TRUE },
2449         { "ViewSidebar", NULL, N_("Side _pane"), "F9",
2450           N_("Show or hide the side pane"),
2451           G_CALLBACK (ev_window_view_sidebar_cb), TRUE },
2452         { "ViewContinuous", NULL, N_("_Continuous"), NULL,
2453           N_("Show the entire document"),
2454           G_CALLBACK (ev_window_cmd_continuous), TRUE },
2455         { "ViewDual", NULL, N_("_Dual"), NULL,
2456           N_("Show two pages at once"),
2457           G_CALLBACK (ev_window_cmd_dual), FALSE },
2458         { "ViewFullscreen", NULL, N_("_Fullscreen"), "F11",
2459           N_("Expand the window to fill the screen"),
2460           G_CALLBACK (ev_window_cmd_view_fullscreen) },
2461         { "ViewPresentation", NULL, N_("_Presentation"), "F12",
2462           N_("Run document as a presentation"),
2463           G_CALLBACK (ev_window_cmd_view_presentation) },
2464         { "ViewBestFit", EV_STOCK_ZOOM_PAGE, N_("_Best Fit"), NULL,
2465           N_("Make the current document fill the window"),
2466           G_CALLBACK (ev_window_cmd_view_best_fit) },
2467         { "ViewPageWidth", EV_STOCK_ZOOM_WIDTH, N_("Fit Page _Width"), NULL,
2468           N_("Make the current document fill the window width"),
2469           G_CALLBACK (ev_window_cmd_view_page_width) },
2470 };
2471
2472 static void
2473 drag_data_received_cb (GtkWidget *widget, GdkDragContext *context,
2474                        gint x, gint y, GtkSelectionData *selection_data,
2475                        guint info, guint time, gpointer gdata)
2476 {
2477         GList    *uri_list = NULL;
2478
2479         uri_list = gnome_vfs_uri_list_parse ((gchar *) selection_data->data);
2480
2481         if (uri_list) {
2482                 ev_window_open_uri_list (EV_WINDOW (widget), uri_list);
2483                 
2484                 gnome_vfs_uri_list_free (uri_list);
2485
2486                 gtk_drag_finish (context, TRUE, FALSE, time);
2487         }
2488 }
2489
2490 static void
2491 activate_link_cb (EvPageAction *page_action, EvLink *link, EvWindow *window)
2492 {
2493         g_return_if_fail (EV_IS_WINDOW (window));
2494
2495         ev_page_cache_set_link (window->priv->page_cache, link);
2496         gtk_widget_grab_focus (window->priv->view);
2497 }
2498
2499 static gboolean
2500 activate_label_cb (EvPageAction *page_action, char *label, EvWindow *window)
2501 {
2502         g_return_val_if_fail (EV_IS_WINDOW (window), FALSE);
2503
2504         gtk_widget_grab_focus (window->priv->view);
2505
2506         return ev_page_cache_set_page_label (window->priv->page_cache, label);
2507 }
2508
2509 static void
2510 register_custom_actions (EvWindow *window, GtkActionGroup *group)
2511 {
2512         GtkAction *action;
2513
2514         action = g_object_new (EV_TYPE_PAGE_ACTION,
2515                                "name", PAGE_SELECTOR_ACTION,
2516                                "label", _("Page"),
2517                                "tooltip", _("Select Page"),
2518                                "visible_overflown", FALSE,
2519                                NULL);
2520         g_signal_connect (action, "activate_link",
2521                           G_CALLBACK (activate_link_cb), window);
2522         g_signal_connect (action, "activate_label",
2523                           G_CALLBACK (activate_label_cb), window);
2524         gtk_action_group_add_action (group, action);
2525         g_object_unref (action);
2526
2527         action = g_object_new (EPHY_TYPE_ZOOM_ACTION,
2528                                "name", ZOOM_CONTROL_ACTION,
2529                                "label", _("Zoom"),
2530                                "stock_id", GTK_STOCK_ZOOM_IN,
2531                                "tooltip", _("Adjust the zoom level"),
2532                                "zoom", 1.0,
2533                                NULL);
2534         g_signal_connect (action, "zoom_to_level",
2535                           G_CALLBACK (zoom_control_changed_cb), window);
2536         gtk_action_group_add_action (group, action);
2537         g_object_unref (action);
2538 }
2539
2540 static void
2541 set_action_properties (GtkActionGroup *action_group)
2542 {
2543         GtkAction *action;
2544
2545         action = gtk_action_group_get_action (action_group, "GoPreviousPage");
2546         /*translators: this is the label for toolbar button*/
2547         g_object_set (action, "short_label", _("Previous"), NULL);
2548         g_object_set (action, "is-important", TRUE, NULL);
2549
2550         action = gtk_action_group_get_action (action_group, "GoNextPage");
2551         g_object_set (action, "is-important", TRUE, NULL);
2552         /*translators: this is the label for toolbar button*/
2553         g_object_set (action, "short_label", _("Next"), NULL);
2554
2555         action = gtk_action_group_get_action (action_group, "ViewZoomIn");
2556         /*translators: this is the label for toolbar button*/
2557         g_object_set (action, "short_label", _("Zoom In"), NULL);
2558         action = gtk_action_group_get_action (action_group, "ViewZoomIn");
2559
2560         action = gtk_action_group_get_action (action_group, "ViewZoomOut");
2561         /*translators: this is the label for toolbar button*/
2562         g_object_set (action, "short_label", _("Zoom Out"), NULL);
2563         action = gtk_action_group_get_action (action_group, "ViewZoomIn");
2564
2565         action = gtk_action_group_get_action (action_group, "ViewBestFit");
2566         /*translators: this is the label for toolbar button*/
2567         g_object_set (action, "short_label", _("Best Fit"), NULL);
2568         action = gtk_action_group_get_action (action_group, "ViewZoomIn");
2569
2570         action = gtk_action_group_get_action (action_group, "ViewPageWidth");
2571         /*translators: this is the label for toolbar button*/
2572         g_object_set (action, "short_label", _("Fit Width"), NULL);
2573         action = gtk_action_group_get_action (action_group, "ViewZoomIn");
2574
2575         action = gtk_action_group_get_action (action_group, "LeaveFullscreen");
2576         g_object_set (action, "is-important", TRUE, NULL);
2577 }
2578
2579 static void
2580 set_chrome_actions (EvWindow *window)
2581 {
2582         EvWindowPrivate *priv = window->priv;
2583         GtkActionGroup *action_group = priv->action_group;
2584         GtkAction *action;
2585
2586         action= gtk_action_group_get_action (action_group, "ViewToolbar");
2587         g_signal_handlers_block_by_func
2588                 (action, G_CALLBACK (ev_window_view_toolbar_cb), window);
2589         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
2590                                       (priv->chrome & EV_CHROME_TOOLBAR) != 0);
2591         g_signal_handlers_unblock_by_func
2592                 (action, G_CALLBACK (ev_window_view_toolbar_cb), window);
2593
2594         action= gtk_action_group_get_action (action_group, "ViewSidebar");
2595         g_signal_handlers_block_by_func
2596                 (action, G_CALLBACK (ev_window_view_sidebar_cb), window);
2597         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
2598                                       (priv->chrome & EV_CHROME_SIDEBAR) != 0);
2599         g_signal_handlers_unblock_by_func
2600                 (action, G_CALLBACK (ev_window_view_sidebar_cb), window);
2601
2602         action= gtk_action_group_get_action (action_group, "ViewStatusbar");
2603         g_signal_handlers_block_by_func
2604                 (action, G_CALLBACK (ev_window_view_statusbar_cb), window);
2605         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
2606                                       (priv->chrome & EV_CHROME_STATUSBAR) != 0);
2607         g_signal_handlers_unblock_by_func
2608                 (action, G_CALLBACK (ev_window_view_statusbar_cb), window);
2609 }
2610
2611 static EvChrome
2612 load_chrome (void)
2613 {
2614         EvChrome chrome = EV_CHROME_NORMAL;
2615         GConfClient *client;
2616         GConfValue *value;
2617
2618         client = gconf_client_get_default ();
2619
2620         value = gconf_client_get (client, GCONF_CHROME_TOOLBAR, NULL);
2621         if (value != NULL) {
2622                 if (value->type == GCONF_VALUE_BOOL && !gconf_value_get_bool (value)) {
2623                         chrome &= ~EV_CHROME_TOOLBAR;
2624                 }
2625                 gconf_value_free (value);
2626         }
2627
2628         value = gconf_client_get (client, GCONF_CHROME_SIDEBAR, NULL);
2629         if (value != NULL) {
2630                 if (value->type == GCONF_VALUE_BOOL && !gconf_value_get_bool (value)) {
2631                         chrome &= ~EV_CHROME_SIDEBAR;
2632                 }
2633                 gconf_value_free (value);
2634         }
2635
2636         value = gconf_client_get (client, GCONF_CHROME_STATUSBAR, NULL);
2637         if (value != NULL) {
2638                 if (value->type == GCONF_VALUE_BOOL && !gconf_value_get_bool (value)) {
2639                         chrome &= ~EV_CHROME_STATUSBAR;
2640                 }
2641                 gconf_value_free (value);
2642         }
2643
2644         g_object_unref (client);
2645
2646         return chrome;
2647 }
2648
2649 static void
2650 sidebar_widget_model_set (EvSidebarLinks *ev_sidebar_links,
2651                           GParamSpec     *pspec,
2652                           EvWindow       *ev_window)
2653 {
2654         GtkTreeModel *model;
2655         GtkAction *action;
2656
2657         g_object_get (G_OBJECT (ev_sidebar_links),
2658                       "model", &model,
2659                       NULL);
2660
2661         action = gtk_action_group_get_action (ev_window->priv->action_group, PAGE_SELECTOR_ACTION);
2662         ev_page_action_set_model (EV_PAGE_ACTION (action), model);
2663 }
2664
2665
2666 static void
2667 set_view_actions_sensitivity (EvWindow *window, gboolean sensitive)
2668 {
2669         if (window->priv->action_group) {
2670                 set_action_sensitive (window, "PageDown", sensitive);
2671                 set_action_sensitive (window, "PageUp", sensitive);
2672                 set_action_sensitive (window, "Space", sensitive);
2673                 set_action_sensitive (window, "ShiftSpace", sensitive);
2674                 set_action_sensitive (window, "BackSpace", sensitive);
2675                 set_action_sensitive (window, "ShiftBackSpace", sensitive);
2676                 set_action_sensitive (window, "Slash", sensitive);
2677                 set_action_sensitive (window, "Plus", sensitive);
2678                 set_action_sensitive (window, "Minus", sensitive);
2679         }
2680 }
2681
2682 static void
2683 view_actions_focus_in_cb (GtkWidget *widget, GdkEventFocus *event, EvWindow *window)
2684 {
2685         set_view_actions_sensitivity (window, TRUE);
2686 }
2687
2688 static void
2689 view_actions_focus_out_cb (GtkWidget *widget, GdkEventFocus *event, EvWindow *window)
2690 {
2691         set_view_actions_sensitivity (window, FALSE);
2692 }
2693
2694 static void
2695 enable_view_actions_for_widget (EvWindow *window, GtkWidget *widget)
2696 {
2697         g_signal_connect_object (widget, "focus_in_event",
2698                                  G_CALLBACK (view_actions_focus_in_cb),
2699                                  window, 0);
2700         g_signal_connect_object (widget, "focus_out_event",
2701                                  G_CALLBACK (view_actions_focus_out_cb),
2702                                  window, 0);
2703 }
2704
2705 static void
2706 ev_window_init (EvWindow *ev_window)
2707 {
2708         GtkActionGroup *action_group;
2709         GtkAccelGroup *accel_group;
2710         GError *error = NULL;
2711         GtkWidget *sidebar_widget, *toolbar_dock, *tree_view;
2712         GConfValue *value;
2713         GConfClient *client;
2714         int sidebar_size;
2715
2716         ev_window->priv = EV_WINDOW_GET_PRIVATE (ev_window);
2717
2718         ev_window->priv->page_mode = PAGE_MODE_DOCUMENT;
2719         update_window_title (NULL, NULL, ev_window);
2720
2721         ev_window->priv->main_box = gtk_vbox_new (FALSE, 0);
2722         gtk_container_add (GTK_CONTAINER (ev_window), ev_window->priv->main_box);
2723         gtk_widget_show (ev_window->priv->main_box);
2724
2725         action_group = gtk_action_group_new ("MenuActions");
2726         ev_window->priv->action_group = action_group;
2727         gtk_action_group_set_translation_domain (action_group, NULL);
2728         gtk_action_group_add_actions (action_group, entries,
2729                                       G_N_ELEMENTS (entries), ev_window);
2730         gtk_action_group_add_toggle_actions (action_group, toggle_entries,
2731                                              G_N_ELEMENTS (toggle_entries),
2732                                              ev_window);
2733         set_action_properties (action_group);
2734         register_custom_actions (ev_window, action_group);
2735
2736         ev_window->priv->ui_manager = gtk_ui_manager_new ();
2737         gtk_ui_manager_insert_action_group (ev_window->priv->ui_manager,
2738                                             action_group, 0);
2739
2740         accel_group =
2741                 gtk_ui_manager_get_accel_group (ev_window->priv->ui_manager);
2742         gtk_window_add_accel_group (GTK_WINDOW (ev_window), accel_group);
2743
2744         set_view_actions_sensitivity (ev_window, FALSE);
2745
2746         g_signal_connect (ev_window->priv->ui_manager, "connect_proxy",
2747                           G_CALLBACK (connect_proxy_cb), ev_window);
2748         g_signal_connect (ev_window->priv->ui_manager, "disconnect_proxy",
2749                           G_CALLBACK (disconnect_proxy_cb), ev_window);
2750
2751         if (!gtk_ui_manager_add_ui_from_file (ev_window->priv->ui_manager,
2752                                               DATADIR"/evince-ui.xml",
2753                                               &error)) {
2754                 g_warning ("building menus failed: %s", error->message);
2755                 g_error_free (error);
2756         }
2757
2758         ev_window->priv->menubar =
2759                  gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
2760                                             "/MainMenu");
2761         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box),
2762                             ev_window->priv->menubar,
2763                             FALSE, FALSE, 0);
2764
2765         /* Toolbar editor */
2766         ev_window->priv->toolbar_model = egg_toolbars_model_new ();
2767         
2768         ev_window->priv->toolbar_file = g_build_filename
2769                         (ev_dot_dir (), "evince_toolbar.xml", NULL);
2770
2771         if (!g_file_test (ev_window->priv->toolbar_file, G_FILE_TEST_EXISTS)) {
2772                 egg_toolbars_model_load (ev_window->priv->toolbar_model,
2773                                          DATADIR"/evince-toolbar.xml");
2774         } else {
2775                 egg_toolbars_model_load (ev_window->priv->toolbar_model,
2776                                          ev_window->priv->toolbar_file);
2777         }
2778
2779         egg_toolbars_model_set_flags (ev_window->priv->toolbar_model, 0,
2780                                       EGG_TB_MODEL_NOT_REMOVABLE); 
2781         
2782         /* This sucks, but there is no way to have a draw=no, expand=true separator
2783          * in a GtkUIManager-built toolbar. So, just add another toolbar.
2784          * See gtk+ bug 166489.
2785          */
2786         toolbar_dock = ev_window->priv->toolbar_dock = gtk_hbox_new (FALSE, 0);
2787         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), toolbar_dock,
2788                             FALSE, FALSE, 0);
2789         gtk_widget_show (toolbar_dock);
2790
2791         ev_window->priv->toolbar = egg_editable_toolbar_new_with_model
2792                                 (ev_window->priv->ui_manager, ev_window->priv->toolbar_model);
2793         egg_editable_toolbar_show (EGG_EDITABLE_TOOLBAR (ev_window->priv->toolbar),
2794                                    "DefaultToolBar");
2795         gtk_box_pack_start (GTK_BOX (toolbar_dock), ev_window->priv->toolbar,
2796                             TRUE, TRUE, 0);
2797         gtk_widget_show (ev_window->priv->toolbar);
2798
2799         ev_window->priv->fullscreen_toolbar =
2800                 gtk_ui_manager_get_widget (ev_window->priv->ui_manager, "/LeaveFullscreenToolbar");
2801         gtk_toolbar_set_show_arrow (GTK_TOOLBAR (ev_window->priv->fullscreen_toolbar), TRUE);
2802         
2803
2804         /* Add the main area */
2805         ev_window->priv->hpaned = gtk_hpaned_new ();
2806         g_signal_connect (ev_window->priv->hpaned,
2807                           "notify::position",
2808                           G_CALLBACK (ev_window_sidebar_position_change_cb),
2809                           ev_window);
2810         
2811         sidebar_size = SIDEBAR_DEFAULT_SIZE;
2812         client = gconf_client_get_default ();
2813         value = gconf_client_get (client, GCONF_SIDEBAR_SIZE, NULL);
2814         if (value != NULL) {
2815                 if (value->type == GCONF_VALUE_INT) {
2816                         sidebar_size = gconf_value_get_int (value);
2817                 }
2818                 gconf_value_free (value);
2819         }
2820         g_object_unref (client);
2821         gtk_paned_set_position (GTK_PANED (ev_window->priv->hpaned), sidebar_size);
2822         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), ev_window->priv->hpaned,
2823                             TRUE, TRUE, 0);
2824         gtk_widget_show (ev_window->priv->hpaned);
2825         
2826         ev_window->priv->sidebar = ev_sidebar_new ();
2827         gtk_paned_pack1 (GTK_PANED (ev_window->priv->hpaned),
2828                          ev_window->priv->sidebar, FALSE, FALSE);
2829         gtk_widget_show (ev_window->priv->sidebar);
2830
2831         /* Stub sidebar, for now */
2832         sidebar_widget = ev_sidebar_links_new ();
2833         g_signal_connect (sidebar_widget,
2834                           "notify::model",
2835                           G_CALLBACK (sidebar_widget_model_set),
2836                           ev_window);
2837         tree_view = ev_sidebar_links_get_treeview
2838                         (EV_SIDEBAR_LINKS (sidebar_widget));
2839         enable_view_actions_for_widget (ev_window, tree_view);
2840         gtk_widget_show (sidebar_widget);
2841         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
2842                              sidebar_widget);
2843
2844         sidebar_widget = ev_sidebar_thumbnails_new ();
2845         tree_view = ev_sidebar_thumbnails_get_treeview
2846                         (EV_SIDEBAR_THUMBNAILS (sidebar_widget));
2847         enable_view_actions_for_widget (ev_window, tree_view);
2848         gtk_widget_show (sidebar_widget);
2849         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
2850                              sidebar_widget);
2851
2852         ev_window->priv->scrolled_window =
2853                 GTK_WIDGET (g_object_new (GTK_TYPE_SCROLLED_WINDOW,
2854                                           "shadow-type", GTK_SHADOW_IN,
2855                                           NULL));
2856         gtk_widget_show (ev_window->priv->scrolled_window);
2857
2858         gtk_paned_add2 (GTK_PANED (ev_window->priv->hpaned),
2859                         ev_window->priv->scrolled_window);
2860
2861         ev_window->priv->view = ev_view_new ();
2862         ev_window->priv->password_view = ev_password_view_new ();
2863         g_signal_connect_swapped (ev_window->priv->password_view,
2864                                   "unlock",
2865                                   G_CALLBACK (ev_window_popup_password_dialog),
2866                                   ev_window);
2867         enable_view_actions_for_widget (ev_window, ev_window->priv->view);
2868         gtk_widget_show (ev_window->priv->view);
2869         gtk_widget_show (ev_window->priv->password_view);
2870
2871         /* We own a ref on these widgets, as we can swap them in and out */
2872         g_object_ref (ev_window->priv->view);
2873         //g_object_ref (ev_window->priv->page_view);
2874         g_object_ref (ev_window->priv->password_view);
2875
2876         gtk_container_add (GTK_CONTAINER (ev_window->priv->scrolled_window),
2877                            ev_window->priv->view);
2878
2879         g_signal_connect (ev_window->priv->view,
2880                           "notify::find-status",
2881                           G_CALLBACK (view_find_status_changed_cb),
2882                           ev_window);
2883         g_signal_connect (ev_window->priv->view,
2884                           "notify::status",
2885                           G_CALLBACK (view_status_changed_cb),
2886                           ev_window);
2887         g_signal_connect (ev_window->priv->view,
2888                           "notify::sizing-mode",
2889                           G_CALLBACK (ev_window_sizing_mode_changed_cb),
2890                           ev_window);
2891         g_signal_connect (ev_window->priv->view,
2892                           "notify::zoom",
2893                           G_CALLBACK (ev_window_zoom_changed_cb),
2894                           ev_window);
2895
2896         ev_window->priv->statusbar = gtk_statusbar_new ();
2897         gtk_box_pack_end (GTK_BOX (ev_window->priv->main_box),
2898                           ev_window->priv->statusbar,
2899                           FALSE, TRUE, 0);
2900         ev_window->priv->help_message_cid = gtk_statusbar_get_context_id
2901                 (GTK_STATUSBAR (ev_window->priv->statusbar), "help_message");
2902         ev_window->priv->view_message_cid = gtk_statusbar_get_context_id
2903                 (GTK_STATUSBAR (ev_window->priv->statusbar), "view_message");
2904
2905         ev_window->priv->find_bar = egg_find_bar_new ();
2906         gtk_box_pack_end (GTK_BOX (ev_window->priv->main_box),
2907                           ev_window->priv->find_bar,
2908                           FALSE, TRUE, 0);
2909
2910         ev_window_setup_recent (ev_window);
2911         ev_window->priv->chrome = load_chrome ();
2912         set_chrome_actions (ev_window);
2913         update_chrome_visibility (ev_window);
2914
2915         /* Connect sidebar signals */
2916         g_signal_connect (ev_window->priv->sidebar,
2917                           "notify::visible",
2918                           G_CALLBACK (ev_window_sidebar_visibility_changed_cb),
2919                           ev_window);
2920         
2921         /* Connect to find bar signals */
2922         g_signal_connect (ev_window->priv->find_bar,
2923                           "previous",
2924                           G_CALLBACK (find_bar_previous_cb),
2925                           ev_window);
2926         g_signal_connect (ev_window->priv->find_bar,
2927                           "next",
2928                           G_CALLBACK (find_bar_next_cb),
2929                           ev_window);
2930         g_signal_connect (ev_window->priv->find_bar,
2931                           "close",
2932                           G_CALLBACK (find_bar_close_cb),
2933                           ev_window);
2934         g_signal_connect (ev_window->priv->find_bar,
2935                           "notify::search-string",
2936                           G_CALLBACK (find_bar_search_changed_cb),
2937                           ev_window);
2938         g_signal_connect (ev_window->priv->find_bar,
2939                           "notify::case-sensitive",
2940                           G_CALLBACK (find_bar_search_changed_cb),
2941                           ev_window);
2942         g_signal_connect (ev_window->priv->find_bar,
2943                           "notify::visible",
2944                           G_CALLBACK (find_bar_search_changed_cb),
2945                           ev_window);
2946
2947         /* Give focus to the document view */
2948         gtk_widget_grab_focus (ev_window->priv->view);
2949
2950         /* Drag and Drop */
2951         gtk_drag_dest_unset (GTK_WIDGET (ev_window));
2952         gtk_drag_dest_set (GTK_WIDGET (ev_window), GTK_DEST_DEFAULT_ALL, ev_drop_types,
2953                            sizeof (ev_drop_types) / sizeof (ev_drop_types[0]),
2954                            GDK_ACTION_COPY);
2955         g_signal_connect (G_OBJECT (ev_window), "drag_data_received",
2956                           G_CALLBACK (drag_data_received_cb), NULL);
2957
2958         /* Set it to something random to force a change */
2959
2960         ev_window_sizing_mode_changed_cb (EV_VIEW (ev_window->priv->view), NULL, ev_window);
2961         update_action_sensitivity (ev_window);
2962 }