]> www.fi.muni.cz Git - evince.git/blob - shell/ev-dualscreen.c
[dualscreen] bugfixes(set_presentation), cleanup
[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 ();
168                         ev_document_model_set_document (ev_dscwindow->priv->notes_model, ev_dscwindow->priv->notes_document);
169                         ev_document_model_set_continuous (ev_dscwindow->priv->notes_model, FALSE);
170                         ev_document_model_set_dual_page (ev_dscwindow->priv->notes_model, FALSE);
171                         ev_document_model_set_sizing_mode (ev_dscwindow->priv->notes_model, EV_SIZING_BEST_FIT);
172                         ev_document_model_set_page (ev_dscwindow->priv->notes_model,
173                                 ev_document_model_get_page (ev_dscwindow->priv->model));
174                         ev_view_set_model(EV_VIEW(ev_dscwindow->priv->notesview),
175                                           ev_dscwindow->priv->notes_model);
176                         g_signal_connect (G_OBJECT(ev_dscwindow->priv->notes_model),
177                                           "notify::page",
178                                           G_CALLBACK (ev_dscwindow_page_changed_cb),
179                                           ev_dscwindow);
180                 }
181         }
182         gtk_widget_destroy (dialog);
183
184         return TRUE;
185 }
186
187 /**
188  * ev_dscwindow_get_control: Get the control instance.
189  *
190  * If there is none, create one.
191  *
192  * Returns: control instance
193  **/
194 EvDSCWindow *
195 ev_dscwindow_get_control (void)
196 {
197         static EvDSCWindow * control = NULL;
198
199         if (!control || !EV_IS_DSCWINDOW (control)) {
200                 control = EV_DSCWINDOW (g_object_new (EV_TYPE_DSCWINDOW, NULL));
201         }
202
203         return control;
204 }
205
206 static void
207 ev_dscwindow_window_placement (EvDSCWindow *ev_dscwindow)
208 {
209         gint num_monitors = get_num_monitors (GTK_WINDOW (ev_dscwindow));
210         if (num_monitors == 2) {
211                 GtkWindow * presentation_window = GTK_WINDOW (ev_dscwindow->priv->presentation_window);
212                 GdkScreen * screen = gtk_window_get_screen (presentation_window);
213                 gint work_monitor = gdk_screen_get_monitor_at_window (screen,
214                         gtk_widget_get_window (GTK_WIDGET (presentation_window)));
215                 gint presentation_monitor = (work_monitor + 1) % 2;
216                 GdkRectangle coords;
217                 gdk_screen_get_monitor_geometry (screen, presentation_monitor,
218                         &coords);
219
220                 gtk_window_move (presentation_window, coords.x, coords.y);
221                 ev_dscwindow->priv->moveback_monitor = work_monitor;
222                 gtk_window_maximize (GTK_WINDOW (ev_dscwindow));
223         }
224 }
225
226 static gboolean
227 ev_dscwindow_notes_clicked (GtkWidget *widget, GdkEvent *event,
228         EvDSCWindow *self)
229 {
230         if (!self || !EV_IS_DSCWINDOW (self) || !event)
231                 return FALSE;
232         gint page = ev_document_model_get_page (self->priv->model);
233
234         if (event->type == GDK_2BUTTON_PRESS && ((GdkEventButton *)event)->button == 1) {
235                 ev_dscwindow_notes_interaction (NULL, self);
236         } else if (event->type == GDK_BUTTON_PRESS && ((GdkEventButton *)event)->button == 1) {
237                 ev_document_model_set_page (self->priv->model, page+1);
238         } else if (event->type == GDK_BUTTON_PRESS && ((GdkEventButton *)event)->button == 3) {
239                 ev_document_model_set_page (self->priv->model, page-1);
240         }
241         return FALSE;
242 }
243
244 /**
245  * ev_dscwindow_set_presentation: Set presentation document
246  * @presentation_window: Main window we can reuse for presentation
247  **/
248 void
249 ev_dscwindow_set_presentation (EvDSCWindow *ev_dscwindow,
250         EvWindow *presentation_window, EvDocument *document, EvViewPresentation *pview)
251 {
252         if (!EV_IS_WINDOW (presentation_window))
253                 return;
254
255         ev_dscwindow->priv->presentation_window = GTK_WIDGET(presentation_window);
256         ev_dscwindow->priv->presentation_document = document;
257         ev_dscwindow->priv->presentation_view = EV_VIEW_PRESENTATION(pview);
258         ev_document_model_set_document(ev_dscwindow->priv->model, document);
259         ev_document_model_set_page(ev_dscwindow->priv->model, ev_view_presentation_get_current_page (pview));
260         g_signal_connect_swapped (ev_dscwindow->priv->presentation_view,
261                                   "destroy",
262                                   G_CALLBACK (gtk_widget_destroy),
263                                   ev_dscwindow);
264         g_signal_connect (G_OBJECT(ev_dscwindow->priv->model),
265                           "notify::page",
266                           G_CALLBACK (ev_dscwindow_page_changed_cb),
267                           ev_dscwindow);
268         g_signal_connect (G_OBJECT(ev_dscwindow->priv->presentation_view),
269                           "notify::page",
270                           G_CALLBACK (ev_dscwindow_presentation_page_changed_cb),
271                           ev_dscwindow);
272         ev_dscwindow_window_placement (ev_dscwindow);
273 }
274
275 /**
276  * ev_dscwindow_end: Destroy control window but keep presentation
277  */
278 static gboolean
279 ev_dscwindow_end (GtkWidget *widget, GdkEvent *event)
280 {
281         gtk_widget_destroy (GTK_WIDGET (ev_dscwindow_get_control ()));
282         return TRUE;
283 }
284
285 /**
286  * ev_dscwindow_init: Initialize multihead presentation
287 *
288 * @ev_dscwindow: EvDSCWindow.
289 *
290 * ev_dscwindow_set_presentation has to be called afterwards for loading in a document. TBD
291 **/
292 static void
293 ev_dscwindow_init (EvDSCWindow *ev_dscwindow)
294 {
295         /*TODO - cleanup!!*/
296
297         ev_dscwindow->priv = EV_DSCWINDOW_GET_PRIVATE (ev_dscwindow);
298         ev_dscwindow->priv->moveback_monitor = -1;
299         ev_dscwindow->priv->notes_document = NULL;
300
301         gtk_window_set_title (GTK_WINDOW (ev_dscwindow), _("Presentation Control"));
302
303         GtkWidget *hpaned = gtk_hpaned_new ();
304         GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
305
306         ev_dscwindow->priv->model = ev_document_model_new ();
307
308         ev_dscwindow->priv->sidebar = ev_sidebar_new ();
309         ev_sidebar_set_model (EV_SIDEBAR (ev_dscwindow->priv->sidebar),
310                               ev_dscwindow->priv->model);
311         GtkWidget *sidebar_widget;
312         sidebar_widget = ev_sidebar_thumbnails_new ();
313         gtk_widget_show (sidebar_widget);
314         ev_sidebar_add_page (EV_SIDEBAR (ev_dscwindow->priv->sidebar),
315                              sidebar_widget);
316
317         gtk_box_pack_start (GTK_BOX (vbox), ev_dscwindow->priv->sidebar, TRUE, TRUE, 0);
318         gtk_widget_show (ev_dscwindow->priv->sidebar);
319
320         ev_document_model_set_continuous (ev_dscwindow->priv->model, FALSE);
321         ev_document_model_set_dual_page (ev_dscwindow->priv->model, FALSE);
322         ev_document_model_set_sizing_mode (ev_dscwindow->priv->model, EV_SIZING_BEST_FIT);
323
324         GtkWidget *expander = gtk_expander_new (_("Expensive features"));
325         gtk_expander_set_expanded (GTK_EXPANDER (expander), TRUE);
326
327         GtkWidget *toolbar = gtk_toolbar_new ();
328
329         GtkToolItem* b_switch = gtk_tool_button_new (NULL, _("Switch monitors"));
330         gtk_tool_button_set_icon_name (GTK_TOOL_BUTTON (b_switch), "object-flip-horizontal");
331         gtk_toolbar_insert (GTK_TOOLBAR (toolbar), b_switch, -1);
332         g_signal_connect (b_switch, "clicked",
333                 G_CALLBACK (ev_dscwindow_switch_monitors), ev_dscwindow);
334
335         GtkToolItem* b_notes = gtk_tool_button_new_from_stock (GTK_STOCK_OPEN);
336         gtk_tool_button_set_label (GTK_TOOL_BUTTON(b_notes), _("Load notes..."));
337         gtk_toolbar_insert (GTK_TOOLBAR (toolbar), b_notes, -1);
338         g_signal_connect (b_notes, "clicked",
339                 G_CALLBACK (ev_dscwindow_notes_interaction), ev_dscwindow);
340
341         GtkToolItem* b_close = gtk_tool_button_new (NULL, _("End presentation"));
342         gtk_tool_button_set_icon_name (GTK_TOOL_BUTTON (b_close), "view-restore");
343         gtk_toolbar_insert (GTK_TOOLBAR (toolbar), b_close, -1);
344
345         g_signal_connect (b_close, "clicked",
346                 G_CALLBACK (ev_dscwindow_end), NULL);
347
348         gtk_container_add (GTK_CONTAINER (expander), toolbar);
349         gtk_box_pack_end (GTK_BOX (vbox), expander, FALSE, TRUE, 0);
350         gtk_paned_add1 (GTK_PANED (hpaned), vbox);
351
352         ev_dscwindow->priv->notesview_scrolled_window = GTK_WIDGET (g_object_new (
353                 GTK_TYPE_SCROLLED_WINDOW, "shadow-type", GTK_SHADOW_IN, NULL));
354         gtk_paned_add2 (GTK_PANED (hpaned), ev_dscwindow->priv->notesview_scrolled_window);
355
356         ev_dscwindow->priv->notesview = ev_view_new ();
357         g_object_ref (ev_dscwindow->priv->notesview);
358         gtk_container_add (GTK_CONTAINER (ev_dscwindow->priv->notesview_scrolled_window),
359                 ev_dscwindow->priv->notesview);
360         ev_dscwindow->priv->notes_model = ev_dscwindow->priv->model;
361         ev_view_set_model (EV_VIEW (ev_dscwindow->priv->notesview), ev_dscwindow->priv->notes_model);
362         gtk_widget_show_all (hpaned);
363         gtk_container_add (GTK_CONTAINER (ev_dscwindow), hpaned);
364
365         gtk_drag_dest_unset (GTK_WIDGET (ev_dscwindow->priv->notesview));
366         gtk_drag_dest_unset (GTK_WIDGET (ev_dscwindow->priv->sidebar));
367
368         gint click = GDK_BUTTON1_MOTION_MASK | GDK_KEY_PRESS_MASK;
369         gtk_widget_add_events (GTK_WIDGET (ev_dscwindow->priv->sidebar), click);
370         /* TODO: actions, this part is broken...*/
371         /*g_signal_connect (ev_dscwindow->priv->notesview, "button-press-event",
372                           G_CALLBACK (ev_dscwindow_notes_clicked), ev_dscwindow);*/
373 }
374
375
376 static void
377 ev_dscwindow_dispose (GObject *obj)
378 {
379         EvDSCWindow * ev_dscwindow = EV_DSCWINDOW (obj);
380         EvDSCWindowPrivate *priv = ev_dscwindow->priv;
381
382         if (priv->moveback_monitor >= 0) {
383                 GtkWindow * presentation_window = GTK_WINDOW (priv->presentation_window);
384                 GdkRectangle coords;
385
386                 gdk_screen_get_monitor_geometry (
387                         gtk_window_get_screen (presentation_window),
388                         priv->moveback_monitor, &coords);
389
390                 gtk_window_move (presentation_window, coords.x, coords.y);
391         }
392         ev_window_stop_presentation (EV_WINDOW(priv->presentation_window), TRUE);
393         /*if (priv->overview) {
394                 g_object_unref (priv->overview);
395                 priv->overview = NULL;
396         }*/
397
398
399         G_OBJECT_CLASS (ev_dscwindow_parent_class)->dispose (obj);
400 }
401
402 static void
403 ev_dscwindow_class_init (EvDSCWindowClass *ev_dscwindow_class)
404 {
405         GObjectClass *g_object_class = G_OBJECT_CLASS (ev_dscwindow_class);
406         g_type_class_add_private (g_object_class, sizeof (EvDSCWindowPrivate));
407         g_object_class->dispose  = ev_dscwindow_dispose;
408 }
409
410 GtkWidget *
411 ev_dscwindow_new (void)
412 {
413         EvDSCWindow *ev_dscwindow;
414
415         ev_dscwindow = g_object_new (EV_TYPE_DSCWINDOW, "type", GTK_WINDOW_TOPLEVEL, NULL);
416         return GTK_WIDGET (ev_dscwindow);
417 }