]> www.fi.muni.cz Git - evince.git/blob - shell/ev-dualscreen.c
83ef7c71fb7079d2dd17f2ec1f44863e1de648ac
[evince.git] / shell / ev-dualscreen.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) 2007 Johannes Buchner
5  *
6  *  Author:
7  *    Johannes Buchner <buchner.johannes@gmx.at>
8  *    Lukas Bezdicka <255993@mail.muni.cz>
9  *
10  * Evince is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * Evince is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <glib/gi18n.h>
30 #include <gtk/gtk.h>
31
32 #include "ev-dualscreen.h"
33 #include "ev-window.h"
34 #include "ev-view.h"
35 #include "ev-view-presentation.h"
36 #include "ev-utils.h"
37 #include "ev-sidebar.h"
38 #include "ev-sidebar-thumbnails.h"
39
40 struct _EvDSCWindowPrivate {
41         GtkWidget       *main_box;
42         GtkWidget       *menubar;
43         GtkWidget       *sidebar;
44         GtkWidget       *notesview;
45         EvDocumentModel *model;
46         EvDocumentModel *notes_model;
47         EvDocument      *presentation_document;
48         EvDocument      *notes_document;
49
50         GtkWidget       *presentation_window;
51         GtkWidget *overview_scrolled_window;
52         GtkWidget *notesview_scrolled_window;
53
54         EvViewPresentation *presentation_view;
55         gint            moveback_monitor;
56 };
57
58 #define EV_DSCWINDOW_GET_PRIVATE(object) \
59         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_DSCWINDOW, EvDSCWindowPrivate))
60 #define PAGE_CACHE_SIZE 52428800 /* 50MB */
61 #define SIDEBAR_DEFAULT_SIZE    132
62
63
64 G_DEFINE_TYPE (EvDSCWindow, ev_dscwindow, GTK_TYPE_WINDOW)
65
66 /**
67  * ev_dscwindow_switch_monitors: Bring the presentation window and the control window each on the other monitor.
68  *
69  * @widget: Callback widget
70  * @self: EvDSCWindow
71  **/
72 static gboolean
73 ev_dscwindow_switch_monitors (GtkWidget *widget, EvDSCWindow *ev_dscwindow)
74 {
75
76         if (!EV_IS_DSCWINDOW (ev_dscwindow))
77                 return FALSE;
78
79         gint num_monitors = get_num_monitors (GTK_WINDOW (ev_dscwindow));
80
81         if (num_monitors == 2) {
82                 GtkWindow *presentation_window = GTK_WINDOW (ev_dscwindow->priv->presentation_window);
83                 GdkScreen *screen = gtk_window_get_screen (presentation_window);
84
85                 gint monitor_1 = gdk_screen_get_monitor_at_window (screen,
86                         gtk_widget_get_window (GTK_WIDGET (presentation_window)));
87
88                 gint monitor_2 = (monitor_1 + 1) % 2;
89
90                 GdkRectangle coords;
91                 gdk_screen_get_monitor_geometry (screen, monitor_2, &coords);
92                 gtk_window_move (presentation_window, coords.x, coords.y);
93                 ev_dscwindow->priv->moveback_monitor = monitor_1;
94
95                 gdk_screen_get_monitor_geometry (screen, monitor_1, &coords);
96                 gtk_window_unmaximize (GTK_WINDOW (ev_dscwindow));
97                 gtk_window_move (GTK_WINDOW (ev_dscwindow), coords.x, coords.y);
98                 gtk_window_maximize (GTK_WINDOW (ev_dscwindow));
99         }
100         return TRUE;
101 }
102
103 /**
104  * ev_dscwindow_notes_interaction: User wants to load a different file as notes.
105  **/
106 static gboolean
107 ev_dscwindow_notes_interaction (GtkContainer *container, EvDSCWindow *self)
108 {
109 /*
110 +       EvDSCWindowPrivate *priv = EV_DSCWINDOW_GET_PRIVATE (self);
111 +       GtkWidget *dialog;
112 +
113 +       dialog = gtk_file_chooser_dialog_new (
114 +               _("Open Document"),
115 +               GTK_WINDOW (self),
116 +               GTK_FILE_CHOOSER_ACTION_OPEN,
117 +               GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
118 +               GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
119 +               NULL);
120 +
121 +       ev_document_factory_add_filters (dialog, NULL);
122 +       gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dialog), FALSE);
123 +       gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (dialog), TRUE);
124 +
125 +       if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
126 +       {
127 +               char * uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dialog));
128 +               GError * error = NULL;
129 +               ev_view_set_loading (EV_VIEW (priv->notesview), TRUE);
130 +
131 +               if (priv->notesdocument) {
132 +                       ev_document_load (priv->notesdocument, uri, &error);
133 +               } else {
134 +                       priv->notesdocument = ev_document_factory_get_document (uri,
135 +                               &error);
136 +               }
137 +               g_free (uri);
138 +               if (error == NULL){
139 +                       ev_view_set_document (EV_VIEW (priv->notesview),
140 +                               priv->notesdocument);
141 +                       /* TODO: go to the same page that is open at the moment, or
142 +                        * move the presentation to the beginning. *//*
143 +               }
144 +       }
145 +       gtk_widget_destroy (dialog);
146 */
147         return TRUE;
148 }
149
150 /**
151  * ev_dscwindow_get_control: Get the control instance.
152  *
153  * If there is none, create one.
154  *
155  * Returns: control instance
156  **/
157 EvDSCWindow *
158 ev_dscwindow_get_control (void)
159 {
160         static EvDSCWindow * control = NULL;
161
162         if (!control || !EV_IS_DSCWINDOW (control)) {
163                 control = EV_DSCWINDOW (g_object_new (EV_TYPE_DSCWINDOW, NULL));
164         }
165
166         return control;
167 }
168
169 /*TODO: Fix me!*/
170 static void
171 ev_dscwindow_window_placement (EvDSCWindow *ev_dscwindow)
172 {
173         gint num_monitors = get_num_monitors (GTK_WINDOW (ev_dscwindow));
174         //if (num_monitors == 2) {
175                 GtkWindow * presentation_window = GTK_WINDOW (ev_dscwindow->priv->presentation_window);
176                 GdkScreen * screen = gtk_window_get_screen (presentation_window);
177                 gint work_monitor = gdk_screen_get_monitor_at_window (screen,
178                         gtk_widget_get_window (GTK_WIDGET (presentation_window)));
179                 gint presentation_monitor = (work_monitor + 1) % 2;
180                 GdkRectangle coords;
181                 gdk_screen_get_monitor_geometry (screen, presentation_monitor,
182                         &coords);
183
184                 gtk_window_move (presentation_window, coords.x, coords.y);
185                 //ev_window_run_presentation (ev_dscwindow->priv->presentation_window);
186                 ev_dscwindow->priv->moveback_monitor = work_monitor;
187                 gtk_window_maximize (GTK_WINDOW (ev_dscwindow));
188         //}
189 }
190 /**
191  * ev_dscwindow_page_changed_cb: Callback to change page on all views
192  *
193  **/
194 static void
195 ev_dscwindow_page_changed_cb (EvDocumentModel *model,
196                            GParamSpec      *pspec,
197                            EvDSCWindow     *ev_dscwindow)
198 {
199         gint page = ev_document_model_get_page (model);
200         if(page != ev_view_presentation_get_current_page (ev_dscwindow->priv->presentation_view))
201                 ev_view_presentation_set_page (EV_VIEW_PRESENTATION(ev_dscwindow->priv->presentation_view), page);
202 }
203 static void
204 ev_dscwindow_presentation_page_changed_cb (EvViewPresentation *pview,
205                            GParamSpec      *pspec,
206                            EvDSCWindow     *ev_dscwindow)
207 {
208         gint page = ev_view_presentation_get_current_page (pview);
209         ev_document_model_set_page (ev_dscwindow->priv->model, page);
210 }
211
212 static gboolean
213 ev_dscwindow_notes_clicked (GtkWidget *widget, GdkEvent *event,
214         EvDSCWindow *self)
215 {
216         if (!self || !EV_IS_DSCWINDOW (self) || !event)
217                 return FALSE;
218         gint page = ev_document_model_get_page (self->priv->model);
219
220         if (event->type == GDK_2BUTTON_PRESS && ((GdkEventButton *)event)->button == 1) {
221                 ev_dscwindow_notes_interaction (NULL, self);
222         } else if (event->type == GDK_BUTTON_PRESS && ((GdkEventButton *)event)->button == 1) {
223                 ev_document_model_set_page (self->priv->model, page+1);
224         } else if (event->type == GDK_BUTTON_PRESS && ((GdkEventButton *)event)->button == 3) {
225                 ev_document_model_set_page (self->priv->model, page-1);
226         }
227         return FALSE;
228 }
229
230 /**
231  * ev_dscwindow_set_presentation: Set presentation document
232  * @presentation_window: Main window we can reuse for presentation
233  **/
234 void
235 ev_dscwindow_set_presentation (EvDSCWindow *ev_dscwindow,
236         EvWindow *presentation_window, EvDocument *document, EvViewPresentation *pview)
237 {
238         if (!EV_IS_WINDOW (presentation_window))
239                 return;
240
241         ev_dscwindow->priv->presentation_window = GTK_WIDGET(presentation_window);
242         ev_dscwindow->priv->presentation_document = document;
243         ev_dscwindow->priv->presentation_view = EV_VIEW_PRESENTATION(pview);
244         ev_document_model_set_document(ev_dscwindow->priv->model,
245                 document);
246         g_signal_connect_swapped (ev_dscwindow->priv->presentation_view,
247                                   "destroy",
248                                   G_CALLBACK (gtk_widget_destroy),
249                                   ev_dscwindow);
250         g_signal_connect (G_OBJECT(ev_dscwindow->priv->presentation_view),
251                           "notify::page",
252                           G_CALLBACK (ev_dscwindow_presentation_page_changed_cb),
253                           ev_dscwindow);
254         ev_dscwindow_window_placement (ev_dscwindow);
255 }
256
257 /**
258  * ev_dscwindow_end: Destroy control window but keep presentation
259  */
260 static gboolean
261 ev_dscwindow_end (GtkWidget *widget, GdkEvent *event)
262 {
263         gtk_widget_destroy (GTK_WIDGET (ev_dscwindow_get_control ()));
264         return TRUE;
265 }
266
267 /**
268  * ev_dscwindow_init: Initialize multihead presentation
269 *
270 * @ev_dscwindow: EvDSCWindow.
271 *
272 * ev_dscwindow_set_presentation has to be called afterwards for loading in a document. TBD
273 **/
274 static void
275 ev_dscwindow_init (EvDSCWindow *ev_dscwindow)
276 {
277         ev_dscwindow->priv = EV_DSCWINDOW_GET_PRIVATE (ev_dscwindow);
278         ev_dscwindow->priv->moveback_monitor = -1;
279         ev_dscwindow->priv->notes_document = NULL;
280
281         gtk_window_set_title (GTK_WINDOW (ev_dscwindow), _("Presentation Control"));
282
283         GtkWidget *h = gtk_hpaned_new ();
284         GtkWidget *v = gtk_vbox_new (FALSE, 0);
285
286         ev_dscwindow->priv->model = ev_document_model_new ();
287         g_signal_connect (G_OBJECT(ev_dscwindow->priv->model),
288                           "notify::page",
289                           G_CALLBACK (ev_dscwindow_page_changed_cb),
290                           ev_dscwindow);
291         ev_dscwindow->priv->sidebar = ev_sidebar_new ();
292         ev_sidebar_set_model (EV_SIDEBAR (ev_dscwindow->priv->sidebar),
293                               ev_dscwindow->priv->model);
294         gtk_box_pack_start (GTK_BOX (v), ev_dscwindow->priv->sidebar, TRUE , TRUE, 0);
295         gtk_widget_show (ev_dscwindow->priv->sidebar);
296
297         GtkWidget *sidebar_widget;
298         sidebar_widget = ev_sidebar_thumbnails_new ();
299
300         gtk_widget_show (sidebar_widget);
301         ev_sidebar_add_page (EV_SIDEBAR (ev_dscwindow->priv->sidebar),
302                              sidebar_widget);
303
304         ev_document_model_set_continuous (ev_dscwindow->priv->model, FALSE);
305         ev_document_model_set_dual_page (ev_dscwindow->priv->model, FALSE);
306         ev_document_model_set_sizing_mode (ev_dscwindow->priv->model, EV_SIZING_BEST_FIT);
307
308         GtkWidget *e = gtk_expander_new (_("Expensive features"));
309         gtk_expander_set_expanded (GTK_EXPANDER (e), TRUE);
310
311         GtkWidget *t = gtk_toolbar_new ();
312
313         GtkToolItem* b_switch = gtk_tool_button_new (NULL, _("Switch monitors"));
314         gtk_tool_button_set_icon_name (GTK_TOOL_BUTTON (b_switch), "object-flip-horizontal");
315         gtk_toolbar_insert (GTK_TOOLBAR (t), b_switch, -1);
316         g_signal_connect (b_switch, "clicked",
317                 G_CALLBACK (ev_dscwindow_switch_monitors), ev_dscwindow);
318
319         GtkToolItem* b_notes = gtk_tool_button_new (NULL, _("Load notes ..."));
320         gtk_tool_button_set_icon_name (GTK_TOOL_BUTTON (b_notes), "object-flip-horizontal");
321         gtk_toolbar_insert (GTK_TOOLBAR (t), b_notes, -1);
322         g_signal_connect (b_notes, "clicked",
323                 G_CALLBACK (ev_dscwindow_notes_interaction), ev_dscwindow);
324
325         GtkToolItem* b_close = gtk_tool_button_new (NULL, _("End presentation"));
326         gtk_tool_button_set_icon_name (GTK_TOOL_BUTTON (b_close), "view-restore");
327         gtk_toolbar_insert (GTK_TOOLBAR (t), b_close, -1);
328         /*TODO: rework close*/
329         g_signal_connect (b_close, "clicked",
330                 G_CALLBACK (ev_dscwindow_end), NULL);
331
332         gtk_container_add (GTK_CONTAINER (e), t);
333         gtk_box_pack_end (GTK_BOX (v), e, FALSE, TRUE, 0);
334         gtk_paned_add1 (GTK_PANED (h), v);
335
336         ev_dscwindow->priv->notesview_scrolled_window = GTK_WIDGET (g_object_new (
337                 GTK_TYPE_SCROLLED_WINDOW, "shadow-type", GTK_SHADOW_IN, NULL));
338         gtk_paned_add2 (GTK_PANED (h), ev_dscwindow->priv->notesview_scrolled_window);
339
340         ev_dscwindow->priv->notesview = ev_view_new ();
341         g_object_ref (ev_dscwindow->priv->notesview);
342         gtk_container_add (GTK_CONTAINER (ev_dscwindow->priv->notesview_scrolled_window),
343                 ev_dscwindow->priv->notesview);
344         ev_view_set_model (EV_VIEW (ev_dscwindow->priv->notesview), ev_dscwindow->priv->model);
345         gtk_widget_show_all (h);
346         gtk_container_add (GTK_CONTAINER (ev_dscwindow), h);
347
348         gtk_drag_dest_unset (GTK_WIDGET (ev_dscwindow->priv->notesview));
349         gtk_drag_dest_unset (GTK_WIDGET (ev_dscwindow->priv->sidebar));
350
351         gint click = GDK_BUTTON1_MOTION_MASK | GDK_KEY_PRESS_MASK;
352         gtk_widget_add_events (GTK_WIDGET (ev_dscwindow->priv->sidebar), click);
353         /* TODO: actions*/
354         g_signal_connect (ev_dscwindow->priv->notesview, "button-press-event",
355                           G_CALLBACK (ev_dscwindow_notes_clicked), ev_dscwindow);
356 }
357
358
359 static void
360 ev_dscwindow_dispose (GObject *obj)
361 {
362         EvDSCWindow * ev_dscwindow = EV_DSCWINDOW (obj);
363         EvDSCWindowPrivate *priv = ev_dscwindow->priv;
364
365         if (priv->moveback_monitor >= 0) {
366                 GtkWindow * presentation_window = GTK_WINDOW (priv->presentation_window);
367                 GdkRectangle coords;
368
369                 gdk_screen_get_monitor_geometry (
370                         gtk_window_get_screen (presentation_window),
371                         priv->moveback_monitor, &coords);
372
373                 gtk_window_move (presentation_window, coords.x, coords.y);
374         }
375         ev_window_stop_presentation (EV_WINDOW(priv->presentation_window), TRUE);
376         /*if (priv->overview) {
377                 g_object_unref (priv->overview);
378                 priv->overview = NULL;
379         }*/
380
381
382         G_OBJECT_CLASS (ev_dscwindow_parent_class)->dispose (obj);
383 }
384
385 static void
386 ev_dscwindow_class_init (EvDSCWindowClass *ev_dscwindow_class)
387 {
388         GObjectClass *g_object_class = G_OBJECT_CLASS (ev_dscwindow_class);
389         g_type_class_add_private (g_object_class, sizeof (EvDSCWindowPrivate));
390         g_object_class->dispose  = ev_dscwindow_dispose;
391 }
392
393 GtkWidget *
394 ev_dscwindow_new (void)
395 {
396         EvDSCWindow *ev_dscwindow;
397
398         ev_dscwindow = g_object_new (EV_TYPE_DSCWINDOW, "type", GTK_WINDOW_TOPLEVEL, NULL);
399         return GTK_WIDGET (ev_dscwindow);
400 }