| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "views/widget/tooltip_manager_gtk.h" | 5 #include "views/widget/tooltip_manager_gtk.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "gfx/font.h" | 9 #include "gfx/font.h" |
| 10 #include "views/focus/focus_manager.h" | 10 #include "views/focus/focus_manager.h" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 // static | 64 // static |
| 65 int TooltipManager::GetMaxWidth(int x, int y) { | 65 int TooltipManager::GetMaxWidth(int x, int y) { |
| 66 gfx::Rect monitor_bounds = | 66 gfx::Rect monitor_bounds = |
| 67 Screen::GetMonitorAreaNearestPoint(gfx::Point(x, y)); | 67 Screen::GetMonitorAreaNearestPoint(gfx::Point(x, y)); |
| 68 // GtkLabel (gtk_label_ensure_layout) forces wrapping at this size. We mirror | 68 // GtkLabel (gtk_label_ensure_layout) forces wrapping at this size. We mirror |
| 69 // the size here otherwise tooltips wider than the size used by gtklabel end | 69 // the size here otherwise tooltips wider than the size used by gtklabel end |
| 70 // up with extraneous empty lines. | 70 // up with extraneous empty lines. |
| 71 return monitor_bounds.width() == 0 ? 800 : (monitor_bounds.width() + 1) / 2; | 71 return monitor_bounds.width() == 0 ? 800 : (monitor_bounds.width() + 1) / 2; |
| 72 } | 72 } |
| 73 | 73 |
| 74 // Callback from gtk_container_foreach. If |*label_p| is NULL and |widget| is | |
| 75 // a GtkLabel, |*label_p| is set to |widget|. Used to find the first GtkLabel | |
| 76 // in a container. | |
| 77 static void LabelLocatorCallback(GtkWidget* widget, | |
| 78 gpointer label_p) { | |
| 79 GtkWidget** label = static_cast<GtkWidget**>(label_p); | |
| 80 if (!*label && GTK_IS_LABEL(widget)) | |
| 81 *label = widget; | |
| 82 } | |
| 83 | |
| 84 // By default GtkTooltip wraps at a longish string. We want more control over | |
| 85 // that wrapping. The only way to do that is dig out the label and set | |
| 86 // gtk_label_set_max_width_chars, which is what this code does. I also tried | |
| 87 // setting a custom widget on the tooltip, but there is a bug in Gtk that | |
| 88 // triggers continually hiding/showing the widget in that case. | |
| 89 static void AdjustLabel(GtkTooltip* tooltip) { | |
| 90 static const char kAdjustedLabelPropertyValue[] = "_adjusted_label_"; | |
| 91 static const char kTooltipLabel[] = "_tooltip_label_"; | |
| 92 gpointer adjusted_value = g_object_get_data(G_OBJECT(tooltip), | |
| 93 kAdjustedLabelPropertyValue); | |
| 94 if (adjusted_value) { | |
| 95 gpointer label_ptr = g_object_get_data(G_OBJECT(tooltip), kTooltipLabel); | |
| 96 if (label_ptr) { | |
| 97 // Setting the text in a label doesn't force recalculating wrap position. | |
| 98 // We force recalculating wrap position by resetting the max width. | |
| 99 gtk_label_set_max_width_chars(reinterpret_cast<GtkLabel*>(label_ptr), | |
| 100 2999); | |
| 101 gtk_label_set_max_width_chars(reinterpret_cast<GtkLabel*>(label_ptr), | |
| 102 3000); | |
| 103 } | |
| 104 return; | |
| 105 } | |
| 106 | |
| 107 adjusted_value = reinterpret_cast<gpointer>(1); | |
| 108 g_object_set_data(G_OBJECT(tooltip), kAdjustedLabelPropertyValue, | |
| 109 adjusted_value); | |
| 110 GtkWidget* parent; | |
| 111 { | |
| 112 // Create a label so that we can get the parent. The Tooltip ends up taking | |
| 113 // ownership of the label and deleting it. | |
| 114 GtkWidget* label = gtk_label_new(""); | |
| 115 gtk_tooltip_set_custom(tooltip, label); | |
| 116 parent = gtk_widget_get_parent(label); | |
| 117 gtk_tooltip_set_custom(tooltip, NULL); | |
| 118 } | |
| 119 if (parent) { | |
| 120 // We found the parent, find the first label, which is where the tooltip | |
| 121 // text ends up going. | |
| 122 GtkLabel* real_label = NULL; | |
| 123 gtk_container_foreach(GTK_CONTAINER(parent), LabelLocatorCallback, | |
| 124 static_cast<gpointer>(&real_label)); | |
| 125 if (real_label) { | |
| 126 gtk_label_set_max_width_chars(GTK_LABEL(real_label), 3000); | |
| 127 g_object_set_data(G_OBJECT(tooltip), kTooltipLabel, | |
| 128 reinterpret_cast<gpointer>(real_label)); | |
| 129 } | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 TooltipManagerGtk::TooltipManagerGtk(WidgetGtk* widget) | 74 TooltipManagerGtk::TooltipManagerGtk(WidgetGtk* widget) |
| 134 : widget_(widget), | 75 : widget_(widget), |
| 135 keyboard_view_(NULL) { | 76 keyboard_view_(NULL), |
| 77 tooltip_window_(widget->window_contents()) { |
| 136 } | 78 } |
| 137 | 79 |
| 138 bool TooltipManagerGtk::ShowTooltip(int x, int y, bool for_keyboard, | 80 bool TooltipManagerGtk::ShowTooltip(int x, int y, bool for_keyboard, |
| 139 GtkTooltip* tooltip) { | 81 GtkTooltip* tooltip) { |
| 140 View* view = NULL; | 82 View* view = NULL; |
| 141 gfx::Point view_loc; | 83 gfx::Point view_loc; |
| 142 if (keyboard_view_) { | 84 if (keyboard_view_) { |
| 143 view = keyboard_view_; | 85 view = keyboard_view_; |
| 144 view_loc.SetPoint(view->width() / 2, view->height() / 2); | 86 view_loc.SetPoint(view->width() / 2, view->height() / 2); |
| 145 } else if (!for_keyboard) { | 87 } else if (!for_keyboard) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 156 } | 98 } |
| 157 } | 99 } |
| 158 | 100 |
| 159 if (!view) | 101 if (!view) |
| 160 return false; | 102 return false; |
| 161 | 103 |
| 162 std::wstring text; | 104 std::wstring text; |
| 163 if (!view->GetTooltipText(view_loc, &text)) | 105 if (!view->GetTooltipText(view_loc, &text)) |
| 164 return false; | 106 return false; |
| 165 | 107 |
| 166 AdjustLabel(tooltip); | |
| 167 | |
| 168 // Sets the area of the tooltip. This way if different views in the same | 108 // Sets the area of the tooltip. This way if different views in the same |
| 169 // widget have tooltips the tooltip doesn't get stuck at the same location. | 109 // widget have tooltips the tooltip doesn't get stuck at the same location. |
| 170 gfx::Rect vis_bounds = view->GetVisibleBounds(); | 110 gfx::Rect vis_bounds = view->GetVisibleBounds(); |
| 171 gfx::Point widget_loc(vis_bounds.origin()); | 111 gfx::Point widget_loc(vis_bounds.origin()); |
| 172 View::ConvertPointToWidget(view, &widget_loc); | 112 View::ConvertPointToWidget(view, &widget_loc); |
| 173 GdkRectangle tip_area = { widget_loc.x(), widget_loc.y(), | 113 GdkRectangle tip_area = { widget_loc.x(), widget_loc.y(), |
| 174 vis_bounds.width(), vis_bounds.height() }; | 114 vis_bounds.width(), vis_bounds.height() }; |
| 175 gtk_tooltip_set_tip_area(tooltip, &tip_area); | 115 gtk_tooltip_set_tip_area(tooltip, &tip_area); |
| 176 | 116 |
| 177 int max_width, line_count; | 117 int max_width, line_count; |
| 178 gfx::Point screen_loc(x, y); | 118 gfx::Point screen_loc(x, y); |
| 179 View::ConvertPointToScreen(widget_->GetRootView(), &screen_loc); | 119 View::ConvertPointToScreen(widget_->GetRootView(), &screen_loc); |
| 180 TrimTooltipToFit(&text, &max_width, &line_count, screen_loc.x(), | 120 TrimTooltipToFit(&text, &max_width, &line_count, screen_loc.x(), |
| 181 screen_loc.y()); | 121 screen_loc.y()); |
| 182 gtk_tooltip_set_text(tooltip, WideToUTF8(text).c_str()); | 122 tooltip_window_.SetTooltipText(text); |
| 183 | |
| 184 | 123 |
| 185 return true; | 124 return true; |
| 186 } | 125 } |
| 187 | 126 |
| 188 void TooltipManagerGtk::UpdateTooltip() { | 127 void TooltipManagerGtk::UpdateTooltip() { |
| 189 // UpdateTooltip may be invoked after the widget has been destroyed. | 128 // UpdateTooltip may be invoked after the widget has been destroyed. |
| 190 GtkWidget* widget = widget_->GetNativeView(); | 129 GtkWidget* widget = widget_->GetNativeView(); |
| 191 if (!widget) | 130 if (!widget) |
| 192 return; | 131 return; |
| 193 | 132 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 if (!g_signal_parse_name("show_help", itype, &signal_id, &detail, FALSE)) { | 173 if (!g_signal_parse_name("show_help", itype, &signal_id, &detail, FALSE)) { |
| 235 NOTREACHED(); | 174 NOTREACHED(); |
| 236 return false; | 175 return false; |
| 237 } | 176 } |
| 238 gboolean result; | 177 gboolean result; |
| 239 g_signal_emit(widget, signal_id, 0, GTK_WIDGET_HELP_TOOLTIP, &result); | 178 g_signal_emit(widget, signal_id, 0, GTK_WIDGET_HELP_TOOLTIP, &result); |
| 240 return true; | 179 return true; |
| 241 } | 180 } |
| 242 | 181 |
| 243 } // namespace views | 182 } // namespace views |
| OLD | NEW |