]> www.fi.muni.cz Git - evince.git/blob - lib/ev-tooltip.c
14326ab2b9b19a17a9395ad0d51557affe8bcded
[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 struct _EvTooltipPrivate {
32         GtkWidget *label;
33 };
34
35 G_DEFINE_TYPE (EvTooltip, ev_tooltip, GTK_TYPE_WINDOW)
36
37 #define EV_TOOLTIP_GET_PRIVATE(object) \
38                 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_TOOLTIP, EvTooltipPrivate))
39
40 static gboolean
41 ev_tooltip_expose_event (GtkWidget      *widget,
42                          GdkEventExpose *event)
43 {
44         gtk_paint_flat_box (widget->style, widget->window,
45                             GTK_STATE_NORMAL, GTK_SHADOW_OUT,
46                             NULL, widget, "tooltip", 0, 0,
47                             widget->allocation.width, widget->allocation.height);
48
49         return GTK_WIDGET_CLASS (ev_tooltip_parent_class)->expose_event (widget, event);
50 }
51
52 static void
53 ev_tooltip_class_init (EvTooltipClass *class)
54 {
55         GObjectClass *g_object_class = G_OBJECT_CLASS (class);
56         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
57
58         widget_class->expose_event = ev_tooltip_expose_event;
59
60         g_type_class_add_private (g_object_class, sizeof (EvTooltipPrivate));
61 }
62
63 static void
64 ev_tooltip_init (EvTooltip *tooltip)
65 {
66         GtkWidget *window = GTK_WIDGET (tooltip);
67         GtkWidget *label;
68
69         tooltip->priv = EV_TOOLTIP_GET_PRIVATE (tooltip);
70
71         gtk_widget_set_name (window, "gtk-tooltips");
72
73         label = gtk_label_new (NULL);
74         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
75         gtk_container_add (GTK_CONTAINER (window), label);
76         gtk_container_set_border_width (GTK_CONTAINER (window), 3);
77         tooltip->priv->label = label;
78
79         gtk_widget_show (label);
80 }
81
82 /* Public functions */
83
84 GtkWidget *
85 ev_tooltip_new (GtkWidget *parent)
86 {
87         GtkWidget *tooltip;
88         GtkWidget *toplevel;
89
90         tooltip = g_object_new (EV_TYPE_TOOLTIP, NULL);
91
92         GTK_WINDOW (tooltip)->type = GTK_WINDOW_POPUP;
93         EV_TOOLTIP (tooltip)->parent = parent;
94
95         toplevel = gtk_widget_get_toplevel (parent);
96         gtk_window_set_transient_for (GTK_WINDOW (tooltip), GTK_WINDOW (toplevel));
97
98         return tooltip;
99 }
100
101 void
102 ev_tooltip_set_text (EvTooltip *tooltip, const char *text)
103 {
104         gtk_label_set_text (GTK_LABEL (tooltip->priv->label), text);
105 }
106
107 void
108 ev_tooltip_set_position (EvTooltip *tooltip, int x, int y)
109 {
110         int root_x = 0, root_y = 0;
111
112         if (tooltip->parent != NULL) {
113                 gdk_window_get_origin (tooltip->parent->window, &root_x, &root_y);
114         }
115
116         gtk_window_move (GTK_WINDOW (tooltip), x + root_x, y + root_y);
117 }