]> www.fi.muni.cz Git - evince.git/blob - lib/ev-tooltip.c
Delay popup showing. Mirror gtk logic/times.
[evince.git] / lib / 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  *    Jonathan Blandford <jrb@alum.mit.edu>
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 "ev-tooltip.h"
28
29 #include <gtk/gtklabel.h>
30
31 #define DEFAULT_DELAY 500
32 #define STICKY_DELAY 0
33 #define STICKY_REVERT_DELAY 1000
34
35 struct _EvTooltipPrivate {
36         GtkWidget *label;
37         GTimeVal last_deactivate;
38         int timer_tag;
39         gboolean active;
40 };
41
42 G_DEFINE_TYPE (EvTooltip, ev_tooltip, GTK_TYPE_WINDOW)
43
44 #define EV_TOOLTIP_GET_PRIVATE(object) \
45                 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_TOOLTIP, EvTooltipPrivate))
46
47 static gboolean
48 ev_tooltip_expose_event (GtkWidget      *widget,
49                          GdkEventExpose *event)
50 {
51         gtk_paint_flat_box (widget->style, widget->window,
52                             GTK_STATE_NORMAL, GTK_SHADOW_OUT,
53                             NULL, widget, "tooltip", 0, 0,
54                             widget->allocation.width, widget->allocation.height);
55
56         return GTK_WIDGET_CLASS (ev_tooltip_parent_class)->expose_event (widget, event);
57 }
58
59 static void
60 ev_tooltip_dispose (GObject *object)
61 {
62         EvTooltip *tooltip = EV_TOOLTIP (object);
63
64         if (tooltip->priv->timer_tag) {
65                 g_source_remove (tooltip->priv->timer_tag);
66                 tooltip->priv->timer_tag = 0;
67         }
68 }
69
70 static void
71 ev_tooltip_class_init (EvTooltipClass *class)
72 {
73         GObjectClass *g_object_class = G_OBJECT_CLASS (class);
74         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
75
76         g_object_class->dispose = ev_tooltip_dispose;
77         widget_class->expose_event = ev_tooltip_expose_event;
78
79         g_type_class_add_private (g_object_class, sizeof (EvTooltipPrivate));
80 }
81
82 static void
83 ev_tooltip_init (EvTooltip *tooltip)
84 {
85         GtkWidget *window = GTK_WIDGET (tooltip);
86         GtkWidget *label;
87
88         tooltip->priv = EV_TOOLTIP_GET_PRIVATE (tooltip);
89
90         gtk_widget_set_name (window, "gtk-tooltips");
91
92         label = gtk_label_new (NULL);
93         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
94         gtk_container_add (GTK_CONTAINER (window), label);
95         gtk_container_set_border_width (GTK_CONTAINER (window), 3);
96         tooltip->priv->label = label;
97
98         gtk_widget_show (label);
99 }
100
101 /* Public functions */
102
103 GtkWidget *
104 ev_tooltip_new (GtkWidget *parent)
105 {
106         GtkWidget *tooltip;
107         GtkWidget *toplevel;
108
109         tooltip = g_object_new (EV_TYPE_TOOLTIP, NULL);
110
111         GTK_WINDOW (tooltip)->type = GTK_WINDOW_POPUP;
112         EV_TOOLTIP (tooltip)->parent = parent;
113
114         toplevel = gtk_widget_get_toplevel (parent);
115         gtk_window_set_transient_for (GTK_WINDOW (tooltip), GTK_WINDOW (toplevel));
116
117         return tooltip;
118 }
119
120 void
121 ev_tooltip_set_text (EvTooltip *tooltip, const char *text)
122 {
123         gtk_label_set_text (GTK_LABEL (tooltip->priv->label), text);
124 }
125
126 void
127 ev_tooltip_set_position (EvTooltip *tooltip, int x, int y)
128 {
129         int root_x = 0, root_y = 0;
130
131         if (tooltip->parent != NULL) {
132                 gdk_window_get_origin (tooltip->parent->window, &root_x, &root_y);
133         }
134
135         gtk_window_move (GTK_WINDOW (tooltip), x + root_x, y + root_y);
136 }
137
138 static gboolean
139 ev_tooltip_recently_shown (EvTooltip *tooltip)
140 {
141         GTimeVal now;
142         glong msec;
143   
144         g_get_current_time (&now);
145
146         msec = (now.tv_sec  - tooltip->priv->last_deactivate.tv_sec) * 1000 +
147                (now.tv_usec - tooltip->priv->last_deactivate.tv_usec) / 1000;
148
149         return (msec < STICKY_REVERT_DELAY);
150 }
151
152 static gint
153 ev_tooltip_timeout (gpointer data)
154 {
155         GtkWidget *tooltip = GTK_WIDGET (data);
156
157         gtk_widget_show (tooltip);
158
159         return FALSE;
160 }
161
162 void
163 ev_tooltip_activate (EvTooltip *tooltip)
164 {
165         int delay;
166
167         if (tooltip->priv->active) {
168                 return;
169         } else {
170                 tooltip->priv->active = TRUE;
171         }
172
173         if (ev_tooltip_recently_shown (tooltip)) {
174                 delay = STICKY_DELAY;
175         } else {
176                 delay = DEFAULT_DELAY;
177         }
178
179         tooltip->priv->timer_tag = g_timeout_add (delay, ev_tooltip_timeout,
180                                                   (gpointer)tooltip);
181 }
182
183 void
184 ev_tooltip_deactivate (EvTooltip *tooltip)
185 {
186         if (!tooltip->priv->active) {
187                 return;
188         } else {
189                 tooltip->priv->active = FALSE;
190         }
191
192         if (tooltip->priv->timer_tag) {
193                 g_source_remove (tooltip->priv->timer_tag);
194                 tooltip->priv->timer_tag = 0;
195         }
196
197         gtk_widget_hide (GTK_WIDGET (tooltip));
198
199         g_get_current_time (&tooltip->priv->last_deactivate);
200 }