1 /* ev-annotation-window.c
2 * this file is part of evince, a gnome document viewer
4 * Copyright (C) 2009 Carlos Garcia Campos <carlosgc@gnome.org>
5 * Copyright (C) 2007 IƱigo Martinez <inigomartinez@gmail.com>
7 * Evince is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Evince is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include "ev-annotation-window.h"
27 #include "ev-stock-icons.h"
28 #include "ev-view-marshal.h"
29 #include "ev-document-misc.h"
43 struct _EvAnnotationWindow {
44 GtkWindow base_instance;
46 EvAnnotation *annotation;
50 GtkWidget *close_button;
65 struct _EvAnnotationWindowClass {
66 GtkWindowClass base_class;
68 void (* closed) (EvAnnotationWindow *window);
69 void (* moved) (EvAnnotationWindow *window,
74 static guint signals[N_SIGNALS];
76 G_DEFINE_TYPE (EvAnnotationWindow, ev_annotation_window, GTK_TYPE_WINDOW)
78 #define EV_ICON_SIZE_ANNOT_WINDOW (ev_annotation_window_get_icon_size())
80 /* Cut and paste from gtkwindow.c */
82 send_focus_change (GtkWidget *widget,
85 GdkEvent *fevent = gdk_event_new (GDK_FOCUS_CHANGE);
87 fevent->focus_change.type = GDK_FOCUS_CHANGE;
88 fevent->focus_change.window = gtk_widget_get_window (widget);
89 fevent->focus_change.in = in;
90 if (fevent->focus_change.window)
91 g_object_ref (fevent->focus_change.window);
93 gtk_widget_send_focus_change (widget, fevent);
95 gdk_event_free (fevent);
99 get_screen_dpi (EvAnnotationWindow *window)
103 screen = gtk_window_get_screen (GTK_WINDOW (window));
104 return ev_document_misc_get_screen_dpi (screen);
108 ev_annotation_window_get_icon_size (void)
110 static GtkIconSize icon_size = 0;
112 if (G_UNLIKELY (icon_size == 0))
113 icon_size = gtk_icon_size_register ("ev-icon-size-annot-window", 8, 8);
119 ev_annotation_window_sync_contents (EvAnnotationWindow *window)
122 GtkTextIter start, end;
123 GtkTextBuffer *buffer;
124 EvAnnotation *annot = window->annotation;
126 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (window->text_view));
127 gtk_text_buffer_get_bounds (buffer, &start, &end);
128 contents = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
129 ev_annotation_set_contents (annot, contents);
134 ev_annotation_window_set_color (EvAnnotationWindow *window,
137 GtkRcStyle *rc_style;
142 /* Apply colors to style */
143 rc_style = gtk_widget_get_modifier_style (GTK_WIDGET (window));
144 rc_style->base[GTK_STATE_NORMAL] = gcolor;
145 rc_style->bg[GTK_STATE_PRELIGHT] = gcolor;
146 rc_style->bg[GTK_STATE_NORMAL] = gcolor;
147 rc_style->bg[GTK_STATE_ACTIVE] = gcolor;
148 rc_style->color_flags[GTK_STATE_PRELIGHT] = GTK_RC_BG;
149 rc_style->color_flags[GTK_STATE_NORMAL] = GTK_RC_BG | GTK_RC_BASE;
150 rc_style->color_flags[GTK_STATE_ACTIVE] = GTK_RC_BG;
152 /* Apply the style to the widgets */
153 g_object_ref (rc_style);
154 gtk_widget_modify_style (GTK_WIDGET (window), rc_style);
155 gtk_widget_modify_style (window->close_button, rc_style);
156 gtk_widget_modify_style (window->resize_se, rc_style);
157 gtk_widget_modify_style (window->resize_sw, rc_style);
158 g_object_unref (rc_style);
162 ev_annotation_window_label_changed (EvAnnotationMarkup *annot,
164 EvAnnotationWindow *window)
166 const gchar *label = ev_annotation_markup_get_label (annot);
168 gtk_window_set_title (GTK_WINDOW (window), label);
169 gtk_label_set_text (GTK_LABEL (window->title), label);
173 ev_annotation_window_color_changed (EvAnnotation *annot,
175 EvAnnotationWindow *window)
179 ev_annotation_get_color (annot, &color);
180 ev_annotation_window_set_color (window, &color);
184 ev_annotation_window_dispose (GObject *object)
186 EvAnnotationWindow *window = EV_ANNOTATION_WINDOW (object);
188 if (window->annotation) {
189 ev_annotation_window_sync_contents (window);
190 g_object_unref (window->annotation);
191 window->annotation = NULL;
194 (* G_OBJECT_CLASS (ev_annotation_window_parent_class)->dispose) (object);
198 ev_annotation_window_set_property (GObject *object,
203 EvAnnotationWindow *window = EV_ANNOTATION_WINDOW (object);
206 case PROP_ANNOTATION:
207 window->annotation = g_value_dup_object (value);
210 window->parent = g_value_get_object (value);
213 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
218 ev_annotation_window_resize (EvAnnotationWindow *window,
219 GdkEventButton *event,
222 if (event->type == GDK_BUTTON_PRESS && event->button == 1) {
223 gtk_window_begin_resize_drag (GTK_WINDOW (window),
224 window->resize_sw == ebox ?
225 GDK_WINDOW_EDGE_SOUTH_WEST :
226 GDK_WINDOW_EDGE_SOUTH_EAST,
227 event->button, event->x_root,
228 event->y_root, event->time);
236 ev_annotation_window_set_resize_cursor (GtkWidget *widget,
237 EvAnnotationWindow *window)
239 GdkWindow *gdk_window = gtk_widget_get_window (widget);
244 if (gtk_widget_is_sensitive (widget)) {
245 GdkDisplay *display = gtk_widget_get_display (widget);
248 cursor = gdk_cursor_new_for_display (display,
249 widget == window->resize_sw ?
250 GDK_BOTTOM_LEFT_CORNER :
251 GDK_BOTTOM_RIGHT_CORNER);
252 gdk_window_set_cursor (gdk_window, cursor);
253 gdk_cursor_unref (cursor);
255 gdk_window_set_cursor (gdk_window, NULL);
260 text_view_button_press (GtkWidget *widget,
261 GdkEventButton *event,
262 EvAnnotationWindow *window)
264 ev_annotation_window_grab_focus (window);
270 ev_annotation_window_close (EvAnnotationWindow *window)
272 gtk_widget_hide (GTK_WIDGET (window));
273 g_signal_emit (window, signals[CLOSED], 0);
277 ev_annotation_window_init (EvAnnotationWindow *window)
279 GtkWidget *vbox, *hbox;
283 gtk_widget_set_can_focus (GTK_WIDGET (window), TRUE);
285 vbox = gtk_vbox_new (FALSE, 0);
288 hbox = gtk_hbox_new (FALSE, 0);
290 icon = gtk_image_new (); /* FIXME: use the annot icon */
291 gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);
292 gtk_widget_show (icon);
294 window->title = gtk_label_new (NULL);
295 gtk_box_pack_start (GTK_BOX (hbox), window->title, TRUE, TRUE, 0);
296 gtk_widget_show (window->title);
298 window->close_button = gtk_button_new ();
299 gtk_button_set_relief (GTK_BUTTON (window->close_button), GTK_RELIEF_NONE);
300 gtk_container_set_border_width (GTK_CONTAINER (window->close_button), 0);
301 g_signal_connect_swapped (window->close_button, "clicked",
302 G_CALLBACK (ev_annotation_window_close),
304 icon = gtk_image_new_from_stock (EV_STOCK_CLOSE, EV_ICON_SIZE_ANNOT_WINDOW);
305 gtk_container_add (GTK_CONTAINER (window->close_button), icon);
306 gtk_widget_show (icon);
308 gtk_box_pack_start (GTK_BOX (hbox), window->close_button, FALSE, FALSE, 0);
309 gtk_widget_show (window->close_button);
311 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
312 gtk_widget_show (hbox);
315 swindow = gtk_scrolled_window_new (NULL, NULL);
316 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
317 GTK_POLICY_AUTOMATIC,
318 GTK_POLICY_AUTOMATIC);
319 window->text_view = gtk_text_view_new ();
320 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (window->text_view), GTK_WRAP_WORD);
321 g_signal_connect (window->text_view, "button_press_event",
322 G_CALLBACK (text_view_button_press),
324 gtk_container_add (GTK_CONTAINER (swindow), window->text_view);
325 gtk_widget_show (window->text_view);
327 gtk_box_pack_start (GTK_BOX (vbox), swindow, TRUE, TRUE, 0);
328 gtk_widget_show (swindow);
331 gtk_window_set_has_resize_grip (GTK_WINDOW(window), FALSE);
332 hbox = gtk_hbox_new (FALSE, 0);
334 window->resize_sw = gtk_event_box_new ();
335 gtk_widget_add_events (window->resize_sw, GDK_BUTTON_PRESS_MASK);
336 g_signal_connect_swapped (window->resize_sw, "button-press-event",
337 G_CALLBACK (ev_annotation_window_resize),
339 g_signal_connect (window->resize_sw, "realize",
340 G_CALLBACK (ev_annotation_window_set_resize_cursor),
343 icon = gtk_image_new_from_stock (EV_STOCK_RESIZE_SW, EV_ICON_SIZE_ANNOT_WINDOW);
344 gtk_container_add (GTK_CONTAINER (window->resize_sw), icon);
345 gtk_widget_show (icon);
346 gtk_box_pack_start (GTK_BOX (hbox), window->resize_sw, FALSE, FALSE, 0);
347 gtk_widget_show (window->resize_sw);
349 window->resize_se = gtk_event_box_new ();
350 gtk_widget_add_events (window->resize_se, GDK_BUTTON_PRESS_MASK);
351 g_signal_connect_swapped (window->resize_se, "button-press-event",
352 G_CALLBACK (ev_annotation_window_resize),
354 g_signal_connect (window->resize_se, "realize",
355 G_CALLBACK (ev_annotation_window_set_resize_cursor),
358 icon = gtk_image_new_from_stock (EV_STOCK_RESIZE_SE, EV_ICON_SIZE_ANNOT_WINDOW);
359 gtk_container_add (GTK_CONTAINER (window->resize_se), icon);
360 gtk_widget_show (icon);
361 gtk_box_pack_end (GTK_BOX (hbox), window->resize_se, FALSE, FALSE, 0);
362 gtk_widget_show (window->resize_se);
364 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
365 gtk_widget_show (hbox);
367 gtk_container_add (GTK_CONTAINER (window), vbox);
368 gtk_container_set_border_width (GTK_CONTAINER (window), 0);
369 gtk_widget_show (vbox);
371 gtk_widget_add_events (GTK_WIDGET (window),
372 GDK_BUTTON_PRESS_MASK |
374 gtk_widget_set_app_paintable (GTK_WIDGET (window), TRUE);
376 gtk_container_set_border_width (GTK_CONTAINER (window), 2);
378 gtk_window_set_accept_focus (GTK_WINDOW (window), TRUE);
379 gtk_window_set_decorated (GTK_WINDOW (window), FALSE);
380 gtk_window_set_skip_taskbar_hint (GTK_WINDOW (window), TRUE);
381 gtk_window_set_skip_pager_hint (GTK_WINDOW (window), TRUE);
382 gtk_window_set_resizable (GTK_WINDOW (window), TRUE);
386 ev_annotation_window_constructor (GType type,
387 guint n_construct_properties,
388 GObjectConstructParam *construct_params)
391 EvAnnotationWindow *window;
393 EvAnnotationMarkup *markup;
394 const gchar *contents;
400 object = G_OBJECT_CLASS (ev_annotation_window_parent_class)->constructor (type,
401 n_construct_properties,
403 window = EV_ANNOTATION_WINDOW (object);
404 annot = window->annotation;
405 markup = EV_ANNOTATION_MARKUP (annot);
407 gtk_window_set_transient_for (GTK_WINDOW (window), window->parent);
408 gtk_window_set_destroy_with_parent (GTK_WINDOW (window), FALSE);
410 label = ev_annotation_markup_get_label (markup);
411 window->is_open = ev_annotation_markup_get_popup_is_open (markup);
412 ev_annotation_markup_get_rectangle (markup, &window->rect);
414 rect = &window->rect;
416 /* Rectangle is at doc resolution (72.0) */
417 scale = get_screen_dpi (window) / 72.0;
418 gtk_window_resize (GTK_WINDOW (window),
419 (gint)((rect->x2 - rect->x1) * scale),
420 (gint)((rect->y2 - rect->y1) * scale));
422 ev_annotation_get_color (annot, &color);
423 ev_annotation_window_set_color (window, &color);
424 gtk_widget_set_name (GTK_WIDGET (window), ev_annotation_get_name (annot));
425 gtk_window_set_title (GTK_WINDOW (window), label);
426 gtk_label_set_text (GTK_LABEL (window->title), label);
428 contents = ev_annotation_get_contents (annot);
430 GtkTextBuffer *buffer;
432 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (window->text_view));
433 gtk_text_buffer_set_text (buffer, contents, -1);
436 g_signal_connect (annot, "notify::label",
437 G_CALLBACK (ev_annotation_window_label_changed),
439 g_signal_connect (annot, "notify::color",
440 G_CALLBACK (ev_annotation_window_color_changed),
447 ev_annotation_window_button_press_event (GtkWidget *widget,
448 GdkEventButton *event)
450 EvAnnotationWindow *window = EV_ANNOTATION_WINDOW (widget);
452 if (event->type == GDK_BUTTON_PRESS && event->button == 1) {
453 window->in_move = TRUE;
454 window->x = event->x_root - event->x;
455 window->y = event->y_root - event->y;
456 gtk_window_begin_move_drag (GTK_WINDOW (widget),
468 ev_annotation_window_configure_event (GtkWidget *widget,
469 GdkEventConfigure *event)
471 EvAnnotationWindow *window = EV_ANNOTATION_WINDOW (widget);
473 if (window->in_move &&
474 (window->x != event->x || window->y != event->y)) {
475 window->x = event->x;
476 window->y = event->y;
479 return GTK_WIDGET_CLASS (ev_annotation_window_parent_class)->configure_event (widget, event);
483 ev_annotation_window_focus_in_event (GtkWidget *widget,
484 GdkEventFocus *event)
486 EvAnnotationWindow *window = EV_ANNOTATION_WINDOW (widget);
488 if (window->in_move) {
489 if (window->orig_x != window->x || window->orig_y != window->y) {
490 window->orig_x = window->x;
491 window->orig_y = window->y;
492 g_signal_emit (window, signals[MOVED], 0, window->x, window->y);
494 window->in_move = FALSE;
501 ev_annotation_window_focus_out_event (GtkWidget *widget,
502 GdkEventFocus *event)
504 EvAnnotationWindow *window = EV_ANNOTATION_WINDOW (widget);
506 ev_annotation_window_sync_contents (window);
512 ev_annotation_window_class_init (EvAnnotationWindowClass *klass)
514 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
515 GtkWidgetClass *gtk_widget_class = GTK_WIDGET_CLASS (klass);
517 g_object_class->constructor = ev_annotation_window_constructor;
518 g_object_class->set_property = ev_annotation_window_set_property;
519 g_object_class->dispose = ev_annotation_window_dispose;
521 gtk_widget_class->button_press_event = ev_annotation_window_button_press_event;
522 gtk_widget_class->configure_event = ev_annotation_window_configure_event;
523 gtk_widget_class->focus_in_event = ev_annotation_window_focus_in_event;
524 gtk_widget_class->focus_out_event = ev_annotation_window_focus_out_event;
526 g_object_class_install_property (g_object_class,
528 g_param_spec_object ("annotation",
530 "The annotation associated to the window",
531 EV_TYPE_ANNOTATION_MARKUP,
533 G_PARAM_CONSTRUCT_ONLY));
534 g_object_class_install_property (g_object_class,
536 g_param_spec_object ("parent",
541 G_PARAM_CONSTRUCT_ONLY));
543 g_signal_new ("closed",
544 G_TYPE_FROM_CLASS (g_object_class),
545 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
546 G_STRUCT_OFFSET (EvAnnotationWindowClass, closed),
548 g_cclosure_marshal_VOID__VOID,
549 G_TYPE_NONE, 0, G_TYPE_NONE);
551 g_signal_new ("moved",
552 G_TYPE_FROM_CLASS (g_object_class),
553 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
554 G_STRUCT_OFFSET (EvAnnotationWindowClass, moved),
556 ev_view_marshal_VOID__INT_INT,
558 G_TYPE_INT, G_TYPE_INT);
563 ev_annotation_window_new (EvAnnotation *annot,
568 g_return_val_if_fail (EV_IS_ANNOTATION_MARKUP (annot), NULL);
569 g_return_val_if_fail (GTK_IS_WINDOW (parent), NULL);
571 window = g_object_new (EV_TYPE_ANNOTATION_WINDOW,
579 ev_annotation_window_get_annotation (EvAnnotationWindow *window)
581 g_return_val_if_fail (EV_IS_ANNOTATION_WINDOW (window), NULL);
583 return window->annotation;
587 ev_annotation_window_set_annotation (EvAnnotationWindow *window,
590 g_return_if_fail (EV_IS_ANNOTATION_WINDOW (window));
591 g_return_if_fail (EV_IS_ANNOTATION (annot));
593 if (annot == window->annotation)
596 g_object_unref (window->annotation);
597 window->annotation = g_object_ref (annot);
598 ev_annotation_window_sync_contents (window);
599 g_object_notify (G_OBJECT (window), "annotation");
603 ev_annotation_window_is_open (EvAnnotationWindow *window)
605 g_return_val_if_fail (EV_IS_ANNOTATION_WINDOW (window), FALSE);
607 return window->is_open;
611 ev_annotation_window_get_rectangle (EvAnnotationWindow *window,
614 g_return_if_fail (EV_IS_ANNOTATION_WINDOW (window));
615 g_return_if_fail (rect != NULL);
617 *rect = window->rect;
621 ev_annotation_window_set_rectangle (EvAnnotationWindow *window,
622 const EvRectangle *rect)
624 g_return_if_fail (EV_IS_ANNOTATION_WINDOW (window));
625 g_return_if_fail (rect != NULL);
627 window->rect = *rect;
631 ev_annotation_window_grab_focus (EvAnnotationWindow *window)
633 g_return_if_fail (EV_IS_ANNOTATION_WINDOW (window));
635 if (!gtk_widget_has_focus (window->text_view)) {
636 gtk_widget_grab_focus (GTK_WIDGET (window));
637 send_focus_change (window->text_view, TRUE);
642 ev_annotation_window_ungrab_focus (EvAnnotationWindow *window)
644 g_return_if_fail (EV_IS_ANNOTATION_WINDOW (window));
646 if (gtk_widget_has_focus (window->text_view)) {
647 send_focus_change (window->text_view, FALSE);
650 ev_annotation_window_sync_contents (window);