]> www.fi.muni.cz Git - evince.git/blob - shell/ev-window.c
2c7676fe613ad7b2d9493438901aa2cb256c92c9
[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 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-sidebar.h"
33 #include "ev-sidebar-bookmarks.h"
34 #include "ev-sidebar-thumbnails.h"
35 #include "ev-view.h"
36 #include "ev-print-job.h"
37 #include "ev-document-find.h"
38 #include "eggfindbar.h"
39
40 #include "pdf-document.h"
41 #include "pixbuf-document.h"
42 #include "gtkgs.h"
43
44 #include <glib/gi18n.h>
45 #include <gtk/gtk.h>
46 #include <libgnomevfs/gnome-vfs-mime-utils.h>
47 #include <libgnomeprintui/gnome-print-dialog.h>
48
49 #include <string.h>
50
51 #include "ev-application.h"
52 #include "ev-stock-icons.h"
53
54 enum {
55         PROP_0,
56         PROP_ATTRIBUTE
57 };
58
59 enum {
60         SIGNAL,
61         N_SIGNALS
62 };
63
64 struct _EvWindowPrivate {
65         GtkWidget *main_box;
66         GtkWidget *hpaned;
67         GtkWidget *sidebar;
68         GtkWidget *find_bar;
69         GtkWidget *view;
70         GtkActionGroup *action_group;
71         GtkUIManager *ui_manager;
72         GtkWidget *statusbar;
73         guint help_message_cid;
74         GtkWidget *exit_fullscreen_popup;
75
76         EvDocument *document;
77
78         gboolean fullscreen_mode;
79 };
80
81 #if 0
82 /* enable these to add support for signals */
83 static guint ev_window_signals [N_SIGNALS] = { 0 };
84 #endif
85
86 static void update_fullscreen_popup (EvWindow *window);
87
88 static GObjectClass *parent_class = NULL;
89
90 G_DEFINE_TYPE (EvWindow, ev_window, GTK_TYPE_WINDOW)
91
92 #define EV_WINDOW_GET_PRIVATE(object) \
93         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_WINDOW, EvWindowPrivate))
94
95 #if 0
96 const char *
97 ev_window_get_attribute (EvWindow *self)
98 {
99         g_return_val_if_fail (self != NULL && EV_IS_WINDOW (self), NULL);
100         
101         return self->priv->attribute;
102 }
103
104 void
105 ev_window_set_attribute (EvWindow* self, const char *attribute)
106 {
107         g_assert (self != NULL && EV_IS_WINDOW (self));
108         g_assert (attribute != NULL);
109
110         if (self->priv->attribute != NULL) {
111                 g_free (self->priv->attribute);
112         }
113
114         self->priv->attribute = g_strdup (attribute);
115
116         g_object_notify (G_OBJECT (self), "attribute");
117 }
118
119 static void
120 ev_window_get_property (GObject *object, guint prop_id, GValue *value,
121                         GParamSpec *param_spec)
122 {
123         EvWindow *self;
124
125         self = EV_WINDOW (object);
126
127         switch (prop_id) {
128         case PROP_ATTRIBUTE:
129                 g_value_set_string (value, self->priv->attribute);
130                 break;
131         default:
132                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
133                                                    prop_id,
134                                                    param_spec);
135                 break;
136         }
137 }
138
139 static void
140 ev_window_set_property (GObject *object, guint prop_id, const GValue *value,
141                         GParamSpec *param_spec)
142 {
143         EvWindow *self;
144         
145         self = EV_WINDOW (object);
146         
147         switch (prop_id) {
148         case PROP_ATTRIBUTE:
149                 ev_window_set_attribute (self, g_value_get_string (value));
150                 break;
151         default:
152                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
153                                                    prop_id,
154                                                    param_spec);
155                 break;
156         }
157 }
158 #endif
159
160 static void
161 set_action_sensitive (EvWindow   *ev_window,
162                       const char *name,
163                       gboolean    sensitive)
164 {
165         GtkAction *action = gtk_action_group_get_action (ev_window->priv->action_group,
166                                                          name);
167         gtk_action_set_sensitive (action, sensitive);
168 }
169
170 static void
171 update_action_sensitivity (EvWindow *ev_window)
172 {
173         int n_pages;
174         int page;
175
176         if (ev_window->priv->document)
177                 n_pages = ev_document_get_n_pages (ev_window->priv->document);
178         else
179                 n_pages = 1;
180
181         page = ev_view_get_page (EV_VIEW (ev_window->priv->view));
182
183         set_action_sensitive (ev_window, "GoFirstPage", page > 1);
184         set_action_sensitive (ev_window, "GoPreviousPage", page > 1);
185         set_action_sensitive (ev_window, "GoNextPage", page < n_pages);
186         set_action_sensitive (ev_window, "GoLastPage", page < n_pages);
187 }
188
189 gboolean
190 ev_window_is_empty (const EvWindow *ev_window)
191 {
192         g_return_val_if_fail (EV_IS_WINDOW (ev_window), FALSE);
193         
194         return ev_window->priv->document == NULL;
195 }
196
197 static void
198 unable_to_load (EvWindow   *ev_window,
199                 const char *error_message)
200 {
201         GtkWidget *dialog;
202
203         dialog = gtk_message_dialog_new (GTK_WINDOW (ev_window),
204                                          GTK_DIALOG_DESTROY_WITH_PARENT,
205                                          GTK_MESSAGE_ERROR,
206                                          GTK_BUTTONS_CLOSE,
207                                          _("Unable to open document"));
208         gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
209                                                   "%s", error_message);
210         gtk_dialog_run (GTK_DIALOG (dialog));
211         gtk_widget_destroy (dialog);
212 }
213
214 /* Would be nice to have this in gdk-pixbuf */
215 static gboolean
216 mime_type_supported_by_gdk_pixbuf (const gchar *mime_type)
217 {
218         GSList *formats, *list;
219         gboolean retval = FALSE;
220         
221         formats = gdk_pixbuf_get_formats ();
222         
223         list = formats;
224         while (list) {
225                 GdkPixbufFormat *format = list->data;
226                 int i;
227                 gchar **mime_types;
228                 
229                 if (gdk_pixbuf_format_is_disabled (format))
230                         continue;
231
232                 mime_types = gdk_pixbuf_format_get_mime_types (format);
233                 
234                 for (i = 0; mime_types[i] != NULL; i++) {
235                         if (strcmp (mime_types[i], mime_type) == 0) {
236                                 retval = TRUE;
237                                 break;
238                         }
239                 }
240                 
241                 if (retval)
242                         break;
243                 
244                 list = list->next;
245         }
246         
247         g_slist_free (formats);
248
249         return retval;
250 }
251
252 void
253 ev_window_open (EvWindow *ev_window, const char *uri)
254 {
255         EvDocument *document = NULL;
256         char *mime_type;
257
258         mime_type = gnome_vfs_get_mime_type (uri);
259
260         if (!strcmp (mime_type, "application/pdf"))
261                 document = g_object_new (PDF_TYPE_DOCUMENT, NULL);
262         else if (!strcmp (mime_type, "application/postscript"))
263                 document = g_object_new (GTK_GS_TYPE, NULL);
264         else if (mime_type_supported_by_gdk_pixbuf (mime_type))
265                 document = g_object_new (PIXBUF_TYPE_DOCUMENT, NULL);
266         
267         if (document) {
268                 GError *error = NULL;
269
270                 if (ev_document_load (document, uri, &error)) {
271                         if (ev_window->priv->document)
272                                 g_object_unref (ev_window->priv->document);
273                         ev_window->priv->document = document;
274
275                         ev_view_set_document (EV_VIEW (ev_window->priv->view),
276                                               document);
277                         ev_sidebar_set_document (EV_SIDEBAR (ev_window->priv->sidebar),
278                                                  document);
279
280                         update_action_sensitivity (ev_window);
281                 
282                 } else {
283                         g_assert (error != NULL);
284                         g_object_unref (document);
285                         unable_to_load (ev_window, error->message);
286                         g_error_free (error);
287                 }
288         } else {
289                 char *error_message;
290
291                 error_message = g_strdup_printf (_("Unhandled MIME type: '%s'"),
292                                                  mime_type);
293                 unable_to_load (ev_window, error_message);
294                 g_free (error_message);
295         }
296
297         g_free (mime_type);
298 }
299
300 static void
301 ev_window_cmd_file_open (GtkAction *action, EvWindow *ev_window)
302 {
303         ev_application_open (EV_APP, NULL);
304 }
305
306 static gboolean
307 using_postscript_printer (GnomePrintConfig *config)
308 {
309         const guchar *driver;
310         const guchar *transport;
311
312         driver = gnome_print_config_get (
313                 config, (const guchar *)"Settings.Engine.Backend.Driver");
314         
315         transport = gnome_print_config_get (
316                 config, (const guchar *)"Settings.Transport.Backend");
317         
318         if (driver) {
319                 if (!strcmp ((const gchar *)driver, "gnome-print-ps"))
320                         return TRUE;
321                 else 
322                         return FALSE;
323         } else  if (transport) {
324                 if (!strcmp ((const gchar *)transport, "CUPS"))
325                         return TRUE;
326         }
327         
328         return FALSE;
329 }
330
331 static void
332 ev_window_print (EvWindow *ev_window)
333 {
334         GnomePrintConfig *config;
335         GnomePrintJob *job;
336         GtkWidget *print_dialog;
337         EvPrintJob *print_job = NULL;
338
339         g_return_if_fail (EV_IS_WINDOW (ev_window));
340         g_return_if_fail (ev_window->priv->document != NULL);
341
342         config = gnome_print_config_default ();
343         job = gnome_print_job_new (config);
344
345         print_dialog = gnome_print_dialog_new (job, _("Print"),
346                                                (GNOME_PRINT_DIALOG_RANGE |
347                                                 GNOME_PRINT_DIALOG_COPIES));
348         gtk_dialog_set_response_sensitive (GTK_DIALOG (print_dialog),
349                                            GNOME_PRINT_DIALOG_RESPONSE_PREVIEW,
350                                            FALSE);
351         
352         while (TRUE) {
353                 int response;
354                 response = gtk_dialog_run (GTK_DIALOG (print_dialog));
355                 
356                 if (response != GNOME_PRINT_DIALOG_RESPONSE_PRINT)
357                         break;
358
359                 /* FIXME: Change this when we have the first backend
360                  * that can print more than postscript
361                  */
362                 if (!using_postscript_printer (config)) {
363                         GtkWidget *dialog;
364                         
365                         dialog = gtk_message_dialog_new (
366                                 GTK_WINDOW (print_dialog), GTK_DIALOG_MODAL,
367                                 GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
368                                 _("Printing is not supported on this printer."));
369                         gtk_message_dialog_format_secondary_text (
370                                 GTK_MESSAGE_DIALOG (dialog),
371                                 _("You were trying to print to a printer using the \"%s\" driver. This program requires a PostScript printer driver."),
372                                 gnome_print_config_get (
373                                         config, "Settings.Engine.Backend.Driver"));
374                         gtk_dialog_run (GTK_DIALOG (dialog));
375                         gtk_widget_destroy (dialog);
376
377                         continue;
378                 }
379
380                 print_job = g_object_new (EV_TYPE_PRINT_JOB,
381                                           "gnome_print_job", job,
382                                           "document", ev_window->priv->document,
383                                           "print_dialog", print_dialog,
384                                           NULL);
385                 break;
386         }
387                                 
388         gtk_widget_destroy (print_dialog);
389
390         if (print_job != NULL)
391                 ev_print_job_print (print_job, GTK_WINDOW (ev_window));
392 }
393
394 static void
395 ev_window_cmd_file_print (GtkAction *action, EvWindow *ev_window)
396 {
397         ev_window_print (ev_window);
398 }
399
400 static void
401 ev_window_cmd_file_close_window (GtkAction *action, EvWindow *ev_window)
402 {
403         g_return_if_fail (EV_IS_WINDOW (ev_window));
404
405         gtk_widget_destroy (GTK_WIDGET (ev_window));
406 }
407
408 static void
409 find_not_supported_dialog (EvWindow   *ev_window)
410 {
411         GtkWidget *dialog;
412
413         /* If you change this so it isn't modal, be sure you don't
414          * allow multiple copies of the dialog...
415          */
416         
417         dialog = gtk_message_dialog_new (GTK_WINDOW (ev_window),
418                                          GTK_DIALOG_DESTROY_WITH_PARENT,
419                                          GTK_MESSAGE_ERROR,
420                                          GTK_BUTTONS_CLOSE,
421                                          _("The \"Find\" feature will not work with this document"));
422         gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
423                                                   _("Searching for text is only supported for PDF documents."));
424         gtk_dialog_run (GTK_DIALOG (dialog));
425         gtk_widget_destroy (dialog);
426 }
427
428 static void
429 ev_window_cmd_edit_find (GtkAction *action, EvWindow *ev_window)
430 {
431         g_return_if_fail (EV_IS_WINDOW (ev_window));
432
433         if (ev_window->priv->document == NULL) {
434                 g_printerr ("We should have set the Find menu item insensitive since there's no document\n");
435         } else if (!EV_IS_DOCUMENT_FIND (ev_window->priv->document)) {
436                 find_not_supported_dialog (ev_window);
437         } else {
438                 gtk_widget_show (ev_window->priv->find_bar);
439
440                 if (ev_window->priv->exit_fullscreen_popup) 
441                         update_fullscreen_popup (ev_window);
442         
443                 egg_find_bar_grab_focus (EGG_FIND_BAR (ev_window->priv->find_bar));
444         }
445 }
446
447 static void
448 ev_window_cmd_edit_copy (GtkAction *action, EvWindow *ev_window)
449 {
450         g_return_if_fail (EV_IS_WINDOW (ev_window));
451
452         /* FIXME */
453 }
454
455 static void
456 update_fullscreen_popup (EvWindow *window)
457 {
458         GtkWidget *popup = window->priv->exit_fullscreen_popup;
459         int popup_width, popup_height;
460         GdkRectangle screen_rect;
461
462         g_return_if_fail (popup != NULL);
463
464         if (!popup)
465                 return;
466         
467         popup_width = popup->requisition.width;
468         popup_height = popup->requisition.height;
469
470         /* FIXME multihead */
471         gdk_screen_get_monitor_geometry (gdk_screen_get_default (),
472                         gdk_screen_get_monitor_at_window
473                         (gdk_screen_get_default (),
474                          GTK_WIDGET (window)->window),
475                          &screen_rect);
476
477         if (GTK_WIDGET_VISIBLE (window->priv->find_bar)) {
478                 GtkRequisition req;
479
480                 gtk_widget_size_request (window->priv->find_bar, &req);
481                 
482                 screen_rect.height -= req.height;
483         }
484         
485         if (gtk_widget_get_direction (popup) == GTK_TEXT_DIR_RTL)
486         {
487                 gtk_window_move (GTK_WINDOW (popup),
488                                  screen_rect.x + screen_rect.width - popup_width,
489                                  screen_rect.height - popup_height);
490         }
491         else
492         {
493                 gtk_window_move (GTK_WINDOW (popup),
494                                 screen_rect.x, screen_rect.height - popup_height);
495         }
496 }
497
498 static void
499 screen_size_changed_cb (GdkScreen *screen,
500                         EvWindow *window)
501 {
502         update_fullscreen_popup (window);
503 }
504
505 static void
506 destroy_exit_fullscreen_popup (EvWindow *window)
507 {
508         if (window->priv->exit_fullscreen_popup != NULL)
509         {
510                 /* FIXME multihead */
511                 g_signal_handlers_disconnect_by_func
512                         (gdk_screen_get_default (),
513                          G_CALLBACK (screen_size_changed_cb), window);
514
515                 gtk_widget_destroy (window->priv->exit_fullscreen_popup);
516                 window->priv->exit_fullscreen_popup = NULL;
517         }
518 }
519
520 static void
521 exit_fullscreen_button_clicked_cb (GtkWidget *button, EvWindow *window)
522 {
523         GtkAction *action;
524
525         action = gtk_action_group_get_action (window->priv->action_group, "ViewFullscreen");
526         g_return_if_fail (action != NULL);
527
528         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), FALSE);
529 }
530
531 static void
532 fullscreen_popup_size_request_cb (GtkWidget *popup, GtkRequisition *req, EvWindow *window)
533 {
534         update_fullscreen_popup (window);
535 }
536
537 static void
538 ev_window_fullscreen (EvWindow *window)
539 {
540         GtkWidget *popup, *button, *icon, *label, *hbox, *main_menu;
541
542         window->priv->fullscreen_mode = TRUE;
543
544         popup = gtk_window_new (GTK_WINDOW_POPUP);
545         window->priv->exit_fullscreen_popup = popup;
546
547         button = gtk_button_new ();
548         g_signal_connect (button, "clicked",
549                           G_CALLBACK (exit_fullscreen_button_clicked_cb),
550                           window);
551         gtk_widget_show (button);
552         gtk_container_add (GTK_CONTAINER (popup), button);
553
554         hbox = gtk_hbox_new (FALSE, 2);
555         gtk_widget_show (hbox);
556         gtk_container_add (GTK_CONTAINER (button), hbox);
557
558         icon = gtk_image_new_from_stock (GTK_STOCK_QUIT, GTK_ICON_SIZE_BUTTON);
559         gtk_widget_show (icon);
560         gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);
561
562         label = gtk_label_new (_("Exit Fullscreen"));
563         gtk_widget_show (label);
564         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
565
566         gtk_window_set_resizable (GTK_WINDOW (popup), FALSE);
567
568         /* FIXME multihead */
569         g_signal_connect (gdk_screen_get_default (), "size-changed",
570                           G_CALLBACK (screen_size_changed_cb), window);
571         g_signal_connect (popup, "size_request",
572                           G_CALLBACK (fullscreen_popup_size_request_cb), window);
573
574         main_menu = gtk_ui_manager_get_widget (window->priv->ui_manager, "/MainMenu");
575         gtk_widget_hide (main_menu);
576         gtk_widget_hide (window->priv->statusbar);
577         
578         update_fullscreen_popup (window);
579
580         gtk_widget_show (popup);
581 }
582
583 static void
584 ev_window_unfullscreen (EvWindow *window)
585 {
586         GtkWidget *main_menu;
587         
588         window->priv->fullscreen_mode = FALSE;
589
590         main_menu = gtk_ui_manager_get_widget (window->priv->ui_manager, "/MainMenu");
591         gtk_widget_show (main_menu);
592         gtk_widget_show (window->priv->statusbar);
593         
594         destroy_exit_fullscreen_popup (window);
595 }
596  
597 static void
598 ev_window_cmd_view_fullscreen (GtkAction *action, EvWindow *ev_window)
599 {
600         gboolean fullscreen;
601         
602         g_return_if_fail (EV_IS_WINDOW (ev_window));
603
604         fullscreen = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
605
606         if (fullscreen) {
607                 gtk_window_fullscreen (GTK_WINDOW (ev_window));
608         } else {
609                 gtk_window_unfullscreen (GTK_WINDOW (ev_window));
610         }
611 }
612
613 static gboolean
614 ev_window_state_event_cb (GtkWidget *widget, GdkEventWindowState *event, EvWindow *window)
615 {
616         if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
617         {
618                 GtkActionGroup *action_group;
619                 GtkAction *action;
620                 gboolean fullscreen;
621
622                 fullscreen = event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN;
623
624                 if (fullscreen)
625                 {
626                         ev_window_fullscreen (window);
627                 }
628                 else
629                 {
630                         ev_window_unfullscreen (window);
631                 }
632
633                 action_group = window->priv->action_group;
634
635                 action = gtk_action_group_get_action (action_group, "ViewFullscreen");
636                 g_signal_handlers_block_by_func
637                         (action, G_CALLBACK (ev_window_cmd_view_fullscreen), window);
638                 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), fullscreen);
639                 g_signal_handlers_unblock_by_func
640                         (action, G_CALLBACK (ev_window_cmd_view_fullscreen), window);
641
642         }
643
644         return FALSE;
645 }
646
647 static gboolean
648 ev_window_focus_out_cb (GtkWidget *widget, GdkEventFocus *event, EvWindow *ev_window)
649 {
650         gtk_window_unfullscreen (GTK_WINDOW (ev_window));
651         
652         return FALSE;
653 }
654
655
656 static void
657 ev_window_cmd_view_zoom_in (GtkAction *action, EvWindow *ev_window)
658 {
659         g_return_if_fail (EV_IS_WINDOW (ev_window));
660
661         ev_view_zoom_in (EV_VIEW (ev_window->priv->view));
662 }
663
664 static void
665 ev_window_cmd_view_zoom_out (GtkAction *action, EvWindow *ev_window)
666 {
667         g_return_if_fail (EV_IS_WINDOW (ev_window));
668
669         ev_view_zoom_out (EV_VIEW (ev_window->priv->view));
670 }
671
672 static void
673 ev_window_cmd_view_normal_size (GtkAction *action, EvWindow *ev_window)
674 {
675         g_return_if_fail (EV_IS_WINDOW (ev_window));
676
677         ev_view_normal_size (EV_VIEW (ev_window->priv->view));
678 }
679
680 static void
681 ev_window_cmd_view_best_fit (GtkAction *action, EvWindow *ev_window)
682 {
683         g_return_if_fail (EV_IS_WINDOW (ev_window));
684
685         ev_view_best_fit (EV_VIEW (ev_window->priv->view));
686 }
687
688 static void
689 ev_window_cmd_view_page_width (GtkAction *action, EvWindow *ev_window)
690 {
691         g_return_if_fail (EV_IS_WINDOW (ev_window));
692
693         ev_view_fit_width (EV_VIEW (ev_window->priv->view));
694 }
695
696 static void
697 ev_window_cmd_go_back (GtkAction *action, EvWindow *ev_window)
698 {
699         g_return_if_fail (EV_IS_WINDOW (ev_window));
700
701         /* FIXME */
702 }
703
704 static void
705 ev_window_cmd_go_forward (GtkAction *action, EvWindow *ev_window)
706 {
707         g_return_if_fail (EV_IS_WINDOW (ev_window));
708
709         /* FIXME */
710 }
711
712 static void
713 ev_window_cmd_go_previous_page (GtkAction *action, EvWindow *ev_window)
714 {
715         g_return_if_fail (EV_IS_WINDOW (ev_window));
716
717         ev_view_set_page (EV_VIEW (ev_window->priv->view),
718                           ev_view_get_page (EV_VIEW (ev_window->priv->view)) - 1);
719 }
720
721 static void
722 ev_window_cmd_go_next_page (GtkAction *action, EvWindow *ev_window)
723 {
724         g_return_if_fail (EV_IS_WINDOW (ev_window));
725
726         ev_view_set_page (EV_VIEW (ev_window->priv->view),
727                           ev_view_get_page (EV_VIEW (ev_window->priv->view)) + 1);
728 }
729
730 static void
731 ev_window_cmd_go_first_page (GtkAction *action, EvWindow *ev_window)
732 {
733         g_return_if_fail (EV_IS_WINDOW (ev_window));
734
735         ev_view_set_page (EV_VIEW (ev_window->priv->view), 1);
736 }
737
738 static void
739 ev_window_cmd_go_last_page (GtkAction *action, EvWindow *ev_window)
740 {
741         g_return_if_fail (EV_IS_WINDOW (ev_window));
742
743         ev_view_set_page (EV_VIEW (ev_window->priv->view), G_MAXINT);
744 }
745
746 static void
747 ev_window_cmd_help_contents (GtkAction *action, EvWindow *ev_window)
748 {
749         g_return_if_fail (EV_IS_WINDOW (ev_window));
750
751         /* FIXME */
752 }
753
754 static void
755 ev_window_cmd_help_about (GtkAction *action, EvWindow *ev_window)
756 {
757         const char *authors[] = {
758                 N_("Many..."),
759                 NULL
760         };
761
762         const char *documenters[] = {
763                 N_("Not so many..."),
764                 NULL
765         };
766
767         const char *license[] = {
768                 N_("Evince is free software; you can redistribute it and/or modify\n"
769                    "it under the terms of the GNU General Public License as published by\n"
770                    "the Free Software Foundation; either version 2 of the License, or\n"
771                    "(at your option) any later version.\n"),            
772                 N_("Evince is distributed in the hope that it will be useful,\n"
773                    "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
774                    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
775                    "GNU General Public License for more details.\n"),
776                 N_("You should have received a copy of the GNU General Public License\n"
777                    "along with Evince; if not, write to the Free Software Foundation, Inc.,\n"
778                    "59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n")
779         };
780
781         char *license_trans;
782
783 #ifdef ENABLE_NLS
784         const char **p;
785
786         for (p = authors; *p; ++p)
787                 *p = _(*p);
788
789         for (p = documenters; *p; ++p)
790                 *p = _(*p);
791 #endif
792
793         license_trans = g_strconcat (_(license[0]), "\n", _(license[1]), "\n",
794                                      _(license[2]), "\n", NULL);
795
796         gtk_show_about_dialog (
797                 GTK_WINDOW (ev_window),
798                 "name", _("Evince"),
799                 "version", VERSION,
800                 "copyright",
801                 _("\xc2\xa9 1996-2004 The Evince authors"),
802                 "license", license_trans,
803                 "website", "http://www.gnome.org/projects/evince",
804                 "comments", _("PostScript and PDF File Viewer."),
805                 "authors", authors,
806                 "documenters", documenters,
807                 "translator-credits", _("translator-credits"),
808                 NULL);
809
810         g_free (license_trans);
811 }
812
813 static void
814 ev_window_view_toolbar_cb (GtkAction *action, EvWindow *ev_window)
815 {
816         g_object_set (
817                 G_OBJECT (gtk_ui_manager_get_widget (
818                                   ev_window->priv->ui_manager,
819                                   "/ToolBar")),
820                 "visible",
821                 gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)),
822                 NULL);
823 }
824
825 static void
826 ev_window_view_statusbar_cb (GtkAction *action, EvWindow *ev_window)
827 {
828         g_object_set (
829                 ev_window->priv->statusbar,
830                 "visible",
831                 gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)),
832                 NULL);
833 }
834
835 static void
836 ev_window_view_sidebar_cb (GtkAction *action, EvWindow *ev_window)
837 {
838         g_object_set (
839                 ev_window->priv->sidebar,
840                 "visible",
841                 gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)),
842                 NULL);
843 }
844
845 static void
846 menu_item_select_cb (GtkMenuItem *proxy, EvWindow *ev_window)
847 {
848         GtkAction *action;
849         char *message;
850
851         action = g_object_get_data (G_OBJECT (proxy), "gtk-action");
852         g_return_if_fail (action != NULL);
853         
854         g_object_get (G_OBJECT (action), "tooltip", &message, NULL);
855         if (message) {
856                 gtk_statusbar_push (GTK_STATUSBAR (ev_window->priv->statusbar),
857                                     ev_window->priv->help_message_cid, message);
858                 g_free (message);
859         }
860 }
861
862 static void
863 menu_item_deselect_cb (GtkMenuItem *proxy, EvWindow *ev_window)
864 {
865         gtk_statusbar_pop (GTK_STATUSBAR (ev_window->priv->statusbar),
866                            ev_window->priv->help_message_cid);
867 }
868
869 static void
870 connect_proxy_cb (GtkUIManager *ui_manager, GtkAction *action,
871                   GtkWidget *proxy, EvWindow *ev_window)
872 {
873         if (GTK_IS_MENU_ITEM (proxy)) {
874                 g_signal_connect (proxy, "select",
875                                   G_CALLBACK (menu_item_select_cb), ev_window);
876                 g_signal_connect (proxy, "deselect",
877                                   G_CALLBACK (menu_item_deselect_cb),
878                                   ev_window);
879         }
880 }
881
882 static void
883 disconnect_proxy_cb (GtkUIManager *ui_manager, GtkAction *action,
884                      GtkWidget *proxy, EvWindow *ev_window)
885 {
886         if (GTK_IS_MENU_ITEM (proxy)) {
887                 g_signal_handlers_disconnect_by_func
888                         (proxy, G_CALLBACK (menu_item_select_cb), ev_window);
889                 g_signal_handlers_disconnect_by_func
890                         (proxy, G_CALLBACK (menu_item_deselect_cb), ev_window);
891         }
892 }
893
894 static void
895 view_page_changed_cb (EvView   *view,
896                       EvWindow *ev_window)
897 {
898         update_action_sensitivity (ev_window);
899 }
900
901 static void
902 view_find_status_changed_cb (EvView   *view,
903                              EvWindow *ev_window)
904 {
905         char *text;
906
907         text = ev_view_get_find_status_message (view);
908
909         egg_find_bar_set_status_text (EGG_FIND_BAR (ev_window->priv->find_bar),
910                                       text);
911
912         g_free (text);
913 }
914
915 static void
916 find_bar_previous_cb (EggFindBar *find_bar,
917                       EvWindow   *ev_window)
918 {
919         /* FIXME - highlight previous result */
920         g_printerr ("Find Previous\n");
921
922 }
923
924 static void
925 find_bar_next_cb (EggFindBar *find_bar,
926                   EvWindow   *ev_window)
927 {
928         /* FIXME - highlight next result */
929         g_printerr ("Find Next\n");
930 }
931
932 static void
933 find_bar_close_cb (EggFindBar *find_bar,
934                    EvWindow   *ev_window)
935 {
936         gtk_widget_hide (ev_window->priv->find_bar);
937
938         if (ev_window->priv->exit_fullscreen_popup)
939                 update_fullscreen_popup (ev_window);
940 }
941
942 static void
943 find_bar_search_changed_cb (EggFindBar *find_bar,
944                             GParamSpec *param,
945                             EvWindow   *ev_window)
946 {
947         gboolean case_sensitive;
948         gboolean visible;
949         const char *search_string;
950
951         g_return_if_fail (EV_IS_WINDOW (ev_window));
952         
953         /* Either the string or case sensitivity could have changed,
954          * we connect this callback to both. We also connect it
955          * to ::visible so when the find bar is hidden, we should
956          * pretend the search string is NULL/""
957          */
958
959         case_sensitive = egg_find_bar_get_case_sensitive (find_bar);
960         visible = GTK_WIDGET_VISIBLE (find_bar);
961         search_string = egg_find_bar_get_search_string (find_bar);
962         
963 #if 0
964         g_printerr ("search for '%s'\n", search_string ? search_string : "(nil)");
965 #endif
966
967         if (ev_window->priv->document &&
968             EV_IS_DOCUMENT_FIND (ev_window->priv->document)) {
969                 if (visible && search_string) {
970                         ev_document_find_begin (EV_DOCUMENT_FIND (ev_window->priv->document), search_string, case_sensitive);
971                 } else {
972                         ev_document_find_cancel (EV_DOCUMENT_FIND (ev_window->priv->document));
973                         egg_find_bar_set_status_text (EGG_FIND_BAR (ev_window->priv->find_bar),
974                                                       NULL);
975                 }
976         }
977 }
978
979 static void
980 ev_window_dispose (GObject *object)
981 {
982         EvWindowPrivate *priv;
983
984         g_return_if_fail (object != NULL && EV_IS_WINDOW (object));
985
986         priv = EV_WINDOW (object)->priv;
987
988         if (priv->ui_manager) {
989                 g_object_unref (priv->ui_manager);
990                 priv->ui_manager = NULL;
991         }
992
993         if (priv->action_group) {
994                 g_object_unref (priv->action_group);
995                 priv->action_group = NULL;
996         }
997         
998         G_OBJECT_CLASS (parent_class)->dispose (object);
999 }
1000
1001 static void
1002 ev_window_class_init (EvWindowClass *ev_window_class)
1003 {
1004         GObjectClass *g_object_class;
1005
1006         parent_class = g_type_class_peek_parent (ev_window_class);
1007
1008         g_object_class = G_OBJECT_CLASS (ev_window_class);
1009         g_object_class->dispose = ev_window_dispose;
1010
1011         g_type_class_add_private (g_object_class, sizeof (EvWindowPrivate));
1012
1013 #if 0
1014         /* setting up signal system */
1015         ev_window_class->signal = ev_window_signal;
1016
1017         ev_window_signals [SIGNAL] = g_signal_new (
1018                 "signal",
1019                 EV_TYPE_WINDOW,
1020                 G_SIGNAL_RUN_LAST,
1021                 G_STRUCT_OFFSET (EvWindowClass,
1022                                  signal),
1023                 NULL,
1024                 NULL,
1025                 g_cclosure_marshal_VOID__STRING,
1026                 G_TYPE_NONE,
1027                 0);
1028         /* setting up property system */
1029         g_object_class->set_property = ev_window_set_property;
1030         g_object_class->get_property = ev_window_get_property;
1031
1032         g_object_class_install_property (
1033                 g_object_class,
1034                 PROP_ATTRIBUTE,
1035                 g_param_spec_string ("attribute",
1036                                      "Attribute",
1037                                      "A simple unneccessary attribute that "
1038                                      "does nothing special except being a "
1039                                      "demonstration for the correct implem"
1040                                      "entation of a GObject property",
1041                                      "default_value",
1042                                      G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
1043 #endif
1044 }
1045
1046 /* Normal items */
1047 static GtkActionEntry entries[] = {
1048         { "File", NULL, N_("_File") },
1049         { "Edit", NULL, N_("_Edit") },
1050         { "View", NULL, N_("_View") },
1051         { "Go", NULL, N_("_Go") },
1052         { "Help", NULL, N_("_Help") },
1053
1054         /* File menu */
1055         { "FileOpen", GTK_STOCK_OPEN, N_("_Open"), "<control>O",
1056           N_("Open a file"),
1057           G_CALLBACK (ev_window_cmd_file_open) },
1058         { "FilePrint", GTK_STOCK_PRINT, N_("_Print"), "<control>P",
1059           N_("Print this document"),
1060           G_CALLBACK (ev_window_cmd_file_print) },
1061         { "FileCloseWindow", GTK_STOCK_CLOSE, N_("_Close"), "<control>W",
1062           N_("Close this window"),
1063           G_CALLBACK (ev_window_cmd_file_close_window) },
1064
1065         /* Edit menu */
1066         { "EditCopy", GTK_STOCK_COPY, N_("_Copy"), "<control>C",
1067           N_("Copy text from the document"),
1068           G_CALLBACK (ev_window_cmd_edit_copy) },
1069         
1070         { "EditFind", GTK_STOCK_FIND, N_("_Find"), "<control>F",
1071           N_("Find a word or phrase in the document"),
1072           G_CALLBACK (ev_window_cmd_edit_find) },
1073
1074         /* View menu */
1075         { "ViewZoomIn", GTK_STOCK_ZOOM_IN, N_("Zoom _In"), "<control>plus",
1076           N_("Enlarge the document"),
1077           G_CALLBACK (ev_window_cmd_view_zoom_in) },
1078         { "ViewZoomOut", GTK_STOCK_ZOOM_OUT, N_("Zoom _Out"), "<control>minus",
1079           N_("Shrink the document"),
1080           G_CALLBACK (ev_window_cmd_view_zoom_out) },
1081         { "ViewNormalSize", GTK_STOCK_ZOOM_100, N_("_Normal Size"), "<control>0",
1082           N_("Zoom to the normal size"),
1083           G_CALLBACK (ev_window_cmd_view_normal_size) },
1084         { "ViewBestFit", GTK_STOCK_ZOOM_FIT, N_("_Best Fit"), NULL,
1085           N_("Zoom to fit the document to the current window"),
1086           G_CALLBACK (ev_window_cmd_view_best_fit) },
1087         { "ViewPageWidth", EV_STOCK_ZOOM_FIT_WIDTH, N_("Fit Page _Width"), NULL,
1088           N_("Zoom to fit the width of the current window "),
1089           G_CALLBACK (ev_window_cmd_view_page_width) },
1090
1091         /* Go menu */
1092         { "GoBack", GTK_STOCK_GO_BACK, N_("_Back"), "<mod1>Left",
1093           N_("Go to the page viewed before this one"),
1094           G_CALLBACK (ev_window_cmd_go_back) },
1095         { "GoForward", GTK_STOCK_GO_FORWARD, N_("Fo_rward"), "<mod1>Right",
1096           N_("Go to the page viewed before this one"),
1097           G_CALLBACK (ev_window_cmd_go_forward) },
1098         { "GoPreviousPage", GTK_STOCK_GO_BACK, N_("_Previous Page"), "<control>Page_Up",
1099           N_("Go to the previous page"),
1100           G_CALLBACK (ev_window_cmd_go_previous_page) },
1101         { "GoNextPage", GTK_STOCK_GO_FORWARD, N_("_Next Page"), "<control>Page_Down",
1102           N_("Go to the next page"),
1103           G_CALLBACK (ev_window_cmd_go_next_page) },
1104         { "GoFirstPage", GTK_STOCK_GOTO_FIRST, N_("_First Page"), "<control>Home",
1105           N_("Go to the first page"),
1106           G_CALLBACK (ev_window_cmd_go_first_page) },        
1107         { "GoLastPage", GTK_STOCK_GOTO_LAST, N_("_Last Page"), "<control>End",
1108           N_("Go to the last page"),
1109           G_CALLBACK (ev_window_cmd_go_last_page) },
1110         
1111         /* Help menu */
1112         { "HelpContents", GTK_STOCK_HELP, N_("_Contents"), NULL,
1113           N_("Display help for the viewer application"),
1114           G_CALLBACK (ev_window_cmd_help_contents) },
1115         
1116         { "HelpAbout", GTK_STOCK_ABOUT, N_("_About"), NULL,
1117           N_("Display credits for the document viewer creators"),
1118           G_CALLBACK (ev_window_cmd_help_about) },
1119 };
1120
1121 /* Toggle items */
1122 static GtkToggleActionEntry toggle_entries[] = {
1123         /* View Menu */
1124         { "ViewToolbar", NULL, N_("_Toolbar"), "<shift><control>T",
1125           N_("Show or hide toolbar"),
1126           G_CALLBACK (ev_window_view_toolbar_cb), TRUE },
1127         { "ViewStatusbar", NULL, N_("_Statusbar"), NULL,
1128           N_("Show or hide statusbar"),
1129           G_CALLBACK (ev_window_view_statusbar_cb), TRUE },
1130         { "ViewSidebar", NULL, N_("Side_bar"), "F9",
1131           N_("Show or hide sidebar"),
1132           G_CALLBACK (ev_window_view_sidebar_cb), TRUE },
1133         { "ViewFullscreen", NULL, N_("_Fullscreen"), "F11",
1134           N_("Expand the window to fill the screen"),
1135           G_CALLBACK (ev_window_cmd_view_fullscreen) },
1136 };
1137
1138 static void
1139 ev_window_init (EvWindow *ev_window)
1140 {
1141         GtkActionGroup *action_group;
1142         GtkAccelGroup *accel_group;
1143         GError *error = NULL;
1144         GtkWidget *scrolled_window;
1145         GtkWidget *menubar;
1146         GtkWidget *toolbar;
1147         GtkWidget *sidebar_widget;
1148
1149         ev_window->priv = EV_WINDOW_GET_PRIVATE (ev_window);
1150
1151         gtk_window_set_title (GTK_WINDOW (ev_window), _("Document Viewer"));
1152
1153         ev_window->priv->main_box = gtk_vbox_new (FALSE, 0);
1154         gtk_container_add (GTK_CONTAINER (ev_window), ev_window->priv->main_box);
1155         gtk_widget_show (ev_window->priv->main_box);
1156         
1157         action_group = gtk_action_group_new ("MenuActions");
1158         ev_window->priv->action_group = action_group;
1159         gtk_action_group_set_translation_domain (action_group, NULL);
1160         gtk_action_group_add_actions (action_group, entries,
1161                                       G_N_ELEMENTS (entries), ev_window);
1162         gtk_action_group_add_toggle_actions (action_group, toggle_entries,
1163                                              G_N_ELEMENTS (toggle_entries),
1164                                              ev_window);
1165
1166         ev_window->priv->ui_manager = gtk_ui_manager_new ();
1167         gtk_ui_manager_insert_action_group (ev_window->priv->ui_manager,
1168                                             action_group, 0);
1169
1170         accel_group =
1171                 gtk_ui_manager_get_accel_group (ev_window->priv->ui_manager);
1172         gtk_window_add_accel_group (GTK_WINDOW (ev_window), accel_group);
1173
1174         g_signal_connect (ev_window->priv->ui_manager, "connect_proxy",
1175                           G_CALLBACK (connect_proxy_cb), ev_window);
1176         g_signal_connect (ev_window->priv->ui_manager, "disconnect_proxy",
1177                           G_CALLBACK (disconnect_proxy_cb), ev_window);
1178
1179         if (!gtk_ui_manager_add_ui_from_file (ev_window->priv->ui_manager,
1180                                               DATADIR"/evince-ui.xml",
1181                                               &error)) {
1182                 g_message ("building menus failed: %s", error->message);
1183                 g_error_free (error);
1184         }
1185
1186         menubar = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
1187                                              "/MainMenu");
1188         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), menubar,
1189                             FALSE, FALSE, 0);
1190
1191         toolbar = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
1192                                              "/ToolBar");
1193         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), toolbar,
1194                             FALSE, FALSE, 0);
1195
1196         /* Add the main area */
1197         ev_window->priv->hpaned = gtk_hpaned_new ();
1198         gtk_widget_show (ev_window->priv->hpaned);
1199         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), ev_window->priv->hpaned,
1200                             TRUE, TRUE, 0);
1201
1202         ev_window->priv->sidebar = ev_sidebar_new ();
1203         gtk_widget_show (ev_window->priv->sidebar);
1204         gtk_paned_add1 (GTK_PANED (ev_window->priv->hpaned),
1205                         ev_window->priv->sidebar);
1206
1207         /* Stub sidebar, for now */
1208         sidebar_widget = ev_sidebar_bookmarks_new ();
1209         gtk_widget_show (sidebar_widget);
1210         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
1211                              "bookmarks",
1212                              _("Bookmarks"),
1213                              sidebar_widget);
1214
1215         sidebar_widget = ev_sidebar_thumbnails_new ();
1216         gtk_widget_show (sidebar_widget);
1217         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
1218                              "thumbnails",
1219                              _("Thumbnails"),
1220                              sidebar_widget);
1221         
1222         scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1223         gtk_widget_show (scrolled_window);
1224         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
1225                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1226
1227         gtk_paned_add2 (GTK_PANED (ev_window->priv->hpaned),
1228                         scrolled_window);
1229
1230         ev_window->priv->view = ev_view_new ();
1231         gtk_widget_show (ev_window->priv->view);
1232         gtk_container_add (GTK_CONTAINER (scrolled_window),
1233                            ev_window->priv->view);
1234         g_signal_connect (ev_window->priv->view,
1235                           "page-changed",
1236                           G_CALLBACK (view_page_changed_cb),
1237                           ev_window);
1238         g_signal_connect (ev_window->priv->view,
1239                           "find-status-changed",
1240                           G_CALLBACK (view_find_status_changed_cb),
1241                           ev_window);
1242         
1243         ev_window->priv->statusbar = gtk_statusbar_new ();
1244         gtk_widget_show (ev_window->priv->statusbar);
1245         gtk_box_pack_end (GTK_BOX (ev_window->priv->main_box),
1246                           ev_window->priv->statusbar,
1247                           FALSE, TRUE, 0);
1248         ev_window->priv->help_message_cid = gtk_statusbar_get_context_id
1249                 (GTK_STATUSBAR (ev_window->priv->statusbar), "help_message");
1250
1251         ev_window->priv->find_bar = egg_find_bar_new ();
1252         gtk_box_pack_end (GTK_BOX (ev_window->priv->main_box),
1253                           ev_window->priv->find_bar,
1254                           FALSE, TRUE, 0);
1255         
1256         /* Connect to find bar signals */
1257         g_signal_connect (ev_window->priv->find_bar,
1258                           "previous",
1259                           G_CALLBACK (find_bar_previous_cb),
1260                           ev_window);
1261         g_signal_connect (ev_window->priv->find_bar,
1262                           "next",
1263                           G_CALLBACK (find_bar_next_cb),
1264                           ev_window);
1265         g_signal_connect (ev_window->priv->find_bar,
1266                           "close",
1267                           G_CALLBACK (find_bar_close_cb),
1268                           ev_window);
1269         g_signal_connect (ev_window->priv->find_bar,
1270                           "notify::search-string",
1271                           G_CALLBACK (find_bar_search_changed_cb),
1272                           ev_window);
1273         g_signal_connect (ev_window->priv->find_bar,
1274                           "notify::case-sensitive",
1275                           G_CALLBACK (find_bar_search_changed_cb),
1276                           ev_window);
1277         g_signal_connect (ev_window->priv->find_bar,
1278                           "notify::visible",
1279                           G_CALLBACK (find_bar_search_changed_cb),
1280                           ev_window);
1281
1282         g_signal_connect (ev_window, "window-state-event",
1283                           G_CALLBACK (ev_window_state_event_cb),
1284                           ev_window);
1285         g_signal_connect (ev_window, "focus_out_event",
1286                           G_CALLBACK (ev_window_focus_out_cb),
1287                           ev_window);
1288         
1289         /* Give focus to the scrolled window */
1290         gtk_widget_grab_focus (scrolled_window);
1291         
1292         update_action_sensitivity (ev_window);
1293