summaryrefslogtreecommitdiff
path: root/0001-New-tooltip-style.patch
blob: 93192a01f929de8830635abf799734e331ba8399 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
From d5c9b3a4149fdeb40ac8377ba0867b447eb0f5aa Mon Sep 17 00:00:00 2001
From: Colin Walters <walters@verbum.org>
Date: Thu, 8 Jul 2010 17:58:40 -0400
Subject: [PATCH] New tooltip style

---
 gtk/gtktooltip.c |  261 +++++++++++++++++++++++++++++++++++++++++++++++++++---
 gtk/gtkwidget.c  |    8 ++
 2 files changed, 258 insertions(+), 11 deletions(-)

diff --git a/gtk/gtktooltip.c b/gtk/gtktooltip.c
index 67c2f6c..75fd6a7 100644
--- a/gtk/gtktooltip.c
+++ b/gtk/gtktooltip.c
@@ -33,6 +33,10 @@
 #include "gtkhbox.h"
 #include "gtkalignment.h"
 
+#ifdef GDK_WINDOWING_X11
+#include "gdk/x11/gdkx.h"
+#endif
+
 #include "gtkalias.h"
 
 #undef DEBUG_TOOLTIP
@@ -97,6 +101,7 @@ static void       gtk_tooltip_display_closed       (GdkDisplay      *display,
 						    GtkTooltip      *tooltip);
 static void       gtk_tooltip_set_last_window      (GtkTooltip      *tooltip,
 						    GdkWindow       *window);
+static void       update_shape                     (GtkTooltip      *tooltip);
 
 
 G_DEFINE_TYPE (GtkTooltip, gtk_tooltip, G_TYPE_OBJECT);
@@ -112,8 +117,43 @@ gtk_tooltip_class_init (GtkTooltipClass *klass)
 }
 
 static void
+on_composited_changed (GtkWidget  *window,
+                       GtkTooltip *tooltip)
+{
+  update_shape (tooltip);
+}
+
+static void
+on_screen_changed (GtkWidget  *window,
+                   GdkScreen  *previous,
+                   GtkTooltip *tooltip)
+{
+  GdkScreen *screen;
+  GdkColormap *cmap;
+
+  screen = gtk_widget_get_screen (window);
+
+  cmap = NULL;
+  if (gdk_screen_is_composited (screen))
+    cmap = gdk_screen_get_rgba_colormap (screen);
+  if (cmap == NULL)
+    cmap = gdk_screen_get_rgb_colormap (screen);
+
+  gtk_widget_set_colormap (window, cmap);
+}
+
+static void
+on_realized (GtkWidget  *window,
+             GtkTooltip *tooltip)
+{
+  update_shape (tooltip);
+}
+
+static void
 gtk_tooltip_init (GtkTooltip *tooltip)
 {
+  GdkScreen *screen;
+
   tooltip->timeout_id = 0;
   tooltip->browse_mode_timeout_id = 0;
 
@@ -129,8 +169,12 @@ gtk_tooltip_init (GtkTooltip *tooltip)
   tooltip->last_window = NULL;
 
   tooltip->window = g_object_ref (gtk_window_new (GTK_WINDOW_POPUP));
+
+  on_screen_changed (tooltip->window, NULL, tooltip);
+
   gtk_window_set_type_hint (GTK_WINDOW (tooltip->window),
 			    GDK_WINDOW_TYPE_HINT_TOOLTIP);
+
   gtk_widget_set_app_paintable (tooltip->window, TRUE);
   gtk_window_set_resizable (GTK_WINDOW (tooltip->window), FALSE);
   gtk_widget_set_name (tooltip->window, "gtk-tooltip");
@@ -147,7 +191,7 @@ gtk_tooltip_init (GtkTooltip *tooltip)
   gtk_widget_show (tooltip->alignment);
 
   g_signal_connect_swapped (tooltip->window, "style-set",
-			    G_CALLBACK (gtk_tooltip_window_style_set), tooltip);
+		            G_CALLBACK (gtk_tooltip_window_style_set), tooltip);
   g_signal_connect_swapped (tooltip->window, "expose-event",
 			    G_CALLBACK (gtk_tooltip_paint_window), tooltip);
 
@@ -164,6 +208,13 @@ gtk_tooltip_init (GtkTooltip *tooltip)
   gtk_box_pack_start (GTK_BOX (tooltip->box), tooltip->label,
 		      FALSE, FALSE, 0);
 
