]> www.fi.muni.cz Git - evince.git/blob - shell/ev-tooltip.c
Add ChangeLog.
[evince.git] / shell / ev-tooltip.c
1 /* this file is part of evince, a gnome document viewer
2  *
3  *  Copyright (C) 2004 Red Hat, Inc.
4  *
5  *  Author:
6  *    Marco Pesenti Gritti <marco@gnome.org>
7  *
8  * Evince is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * Evince is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gtk/gtk.h>
28
29 #include "ev-tooltip.h"
30
31 #define DEFAULT_DELAY 500
32 #define STICKY_DELAY 500
33 #define STICKY_REVERT_DELAY 1000
34 #define SPACE_FROM_CURSOR 10
35
36 struct _EvTooltipPrivate {
37         GtkWidget *label;
38         GTimeVal last_deactivate;
39         int timer_tag;
40         gboolean active;
41 };
42
43 G_DEFINE_TYPE (EvTooltip, ev_tooltip, GTK_TYPE_WINDOW)
44
45 #define EV_TOOLTIP_GET_PRIVATE(object) \
46                 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_TOOLTIP, EvTooltipPrivate))
47
48 static gboolean
49 ev_tooltip_expose_event (GtkWidget      *widget,
50                          GdkEventExpose *event)
51 {
52         gtk_paint_flat_box (widget->style, widget->window,
53                             GTK_STATE_NORMAL, GTK_SHADOW_OUT,
54                             NULL, widget, "tooltip", 0, 0,
55                             widget->allocation.width, widget->allocation.height);
56
57         return GTK_WIDGET_CLASS (ev_tooltip_parent_class)->expose_event (widget, event);
58 }
59
60 static void
61 ev_tooltip_dispose (GObject *object)
62 {
63         EvTooltip *tooltip = EV_TOOLTIP (object);
64
65         if (tooltip->priv->timer_tag) {
66                 g_source_remove (tooltip->priv->timer_tag);
67                 tooltip->priv->timer_tag = 0;
68         }
69
70         G_OBJECT_CLASS (ev_tooltip_parent_class)->dispose (object);
71 }
72
73 static void
74 ev_tooltip_class_init (EvTooltipClass *class)
75 {
76         GObjectClass *g_object_class = G_OBJECT_CLASS (class);
77         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
78
79         g_object_class->dispose = ev_tooltip_dispose;
80         widget_class->expose_event = ev_tooltip_expose_event;
81
82         g_type_class_add_private (g_object_class, sizeof (EvTooltipPrivate));
83 }
84
85 static void
86 ev_tooltip_init (EvTooltip *tooltip)
87 {
88         GtkWidget *window = GTK_WIDGET (tooltip);
89         GtkWidget *label;
90
91         tooltip->priv = EV_TOOLTIP_GET_PRIVATE (tooltip);
92
93         gtk_widget_set_app_paintable (GTK_WIDGET (tooltip), TRUE);
94         gtk_window_set_resizable (GTK_WINDOW (tooltip), FALSE);
95         gtk_widget_set_name (window, "gtk-tooltips");
96
97         label = gtk_label_new (NULL);
98         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
99         gtk_container_add (GTK_CONTAINER (window), label);
100         gtk_container_set_border_width (GTK_CONTAINER (window), 3);
101         tooltip->priv->label = label;
102
103         gtk_widget_show (label);
104 }
105
106 /* Public functions */
107
108 GtkWidget *
109 ev_tooltip_new (GtkWidget *parent)
110 {
111         GtkWidget *tooltip;
112         GtkWidget *toplevel;
113
114         tooltip = g_object_new (EV_TYPE_TOOLTIP, NULL);
115
116         GTK_WINDOW (tooltip)->type = GTK_WINDOW_POPUP;
117         EV_TOOLTIP (tooltip)->parent = parent;
118
119         toplevel = gtk_widget_get_toplevel (parent);
120         gtk_window_set_transient_for (GTK_WINDOW (tooltip), GTK_WINDOW (toplevel));
121
122         return tooltip;
123 }
124
125 void
126 ev_tooltip_set_text (EvTooltip *tooltip, const char *text)
127 {
128         gtk_label_set_text (GTK_LABEL (tooltip->priv->label), text);
129 }
130
131 void
132 ev_tooltip_set_position (EvTooltip *tooltip, int x, int y)
133 {
134         int root_x, root_y;
135
136         if (tooltip->parent != NULL) {
137                 gdk_window_get_origin (tooltip->parent->window, &root_x, &root_y);
138         }
139
140         gtk_window_move (GTK_WINDOW (tooltip),
141                          x + root_x + SPACE_FROM_CURSOR,
142                          y + root_y + SPACE_FROM_CURSOR);
143 }
144
145 static gboolean
146 ev_tooltip_recently_shown (EvTooltip *tooltip)
147 {
148         GTimeVal now;
149         glong msec;
150   
151         g_get_current_time (&now);
152
153         msec = (now.tv_sec  - tooltip->priv->last_deactivate.tv_sec) * 1000 +
154                (now.tv_usec - tooltip->priv->last_deactivate.tv_usec) / 1000;
155
156         return (msec < STICKY_REVERT_DELAY);
157 }
158
159 static gint
160 ev_tooltip_timeout (gpointer data)
161 {
162         GtkWidget *tooltip = GTK_WIDGET (data);
163
164         gtk_widget_show (tooltip);
165
166         return FALSE;
167 }
168
169 void
170 ev_tooltip_activate (EvTooltip *tooltip)
171 {
172         int delay;
173
174         if (tooltip->priv->active) {
175                 return;
176         } else {
177                 tooltip->priv->active = TRUE;
178         }
179
180         if (ev_tooltip_recently_shown (tooltip)) {
181                 delay = STICKY_DELAY;
182         } else {
183                 delay = DEFAULT_DELAY;
184         }
185
186         tooltip->priv->timer_tag = g_timeout_add (delay, ev_tooltip_timeout,
187                                                   (gpointer)tooltip);
188 }
189
190 void
191 ev_tooltip_deactivate (EvTooltip *tooltip)
192 {
193         if (!tooltip->priv->active) {
194                 return;
195         } else {
196                 tooltip->priv->active = FALSE;
197         }
198
199         if (tooltip->priv->timer_tag) {
200                 g_source_remove (tooltip->priv->timer_tag);
201                 tooltip->priv->timer_tag = 0;
202         }
203
204         gtk_widget_hide (GTK_WIDGET (tooltip));
205
206         g_get_current_time (&tooltip->priv->last_deactivate);
207 }