| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 "ui/views/widget/tooltip_manager_gtk.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/utf_string_conversions.h" | |
| 9 #include "ui/gfx/font.h" | |
| 10 #include "ui/gfx/screen.h" | |
| 11 #include "ui/views/focus/focus_manager.h" | |
| 12 #include "ui/views/widget/native_widget_gtk.h" | |
| 13 #include "views/view.h" | |
| 14 | |
| 15 // WARNING: this implementation is good for a start, but it doesn't give us | |
| 16 // control of tooltip positioning both on mouse events and when showing from | |
| 17 // keyboard. We may need to write our own to give us the control we need. | |
| 18 | |
| 19 namespace views { | |
| 20 | |
| 21 static gfx::Font* LoadDefaultFont() { | |
| 22 // Create a tooltip widget and extract the font from it (we have to realize | |
| 23 // it to make sure the correct font gets set). | |
| 24 GtkWidget* window = gtk_window_new(GTK_WINDOW_POPUP); | |
| 25 gtk_widget_set_name(window, "gtk-tooltip"); | |
| 26 GtkWidget* label = gtk_label_new(""); | |
| 27 gtk_widget_show(label); | |
| 28 | |
| 29 gtk_container_add(GTK_CONTAINER(window), label); | |
| 30 gtk_widget_realize(window); | |
| 31 | |
| 32 GtkStyle* style = gtk_widget_get_style(label); | |
| 33 gfx::Font* font = new gfx::Font(style->font_desc); | |
| 34 | |
| 35 gtk_widget_destroy(window); | |
| 36 | |
| 37 return font; | |
| 38 } | |
| 39 | |
| 40 // static | |
| 41 int TooltipManager::GetTooltipHeight() { | |
| 42 // This is only used to position the tooltip, and we don't yet support | |
| 43 // positioning the tooltip, it isn't worth trying to implement this. | |
| 44 return 0; | |
| 45 } | |
| 46 | |
| 47 // static | |
| 48 gfx::Font TooltipManager::GetDefaultFont() { | |
| 49 static gfx::Font* font = NULL; | |
| 50 if (!font) | |
| 51 font = LoadDefaultFont(); | |
| 52 | |
| 53 return *font; | |
| 54 } | |
| 55 | |
| 56 // static | |
| 57 int TooltipManager::GetMaxWidth(int x, int y) { | |
| 58 gfx::Rect monitor_bounds = | |
| 59 gfx::Screen::GetMonitorAreaNearestPoint(gfx::Point(x, y)); | |
| 60 // GtkLabel (gtk_label_ensure_layout) forces wrapping at this size. We mirror | |
| 61 // the size here otherwise tooltips wider than the size used by gtklabel end | |
| 62 // up with extraneous empty lines. | |
| 63 return monitor_bounds.width() == 0 ? 800 : (monitor_bounds.width() + 1) / 2; | |
| 64 } | |
| 65 | |
| 66 TooltipManagerGtk::TooltipManagerGtk(NativeWidgetGtk* widget) | |
| 67 : widget_(widget), | |
| 68 keyboard_view_(NULL), | |
| 69 tooltip_window_(widget->window_contents()) { | |
| 70 } | |
| 71 | |
| 72 bool TooltipManagerGtk::ShowTooltip(int x, int y, bool for_keyboard, | |
| 73 GtkTooltip* tooltip) { | |
| 74 const View* view = NULL; | |
| 75 gfx::Point view_loc; | |
| 76 if (keyboard_view_) { | |
| 77 view = keyboard_view_; | |
| 78 view_loc.SetPoint(view->width() / 2, view->height() / 2); | |
| 79 } else if (!for_keyboard) { | |
| 80 View* root_view = widget_->GetWidget()->GetRootView(); | |
| 81 view = root_view->GetEventHandlerForPoint(gfx::Point(x, y)); | |
| 82 view_loc.SetPoint(x, y); | |
| 83 View::ConvertPointFromWidget(view, &view_loc); | |
| 84 } else { | |
| 85 const FocusManager* focus_manager = widget_->GetWidget()->GetFocusManager(); | |
| 86 if (focus_manager) { | |
| 87 view = focus_manager->GetFocusedView(); | |
| 88 if (view) | |
| 89 view_loc.SetPoint(view->width() / 2, view->height() / 2); | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 if (!view) | |
| 94 return false; | |
| 95 | |
| 96 string16 text; | |
| 97 if (!view->GetTooltipText(view_loc, &text)) | |
| 98 return false; | |
| 99 | |
| 100 // Sets the area of the tooltip. This way if different views in the same | |
| 101 // widget have tooltips the tooltip doesn't get stuck at the same location. | |
| 102 gfx::Rect vis_bounds = view->GetVisibleBounds(); | |
| 103 gfx::Point widget_loc(vis_bounds.origin()); | |
| 104 View::ConvertPointToWidget(view, &widget_loc); | |
| 105 GdkRectangle tip_area = { widget_loc.x(), widget_loc.y(), | |
| 106 vis_bounds.width(), vis_bounds.height() }; | |
| 107 gtk_tooltip_set_tip_area(tooltip, &tip_area); | |
| 108 | |
| 109 int max_width, line_count; | |
| 110 gfx::Point screen_loc(x, y); | |
| 111 View::ConvertPointToScreen(widget_->GetWidget()->GetRootView(), &screen_loc); | |
| 112 TrimTooltipToFit(&text, &max_width, &line_count, screen_loc.x(), | |
| 113 screen_loc.y()); | |
| 114 tooltip_window_.SetTooltipText(text); | |
| 115 | |
| 116 return true; | |
| 117 } | |
| 118 | |
| 119 void TooltipManagerGtk::UpdateTooltip() { | |
| 120 // UpdateTooltip may be invoked after the widget has been destroyed. | |
| 121 GtkWidget* widget = widget_->GetNativeView(); | |
| 122 if (!widget) | |
| 123 return; | |
| 124 | |
| 125 GdkDisplay* display = gtk_widget_get_display(widget); | |
| 126 if (display) | |
| 127 gtk_tooltip_trigger_tooltip_query(display); | |
| 128 } | |
| 129 | |
| 130 void TooltipManagerGtk::TooltipTextChanged(View* view) { | |
| 131 UpdateTooltip(); | |
| 132 } | |
| 133 | |
| 134 void TooltipManagerGtk::ShowKeyboardTooltip(View* view) { | |
| 135 if (view == keyboard_view_) | |
| 136 return; // We're already showing the tip for the specified view. | |
| 137 | |
| 138 // We have to hide the current tooltip, then show again. | |
| 139 HideKeyboardTooltip(); | |
| 140 | |
| 141 string16 tooltip_text; | |
| 142 if (!view->GetTooltipText(gfx::Point(), &tooltip_text)) | |
| 143 return; // The view doesn't have a tooltip, nothing to do. | |
| 144 | |
| 145 keyboard_view_ = view; | |
| 146 if (!SendShowHelpSignal()) { | |
| 147 keyboard_view_ = NULL; | |
| 148 return; | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 void TooltipManagerGtk::HideKeyboardTooltip() { | |
| 153 if (!keyboard_view_) | |
| 154 return; | |
| 155 | |
| 156 SendShowHelpSignal(); | |
| 157 keyboard_view_ = NULL; | |
| 158 } | |
| 159 | |
| 160 bool TooltipManagerGtk::SendShowHelpSignal() { | |
| 161 GtkWidget* widget = widget_->window_contents(); | |
| 162 GType itype = G_TYPE_FROM_INSTANCE(G_OBJECT(widget)); | |
| 163 guint signal_id; | |
| 164 GQuark detail; | |
| 165 if (!g_signal_parse_name("show_help", itype, &signal_id, &detail, FALSE)) { | |
| 166 NOTREACHED(); | |
| 167 return false; | |
| 168 } | |
| 169 gboolean result; | |
| 170 g_signal_emit(widget, signal_id, 0, GTK_WIDGET_HELP_TOOLTIP, &result); | |
| 171 return true; | |
| 172 } | |
| 173 | |
| 174 } // namespace views | |
| OLD | NEW |