1 /* this file is part of evince, a gnome document viewer
3 * Copyright (C) 2009 Carlos Garcia Campos
4 * Copyright (C) 2005 Red Hat, Inc
6 * Evince is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Evince is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "ev-document-model.h"
24 #include "ev-view-type-builtins.h"
25 #include "ev-view-marshal.h"
27 struct _EvDocumentModel
37 EvSizingMode sizing_mode;
41 guint inverted_colors : 1;
47 struct _EvDocumentModelClass
49 GObjectClass base_class;
52 void (* page_changed) (EvDocumentModel *model,
76 static guint signals[N_SIGNALS] = { 0 };
78 G_DEFINE_TYPE (EvDocumentModel, ev_document_model, G_TYPE_OBJECT)
81 ev_document_model_finalize (GObject *object)
83 EvDocumentModel *model = EV_DOCUMENT_MODEL (object);
85 if (model->document) {
86 g_object_unref (model->document);
87 model->document = NULL;
90 G_OBJECT_CLASS (ev_document_model_parent_class)->finalize (object);
94 ev_document_model_set_property (GObject *object,
99 EvDocumentModel *model = EV_DOCUMENT_MODEL (object);
103 ev_document_model_set_document (model, (EvDocument *)g_value_get_object (value));
106 ev_document_model_set_page (model, g_value_get_int (value));
109 ev_document_model_set_rotation (model, g_value_get_int (value));
111 case PROP_INVERTED_COLORS:
112 ev_document_model_set_inverted_colors (model, g_value_get_boolean (value));
115 ev_document_model_set_scale (model, g_value_get_double (value));
117 case PROP_SIZING_MODE:
118 ev_document_model_set_sizing_mode (model, g_value_get_enum (value));
120 case PROP_CONTINUOUS:
121 ev_document_model_set_continuous (model, g_value_get_boolean (value));
124 ev_document_model_set_dual_page (model, g_value_get_boolean (value));
126 case PROP_FULLSCREEN:
127 ev_document_model_set_fullscreen (model, g_value_get_boolean (value));
130 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
135 ev_document_model_get_property (GObject *object,
140 EvDocumentModel *model = EV_DOCUMENT_MODEL (object);
144 g_value_set_object (value, model->document);
147 g_value_set_int (value, model->page);
150 g_value_set_int (value, model->rotation);
152 case PROP_INVERTED_COLORS:
153 g_value_set_boolean (value, model->inverted_colors);
156 g_value_set_double (value, model->scale);
158 case PROP_SIZING_MODE:
159 g_value_set_enum (value, model->sizing_mode);
161 case PROP_CONTINUOUS:
162 g_value_set_boolean (value, ev_document_model_get_continuous (model));
165 g_value_set_boolean (value, ev_document_model_get_dual_page (model));
167 case PROP_FULLSCREEN:
168 g_value_set_boolean (value, ev_document_model_get_fullscreen (model));
171 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176 ev_document_model_class_init (EvDocumentModelClass *klass)
178 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
180 g_object_class->get_property = ev_document_model_get_property;
181 g_object_class->set_property = ev_document_model_set_property;
182 g_object_class->finalize = ev_document_model_finalize;
185 g_object_class_install_property (g_object_class,
187 g_param_spec_object ("document",
189 "The current document",
192 g_object_class_install_property (g_object_class,
194 g_param_spec_int ("page",
199 g_object_class_install_property (g_object_class,
201 g_param_spec_int ("rotation",
203 "Current rotation angle",
206 g_object_class_install_property (g_object_class,
207 PROP_INVERTED_COLORS,
208 g_param_spec_boolean ("inverted-colors",
210 "Whether document is displayed with inverted colors",
213 g_object_class_install_property (g_object_class,
215 g_param_spec_double ("scale",
217 "Current scale factor",
220 g_object_class_install_property (g_object_class,
222 g_param_spec_enum ("sizing-mode",
224 "Current sizing mode",
228 g_object_class_install_property (g_object_class,
230 g_param_spec_boolean ("continuous",
232 "Whether document is displayed in continuous mode",
235 g_object_class_install_property (g_object_class,
237 g_param_spec_boolean ("dual-page",
239 "Whether document is displayed in dual page mode",
242 g_object_class_install_property (g_object_class,
244 g_param_spec_boolean ("fullscreen",
246 "Whether document is displayed in fullscreen mode",
251 signals [PAGE_CHANGED] =
252 g_signal_new ("page-changed",
253 EV_TYPE_DOCUMENT_MODEL,
255 G_STRUCT_OFFSET (EvDocumentModelClass, page_changed),
257 ev_view_marshal_VOID__INT_INT,
259 G_TYPE_INT, G_TYPE_INT);
263 ev_document_model_init (EvDocumentModel *model)
267 model->sizing_mode = EV_SIZING_FIT_WIDTH;
268 model->continuous = TRUE;
269 model->inverted_colors = FALSE;
270 model->min_scale = 0.;
271 model->max_scale = G_MAXDOUBLE;
275 ev_document_model_new (void)
277 return g_object_new (EV_TYPE_DOCUMENT_MODEL, NULL);
281 ev_document_model_new_with_document (EvDocument *document)
283 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
285 return g_object_new (EV_TYPE_DOCUMENT_MODEL, "document", document, NULL);
289 ev_document_model_set_document (EvDocumentModel *model,
290 EvDocument *document)
292 g_return_if_fail (EV_IS_DOCUMENT_MODEL (model));
293 g_return_if_fail (EV_IS_DOCUMENT (document));
295 if (document == model->document)
299 g_object_unref (model->document);
300 model->document = g_object_ref (document);
302 model->n_pages = ev_document_get_n_pages (document);
303 ev_document_model_set_page (model, CLAMP (model->page, 0,
304 model->n_pages - 1));
306 g_object_notify (G_OBJECT (model), "document");
310 ev_document_model_get_document (EvDocumentModel *model)
312 g_return_val_if_fail (EV_IS_DOCUMENT_MODEL (model), NULL);
314 return model->document;
318 ev_document_model_set_page (EvDocumentModel *model,
323 g_return_if_fail (EV_IS_DOCUMENT_MODEL (model));
325 if (model->page == page)
327 if (page < 0 || (model->document && page >= model->n_pages))
330 old_page = model->page;
332 g_signal_emit (model, signals[PAGE_CHANGED], 0, old_page, page);
334 g_object_notify (G_OBJECT (model), "page");
338 ev_document_model_set_page_by_label (EvDocumentModel *model,
339 const gchar *page_label)
343 g_return_if_fail (EV_IS_DOCUMENT_MODEL (model));
344 g_return_if_fail (model->document != NULL);
346 if (ev_document_find_page_by_label (model->document, page_label, &page))
347 ev_document_model_set_page (model, page);
351 ev_document_model_get_page (EvDocumentModel *model)
353 g_return_val_if_fail (EV_IS_DOCUMENT_MODEL (model), -1);
359 ev_document_model_set_scale (EvDocumentModel *model,
362 g_return_if_fail (EV_IS_DOCUMENT_MODEL (model));
364 scale = CLAMP (scale,
365 model->sizing_mode == EV_SIZING_FREE ?
366 model->min_scale : 0, model->max_scale);
368 if (scale == model->scale)
371 model->scale = scale;
373 g_object_notify (G_OBJECT (model), "scale");
377 ev_document_model_get_scale (EvDocumentModel *model)
379 g_return_val_if_fail (EV_IS_DOCUMENT_MODEL (model), 1.0);
385 ev_document_model_set_max_scale (EvDocumentModel *model,
388 g_return_if_fail (EV_IS_DOCUMENT_MODEL (model));
390 if (max_scale == model->max_scale)
393 model->max_scale = max_scale;
395 if (model->scale > max_scale)
396 ev_document_model_set_scale (model, max_scale);
400 ev_document_model_get_max_scale (EvDocumentModel *model)
402 g_return_val_if_fail (EV_IS_DOCUMENT_MODEL (model), 1.0);
404 return model->max_scale;
408 ev_document_model_set_min_scale (EvDocumentModel *model,
411 g_return_if_fail (EV_IS_DOCUMENT_MODEL (model));
413 if (min_scale == model->min_scale)
416 model->min_scale = min_scale;
418 if (model->scale < min_scale)
419 ev_document_model_set_scale (model, min_scale);
423 ev_document_model_get_min_scale (EvDocumentModel *model)
425 g_return_val_if_fail (EV_IS_DOCUMENT_MODEL (model), 0.);
427 return model->min_scale;
431 ev_document_model_set_sizing_mode (EvDocumentModel *model,
434 g_return_if_fail (EV_IS_DOCUMENT_MODEL (model));
436 if (mode == model->sizing_mode)
439 model->sizing_mode = mode;
441 g_object_notify (G_OBJECT (model), "sizing-mode");
445 ev_document_model_get_sizing_mode (EvDocumentModel *model)
447 g_return_val_if_fail (EV_IS_DOCUMENT_MODEL (model), EV_SIZING_FIT_WIDTH);
449 return model->sizing_mode;
453 ev_document_model_set_rotation (EvDocumentModel *model,
456 g_return_if_fail (EV_IS_DOCUMENT_MODEL (model));
460 else if (rotation < 0)
463 if (rotation == model->rotation)
466 model->rotation = rotation;
468 g_object_notify (G_OBJECT (model), "rotation");
472 ev_document_model_get_rotation (EvDocumentModel *model)
474 g_return_val_if_fail (EV_IS_DOCUMENT_MODEL (model), 0);
476 return model->rotation;
480 ev_document_model_set_inverted_colors (EvDocumentModel *model,
481 gboolean inverted_colors)
483 g_return_if_fail (EV_IS_DOCUMENT_MODEL (model));
485 if (inverted_colors == model->inverted_colors)
488 model->inverted_colors = inverted_colors;
490 g_object_notify (G_OBJECT (model), "inverted-colors");
494 ev_document_model_get_inverted_colors (EvDocumentModel *model)
496 g_return_val_if_fail (EV_IS_DOCUMENT_MODEL (model), FALSE);
498 return model->inverted_colors;
502 ev_document_model_set_continuous (EvDocumentModel *model,
505 g_return_if_fail (EV_IS_DOCUMENT_MODEL (model));
507 continuous = continuous != FALSE;
509 if (continuous == model->continuous)
512 model->continuous = continuous;
514 g_object_notify (G_OBJECT (model), "continuous");
518 ev_document_model_get_continuous (EvDocumentModel *model)
520 g_return_val_if_fail (EV_IS_DOCUMENT_MODEL (model), TRUE);
522 return model->continuous;
526 ev_document_model_set_dual_page (EvDocumentModel *model,
529 g_return_if_fail (EV_IS_DOCUMENT_MODEL (model));
531 dual_page = dual_page != FALSE;
533 if (dual_page == model->dual_page)
536 model->dual_page = dual_page;
538 g_object_notify (G_OBJECT (model), "dual-page");
542 ev_document_model_get_dual_page (EvDocumentModel *model)
544 g_return_val_if_fail (EV_IS_DOCUMENT_MODEL (model), FALSE);
546 return model->dual_page;
550 ev_document_model_set_fullscreen (EvDocumentModel *model,
553 g_return_if_fail (EV_IS_DOCUMENT_MODEL (model));
555 fullscreen = fullscreen != FALSE;
557 if (fullscreen == model->fullscreen)
560 model->fullscreen = fullscreen;
562 g_object_notify (G_OBJECT (model), "fullscreen");
566 ev_document_model_get_fullscreen (EvDocumentModel *model)
568 g_return_val_if_fail (EV_IS_DOCUMENT_MODEL (model), FALSE);
570 return model->fullscreen;