+  g_signal_connect (tooltip->window, "composited-changed",
+                    G_CALLBACK (on_composited_changed), tooltip);
+  g_signal_connect (tooltip->window, "screen-changed",
+                    G_CALLBACK (on_screen_changed), tooltip);
+  g_signal_connect (tooltip->window, "realize",
+                    G_CALLBACK (on_realized), tooltip);
+
   tooltip->custom_widget = NULL;
 }
 
@@ -510,19 +561,207 @@ gtk_tooltip_window_style_set (GtkTooltip *tooltip)
   gtk_widget_queue_draw (tooltip->window);
 }
 
+static void
+draw_round_rect (cairo_t *cr,
+                 gdouble  aspect,
+                 gdouble  x,
+                 gdouble  y,
+                 gdouble  corner_radius,
+                 gdouble  width,
+                 gdouble  height)
+{
+  gdouble radius = corner_radius / aspect;
+
+  cairo_move_to (cr, x + radius, y);
+
+  /* top-right, left of the corner */
+  cairo_line_to (cr, x + width - radius, y);
+
+  /* top-right, below the corner */
+  cairo_arc (cr,
+             x + width - radius, y + radius, radius,
+             -90.0f * G_PI / 180.0f, 0.0f * G_PI / 180.0f);
+
+  /* bottom-right, above the corner */
+  cairo_line_to (cr, x + width, y + height - radius);
+
+  /* bottom-right, left of the corner */
+  cairo_arc (cr,
+             x + width - radius, y + height - radius, radius,
+             0.0f * G_PI / 180.0f, 90.0f * G_PI / 180.0f);
+
+  /* bottom-left, right of the corner */
+  cairo_line_to (cr, x + radius, y + height);
+
+  /* bottom-left, above the corner */
+  cairo_arc (cr,
+             x + radius, y + height - radius, radius,
+             90.0f * G_PI / 180.0f, 180.0f * G_PI / 180.0f);
+
+  /* top-left, below the corner */
+  cairo_line_to (cr, x, y + radius);
+
+  /* top-left, right of the corner */
+  cairo_arc (cr,
+             x + radius, y + radius, radius,
+             180.0f * G_PI / 180.0f, 270.0f * G_PI / 180.0f);
+
+  cairo_close_path (cr);
+}
+
+static void
+fill_background (GtkWidget  *widget,
+                 cairo_t    *cr)
+{
+  GdkColor color;
+  gdouble  r, g, b;
+  gint     radius;
+  gdouble  background_alpha;
+
+  if (gdk_screen_is_composited (gtk_widget_get_screen (widget)))
+    background_alpha = 0.90;
+  else
+    background_alpha = 1.0;
+
+  radius = MIN (widget->style->xthickness, widget->style->ythickness);
+  radius = MAX (radius, 1);
+
+  cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
+  cairo_paint (cr);
+  cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
+
+  draw_round_rect (cr,
+                   1.0, 0.5, 0.5, radius,
+                   widget->allocation.width - 1,
+                   widget->allocation.height - 1);
+
+  color = widget->style->bg [GTK_STATE_NORMAL];
+  r = (float)color.red / 65535.0;
+  g = (float)color.green / 65535.0;
+  b = (float)color.blue / 65535.0;
+  cairo_set_source_rgba (cr, r, g, b, background_alpha);
+  cairo_fill_preserve (cr);
+
+  color = widget->style->bg [GTK_STATE_SELECTED];
+  r = (float) color.red / 65535.0;
+  g = (float) color.green / 65535.0;
+  b = (float) color.blue / 65535.0;
+
+  cairo_set_source_rgba (cr, r, g, b, background_alpha);
+  cairo_set_line_width (cr, 1.0);
+  cairo_stroke (cr);
+}
+
+static void
+update_shape (GtkTooltip *tooltip)
+{
+  GdkScreen *screen;
+  GdkBitmap *mask;
+  cairo_t *cr;
+  gint width, height;
+  gboolean new_style;
+  gint radius;
+
+  gtk_widget_style_get (tooltip->window, "new-tooltip-style", &new_style, NULL);
+
+  if (!new_style)
+    {
+      gtk_widget_shape_combine_mask (tooltip->window, NULL, 0, 0);
+     return;
+    }
+
+  screen = gtk_widget_get_screen (tooltip->window);
+
+  gtk_window_get_size (GTK_WINDOW (tooltip->window), &width, &height);
+
+  if (gdk_screen_is_composited (screen))
+    {
+      GdkRectangle rect;
+      const char *wm;
+
+      gtk_widget_shape_combine_mask (tooltip->window, NULL, 0, 0);
+#ifdef GDK_WINDOWING_X11
+      /* This is a hack to keep the Metacity compositor from slapping a
+       * non-shaped shadow around the shaped tooltip
+       */
+      if (!gtk_widget_get_mapped (tooltip->window))
+        {
+          wm = gdk_x11_screen_get_window_manager_name (screen);
+          if (g_strcmp0 (wm, "Metacity") == 0)
+            gtk_window_set_type_hint (GTK_WINDOW (tooltip->window),
+                                     GDK_WINDOW_TYPE_HINT_DND);
+        }
+#endif
+      return;
+    }
+
+  radius = MIN (tooltip->window->style->xthickness,
+                tooltip->window->style->ythickness);
+  radius = MAX (radius, 1);
+  mask = (GdkBitmap *) gdk_pixmap_new (NULL, width, height, 1);
+  cr = gdk_cairo_create (mask);
+  if (cairo_status (cr) == CAIRO_STATUS_SUCCESS)
+    {
+      cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
+      cairo_paint (cr);
+
+      cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
+      cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
+      draw_round_rect (cr, 1.0, 0, 0, radius + 1, width, height);
+      cairo_fill (cr);
+
+      gtk_widget_shape_combine_mask (tooltip->window, mask, 0, 0);
+    }
+  cairo_destroy (cr);
+
+  g_object_unref (mask);
+}
+
 static gboolean
 gtk_tooltip_paint_window (GtkTooltip *tooltip)
 {
-  gtk_paint_flat_box (tooltip->window->style,
-		      tooltip->window->window,
-		      GTK_STATE_NORMAL,
-		      GTK_SHADOW_OUT,
-		      NULL,
-		      tooltip->window,
-		      "tooltip",
-		      0, 0,
-		      tooltip->window->allocation.width,
-		      tooltip->window->allocation.height);
+  gboolean new_style;
+
+  gtk_widget_style_get (tooltip->window, "new-tooltip-style", &new_style, NULL);
+
+  if (new_style)
+    {
+      cairo_t         *context;
+      cairo_surface_t *surface;
+      cairo_t         *cr;
+
+      context = gdk_cairo_create (tooltip->window->window);
+
+      cairo_set_operator (context, CAIRO_OPERATOR_SOURCE);
+      surface = cairo_surface_create_similar (cairo_get_target (context),
+                                              CAIRO_CONTENT_COLOR_ALPHA,
+                                              tooltip->window->allocation.width,
+                                              tooltip->window->allocation.height);
+      cr = cairo_create (surface);
+
+      fill_background (tooltip->window, cr);
+
+      cairo_destroy (cr);
+      cairo_set_source_surface (context, surface, 0, 0);
+      cairo_paint (context);
+      cairo_surface_destroy (surface);
+      cairo_destroy (context);
+
+      update_shape (tooltip);
+    }
+  else
+    {
+      gtk_paint_flat_box (tooltip->window->style,
+                          tooltip->window->window,
+                          GTK_STATE_NORMAL,
+                          GTK_SHADOW_OUT,
+                          NULL,
+                          tooltip->window,
+                          "tooltip",
+                          0, 0,
+                          tooltip->window->allocation.width,
+                          tooltip->window->allocation.height);
+    }
 
   return FALSE;
 }
diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c
index d302589..0f71849 100644
--- a/gtk/gtkwidget.c
+++ b/gtk/gtkwidget.c
@@ -2608,6 +2608,14 @@ gtk_widget_class_init (GtkWidgetClass *klass)
                                                              P_("The length of vertical scroll arrows"),
                                                              1, G_MAXINT, 16,
                                                              GTK_PARAM_READABLE));
+
+  gtk_widget_class_install_style_property (klass,
+                                           g_param_spec_boolean ("new-tooltip-style",
+                                                                 NULL,
+                                                                 NULL,
+                                                                 FALSE,
+                                                                 GTK_PARAM_READABLE));
+
 }
 
 static void
-- 
1.7.1.1

bgstack15