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