]> www.fi.muni.cz Git - evince.git/blob - shell/ev-window.c
52fdd7594e9453103ede20bd8c4cfba049978136
[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-window-title.h"
33 #include "ev-page-action.h"
34 #include "ev-sidebar.h"
35 #include "ev-sidebar-links.h"
36 #include "ev-sidebar-attachments.h"
37 #include "ev-sidebar-thumbnails.h"
38 #include "ev-view.h"
39 #include "ev-password.h"
40 #include "ev-password-view.h"
41 #include "ev-properties-dialog.h"
42 #include "ev-ps-exporter.h"
43 #include "ev-document-thumbnails.h"
44 #include "ev-document-links.h"
45 #include "ev-document-fonts.h"
46 #include "ev-document-find.h"
47 #include "ev-document-security.h"
48 #include "ev-document-factory.h"
49 #include "ev-job-queue.h"
50 #include "ev-jobs.h"
51 #include "ev-sidebar-page.h"
52 #include "eggfindbar.h"
53
54 #ifndef HAVE_GTK_RECENT
55 #include "egg-recent-view-uimanager.h"
56 #include "egg-recent-view.h"
57 #include "egg-recent-model.h"
58 #endif
59
60 #include "egg-toolbar-editor.h"
61 #include "egg-editable-toolbar.h"
62 #include "egg-toolbars-model.h"
63 #include "ephy-zoom.h"
64 #include "ephy-zoom-action.h"
65 #include "ev-application.h"
66 #include "ev-stock-icons.h"
67 #include "ev-metadata-manager.h"
68 #include "ev-file-helpers.h"
69 #include "ev-utils.h"
70 #include "ev-debug.h"
71
72 #ifdef WITH_GNOME_PRINT
73 #include "ev-print-job.h"
74 #include <libgnomeprintui/gnome-print-dialog.h>
75 #endif
76
77 #ifdef WITH_GTK_PRINT
78 #include <gtk/gtkprintunixdialog.h>
79 #endif
80
81 #include <poppler.h>
82
83 #include <glib/gi18n.h>
84 #include <gtk/gtk.h>
85 #include <gnome.h>
86 #include <libgnomevfs/gnome-vfs-utils.h>
87 #include <gconf/gconf-client.h>
88
89 #include <string.h>
90
91 typedef enum {
92         PAGE_MODE_DOCUMENT,
93         PAGE_MODE_PASSWORD
94 } EvWindowPageMode;
95
96 typedef enum {
97         EV_CHROME_MENUBAR       = 1 << 0,
98         EV_CHROME_TOOLBAR       = 1 << 1,
99         EV_CHROME_FINDBAR       = 1 << 2,
100         EV_CHROME_RAISE_TOOLBAR = 1 << 3,
101         EV_CHROME_FULLSCREEN_TOOLBAR    = 1 << 4,
102         EV_CHROME_SIDEBAR       = 1 << 5,
103         EV_CHROME_NORMAL        = EV_CHROME_MENUBAR | EV_CHROME_TOOLBAR | EV_CHROME_SIDEBAR
104 } EvChrome;
105
106 struct _EvWindowPrivate {
107         /* UI */
108         EvChrome chrome;
109
110         GtkWidget *main_box;
111         GtkWidget *menubar;
112         GtkWidget *toolbar_dock;
113         GtkWidget *toolbar;
114         GtkWidget *hpaned;
115         GtkWidget *sidebar;
116         GtkWidget *find_bar;
117         GtkWidget *scrolled_window;
118         GtkWidget *view;
119         GtkWidget *password_view;
120         GtkWidget *sidebar_thumbs;
121         GtkWidget *sidebar_links;
122         GtkWidget *sidebar_attachments;
123
124         /* Dialogs */
125         GtkWidget *properties;
126 #ifdef WITH_PRINT
127         GtkWidget *print_dialog;
128 #endif
129         GtkWidget *password_dialog;
130
131         /* UI Builders */
132         GtkActionGroup   *action_group;
133         GtkActionGroup   *view_popup_action_group;
134         GtkActionGroup   *attachment_popup_action_group;
135 #ifdef HAVE_GTK_RECENT
136         GtkRecentManager *recent_manager;
137         GtkActionGroup   *recent_action_group;
138         guint             recent_ui_id;
139 #endif
140         GtkUIManager     *ui_manager;
141
142         /* Fullscreen mode */
143         GtkWidget *fullscreen_toolbar;
144         GtkWidget *fullscreen_popup;
145         guint      fullscreen_timeout_id;
146         
147         /* Popup link */
148         GtkWidget *view_popup;
149         EvLink    *link;
150
151         /* Popup attachment */
152         GtkWidget    *attachment_popup;
153         GList        *attach_list;
154
155         /* Document */
156         char *uri;
157         char *local_uri;
158         EvLinkDest *dest;
159         
160         EvDocument *document;
161
162         EvPageCache *page_cache;
163         EvWindowPageMode page_mode;
164         EvWindowTitle *title;
165 #ifndef HAVE_GTK_RECENT
166         EggRecentViewUIManager *recent_view;
167 #endif
168
169         EvJob *xfer_job;
170 #ifdef WITH_GNOME_PRINT
171         GnomePrintJob *print_job;
172 #endif
173
174 #ifdef WITH_GTK_PRINT
175         EvJob            *print_job;
176         GtkPrintJob      *gtk_print_job;
177         GtkPrinter       *printer;
178         GtkPrintSettings *print_settings;
179         GtkPageSetup     *print_page_setup;
180 #endif
181 };
182
183 static const GtkTargetEntry ev_drop_types[] = {
184         { "text/uri-list", 0, 0 }
185 };
186
187
188 #define EV_WINDOW_GET_PRIVATE(object) \
189         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_WINDOW, EvWindowPrivate))
190
191 #define PAGE_SELECTOR_ACTION    "PageSelector"
192 #define ZOOM_CONTROL_ACTION     "ViewZoom"
193
194 #define GCONF_OVERRIDE_RESTRICTIONS "/apps/evince/override_restrictions"
195 #define GCONF_LOCKDOWN_SAVE         "/desktop/gnome/lockdown/disable_save_to_disk"
196 #define GCONF_LOCKDOWN_PRINT        "/desktop/gnome/lockdown/disable_printing"
197
198 #define FULLSCREEN_TIMEOUT 5 * 1000
199
200 #define SIDEBAR_DEFAULT_SIZE    132
201 #define LINKS_SIDEBAR_ID "links"
202 #define THUMBNAILS_SIDEBAR_ID "thumbnails"
203 #define ATTACHMENTS_SIDEBAR_ID "attachments"
204
205 static void     ev_window_update_actions                (EvWindow *ev_window);
206 static void     ev_window_update_fullscreen_popup       (EvWindow         *window);
207 static void     ev_window_sidebar_visibility_changed_cb (EvSidebar        *ev_sidebar,
208                                                          GParamSpec       *pspec,
209                                                          EvWindow         *ev_window);
210 static void     ev_window_set_page_mode                 (EvWindow         *window,
211                                                          EvWindowPageMode  page_mode);
212 static void     ev_window_xfer_job_cb                   (EvJobXfer        *job,
213                                                          gpointer          data);
214 #ifdef WITH_GTK_PRINT
215 static void     ev_window_print_job_cb                  (EvJobPrint       *job,
216                                                          EvWindow         *window);
217 #endif
218 static void     ev_window_sizing_mode_changed_cb        (EvView           *view,
219                                                          GParamSpec       *pspec,
220                                                          EvWindow         *ev_window);
221 static void     ev_window_zoom_changed_cb               (EvView           *view,
222                                                          GParamSpec       *pspec,
223                                                          EvWindow         *ev_window);
224 static void     ev_window_add_recent                    (EvWindow         *window,
225                                                          const char       *filename);
226 static void     ev_window_run_fullscreen                (EvWindow         *window);
227 static void     ev_window_stop_fullscreen               (EvWindow         *window);
228 static void     ev_window_cmd_view_fullscreen           (GtkAction        *action,
229                                                          EvWindow         *window);
230 static void     ev_window_run_presentation              (EvWindow         *window);
231 static void     ev_window_stop_presentation             (EvWindow         *window);
232 static void     ev_window_cmd_view_presentation         (GtkAction        *action,
233                                                          EvWindow         *window);
234 static void     ev_window_run_preview                   (EvWindow         *window);
235 static void     ev_view_popup_cmd_open_link             (GtkAction        *action,
236                                                          EvWindow         *window);
237 static void     ev_view_popup_cmd_copy_link_address     (GtkAction        *action,
238                                                          EvWindow         *window);
239 static void     ev_attachment_popup_cmd_open_attachment (GtkAction        *action,
240                                                          EvWindow *window);
241 static void     ev_attachment_popup_cmd_save_attachment_as (GtkAction *action, 
242                                                          EvWindow *window);
243 static void     ev_window_cmd_view_best_fit             (GtkAction        *action, 
244                                                          EvWindow         *ev_window);
245 static void     ev_window_cmd_view_page_width           (GtkAction        *action, 
246                                                          EvWindow         *ev_window);
247 static void     fullscreen_set_timeout                  (EvWindow *window);
248 static gboolean fullscreen_motion_notify_cb             (GtkWidget *widget,
249                                                          GdkEventMotion *event,
250                                                          gpointer user_data);
251 static gboolean fullscreen_leave_notify_cb              (GtkWidget *widget,
252                                                          GdkEventCrossing *event,
253                                                          gpointer user_data);
254
255 G_DEFINE_TYPE (EvWindow, ev_window, GTK_TYPE_WINDOW)
256
257 static void
258 ev_window_set_action_sensitive (EvWindow   *ev_window,
259                                 const char *name,
260                                 gboolean    sensitive)
261 {
262         GtkAction *action = gtk_action_group_get_action (ev_window->priv->action_group,
263                                                          name);
264         gtk_action_set_sensitive (action, sensitive);
265 }
266
267
268 static void
269 ev_window_setup_action_sensitivity (EvWindow *ev_window)
270 {
271         EvDocument *document = ev_window->priv->document;
272         const EvDocumentInfo *info = NULL;
273
274         gboolean has_document = FALSE;
275         gboolean ok_to_print = TRUE;
276         gboolean ok_to_copy = TRUE;
277         gboolean has_properties = TRUE;
278         gboolean override_restrictions = FALSE;
279         gboolean can_get_text = FALSE;
280         gboolean has_pages = FALSE;
281         gboolean can_find = FALSE;
282
283         GConfClient *client;
284
285         if (document) {
286                 has_document = TRUE;
287                 info = ev_page_cache_get_info (ev_window->priv->page_cache);
288         }
289
290         if (has_document && ev_window->priv->page_cache) {
291                 has_pages = ev_page_cache_get_n_pages (ev_window->priv->page_cache) > 0;
292         }
293
294         if (!info || info->fields_mask == 0) {
295                 has_properties = FALSE;
296         }
297
298         if (has_document && ev_document_can_get_text (document)) {
299                 can_get_text = TRUE;
300         }
301         
302         if (has_pages && EV_IS_DOCUMENT_FIND (document)) {
303                 can_find = TRUE;
304         }
305
306         client = gconf_client_get_default ();
307         override_restrictions = gconf_client_get_bool (client, 
308                                                        GCONF_OVERRIDE_RESTRICTIONS, 
309                                                        NULL);
310         if (!override_restrictions && info && info->fields_mask & EV_DOCUMENT_INFO_PERMISSIONS) {
311                 ok_to_print = (info->permissions & EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT);
312                 ok_to_copy = (info->permissions & EV_DOCUMENT_PERMISSIONS_OK_TO_COPY);
313         }
314
315         if (has_document && !EV_IS_PS_EXPORTER(document))
316                 ok_to_print = FALSE;
317
318         
319         if (gconf_client_get_bool (client, GCONF_LOCKDOWN_SAVE, NULL)) {
320                 ok_to_copy = FALSE;
321         }
322
323         if (gconf_client_get_bool (client, GCONF_LOCKDOWN_PRINT, NULL)) {
324                 ok_to_print = FALSE;
325         }
326 #ifndef WITH_PRINT
327         ok_to_print = FALSE;
328 #endif
329         g_object_unref (client);
330
331
332         /* File menu */
333         ev_window_set_action_sensitive (ev_window, "FileSaveAs", has_document && ok_to_copy);
334         ev_window_set_action_sensitive (ev_window, "FilePrint", has_pages && ok_to_print);
335         ev_window_set_action_sensitive (ev_window, "FileProperties", has_document && has_properties);
336
337         /* Edit menu */
338         ev_window_set_action_sensitive (ev_window, "EditSelectAll", has_pages && can_get_text);
339         ev_window_set_action_sensitive (ev_window, "EditFind", can_find);
340         ev_window_set_action_sensitive (ev_window, "Slash", can_find);
341         ev_window_set_action_sensitive (ev_window, "EditRotateLeft", has_pages);
342         ev_window_set_action_sensitive (ev_window, "EditRotateRight", has_pages);
343
344         /* View menu */
345         ev_window_set_action_sensitive (ev_window, "ViewContinuous", has_pages);
346         ev_window_set_action_sensitive (ev_window, "ViewDual", has_pages);
347         ev_window_set_action_sensitive (ev_window, "ViewBestFit", has_pages);
348         ev_window_set_action_sensitive (ev_window, "ViewPageWidth", has_pages);
349         ev_window_set_action_sensitive (ev_window, "ViewReload", has_pages);
350
351         /* Toolbar-specific actions: */
352         ev_window_set_action_sensitive (ev_window, PAGE_SELECTOR_ACTION, has_pages);
353         ev_window_set_action_sensitive (ev_window, ZOOM_CONTROL_ACTION,  has_pages);
354
355         ev_window_update_actions (ev_window);
356 }
357
358 static void
359 ev_window_update_actions (EvWindow *ev_window)
360 {
361         EvView *view = EV_VIEW (ev_window->priv->view);
362         int n_pages = 0, page = -1;
363         gboolean has_pages = FALSE;
364
365         if (ev_window->priv->document && ev_window->priv->page_cache) {
366                 page = ev_page_cache_get_current_page (ev_window->priv->page_cache);
367                 n_pages = ev_page_cache_get_n_pages (ev_window->priv->page_cache);
368                 has_pages = n_pages > 0;
369         }
370
371         ev_window_set_action_sensitive (ev_window, "EditCopy", has_pages && ev_view_get_has_selection (view));
372         ev_window_set_action_sensitive (ev_window, "EditFindNext",
373                               ev_view_can_find_next (view));
374         ev_window_set_action_sensitive (ev_window, "EditFindPrevious",
375                               ev_view_can_find_previous (view));
376
377         ev_window_set_action_sensitive (ev_window, "ViewZoomIn",
378                               has_pages && ev_view_can_zoom_in (view));
379         ev_window_set_action_sensitive (ev_window, "ViewZoomOut",
380                               has_pages && ev_view_can_zoom_out (view));
381
382         /* Go menu */
383         if (has_pages) {
384                 ev_window_set_action_sensitive (ev_window, "GoPreviousPage", page > 0);
385                 ev_window_set_action_sensitive (ev_window, "GoNextPage", page < n_pages - 1);
386                 ev_window_set_action_sensitive (ev_window, "GoFirstPage", page > 0);
387                 ev_window_set_action_sensitive (ev_window, "GoLastPage", page < n_pages - 1);
388         } else {
389                 ev_window_set_action_sensitive (ev_window, "GoFirstPage", FALSE);
390                 ev_window_set_action_sensitive (ev_window, "GoPreviousPage", FALSE);
391                 ev_window_set_action_sensitive (ev_window, "GoNextPage", FALSE);
392                 ev_window_set_action_sensitive (ev_window, "GoLastPage", FALSE);
393         }
394
395         if (has_pages &&
396             ev_view_get_sizing_mode (view) != EV_SIZING_FIT_WIDTH &&
397             ev_view_get_sizing_mode (view) != EV_SIZING_BEST_FIT) {
398                 GtkAction *action;
399                 float      zoom;
400                 float      real_zoom;
401
402                 action = gtk_action_group_get_action (ev_window->priv->action_group, 
403                                                       ZOOM_CONTROL_ACTION);
404
405                 real_zoom = ev_view_get_zoom (EV_VIEW (ev_window->priv->view));
406                 zoom = ephy_zoom_get_nearest_zoom_level (real_zoom);
407
408                 ephy_zoom_action_set_zoom_level (EPHY_ZOOM_ACTION (action), zoom);
409         }
410 }
411
412 static void
413 ev_window_set_view_accels_sensitivity (EvWindow *window, gboolean sensitive)
414 {
415         gboolean can_find;
416         
417         can_find = window->priv->document && 
418             EV_IS_DOCUMENT_FIND (window->priv->document);
419
420         if (window->priv->action_group) {
421                 ev_window_set_action_sensitive (window, "PageDown", sensitive);
422                 ev_window_set_action_sensitive (window, "PageUp", sensitive);
423                 ev_window_set_action_sensitive (window, "Space", sensitive);
424                 ev_window_set_action_sensitive (window, "ShiftSpace", sensitive);
425                 ev_window_set_action_sensitive (window, "BackSpace", sensitive);
426                 ev_window_set_action_sensitive (window, "ShiftBackSpace", sensitive);
427                 ev_window_set_action_sensitive (window, "Return", sensitive);
428                 ev_window_set_action_sensitive (window, "ShiftReturn", sensitive);
429                 ev_window_set_action_sensitive (window, "Plus", sensitive);
430                 ev_window_set_action_sensitive (window, "Minus", sensitive);
431                 ev_window_set_action_sensitive (window, "KpPlus", sensitive);
432                 ev_window_set_action_sensitive (window, "KpMinus", sensitive);
433                 ev_window_set_action_sensitive (window, "Equal", sensitive);
434
435                 ev_window_set_action_sensitive (window, "Slash", sensitive && can_find);
436         }
437 }
438
439 static void
440 set_widget_visibility (GtkWidget *widget, gboolean visible)
441 {
442         g_assert (GTK_IS_WIDGET (widget));
443         
444         if (visible)
445                 gtk_widget_show (widget);
446         else
447                 gtk_widget_hide (widget);
448 }
449
450 static void
451 update_chrome_visibility (EvWindow *window)
452 {
453         EvWindowPrivate *priv = window->priv;
454         gboolean menubar, toolbar, findbar, fullscreen_toolbar, sidebar;
455         gboolean fullscreen_mode, presentation, fullscreen;
456
457         presentation = ev_view_get_presentation (EV_VIEW (priv->view));
458         fullscreen = ev_view_get_fullscreen (EV_VIEW (priv->view));
459         fullscreen_mode = fullscreen || presentation;
460
461         menubar = (priv->chrome & EV_CHROME_MENUBAR) != 0 && !fullscreen_mode;
462         toolbar = ((priv->chrome & EV_CHROME_TOOLBAR) != 0  || 
463                    (priv->chrome & EV_CHROME_RAISE_TOOLBAR) != 0) && !fullscreen_mode;
464         fullscreen_toolbar = ((priv->chrome & EV_CHROME_FULLSCREEN_TOOLBAR) != 0 || 
465                               (priv->chrome & EV_CHROME_RAISE_TOOLBAR) != 0) && fullscreen;
466         findbar = (priv->chrome & EV_CHROME_FINDBAR) != 0;
467         sidebar = (priv->chrome & EV_CHROME_SIDEBAR) != 0 && !fullscreen_mode;
468
469         set_widget_visibility (priv->menubar, menubar); 
470         set_widget_visibility (priv->toolbar_dock, toolbar);
471         set_widget_visibility (priv->find_bar, findbar);
472         set_widget_visibility (priv->sidebar, sidebar);
473
474         ev_window_set_action_sensitive (window, "EditToolbar", toolbar);
475         gtk_widget_set_sensitive (priv->menubar, menubar);
476
477         if (priv->fullscreen_popup != NULL) {
478                 set_widget_visibility (priv->fullscreen_toolbar, fullscreen_toolbar);
479                 set_widget_visibility (priv->fullscreen_popup, fullscreen_toolbar);
480         }
481 }
482
483 static void
484 update_chrome_flag (EvWindow *window, EvChrome flag, gboolean active)
485 {
486         EvWindowPrivate *priv = window->priv;
487         
488         if (active) {
489                 priv->chrome |= flag;
490         } else {
491                 priv->chrome &= ~flag;
492         }
493
494         update_chrome_visibility (window);
495 }
496
497 static void
498 update_sizing_buttons (EvWindow *window)
499 {
500         GtkActionGroup *action_group = window->priv->action_group;
501         GtkAction *action;
502         gboolean best_fit, page_width;
503         EvSizingMode sizing_mode;
504
505         if (window->priv->view == NULL)
506                 return;
507
508         g_object_get (window->priv->view,
509                       "sizing_mode", &sizing_mode,
510                       NULL);
511
512         switch (sizing_mode) {
513                 case EV_SIZING_BEST_FIT:
514                         best_fit = TRUE;
515                         page_width = FALSE;
516                         break;
517                 case EV_SIZING_FIT_WIDTH:
518                         best_fit = FALSE;
519                         page_width = TRUE;
520                         break;
521                 default:
522                         best_fit = page_width = FALSE;
523                         break;
524         }
525
526         action = gtk_action_group_get_action (action_group, "ViewBestFit");
527         g_signal_handlers_block_by_func
528                 (action, G_CALLBACK (ev_window_cmd_view_best_fit), window);
529         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), best_fit);
530         g_signal_handlers_unblock_by_func
531                 (action, G_CALLBACK (ev_window_cmd_view_best_fit), window);
532
533         action = gtk_action_group_get_action (action_group, "ViewPageWidth");   
534         g_signal_handlers_block_by_func
535                 (action, G_CALLBACK (ev_window_cmd_view_page_width), window);
536         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), page_width);
537         g_signal_handlers_unblock_by_func
538                 (action, G_CALLBACK (ev_window_cmd_view_page_width), window);
539
540         action = gtk_action_group_get_action (window->priv->action_group, 
541                                               ZOOM_CONTROL_ACTION);     
542         if (best_fit) {
543                 ephy_zoom_action_set_zoom_level (EPHY_ZOOM_ACTION (action), 
544                                                  EPHY_ZOOM_BEST_FIT);
545         } else if (page_width) {
546                 ephy_zoom_action_set_zoom_level (EPHY_ZOOM_ACTION (action), 
547                                                  EPHY_ZOOM_FIT_WIDTH);
548         }
549 }
550
551 gboolean
552 ev_window_is_empty (const EvWindow *ev_window)
553 {
554         g_return_val_if_fail (EV_IS_WINDOW (ev_window), FALSE);
555
556         return (ev_window->priv->document == NULL) && 
557                 (ev_window->priv->xfer_job == NULL);
558 }
559
560 static void
561 ev_window_error_dialog_response_cb (GtkWidget *dialog,
562                                    gint       response_id,
563                                    EvWindow  *ev_window)
564 {
565         gtk_widget_destroy (dialog);
566 }
567
568 static void
569 ev_window_error_dialog (GtkWindow *window, const gchar *msg, GError *error)
570 {
571         GtkWidget *dialog;
572
573         dialog = gtk_message_dialog_new (GTK_WINDOW (window),
574                                          GTK_DIALOG_DESTROY_WITH_PARENT,
575                                          GTK_MESSAGE_ERROR,
576                                          GTK_BUTTONS_CLOSE,
577                                          msg);
578         gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
579                                                   "%s", error->message);
580         g_signal_connect (dialog, "response",
581                           G_CALLBACK (ev_window_error_dialog_response_cb),
582                            window);
583         gtk_widget_show (dialog);
584 }
585
586 static void
587 find_changed_cb (EvDocument *document, int page, EvWindow *ev_window)
588 {
589         ev_window_update_actions (ev_window);
590 }
591
592 static void
593 page_changed_cb (EvPageCache *page_cache,
594                  gint         page,
595                  EvWindow    *ev_window)
596 {
597         ev_window_update_actions (ev_window);
598
599         if (!ev_window_is_empty (ev_window))
600                 ev_metadata_manager_set_int (ev_window->priv->uri, "page", page);
601 }
602
603 static void
604 update_document_mode (EvWindow *window, EvDocumentMode mode)
605 {
606         if (mode == EV_DOCUMENT_MODE_PRESENTATION) {
607                 ev_window_run_presentation (window);
608         }
609         else if (mode == EV_DOCUMENT_MODE_FULL_SCREEN) {
610                 ev_window_run_fullscreen (window);
611         }
612 }
613
614 static void
615 setup_document_from_metadata (EvWindow *window)
616 {
617         char *uri = window->priv->uri;
618         GValue page = { 0, };
619         gint new_page;
620
621         if (uri && ev_metadata_manager_get (uri, "page", &page, TRUE)) {
622                 new_page = CLAMP (g_value_get_int (&page), 0, ev_page_cache_get_n_pages (window->priv->page_cache) - 1);
623                 ev_page_cache_set_current_page (window->priv->page_cache,
624                                                 new_page);
625                 g_value_unset (&page);
626         }
627 }
628
629 static void
630 setup_chrome_from_metadata (EvWindow *window)
631 {
632         EvChrome chrome = EV_CHROME_NORMAL;
633         GValue show_toolbar = { 0, };
634
635         if (ev_metadata_manager_get (NULL, "show_toolbar", &show_toolbar, FALSE)) {
636                 if (!g_value_get_boolean (&show_toolbar))
637                         chrome &= ~EV_CHROME_TOOLBAR;
638                 g_value_unset (&show_toolbar);
639         }
640         window->priv->chrome = chrome;
641 }
642
643 static void
644 setup_sidebar_from_metadata (EvWindow *window, EvDocument *document)
645 {
646         char *uri = window->priv->uri;
647         GtkWidget *sidebar = window->priv->sidebar;
648         GtkWidget *links = window->priv->sidebar_links;
649         GtkWidget *thumbs = window->priv->sidebar_thumbs;
650         GtkWidget *attachments = window->priv->sidebar_attachments;
651         GValue sidebar_size = { 0, };
652         GValue sidebar_page = { 0, };
653         GValue sidebar_visibility = { 0, };
654
655         if (ev_metadata_manager_get (uri, "sidebar_size", &sidebar_size, FALSE)) {
656                 gtk_paned_set_position (GTK_PANED (window->priv->hpaned),
657                                         g_value_get_int (&sidebar_size));
658                 g_value_unset(&sidebar_size);
659         }
660         
661         if (document && ev_metadata_manager_get (uri, "sidebar_page", &sidebar_page, FALSE)) {
662                 const char *page_id = g_value_get_string (&sidebar_page);
663                 
664                 if (strcmp (page_id, LINKS_SIDEBAR_ID) == 0 && ev_sidebar_page_support_document (EV_SIDEBAR_PAGE (links), document)) {
665                         ev_sidebar_set_page (EV_SIDEBAR (sidebar), links);
666                 } else if (strcmp (page_id, THUMBNAILS_SIDEBAR_ID) && ev_sidebar_page_support_document (EV_SIDEBAR_PAGE (thumbs), document)) {
667                         ev_sidebar_set_page (EV_SIDEBAR (sidebar), thumbs);
668                 } else if (strcmp (page_id, ATTACHMENTS_SIDEBAR_ID) && ev_sidebar_page_support_document (EV_SIDEBAR_PAGE (attachments), document)) {
669                         ev_sidebar_set_page (EV_SIDEBAR (sidebar), thumbs);
670                 }
671                 g_value_unset (&sidebar_page);
672         } else if (document && ev_sidebar_page_support_document (EV_SIDEBAR_PAGE (links), document)) {
673                 ev_sidebar_set_page (EV_SIDEBAR (sidebar), links);
674         }
675
676         if (ev_metadata_manager_get (uri, "sidebar_visibility", &sidebar_visibility, FALSE)) {
677                 update_chrome_flag (window, EV_CHROME_SIDEBAR, g_value_get_boolean (&sidebar_visibility));
678                 g_value_unset (&sidebar_visibility);
679         }
680 }
681
682 static void
683 setup_size_from_metadata (EvWindow *window)
684 {
685         char *uri = window->priv->uri;
686         GValue width = { 0, };
687         GValue height = { 0, };
688         GValue width_ratio = { 0, };
689         GValue height_ratio = { 0, };
690         GValue maximized = { 0, };
691         GValue x = { 0, };
692         GValue y = { 0, };
693
694         if (ev_metadata_manager_get (uri, "window_maximized", &maximized, FALSE)) {
695                 if (g_value_get_boolean (&maximized)) {
696                         gtk_window_maximize (GTK_WINDOW (window));
697                         return;
698                 } else {
699                         gtk_window_unmaximize (GTK_WINDOW (window));
700                 }
701                 g_value_unset (&maximized);
702         }
703
704         if (ev_metadata_manager_get (uri, "window_x", &x, TRUE) &&
705             ev_metadata_manager_get (uri, "window_y", &y, TRUE)) {
706                 gtk_window_move (GTK_WINDOW (window), g_value_get_int (&x),
707                                  g_value_get_int (&y));
708                 g_value_unset (&x);
709                 g_value_unset (&y);
710         }
711
712         if (ev_metadata_manager_get (uri, "window_width", &width, TRUE) &&
713             ev_metadata_manager_get (uri, "window_height", &height, TRUE)) {
714                 gtk_window_resize (GTK_WINDOW (window),
715                                    g_value_get_int (&width),
716                                    g_value_get_int (&height));
717                 g_value_unset (&width);
718                 g_value_unset (&height);
719                 return;
720         }
721
722         if (window->priv->page_cache &&
723             ev_metadata_manager_get (uri, "window_width_ratio", &width_ratio, FALSE) &&
724             ev_metadata_manager_get (uri, "window_height_ratio", &height_ratio, FALSE)) {
725                 gint document_width;
726                 gint document_height;
727
728                 ev_page_cache_get_max_width (window->priv->page_cache, 
729                                              0, 1.0,
730                                              &document_width);
731                 ev_page_cache_get_max_height (window->priv->page_cache, 
732                                              0, 1.0,
733                                              &document_height);                 
734                 
735                 gtk_window_resize (GTK_WINDOW (window),
736                                    g_value_get_double (&width_ratio) * document_width,
737                                    g_value_get_double (&height_ratio) * document_height);
738                 g_value_unset (&width_ratio);
739                 g_value_unset (&height_ratio);
740         }
741
742 }
743
744 static void
745 setup_view_from_metadata (EvWindow *window)
746 {
747         EvView *view = EV_VIEW (window->priv->view);
748         char *uri = window->priv->uri;
749         GEnumValue *enum_value;
750         GValue sizing_mode = { 0, };
751         GValue zoom = { 0, };
752         GValue continuous = { 0, };
753         GValue dual_page = { 0, };
754         GValue presentation = { 0, };
755         GValue fullscreen = { 0, };
756         GValue rotation = { 0, };
757         GValue maximized = { 0, };
758
759         /* Maximized */
760         if (ev_metadata_manager_get (uri, "window_maximized", &maximized, FALSE)) {
761                 if (g_value_get_boolean (&maximized)) {
762                         gtk_window_maximize (GTK_WINDOW (window));
763                 } else {
764                         gtk_window_unmaximize (GTK_WINDOW (window));
765                 }
766                 g_value_unset (&maximized);
767         }
768
769         /* Sizing mode */
770         if (ev_metadata_manager_get (uri, "sizing_mode", &sizing_mode, FALSE)) {
771                 enum_value = g_enum_get_value_by_nick
772                         (EV_SIZING_MODE_CLASS, g_value_get_string (&sizing_mode));
773                 g_value_unset (&sizing_mode);
774                 ev_view_set_sizing_mode (view, enum_value->value);
775         }
776
777         /* Zoom */
778         if (ev_metadata_manager_get (uri, "zoom", &zoom, FALSE) &&
779             ev_view_get_sizing_mode (view) == EV_SIZING_FREE) {
780                 ev_view_set_zoom (view, g_value_get_double (&zoom), FALSE);
781                 g_value_unset (&zoom);
782         }
783
784         /* Continuous */
785         if (ev_metadata_manager_get (uri, "continuous", &continuous, FALSE)) {
786                 ev_view_set_continuous (view, g_value_get_boolean (&continuous));
787                 g_value_unset (&continuous);
788         }
789
790         /* Dual page */
791         if (ev_metadata_manager_get (uri, "dual-page", &dual_page, FALSE)) {
792                 ev_view_set_dual_page (view, g_value_get_boolean (&dual_page));
793                 g_value_unset (&dual_page);
794         }
795
796         /* Presentation */
797         if (ev_metadata_manager_get (uri, "presentation", &presentation, FALSE)) {
798                 if (g_value_get_boolean (&presentation) && uri) {
799                         ev_window_run_presentation (window);
800                 }
801                 g_value_unset (&presentation);
802         }
803
804         /* Fullscreen */
805         if (ev_metadata_manager_get (uri, "fullscreen", &fullscreen, FALSE)) {
806                 if (g_value_get_boolean (&fullscreen) && uri) {
807                         ev_window_run_fullscreen (window);
808                 }
809                 g_value_unset (&fullscreen);
810         }
811
812         /* Rotation */
813         if (ev_metadata_manager_get (uri, "rotation", &rotation, TRUE)) {
814                 if (g_value_get_int (&rotation)) {
815                         switch (g_value_get_int (&rotation)) {
816                                 case 90:
817                                         ev_view_set_rotation (view, 90);
818                                         break;
819                                 case 180:
820                                         ev_view_set_rotation (view, 180);
821                                         break;
822                                 case 270:
823                                         ev_view_set_rotation (view, 270);
824                                         break;
825                                 default:
826                                         break;
827                         }
828                 }
829                 g_value_unset (&rotation);
830         }
831 }
832
833 static void
834 ev_window_setup_document (EvWindow *ev_window)
835 {
836         const EvDocumentInfo *info;
837         EvDocument *document;
838         EvView *view = EV_VIEW (ev_window->priv->view);
839         EvSidebar *sidebar = EV_SIDEBAR (ev_window->priv->sidebar);
840         GtkAction *action;
841
842         document = ev_window->priv->document;
843         ev_window->priv->page_cache = ev_page_cache_get (ev_window->priv->document);
844         g_signal_connect (ev_window->priv->page_cache, "page-changed", G_CALLBACK (page_changed_cb), ev_window);
845
846         if (EV_IS_DOCUMENT_FIND (document)) {
847                 g_signal_connect_object (G_OBJECT (document),
848                                          "find_changed",
849                                          G_CALLBACK (find_changed_cb),  
850                                          ev_window, 0);
851         }
852
853         ev_sidebar_set_document (sidebar, document);
854
855         if (ev_page_cache_get_n_pages (ev_window->priv->page_cache) > 0) {
856                 ev_view_set_document (view, document);
857         }
858         ev_window_set_page_mode (ev_window, PAGE_MODE_DOCUMENT);
859
860         ev_window_title_set_document (ev_window->priv->title, document);
861         ev_window_title_set_uri (ev_window->priv->title, ev_window->priv->uri);
862
863         action = gtk_action_group_get_action (ev_window->priv->action_group, PAGE_SELECTOR_ACTION);
864         ev_page_action_set_document (EV_PAGE_ACTION (action), document);
865         ev_window_setup_action_sensitivity (ev_window);
866
867         if (ev_window->priv->properties) {
868                 ev_properties_dialog_set_document (EV_PROPERTIES_DIALOG (ev_window->priv->properties),
869                                                    ev_window->priv->document);
870         }
871         
872         setup_size_from_metadata (ev_window);
873         setup_document_from_metadata (ev_window);
874         setup_sidebar_from_metadata (ev_window, document);
875
876         info = ev_page_cache_get_info (ev_window->priv->page_cache);
877         update_document_mode (ev_window, info->mode);
878 }
879
880 static void
881 password_dialog_response (GtkWidget *password_dialog,
882                           gint       response_id,
883                           EvWindow  *ev_window)
884 {
885         char *password;
886         
887         if (response_id == GTK_RESPONSE_OK) {
888
889                 password = ev_password_dialog_get_password (EV_PASSWORD_DIALOG (password_dialog));
890                 if (password) {
891                         ev_document_doc_mutex_lock ();
892                         ev_document_security_set_password (EV_DOCUMENT_SECURITY (ev_window->priv->xfer_job->document),
893                                                            password);
894                         ev_document_doc_mutex_unlock ();
895                 }
896                 g_free (password);
897
898                 ev_password_dialog_save_password (EV_PASSWORD_DIALOG (password_dialog));
899
900                 ev_window_title_set_type (ev_window->priv->title, EV_WINDOW_TITLE_DOCUMENT);
901                 ev_job_queue_add_job (ev_window->priv->xfer_job, EV_JOB_PRIORITY_HIGH);
902                 
903                 gtk_widget_destroy (password_dialog);
904                         
905                 return;
906         }
907
908         gtk_widget_set_sensitive (ev_window->priv->password_view, TRUE);
909         gtk_widget_destroy (password_dialog);
910 }
911
912 /* Called either by ev_window_xfer_job_cb or by the "unlock" callback on the
913  * password_view page.  It assumes that ev_window->priv->password_* has been set
914  * correctly.  These are cleared by password_dialog_response() */
915
916 static void
917 ev_window_popup_password_dialog (EvWindow *ev_window)
918 {
919         g_assert (ev_window->priv->xfer_job);
920
921         gtk_widget_set_sensitive (ev_window->priv->password_view, FALSE);
922
923         ev_window_title_set_uri (ev_window->priv->title, ev_window->priv->uri);
924         ev_window_title_set_type (ev_window->priv->title, EV_WINDOW_TITLE_PASSWORD);
925
926         if (ev_window->priv->password_dialog == NULL) {
927                 ev_window->priv->password_dialog =
928                         g_object_new (EV_TYPE_PASSWORD_DIALOG, "uri", ev_window->priv->uri, NULL);
929                 gtk_window_set_transient_for (GTK_WINDOW (ev_window->priv->password_dialog), GTK_WINDOW (ev_window));
930
931                 g_object_add_weak_pointer (G_OBJECT (ev_window->priv->password_dialog),
932                                            (gpointer *) &(ev_window->priv->password_dialog));
933                 g_signal_connect (ev_window->priv->password_dialog,
934                                   "response",
935                                   G_CALLBACK (password_dialog_response),
936                                   ev_window);
937                 gtk_widget_show (ev_window->priv->password_dialog);
938         } else {
939                 ev_password_dialog_set_bad_pass (EV_PASSWORD_DIALOG (ev_window->priv->password_dialog));
940         }
941 }
942
943 static void
944 ev_window_clear_xfer_job (EvWindow *ev_window)
945 {
946     if (ev_window->priv->xfer_job != NULL) {
947
948         if (!ev_window->priv->xfer_job->finished)
949                 ev_job_queue_remove_job (ev_window->priv->xfer_job);
950
951         g_signal_handlers_disconnect_by_func (ev_window->priv->xfer_job, ev_window_xfer_job_cb, ev_window);
952         g_object_unref (ev_window->priv->xfer_job);
953         ev_window->priv->xfer_job = NULL;
954     }
955 }
956
957 static void
958 ev_window_clear_local_uri (EvWindow *ev_window)
959 {
960     char *filename;
961     
962     if (ev_window->priv->local_uri) {
963             filename = g_filename_from_uri (ev_window->priv->local_uri, NULL, NULL);
964             if (filename != NULL) {
965                     unlink (filename);
966                     g_free (filename);
967             }
968             g_free (ev_window->priv->local_uri);
969             ev_window->priv->local_uri = NULL;
970     }
971 }
972
973 /* This callback will executed when load job will be finished.
974  *
975  * Since the flow of the error dialog is very confusing, we assume that both
976  * document and uri will go away after this function is called, and thus we need
977  * to ref/dup them.  Additionally, it needs to clear
978  * ev_window->priv->password_{uri,document}, and thus people who call this
979  * function should _not_ necessarily expect those to exist after being
980  * called. */
981 static void
982 ev_window_xfer_job_cb  (EvJobXfer *job,
983                         gpointer data)
984 {
985         EvWindow *ev_window = EV_WINDOW (data);
986         EvDocument *document = EV_JOB (job)->document;
987
988         g_assert (job->uri);
989         
990         ev_view_set_loading (EV_VIEW (ev_window->priv->view), FALSE);
991
992         /* Success! */
993         if (job->error == NULL) {
994
995                 if (ev_window->priv->uri)
996                         g_free (ev_window->priv->uri);
997                 ev_window->priv->uri = g_strdup (job->uri);
998
999                 if (ev_window->priv->local_uri)
1000                         g_free (ev_window->priv->local_uri);
1001                 ev_window->priv->local_uri = 
1002                     job->local_uri ? g_strdup (job->local_uri) : NULL;
1003                 
1004                 if (ev_window->priv->document)
1005                         g_object_unref (ev_window->priv->document);
1006                 ev_window->priv->document = g_object_ref (document);
1007
1008                 setup_view_from_metadata (ev_window);
1009                 ev_window_setup_document (ev_window);
1010                 ev_window_add_recent (ev_window, ev_window->priv->uri);
1011
1012                 if (job->dest)
1013                         ev_window_goto_dest (ev_window, job->dest);
1014
1015                 switch (job->mode) {
1016                         case EV_WINDOW_MODE_FULLSCREEN:
1017                                 ev_window_run_fullscreen (ev_window);
1018                                 break;
1019                         case EV_WINDOW_MODE_PRESENTATION:
1020                                 ev_window_run_presentation (ev_window);
1021                                 break;
1022                         case EV_WINDOW_MODE_PREVIEW:
1023                                 ev_window_run_preview (ev_window);
1024                                 break;
1025                         default:
1026                                 break;
1027                 }
1028
1029                 ev_window_clear_xfer_job (ev_window);           
1030                 return;
1031         }
1032
1033         if (job->error->domain == EV_DOCUMENT_ERROR &&
1034             job->error->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
1035                 gchar *base_name, *file_name;
1036
1037                 g_free (ev_window->priv->uri);
1038                 ev_window->priv->uri = g_strdup (job->uri);
1039                 setup_view_from_metadata (ev_window);
1040
1041                 file_name = gnome_vfs_format_uri_for_display (job->uri);
1042                 base_name = g_path_get_basename (file_name);
1043                 ev_password_view_set_file_name (EV_PASSWORD_VIEW (ev_window->priv->password_view),
1044                                                 base_name);
1045                 g_free (file_name);
1046                 g_free (base_name);
1047                 ev_window_set_page_mode (ev_window, PAGE_MODE_PASSWORD);
1048                 
1049                 ev_window_popup_password_dialog (ev_window);
1050         } else {
1051                 ev_window_error_dialog (GTK_WINDOW (ev_window), 
1052                                         _("Unable to open document"),
1053                                         job->error);
1054                 ev_window_clear_xfer_job (ev_window);
1055         }       
1056
1057         return;
1058 }
1059
1060 const char *
1061 ev_window_get_uri (EvWindow *ev_window)
1062 {
1063         return ev_window->priv->uri;
1064 }
1065
1066 static void
1067 ev_window_close_dialogs (EvWindow *ev_window)
1068 {
1069         if (ev_window->priv->password_dialog)
1070                 gtk_widget_destroy (ev_window->priv->password_dialog);
1071         ev_window->priv->password_dialog = NULL;
1072         
1073 #ifdef WITH_PRINT
1074         if (ev_window->priv->print_dialog)
1075                 gtk_widget_destroy (ev_window->priv->print_dialog);
1076         ev_window->priv->print_dialog = NULL;
1077 #endif
1078
1079 #ifdef WITH_GNOME_PRINT
1080         if (ev_window->priv->print_job)
1081                 g_object_unref (ev_window->priv->print_job);
1082         ev_window->priv->print_job = NULL;
1083 #endif
1084         
1085         if (ev_window->priv->properties)
1086                 gtk_widget_destroy (ev_window->priv->properties);
1087         ev_window->priv->properties = NULL;
1088 }
1089
1090 void
1091 ev_window_open_uri (EvWindow       *ev_window,
1092                     const char     *uri,
1093                     EvLinkDest     *dest,
1094                     EvWindowRunMode mode)
1095 {
1096         ev_window_close_dialogs (ev_window);
1097         ev_window_clear_xfer_job (ev_window);
1098         ev_window_clear_local_uri (ev_window);
1099         ev_view_set_loading (EV_VIEW (ev_window->priv->view), TRUE);
1100
1101         ev_window->priv->xfer_job = ev_job_xfer_new (uri, dest, mode);
1102         g_signal_connect (ev_window->priv->xfer_job,
1103                           "finished",
1104                           G_CALLBACK (ev_window_xfer_job_cb),
1105                           ev_window);
1106         ev_job_queue_add_job (ev_window->priv->xfer_job, EV_JOB_PRIORITY_HIGH);
1107 }
1108
1109 void
1110 ev_window_goto_dest (EvWindow *ev_window, EvLinkDest *dest)
1111 {
1112         ev_view_goto_dest (EV_VIEW (ev_window->priv->view), dest);
1113 }
1114
1115 static void
1116 file_open_dialog_response_cb (GtkWidget *chooser,
1117                               gint       response_id,
1118                               EvWindow  *ev_window)
1119 {
1120         if (response_id == GTK_RESPONSE_OK) {
1121                 GSList *uris;
1122
1123                 uris = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (chooser));
1124
1125                 ev_application_open_uri_list (EV_APP, uris, GDK_CURRENT_TIME);
1126         
1127                 g_slist_foreach (uris, (GFunc)g_free, NULL);    
1128                 g_slist_free (uris);
1129         }
1130         ev_application_set_chooser_uri (EV_APP, 
1131                                         gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (chooser)));
1132
1133         gtk_widget_destroy (chooser);
1134 }
1135
1136 static void
1137 ev_window_cmd_file_open (GtkAction *action, EvWindow *window)
1138 {
1139         GtkWidget *chooser;
1140
1141         chooser = gtk_file_chooser_dialog_new (_("Open Document"),
1142                                                GTK_WINDOW (window),
1143                                                GTK_FILE_CHOOSER_ACTION_OPEN,
1144                                                GTK_STOCK_CANCEL,
1145                                                GTK_RESPONSE_CANCEL,
1146                                                GTK_STOCK_OPEN, GTK_RESPONSE_OK,
1147                                                NULL);
1148
1149         ev_document_factory_add_filters (chooser, NULL);
1150         gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (chooser), TRUE);
1151         gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (chooser), FALSE);
1152         if (ev_application_get_chooser_uri (EV_APP) != NULL) {
1153                 gtk_file_chooser_set_uri (GTK_FILE_CHOOSER (chooser),
1154                                           ev_application_get_chooser_uri (EV_APP));
1155         } else if (window->priv->uri != NULL) {
1156                 gtk_file_chooser_set_uri (GTK_FILE_CHOOSER (chooser),
1157                                           window->priv->uri);
1158         }
1159         
1160         g_signal_connect (chooser, "response",
1161                           G_CALLBACK (file_open_dialog_response_cb),
1162                           window);
1163
1164         gtk_widget_show (chooser);
1165 }
1166
1167 #ifdef HAVE_GTK_RECENT
1168 static void
1169 ev_window_cmd_recent_file_activate (GtkAction     *action,
1170                                     GtkRecentInfo *info)
1171 {
1172         const gchar *uri;
1173
1174         uri = gtk_recent_info_get_uri (info);
1175         
1176         ev_application_open_uri (EV_APP, uri, NULL,
1177                                  GDK_CURRENT_TIME, NULL);
1178 }
1179 #else
1180 static void
1181 ev_window_cmd_recent_file_activate (GtkAction *action,
1182                                     EvWindow *ev_window)
1183 {
1184         char *uri;
1185         EggRecentItem *item;
1186
1187         item = egg_recent_view_uimanager_get_item (ev_window->priv->recent_view,
1188                                                    action);
1189
1190         uri = egg_recent_item_get_uri (item);
1191
1192         ev_application_open_uri (EV_APP, uri, NULL,
1193                                  GDK_CURRENT_TIME, NULL);
1194         
1195         g_free (uri);
1196 }
1197 #endif /* HAVE_GTK_RECENT */
1198
1199 static void
1200 ev_window_add_recent (EvWindow *window, const char *filename)
1201 {
1202 #ifdef HAVE_GTK_RECENT
1203         gtk_recent_manager_add_item (window->priv->recent_manager, filename);
1204 #else
1205         EggRecentItem *item;
1206
1207         item = egg_recent_item_new_from_uri (filename);
1208         egg_recent_item_add_group (item, "Evince");
1209         egg_recent_model_add_full (ev_application_get_recent_model (EV_APP), item);
1210 #endif /* HAVE_GTK_RECENT */
1211 }
1212
1213 #ifdef HAVE_GTK_RECENT
1214 static gint
1215 compare_recent_items (GtkRecentInfo *a, GtkRecentInfo *b)
1216 {
1217         gboolean     has_ev_a, has_ev_b;
1218         const gchar *evince = g_get_application_name ();
1219
1220         has_ev_a = gtk_recent_info_has_application (a, evince);
1221         has_ev_b = gtk_recent_info_has_application (b, evince);
1222         
1223         if (has_ev_a && has_ev_b) {
1224                 time_t time_a, time_b;
1225
1226                 time_a = gtk_recent_info_get_modified (a);
1227                 time_b = gtk_recent_info_get_modified (b);
1228
1229                 return (time_b - time_a);
1230         } else if (has_ev_a) {
1231                 return -1;
1232         } else if (has_ev_b) {
1233                 return 1;
1234         }
1235
1236         return 0;
1237 }
1238 #endif /* HAVE_GTK_RECENT */
1239
1240 /*
1241  * Doubles underscore to avoid spurious menu accels.
1242  */
1243 static gchar * 
1244 ev_window_get_recent_file_label (gint index, const gchar *filename)
1245 {
1246         GString *str;
1247         gint length;
1248         const gchar *p;
1249         const gchar *end;
1250  
1251         g_return_val_if_fail (filename != NULL, NULL);
1252         
1253         length = strlen (filename);
1254         str = g_string_sized_new (length + 10);
1255         g_string_printf (str, "_%d.  ", index);
1256
1257         p = filename;
1258         end = filename + length;
1259  
1260         while (p != end)
1261         {
1262                 const gchar *next;
1263                 next = g_utf8_next_char (p);
1264  
1265                 switch (*p)
1266                 {
1267                         case '_':
1268                                 g_string_append (str, "__");
1269                                 break;
1270                         default:
1271                                 g_string_append_len (str, p, next - p);
1272                                 break;
1273                 }
1274  
1275                 p = next;
1276         }
1277  
1278         return g_string_free (str, FALSE);
1279 }
1280
1281 static void
1282 ev_window_setup_recent (EvWindow *ev_window)
1283 {
1284 #ifdef HAVE_GTK_RECENT
1285         GList        *items, *l;
1286         guint         n_items = 0;
1287         const gchar  *evince = g_get_application_name ();
1288         static guint  i = 0;
1289
1290         if (ev_window->priv->recent_ui_id > 0) {
1291                 gtk_ui_manager_remove_ui (ev_window->priv->ui_manager,
1292                                           ev_window->priv->recent_ui_id);
1293                 gtk_ui_manager_ensure_update (ev_window->priv->ui_manager);
1294         }
1295         ev_window->priv->recent_ui_id = gtk_ui_manager_new_merge_id (ev_window->priv->ui_manager);
1296
1297         if (ev_window->priv->recent_action_group) {
1298                 gtk_ui_manager_remove_action_group (ev_window->priv->ui_manager,
1299                                                     ev_window->priv->recent_action_group);
1300                 g_object_unref (ev_window->priv->recent_action_group);
1301         }
1302         ev_window->priv->recent_action_group = gtk_action_group_new ("RecentFilesActions");
1303         gtk_ui_manager_insert_action_group (ev_window->priv->ui_manager,
1304                                             ev_window->priv->recent_action_group, 0);
1305
1306         items = gtk_recent_manager_get_items (ev_window->priv->recent_manager);
1307         items = g_list_sort (items, (GCompareFunc) compare_recent_items);
1308
1309         for (l = items; l && l->data; l = g_list_next (l)) {
1310                 GtkRecentInfo *info;
1311                 GtkAction     *action;
1312                 gchar         *action_name;
1313                 gchar         *label;
1314
1315                 info = (GtkRecentInfo *) l->data;
1316
1317                 if (!gtk_recent_info_has_application (info, evince))
1318                         continue;
1319
1320                 action_name = g_strdup_printf ("RecentFile%u", i++);
1321                 label = ev_window_get_recent_file_label (
1322                         n_items + 1, gtk_recent_info_get_display_name (info));
1323                 
1324                 action = g_object_new (GTK_TYPE_ACTION,
1325                                        "name", action_name,
1326                                        "label", label,
1327                                        NULL);
1328
1329                 g_object_weak_ref (G_OBJECT (action),
1330                                    (GWeakNotify) gtk_recent_info_unref,
1331                                    gtk_recent_info_ref (info));
1332                 g_signal_connect (G_OBJECT (action), "activate",
1333                                   G_CALLBACK (ev_window_cmd_recent_file_activate),
1334                                   (gpointer) info);
1335
1336                 gtk_action_group_add_action (ev_window->priv->recent_action_group,
1337                                              action);
1338                 g_object_unref (action);
1339
1340                 gtk_ui_manager_add_ui (ev_window->priv->ui_manager,
1341                                        ev_window->priv->recent_ui_id,
1342                                        "/MainMenu/FileMenu/RecentFilesMenu",
1343                                        label,
1344                                        action_name,
1345                                        GTK_UI_MANAGER_MENUITEM,
1346                                        FALSE);
1347                 g_free (action_name);
1348                 g_free (label);
1349
1350                 if (++n_items == 5)
1351                         break;
1352         }
1353         
1354         g_list_foreach (items, (GFunc) gtk_recent_info_unref, NULL);
1355         g_list_free (items);
1356 #else /* HAVE_GTK_RECENT */
1357         ev_window->priv->recent_view = egg_recent_view_uimanager_new (ev_window->priv->ui_manager,
1358                                                                       "/MainMenu/FileMenu/RecentFilesMenu",
1359                                                                       G_CALLBACK (ev_window_cmd_recent_file_activate), 
1360                                                                       ev_window);       
1361
1362         egg_recent_view_uimanager_show_icons (EGG_RECENT_VIEW_UIMANAGER (ev_window->priv->recent_view), FALSE);
1363
1364         egg_recent_view_set_model (EGG_RECENT_VIEW (ev_window->priv->recent_view),
1365                                    ev_application_get_recent_model (EV_APP));
1366
1367         egg_recent_view_uimanager_set_trailing_sep (ev_window->priv->recent_view, TRUE);
1368         
1369         g_signal_connect (ev_window->priv->recent_view, "activate",
1370                         G_CALLBACK (ev_window_cmd_recent_file_activate), ev_window);
1371 #endif /* HAVE_GTK_RECENT */
1372 }
1373
1374 static void
1375 file_save_dialog_response_cb (GtkWidget *fc,
1376                               gint       response_id,
1377                               EvWindow  *ev_window)
1378 {
1379         gboolean success;
1380
1381         if (response_id == GTK_RESPONSE_OK) {
1382                 gchar *uri;
1383                 GError *err = NULL;
1384
1385                 uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (fc));
1386
1387                 ev_document_doc_mutex_lock ();
1388                 success = ev_document_save (ev_window->priv->document, uri, &err);
1389                 ev_document_doc_mutex_unlock ();
1390
1391                 if (err) {
1392                         gchar *msg;
1393                         msg = g_strdup_printf (_("The file could not be saved as “%s”."), uri);
1394                         ev_window_error_dialog (GTK_WINDOW (fc), msg, err);
1395                         g_free (msg);
1396                 }
1397
1398                 g_free (uri);
1399         }
1400
1401         gtk_widget_destroy (fc);
1402 }
1403
1404 static void
1405 ev_window_cmd_save_as (GtkAction *action, EvWindow *ev_window)
1406 {
1407         GtkWidget *fc;
1408         gchar *base_name;
1409         gchar *file_name;
1410
1411         fc = gtk_file_chooser_dialog_new (
1412                 _("Save a Copy"),
1413                 GTK_WINDOW (ev_window), GTK_FILE_CHOOSER_ACTION_SAVE,
1414                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1415                 GTK_STOCK_SAVE, GTK_RESPONSE_OK,
1416                 NULL);
1417
1418         ev_document_factory_add_filters (fc, ev_window->priv->document);
1419         gtk_dialog_set_default_response (GTK_DIALOG (fc), GTK_RESPONSE_OK);
1420
1421         gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER (fc), TRUE);    
1422         file_name = gnome_vfs_format_uri_for_display (ev_window->priv->uri);
1423         base_name = g_path_get_basename (file_name);
1424         gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (fc), base_name);
1425         g_free (file_name);
1426         g_free (base_name);
1427
1428         g_signal_connect (fc, "response",
1429                           G_CALLBACK (file_save_dialog_response_cb),
1430                           ev_window);
1431
1432         gtk_widget_show (fc);
1433 }
1434
1435 #ifdef WITH_GTK_PRINT
1436 static void
1437 ev_window_clear_print_job (EvWindow *window)
1438 {
1439         if (window->priv->print_job) {
1440                 if (!window->priv->print_job->finished)
1441                         ev_job_queue_remove_job (window->priv->print_job);
1442
1443                 g_signal_handlers_disconnect_by_func (window->priv->print_job,
1444                                                       ev_window_print_job_cb,
1445                                                       window);
1446                 g_object_unref (window->priv->print_job);
1447                 window->priv->print_job = NULL;
1448         }
1449 }
1450
1451 static void
1452 ev_window_print_finished (GtkPrintJob *print_job,
1453                           EvWindow    *window,
1454                           GError      *error)
1455 {
1456         ev_window_clear_print_job (window);
1457         
1458         if (error) {
1459                 GtkWidget *dialog;
1460                 
1461                 dialog = gtk_message_dialog_new (GTK_WINDOW (window),
1462                                                  GTK_DIALOG_MODAL,
1463                                                  GTK_MESSAGE_ERROR,
1464                                                  GTK_BUTTONS_OK,
1465                                                  _("Failed to print document"));
1466                 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1467                                                           error->message);
1468
1469                 gtk_dialog_run (GTK_DIALOG (dialog));
1470                 gtk_widget_destroy (dialog);
1471         }
1472 }
1473
1474 static void
1475 ev_window_print_send (EvWindow    *window,
1476                       const gchar *filename)
1477 {
1478         GtkPrintJob *job;
1479         GError      *error = NULL;
1480         
1481         if (window->priv->gtk_print_job)
1482                 g_object_unref (window->priv->gtk_print_job);
1483         
1484         job = gtk_print_job_new ("evince-print",
1485                                  window->priv->printer,
1486                                  window->priv->print_settings,
1487                                  window->priv->print_page_setup);
1488         
1489         window->priv->gtk_print_job = job;
1490
1491         if (gtk_print_job_set_source_file (job, filename, &error)) {
1492                 gtk_print_job_send (job,
1493                                     (GtkPrintJobCompleteFunc)ev_window_print_finished,
1494                                     window, NULL);
1495         } else {
1496                 ev_window_clear_print_job (window);
1497                 g_warning (error->message);
1498                 g_error_free (error);
1499         }
1500 }
1501
1502 static void
1503 ev_window_print_job_cb (EvJobPrint *job,
1504                         EvWindow   *window)
1505 {
1506         if (job->error) {
1507                 g_warning (job->error->message);
1508                 ev_window_clear_print_job (window);
1509                 return;
1510         }
1511
1512         g_assert (job->temp_file != NULL);
1513
1514         ev_window_print_send (window, job->temp_file);
1515 }
1516
1517 static gboolean
1518 ev_window_print_dialog_response_cb (GtkDialog *dialog,
1519                                     gint       response,
1520                                     EvWindow  *window)
1521 {
1522         EvPrintRange *ranges = NULL;
1523         EvPageCache  *page_cache;
1524         gint          n_ranges = 0;
1525         gint          copies;
1526         gboolean      collate;
1527         gboolean      reverse;
1528         gdouble       scale;
1529         gint          current_page;
1530         gint          width;
1531         gint          height;
1532         GtkPrintPages print_pages;
1533         
1534         if (response != GTK_RESPONSE_OK) {
1535                 gtk_widget_destroy (GTK_WIDGET (dialog));
1536                 window->priv->print_dialog = NULL;
1537
1538                 return FALSE;
1539         }
1540
1541         if (window->priv->printer)
1542                 g_object_unref (window->priv->printer);
1543         if (window->priv->print_settings)
1544                 g_object_unref (window->priv->print_settings);
1545         if (window->priv->print_page_setup)
1546                 g_object_unref (window->priv->print_page_setup);
1547         
1548         window->priv->printer = g_object_ref (
1549                 gtk_print_unix_dialog_get_selected_printer (GTK_PRINT_UNIX_DIALOG (dialog)));
1550         window->priv->print_settings = g_object_ref (
1551                 gtk_print_unix_dialog_get_settings (GTK_PRINT_UNIX_DIALOG (dialog)));
1552         window->priv->print_page_setup = g_object_ref (
1553                 gtk_print_unix_dialog_get_page_setup (GTK_PRINT_UNIX_DIALOG (dialog)));
1554
1555         if (!gtk_printer_accepts_ps (window->priv->printer)) {
1556                 GtkWidget *msgdialog;
1557
1558                 msgdialog = gtk_message_dialog_new (GTK_WINDOW (dialog),
1559                                                     GTK_DIALOG_MODAL,
1560                                                     GTK_MESSAGE_ERROR,
1561                                                     GTK_BUTTONS_OK,
1562                                                     _("Printing is not supported on this printer."));
1563                 
1564                 gtk_dialog_run (GTK_DIALOG (msgdialog));
1565                 gtk_widget_destroy (msgdialog);
1566
1567                 return FALSE;
1568         }
1569
1570         ev_window_clear_print_job (window);
1571         
1572         current_page = gtk_print_unix_dialog_get_current_page (GTK_PRINT_UNIX_DIALOG (dialog));
1573         print_pages = gtk_print_settings_get_print_pages (window->priv->print_settings);
1574         
1575         switch (print_pages) {
1576         case GTK_PRINT_PAGES_CURRENT:
1577                 ranges = g_new0 (EvPrintRange, 1);
1578                 
1579                 ranges->start = current_page;
1580                 ranges->end = current_page;
1581                 n_ranges = 1;
1582                                 
1583                 break;
1584         case GTK_PRINT_PAGES_RANGES: {
1585                 GtkPageRange *page_range;
1586                 
1587                 page_range = gtk_print_settings_get_page_ranges (window->priv->print_settings,
1588                                                                  &n_ranges);
1589                 if (n_ranges > 0)
1590                         ranges = g_memdup (page_range, n_ranges * sizeof (GtkPageRange));
1591         }
1592                 break;
1593         default:
1594                 break;
1595         }
1596
1597         scale = gtk_print_settings_get_scale (window->priv->print_settings) * 0.01;
1598         page_cache = ev_page_cache_get (window->priv->document);
1599         ev_page_cache_get_size (page_cache,
1600                                 current_page,
1601                                 0, scale,
1602                                 &width, &height);
1603         
1604         copies = gtk_print_settings_get_n_copies (window->priv->print_settings);
1605         collate = gtk_print_settings_get_collate (window->priv->print_settings);
1606         reverse = gtk_print_settings_get_reverse (window->priv->print_settings);
1607         
1608         window->priv->print_job = ev_job_print_new (window->priv->document,
1609                                                     (gdouble)width,
1610                                                     (gdouble)height,
1611                                                     ranges, n_ranges,
1612                                                     copies, collate, reverse);
1613         
1614         g_signal_connect (window->priv->print_job, "finished",
1615                           G_CALLBACK (ev_window_print_job_cb),
1616                           window);
1617         /* The priority doesn't matter for this job */
1618         ev_job_queue_add_job (window->priv->print_job, EV_JOB_PRIORITY_LOW);
1619         
1620         gtk_widget_destroy (GTK_WIDGET (dialog));
1621         window->priv->print_dialog = NULL;
1622
1623         return TRUE;
1624 }
1625
1626 void
1627 ev_window_print_range (EvWindow *ev_window, int first_page, int last_page)
1628 {
1629         GtkWidget   *dialog;
1630         EvPageCache *page_cache;
1631         gint         current_page;
1632         gint         document_last_page;
1633
1634         g_return_if_fail (EV_IS_WINDOW (ev_window));
1635         g_return_if_fail (ev_window->priv->document != NULL);
1636
1637         if (ev_window->priv->print_dialog) {
1638                 gtk_window_present (GTK_WINDOW (ev_window->priv->print_dialog));
1639                 return;
1640         }
1641         
1642         page_cache = ev_page_cache_get (ev_window->priv->document);
1643         current_page = ev_page_cache_get_current_page (page_cache);
1644         document_last_page = ev_page_cache_get_n_pages (page_cache);
1645
1646         if (!ev_window->priv->print_settings)
1647                 ev_window->priv->print_settings = gtk_print_settings_new ();
1648
1649         if (first_page != 1 || last_page != document_last_page) {
1650                 GtkPageRange range;
1651
1652                 /* Ranges in GtkPrint are 0 - N */
1653                 range.start = first_page - 1;
1654                 range.end = last_page - 1;
1655                 
1656                 gtk_print_settings_set_print_pages (ev_window->priv->print_settings,
1657                                                     GTK_PRINT_PAGES_RANGES);
1658                 gtk_print_settings_set_page_ranges (ev_window->priv->print_settings,
1659                                                     &range, 1);
1660         }
1661
1662         dialog = gtk_print_unix_dialog_new (_("Print"), GTK_WINDOW (ev_window));
1663         ev_window->priv->print_dialog = dialog;
1664         gtk_print_unix_dialog_set_manual_capabilities (GTK_PRINT_UNIX_DIALOG (dialog),
1665                                                        GTK_PRINT_CAPABILITY_PAGE_SET |
1666                                                        GTK_PRINT_CAPABILITY_COPIES |
1667                                                        GTK_PRINT_CAPABILITY_COLLATE |
1668                                                        GTK_PRINT_CAPABILITY_REVERSE |
1669                                                        GTK_PRINT_CAPABILITY_SCALE |
1670                                                        GTK_PRINT_CAPABILITY_GENERATE_PS);
1671         gtk_print_unix_dialog_set_current_page (GTK_PRINT_UNIX_DIALOG (dialog),
1672                                                 current_page);
1673         
1674         gtk_print_unix_dialog_set_settings (GTK_PRINT_UNIX_DIALOG (dialog),
1675                                             ev_window->priv->print_settings);
1676         
1677         if (ev_window->priv->print_page_setup)
1678                 gtk_print_unix_dialog_set_page_setup (GTK_PRINT_UNIX_DIALOG (dialog),
1679                                                       ev_window->priv->print_page_setup);
1680         
1681         g_signal_connect (G_OBJECT (dialog), "response",
1682                           G_CALLBACK (ev_window_print_dialog_response_cb),
1683                           ev_window);
1684
1685         gtk_widget_show (dialog);
1686 }
1687 #endif /* WITH_GTK_PRINT */
1688
1689 #ifdef WITH_GNOME_PRINT
1690 static gboolean
1691 ev_window_print_dialog_response_cb (GtkDialog *print_dialog,
1692                                     gint       response,
1693                                     EvWindow  *ev_window)
1694 {
1695         EvPrintJob *print_job;
1696         GnomePrintConfig *config;
1697     
1698         if (response != GNOME_PRINT_DIALOG_RESPONSE_PRINT) {
1699                 gtk_widget_destroy (GTK_WIDGET (print_dialog));
1700                 ev_window->priv->print_dialog = NULL;
1701                 g_object_unref (ev_window->priv->print_job);
1702                 ev_window->priv->print_job = NULL;
1703                 
1704                 return FALSE;
1705         }
1706
1707         config = gnome_print_dialog_get_config (GNOME_PRINT_DIALOG (print_dialog));
1708
1709         /* FIXME: Change this when we have the first backend
1710          * that can print more than postscript
1711          */
1712         if (using_pdf_printer (config)) {
1713                 GtkWidget *dialog;
1714                 
1715                 dialog = gtk_message_dialog_new (GTK_WINDOW (print_dialog), GTK_DIALOG_MODAL,
1716                                                  GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
1717                                                  _("Generating PDF is not supported"));
1718                 gtk_dialog_run (GTK_DIALOG (dialog));
1719                 gtk_widget_destroy (dialog);
1720                 
1721                 return FALSE;
1722         } else if (!using_postscript_printer (config)) {
1723                 GtkWidget *dialog;
1724
1725                 dialog = gtk_message_dialog_new (GTK_WINDOW (print_dialog), GTK_DIALOG_MODAL,
1726                                                  GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
1727                                                  _("Printing is not supported on this printer."));
1728                 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1729                                                           _("You were trying to print to a printer using the “%s” driver. "
1730                                                             "This program requires a PostScript printer driver."),
1731                                                           gnome_print_config_get (config, (guchar *)"Settings.Engine.Backend.Driver"));
1732                 gtk_dialog_run (GTK_DIALOG (dialog));
1733                 gtk_widget_destroy (dialog);
1734                 
1735                 return FALSE;
1736         }
1737
1738         save_print_config_to_file (config);
1739     
1740         print_job = g_object_new (EV_TYPE_PRINT_JOB,
1741                                   "gnome_print_job", ev_window->priv->print_job,
1742                                   "document", ev_window->priv->document,
1743                                   "print_dialog", print_dialog,
1744                                   NULL);
1745
1746         if (print_job != NULL) {
1747                 ev_print_job_print (print_job, GTK_WINDOW (ev_window));
1748                 g_object_unref (print_job);
1749         }
1750
1751         g_object_unref (config);
1752
1753         gtk_widget_destroy (GTK_WIDGET (print_dialog));
1754         ev_window->priv->print_dialog = NULL;
1755         g_object_unref (ev_window->priv->print_job);
1756         ev_window->priv->print_job = NULL;
1757
1758         return FALSE;
1759 }
1760
1761 void
1762 ev_window_print_range (EvWindow *ev_window, int first_page, int last_page)
1763 {
1764         GnomePrintConfig *config;
1765         gchar *pages_label;
1766
1767         g_return_if_fail (EV_IS_WINDOW (ev_window));
1768         g_return_if_fail (ev_window->priv->document != NULL);
1769
1770         config = load_print_config_from_file ();
1771
1772         if (ev_window->priv->print_job == NULL)
1773                 ev_window->priv->print_job = gnome_print_job_new (config);
1774         
1775         if (ev_window->priv->print_dialog == NULL) {
1776                 ev_window->priv->print_dialog =
1777                         gnome_print_dialog_new (ev_window->priv->print_job,
1778                                                 (guchar *) _("Print"),
1779                                                 (GNOME_PRINT_DIALOG_RANGE |
1780                                                  GNOME_PRINT_DIALOG_COPIES));
1781         }
1782         
1783         gtk_window_set_transient_for (GTK_WINDOW (ev_window->priv->print_dialog),
1784                                       GTK_WINDOW (ev_window));                                                          
1785         g_object_unref (config);                                                                
1786
1787         pages_label = g_strconcat (_("Pages"), " ", NULL);
1788         gnome_print_dialog_construct_range_page (GNOME_PRINT_DIALOG (ev_window->priv->print_dialog),
1789                                                  GNOME_PRINT_RANGE_ALL |
1790                                                  GNOME_PRINT_RANGE_RANGE,
1791                                                  first_page, last_page,
1792                                                  NULL, (const guchar *)pages_label);
1793         g_free (pages_label);
1794                                                  
1795         gtk_dialog_set_response_sensitive (GTK_DIALOG (ev_window->priv->print_dialog),
1796                                            GNOME_PRINT_DIALOG_RESPONSE_PREVIEW,
1797                                            FALSE);
1798
1799         g_signal_connect (G_OBJECT (ev_window->priv->print_dialog), "response",
1800                           G_CALLBACK (ev_window_print_dialog_response_cb),
1801                           ev_window);
1802         gtk_widget_show (ev_window->priv->print_dialog);
1803 }
1804 #endif /* WITH_GNOME_PRINT */
1805
1806 static void
1807 ev_window_print (EvWindow *window)
1808 {
1809         EvPageCache *page_cache;
1810         gint         last_page;
1811
1812         page_cache = ev_page_cache_get (window->priv->document);
1813         last_page = ev_page_cache_get_n_pages (page_cache);
1814
1815 #ifdef WITH_PRINT
1816         ev_window_print_range (window, 1, last_page);
1817 #endif
1818 }
1819
1820 static void
1821 ev_window_cmd_file_print (GtkAction *action, EvWindow *ev_window)
1822 {
1823         ev_window_print (ev_window);
1824 }
1825
1826 static void
1827 ev_window_cmd_file_properties (GtkAction *action, EvWindow *ev_window)
1828 {
1829         if (ev_window->priv->properties == NULL) {
1830                 ev_window->priv->properties = ev_properties_dialog_new ();
1831                 ev_properties_dialog_set_document (EV_PROPERTIES_DIALOG (ev_window->priv->properties),
1832                                                    ev_window->priv->document);
1833                 g_object_add_weak_pointer (G_OBJECT (ev_window->priv->properties),
1834                                            (gpointer *) &(ev_window->priv->properties));
1835                 gtk_window_set_transient_for (GTK_WINDOW (ev_window->priv->properties),
1836                                               GTK_WINDOW (ev_window));
1837         }
1838
1839         gtk_widget_show (ev_window->priv->properties);
1840 }
1841                                         
1842 static void
1843 ev_window_cmd_file_close_window (GtkAction *action, EvWindow *ev_window)
1844 {
1845         g_return_if_fail (EV_IS_WINDOW (ev_window));
1846
1847         gtk_widget_destroy (GTK_WIDGET (ev_window));
1848 }
1849
1850 static void
1851 ev_window_cmd_focus_page_selector (GtkAction *act, EvWindow *window)
1852 {
1853         GtkAction *action;
1854         
1855         update_chrome_flag (window, EV_CHROME_RAISE_TOOLBAR, TRUE);
1856         ev_window_set_action_sensitive (window, "ViewToolbar", FALSE);
1857         
1858         action = gtk_action_group_get_action (window->priv->action_group,
1859                                               PAGE_SELECTOR_ACTION);
1860         ev_page_action_grab_focus (EV_PAGE_ACTION (action));
1861 }
1862
1863 static void
1864 ev_window_cmd_scroll_forward (GtkAction *action, EvWindow *window)
1865 {
1866         ev_view_scroll (EV_VIEW (window->priv->view), EV_SCROLL_PAGE_FORWARD, FALSE);
1867 }
1868
1869 static void
1870 ev_window_cmd_scroll_backward (GtkAction *action, EvWindow *window)
1871 {
1872         ev_view_scroll (EV_VIEW (window->priv->view), EV_SCROLL_PAGE_BACKWARD, FALSE);
1873 }
1874
1875 static void
1876 ev_window_cmd_continuous (GtkAction *action, EvWindow *ev_window)
1877 {
1878         gboolean continuous;
1879
1880         ev_window_stop_presentation (ev_window);
1881         continuous = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
1882         g_object_set (G_OBJECT (ev_window->priv->view),
1883                       "continuous", continuous,
1884                       NULL);
1885         ev_window_update_actions (ev_window);
1886 }
1887
1888 static void
1889 ev_window_cmd_dual (GtkAction *action, EvWindow *ev_window)
1890 {
1891         gboolean dual_page;
1892
1893         ev_window_stop_presentation (ev_window);
1894         dual_page = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
1895         g_object_set (G_OBJECT (ev_window->priv->view),
1896                       "dual-page", dual_page,
1897                       NULL);
1898         ev_window_update_actions (ev_window);
1899 }
1900
1901 static void
1902 ev_window_cmd_view_best_fit (GtkAction *action, EvWindow *ev_window)
1903 {
1904         ev_window_stop_presentation (ev_window);
1905
1906         if (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action))) {
1907                 ev_view_set_sizing_mode (EV_VIEW (ev_window->priv->view), EV_SIZING_BEST_FIT);
1908         } else {
1909                 ev_view_set_sizing_mode (EV_VIEW (ev_window->priv->view), EV_SIZING_FREE);
1910         }
1911         ev_window_update_actions (ev_window);
1912 }
1913
1914 static void
1915 ev_window_cmd_view_page_width (GtkAction *action, EvWindow *ev_window)
1916 {
1917         ev_window_stop_presentation (ev_window);
1918
1919         if (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action))) {
1920                 ev_view_set_sizing_mode (EV_VIEW (ev_window->priv->view), EV_SIZING_FIT_WIDTH);
1921         } else {
1922                 ev_view_set_sizing_mode (EV_VIEW (ev_window->priv->view), EV_SIZING_FREE);
1923         }
1924         ev_window_update_actions (ev_window);
1925 }
1926
1927
1928 static void
1929 ev_window_cmd_edit_select_all (GtkAction *action, EvWindow *ev_window)
1930 {
1931         g_return_if_fail (EV_IS_WINDOW (ev_window));
1932
1933         ev_view_select_all (EV_VIEW (ev_window->priv->view));
1934 }
1935
1936 static void
1937 ev_window_cmd_edit_find (GtkAction *action, EvWindow *ev_window)
1938 {
1939         g_return_if_fail (EV_IS_WINDOW (ev_window));
1940
1941         if (ev_window->priv->document == NULL || !EV_IS_DOCUMENT_FIND (ev_window->priv->document)) {
1942                 g_error ("Find action should be insensitive since document doesn't support find");
1943                 return;
1944         } 
1945
1946         update_chrome_flag (ev_window, EV_CHROME_FINDBAR, TRUE);
1947         gtk_widget_grab_focus (ev_window->priv->find_bar);
1948 }
1949
1950 static void
1951 ev_window_cmd_edit_find_next (GtkAction *action, EvWindow *ev_window)
1952 {
1953         g_return_if_fail (EV_IS_WINDOW (ev_window));
1954
1955         ev_view_find_next (EV_VIEW (ev_window->priv->view));
1956 }
1957
1958 static void
1959 ev_window_cmd_edit_find_previous (GtkAction *action, EvWindow *ev_window)
1960 {
1961         g_return_if_fail (EV_IS_WINDOW (ev_window));
1962
1963         ev_view_find_previous (EV_VIEW (ev_window->priv->view));
1964 }
1965
1966 static void
1967 ev_window_cmd_edit_copy (GtkAction *action, EvWindow *ev_window)
1968 {
1969         g_return_if_fail (EV_IS_WINDOW (ev_window));
1970
1971         ev_view_copy (EV_VIEW (ev_window->priv->view));
1972 }
1973
1974 static void
1975 ev_window_update_fullscreen_popup (EvWindow *window)
1976 {
1977         GtkWidget *popup = window->priv->fullscreen_popup;
1978         int popup_width, popup_height;
1979         GdkScreen *screen;
1980         GdkRectangle screen_rect;
1981         gboolean toolbar;
1982
1983         g_return_if_fail (popup != NULL);
1984
1985         if (GTK_WIDGET (window)->window == NULL)
1986                 return;
1987
1988         toolbar = (window->priv->chrome & EV_CHROME_FULLSCREEN_TOOLBAR) != 0 || 
1989                   (window->priv->chrome & EV_CHROME_RAISE_TOOLBAR) != 0;
1990         popup_width = popup->requisition.width;
1991         popup_height = popup->requisition.height;
1992
1993         screen = gtk_widget_get_screen (GTK_WIDGET (window));
1994         gdk_screen_get_monitor_geometry (screen,
1995                         gdk_screen_get_monitor_at_window
1996                         (screen,
1997                          GTK_WIDGET (window)->window),
1998                          &screen_rect);
1999         if (toolbar) {
2000                 gtk_widget_set_size_request (popup,
2001                                              screen_rect.width,
2002                                              -1);
2003                 gtk_window_move (GTK_WINDOW (popup),
2004                                  screen_rect.x,
2005                                  screen_rect.y);
2006
2007         } else {
2008                 if (gtk_widget_get_direction (popup) == GTK_TEXT_DIR_RTL)
2009                 {
2010                         gtk_window_move (GTK_WINDOW (popup),
2011                                          screen_rect.x,
2012                                          screen_rect.y);
2013                 } else {
2014                         gtk_window_move (GTK_WINDOW (popup),
2015                                          screen_rect.x + screen_rect.width - popup_width,
2016                                          screen_rect.y);
2017                 }
2018         }
2019 }
2020
2021 static void
2022 screen_size_changed_cb (GdkScreen *screen,
2023                         EvWindow *window)
2024 {
2025         ev_window_update_fullscreen_popup (window);
2026 }
2027
2028 static void
2029 ev_window_sidebar_position_change_cb (GObject *object, GParamSpec *pspec,
2030                                       EvWindow *ev_window)
2031 {
2032         if (!ev_window_is_empty (ev_window))
2033                 ev_metadata_manager_set_int (ev_window->priv->uri, "sidebar_size",
2034                                              gtk_paned_get_position (GTK_PANED (object)));
2035 }
2036
2037 static void
2038 destroy_fullscreen_popup (EvWindow *window)
2039 {
2040         if (window->priv->fullscreen_popup != NULL)
2041         {
2042                 gtk_widget_destroy (window->priv->fullscreen_popup);
2043                 window->priv->fullscreen_popup = NULL;
2044         }
2045 }
2046
2047 static void
2048 exit_fullscreen_button_clicked_cb (GtkWidget *button, EvWindow *window)
2049 {
2050         GtkAction *action;
2051
2052         action = gtk_action_group_get_action (window->priv->action_group, "ViewFullscreen");
2053         g_return_if_fail (action != NULL);
2054
2055         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), FALSE);
2056 }
2057
2058 static void
2059 fullscreen_popup_size_request_cb (GtkWidget *popup, GtkRequisition *req, EvWindow *window)
2060 {
2061         ev_window_update_fullscreen_popup (window);
2062 }
2063
2064 static gboolean
2065 fullscreen_timeout_cb (EvWindow *window)
2066 {
2067         EvView *view = EV_VIEW (window->priv->view);
2068
2069         if (!view || !ev_view_get_fullscreen (EV_VIEW (view)))
2070                 return FALSE;
2071         
2072         update_chrome_flag (window, EV_CHROME_FULLSCREEN_TOOLBAR, FALSE);
2073         ev_view_hide_cursor (EV_VIEW (window->priv->view));
2074         window->priv->fullscreen_timeout_id = 0;
2075
2076         return FALSE;
2077 }
2078
2079 static void
2080 fullscreen_set_timeout (EvWindow *window)
2081 {
2082         if (window->priv->fullscreen_timeout_id != 0) {
2083                 g_source_remove (window->priv->fullscreen_timeout_id);
2084         }
2085         
2086         window->priv->fullscreen_timeout_id = 
2087             g_timeout_add (FULLSCREEN_TIMEOUT, (GSourceFunc)fullscreen_timeout_cb, window);
2088
2089         update_chrome_flag (window, EV_CHROME_FULLSCREEN_TOOLBAR, TRUE);
2090         update_chrome_visibility (window);
2091         ev_view_show_cursor (EV_VIEW (window->priv->view));
2092 }
2093
2094 static void
2095 fullscreen_clear_timeout (EvWindow *window)
2096 {
2097         if (window->priv->fullscreen_timeout_id != 0) {
2098                 g_source_remove (window->priv->fullscreen_timeout_id);
2099         }
2100         
2101         window->priv->fullscreen_timeout_id = 0;
2102         update_chrome_visibility (window);
2103         ev_view_show_cursor (EV_VIEW (window->priv->view));
2104 }
2105
2106
2107 static gboolean
2108 fullscreen_motion_notify_cb (GtkWidget *widget,
2109                              GdkEventMotion *event,
2110                              gpointer user_data)
2111 {
2112         EvWindow *window = EV_WINDOW (user_data);
2113
2114         fullscreen_set_timeout (window);
2115
2116         return FALSE;
2117 }
2118
2119 static gboolean
2120 fullscreen_leave_notify_cb (GtkWidget *widget,
2121                             GdkEventCrossing *event,
2122                             gpointer user_data)
2123 {
2124         EvWindow *window = EV_WINDOW (user_data);
2125
2126         fullscreen_clear_timeout (window);
2127
2128         return FALSE;
2129 }
2130
2131 static GtkWidget *
2132 ev_window_get_exit_fullscreen_button (EvWindow *window)
2133 {
2134         GtkWidget *button, *icon, *label, *hbox;
2135
2136         button = gtk_button_new ();
2137         g_signal_connect (button, "clicked",
2138                           G_CALLBACK (exit_fullscreen_button_clicked_cb),
2139                           window);
2140         gtk_widget_show (button);
2141
2142         hbox = gtk_hbox_new (FALSE, 2);
2143         gtk_widget_show (hbox);
2144         gtk_container_add (GTK_CONTAINER (button), hbox);
2145
2146         icon = gtk_image_new_from_stock (EV_STOCK_LEAVE_FULLSCREEN, GTK_ICON_SIZE_BUTTON);
2147         gtk_widget_show (icon);
2148         gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);
2149
2150         label = gtk_label_new (_("Leave Fullscreen"));
2151         gtk_widget_show (label);
2152         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
2153
2154         return button;
2155 }
2156
2157 static GtkWidget *
2158 ev_window_create_fullscreen_popup (EvWindow *window)
2159 {
2160         GtkWidget *popup;
2161         GtkWidget *hbox;
2162         GtkWidget *button;
2163         GdkScreen *screen;
2164
2165         window->priv->fullscreen_toolbar = egg_editable_toolbar_new_with_model
2166                         (window->priv->ui_manager, ev_application_get_toolbars_model (EV_APP), NULL);
2167
2168         popup = gtk_window_new (GTK_WINDOW_POPUP);
2169         hbox = gtk_hbox_new (FALSE, 0);
2170         button = ev_window_get_exit_fullscreen_button (window);
2171
2172         gtk_container_add (GTK_CONTAINER (popup), hbox);
2173         gtk_box_pack_start (GTK_BOX (hbox), window->priv->fullscreen_toolbar,
2174                             TRUE, TRUE, 0);
2175         gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
2176
2177         gtk_widget_show (button);
2178         gtk_widget_show (hbox);
2179
2180         gtk_window_set_resizable (GTK_WINDOW (popup), FALSE);
2181
2182         screen = gtk_widget_get_screen (GTK_WIDGET (window));
2183         g_signal_connect_object (screen, "size-changed",
2184                                  G_CALLBACK (screen_size_changed_cb),
2185                                  window, 0);
2186         g_signal_connect_object (popup, "size_request",
2187                                  G_CALLBACK (fullscreen_popup_size_request_cb),
2188                                  window, 0);
2189
2190         gtk_window_set_screen (GTK_WINDOW (popup),
2191                                gtk_widget_get_screen (GTK_WIDGET (window)));
2192
2193         return popup;
2194 }
2195
2196
2197 static void
2198 ev_window_update_fullscreen_action (EvWindow *window)
2199 {
2200         GtkAction *action;
2201
2202         action = gtk_action_group_get_action (window->priv->action_group, "ViewFullscreen");
2203         g_signal_handlers_block_by_func
2204                 (action, G_CALLBACK (ev_window_cmd_view_fullscreen), window);
2205         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
2206                                       ev_view_get_fullscreen (EV_VIEW (window->priv->view)));
2207         g_signal_handlers_unblock_by_func
2208                 (action, G_CALLBACK (ev_window_cmd_view_fullscreen), window);
2209 }
2210
2211 static void
2212 ev_window_run_fullscreen (EvWindow *window)
2213 {
2214         if (window->priv->fullscreen_popup == NULL)
2215                 window->priv->fullscreen_popup
2216                         = ev_window_create_fullscreen_popup (window);
2217
2218         g_object_set (G_OBJECT (window->priv->scrolled_window),
2219                       "shadow-type", GTK_SHADOW_NONE,
2220                       NULL);
2221
2222         ev_view_set_fullscreen (EV_VIEW (window->priv->view), TRUE);
2223         ev_window_update_fullscreen_action (window);
2224         
2225         gtk_window_fullscreen (GTK_WINDOW (window));
2226         gtk_widget_grab_focus (window->priv->view);
2227         ev_window_update_fullscreen_popup (window);
2228
2229         g_signal_connect (window->priv->view,
2230                           "motion-notify-event",
2231                           G_CALLBACK (fullscreen_motion_notify_cb),
2232                           window);
2233         g_signal_connect (window->priv->view,
2234                           "leave-notify-event",
2235                           G_CALLBACK (fullscreen_leave_notify_cb),
2236                           window);
2237         fullscreen_set_timeout (window);
2238
2239         if (!ev_window_is_empty (window))
2240                 ev_metadata_manager_set_boolean (window->priv->uri, "fullscreen", TRUE);
2241 }
2242
2243 static void
2244 ev_window_stop_fullscreen (EvWindow *window)
2245 {
2246         EvView *view = EV_VIEW (window->priv->view);
2247
2248         if (!ev_view_get_fullscreen (EV_VIEW (view)))
2249                 return;
2250
2251         g_object_set (G_OBJECT (window->priv->scrolled_window),
2252                       "shadow-type", GTK_SHADOW_IN,
2253                       NULL);
2254
2255         ev_view_set_fullscreen (view, FALSE);
2256         ev_window_update_fullscreen_action (window);
2257         gtk_window_unfullscreen (GTK_WINDOW (window));
2258
2259         g_signal_handlers_disconnect_by_func (window->priv->view,
2260                                               (gpointer) fullscreen_motion_notify_cb,
2261                                               window);
2262         g_signal_handlers_disconnect_by_func (window->priv->view,
2263                                               (gpointer) fullscreen_leave_notify_cb,
2264                                               window);
2265         fullscreen_clear_timeout (window);
2266
2267         if (!ev_window_is_empty (window))
2268                 ev_metadata_manager_set_boolean (window->priv->uri, "fullscreen", FALSE);
2269 }
2270
2271 static void
2272 ev_window_cmd_view_fullscreen (GtkAction *action, EvWindow *window)
2273 {
2274         gboolean fullscreen;
2275
2276         g_return_if_fail (EV_IS_WINDOW (window));
2277         ev_window_stop_presentation (window);
2278
2279         fullscreen = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
2280         if (fullscreen) {
2281                 ev_window_run_fullscreen (window);
2282         } else {
2283                 ev_window_stop_fullscreen (window);
2284         }
2285 }
2286
2287 static void
2288 ev_window_update_presentation_action (EvWindow *window)
2289 {
2290         GtkAction *action;
2291
2292         action = gtk_action_group_get_action (window->priv->action_group, "ViewPresentation");
2293         g_signal_handlers_block_by_func
2294                 (action, G_CALLBACK (ev_window_cmd_view_presentation), window);
2295         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
2296                                       ev_view_get_presentation (EV_VIEW (window->priv->view)));
2297         g_signal_handlers_unblock_by_func
2298                 (action, G_CALLBACK (ev_window_cmd_view_presentation), window);
2299 }
2300
2301 static void
2302 ev_window_run_presentation (EvWindow *window)
2303 {
2304         g_object_set (G_OBJECT (window->priv->scrolled_window),
2305                       "shadow-type", GTK_SHADOW_NONE,
2306                       NULL);
2307
2308         ev_view_set_presentation (EV_VIEW (window->priv->view), TRUE);
2309         ev_window_update_presentation_action (window);
2310
2311         gtk_widget_grab_focus (window->priv->view);
2312         gtk_window_fullscreen (GTK_WINDOW (window));
2313
2314         g_signal_connect (window->priv->view,
2315                           "motion-notify-event",
2316                           G_CALLBACK (fullscreen_motion_notify_cb),
2317                           window);
2318         g_signal_connect (window->priv->view,
2319                           "leave-notify-event",
2320                           G_CALLBACK (fullscreen_leave_notify_cb),
2321                           window);
2322         fullscreen_set_timeout (window);
2323
2324         if (!ev_window_is_empty (window))
2325                 ev_metadata_manager_set_boolean (window->priv->uri, "presentation", TRUE);
2326 }
2327
2328 static void
2329 ev_window_stop_presentation (EvWindow *window)
2330 {
2331         if (!ev_view_get_presentation (EV_VIEW (window->priv->view)))
2332                 return;
2333
2334         g_object_set (G_OBJECT (window->priv->scrolled_window),
2335                       "shadow-type", GTK_SHADOW_IN,
2336                       NULL);
2337
2338         ev_view_set_presentation (EV_VIEW (window->priv->view), FALSE);
2339         ev_window_update_presentation_action (window);
2340         gtk_window_unfullscreen (GTK_WINDOW (window));
2341
2342         g_signal_handlers_disconnect_by_func (window->priv->view,
2343                                               (gpointer) fullscreen_motion_notify_cb,
2344                                               window);
2345         g_signal_handlers_disconnect_by_func (window->priv->view,
2346                                               (gpointer) fullscreen_leave_notify_cb,
2347                                               window);
2348         fullscreen_clear_timeout (window);
2349
2350         if (!ev_window_is_empty (window))
2351                 ev_metadata_manager_set_boolean (window->priv->uri, "presentation", FALSE);
2352 }
2353
2354 static void
2355 ev_window_cmd_view_presentation (GtkAction *action, EvWindow *window)
2356 {
2357         gboolean presentation;
2358
2359         g_return_if_fail (EV_IS_WINDOW (window));
2360         ev_window_stop_fullscreen (window);
2361
2362         presentation = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
2363         if (presentation) {
2364                 ev_window_run_presentation (window);
2365         } else {
2366                 ev_window_stop_presentation (window);
2367         }
2368 }
2369
2370 static void
2371 ev_window_run_preview (EvWindow *window)
2372 {
2373         EggToolbarsModel *model;
2374         EggTbModelFlags   flags;
2375
2376         model = egg_toolbars_model_new ();
2377
2378         egg_toolbars_model_load_toolbars (model,
2379                                           DATADIR"/evince-toolbar.xml");
2380         
2381         flags = egg_toolbars_model_get_flags (model, 1);
2382         egg_toolbars_model_set_flags (model, 1, flags &= ~(EGG_TB_MODEL_HIDDEN));
2383
2384         egg_editable_toolbar_set_model (EGG_EDITABLE_TOOLBAR (window->priv->toolbar),
2385                                         model);
2386         
2387         egg_editable_toolbar_hide (EGG_EDITABLE_TOOLBAR (window->priv->toolbar),
2388                                    "DefaultToolBar");
2389         egg_editable_toolbar_show (EGG_EDITABLE_TOOLBAR (window->priv->toolbar),
2390                                    "PreviewToolBar");
2391
2392         ev_view_set_continuous (EV_VIEW (window->priv->view), FALSE); 
2393         
2394         update_chrome_flag (window, EV_CHROME_MENUBAR, FALSE);
2395         update_chrome_flag (window, EV_CHROME_SIDEBAR, FALSE);
2396         update_chrome_visibility (window);
2397 }
2398
2399 static gboolean
2400 ev_window_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
2401 {
2402         EvWindow *window = EV_WINDOW (widget);
2403         EvWindowPrivate *priv = window->priv;
2404
2405         if (ev_view_get_fullscreen (EV_VIEW (priv->view)))
2406                 fullscreen_set_timeout (window);
2407
2408         return GTK_WIDGET_CLASS (ev_window_parent_class)->focus_in_event (widget, event);
2409 }
2410
2411 static gboolean
2412 ev_window_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
2413 {
2414         EvWindow *window = EV_WINDOW (widget);
2415         EvWindowPrivate *priv = window->priv;
2416
2417         if (ev_view_get_fullscreen (EV_VIEW (priv->view))) {
2418                 fullscreen_set_timeout (window);
2419                 gtk_widget_hide (priv->fullscreen_popup);
2420         }
2421
2422         return GTK_WIDGET_CLASS (ev_window_parent_class)->focus_out_event (widget, event);
2423 }
2424
2425 static void
2426 ev_window_screen_changed (GtkWidget *widget,
2427                           GdkScreen *old_screen)
2428 {
2429         EvWindow *window = EV_WINDOW (widget);
2430         EvWindowPrivate *priv = window->priv;
2431         GdkScreen *screen;
2432
2433         screen = gtk_widget_get_screen (widget);
2434         if (screen == old_screen)
2435                 return;
2436
2437 #ifdef HAVE_GTK_RECENT
2438         if (old_screen) {
2439                 g_signal_handlers_disconnect_by_func (
2440                         gtk_recent_manager_get_for_screen (old_screen),
2441                         G_CALLBACK (ev_window_setup_recent), window);
2442         }
2443
2444         priv->recent_manager = gtk_recent_manager_get_for_screen (screen);
2445         g_signal_connect_swapped (priv->recent_manager,
2446                                   "changed",
2447                                   G_CALLBACK (ev_window_setup_recent),
2448                                   window);
2449 #endif
2450         
2451         if (GTK_WIDGET_CLASS (ev_window_parent_class)->screen_changed) {
2452                 GTK_WIDGET_CLASS (ev_window_parent_class)->screen_changed (widget, old_screen);
2453         }
2454
2455         if (priv->fullscreen_popup != NULL) {
2456                 g_signal_handlers_disconnect_by_func
2457                         (old_screen, G_CALLBACK (screen_size_changed_cb), window);
2458
2459                 g_signal_connect_object (screen, "size-changed",
2460                                          G_CALLBACK (screen_size_changed_cb),
2461                                          window, 0);
2462                 gtk_window_set_screen (GTK_WINDOW (priv->fullscreen_popup), screen);
2463
2464                 ev_window_update_fullscreen_popup (window);
2465         }
2466 }
2467
2468 static void
2469 ev_window_set_page_mode (EvWindow         *window,
2470                          EvWindowPageMode  page_mode)
2471 {
2472         GtkWidget *child = NULL;
2473         GtkWidget *real_child;
2474
2475         if (window->priv->page_mode == page_mode)
2476                 return;
2477
2478         window->priv->page_mode = page_mode;
2479
2480         switch (page_mode) {
2481                 case PAGE_MODE_DOCUMENT:
2482                         child = window->priv->view;
2483                         break;
2484                 case PAGE_MODE_PASSWORD:
2485                         child = window->priv->password_view;
2486                         break;
2487                 default:
2488                         g_assert_not_reached ();
2489         }
2490
2491         real_child = gtk_bin_get_child (GTK_BIN (window->priv->scrolled_window));
2492         if (child != real_child) {
2493                 gtk_container_remove (GTK_CONTAINER (window->priv->scrolled_window),
2494                                       real_child);
2495                 gtk_container_add (GTK_CONTAINER (window->priv->scrolled_window),
2496                                    child);
2497         }
2498         ev_window_update_actions (window);
2499 }
2500
2501
2502 static void
2503 ev_window_cmd_edit_rotate_left (GtkAction *action, EvWindow *ev_window)
2504 {
2505         ev_view_rotate_left (EV_VIEW (ev_window->priv->view));
2506 }
2507
2508 static void
2509 ev_window_cmd_edit_rotate_right (GtkAction *action, EvWindow *ev_window)
2510 {
2511         ev_view_rotate_right (EV_VIEW (ev_window->priv->view));
2512 }
2513
2514 static void
2515 ev_window_cmd_edit_toolbar_cb (GtkDialog *dialog, gint response, gpointer data)
2516 {
2517         EvWindow *ev_window = EV_WINDOW (data);
2518         egg_editable_toolbar_set_edit_mode
2519                         (EGG_EDITABLE_TOOLBAR (ev_window->priv->toolbar), FALSE);
2520         ev_application_save_toolbars_model (EV_APP);
2521         gtk_widget_destroy (GTK_WIDGET (dialog));
2522 }
2523
2524 static void
2525 ev_window_cmd_edit_toolbar (GtkAction *action, EvWindow *ev_window)
2526 {
2527         GtkWidget *dialog;
2528         GtkWidget *editor;
2529
2530         dialog = gtk_dialog_new_with_buttons (_("Toolbar Editor"),
2531                                               GTK_WINDOW (ev_window), 
2532                                               GTK_DIALOG_DESTROY_WITH_PARENT, 
2533                                               GTK_STOCK_CLOSE,
2534                                               GTK_RESPONSE_CLOSE, 
2535                                               NULL);
2536         gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CLOSE);
2537         gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (dialog)), 5);
2538         gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 2);
2539         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
2540         gtk_window_set_default_size (GTK_WINDOW (dialog), 500, 400);
2541           
2542         editor = egg_toolbar_editor_new (ev_window->priv->ui_manager,
2543                                          ev_application_get_toolbars_model (EV_APP));
2544         gtk_container_set_border_width (GTK_CONTAINER (editor), 5);
2545         gtk_box_set_spacing (GTK_BOX (EGG_TOOLBAR_EDITOR (editor)), 5);
2546              
2547         gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), editor);
2548
2549         egg_editable_toolbar_set_edit_mode
2550                 (EGG_EDITABLE_TOOLBAR (ev_window->priv->toolbar), TRUE);
2551
2552         g_signal_connect (dialog, "response",
2553                           G_CALLBACK (ev_window_cmd_edit_toolbar_cb),
2554                           ev_window);
2555         gtk_widget_show_all (dialog);
2556 }
2557
2558 static void
2559 ev_window_cmd_view_zoom_in (GtkAction *action, EvWindow *ev_window)
2560 {
2561         g_return_if_fail (EV_IS_WINDOW (ev_window));
2562
2563         ev_view_set_sizing_mode (EV_VIEW (ev_window->priv->view), EV_SIZING_FREE);
2564         ev_view_zoom_in (EV_VIEW (ev_window->priv->view));
2565 }
2566
2567 static void
2568 ev_window_cmd_view_zoom_out (GtkAction *action, EvWindow *ev_window)
2569 {
2570         g_return_if_fail (EV_IS_WINDOW (ev_window));
2571
2572         ev_view_set_sizing_mode (EV_VIEW (ev_window->priv->view), EV_SIZING_FREE);
2573         ev_view_zoom_out (EV_VIEW (ev_window->priv->view));
2574 }
2575
2576 static void
2577 ev_window_cmd_go_previous_page (GtkAction *action, EvWindow *ev_window)
2578 {
2579         g_return_if_fail (EV_IS_WINDOW (ev_window));
2580
2581         ev_view_previous_page (EV_VIEW (ev_window->priv->view));
2582 }
2583
2584 static void
2585 ev_window_cmd_go_next_page (GtkAction *action, EvWindow *ev_window)
2586 {
2587         g_return_if_fail (EV_IS_WINDOW (ev_window));
2588
2589         ev_view_next_page (EV_VIEW (ev_window->priv->view));
2590 }
2591
2592 static void
2593 ev_window_cmd_go_first_page (GtkAction *action, EvWindow *ev_window)
2594 {
2595         g_return_if_fail (EV_IS_WINDOW (ev_window));
2596
2597         ev_page_cache_set_current_page (ev_window->priv->page_cache, 0);
2598 }
2599
2600 static void
2601 ev_window_cmd_go_last_page (GtkAction *action, EvWindow *ev_window)
2602 {
2603         int n_pages;
2604
2605         g_return_if_fail (EV_IS_WINDOW (ev_window));
2606
2607         n_pages = ev_page_cache_get_n_pages (ev_window->priv->page_cache);
2608         ev_page_cache_set_current_page (ev_window->priv->page_cache, n_pages - 1);
2609 }
2610
2611 static void
2612 ev_window_cmd_go_forward (GtkAction *action, EvWindow *ev_window)
2613 {
2614         int n_pages, current_page;
2615         
2616         g_return_if_fail (EV_IS_WINDOW (ev_window));
2617
2618         n_pages = ev_page_cache_get_n_pages (ev_window->priv->page_cache);
2619         current_page = ev_page_cache_get_current_page (ev_window->priv->page_cache);
2620         
2621         if (current_page + 10 < n_pages)
2622                 ev_page_cache_set_current_page (ev_window->priv->page_cache, current_page + 10);
2623 }
2624
2625 static void
2626 ev_window_cmd_go_backward (GtkAction *action, EvWindow *ev_window)
2627 {
2628         int current_page;
2629         
2630         g_return_if_fail (EV_IS_WINDOW (ev_window));
2631
2632         current_page = ev_page_cache_get_current_page (ev_window->priv->page_cache);
2633         
2634         if (current_page - 10 >= 0)
2635                 ev_page_cache_set_current_page (ev_window->priv->page_cache, current_page - 10);
2636 }
2637
2638 static void
2639 ev_window_cmd_view_reload (GtkAction *action, EvWindow *ev_window)
2640 {
2641         char *uri;
2642
2643         g_return_if_fail (EV_IS_WINDOW (ev_window));
2644
2645         uri = g_strdup (ev_window->priv->uri);
2646
2647         ev_window_open_uri (ev_window, uri, NULL, 0);
2648
2649         g_free (uri);
2650 }
2651
2652 static void
2653 ev_window_cmd_help_contents (GtkAction *action, EvWindow *ev_window)
2654 {
2655         GError *error = NULL;
2656
2657         g_return_if_fail (EV_IS_WINDOW (ev_window));
2658
2659         gnome_help_display ("evince.xml", NULL, &error);
2660
2661         if(error != NULL) {
2662                 g_warning (error->message);
2663                 g_error_free (error);
2664         }
2665 }
2666
2667 static void
2668 ev_window_cmd_leave_fullscreen (GtkAction *action, EvWindow *window)
2669 {
2670         gtk_window_unfullscreen (GTK_WINDOW (window));
2671 }
2672
2673 static void
2674 ev_window_cmd_escape (GtkAction *action, EvWindow *window)
2675 {
2676         GtkWidget *widget;
2677
2678         widget = gtk_window_get_focus (GTK_WINDOW (window));
2679         if (widget && gtk_widget_get_ancestor (widget, EGG_TYPE_FIND_BAR)) {
2680                 update_chrome_flag (window, EV_CHROME_FINDBAR, FALSE);
2681                 gtk_widget_grab_focus (window->priv->view);
2682         } else {
2683                 gboolean fullscreen;
2684                 gboolean presentation;
2685
2686                 g_object_get (window->priv->view,
2687                               "fullscreen", &fullscreen,
2688                               "presentation", &presentation,
2689                               NULL);
2690
2691                 if (fullscreen) {
2692                         ev_window_stop_fullscreen (window);
2693                 } else if (presentation) {
2694                         ev_window_stop_presentation (window);
2695                         gtk_widget_grab_focus (window->priv->view);
2696                 } else {
2697                         gtk_widget_grab_focus (window->priv->view);
2698                 }
2699
2700                 if (fullscreen && presentation)
2701                         g_warning ("Both fullscreen and presentation set somehow");
2702         }
2703 }
2704
2705 static void
2706 update_view_size (EvView *view, EvWindow *window)
2707 {
2708         int width, height;
2709         GtkRequisition vsb_requisition;
2710         GtkRequisition hsb_requisition;
2711         int scrollbar_spacing;
2712
2713         /* Calculate the width available for the */
2714         width = window->priv->scrolled_window->allocation.width;
2715         height = window->priv->scrolled_window->allocation.height;
2716
2717         if (gtk_scrolled_window_get_shadow_type (GTK_SCROLLED_WINDOW (window->priv->scrolled_window)) == GTK_SHADOW_IN) {
2718                 width -= 2 * window->priv->view->style->xthickness;
2719                 height -= 2 * window->priv->view->style->ythickness;
2720         }
2721
2722         gtk_widget_size_request (GTK_SCROLLED_WINDOW (window->priv->scrolled_window)->vscrollbar,
2723                                  &vsb_requisition);
2724         gtk_widget_size_request (GTK_SCROLLED_WINDOW (window->priv->scrolled_window)->hscrollbar,
2725                                  &hsb_requisition);
2726         gtk_widget_style_get (window->priv->scrolled_window,
2727                               "scrollbar_spacing", &scrollbar_spacing,
2728                               NULL);
2729
2730         ev_view_set_zoom_for_size (EV_VIEW (window->priv->view),
2731                                    MAX (1, width),
2732                                    MAX (1, height),
2733                                    vsb_requisition.width + scrollbar_spacing,
2734                                    hsb_requisition.height + scrollbar_spacing);
2735 }
2736
2737 static void
2738 save_sizing_mode (EvWindow *window)
2739 {
2740         EvSizingMode mode;
2741         GEnumValue *enum_value;
2742
2743         mode = ev_view_get_sizing_mode (EV_VIEW (window->priv->view));
2744         enum_value = g_enum_get_value (EV_SIZING_MODE_CLASS, mode);
2745
2746         if (!ev_window_is_empty (window))
2747                 ev_metadata_manager_set_string (window->priv->uri, "sizing_mode",
2748                                                 enum_value->value_nick);
2749 }
2750
2751 static void     
2752 ev_window_sizing_mode_changed_cb (EvView *view, GParamSpec *pspec,
2753                                   EvWindow   *ev_window)
2754 {
2755         GtkWidget *scrolled_window;
2756         EvSizingMode sizing_mode;
2757
2758         g_object_get (ev_window->priv->view,
2759                       "sizing-mode", &sizing_mode,
2760                       NULL);
2761
2762         scrolled_window = ev_window->priv->scrolled_window;
2763
2764         g_signal_handlers_disconnect_by_func (ev_window->priv->view, update_view_size, ev_window);
2765
2766         if (sizing_mode != EV_SIZING_FREE)
2767                 update_view_size (NULL, ev_window);
2768
2769         switch (sizing_mode) {
2770                 case EV_SIZING_BEST_FIT:
2771                         g_object_set (G_OBJECT (scrolled_window),
2772                                       "hscrollbar-policy", GTK_POLICY_NEVER,
2773                                       "vscrollbar-policy", GTK_POLICY_AUTOMATIC,
2774                                       NULL);
2775                         g_signal_connect (ev_window->priv->view, "zoom_invalid",
2776                                           G_CALLBACK (update_view_size),
2777                                           ev_window);
2778                         break;
2779                 case EV_SIZING_FIT_WIDTH:
2780                         g_object_set (G_OBJECT (scrolled_window),
2781                                       "hscrollbar-policy", GTK_POLICY_NEVER,
2782                                       "vscrollbar-policy", GTK_POLICY_AUTOMATIC,
2783                                       NULL);
2784                         g_signal_connect (ev_window->priv->view, "zoom_invalid",
2785                                           G_CALLBACK (update_view_size),
2786                                           ev_window);
2787                         break;
2788                 case EV_SIZING_FREE:
2789                         g_object_set (G_OBJECT (scrolled_window),
2790                                       "hscrollbar-policy", GTK_POLICY_AUTOMATIC,
2791                                       "vscrollbar-policy", GTK_POLICY_AUTOMATIC,
2792                                       NULL);
2793                         break;
2794         }
2795
2796         update_sizing_buttons (ev_window);
2797         save_sizing_mode (ev_window);
2798 }
2799
2800 static void     
2801 ev_window_zoom_changed_cb (EvView *view, GParamSpec *pspec, EvWindow *ev_window)
2802 {
2803         ev_window_update_actions (ev_window);
2804
2805         if (ev_view_get_sizing_mode (view) == EV_SIZING_FREE && !ev_window_is_empty (ev_window)) {
2806                 ev_metadata_manager_set_double (ev_window->priv->uri, "zoom",
2807                                                 ev_view_get_zoom (view));
2808         }
2809 }
2810
2811 static void
2812 ev_window_update_continuous_action (EvWindow *window)
2813 {
2814         GtkAction *action;
2815
2816         action = gtk_action_group_get_action (window->priv->action_group, "ViewContinuous");
2817         g_signal_handlers_block_by_func
2818                 (action, G_CALLBACK (ev_window_cmd_continuous), window);
2819         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
2820                                       ev_view_get_continuous (EV_VIEW (window->priv->view)));
2821         g_signal_handlers_unblock_by_func
2822                 (action, G_CALLBACK (ev_window_cmd_continuous), window);
2823 }
2824
2825 static void
2826 ev_window_update_dual_page_action (EvWindow *window)
2827 {
2828         GtkAction *action;
2829
2830         action = gtk_action_group_get_action (window->priv->action_group, "ViewDual");
2831         g_signal_handlers_block_by_func
2832                 (action, G_CALLBACK (ev_window_cmd_dual), window);
2833         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
2834                                       ev_view_get_dual_page (EV_VIEW (window->priv->view)));
2835         g_signal_handlers_unblock_by_func
2836                 (action, G_CALLBACK (ev_window_cmd_dual), window);
2837 }
2838
2839 static void     
2840 ev_window_continuous_changed_cb (EvView *view, GParamSpec *pspec, EvWindow *ev_window)
2841 {
2842         ev_window_update_continuous_action (ev_window);
2843
2844         if (!ev_window_is_empty (ev_window))
2845                 ev_metadata_manager_set_boolean (ev_window->priv->uri, "continuous",
2846                                                  ev_view_get_continuous (EV_VIEW (ev_window->priv->view)));
2847 }
2848
2849 static void     
2850 ev_window_rotation_changed_cb (EvView *view, GParamSpec *pspec, EvWindow *window)
2851 {
2852         int rotation;
2853
2854         rotation = ev_view_get_rotation (EV_VIEW (window->priv->view));
2855
2856         if (!ev_window_is_empty (window))
2857                 ev_metadata_manager_set_int (window->priv->uri, "rotation",
2858                                              rotation);
2859
2860         ev_sidebar_thumbnails_refresh (EV_SIDEBAR_THUMBNAILS (window->priv->sidebar_thumbs),
2861                                        rotation);
2862 }
2863
2864 static void
2865 ev_window_has_selection_changed_cb (EvView *view, GParamSpec *pspec, EvWindow *window)
2866 {
2867         ev_window_update_actions (window);
2868 }
2869
2870 static void     
2871 ev_window_dual_mode_changed_cb (EvView *view, GParamSpec *pspec, EvWindow *ev_window)
2872 {
2873         ev_window_update_dual_page_action (ev_window);
2874
2875         if (!ev_window_is_empty (ev_window))
2876                 ev_metadata_manager_set_boolean (ev_window->priv->uri, "dual-page",
2877                                                  ev_view_get_dual_page (EV_VIEW (ev_window->priv->view)));
2878 }
2879
2880 static char *
2881 build_comments_string (void)
2882 {
2883         PopplerBackend backend;
2884         const char *backend_name;
2885         const char *version;
2886
2887         backend = poppler_get_backend ();
2888         version = poppler_get_version ();
2889         switch (backend) {
2890                 case POPPLER_BACKEND_CAIRO:
2891                         backend_name = "cairo";
2892                         break;
2893                 case POPPLER_BACKEND_SPLASH:
2894                         backend_name = "splash";
2895                         break;
2896                 default:
2897                         backend_name = "unknown";
2898                         break;
2899         }
2900
2901         return g_strdup_printf (_("PostScript and PDF File Viewer.\n"
2902                                   "Using poppler %s (%s)"),
2903                                 version, backend_name);
2904 }
2905
2906 static void
2907 ev_window_cmd_help_about (GtkAction *action, EvWindow *ev_window)
2908 {
2909         const char *authors[] = {
2910                 "Martin Kretzschmar <m_kretzschmar@gmx.net>",
2911                 "Jonathan Blandford <jrb@gnome.org>",
2912                 "Marco Pesenti Gritti <marco@gnome.org>",
2913                 "Nickolay V. Shmyrev <nshmyrev@yandex.ru>",
2914                 "Bryan Clark <clarkbw@gnome.org>",
2915                 "Carlos Garcia Campos <carlosgc@gnome.org>",
2916                 "Wouter Bolsterlee <wbolster@gnome.org>",
2917                 NULL
2918         };
2919
2920         const char *documenters[] = {
2921                 "Nickolay V. Shmyrev <nshmyrev@yandex.ru>",
2922                 NULL
2923         };
2924
2925         const char *license[] = {
2926                 N_("Evince is free software; you can redistribute it and/or modify "
2927                    "it under the terms of the GNU General Public License as published by "
2928                    "the Free Software Foundation; either version 2 of the License, or "
2929                    "(at your option) any later version.\n"),
2930                 N_("Evince is distributed in the hope that it will be useful, "
2931                    "but WITHOUT ANY WARRANTY; without even the implied warranty of "
2932                    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the "
2933                    "GNU General Public License for more details.\n"),
2934                 N_("You should have received a copy of the GNU General Public License "
2935                    "along with Evince; if not, write to the Free Software Foundation, Inc., "
2936                    "59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n")
2937         };
2938
2939         char *license_trans;
2940         char *comments;
2941
2942 #ifdef ENABLE_NLS
2943         const char **p;
2944
2945         for (p = authors; *p; ++p)
2946                 *p = _(*p);
2947
2948         for (p = documenters; *p; ++p)
2949                 *p = _(*p);
2950 #endif
2951
2952         license_trans = g_strconcat (_(license[0]), "\n", _(license[1]), "\n",
2953                                      _(license[2]), "\n", NULL);
2954         comments = build_comments_string ();
2955
2956         gtk_show_about_dialog (
2957                 GTK_WINDOW (ev_window),
2958                 "name", _("Evince"),
2959                 "version", VERSION,
2960                 "copyright",
2961                 _("\xc2\xa9 1996-2005 The Evince authors"),
2962                 "license", license_trans,
2963                 "website", "http://www.gnome.org/projects/evince",
2964                 "comments", comments,
2965                 "authors", authors,
2966                 "documenters", documenters,
2967                 "translator-credits", _("translator-credits"),
2968                 "logo-icon-name", "evince",
2969                 "wrap-license", TRUE,
2970                 NULL);
2971
2972         g_free (comments);
2973         g_free (license_trans);
2974 }
2975
2976 static void
2977 ev_window_view_toolbar_cb (GtkAction *action, EvWindow *ev_window)
2978 {
2979         gboolean active;
2980         
2981         active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
2982         update_chrome_flag (ev_window, EV_CHROME_TOOLBAR, active);
2983         ev_metadata_manager_set_boolean (NULL, "show_toolbar", active);
2984 }
2985
2986 static void
2987 ev_window_view_sidebar_cb (GtkAction *action, EvWindow *ev_window)
2988 {
2989         update_chrome_flag (ev_window, EV_CHROME_SIDEBAR,
2990                             gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
2991 }
2992
2993 static void
2994 ev_window_sidebar_current_page_changed_cb (EvSidebar  *ev_sidebar,
2995                                            GParamSpec *pspec,
2996                                            EvWindow   *ev_window)
2997 {
2998         GtkWidget *current_page;
2999         const char *id;
3000
3001         g_object_get (G_OBJECT (ev_sidebar), "current_page", &current_page, NULL);
3002
3003         if (current_page == ev_window->priv->sidebar_links) {
3004                 id = LINKS_SIDEBAR_ID;
3005         } else if (current_page == ev_window->priv->sidebar_thumbs) {
3006                 id = THUMBNAILS_SIDEBAR_ID;
3007         } else if (current_page == ev_window->priv->sidebar_attachments) {
3008                 id = ATTACHMENTS_SIDEBAR_ID;
3009         } else {
3010                 g_assert_not_reached();
3011         }
3012
3013         g_object_unref (current_page);
3014
3015         if (!ev_window_is_empty (ev_window))
3016                 ev_metadata_manager_set_string (ev_window->priv->uri, "sidebar_page", id);
3017 }
3018
3019 static void
3020 ev_window_sidebar_visibility_changed_cb (EvSidebar  *ev_sidebar,
3021                                          GParamSpec *pspec,
3022                                          EvWindow   *ev_window)
3023 {
3024         EvView *view = EV_VIEW (ev_window->priv->view);
3025         GtkAction *action;
3026
3027         action = gtk_action_group_get_action (ev_window->priv->action_group, "ViewSidebar");
3028         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
3029                                       GTK_WIDGET_VISIBLE (ev_sidebar));
3030
3031         if (!ev_view_get_presentation (view) && 
3032             !ev_view_get_fullscreen (view)) {
3033                 ev_metadata_manager_set_boolean (ev_window->priv->uri, "sidebar_visibility",
3034                                                  GTK_WIDGET_VISIBLE (ev_sidebar));
3035         }
3036 }
3037
3038 static gboolean
3039 view_menu_popup_cb (EvView   *view,
3040                     EvLink   *link,
3041                     EvWindow *ev_window)
3042 {
3043         GtkWidget *popup;
3044         gboolean   show_external = FALSE;
3045         gboolean   show_internal = FALSE;
3046         GtkAction *action;
3047
3048         if (ev_view_get_presentation (EV_VIEW (ev_window->priv->view)))
3049                 return FALSE;
3050         
3051         if (ev_window->priv->link)
3052                 g_object_unref (ev_window->priv->link);
3053         
3054         if (link)
3055                 ev_window->priv->link = g_object_ref (link);
3056         else    
3057                 ev_window->priv->link = NULL;
3058
3059         popup = ev_window->priv->view_popup;
3060
3061         if (ev_window->priv->link) {
3062                 EvLinkAction *ev_action;
3063
3064                 ev_action = ev_link_get_action (link);
3065                 if (!ev_action)
3066                         return FALSE;
3067                 
3068                 switch (ev_link_action_get_action_type (ev_action)) {
3069                         case EV_LINK_ACTION_TYPE_GOTO_DEST:
3070                         case EV_LINK_ACTION_TYPE_GOTO_REMOTE:
3071                                 show_internal = TRUE;
3072                                 break;
3073                         case EV_LINK_ACTION_TYPE_EXTERNAL_URI:
3074                         case EV_LINK_ACTION_TYPE_LAUNCH:
3075                                 show_external = TRUE;
3076                                 break;
3077                         default:
3078                                 break;
3079                 }
3080         }
3081         
3082         action = gtk_action_group_get_action (ev_window->priv->view_popup_action_group,
3083                                               "OpenLink");
3084         gtk_action_set_visible (action, show_external);
3085
3086         action = gtk_action_group_get_action (ev_window->priv->view_popup_action_group,
3087                                               "CopyLinkAddress");
3088         gtk_action_set_visible (action, show_external);
3089
3090         action = gtk_action_group_get_action (ev_window->priv->view_popup_action_group,
3091                                               "GoLink");
3092         gtk_action_set_visible (action, show_internal);
3093
3094         gtk_menu_popup (GTK_MENU (popup), NULL, NULL,
3095                         NULL, NULL,
3096                         3, gtk_get_current_event_time ());
3097         return TRUE;
3098 }
3099
3100 static gboolean
3101 attachment_bar_menu_popup_cb (EvSidebarAttachments *attachbar,
3102                               GList           *attach_list,
3103                               EvWindow        *ev_window)
3104 {
3105         GtkWidget *popup;
3106
3107         g_assert (attach_list != NULL);
3108
3109         if (ev_window->priv->attach_list) {
3110                 g_list_foreach (ev_window->priv->attach_list,
3111                                 (GFunc) g_object_unref, NULL);
3112                 g_list_free (ev_window->priv->attach_list);
3113         }
3114         
3115         ev_window->priv->attach_list = attach_list;
3116         
3117         popup = ev_window->priv->attachment_popup;
3118
3119         gtk_menu_popup (GTK_MENU (popup), NULL, NULL,
3120                         NULL, NULL,
3121                         3, gtk_get_current_event_time ());
3122
3123         return TRUE;
3124 }
3125
3126 static void
3127 view_find_status_changed_cb (EvView     *view,
3128                              GParamSpec *pspec,
3129                              EvWindow   *ev_window)
3130 {
3131         const char *text;
3132
3133         text = ev_view_get_find_status (view);
3134         egg_find_bar_set_status_text (EGG_FIND_BAR (ev_window->priv->find_bar),
3135                                       text);
3136 }
3137
3138 static void
3139 find_bar_previous_cb (EggFindBar *find_bar,
3140                       EvWindow   *ev_window)
3141 {
3142         ev_view_find_previous (EV_VIEW (ev_window->priv->view));
3143 }
3144
3145 static void
3146 find_bar_next_cb (EggFindBar *find_bar,
3147                   EvWindow   *ev_window)
3148 {
3149         ev_view_find_next (EV_VIEW (ev_window->priv->view));
3150 }
3151
3152 static void
3153 find_bar_close_cb (EggFindBar *find_bar,
3154                    EvWindow   *ev_window)
3155 {
3156         update_chrome_flag (ev_window, EV_CHROME_FINDBAR, FALSE);
3157 }
3158
3159 static void
3160 find_bar_search_changed_cb (EggFindBar *find_bar,
3161                             GParamSpec *param,
3162                             EvWindow   *ev_window)
3163 {
3164         gboolean case_sensitive;
3165         gboolean visible;
3166         const char *search_string;
3167
3168         g_return_if_fail (EV_IS_WINDOW (ev_window));
3169
3170         /* Either the string or case sensitivity could have changed,
3171          * we connect this callback to both. We also connect it
3172          * to ::visible so when the find bar is hidden, we should
3173          * pretend the search string is NULL/""
3174          */
3175
3176         case_sensitive = egg_find_bar_get_case_sensitive (find_bar);
3177         visible = GTK_WIDGET_VISIBLE (find_bar);
3178         search_string = egg_find_bar_get_search_string (find_bar);
3179
3180         ev_view_search_changed (EV_VIEW(ev_window->priv->view));
3181
3182         if (ev_window->priv->document &&
3183             EV_IS_DOCUMENT_FIND (ev_window->priv->document)) {
3184                 if (visible && search_string && search_string[0]) {
3185                         ev_document_doc_mutex_lock ();
3186                         ev_document_find_begin (EV_DOCUMENT_FIND (ev_window->priv->document), 
3187                                                 ev_page_cache_get_current_page (ev_window->priv->page_cache),
3188                                                 search_string,
3189                                                 case_sensitive);
3190                         ev_document_doc_mutex_unlock ();
3191                 } else {
3192                         ev_document_doc_mutex_lock ();
3193                         ev_document_find_cancel (EV_DOCUMENT_FIND (ev_window->priv->document));
3194                         ev_document_doc_mutex_unlock ();
3195
3196                         ev_window_update_actions (ev_window);
3197                         egg_find_bar_set_status_text (EGG_FIND_BAR (ev_window->priv->find_bar),
3198                                                       NULL);
3199                         gtk_widget_queue_draw (GTK_WIDGET (ev_window->priv->view));
3200                 }
3201         }
3202 }
3203
3204 static void
3205 find_bar_scroll(EggFindBar *find_bar, GtkScrollType scroll, EvWindow* ev_window)
3206 {
3207         ev_view_scroll(EV_VIEW(ev_window->priv->view), scroll, FALSE);
3208 }
3209
3210 static void
3211 zoom_control_changed_cb (EphyZoomAction *action,
3212                          float           zoom,
3213                          EvWindow       *ev_window)
3214 {
3215         EvSizingMode mode;
3216         
3217         g_return_if_fail (EV_IS_WINDOW (ev_window));
3218
3219         if (zoom == EPHY_ZOOM_BEST_FIT) {
3220                 mode = EV_SIZING_BEST_FIT;
3221         } else if (zoom == EPHY_ZOOM_FIT_WIDTH) {
3222                 mode = EV_SIZING_FIT_WIDTH;
3223         } else {
3224                 mode = EV_SIZING_FREE;
3225         }
3226         
3227         ev_view_set_sizing_mode (EV_VIEW (ev_window->priv->view), mode);
3228         
3229         if (mode == EV_SIZING_FREE) {
3230                 ev_view_set_zoom (EV_VIEW (ev_window->priv->view), zoom, FALSE);
3231         }
3232 }
3233
3234 static void
3235 ev_window_finalize (GObject *object)
3236 {
3237         GList *windows = ev_application_get_windows (EV_APP);
3238
3239         if (windows == NULL) {
3240                 ev_application_shutdown (EV_APP);
3241         } else {
3242                 g_list_free (windows);
3243         }
3244         
3245         G_OBJECT_CLASS (ev_window_parent_class)->finalize (object);
3246 }
3247
3248 static void
3249 ev_window_dispose (GObject *object)
3250 {
3251         EvWindow *window = EV_WINDOW (object);
3252         EvWindowPrivate *priv = window->priv;
3253
3254         if (priv->title) {
3255                 ev_window_title_free (priv->title);
3256                 priv->title = NULL;
3257         }
3258
3259         if (priv->ui_manager) {
3260                 g_object_unref (priv->ui_manager);
3261                 priv->ui_manager = NULL;
3262         }
3263
3264         if (priv->action_group) {
3265                 g_object_unref (priv->action_group);
3266                 priv->action_group = NULL;
3267         }
3268
3269         if (priv->view_popup_action_group) {
3270                 g_object_unref (priv->view_popup_action_group);
3271                 priv->view_popup_action_group = NULL;
3272         }
3273
3274         if (priv->attachment_popup_action_group) {
3275                 g_object_unref (priv->attachment_popup_action_group);
3276                 priv->attachment_popup_action_group = NULL;
3277         }
3278
3279 #ifdef HAVE_GTK_RECENT
3280         if (priv->recent_action_group) {
3281                 g_object_unref (priv->recent_action_group);
3282                 priv->recent_action_group = NULL;
3283         }
3284
3285         if (priv->recent_manager) {
3286                 g_signal_handlers_disconnect_by_func (priv->recent_manager,
3287                                                       ev_window_setup_recent,
3288                                                       window);
3289                 priv->recent_manager = NULL;
3290         }
3291
3292         priv->recent_ui_id = 0;
3293 #else
3294         if (priv->recent_view) {
3295                 g_object_unref (priv->recent_view);
3296                 priv->recent_view = NULL;
3297         }
3298 #endif /* HAVE_GTK_RECENT */
3299
3300         if (priv->page_cache) {
3301                 g_signal_handlers_disconnect_by_func (priv->page_cache, page_changed_cb, window);
3302                 priv->page_cache = NULL;
3303         }
3304
3305         if (priv->document) {
3306                 g_object_unref (priv->document);
3307                 priv->document = NULL;
3308         }
3309         
3310         if (priv->view) {
3311                 g_object_unref (priv->view);
3312                 priv->view = NULL;
3313         }
3314
3315         if (priv->password_view) {
3316                 g_object_unref (priv->password_view);
3317                 priv->password_view = NULL;
3318         }
3319
3320         if (priv->xfer_job) {
3321                 ev_window_clear_xfer_job (window);
3322         }
3323         
3324         if (priv->local_uri) {
3325                 ev_window_clear_local_uri (window);
3326         }
3327         
3328         ev_window_close_dialogs (window);
3329
3330 #ifdef WITH_GTK_PRINT
3331         ev_window_clear_print_job (window);
3332
3333         if (window->priv->gtk_print_job) {
3334                 g_object_unref (window->priv->gtk_print_job);
3335                 window->priv->gtk_print_job = NULL;
3336         }
3337         
3338         if (window->priv->printer) {
3339                 g_object_unref (window->priv->printer);
3340                 window->priv->printer = NULL;
3341         }
3342
3343         if (window->priv->print_settings) {
3344                 g_object_unref (window->priv->print_settings);
3345                 window->priv->print_settings = NULL;
3346         }
3347
3348         if (window->priv->print_page_setup) {
3349                 g_object_unref (window->priv->print_page_setup);
3350                 window->priv->print_page_setup = NULL;
3351         }
3352 #endif
3353
3354         if (priv->link) {
3355                 g_object_unref (priv->link);
3356                 priv->link = NULL;
3357         }
3358
3359         if (priv->attach_list) {
3360                 g_list_foreach (priv->attach_list,
3361                                 (GFunc) g_object_unref,
3362                                 NULL);
3363                 g_list_free (priv->attach_list);
3364                 priv->attach_list = NULL;
3365         }
3366
3367         if (priv->find_bar) {
3368                 g_signal_handlers_disconnect_by_func
3369                         (window->priv->find_bar,
3370                          G_CALLBACK (find_bar_close_cb),
3371                          window);
3372                 priv->find_bar = NULL;
3373         }
3374
3375         if (priv->uri) {
3376                 g_free (priv->uri);
3377                 priv->uri = NULL;
3378         }
3379
3380         if (priv->dest) {
3381                 g_object_unref (priv->dest);
3382                 priv->dest = NULL;
3383         }
3384
3385         if (priv->fullscreen_timeout_id) {
3386                 g_source_remove (priv->fullscreen_timeout_id);
3387                 priv->fullscreen_timeout_id = 0;
3388         }
3389         destroy_fullscreen_popup (window);
3390
3391         G_OBJECT_CLASS (ev_window_parent_class)->dispose (object);
3392 }
3393
3394 static void
3395 ev_window_class_init (EvWindowClass *ev_window_class)
3396 {
3397         GObjectClass *g_object_class = G_OBJECT_CLASS (ev_window_class);
3398         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (ev_window_class);
3399
3400         g_object_class->dispose = ev_window_dispose;
3401         g_object_class->finalize = ev_window_finalize;
3402
3403         widget_class->focus_in_event = ev_window_focus_in_event;
3404         widget_class->focus_out_event = ev_window_focus_out_event;
3405         widget_class->screen_changed = ev_window_screen_changed;
3406
3407         g_type_class_add_private (g_object_class, sizeof (EvWindowPrivate));
3408 }
3409
3410 /* Normal items */
3411 static const GtkActionEntry entries[] = {
3412         { "File", NULL, N_("_File") },
3413         { "Edit", NULL, N_("_Edit") },
3414         { "View", NULL, N_("_View") },
3415         { "Go", NULL, N_("_Go") },
3416         { "Help", NULL, N_("_Help") },
3417
3418         /* File menu */
3419         { "FileOpen", GTK_STOCK_OPEN, N_("_Open..."), "<control>O",
3420           N_("Open an existing document"),
3421           G_CALLBACK (ev_window_cmd_file_open) },
3422         { "FileSaveAs", GTK_STOCK_SAVE_AS, N_("_Save a Copy..."), "<control>S",
3423           N_("Save a copy of the current document"),
3424           G_CALLBACK (ev_window_cmd_save_as) },
3425         { "FilePrint", GTK_STOCK_PRINT, N_("_Print..."), "<control>P",
3426           N_("Print this document"),
3427           G_CALLBACK (ev_window_cmd_file_print) },
3428         { "FileProperties", GTK_STOCK_PROPERTIES, N_("P_roperties"), "<alt>Return", NULL,
3429           G_CALLBACK (ev_window_cmd_file_properties) },                       
3430         { "FileCloseWindow", GTK_STOCK_CLOSE, NULL, "<control>W", NULL,
3431           G_CALLBACK (ev_window_cmd_file_close_window) },
3432
3433         /* Edit menu */
3434         { "EditCopy", GTK_STOCK_COPY, NULL, "<control>C", NULL,
3435           G_CALLBACK (ev_window_cmd_edit_copy) },
3436         { "EditSelectAll", NULL, N_("Select _All"), "<control>A", NULL,
3437           G_CALLBACK (ev_window_cmd_edit_select_all) },
3438         { "EditFind", GTK_STOCK_FIND, N_("_Find..."), "<control>F",
3439           N_("Find a word or phrase in the document"),
3440           G_CALLBACK (ev_window_cmd_edit_find) },
3441         { "EditFindNext", NULL, N_("Find Ne_xt"), "<control>G", NULL,
3442           G_CALLBACK (ev_window_cmd_edit_find_next) },
3443         { "EditFindPrevious", NULL, N_("Find Pre_vious"), "<shift><control>G", NULL,
3444           G_CALLBACK (ev_window_cmd_edit_find_previous) },
3445         { "EditToolbar", NULL, N_("T_oolbar"), NULL, NULL,
3446           G_CALLBACK (ev_window_cmd_edit_toolbar) },
3447         { "EditRotateLeft", NULL, N_("Rotate _Left"), NULL, NULL,
3448           G_CALLBACK (ev_window_cmd_edit_rotate_left) },
3449         { "EditRotateRight", NULL, N_("Rotate _Right"), NULL, NULL,
3450           G_CALLBACK (ev_window_cmd_edit_rotate_right) },
3451
3452         /* View menu */
3453         { "ViewZoomIn", GTK_STOCK_ZOOM_IN, NULL, "<control>plus",
3454           N_("Enlarge the document"),
3455           G_CALLBACK (ev_window_cmd_view_zoom_in) },
3456         { "ViewZoomOut", GTK_STOCK_ZOOM_OUT, NULL, "<control>minus",
3457           N_("Shrink the document"),
3458           G_CALLBACK (ev_window_cmd_view_zoom_out) },
3459         { "ViewReload", GTK_STOCK_REFRESH, N_("_Reload"), "<control>R",
3460           N_("Reload the document"),
3461           G_CALLBACK (ev_window_cmd_view_reload) },
3462
3463         /* Go menu */
3464         { "GoPreviousPage", GTK_STOCK_GO_BACK, N_("_Previous Page"), "<control>Page_Up",
3465           N_("Go to the previous page"),
3466           G_CALLBACK (ev_window_cmd_go_previous_page) },
3467         { "GoNextPage", GTK_STOCK_GO_FORWARD, N_("_Next Page"), "<control>Page_Down",
3468           N_("Go to the next page"),
3469           G_CALLBACK (ev_window_cmd_go_next_page) },
3470         { "GoFirstPage", GTK_STOCK_GOTO_FIRST, N_("_First Page"), "<control>Home",
3471           N_("Go to the first page"),
3472           G_CALLBACK (ev_window_cmd_go_first_page) },
3473         { "GoLastPage", GTK_STOCK_GOTO_LAST, N_("_Last Page"), "<control>End",
3474           N_("Go to the last page"),
3475           G_CALLBACK (ev_window_cmd_go_last_page) },
3476
3477         /* Help menu */
3478         { "HelpContents", GTK_STOCK_HELP, N_("_Contents"), "F1", NULL,
3479           G_CALLBACK (ev_window_cmd_help_contents) },
3480
3481         { "HelpAbout", GTK_STOCK_ABOUT, N_("_About"), NULL, NULL,
3482           G_CALLBACK (ev_window_cmd_help_about) },
3483
3484         /* Toolbar-only */
3485         { "LeaveFullscreen", EV_STOCK_LEAVE_FULLSCREEN, N_("Leave Fullscreen"), NULL,
3486           N_("Leave fullscreen mode"),
3487           G_CALLBACK (ev_window_cmd_leave_fullscreen) },
3488
3489         /* Accellerators */
3490         { "Escape", NULL, "", "Escape", "",
3491           G_CALLBACK (ev_window_cmd_escape) },
3492         { "Slash", GTK_STOCK_FIND, NULL, "slash", NULL,
3493           G_CALLBACK (ev_window_cmd_edit_find) },
3494         { "PageDown", NULL, "", "Page_Down", NULL,
3495           G_CALLBACK (ev_window_cmd_scroll_forward) },
3496         { "PageUp", NULL, "", "Page_Up", NULL,
3497           G_CALLBACK (ev_window_cmd_scroll_backward) },
3498         { "Space", NULL, "", "space", NULL,
3499           G_CALLBACK (ev_window_cmd_scroll_forward) },
3500         { "ShiftSpace", NULL, "", "<shift>space", NULL,
3501           G_CALLBACK (ev_window_cmd_scroll_backward) },
3502         { "BackSpace", NULL, "", "BackSpace", NULL,
3503           G_CALLBACK (ev_window_cmd_scroll_backward) },
3504         { "ShiftBackSpace", NULL, "", "<shift>BackSpace", NULL,
3505           G_CALLBACK (ev_window_cmd_scroll_forward) },
3506         { "Return", NULL, "", "Return", NULL,
3507           G_CALLBACK (ev_window_cmd_scroll_forward) },
3508         { "ShiftReturn", NULL, "", "<shift>Return", NULL,
3509           G_CALLBACK (ev_window_cmd_scroll_backward) },
3510         { "Plus", GTK_STOCK_ZOOM_IN, NULL, "plus", NULL,
3511           G_CALLBACK (ev_window_cmd_view_zoom_in) },
3512         { "CtrlEqual", GTK_STOCK_ZOOM_IN, NULL, "<control>equal", NULL,
3513           G_CALLBACK (ev_window_cmd_view_zoom_in) },
3514         { "Equal", GTK_STOCK_ZOOM_IN, NULL, "equal", NULL,
3515           G_CALLBACK (ev_window_cmd_view_zoom_in) },
3516         { "Minus", GTK_STOCK_ZOOM_OUT, NULL, "minus", NULL,
3517           G_CALLBACK (ev_window_cmd_view_zoom_out) },
3518         { "FocusPageSelector", NULL, "", "<control>l", NULL,
3519           G_CALLBACK (ev_window_cmd_focus_page_selector) },
3520         { "GoBackwardFast", NULL, "", "<shift>Page_Up", NULL,
3521           G_CALLBACK (ev_window_cmd_go_backward) },
3522         { "GoForwardFast", NULL, "", "<shift>Page_Down", NULL,
3523           G_CALLBACK (ev_window_cmd_go_forward) },
3524         { "KpPlus", GTK_STOCK_ZOOM_IN, NULL, "KP_Add", NULL,
3525           G_CALLBACK (ev_window_cmd_view_zoom_in) },
3526         { "KpMinus", GTK_STOCK_ZOOM_OUT, NULL, "KP_Subtract", NULL,
3527           G_CALLBACK (ev_window_cmd_view_zoom_out) },
3528         { "CtrlKpPlus", GTK_STOCK_ZOOM_IN, NULL, "<control>KP_Add", NULL,
3529           G_CALLBACK (ev_window_cmd_view_zoom_in) },
3530         { "CtrlKpMinus", GTK_STOCK_ZOOM_OUT, NULL, "<control>KP_Subtract", NULL,
3531           G_CALLBACK (ev_window_cmd_view_zoom_out) },
3532 };
3533
3534 /* Toggle items */
3535 static const GtkToggleActionEntry toggle_entries[] = {
3536         /* View Menu */
3537         { "ViewToolbar", NULL, N_("_Toolbar"), NULL,
3538           N_("Show or hide the toolbar"),
3539           G_CALLBACK (ev_window_view_toolbar_cb), TRUE },
3540         { "ViewSidebar", GTK_STOCK_INDEX, N_("Side _Pane"), "F9",
3541           N_("Show or hide the side pane"),
3542           G_CALLBACK (ev_window_view_sidebar_cb), TRUE },
3543         { "ViewContinuous", EV_STOCK_VIEW_CONTINUOUS, N_("_Continuous"), NULL,
3544           N_("Show the entire document"),
3545           G_CALLBACK (ev_window_cmd_continuous), TRUE },
3546         { "ViewDual", EV_STOCK_VIEW_DUAL, N_("_Dual"), NULL,
3547           N_("Show two pages at once"),
3548           G_CALLBACK (ev_window_cmd_dual), FALSE },
3549         { "ViewFullscreen", NULL, N_("_Fullscreen"), "F11",
3550           N_("Expand the window to fill the screen"),
3551           G_CALLBACK (ev_window_cmd_view_fullscreen) },
3552         { "ViewPresentation", NULL, N_("_Presentation"), "F5",
3553           N_("Run document as a presentation"),
3554           G_CALLBACK (ev_window_cmd_view_presentation) },
3555         { "ViewBestFit", EV_STOCK_ZOOM_PAGE, N_("_Best Fit"), NULL,
3556           N_("Make the current document fill the window"),
3557           G_CALLBACK (ev_window_cmd_view_best_fit) },
3558         { "ViewPageWidth", EV_STOCK_ZOOM_WIDTH, N_("Fit Page _Width"), NULL,
3559           N_("Make the current document fill the window width"),
3560           G_CALLBACK (ev_window_cmd_view_page_width) },
3561 };
3562
3563 /* Popups specific items */
3564 static const GtkActionEntry view_popup_entries [] = {
3565         /* Links */
3566         { "OpenLink", GTK_STOCK_OPEN, N_("_Open Link"), NULL,
3567           NULL, G_CALLBACK (ev_view_popup_cmd_open_link) },
3568         { "GoLink", GTK_STOCK_GO_FORWARD, N_("_Go To"), NULL,
3569           NULL, G_CALLBACK (ev_view_popup_cmd_open_link) },
3570         { "CopyLinkAddress", NULL, N_("_Copy Link Address"), NULL,
3571           NULL,
3572           G_CALLBACK (ev_view_popup_cmd_copy_link_address) },
3573 };
3574
3575 static const GtkActionEntry attachment_popup_entries [] = {
3576         { "OpenAttachment", GTK_STOCK_OPEN, N_("_Open..."), NULL,
3577           NULL, G_CALLBACK (ev_attachment_popup_cmd_open_attachment) },
3578         { "SaveAttachmentAs", GTK_STOCK_SAVE_AS, N_("_Save a Copy..."), NULL,
3579           NULL, G_CALLBACK (ev_attachment_popup_cmd_save_attachment_as) },
3580 };
3581
3582 static void
3583 drag_data_received_cb (GtkWidget *widget, GdkDragContext *context,
3584                        gint x, gint y, GtkSelectionData *selection_data,
3585                        guint info, guint time, gpointer gdata)
3586 {
3587         GList  *uri_list = NULL;
3588         GSList *uris = NULL;
3589         gchar  *uri;
3590
3591         uri_list = gnome_vfs_uri_list_parse ((gchar *) selection_data->data);
3592
3593         if (uri_list) {
3594                 while (uri_list) {
3595                         uri = gnome_vfs_uri_to_string (uri_list->data, GNOME_VFS_URI_HIDE_NONE);
3596                         uris = g_slist_append (uris, (gpointer) uri);
3597                         
3598                         uri_list = g_list_next (uri_list);
3599                 }
3600
3601                 gnome_vfs_uri_list_free (uri_list);
3602                 
3603                 ev_application_open_uri_list (EV_APP, uris, 0);
3604                 
3605                 g_slist_free (uris);
3606         }
3607 }
3608
3609 static void
3610 activate_link_cb (EvPageAction *page_action, EvLink *link, EvWindow *window)
3611 {
3612         ev_view_handle_link (EV_VIEW (window->priv->view), link);
3613         gtk_widget_grab_focus (window->priv->view);
3614 }
3615
3616 static gboolean
3617 activate_label_cb (EvPageAction *page_action, char *label, EvWindow *window)
3618 {
3619         g_return_val_if_fail (EV_IS_WINDOW (window), FALSE);
3620
3621         gtk_widget_grab_focus (window->priv->view);
3622
3623         return ev_page_cache_set_page_label (window->priv->page_cache, label);
3624 }
3625
3626 static void
3627 register_custom_actions (EvWindow *window, GtkActionGroup *group)
3628 {
3629         GtkAction *action;
3630
3631         action = g_object_new (EV_TYPE_PAGE_ACTION,
3632                                "name", PAGE_SELECTOR_ACTION,
3633                                "label", _("Page"),
3634                                "tooltip", _("Select Page"),
3635                                "visible_overflown", FALSE,
3636                                NULL);
3637         g_signal_connect (action, "activate_link",
3638                           G_CALLBACK (activate_link_cb), window);
3639         g_signal_connect (action, "activate_label",
3640                           G_CALLBACK (activate_label_cb), window);
3641         gtk_action_group_add_action (group, action);
3642         g_object_unref (action);
3643
3644         action = g_object_new (EPHY_TYPE_ZOOM_ACTION,
3645                                "name", ZOOM_CONTROL_ACTION,
3646                                "label", _("Zoom"),
3647                                "stock_id", GTK_STOCK_ZOOM_IN,
3648                                "tooltip", _("Adjust the zoom level"),
3649                                "zoom", 1.0,
3650                                NULL);
3651         g_signal_connect (action, "zoom_to_level",
3652                           G_CALLBACK (zoom_control_changed_cb), window);
3653         gtk_action_group_add_action (group, action);
3654         g_object_unref (action);
3655 }
3656
3657 static void
3658 set_action_properties (GtkActionGroup *action_group)
3659 {
3660         GtkAction *action;
3661
3662         action = gtk_action_group_get_action (action_group, "GoPreviousPage");
3663         /*translators: this is the label for toolbar button*/
3664         g_object_set (action, "short_label", _("Previous"), NULL);
3665         g_object_set (action, "is-important", TRUE, NULL);
3666
3667         action = gtk_action_group_get_action (action_group, "GoNextPage");
3668         g_object_set (action, "is-important", TRUE, NULL);
3669         /*translators: this is the label for toolbar button*/
3670         g_object_set (action, "short_label", _("Next"), NULL);
3671
3672         action = gtk_action_group_get_action (action_group, "ViewZoomIn");
3673         /*translators: this is the label for toolbar button*/
3674         g_object_set (action, "short_label", _("Zoom In"), NULL);
3675         action = gtk_action_group_get_action (action_group, "ViewZoomIn");
3676
3677         action = gtk_action_group_get_action (action_group, "ViewZoomOut");
3678         /*translators: this is the label for toolbar button*/
3679         g_object_set (action, "short_label", _("Zoom Out"), NULL);
3680         action = gtk_action_group_get_action (action_group, "ViewZoomIn");
3681
3682         action = gtk_action_group_get_action (action_group, "ViewBestFit");
3683         /*translators: this is the label for toolbar button*/
3684         g_object_set (action, "short_label", _("Best Fit"), NULL);
3685         action = gtk_action_group_get_action (action_group, "ViewZoomIn");
3686
3687         action = gtk_action_group_get_action (action_group, "ViewPageWidth");
3688         /*translators: this is the label for toolbar button*/
3689         g_object_set (action, "short_label", _("Fit Width"), NULL);
3690         action = gtk_action_group_get_action (action_group, "ViewZoomIn");
3691
3692         action = gtk_action_group_get_action (action_group, "LeaveFullscreen");
3693         g_object_set (action, "is-important", TRUE, NULL);
3694 }
3695
3696 static void
3697 set_chrome_actions (EvWindow *window)
3698 {
3699         EvWindowPrivate *priv = window->priv;
3700         GtkActionGroup *action_group = priv->action_group;
3701         GtkAction *action;
3702
3703         action= gtk_action_group_get_action (action_group, "ViewToolbar");
3704         g_signal_handlers_block_by_func
3705                 (action, G_CALLBACK (ev_window_view_toolbar_cb), window);
3706         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
3707                                       (priv->chrome & EV_CHROME_TOOLBAR) != 0);
3708         g_signal_handlers_unblock_by_func
3709                 (action, G_CALLBACK (ev_window_view_toolbar_cb), window);
3710 }
3711
3712 static void
3713 sidebar_widget_model_set (EvSidebarLinks *ev_sidebar_links,
3714                           GParamSpec     *pspec,
3715                           EvWindow       *ev_window)
3716 {
3717         GtkTreeModel *model;
3718         GtkAction *action;
3719
3720         g_object_get (G_OBJECT (ev_sidebar_links),
3721                       "model", &model,
3722                       NULL);
3723
3724         action = gtk_action_group_get_action (ev_window->priv->action_group, PAGE_SELECTOR_ACTION);
3725         ev_page_action_set_model (EV_PAGE_ACTION (action), model);
3726         g_object_unref (model);
3727 }
3728
3729 static gboolean
3730 view_actions_focus_in_cb (GtkWidget *widget, GdkEventFocus *event, EvWindow *window)
3731 {
3732         update_chrome_flag (window, EV_CHROME_RAISE_TOOLBAR, FALSE);
3733         ev_window_set_action_sensitive (window, "ViewToolbar", TRUE);
3734
3735         ev_window_set_view_accels_sensitivity (window, TRUE);
3736
3737         return FALSE;
3738 }
3739
3740 static gboolean
3741 view_actions_focus_out_cb (GtkWidget *widget, GdkEventFocus *event, EvWindow *window)
3742 {
3743         ev_window_set_view_accels_sensitivity (window, FALSE);
3744
3745         return FALSE;
3746 }
3747
3748 static void
3749 sidebar_page_main_widget_update_cb (GObject *ev_sidebar_page,
3750                                     GParamSpec         *pspec,
3751                                     EvWindow           *ev_window)
3752 {
3753         GtkWidget *widget;
3754         
3755         g_object_get (ev_sidebar_page, "main_widget", &widget, NULL);
3756
3757         if (widget != NULL) {           
3758                 g_signal_connect_object (widget, "focus_in_event",
3759                                          G_CALLBACK (view_actions_focus_in_cb),
3760                                          ev_window, 0);
3761                 g_signal_connect_object (widget, "focus_out_event",
3762                                          G_CALLBACK (view_actions_focus_out_cb),
3763                                          ev_window, 0);
3764                 g_object_unref (widget);
3765         }
3766 }
3767
3768 static gboolean
3769 window_state_event_cb (EvWindow *window, GdkEventWindowState *event, gpointer dummy)
3770 {
3771         if (!(event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN)) {
3772                 gboolean maximized;
3773
3774                 maximized = event->new_window_state & GDK_WINDOW_STATE_MAXIMIZED;
3775                 if (!ev_window_is_empty (window))
3776                         ev_metadata_manager_set_boolean (window->priv->uri, "window_maximized", maximized);
3777         }
3778
3779         return FALSE;
3780 }
3781
3782 static gboolean
3783 window_configure_event_cb (EvWindow *window, GdkEventConfigure *event, gpointer dummy)
3784 {
3785         char *uri = window->priv->uri;
3786         GdkWindowState state;
3787         int x, y, width, height, document_width, document_height;
3788
3789         state = gdk_window_get_state (GTK_WIDGET (window)->window);
3790
3791         if (!(state & GDK_WINDOW_STATE_FULLSCREEN)) {
3792                 gtk_window_get_position (GTK_WINDOW (window), &x, &y);
3793                 gtk_window_get_size (GTK_WINDOW (window), &width, &height);
3794
3795                 if (!ev_window_is_empty (window) && window->priv->page_cache) {
3796                         ev_page_cache_get_max_width (window->priv->page_cache, 
3797                                                      0, 1.0,
3798                                                      &document_width);
3799                         ev_page_cache_get_max_height (window->priv->page_cache, 
3800                                                       0, 1.0,
3801                                                       &document_height);                        
3802                         ev_metadata_manager_set_int (uri, "window_x", x);
3803                         ev_metadata_manager_set_int (uri, "window_y", y);
3804                         ev_metadata_manager_set_double (uri, "window_width_ratio", 
3805                                                         (double)width/document_width);
3806                         ev_metadata_manager_set_double (uri, "window_height_ratio", 
3807                                                         (double)height/document_height);
3808                 }
3809         }
3810
3811         return FALSE;
3812 }
3813
3814 static void
3815 sidebar_links_link_activated_cb (EvSidebarLinks *sidebar_links, EvLink *link, EvWindow *window)
3816 {
3817         ev_view_handle_link (EV_VIEW (window->priv->view), link);
3818 }
3819
3820 static void
3821 launch_action (EvWindow *window, EvLinkAction *action)
3822 {
3823         const char *filename = ev_link_action_get_filename (action);
3824         char *uri = NULL;
3825
3826         if (filename  && g_path_is_absolute (filename)) {
3827                 uri = gnome_vfs_get_uri_from_local_path (filename);
3828         } else {
3829                 GnomeVFSURI *base_uri, *resolved_uri;
3830
3831                 base_uri = gnome_vfs_uri_new (window->priv->uri);
3832                 if (base_uri && filename) {
3833                         resolved_uri = gnome_vfs_uri_resolve_relative (base_uri, filename);     
3834                         if (resolved_uri) {
3835                                 uri = gnome_vfs_uri_to_string (resolved_uri, GNOME_VFS_URI_HIDE_NONE);
3836                                 gnome_vfs_uri_unref (resolved_uri);
3837                         }
3838                         gnome_vfs_uri_unref (base_uri);
3839                 }
3840         }
3841
3842         if (uri) {
3843                 gnome_vfs_url_show (uri);
3844         } else {
3845                 gnome_vfs_url_show (filename);
3846         }
3847
3848         g_free (uri);
3849
3850         /* According to the PDF spec filename can be an executable. I'm not sure
3851            allowing to launch executables is a good idea though. -- marco */
3852 }
3853
3854 static void
3855 launch_external_uri (EvWindow *window, EvLinkAction *action)
3856 {
3857         const char *uri;
3858         char *escaped;
3859
3860         uri = ev_link_action_get_uri (action);
3861         escaped = gnome_vfs_escape_host_and_path_string (uri);
3862
3863         gnome_vfs_url_show (escaped);
3864         g_free (escaped);
3865 }
3866
3867 static void
3868 open_remote_link (EvWindow *window, EvLinkAction *action)
3869 {
3870         gchar *uri;
3871         gchar *dir;
3872
3873         dir = g_path_get_dirname (window->priv->uri);
3874         
3875         uri = g_build_filename (dir, ev_link_action_get_filename (action),
3876                                 NULL);
3877         g_free (dir);
3878
3879         ev_application_open_uri_at_dest (EV_APP, uri,
3880                                          ev_link_action_get_dest (action),
3881                                          0,
3882                                          GDK_CURRENT_TIME);
3883
3884         g_free (uri);
3885 }
3886
3887 static void
3888 do_action_named (EvWindow *window, EvLinkAction *action)
3889 {
3890         const gchar *name = ev_link_action_get_name (action);
3891
3892         if (g_ascii_strcasecmp (name, "FirstPage") == 0) {
3893                 ev_window_cmd_go_first_page (NULL, window);
3894         } else if (g_ascii_strcasecmp (name, "PrevPage") == 0) {
3895                 ev_window_cmd_go_previous_page (NULL, window);
3896         } else if (g_ascii_strcasecmp (name, "NextPage") == 0) {
3897                 ev_window_cmd_go_next_page (NULL, window);
3898         } else if (g_ascii_strcasecmp (name, "LastPage") == 0) {
3899                 ev_window_cmd_go_last_page (NULL, window);
3900         } else if (g_ascii_strcasecmp (name, "GoToPage") == 0) {
3901                 ev_window_cmd_focus_page_selector (NULL, window);
3902         } else if (g_ascii_strcasecmp (name, "Find") == 0) {
3903                 ev_window_cmd_edit_find (NULL, window);
3904         } else if (g_ascii_strcasecmp (name, "Close") == 0) {
3905                 ev_window_cmd_file_close_window (NULL, window);
3906         } else {
3907                 g_warning ("Unimplemented named action: %s, please post a "
3908                            "bug report in Evince bugzilla "
3909                            "(http://bugzilla.gnome.org) with a testcase.",
3910                            name);
3911         }
3912 }
3913
3914 static void
3915 view_external_link_cb (EvView *view, EvLinkAction *action, EvWindow *window)
3916 {
3917         switch (ev_link_action_get_action_type (action)) {
3918                 case EV_LINK_ACTION_TYPE_EXTERNAL_URI:
3919                         launch_external_uri (window, action);
3920                         break;
3921                 case EV_LINK_ACTION_TYPE_LAUNCH:
3922                         launch_action (window, action);
3923                         break;
3924                 case EV_LINK_ACTION_TYPE_GOTO_REMOTE:
3925                         open_remote_link (window, action);
3926                         break;
3927                 case EV_LINK_ACTION_TYPE_NAMED:
3928                         do_action_named (window, action);
3929                         break;
3930                 default:
3931                         g_assert_not_reached ();
3932         }
3933 }
3934
3935 static void
3936 ev_view_popup_cmd_open_link (GtkAction *action, EvWindow *window)
3937 {
3938         ev_view_handle_link (EV_VIEW (window->priv->view), window->priv->link);
3939 }
3940
3941 static void
3942 ev_view_popup_cmd_copy_link_address (GtkAction *action, EvWindow *window)
3943 {
3944         GtkClipboard *clipboard;
3945         EvLinkAction *ev_action;
3946         const gchar *uri;
3947
3948         ev_action = ev_link_get_action (window->priv->link);
3949         if (!ev_action)
3950                 return;
3951
3952         uri = ev_link_action_get_uri (ev_action);
3953
3954         clipboard = gtk_widget_get_clipboard (GTK_WIDGET (window),
3955                                               GDK_SELECTION_CLIPBOARD);
3956         gtk_clipboard_set_text (clipboard, uri, -1);
3957 }
3958
3959 static void
3960 ev_attachment_popup_cmd_open_attachment (GtkAction *action, EvWindow *window)
3961 {
3962         GList *l;
3963         
3964         if (!window->priv->attach_list)
3965                 return;
3966
3967         for (l = window->priv->attach_list; l && l->data; l = g_list_next (l)) {
3968                 EvAttachment *attachment;
3969                 GError       *error = NULL;
3970                 
3971                 attachment = (EvAttachment *) l->data;
3972                 
3973                 ev_attachment_open (attachment, &error);
3974
3975                 if (error) {
3976                         ev_window_error_dialog (GTK_WINDOW (window),
3977                                                 _("Unable to open attachment"),
3978                                                 error);
3979                         g_error_free (error);
3980                 }
3981         }
3982 }
3983
3984 static void
3985 attachment_save_dialog_response_cb (GtkWidget *fc,
3986                                     gint       response_id,
3987                                     EvWindow  *ev_window)
3988 {
3989         gchar                *uri;
3990         GList                *l;
3991         GtkFileChooserAction  fc_action;
3992         gboolean              is_dir;
3993         
3994         if (response_id != GTK_RESPONSE_OK) {
3995                 gtk_widget_destroy (fc);
3996                 return;
3997         }
3998
3999         uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (fc));
4000         
4001         g_object_get (G_OBJECT (fc), "action", &fc_action, NULL);
4002         is_dir = (fc_action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
4003         
4004         for (l = ev_window->priv->attach_list; l && l->data; l = g_list_next (l)) {
4005                 EvAttachment *attachment;
4006                 gchar        *filename;
4007                 GError       *error = NULL;
4008                 
4009                 attachment = (EvAttachment *) l->data;
4010
4011                 if (is_dir) {
4012                         filename = g_strjoin ("/", uri,
4013                                               ev_attachment_get_name (attachment),
4014                                               NULL);
4015                 } else {
4016                         filename = g_strdup (uri);
4017                 }
4018                 
4019                 ev_attachment_save (attachment, filename, &error);
4020                 g_free (filename);
4021                 
4022                 if (error) {
4023                         ev_window_error_dialog (GTK_WINDOW (fc),
4024                                                 _("The attachment could not be saved."),
4025                                                 error);
4026                         g_error_free (error);
4027                 }
4028         }
4029
4030         g_free (uri);
4031
4032         gtk_widget_destroy (fc);
4033 }
4034
4035 static void
4036 ev_attachment_popup_cmd_save_attachment_as (GtkAction *action, EvWindow *window)
4037 {
4038         GtkWidget    *fc;
4039         EvAttachment *attachment = NULL;
4040
4041         if (!window->priv->attach_list)
4042                 return;
4043
4044         if (g_list_length (window->priv->attach_list) == 1)
4045                 attachment = (EvAttachment *) window->priv->attach_list->data;
4046         
4047         fc = gtk_file_chooser_dialog_new (
4048                 _("Save a Copy"),
4049                 GTK_WINDOW (window),
4050                 attachment ? GTK_FILE_CHOOSER_ACTION_SAVE : GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
4051                 GTK_STOCK_CANCEL,
4052                 GTK_RESPONSE_CANCEL,
4053                 GTK_STOCK_SAVE, GTK_RESPONSE_OK,
4054                 NULL);
4055
4056         gtk_dialog_set_default_response (GTK_DIALOG (fc), GTK_RESPONSE_OK);
4057         gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (fc), TRUE);
4058
4059         if (attachment)
4060                 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (fc),
4061                                                    ev_attachment_get_name (attachment));
4062
4063         g_signal_connect (fc, "response",
4064                           G_CALLBACK (attachment_save_dialog_response_cb),
4065                           window);
4066
4067         gtk_widget_show (fc);
4068 }
4069
4070 static void
4071 ev_window_init (EvWindow *ev_window)
4072 {
4073         GtkActionGroup *action_group;
4074         GtkAccelGroup *accel_group;
4075         GError *error = NULL;
4076         GtkWidget *sidebar_widget, *toolbar_dock;
4077
4078         g_signal_connect (ev_window, "configure_event",
4079                           G_CALLBACK (window_configure_event_cb), NULL);
4080         g_signal_connect (ev_window, "window_state_event",
4081                           G_CALLBACK (window_state_event_cb), NULL);
4082         g_signal_connect (ev_window, "notify",
4083                           G_CALLBACK (fullscreen_timeout_cb), NULL);
4084
4085         ev_window->priv = EV_WINDOW_GET_PRIVATE (ev_window);
4086
4087         ev_window->priv->page_mode = PAGE_MODE_DOCUMENT;
4088         ev_window->priv->title = ev_window_title_new (ev_window);
4089
4090         ev_window->priv->main_box = gtk_vbox_new (FALSE, 0);
4091         gtk_container_add (GTK_CONTAINER (ev_window), ev_window->priv->main_box);
4092         gtk_widget_show (ev_window->priv->main_box);
4093
4094         action_group = gtk_action_group_new ("MenuActions");
4095         ev_window->priv->action_group = action_group;
4096         gtk_action_group_set_translation_domain (action_group, NULL);
4097         gtk_action_group_add_actions (action_group, entries,
4098                                       G_N_ELEMENTS (entries), ev_window);
4099         gtk_action_group_add_toggle_actions (action_group, toggle_entries,
4100                                              G_N_ELEMENTS (toggle_entries),
4101                                              ev_window);
4102         set_action_properties (action_group);
4103         register_custom_actions (ev_window, action_group);
4104
4105         ev_window->priv->ui_manager = gtk_ui_manager_new ();
4106         gtk_ui_manager_insert_action_group (ev_window->priv->ui_manager,
4107                                             action_group, 0);
4108
4109         accel_group =
4110                 gtk_ui_manager_get_accel_group (ev_window->priv->ui_manager);
4111         gtk_window_add_accel_group (GTK_WINDOW (ev_window), accel_group);
4112
4113         ev_window_set_view_accels_sensitivity (ev_window, FALSE);
4114
4115         action_group = gtk_action_group_new ("ViewPopupActions");
4116         ev_window->priv->view_popup_action_group = action_group;
4117         gtk_action_group_set_translation_domain (action_group, NULL);
4118         gtk_action_group_add_actions (action_group, view_popup_entries,
4119                                       G_N_ELEMENTS (view_popup_entries),
4120                                       ev_window);
4121         gtk_ui_manager_insert_action_group (ev_window->priv->ui_manager,
4122                                             action_group, 0);
4123
4124         action_group = gtk_action_group_new ("AttachmentPopupActions");
4125         ev_window->priv->attachment_popup_action_group = action_group;
4126         gtk_action_group_set_translation_domain (action_group, NULL);
4127         gtk_action_group_add_actions (action_group, attachment_popup_entries,
4128                                       G_N_ELEMENTS (attachment_popup_entries),
4129                                       ev_window);
4130         gtk_ui_manager_insert_action_group (ev_window->priv->ui_manager,
4131                                             action_group, 0);
4132
4133         if (!gtk_ui_manager_add_ui_from_file (ev_window->priv->ui_manager,
4134                                               DATADIR"/evince-ui.xml",
4135                                               &error)) {
4136                 g_warning ("building menus failed: %s", error->message);
4137                 g_error_free (error);
4138         }
4139         
4140 #ifdef HAVE_GTK_RECENT
4141         ev_window->priv->recent_manager = gtk_recent_manager_get_for_screen (
4142                 gtk_widget_get_screen (GTK_WIDGET (ev_window)));
4143         ev_window->priv->recent_action_group = NULL;
4144         ev_window->priv->recent_ui_id = 0;
4145         g_signal_connect_swapped (ev_window->priv->recent_manager,
4146                                   "changed",
4147                                   G_CALLBACK (ev_window_setup_recent),
4148                                   ev_window);
4149 #endif /* HAVE_GTK_RECENT */
4150         
4151         ev_window->priv->menubar =
4152                  gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
4153                                             "/MainMenu");
4154         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box),
4155                             ev_window->priv->menubar,
4156                             FALSE, FALSE, 0);
4157
4158         /* This sucks, but there is no way to have a draw=no, expand=true separator
4159          * in a GtkUIManager-built toolbar. So, just add another toolbar.
4160          * See gtk+ bug 166489.
4161          */
4162         toolbar_dock = ev_window->priv->toolbar_dock = gtk_hbox_new (FALSE, 0);
4163         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), toolbar_dock,
4164                             FALSE, FALSE, 0);
4165         gtk_widget_show (toolbar_dock);
4166
4167         ev_window->priv->toolbar = egg_editable_toolbar_new_with_model
4168                                 (ev_window->priv->ui_manager, ev_application_get_toolbars_model (EV_APP), NULL);
4169         egg_editable_toolbar_show (EGG_EDITABLE_TOOLBAR (ev_window->priv->toolbar),
4170                                    "DefaultToolBar");
4171         gtk_box_pack_start (GTK_BOX (toolbar_dock), ev_window->priv->toolbar,
4172                             TRUE, TRUE, 0);
4173         gtk_widget_show (ev_window->priv->toolbar);
4174
4175         /* Add the main area */
4176         ev_window->priv->hpaned = gtk_hpaned_new ();
4177         g_signal_connect (ev_window->priv->hpaned,
4178                           "notify::position",
4179                           G_CALLBACK (ev_window_sidebar_position_change_cb),
4180                           ev_window);
4181         
4182         gtk_paned_set_position (GTK_PANED (ev_window->priv->hpaned), SIDEBAR_DEFAULT_SIZE);
4183         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), ev_window->priv->hpaned,
4184                             TRUE, TRUE, 0);
4185         gtk_widget_show (ev_window->priv->hpaned);
4186         
4187         ev_window->priv->sidebar = ev_sidebar_new ();
4188         gtk_paned_pack1 (GTK_PANED (ev_window->priv->hpaned),
4189                          ev_window->priv->sidebar, FALSE, FALSE);
4190         gtk_widget_show (ev_window->priv->sidebar);
4191
4192         /* Stub sidebar, for now */
4193
4194         sidebar_widget = ev_sidebar_links_new ();
4195         ev_window->priv->sidebar_links = sidebar_widget;
4196         g_signal_connect (sidebar_widget,
4197                           "notify::model",
4198                           G_CALLBACK (sidebar_widget_model_set),
4199                           ev_window);
4200         g_signal_connect (sidebar_widget,
4201                           "link_activated",
4202                           G_CALLBACK (sidebar_links_link_activated_cb),
4203                           ev_window);
4204         sidebar_page_main_widget_update_cb (G_OBJECT (sidebar_widget), NULL, ev_window);
4205         gtk_widget_show (sidebar_widget);
4206         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
4207                              sidebar_widget);
4208
4209         sidebar_widget = ev_sidebar_attachments_new ();
4210         ev_window->priv->sidebar_attachments = sidebar_widget;
4211         g_signal_connect_object (sidebar_widget,
4212                                  "popup",
4213                                  G_CALLBACK (attachment_bar_menu_popup_cb),
4214                                  ev_window, 0);
4215         gtk_widget_show (sidebar_widget);
4216         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
4217                              sidebar_widget);
4218
4219         sidebar_widget = ev_sidebar_thumbnails_new ();
4220         ev_window->priv->sidebar_thumbs = sidebar_widget;
4221         g_signal_connect (sidebar_widget,
4222                           "notify::main-widget",
4223                           G_CALLBACK (sidebar_page_main_widget_update_cb),
4224                           ev_window);
4225         sidebar_page_main_widget_update_cb (G_OBJECT (sidebar_widget), NULL, ev_window);
4226         gtk_widget_show (sidebar_widget);
4227         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
4228                              sidebar_widget);
4229
4230         ev_window->priv->scrolled_window =
4231                 GTK_WIDGET (g_object_new (GTK_TYPE_SCROLLED_WINDOW,
4232                                           "shadow-type", GTK_SHADOW_IN,
4233                                           NULL));
4234         gtk_widget_show (ev_window->priv->scrolled_window);
4235
4236         gtk_paned_add2 (GTK_PANED (ev_window->priv->hpaned),
4237                         ev_window->priv->scrolled_window);
4238
4239         ev_window->priv->view = ev_view_new ();
4240         ev_window->priv->password_view = ev_password_view_new ();
4241         g_signal_connect_swapped (ev_window->priv->password_view,
4242                                   "unlock",
4243                                   G_CALLBACK (ev_window_popup_password_dialog),
4244                                   ev_window);
4245         g_signal_connect_object (ev_window->priv->view, "focus_in_event",
4246                                  G_CALLBACK (view_actions_focus_in_cb),
4247                                  ev_window, 0);
4248         g_signal_connect_object (ev_window->priv->view, "focus_out_event",
4249                                  G_CALLBACK (view_actions_focus_out_cb),
4250                                  ev_window, 0);
4251         g_signal_connect_object (ev_window->priv->view, "external-link",
4252                                  G_CALLBACK (view_external_link_cb),
4253                                  ev_window, 0);
4254         g_signal_connect_object (ev_window->priv->view,
4255                                  "popup",
4256                                  G_CALLBACK (view_menu_popup_cb),
4257                                  ev_window, 0);
4258         gtk_widget_show (ev_window->priv->view);
4259         gtk_widget_show (ev_window->priv->password_view);
4260
4261         /* Find Bar */
4262         ev_window->priv->find_bar = egg_find_bar_new ();
4263         gtk_box_pack_end (GTK_BOX (ev_window->priv->main_box),
4264                           ev_window->priv->find_bar,
4265                           FALSE, TRUE, 0);
4266
4267         /* We own a ref on these widgets, as we can swap them in and out */
4268         g_object_ref (ev_window->priv->view);
4269         g_object_ref (ev_window->priv->password_view);
4270
4271         gtk_container_add (GTK_CONTAINER (ev_window->priv->scrolled_window),
4272                            ev_window->priv->view);
4273
4274         g_signal_connect (ev_window->priv->view,
4275                           "notify::find-status",
4276                           G_CALLBACK (view_find_status_changed_cb),
4277                           ev_window);
4278         g_signal_connect (ev_window->priv->view,
4279                           "notify::sizing-mode",
4280                           G_CALLBACK (ev_window_sizing_mode_changed_cb),
4281                           ev_window);
4282         g_signal_connect (ev_window->priv->view,
4283                           "notify::zoom",
4284                           G_CALLBACK (ev_window_zoom_changed_cb),
4285                           ev_window);
4286         g_signal_connect (ev_window->priv->view,
4287                           "notify::dual-page",
4288                           G_CALLBACK (ev_window_dual_mode_changed_cb),
4289                           ev_window);
4290         g_signal_connect (ev_window->priv->view,
4291                           "notify::continuous",
4292                           G_CALLBACK (ev_window_continuous_changed_cb),
4293                           ev_window);
4294         g_signal_connect (ev_window->priv->view,
4295                           "notify::rotation",
4296                           G_CALLBACK (ev_window_rotation_changed_cb),
4297                           ev_window);
4298         g_signal_connect (ev_window->priv->view,
4299                           "notify::has-selection",
4300                           G_CALLBACK (ev_window_has_selection_changed_cb),
4301                           ev_window);
4302
4303         /* Connect sidebar signals */
4304         g_signal_connect (ev_window->priv->sidebar,
4305                           "notify::visible",
4306                           G_CALLBACK (ev_window_sidebar_visibility_changed_cb),
4307                           ev_window);
4308         g_signal_connect (ev_window->priv->sidebar,
4309                           "notify::current-page",
4310                           G_CALLBACK (ev_window_sidebar_current_page_changed_cb),
4311                           ev_window);
4312
4313         /* Connect to find bar signals */
4314         g_signal_connect (ev_window->priv->find_bar,
4315                           "previous",
4316                           G_CALLBACK (find_bar_previous_cb),
4317                           ev_window);
4318         g_signal_connect (ev_window->priv->find_bar,
4319                           "next",
4320                           G_CALLBACK (find_bar_next_cb),
4321                           ev_window);
4322         g_signal_connect (ev_window->priv->find_bar,
4323                           "close",
4324                           G_CALLBACK (find_bar_close_cb),
4325                           ev_window);
4326         g_signal_connect (ev_window->priv->find_bar,
4327                           "notify::search-string",
4328                           G_CALLBACK (find_bar_search_changed_cb),
4329                           ev_window);
4330         g_signal_connect (ev_window->priv->find_bar,
4331                           "notify::case-sensitive",
4332                           G_CALLBACK (find_bar_search_changed_cb),
4333                           ev_window);
4334         g_signal_connect (ev_window->priv->find_bar,
4335                           "notify::visible",
4336                           G_CALLBACK (find_bar_search_changed_cb),
4337                           ev_window);
4338         g_signal_connect (ev_window->priv->find_bar,
4339                           "scroll",
4340                           G_CALLBACK (find_bar_scroll),
4341                           ev_window);
4342
4343         /* Popups */
4344         ev_window->priv->view_popup = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
4345                                                                  "/DocumentPopup");
4346         ev_window->priv->link = NULL;
4347
4348         ev_window->priv->attachment_popup = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
4349                                                                        "/AttachmentPopup");
4350         ev_window->priv->attach_list = NULL;
4351
4352         /* Give focus to the document view */
4353         gtk_widget_grab_focus (ev_window->priv->view);
4354
4355         /* Drag and Drop */
4356         gtk_drag_dest_unset (GTK_WIDGET (ev_window->priv->view));
4357         gtk_drag_dest_set (GTK_WIDGET (ev_window->priv->view),
4358                            GTK_DEST_DEFAULT_ALL,
4359                            ev_drop_types,
4360                            sizeof (ev_drop_types) / sizeof (ev_drop_types[0]),
4361                            GDK_ACTION_COPY);
4362         g_signal_connect_swapped (G_OBJECT (ev_window->priv->view), "drag-data-received",
4363                                   G_CALLBACK (drag_data_received_cb),
4364                                   ev_window);
4365         
4366         /* Set it user interface params */
4367
4368         ev_window_setup_recent (ev_window);
4369
4370         setup_chrome_from_metadata (ev_window);
4371         set_chrome_actions (ev_window);
4372         update_chrome_visibility (ev_window);
4373
4374         gtk_window_set_default_size (GTK_WINDOW (ev_window), 600, 600);
4375
4376         setup_view_from_metadata (ev_window);
4377         setup_sidebar_from_metadata (ev_window, NULL);
4378
4379         ev_window_sizing_mode_changed_cb (EV_VIEW (ev_window->priv->view), NULL, ev_window);
4380         ev_window_setup_action_sensitivity (ev_window);
4381 }
4382
4383 GtkWidget *
4384 ev_window_new (void)
4385 {
4386         GtkWidget *ev_window;
4387
4388         ev_window = GTK_WIDGET (g_object_new (EV_TYPE_WINDOW,
4389                                               "type", GTK_WINDOW_TOPLEVEL,
4390                                               NULL));
4391
4392         return ev_window;
4393 }