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