]> www.fi.muni.cz Git - evince.git/blob - shell/ev-dualscreen.c
[dualscreen] in short "Disziplin ist Macht."
[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 #include "ev-presentation-timer.h"
40
41 struct _EvDSCWindowPrivate {
42         GtkWidget       *main_box;
43         GtkWidget       *menubar;
44         GtkWidget       *sidebar;
45         GtkWidget       *notesview;
46         GtkWidget       *timer;
47         EvDocumentModel *model;
48         EvDocumentModel *notes_model;
49         EvDocument      *presentation_document;
50         EvDocument      *notes_document;
51
52         GtkWidget       *presentation_window;
53         GtkWidget       *overview_scrolled_window;
54         GtkWidget       *notesview_scrolled_window;
55
56         EvViewPresentation *presentation_view;
57         gint            moveback_monitor;
58 };
59
60 #define EV_DSCWINDOW_GET_PRIVATE(object) \
61         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_DSCWINDOW, EvDSCWindowPrivate))
62 #define PAGE_CACHE_SIZE 52428800 /* 50MB */
63
64 G_DEFINE_TYPE (EvDSCWindow, ev_dscwindow, GTK_TYPE_WINDOW)
65
66 static gboolean
67 ev_dscwindow_switch_monitors (GtkWidget *widget, EvDSCWindow *ev_dscwindow)
68 {
69         if (!EV_IS_DSCWINDOW (ev_dscwindow))
70                 return FALSE;
71
72         gint num_monitors = get_num_monitors (GTK_WINDOW (ev_dscwindow));
73
74         if (num_monitors == 2) {
75                 GtkWindow *presentation_window = GTK_WINDOW (ev_dscwindow->priv->presentation_window);
76                 GdkScreen *screen = gtk_window_get_screen (presentation_window);
77
78                 gint monitor_1 = gdk_screen_get_monitor_at_window (screen,
79                         gtk_widget_get_window (GTK_WIDGET (presentation_window)));
80
81                 gint monitor_2 = (monitor_1 + 1) % 2;
82
83                 GdkRectangle coords;
84                 gdk_screen_get_monitor_geometry (screen, monitor_2, &coords);
85                 gtk_window_move (presentation_window, coords.x, coords.y);
86                 ev_dscwindow->priv->moveback_monitor = monitor_1;
87
88                 gdk_screen_get_monitor_geometry (screen, monitor_1, &coords);
89                 gtk_window_unmaximize (GTK_WINDOW (ev_dscwindow));
90                 gtk_window_move (GTK_WINDOW (ev_dscwindow), coords.x, coords.y);
91                 gtk_window_maximize (GTK_WINDOW (ev_dscwindow));
92         }
93         return TRUE;
94 }
95
96 static void
97 ev_dscwindow_sidebar_visibility(GtkWidget *sidebar)
98 {
99         gtk_widget_set_visible (sidebar, !(gtk_widget_get_visible(sidebar)));
100 }
101
102 static void
103 ev_dscwindow_page_changed_cb (EvDocumentModel *model,
104                            GParamSpec      *pspec,
105                            EvDSCWindow     *ev_dscwindow)
106 {
107         gint page = ev_document_model_get_page (model);
108         if(page != ev_view_presentation_get_current_page (ev_dscwindow->priv->presentation_view)) {
109                 ev_view_presentation_set_page (EV_VIEW_PRESENTATION(ev_dscwindow->priv->presentation_view), page);
110         if( ev_document_model_get_page (ev_dscwindow->priv->model) != page)
111                 ev_document_model_set_page(ev_dscwindow->priv->model, page);
112         if( ev_document_model_get_page (ev_dscwindow->priv->notes_model) != page)
113                 ev_document_model_set_page(ev_dscwindow->priv->notes_model, page);
114         }
115 }
116
117 static void
118 ev_dscwindow_presentation_page_changed_cb (EvViewPresentation *pview,
119                            GParamSpec      *pspec,
120                            EvDSCWindow     *ev_dscwindow)
121 {
122         gint page = ev_view_presentation_get_current_page (pview);
123         if( ev_document_model_get_page (ev_dscwindow->priv->model) != page)
124                 ev_document_model_set_page(ev_dscwindow->priv->model, page);
125         if( ev_document_model_get_page (ev_dscwindow->priv->notes_model) != page)
126                 ev_document_model_set_page(ev_dscwindow->priv->notes_model, page);
127 }
128
129 static gboolean
130 ev_dscwindow_notes_interaction (GtkContainer *container, EvDSCWindow *ev_dscwindow)
131 {
132         GtkWidget *dialog;
133
134         dialog = gtk_file_chooser_dialog_new (
135                 _("Open Document"),
136                 GTK_WINDOW (ev_dscwindow),
137                 GTK_FILE_CHOOSER_ACTION_OPEN,
138                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
139                 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
140                 NULL);
141
142         ev_document_factory_add_filters (dialog, NULL);
143         gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dialog), FALSE);
144         gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (dialog), TRUE);
145
146         if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
147         {
148                 char * uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dialog));
149                 GError * error = NULL;
150                 ev_view_set_loading (EV_VIEW (ev_dscwindow->priv->notesview), TRUE);
151
152                 if (ev_dscwindow->priv->notes_document) {
153                         ev_document_load (ev_dscwindow->priv->notes_document, uri, &error);
154                 } else {
155                         ev_dscwindow->priv->notes_document = ev_document_factory_get_document (uri,
156                                 &error);
157                 }
158                 g_free (uri);
159                 if (error == NULL){
160                         ev_dscwindow->priv->notes_model = ev_document_model_new ();
161                         ev_document_model_set_document (ev_dscwindow->priv->notes_model, ev_dscwindow->priv->notes_document);
162                         ev_document_model_set_continuous (ev_dscwindow->priv->notes_model, FALSE);
163                         ev_document_model_set_dual_page (ev_dscwindow->priv->notes_model, FALSE);
164                         ev_document_model_set_sizing_mode (ev_dscwindow->priv->notes_model, EV_SIZING_BEST_FIT);
165                         ev_document_model_set_page (ev_dscwindow->priv->notes_model,
166                                 ev_document_model_get_page (ev_dscwindow->priv->model));
167                         ev_view_set_model(EV_VIEW(ev_dscwindow->priv->notesview),
168                                           ev_dscwindow->priv->notes_model);
169                         g_signal_connect (G_OBJECT(ev_dscwindow->priv->notes_model),
170                                           "notify::page",
171                                           G_CALLBACK (ev_dscwindow_page_changed_cb),
172                                           ev_dscwindow);
173                 }
174         }
175         gtk_widget_destroy (dialog);
176
177         return TRUE;
178 }
179
180 EvDSCWindow *
181 ev_dscwindow_get_control (void)
182 {
183         static EvDSCWindow * control = NULL;
184
185         if (!control || !EV_IS_DSCWINDOW (control)) {
186                 control = EV_DSCWINDOW (g_object_new (EV_TYPE_DSCWINDOW, NULL));
187         }
188
189         return control;
190 }
191
192 static void
193 ev_dscwindow_window_placement (EvDSCWindow *ev_dscwindow)
194 {
195         GtkWindow * presentation_window = GTK_WINDOW (ev_dscwindow->priv->presentation_window);
196         GdkScreen * screen = gtk_window_get_screen (presentation_window);
197         gint work_monitor = gdk_screen_get_monitor_at_window (screen,
198                 gtk_widget_get_window (GTK_WIDGET (presentation_window)));
199         gint presentation_monitor = (work_monitor + 1) % 2;
200         GdkRectangle coords;
201         gdk_screen_get_monitor_geometry (screen, presentation_monitor,
202                 &coords);
203
204         gtk_window_move (presentation_window, coords.x, coords.y);
205         ev_dscwindow->priv->moveback_monitor = work_monitor;
206         gtk_window_maximize (GTK_WINDOW (ev_dscwindow));
207 }
208
209 void
210 ev_dscwindow_set_presentation (EvDSCWindow *ev_dscwindow,
211         EvWindow *presentation_window, EvDocument *document, EvViewPresentation *pview)
212 {
213         if (!EV_IS_WINDOW (presentation_window))
214                 return;
215
216         ev_dscwindow->priv->presentation_window = GTK_WIDGET(presentation_window);
217         ev_dscwindow->priv->presentation_document = document;
218         ev_dscwindow->priv->presentation_view = EV_VIEW_PRESENTATION(pview);
219
220         ev_document_model_set_document(ev_dscwindow->priv->model, document);
221         ev_document_model_set_page(ev_dscwindow->priv->model, ev_view_presentation_get_current_page (pview));
222         /*signals*/
223         g_signal_connect_swapped (ev_dscwindow->priv->presentation_view,
224                                   "destroy",
225                                   G_CALLBACK (gtk_widget_destroy),
226                                   ev_dscwindow);
227         g_signal_connect (G_OBJECT(ev_dscwindow->priv->model),
228                           "notify::page",
229                           G_CALLBACK (ev_dscwindow_page_changed_cb),
230                           ev_dscwindow);
231         g_signal_connect (G_OBJECT(ev_dscwindow->priv->presentation_view),
232                           "notify::page",
233                           G_CALLBACK (ev_dscwindow_presentation_page_changed_cb),
234                           ev_dscwindow);
235
236         ev_dscwindow_window_placement (ev_dscwindow);
237 }
238
239 static gboolean
240 ev_dscwindow_end (GtkWidget *widget, GdkEvent *event)
241 {
242         gtk_widget_destroy (GTK_WIDGET (ev_dscwindow_get_control ()));
243         return TRUE;
244 }
245
246 static void
247 ev_dscwindow_init (EvDSCWindow *ev_dscwindow)
248 {
249
250         ev_dscwindow->priv = EV_DSCWINDOW_GET_PRIVATE (ev_dscwindow);
251         ev_dscwindow->priv->moveback_monitor = -1;
252         ev_dscwindow->priv->notes_document = NULL;
253
254         gtk_window_set_title (GTK_WINDOW (ev_dscwindow), _("Presentation Control"));
255
256         GtkWidget *hpaned = gtk_hpaned_new ();
257         GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
258
259         ev_dscwindow->priv->model = ev_document_model_new ();
260         ev_document_model_set_continuous (ev_dscwindow->priv->model, FALSE);
261         ev_document_model_set_dual_page (ev_dscwindow->priv->model, FALSE);
262         ev_document_model_set_sizing_mode (ev_dscwindow->priv->model, EV_SIZING_BEST_FIT);
263
264         ev_dscwindow->priv->sidebar = ev_sidebar_new ();
265         ev_sidebar_set_model (EV_SIDEBAR (ev_dscwindow->priv->sidebar),
266                               ev_dscwindow->priv->model);
267         GtkWidget *sidebar_widget;
268         sidebar_widget = ev_sidebar_thumbnails_new ();
269         gtk_widget_show (sidebar_widget);
270         ev_sidebar_add_page (EV_SIDEBAR (ev_dscwindow->priv->sidebar),
271                              sidebar_widget);
272         gtk_widget_show (ev_dscwindow->priv->sidebar);
273         gtk_paned_add1(GTK_PANED(hpaned), ev_dscwindow->priv->sidebar);
274
275         ev_dscwindow->priv->notesview_scrolled_window = GTK_WIDGET (
276                 g_object_new (GTK_TYPE_SCROLLED_WINDOW,"shadow-type",
277                                 GTK_SHADOW_IN, NULL));
278         gtk_paned_add2 (GTK_PANED (hpaned), ev_dscwindow->priv->notesview_scrolled_window);
279
280         ev_dscwindow->priv->notesview = ev_view_new ();
281         g_object_ref (ev_dscwindow->priv->notesview);
282         gtk_container_add (GTK_CONTAINER (ev_dscwindow->priv->notesview_scrolled_window),
283                 ev_dscwindow->priv->notesview);
284         ev_dscwindow->priv->notes_model = ev_dscwindow->priv->model;
285         ev_view_set_model (EV_VIEW (ev_dscwindow->priv->notesview), ev_dscwindow->priv->notes_model);
286         gtk_widget_show_all (hpaned);
287         gtk_box_pack_start(GTK_BOX(vbox),hpaned,TRUE,TRUE,0);
288
289         GtkWidget *expander = gtk_expander_new (_("Expensive features"));
290         gtk_expander_set_expanded (GTK_EXPANDER (expander), TRUE);
291         GtkWidget *toolbar = gtk_toolbar_new ();
292
293         GtkToolItem *b_switch = gtk_tool_button_new (NULL, _("Switch monitors"));
294         gtk_tool_button_set_icon_name (GTK_TOOL_BUTTON (b_switch), "object-flip-horizontal");
295         gtk_toolbar_insert (GTK_TOOLBAR (toolbar), b_switch, -1);
296         g_signal_connect (b_switch, "clicked",
297                 G_CALLBACK (ev_dscwindow_switch_monitors), ev_dscwindow);
298
299         GtkToolItem *b_notes = gtk_tool_button_new_from_stock (GTK_STOCK_OPEN);
300         gtk_tool_button_set_label (GTK_TOOL_BUTTON(b_notes), _("Load notes..."));
301         gtk_toolbar_insert (GTK_TOOLBAR (toolbar), b_notes, -1);
302         g_signal_connect (b_notes, "clicked",
303                 G_CALLBACK (ev_dscwindow_notes_interaction), ev_dscwindow);
304
305         GtkToolItem *b_close = gtk_tool_button_new_from_stock (GTK_STOCK_CLOSE);
306         gtk_tool_button_set_label (GTK_TOOL_BUTTON(b_close), _("End presentation"));
307         gtk_toolbar_insert (GTK_TOOLBAR (toolbar), b_close, -1);
308         g_signal_connect (b_close, "clicked",
309                 G_CALLBACK (ev_dscwindow_end), NULL);
310
311         GtkToolItem *b_sidebar = gtk_tool_button_new_from_stock (GTK_STOCK_PAGE_SETUP);
312         gtk_tool_button_set_label (GTK_TOOL_BUTTON(b_sidebar), _("Show sidebar"));
313         gtk_toolbar_insert (GTK_TOOLBAR (toolbar), b_sidebar, -1);
314         g_signal_connect_swapped (b_sidebar, "clicked",
315                 G_CALLBACK (ev_dscwindow_sidebar_visibility), ev_dscwindow->priv->sidebar);
316
317         gtk_container_add (GTK_CONTAINER (expander), toolbar);
318
319         GtkWidget *hbox = gtk_hbox_new (FALSE,0);
320         gtk_box_pack_start (GTK_BOX (hbox), expander, FALSE, TRUE, 0);
321         ev_dscwindow->priv->timer = ev_presentation_timer_new ();
322         gtk_box_pack_end (GTK_BOX (hbox), ev_dscwindow->priv->timer, TRUE, TRUE, 0);
323         gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, TRUE, 0);
324         gtk_widget_show_all(vbox);
325         gtk_container_add (GTK_CONTAINER (ev_dscwindow), vbox);
326 }
327
328 static void
329 ev_dscwindow_dispose (GObject *obj)
330 {
331         EvDSCWindow * ev_dscwindow = EV_DSCWINDOW (obj);
332         EvDSCWindowPrivate *priv = ev_dscwindow->priv;
333
334         if (priv->moveback_monitor >= 0) {
335                 GtkWindow * presentation_window = GTK_WINDOW (priv->presentation_window);
336                 GdkRectangle coords;
337
338                 gdk_screen_get_monitor_geometry (
339                         gtk_window_get_screen (presentation_window),
340                         priv->moveback_monitor, &coords);
341
342                 gtk_window_move (presentation_window, coords.x, coords.y);
343         }
344         ev_window_stop_presentation (EV_WINDOW(priv->presentation_window), TRUE);
345         /*if (priv->overview) {
346                 g_object_unref (priv->overview);
347                 priv->overview = NULL;
348         }*/
349
350         G_OBJECT_CLASS (ev_dscwindow_parent_class)->dispose (obj);
351 }
352
353 static void
354 ev_dscwindow_class_init (EvDSCWindowClass *ev_dscwindow_class)
355 {
356         GObjectClass *g_object_class = G_OBJECT_CLASS (ev_dscwindow_class);
357         g_type_class_add_private (g_object_class, sizeof (EvDSCWindowPrivate));
358         g_object_class->dispose  = ev_dscwindow_dispose;
359 }
360
361 GtkWidget *
362 ev_dscwindow_new (void)
363 {
364         EvDSCWindow *ev_dscwindow;
365
366         ev_dscwindow = g_object_new (EV_TYPE_DSCWINDOW, "type", GTK_WINDOW_TOPLEVEL, NULL);
367         return GTK_WIDGET (ev_dscwindow);
368 }