]> www.fi.muni.cz Git - evince.git/blob - shell/ev-dualscreen.c
[dualscreen] notes loading
[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_page_changed_cb: Callback to change page on all views
105  *
106  **/
107 static void
108 ev_dscwindow_page_changed_cb (EvDocumentModel *model,
109                            GParamSpec      *pspec,
110                            EvDSCWindow     *ev_dscwindow)
111 {
112         gint page = ev_document_model_get_page (model);
113         if(page != ev_view_presentation_get_current_page (ev_dscwindow->priv->presentation_view)) {
114                 ev_view_presentation_set_page (EV_VIEW_PRESENTATION(ev_dscwindow->priv->presentation_view), page);
115         if( ev_document_model_get_page (ev_dscwindow->priv->model) != page)
116                 ev_document_model_set_page(ev_dscwindow->priv->model, page);
117         if( ev_document_model_get_page (ev_dscwindow->priv->notes_model) != page)
118                 ev_document_model_set_page(ev_dscwindow->priv->notes_model, page);
119         }
120 }
121 static void
122 ev_dscwindow_presentation_page_changed_cb (EvViewPresentation *pview,
123                            GParamSpec      *pspec,
124                            EvDSCWindow     *ev_dscwindow)
125 {
126         gint page = ev_view_presentation_get_current_page (pview);
127         if( ev_document_model_get_page (ev_dscwindow->priv->model) != page)
128                 ev_document_model_set_page(ev_dscwindow->priv->model, page);
129         if( ev_document_model_get_page (ev_dscwindow->priv->notes_model) != page)
130                 ev_document_model_set_page(ev_dscwindow->priv->notes_model, page);
131 }
132
133 /**
134  * ev_dscwindow_notes_interaction: User wants to load a different file as notes.
135  **/
136 static gboolean
137 ev_dscwindow_notes_interaction (GtkContainer *container, EvDSCWindow *ev_dscwindow)
138 {
139         GtkWidget *dialog;
140
141         dialog = gtk_file_chooser_dialog_new (
142                 _("Open Document"),
143                 GTK_WINDOW (ev_dscwindow),
144                 GTK_FILE_CHOOSER_ACTION_OPEN,
145                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
146                 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
147                 NULL);
148
149         ev_document_factory_add_filters (dialog, NULL);
150         gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dialog), FALSE);
151         gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (dialog), TRUE);
152
153         if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
154         {
155                 char * uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dialog));
156                 GError * error = NULL;
157                 ev_view_set_loading (EV_VIEW (ev_dscwindow->priv->notesview), TRUE);
158
159                 if (ev_dscwindow->priv->notes_document) {
160                         ev_document_load (ev_dscwindow->priv->notes_document, uri, &error);
161                 } else {
162                         ev_dscwindow->priv->notes_document = ev_document_factory_get_document (uri,
163                                 &error);
164                 }
165                 g_free (uri);
166                 if (error == NULL){
167                         ev_dscwindow->priv->notes_model = ev_document_model_new_with_document (ev_dscwindow->priv->notes_document);
168                         ev_view_set_model(EV_VIEW(ev_dscwindow->priv->notesview),
169                                           ev_dscwindow->priv->notes_model);
170                         g_signal_connect (G_OBJECT(ev_dscwindow->priv->notes_model),
171                                           "notify::page",
172                                           G_CALLBACK (ev_dscwindow_page_changed_cb),
173                                           ev_dscwindow);
174                 }
175         }
176         gtk_widget_destroy (dialog);
177
178         return TRUE;
179 }
180
181 /**
182  * ev_dscwindow_get_control: Get the control instance.
183  *
184  * If there is none, create one.
185  *
186  * Returns: control instance
187  **/
188 EvDSCWindow *
189 ev_dscwindow_get_control (void)
190 {
191         static EvDSCWindow * control = NULL;
192
193         if (!control || !EV_IS_DSCWINDOW (control)) {
194                 control = EV_DSCWINDOW (g_object_new (EV_TYPE_DSCWINDOW, NULL));
195         }
196
197         return control;
198 }
199
200 static void
201 ev_dscwindow_window_placement (EvDSCWindow *ev_dscwindow)
202 {
203         gint num_monitors = get_num_monitors (GTK_WINDOW (ev_dscwindow));
204         if (num_monitors == 2) {
205                 GtkWindow * presentation_window = GTK_WINDOW (ev_dscwindow->priv->presentation_window);
206                 GdkScreen * screen = gtk_window_get_screen (presentation_window);
207                 gint work_monitor = gdk_screen_get_monitor_at_window (screen,
208                         gtk_widget_get_window (GTK_WIDGET (presentation_window)));
209                 gint presentation_monitor = (work_monitor + 1) % 2;
210                 GdkRectangle coords;
211                 gdk_screen_get_monitor_geometry (screen, presentation_monitor,
212                         &coords);
213
214                 gtk_window_move (presentation_window, coords.x, coords.y);
215                 ev_dscwindow->priv->moveback_monitor = work_monitor;
216                 gtk_window_maximize (GTK_WINDOW (ev_dscwindow));
217         }
218 }
219
220 static gboolean
221 ev_dscwindow_notes_clicked (GtkWidget *widget, GdkEvent *event,
222         EvDSCWindow *self)
223 {
224         if (!self || !EV_IS_DSCWINDOW (self) || !event)
225                 return FALSE;
226         gint page = ev_document_model_get_page (self->priv->model);
227
228         if (event->type == GDK_2BUTTON_PRESS && ((GdkEventButton *)event)->button == 1) {
229                 ev_dscwindow_notes_interaction (NULL, self);
230         } else if (event->type == GDK_BUTTON_PRESS && ((GdkEventButton *)event)->button == 1) {
231                 ev_document_model_set_page (self->priv->model, page+1);
232         } else if (event->type == GDK_BUTTON_PRESS && ((GdkEventButton *)event)->button == 3) {
233                 ev_document_model_set_page (self->priv->model, page-1);
234         }
235         return FALSE;
236 }
237
238 /**
239  * ev_dscwindow_set_presentation: Set presentation document
240  * @presentation_window: Main window we can reuse for presentation
241  **/
242 void
243 ev_dscwindow_set_presentation (EvDSCWindow *ev_dscwindow,
244         EvWindow *presentation_window, EvDocument *document, EvViewPresentation *pview)
245 {
246         if (!EV_IS_WINDOW (presentation_window))
247                 return;
248
249         ev_dscwindow->priv->presentation_window = GTK_WIDGET(presentation_window);
250         ev_dscwindow->priv->presentation_document = document;
251         ev_dscwindow->priv->presentation_view = EV_VIEW_PRESENTATION(pview);
252         ev_document_model_set_document(ev_dscwindow->priv->model,
253                 document);
254         g_signal_connect_swapped (ev_dscwindow->priv->presentation_view,
255                                   "destroy",
256                                   G_CALLBACK (gtk_widget_destroy),
257                                   ev_dscwindow);
258         g_signal_connect (G_OBJECT(ev_dscwindow->priv->presentation_view),
259                           "notify::page",
260                           G_CALLBACK (ev_dscwindow_presentation_page_changed_cb),
261                           ev_dscwindow);
262         ev_dscwindow_window_placement (ev_dscwindow);
263 }
264
265 /**
266  * ev_dscwindow_end: Destroy control window but keep presentation
267  */
268 static gboolean
269 ev_dscwindow_end (GtkWidget *widget, GdkEvent *event)
270 {
271         gtk_widget_destroy (GTK_WIDGET (ev_dscwindow_get_control ()));
272         return TRUE;
273 }
274
275 /**
276  * ev_dscwindow_init: Initialize multihead presentation
277 *
278 * @ev_dscwindow: EvDSCWindow.
279 *
280 * ev_dscwindow_set_presentation has to be called afterwards for loading in a document. TBD
281 **/
282 static void
283 ev_dscwindow_init (EvDSCWindow *ev_dscwindow)
284 {
285         ev_dscwindow->priv = EV_DSCWINDOW_GET_PRIVATE (ev_dscwindow);
286         ev_dscwindow->priv->moveback_monitor = -1;
287         ev_dscwindow->priv->notes_document = NULL;
288
289         gtk_window_set_title (GTK_WINDOW (ev_dscwindow), _("Presentation Control"));
290
291         GtkWidget *h = gtk_hpaned_new ();
292         GtkWidget *v = gtk_vbox_new (FALSE, 0);
293
294         ev_dscwindow->priv->model = ev_document_model_new ();
295         g_signal_connect (G_OBJECT(ev_dscwindow->priv->model),
296                           "notify::page",
297                           G_CALLBACK (ev_dscwindow_page_changed_cb),
298                           ev_dscwindow);
299         ev_dscwindow->priv->sidebar = ev_sidebar_new ();
300         ev_sidebar_set_model (EV_SIDEBAR (ev_dscwindow->priv->sidebar),
301                               ev_dscwindow->priv->model);
302         gtk_box_pack_start (GTK_BOX (v), ev_dscwindow->priv->sidebar, TRUE , TRUE, 0);
303         gtk_widget_show (ev_dscwindow->priv->sidebar);
304
305         GtkWidget *sidebar_widget;
306         sidebar_widget = ev_sidebar_thumbnails_new ();
307
308         gtk_widget_show (sidebar_widget);
309         ev_sidebar_add_page (EV_SIDEBAR (ev_dscwindow->priv->sidebar),
310                              sidebar_widget);
311
312         ev_document_model_set_continuous (ev_dscwindow->priv->model, FALSE);
313         ev_document_model_set_dual_page (ev_dscwindow->priv->model, FALSE);
314         ev_document_model_set_sizing_mode (ev_dscwindow->priv->model, EV_SIZING_BEST_FIT);
315
316         GtkWidget *e = gtk_expander_new (_("Expensive features"));
317         gtk_expander_set_expanded (GTK_EXPANDER (e), TRUE);
318
319         GtkWidget *t = gtk_toolbar_new ();
320
321         GtkToolItem* b_switch = gtk_tool_button_new (NULL, _("Switch monitors"));
322         gtk_tool_button_set_icon_name (GTK_TOOL_BUTTON (b_switch), "object-flip-horizontal");
323         gtk_toolbar_insert (GTK_TOOLBAR (t), b_switch, -1);
324         g_signal_connect (b_switch, "clicked",
325                 G_CALLBACK (ev_dscwindow_switch_monitors), ev_dscwindow);
326
327         GtkToolItem* b_notes = gtk_tool_button_new (NULL, _("Load notes ..."));
328         gtk_tool_button_set_icon_name (GTK_TOOL_BUTTON (b_notes), "object-flip-horizontal");
329         gtk_toolbar_insert (GTK_TOOLBAR (t), b_notes, -1);
330         g_signal_connect (b_notes, "clicked",
331                 G_CALLBACK (ev_dscwindow_notes_interaction), ev_dscwindow);
332
333         GtkToolItem* b_close = gtk_tool_button_new (NULL, _("End presentation"));
334         gtk_tool_button_set_icon_name (GTK_TOOL_BUTTON (b_close), "view-restore");
335         gtk_toolbar_insert (GTK_TOOLBAR (t), b_close, -1);
336
337         g_signal_connect (b_close, "clicked",
338                 G_CALLBACK (ev_dscwindow_end), NULL);
339
340         gtk_container_add (GTK_CONTAINER (e), t);
341         gtk_box_pack_end (GTK_BOX (v), e, FALSE, TRUE, 0);
342         gtk_paned_add1 (GTK_PANED (h), v);
343
344         ev_dscwindow->priv->notesview_scrolled_window = GTK_WIDGET (g_object_new (
345                 GTK_TYPE_SCROLLED_WINDOW, "shadow-type", GTK_SHADOW_IN, NULL));
346         gtk_paned_add2 (GTK_PANED (h), ev_dscwindow->priv->notesview_scrolled_window);
347
348         ev_dscwindow->priv->notesview = ev_view_new ();
349         g_object_ref (ev_dscwindow->priv->notesview);
350         gtk_container_add (GTK_CONTAINER (ev_dscwindow->priv->notesview_scrolled_window),
351                 ev_dscwindow->priv->notesview);
352         ev_dscwindow->priv->notes_model = ev_dscwindow->priv->model;
353         ev_view_set_model (EV_VIEW (ev_dscwindow->priv->notesview), ev_dscwindow->priv->notes_model);
354         gtk_widget_show_all (h);
355         gtk_container_add (GTK_CONTAINER (ev_dscwindow), h);
356
357         gtk_drag_dest_unset (GTK_WIDGET (ev_dscwindow->priv->notesview));
358         gtk_drag_dest_unset (GTK_WIDGET (ev_dscwindow->priv->sidebar));
359
360         gint click = GDK_BUTTON1_MOTION_MASK | GDK_KEY_PRESS_MASK;
361         gtk_widget_add_events (GTK_WIDGET (ev_dscwindow->priv->sidebar), click);
362         /* TODO: actions*/
363         g_signal_connect (ev_dscwindow->priv->notesview, "button-press-event",
364                           G_CALLBACK (ev_dscwindow_notes_clicked), ev_dscwindow);
365 }
366
367
368 static void
369 ev_dscwindow_dispose (GObject *obj)
370 {
371         EvDSCWindow * ev_dscwindow = EV_DSCWINDOW (obj);
372         EvDSCWindowPrivate *priv = ev_dscwindow->priv;
373
374         if (priv->moveback_monitor >= 0) {
375                 GtkWindow * presentation_window = GTK_WINDOW (priv->presentation_window);
376                 GdkRectangle coords;
377
378                 gdk_screen_get_monitor_geometry (
379                         gtk_window_get_screen (presentation_window),
380                         priv->moveback_monitor, &coords);
381
382                 gtk_window_move (presentation_window, coords.x, coords.y);
383         }
384         ev_window_stop_presentation (EV_WINDOW(priv->presentation_window), TRUE);
385         /*if (priv->overview) {
386                 g_object_unref (priv->overview);
387                 priv->overview = NULL;
388         }*/
389
390
391         G_OBJECT_CLASS (ev_dscwindow_parent_class)->dispose (obj);
392 }
393
394 static void
395 ev_dscwindow_class_init (EvDSCWindowClass *ev_dscwindow_class)
396 {
397         GObjectClass *g_object_class = G_OBJECT_CLASS (ev_dscwindow_class);
398         g_type_class_add_private (g_object_class, sizeof (EvDSCWindowPrivate));
399         g_object_class->dispose  = ev_dscwindow_dispose;
400 }
401
402 GtkWidget *
403 ev_dscwindow_new (void)
404 {
405         EvDSCWindow *ev_dscwindow;
406
407         ev_dscwindow = g_object_new (EV_TYPE_DSCWINDOW, "type", GTK_WINDOW_TOPLEVEL, NULL);
408         return GTK_WIDGET (ev_dscwindow);
409 }