]> www.fi.muni.cz Git - evince.git/blob - shell/ev-dualscreen.c
bdf695ff598127a923ec7a749be8392a61da0e73
[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
62 G_DEFINE_TYPE (EvDSCWindow, ev_dscwindow, GTK_TYPE_WINDOW)
63
64 static gboolean
65 ev_dscwindow_switch_monitors (GtkWidget *widget, EvDSCWindow *ev_dscwindow)
66 {
67         if (!EV_IS_DSCWINDOW (ev_dscwindow))
68                 return FALSE;
69
70         gint num_monitors = get_num_monitors (GTK_WINDOW (ev_dscwindow));
71
72         if (num_monitors == 2) {
73                 GtkWindow *presentation_window = GTK_WINDOW (ev_dscwindow->priv->presentation_window);
74                 GdkScreen *screen = gtk_window_get_screen (presentation_window);
75
76                 gint monitor_1 = gdk_screen_get_monitor_at_window (screen,
77                         gtk_widget_get_window (GTK_WIDGET (presentation_window)));
78
79                 gint monitor_2 = (monitor_1 + 1) % 2;
80
81                 GdkRectangle coords;
82                 gdk_screen_get_monitor_geometry (screen, monitor_2, &coords);
83                 gtk_window_move (presentation_window, coords.x, coords.y);
84                 ev_dscwindow->priv->moveback_monitor = monitor_1;
85
86                 gdk_screen_get_monitor_geometry (screen, monitor_1, &coords);
87                 gtk_window_unmaximize (GTK_WINDOW (ev_dscwindow));
88                 gtk_window_move (GTK_WINDOW (ev_dscwindow), coords.x, coords.y);
89                 gtk_window_maximize (GTK_WINDOW (ev_dscwindow));
90         }
91         return TRUE;
92 }
93
94 static void
95 ev_dscwindow_sidebar_visibility(GtkWidget *sidebar)
96 {
97         gtk_widget_set_visible (sidebar, !(gtk_widget_get_visible(sidebar)));
98 }
99
100 static void
101 ev_dscwindow_page_changed_cb (EvDocumentModel *model,
102                            GParamSpec      *pspec,
103                            EvDSCWindow     *ev_dscwindow)
104 {
105         gint page = ev_document_model_get_page (model);
106         if(page != ev_view_presentation_get_current_page (ev_dscwindow->priv->presentation_view)) {
107                 ev_view_presentation_set_page (EV_VIEW_PRESENTATION(ev_dscwindow->priv->presentation_view), page);
108         if( ev_document_model_get_page (ev_dscwindow->priv->model) != page)
109                 ev_document_model_set_page(ev_dscwindow->priv->model, page);
110         if( ev_document_model_get_page (ev_dscwindow->priv->notes_model) != page)
111                 ev_document_model_set_page(ev_dscwindow->priv->notes_model, page);
112         }
113 }
114
115 static void
116 ev_dscwindow_presentation_page_changed_cb (EvViewPresentation *pview,
117                            GParamSpec      *pspec,
118                            EvDSCWindow     *ev_dscwindow)
119 {
120         gint page = ev_view_presentation_get_current_page (pview);
121         if( ev_document_model_get_page (ev_dscwindow->priv->model) != page)
122                 ev_document_model_set_page(ev_dscwindow->priv->model, page);
123         if( ev_document_model_get_page (ev_dscwindow->priv->notes_model) != page)
124                 ev_document_model_set_page(ev_dscwindow->priv->notes_model, page);
125 }
126
127 /**
128  * ev_dscwindow_notes_interaction: User wants to load a different file as notes.
129  **/
130 static gboolean
131 ev_dscwindow_notes_interaction (GtkContainer *container, EvDSCWindow *ev_dscwindow)
132 {
133         GtkWidget *dialog;
134
135         dialog = gtk_file_chooser_dialog_new (
136                 _("Open Document"),
137                 GTK_WINDOW (ev_dscwindow),
138                 GTK_FILE_CHOOSER_ACTION_OPEN,
139                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
140                 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
141                 NULL);
142
143         ev_document_factory_add_filters (dialog, NULL);
144         gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dialog), FALSE);
145         gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (dialog), TRUE);
146
147         if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
148         {
149                 char * uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dialog));
150                 GError * error = NULL;
151                 ev_view_set_loading (EV_VIEW (ev_dscwindow->priv->notesview), TRUE);
152
153                 if (ev_dscwindow->priv->notes_document) {
154                         ev_document_load (ev_dscwindow->priv->notes_document, uri, &error);
155                 } else {
156                         ev_dscwindow->priv->notes_document = ev_document_factory_get_document (uri,
157                                 &error);
158                 }
159                 g_free (uri);
160                 if (error == NULL){
161                         ev_dscwindow->priv->notes_model = ev_document_model_new ();
162                         ev_document_model_set_document (ev_dscwindow->priv->notes_model, ev_dscwindow->priv->notes_document);
163                         ev_document_model_set_continuous (ev_dscwindow->priv->notes_model, FALSE);
164                         ev_document_model_set_dual_page (ev_dscwindow->priv->notes_model, FALSE);
165                         ev_document_model_set_sizing_mode (ev_dscwindow->priv->notes_model, EV_SIZING_BEST_FIT);
166                         ev_document_model_set_page (ev_dscwindow->priv->notes_model,
167                                 ev_document_model_get_page (ev_dscwindow->priv->model));
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 void
239 ev_dscwindow_set_presentation (EvDSCWindow *ev_dscwindow,
240         EvWindow *presentation_window, EvDocument *document, EvViewPresentation *pview)
241 {
242         if (!EV_IS_WINDOW (presentation_window))
243                 return;
244
245         ev_dscwindow->priv->presentation_window = GTK_WIDGET(presentation_window);
246         ev_dscwindow->priv->presentation_document = document;
247         ev_dscwindow->priv->presentation_view = EV_VIEW_PRESENTATION(pview);
248         ev_document_model_set_document(ev_dscwindow->priv->model, document);
249         ev_document_model_set_page(ev_dscwindow->priv->model, ev_view_presentation_get_current_page (pview));
250         g_signal_connect_swapped (ev_dscwindow->priv->presentation_view,
251                                   "destroy",
252                                   G_CALLBACK (gtk_widget_destroy),
253                                   ev_dscwindow);
254         g_signal_connect (G_OBJECT(ev_dscwindow->priv->model),
255                           "notify::page",
256                           G_CALLBACK (ev_dscwindow_page_changed_cb),
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
286         ev_dscwindow->priv = EV_DSCWINDOW_GET_PRIVATE (ev_dscwindow);
287         ev_dscwindow->priv->moveback_monitor = -1;
288         ev_dscwindow->priv->notes_document = NULL;
289
290         gtk_window_set_title (GTK_WINDOW (ev_dscwindow), _("Presentation Control"));
291
292         GtkWidget *hpaned = gtk_hpaned_new ();
293         GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
294
295         ev_dscwindow->priv->model = ev_document_model_new ();
296         ev_document_model_set_continuous (ev_dscwindow->priv->model, FALSE);
297         ev_document_model_set_dual_page (ev_dscwindow->priv->model, FALSE);
298         ev_document_model_set_sizing_mode (ev_dscwindow->priv->model, EV_SIZING_BEST_FIT);
299
300         ev_dscwindow->priv->sidebar = ev_sidebar_new ();
301         ev_sidebar_set_model (EV_SIDEBAR (ev_dscwindow->priv->sidebar),
302                               ev_dscwindow->priv->model);
303         GtkWidget *sidebar_widget;
304         sidebar_widget = ev_sidebar_thumbnails_new ();
305         gtk_widget_show (sidebar_widget);
306         ev_sidebar_add_page (EV_SIDEBAR (ev_dscwindow->priv->sidebar),
307                              sidebar_widget);
308         gtk_widget_show (ev_dscwindow->priv->sidebar);
309         gtk_paned_add1(GTK_PANED(hpaned), ev_dscwindow->priv->sidebar);
310
311         ev_dscwindow->priv->notesview_scrolled_window = GTK_WIDGET (
312                 g_object_new (GTK_TYPE_SCROLLED_WINDOW,"shadow-type",
313                                 GTK_SHADOW_IN, NULL));
314         gtk_paned_add2 (GTK_PANED (hpaned), ev_dscwindow->priv->notesview_scrolled_window);
315
316         ev_dscwindow->priv->notesview = ev_view_new ();
317         g_object_ref (ev_dscwindow->priv->notesview);
318         gtk_container_add (GTK_CONTAINER (ev_dscwindow->priv->notesview_scrolled_window),
319                 ev_dscwindow->priv->notesview);
320         ev_dscwindow->priv->notes_model = ev_dscwindow->priv->model;
321         ev_view_set_model (EV_VIEW (ev_dscwindow->priv->notesview), ev_dscwindow->priv->notes_model);
322         gtk_widget_show_all (hpaned);
323         gtk_box_pack_start(GTK_BOX(vbox),hpaned,TRUE,TRUE,0);
324
325         GtkWidget *expander = gtk_expander_new (_("Expensive features"));
326         gtk_expander_set_expanded (GTK_EXPANDER (expander), TRUE);
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_from_stock (GTK_STOCK_CLOSE);
342         gtk_tool_button_set_label (GTK_TOOL_BUTTON(b_close), _("End presentation"));
343         gtk_toolbar_insert (GTK_TOOLBAR (toolbar), b_close, -1);
344         g_signal_connect (b_close, "clicked",
345                 G_CALLBACK (ev_dscwindow_end), NULL);
346
347         GtkToolItem *b_sidebar = gtk_tool_button_new_from_stock (GTK_STOCK_PAGE_SETUP);
348         gtk_tool_button_set_label (GTK_TOOL_BUTTON(b_sidebar), _("Show sidebar"));
349         gtk_toolbar_insert (GTK_TOOLBAR (toolbar), b_sidebar, -1);
350         g_signal_connect_swapped (b_sidebar, "clicked",
351                 G_CALLBACK (ev_dscwindow_sidebar_visibility), ev_dscwindow->priv->sidebar);
352
353         gtk_container_add (GTK_CONTAINER (expander), toolbar);
354         gtk_box_pack_end (GTK_BOX (vbox), expander, FALSE, TRUE, 0);
355
356 //      gtk_drag_dest_unset (GTK_WIDGET (ev_dscwindow->priv->notesview));
357 //      gtk_drag_dest_unset (GTK_WIDGET (ev_dscwindow->priv->sidebar));
358
359 //      gint click = GDK_BUTTON1_MOTION_MASK | GDK_KEY_PRESS_MASK;
360 //      gtk_widget_add_events (GTK_WIDGET (ev_dscwindow->priv->sidebar), click);
361         /* TODO: actions, this part is broken...*/
362         /*g_signal_connect (ev_dscwindow->priv->notesview, "button-press-event",
363                           G_CALLBACK (ev_dscwindow_notes_clicked), ev_dscwindow);*/
364
365         gtk_widget_show_all(vbox);
366         gtk_container_add (GTK_CONTAINER (ev_dscwindow), vbox);
367 }
368
369
370 static void
371 ev_dscwindow_dispose (GObject *obj)
372 {
373         EvDSCWindow * ev_dscwindow = EV_DSCWINDOW (obj);
374         EvDSCWindowPrivate *priv = ev_dscwindow->priv;
375
376         if (priv->moveback_monitor >= 0) {
377                 GtkWindow * presentation_window = GTK_WINDOW (priv->presentation_window);
378                 GdkRectangle coords;
379
380                 gdk_screen_get_monitor_geometry (
381                         gtk_window_get_screen (presentation_window),
382                         priv->moveback_monitor, &coords);
383
384                 gtk_window_move (presentation_window, coords.x, coords.y);
385         }
386         ev_window_stop_presentation (EV_WINDOW(priv->presentation_window), TRUE);
387         /*if (priv->overview) {
388                 g_object_unref (priv->overview);
389                 priv->overview = NULL;
390         }*/
391
392
393         G_OBJECT_CLASS (ev_dscwindow_parent_class)->dispose (obj);
394 }
395
396 static void
397 ev_dscwindow_class_init (EvDSCWindowClass *ev_dscwindow_class)
398 {
399         GObjectClass *g_object_class = G_OBJECT_CLASS (ev_dscwindow_class);
400         g_type_class_add_private (g_object_class, sizeof (EvDSCWindowPrivate));
401         g_object_class->dispose  = ev_dscwindow_dispose;
402 }
403
404 GtkWidget *
405 ev_dscwindow_new (void)
406 {
407         EvDSCWindow *ev_dscwindow;
408
409         ev_dscwindow = g_object_new (EV_TYPE_DSCWINDOW, "type", GTK_WINDOW_TOPLEVEL, NULL);
410         return GTK_WIDGET (ev_dscwindow);
411 }