1 | /* Metacity fixed tooltip routine */ |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) 2001 Havoc Pennington |
---|
5 | * Copyright (C) 2003-2006 Vincent Untz |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License as |
---|
9 | * published by the Free Software Foundation; either version 2 of the |
---|
10 | * License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program 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. |
---|
16 | * |
---|
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., 59 Temple Place - Suite 330, Boston, MA |
---|
20 | * 02111-1307, USA. |
---|
21 | */ |
---|
22 | |
---|
23 | #include "fixedtip.h" |
---|
24 | |
---|
25 | /* Signals */ |
---|
26 | enum |
---|
27 | { |
---|
28 | CLICKED, |
---|
29 | LAST_SIGNAL |
---|
30 | }; |
---|
31 | |
---|
32 | static guint fixedtip_signals[LAST_SIGNAL] = { 0 }; |
---|
33 | |
---|
34 | struct _NaFixedTipPrivate |
---|
35 | { |
---|
36 | GtkWidget *parent; |
---|
37 | GtkWidget *label; |
---|
38 | GtkOrientation orientation; |
---|
39 | }; |
---|
40 | |
---|
41 | G_DEFINE_TYPE (NaFixedTip, na_fixed_tip, GTK_TYPE_WINDOW) |
---|
42 | |
---|
43 | static gboolean |
---|
44 | button_press_handler (GtkWidget *fixedtip, |
---|
45 | GdkEventButton *event, |
---|
46 | gpointer data) |
---|
47 | { |
---|
48 | if (event->button == 1 && event->type == GDK_BUTTON_PRESS) |
---|
49 | g_signal_emit (fixedtip, fixedtip_signals[CLICKED], 0); |
---|
50 | |
---|
51 | return FALSE; |
---|
52 | } |
---|
53 | |
---|
54 | static gboolean |
---|
55 | expose_handler (GtkWidget *fixedtip) |
---|
56 | { |
---|
57 | GtkRequisition req; |
---|
58 | |
---|
59 | gtk_widget_size_request (fixedtip, &req); |
---|
60 | |
---|
61 | gtk_paint_flat_box (fixedtip->style, fixedtip->window, |
---|
62 | GTK_STATE_NORMAL, GTK_SHADOW_OUT, |
---|
63 | NULL, fixedtip, "tooltip", |
---|
64 | 0, 0, req.width, req.height); |
---|
65 | |
---|
66 | return FALSE; |
---|
67 | } |
---|
68 | |
---|
69 | static void |
---|
70 | na_fixed_tip_class_init (NaFixedTipClass *class) |
---|
71 | { |
---|
72 | fixedtip_signals[CLICKED] = |
---|
73 | g_signal_new ("clicked", |
---|
74 | G_OBJECT_CLASS_TYPE (class), |
---|
75 | G_SIGNAL_RUN_LAST, |
---|
76 | G_STRUCT_OFFSET (NaFixedTipClass, clicked), |
---|
77 | NULL, NULL, |
---|
78 | g_cclosure_marshal_VOID__VOID, |
---|
79 | G_TYPE_NONE, 0); |
---|
80 | |
---|
81 | g_type_class_add_private (class, sizeof (NaFixedTipPrivate)); |
---|
82 | } |
---|
83 | |
---|
84 | static void |
---|
85 | na_fixed_tip_init (NaFixedTip *fixedtip) |
---|
86 | { |
---|
87 | GtkWidget *label; |
---|
88 | |
---|
89 | fixedtip->priv = G_TYPE_INSTANCE_GET_PRIVATE (fixedtip, NA_TYPE_FIXED_TIP, |
---|
90 | NaFixedTipPrivate); |
---|
91 | |
---|
92 | gtk_window_set_type_hint (GTK_WINDOW (fixedtip), |
---|
93 | GDK_WINDOW_TYPE_HINT_TOOLTIP); |
---|
94 | |
---|
95 | gtk_widget_set_app_paintable (GTK_WIDGET (fixedtip), TRUE); |
---|
96 | gtk_window_set_resizable (GTK_WINDOW (fixedtip), FALSE); |
---|
97 | gtk_widget_set_name (GTK_WIDGET (fixedtip), "gtk-tooltips"); |
---|
98 | gtk_container_set_border_width (GTK_CONTAINER (fixedtip), 4); |
---|
99 | |
---|
100 | label = gtk_label_new (NULL); |
---|
101 | gtk_label_set_line_wrap (GTK_LABEL (label), TRUE); |
---|
102 | gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5); |
---|
103 | gtk_widget_show (label); |
---|
104 | gtk_container_add (GTK_CONTAINER (fixedtip), label); |
---|
105 | fixedtip->priv->label = label; |
---|
106 | |
---|
107 | g_signal_connect (fixedtip, "expose_event", |
---|
108 | G_CALLBACK (expose_handler), NULL); |
---|
109 | |
---|
110 | gtk_widget_add_events (GTK_WIDGET (fixedtip), GDK_BUTTON_PRESS_MASK); |
---|
111 | |
---|
112 | g_signal_connect (fixedtip, "button_press_event", |
---|
113 | G_CALLBACK (button_press_handler), NULL); |
---|
114 | |
---|
115 | fixedtip->priv->orientation = GTK_ORIENTATION_HORIZONTAL; |
---|
116 | } |
---|
117 | |
---|
118 | static void |
---|
119 | na_fixed_tip_position (NaFixedTip *fixedtip) |
---|
120 | { |
---|
121 | GdkScreen *screen; |
---|
122 | GtkRequisition req; |
---|
123 | int root_x; |
---|
124 | int root_y; |
---|
125 | int parent_width; |
---|
126 | int parent_height; |
---|
127 | int screen_width; |
---|
128 | int screen_height; |
---|
129 | |
---|
130 | screen = gtk_widget_get_screen (fixedtip->priv->parent); |
---|
131 | gtk_window_set_screen (GTK_WINDOW (fixedtip), screen); |
---|
132 | |
---|
133 | gtk_widget_size_request (GTK_WIDGET (fixedtip), &req); |
---|
134 | |
---|
135 | gdk_window_get_origin (fixedtip->priv->parent->window, &root_x, &root_y); |
---|
136 | gdk_drawable_get_size (GDK_DRAWABLE (fixedtip->priv->parent->window), |
---|
137 | &parent_width, &parent_height); |
---|
138 | |
---|
139 | screen_width = gdk_screen_get_width (screen); |
---|
140 | screen_height = gdk_screen_get_height (screen); |
---|
141 | |
---|
142 | /* pad between panel and message window */ |
---|
143 | #define PAD 5 |
---|
144 | |
---|
145 | if (fixedtip->priv->orientation == GTK_ORIENTATION_VERTICAL) |
---|
146 | { |
---|
147 | if (root_x <= screen_width / 2) |
---|
148 | root_x += parent_width + PAD; |
---|
149 | else |
---|
150 | root_x -= req.width + PAD; |
---|
151 | } |
---|
152 | else |
---|
153 | { |
---|
154 | if (root_y <= screen_height / 2) |
---|
155 | root_y += parent_height + PAD; |
---|
156 | else |
---|
157 | root_y -= req.height + PAD; |
---|
158 | } |
---|
159 | |
---|
160 | /* Push onscreen */ |
---|
161 | if ((root_x + req.width) > screen_width) |
---|
162 | root_x = screen_width - req.width; |
---|
163 | |
---|
164 | if ((root_y + req.height) > screen_height) |
---|
165 | root_y = screen_height - req.height; |
---|
166 | |
---|
167 | gtk_window_move (GTK_WINDOW (fixedtip), root_x, root_y); |
---|
168 | } |
---|
169 | |
---|
170 | static void |
---|
171 | na_fixed_tip_parent_size_allocated (GtkWidget *parent, |
---|
172 | GtkAllocation *allocation, |
---|
173 | NaFixedTip *fixedtip) |
---|
174 | { |
---|
175 | na_fixed_tip_position (fixedtip); |
---|
176 | } |
---|
177 | |
---|
178 | static void |
---|
179 | na_fixed_tip_parent_screen_changed (GtkWidget *parent, |
---|
180 | GdkScreen *new_screen, |
---|
181 | NaFixedTip *fixedtip) |
---|
182 | { |
---|
183 | na_fixed_tip_position (fixedtip); |
---|
184 | } |
---|
185 | |
---|
186 | GtkWidget * |
---|
187 | na_fixed_tip_new (GtkWidget *parent, |
---|
188 | GtkOrientation orientation) |
---|
189 | { |
---|
190 | NaFixedTip *fixedtip; |
---|
191 | |
---|
192 | g_return_val_if_fail (parent != NULL, NULL); |
---|
193 | |
---|
194 | fixedtip = g_object_new (NA_TYPE_FIXED_TIP, NULL); |
---|
195 | |
---|
196 | /* It doesn't work if we do this in na_fixed_tip_init(), so do it here */ |
---|
197 | GTK_WINDOW (fixedtip)->type = GTK_WINDOW_POPUP; |
---|
198 | |
---|
199 | fixedtip->priv->parent = parent; |
---|
200 | |
---|
201 | #if 0 |
---|
202 | //FIXME: would be nice to be able to get the toplevel for the tip, but this |
---|
203 | //doesn't work |
---|
204 | GtkWidget *toplevel; |
---|
205 | |
---|
206 | toplevel = gtk_widget_get_toplevel (parent); |
---|
207 | /* |
---|
208 | if (toplevel && GTK_WIDGET_TOPLEVEL (toplevel) && GTK_IS_WINDOW (toplevel)) |
---|
209 | gtk_window_set_transient_for (GTK_WINDOW (fixedtip), GTK_WINDOW (toplevel)); |
---|
210 | */ |
---|
211 | #endif |
---|
212 | |
---|
213 | fixedtip->priv->orientation = orientation; |
---|
214 | |
---|
215 | //FIXME: would be nice to move the tip when the notification area moves |
---|
216 | g_signal_connect_object (parent, "size-allocate", |
---|
217 | G_CALLBACK (na_fixed_tip_parent_size_allocated), |
---|
218 | fixedtip, 0); |
---|
219 | g_signal_connect_object (parent, "screen-changed", |
---|
220 | G_CALLBACK (na_fixed_tip_parent_screen_changed), |
---|
221 | fixedtip, 0); |
---|
222 | |
---|
223 | na_fixed_tip_position (fixedtip); |
---|
224 | |
---|
225 | return GTK_WIDGET (fixedtip); |
---|
226 | } |
---|
227 | |
---|
228 | void |
---|
229 | na_fixed_tip_set_markup (GtkWidget *widget, |
---|
230 | const char *markup_text) |
---|
231 | { |
---|
232 | NaFixedTip *fixedtip; |
---|
233 | |
---|
234 | g_return_if_fail (NA_IS_FIXED_TIP (widget)); |
---|
235 | |
---|
236 | fixedtip = NA_FIXED_TIP (widget); |
---|
237 | |
---|
238 | gtk_label_set_markup (GTK_LABEL (fixedtip->priv->label), |
---|
239 | markup_text); |
---|
240 | |
---|
241 | na_fixed_tip_position (fixedtip); |
---|
242 | } |
---|
243 | |
---|
244 | void |
---|
245 | na_fixed_tip_set_orientation (GtkWidget *widget, |
---|
246 | GtkOrientation orientation) |
---|
247 | { |
---|
248 | NaFixedTip *fixedtip; |
---|
249 | |
---|
250 | g_return_if_fail (NA_IS_FIXED_TIP (widget)); |
---|
251 | |
---|
252 | fixedtip = NA_FIXED_TIP (widget); |
---|
253 | |
---|
254 | if (orientation == fixedtip->priv->orientation) |
---|
255 | return; |
---|
256 | |
---|
257 | fixedtip->priv->orientation = orientation; |
---|
258 | |
---|
259 | na_fixed_tip_position (fixedtip); |
---|
260 | } |
---|