OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "views/widget/tooltip_window_gtk.h" | |
6 | |
7 #include <gtk/gtk.h> | |
8 | |
9 #include "base/utf_string_conversions.h" | |
10 | |
11 namespace views { | |
12 | |
13 TooltipWindowGtk::TooltipWindowGtk(GtkWidget* widget) | |
14 : host_(widget), | |
15 window_(NULL), | |
16 alignment_(NULL), | |
17 label_(NULL) { | |
18 Init(); | |
19 } | |
20 | |
21 TooltipWindowGtk::~TooltipWindowGtk() { | |
22 if (window_) | |
23 gtk_widget_destroy(window_); | |
24 } | |
25 | |
26 void TooltipWindowGtk::SetTooltipText(const std::wstring& text) { | |
27 const std::string& utf8 = WideToUTF8(text); | |
28 | |
29 gtk_label_set_text(label(), utf8.c_str()); | |
30 } | |
31 | |
32 GtkLabel* TooltipWindowGtk::label() { | |
33 return GTK_LABEL(label_); | |
34 } | |
35 | |
36 void TooltipWindowGtk::Init() { | |
37 // Creates and setup tooltip window. | |
38 window_ = gtk_window_new(GTK_WINDOW_POPUP); | |
39 gtk_window_set_type_hint(GTK_WINDOW(window_), GDK_WINDOW_TYPE_HINT_TOOLTIP); | |
40 gtk_widget_set_app_paintable(window_, TRUE); | |
41 gtk_window_set_resizable(GTK_WINDOW(window_), FALSE); | |
42 gtk_widget_set_name(window_, "gtk-tooltip"); | |
43 | |
44 GdkColormap* rgba_colormap = | |
45 gdk_screen_get_rgba_colormap(gdk_screen_get_default()); | |
46 if (rgba_colormap) | |
47 gtk_widget_set_colormap(window_, rgba_colormap); | |
48 | |
49 g_signal_connect(G_OBJECT(window_), "expose-event", | |
50 G_CALLBACK(&OnPaintThunk), this); | |
51 g_signal_connect(G_OBJECT(window_), "style-set", | |
52 G_CALLBACK(&OnStyleSetThunk), this); | |
53 | |
54 alignment_ = gtk_alignment_new(0.5, 0.5, 1.0, 1.0); | |
55 gtk_container_add(GTK_CONTAINER(window_), alignment_); | |
56 gtk_widget_show(alignment_); | |
57 | |
58 label_ = gtk_label_new(""); | |
59 gtk_label_set_line_wrap(GTK_LABEL(label_), TRUE); | |
60 gtk_container_add(GTK_CONTAINER(alignment_), label_); | |
61 gtk_widget_show(label_); | |
62 | |
63 // Associates the tooltip window with given widget | |
64 gtk_widget_set_tooltip_window(host_, GTK_WINDOW(window_)); | |
65 } | |
66 | |
67 // Paints our customized tooltip window. | |
68 gboolean TooltipWindowGtk::OnPaint(GtkWidget* widget, GdkEventExpose* event) { | |
69 gtk_paint_flat_box(widget->style, | |
70 widget->window, | |
71 GTK_STATE_NORMAL, | |
72 GTK_SHADOW_OUT, | |
73 NULL, | |
74 widget, | |
75 "tooltip", | |
76 0, 0, | |
77 widget->allocation.width, | |
78 widget->allocation.height); | |
79 | |
80 return FALSE; | |
81 } | |
82 | |
83 // Style change handler. | |
84 void TooltipWindowGtk::OnStyleSet(GtkWidget* widget, | |
85 GtkStyle* previous_style) { | |
86 gtk_alignment_set_padding(GTK_ALIGNMENT(alignment_), | |
87 widget->style->ythickness, | |
88 widget->style->ythickness, | |
89 widget->style->xthickness, | |
90 widget->style->xthickness); | |
91 | |
92 gtk_widget_queue_draw(widget); | |
93 } | |
94 | |
95 } // namespace views | |
OLD | NEW |