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