1 // Copyright (c) 2012 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 "chrome/browser/ui/gtk/find_bar_gtk.h" 6 7 #include <gdk/gdkkeysyms.h> 8 9 #include <algorithm> 10 #include <string> 11 #include <vector> 12 13 #include "base/debug/trace_event.h" 14 #include "base/i18n/rtl.h" 15 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_util.h" 17 #include "base/strings/utf_string_conversions.h" 18 #include "chrome/browser/chrome_notification_types.h" 19 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/themes/theme_properties.h" 21 #include "chrome/browser/ui/browser.h" 22 #include "chrome/browser/ui/find_bar/find_bar_controller.h" 23 #include "chrome/browser/ui/find_bar/find_bar_state.h" 24 #include "chrome/browser/ui/find_bar/find_bar_state_factory.h" 25 #include "chrome/browser/ui/find_bar/find_notification_details.h" 26 #include "chrome/browser/ui/find_bar/find_tab_helper.h" 27 #include "chrome/browser/ui/gtk/browser_window_gtk.h" 28 #include "chrome/browser/ui/gtk/custom_button.h" 29 #include "chrome/browser/ui/gtk/gtk_theme_service.h" 30 #include "chrome/browser/ui/gtk/gtk_util.h" 31 #include "chrome/browser/ui/gtk/nine_box.h" 32 #include "chrome/browser/ui/gtk/slide_animator_gtk.h" 33 #include "chrome/browser/ui/gtk/tab_contents_container_gtk.h" 34 #include "chrome/browser/ui/gtk/tabs/tab_strip_gtk.h" 35 #include "chrome/browser/ui/gtk/view_id_util.h" 36 #include "content/public/browser/native_web_keyboard_event.h" 37 #include "content/public/browser/notification_source.h" 38 #include "content/public/browser/render_view_host.h" 39 #include "content/public/browser/web_contents.h" 40 #include "content/public/browser/web_contents_view.h" 41 #include "grit/generated_resources.h" 42 #include "grit/theme_resources.h" 43 #include "grit/ui_resources.h" 44 #include "ui/base/gtk/gtk_floating_container.h" 45 #include "ui/base/gtk/gtk_hig_constants.h" 46 #include "ui/base/l10n/l10n_util.h" 47 #include "ui/base/resource/resource_bundle.h" 48 #include "ui/gfx/image/cairo_cached_surface.h" 49 #include "ui/gfx/image/image.h" 50 51 using content::NativeWebKeyboardEvent; 52 53 namespace { 54 55 // Used as the color of the text in the entry box and the text for the results 56 // label for failure searches. 57 const GdkColor& kEntryTextColor = ui::kGdkBlack; 58 59 // Used as the color of the background of the entry box and the background of 60 // the find label for successful searches. 61 const GdkColor& kEntryBackgroundColor = ui::kGdkWhite; 62 const GdkColor kFindFailureBackgroundColor = GDK_COLOR_RGB(255, 102, 102); 63 const GdkColor kFindSuccessTextColor = GDK_COLOR_RGB(178, 178, 178); 64 65 // Padding around the container. 66 const int kBarPaddingTop = 2; 67 const int kBarPaddingBottom = 3; 68 const int kEntryPaddingLeft = 6; 69 const int kCloseButtonPadding = 3; 70 const int kBarPaddingRight = 4; 71 72 // The height of the findbar dialog, as dictated by the size of the background 73 // images. 74 const int kFindBarHeight = 32; 75 76 // The default width of the findbar dialog. It may get smaller if the window 77 // is narrow. 78 const int kFindBarWidth = 303; 79 80 // The size of the "rounded" corners. 81 const int kCornerSize = 3; 82 83 enum FrameType { 84 FRAME_MASK, 85 FRAME_STROKE, 86 }; 87 88 // Returns a list of points that either form the outline of the status bubble 89 // (|type| == FRAME_MASK) or form the inner border around the inner edge 90 // (|type| == FRAME_STROKE). 91 std::vector<GdkPoint> MakeFramePolygonPoints(int width, 92 int height, 93 FrameType type) { 94 using gtk_util::MakeBidiGdkPoint; 95 std::vector<GdkPoint> points; 96 97 bool ltr = !base::i18n::IsRTL(); 98 // If we have a stroke, we have to offset some of our points by 1 pixel. 99 // We have to inset by 1 pixel when we draw horizontal lines that are on the 100 // bottom or when we draw vertical lines that are closer to the end (end is 101 // right for ltr). 102 int y_off = (type == FRAME_MASK) ? 0 : -1; 103 // We use this one for LTR. 104 int x_off_l = ltr ? y_off : 0; 105 // We use this one for RTL. 106 int x_off_r = !ltr ? -y_off : 0; 107 108 // Top left corner 109 points.push_back(MakeBidiGdkPoint(x_off_r, 0, width, ltr)); 110 points.push_back(MakeBidiGdkPoint( 111 kCornerSize + x_off_r, kCornerSize, width, ltr)); 112 113 // Bottom left corner 114 points.push_back(MakeBidiGdkPoint( 115 kCornerSize + x_off_r, height - kCornerSize, width, ltr)); 116 points.push_back(MakeBidiGdkPoint( 117 (2 * kCornerSize) + x_off_l, height + y_off, 118 width, ltr)); 119 120 // Bottom right corner 121 points.push_back(MakeBidiGdkPoint( 122 width - (2 * kCornerSize) + x_off_r, height + y_off, 123 width, ltr)); 124 points.push_back(MakeBidiGdkPoint( 125 width - kCornerSize + x_off_l, height - kCornerSize, width, ltr)); 126 127 // Top right corner 128 points.push_back(MakeBidiGdkPoint( 129 width - kCornerSize + x_off_l, kCornerSize, width, ltr)); 130 points.push_back(MakeBidiGdkPoint(width + x_off_l, 0, width, ltr)); 131 132 return points; 133 } 134 135 // Give the findbar dialog its unique shape using images. 136 void SetDialogShape(GtkWidget* widget) { 137 static NineBox* dialog_shape = NULL; 138 if (!dialog_shape) { 139 dialog_shape = new NineBox( 140 IDR_FIND_DLG_LEFT_BACKGROUND, 141 IDR_FIND_DLG_MIDDLE_BACKGROUND, 142 IDR_FIND_DLG_RIGHT_BACKGROUND, 143 0, 0, 0, 0, 0, 0); 144 } 145 146 dialog_shape->ContourWidget(widget); 147 } 148 149 // Return a ninebox that will paint the border of the findbar dialog. This is 150 // shared across all instances of the findbar. Do not free the returned pointer. 151 const NineBox* GetDialogBorder() { 152 static NineBox* dialog_border = NULL; 153 if (!dialog_border) { 154 dialog_border = new NineBox( 155 IDR_FIND_DIALOG_LEFT, 156 IDR_FIND_DIALOG_MIDDLE, 157 IDR_FIND_DIALOG_RIGHT, 158 0, 0, 0, 0, 0, 0); 159 } 160 161 return dialog_border; 162 } 163 164 // Like gtk_util::CreateGtkBorderBin, but allows control over the alignment and 165 // returns both the event box and the alignment so we can modify it during its 166 // lifetime (i.e. during a theme change). 167 void BuildBorder(GtkWidget* child, 168 int padding_top, int padding_bottom, int padding_left, 169 int padding_right, 170 GtkWidget** ebox, GtkWidget** alignment) { 171 *ebox = gtk_event_box_new(); 172 *alignment = gtk_alignment_new(0.0, 0.0, 1.0, 1.0); 173 gtk_alignment_set_padding(GTK_ALIGNMENT(*alignment), 174 padding_top, padding_bottom, padding_left, 175 padding_right); 176 gtk_container_add(GTK_CONTAINER(*alignment), child); 177 gtk_container_add(GTK_CONTAINER(*ebox), *alignment); 178 } 179 180 } // namespace 181 182 FindBarGtk::FindBarGtk(BrowserWindowGtk* window) 183 : browser_(window->browser()), 184 window_(window), 185 theme_service_(GtkThemeService::GetFrom(browser_->profile())), 186 container_width_(-1), 187 container_height_(-1), 188 match_label_failure_(false), 189 ignore_changed_signal_(false) { 190 InitWidgets(); 191 ViewIDUtil::SetID(text_entry_, VIEW_ID_FIND_IN_PAGE_TEXT_FIELD); 192 193 // Insert the widget into the browser gtk hierarchy. 194 window_->AddFindBar(this); 195 196 // Hook up signals after the widget has been added to the hierarchy so the 197 // widget will be realized. 198 g_signal_connect(text_entry_, "changed", 199 G_CALLBACK(OnChanged), this); 200 g_signal_connect_after(text_entry_, "key-press-event", 201 G_CALLBACK(OnKeyPressEvent), this); 202 g_signal_connect_after(text_entry_, "key-release-event", 203 G_CALLBACK(OnKeyReleaseEvent), this); 204 // When the user tabs to us or clicks on us, save where the focus used to 205 // be. 206 g_signal_connect(text_entry_, "focus", 207 G_CALLBACK(OnFocus), this); 208 gtk_widget_add_events(text_entry_, GDK_BUTTON_PRESS_MASK); 209 g_signal_connect(text_entry_, "button-press-event", 210 G_CALLBACK(OnButtonPress), this); 211 g_signal_connect(text_entry_, "move-cursor", G_CALLBACK(OnMoveCursor), this); 212 g_signal_connect(text_entry_, "activate", G_CALLBACK(OnActivateThunk), this); 213 g_signal_connect(text_entry_, "direction-changed", 214 G_CALLBACK(OnWidgetDirectionChanged), this); 215 g_signal_connect(text_entry_, "focus-in-event", 216 G_CALLBACK(OnFocusInThunk), this); 217 g_signal_connect(text_entry_, "focus-out-event", 218 G_CALLBACK(OnFocusOutThunk), this); 219 g_signal_connect(container_, "expose-event", 220 G_CALLBACK(OnExpose), this); 221 } 222 223 FindBarGtk::~FindBarGtk() { 224 } 225 226 void FindBarGtk::InitWidgets() { 227 // The find bar is basically an hbox with a gtkentry (text box) followed by 3 228 // buttons (previous result, next result, close). We wrap the hbox in a gtk 229 // alignment and a gtk event box to get the padding and light blue 230 // background. We put that event box in a fixed in order to control its 231 // lateral position. We put that fixed in a SlideAnimatorGtk in order to get 232 // the slide effect. 233 GtkWidget* hbox = gtk_hbox_new(false, 0); 234 container_ = gtk_util::CreateGtkBorderBin(hbox, NULL, 235 kBarPaddingTop, kBarPaddingBottom, 236 kEntryPaddingLeft, kBarPaddingRight); 237 gtk_widget_set_size_request(container_, kFindBarWidth, kFindBarHeight); 238 ViewIDUtil::SetID(container_, VIEW_ID_FIND_IN_PAGE); 239 gtk_widget_set_app_paintable(container_, TRUE); 240 241 slide_widget_.reset(new SlideAnimatorGtk(container_, 242 SlideAnimatorGtk::DOWN, 243 0, false, true, NULL)); 244 245 GtkWidget* close_alignment = gtk_alignment_new(0, 0.6, 1, 0); 246 close_button_.reset(CustomDrawButton::CloseButtonBar(theme_service_)); 247 gtk_container_add(GTK_CONTAINER(close_alignment), close_button_->widget()); 248 gtk_box_pack_end(GTK_BOX(hbox), close_alignment, FALSE, FALSE, 249 kCloseButtonPadding); 250 g_signal_connect(close_button_->widget(), "clicked", 251 G_CALLBACK(OnClickedThunk), this); 252 gtk_widget_set_tooltip_text(close_button_->widget(), 253 l10n_util::GetStringUTF8(IDS_FIND_IN_PAGE_CLOSE_TOOLTIP).c_str()); 254 255 find_next_button_.reset(new CustomDrawButton(theme_service_, 256 IDR_FINDINPAGE_NEXT, IDR_FINDINPAGE_NEXT_H, IDR_FINDINPAGE_NEXT_H, 257 IDR_FINDINPAGE_NEXT_D, GTK_STOCK_GO_DOWN, GTK_ICON_SIZE_MENU)); 258 g_signal_connect(find_next_button_->widget(), "clicked", 259 G_CALLBACK(OnClickedThunk), this); 260 gtk_widget_set_tooltip_text(find_next_button_->widget(), 261 l10n_util::GetStringUTF8(IDS_FIND_IN_PAGE_NEXT_TOOLTIP).c_str()); 262 gtk_box_pack_end(GTK_BOX(hbox), find_next_button_->widget(), 263 FALSE, FALSE, 0); 264 265 find_previous_button_.reset(new CustomDrawButton(theme_service_, 266 IDR_FINDINPAGE_PREV, IDR_FINDINPAGE_PREV_H, IDR_FINDINPAGE_PREV_H, 267 IDR_FINDINPAGE_PREV_D, GTK_STOCK_GO_UP, GTK_ICON_SIZE_MENU)); 268 g_signal_connect(find_previous_button_->widget(), "clicked", 269 G_CALLBACK(OnClickedThunk), this); 270 gtk_widget_set_tooltip_text(find_previous_button_->widget(), 271 l10n_util::GetStringUTF8(IDS_FIND_IN_PAGE_PREVIOUS_TOOLTIP).c_str()); 272 gtk_box_pack_end(GTK_BOX(hbox), find_previous_button_->widget(), 273 FALSE, FALSE, 0); 274 275 // Make a box for the edit and match count widgets. 276 GtkWidget* content_hbox = gtk_hbox_new(FALSE, 0); 277 278 text_entry_ = gtk_entry_new(); 279 gtk_entry_set_has_frame(GTK_ENTRY(text_entry_), FALSE); 280 281 match_count_label_ = gtk_label_new(NULL); 282 // This line adds padding on the sides so that the label has even padding on 283 // all edges. 284 gtk_misc_set_padding(GTK_MISC(match_count_label_), 2, 0); 285 match_count_event_box_ = gtk_event_box_new(); 286 GtkWidget* match_count_centerer = gtk_vbox_new(FALSE, 0); 287 gtk_box_pack_start(GTK_BOX(match_count_centerer), match_count_event_box_, 288 TRUE, TRUE, 0); 289 gtk_container_set_border_width(GTK_CONTAINER(match_count_centerer), 1); 290 gtk_container_add(GTK_CONTAINER(match_count_event_box_), match_count_label_); 291 292 gtk_box_pack_end(GTK_BOX(content_hbox), match_count_centerer, 293 FALSE, FALSE, 0); 294 gtk_box_pack_end(GTK_BOX(content_hbox), text_entry_, TRUE, TRUE, 0); 295 296 // This event box is necessary to color in the area above and below the match 297 // count label, and is where we draw the entry background onto in GTK mode. 298 BuildBorder(content_hbox, 0, 0, 0, 0, 299 &content_event_box_, &content_alignment_); 300 gtk_widget_set_app_paintable(content_event_box_, TRUE); 301 g_signal_connect(content_event_box_, "expose-event", 302 G_CALLBACK(OnContentEventBoxExpose), this); 303 304 // This alignment isn't centered and is used for spacing in chrome theme 305 // mode. (It's also used in GTK mode for padding because left padding doesn't 306 // equal bottom padding naturally.) 307 BuildBorder(content_event_box_, 2, 2, 2, 0, 308 &border_bin_, &border_bin_alignment_); 309 gtk_box_pack_end(GTK_BOX(hbox), border_bin_, TRUE, TRUE, 0); 310 311 theme_service_->InitThemesFor(this); 312 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, 313 content::Source<ThemeService>(theme_service_)); 314 315 g_signal_connect(widget(), "parent-set", G_CALLBACK(OnParentSet), this); 316 317 // We take care to avoid showing the slide animator widget. 318 gtk_widget_show_all(container_); 319 gtk_widget_show(widget()); 320 } 321 322 FindBarController* FindBarGtk::GetFindBarController() const { 323 return find_bar_controller_; 324 } 325 326 void FindBarGtk::SetFindBarController(FindBarController* find_bar_controller) { 327 find_bar_controller_ = find_bar_controller; 328 } 329 330 void FindBarGtk::Show(bool animate) { 331 if (animate) { 332 slide_widget_->Open(); 333 selection_rect_ = gfx::Rect(); 334 Reposition(); 335 GdkWindow* gdk_window = gtk_widget_get_window(container_); 336 if (gdk_window) 337 gdk_window_raise(gdk_window); 338 } else { 339 slide_widget_->OpenWithoutAnimation(); 340 } 341 } 342 343 void FindBarGtk::Hide(bool animate) { 344 if (animate) 345 slide_widget_->Close(); 346 else 347 slide_widget_->CloseWithoutAnimation(); 348 } 349 350 void FindBarGtk::SetFocusAndSelection() { 351 StoreOutsideFocus(); 352 gtk_widget_grab_focus(text_entry_); 353 // Select all the text. 354 gtk_editable_select_region(GTK_EDITABLE(text_entry_), 0, -1); 355 } 356 357 void FindBarGtk::ClearResults(const FindNotificationDetails& results) { 358 UpdateUIForFindResult(results, string16()); 359 } 360 361 void FindBarGtk::StopAnimation() { 362 slide_widget_->End(); 363 } 364 365 void FindBarGtk::MoveWindowIfNecessary(const gfx::Rect& selection_rect, 366 bool no_redraw) { 367 // Not moving the window on demand, so do nothing. 368 } 369 370 void FindBarGtk::SetFindText(const string16& find_text) { 371 std::string find_text_utf8 = UTF16ToUTF8(find_text); 372 373 // Ignore the "changed" signal handler because programatically setting the 374 // text should not fire a "changed" event. 375 ignore_changed_signal_ = true; 376 gtk_entry_set_text(GTK_ENTRY(text_entry_), find_text_utf8.c_str()); 377 ignore_changed_signal_ = false; 378 } 379 380 void FindBarGtk::UpdateUIForFindResult(const FindNotificationDetails& result, 381 const string16& find_text) { 382 selection_rect_ = result.selection_rect(); 383 int xposition = GetDialogPosition(result.selection_rect()).x(); 384 GtkAllocation allocation; 385 gtk_widget_get_allocation(widget(), &allocation); 386 if (xposition != allocation.x) 387 Reposition(); 388 389 // Once we find a match we no longer want to keep track of what had 390 // focus. EndFindSession will then set the focus to the page content. 391 if (result.number_of_matches() > 0) 392 focus_store_.Store(NULL); 393 394 std::string find_text_utf8 = UTF16ToUTF8(find_text); 395 bool have_valid_range = 396 result.number_of_matches() != -1 && result.active_match_ordinal() != -1; 397 398 std::string entry_text(gtk_entry_get_text(GTK_ENTRY(text_entry_))); 399 if (entry_text != find_text_utf8) { 400 SetFindText(find_text); 401 gtk_editable_select_region(GTK_EDITABLE(text_entry_), 0, -1); 402 } 403 404 if (!find_text.empty() && have_valid_range) { 405 gtk_label_set_text(GTK_LABEL(match_count_label_), 406 l10n_util::GetStringFUTF8(IDS_FIND_IN_PAGE_COUNT, 407 base::IntToString16(result.active_match_ordinal()), 408 base::IntToString16(result.number_of_matches())).c_str()); 409 UpdateMatchLabelAppearance(result.number_of_matches() == 0 && 410 result.final_update()); 411 } else { 412 // If there was no text entered, we don't show anything in the result count 413 // area. 414 gtk_label_set_text(GTK_LABEL(match_count_label_), ""); 415 UpdateMatchLabelAppearance(false); 416 } 417 } 418 419 void FindBarGtk::AudibleAlert() { 420 // This call causes a lot of weird bugs, especially when using the custom 421 // frame. TODO(estade): if people complain, re-enable it. See 422 // http://crbug.com/27635 and others. 423 // 424 // gtk_widget_error_bell(widget()); 425 } 426 427 gfx::Rect FindBarGtk::GetDialogPosition( 428 const gfx::Rect& avoid_overlapping_rect) { 429 bool ltr = !base::i18n::IsRTL(); 430 // 15 is the size of the scrollbar, copied from ScrollbarThemeChromium. 431 // The height is not used. 432 // At very low browser widths we can wind up with a negative |dialog_bounds| 433 // width, so clamp it to 0. 434 GtkAllocation parent_allocation; 435 gtk_widget_get_allocation(gtk_widget_get_parent(widget()), 436 &parent_allocation); 437 gfx::Rect dialog_bounds = gfx::Rect(ltr ? 0 : 15, 0, 438 std::max(0, parent_allocation.width - (ltr ? 15 : 0)), 0); 439 440 GtkRequisition req; 441 gtk_widget_size_request(container_, &req); 442 gfx::Size prefsize(req.width, req.height); 443 444 gfx::Rect view_location( 445 ltr ? dialog_bounds.width() - prefsize.width() : dialog_bounds.x(), 446 dialog_bounds.y(), prefsize.width(), prefsize.height()); 447 gfx::Rect new_pos = FindBarController::GetLocationForFindbarView( 448 view_location, dialog_bounds, avoid_overlapping_rect); 449 450 return new_pos; 451 } 452 453 bool FindBarGtk::IsFindBarVisible() { 454 return gtk_widget_get_visible(widget()); 455 } 456 457 void FindBarGtk::RestoreSavedFocus() { 458 // This function sometimes gets called when we don't have focus. We should do 459 // nothing in this case. 460 if (!gtk_widget_is_focus(text_entry_)) 461 return; 462 463 if (focus_store_.widget()) 464 gtk_widget_grab_focus(focus_store_.widget()); 465 else 466 find_bar_controller_->web_contents()->GetView()->Focus(); 467 } 468 469 bool FindBarGtk::HasGlobalFindPasteboard() { 470 return false; 471 } 472 473 void FindBarGtk::UpdateFindBarForChangedWebContents() { 474 } 475 476 FindBarTesting* FindBarGtk::GetFindBarTesting() { 477 return this; 478 } 479 480 void FindBarGtk::Observe(int type, 481 const content::NotificationSource& source, 482 const content::NotificationDetails& details) { 483 DCHECK_EQ(type, chrome::NOTIFICATION_BROWSER_THEME_CHANGED); 484 485 // Force reshapings of the find bar window. 486 container_width_ = -1; 487 container_height_ = -1; 488 489 if (theme_service_->UsingNativeTheme()) { 490 gtk_widget_modify_cursor(text_entry_, NULL, NULL); 491 gtk_widget_modify_base(text_entry_, GTK_STATE_NORMAL, NULL); 492 gtk_widget_modify_text(text_entry_, GTK_STATE_NORMAL, NULL); 493 494 // Prevent forced font sizes because it causes the jump up and down 495 // character movement (http://crbug.com/22614), and because it will 496 // prevent centering of the text entry. 497 gtk_util::UndoForceFontSize(text_entry_); 498 gtk_util::UndoForceFontSize(match_count_label_); 499 500 gtk_widget_set_size_request(content_event_box_, -1, -1); 501 gtk_widget_modify_bg(content_event_box_, GTK_STATE_NORMAL, NULL); 502 503 // Replicate the normal GtkEntry behaviour by drawing the entry 504 // background. We set the fake alignment to be the frame thickness. 505 GtkStyle* style = gtk_rc_get_style(text_entry_); 506 gint xborder = style->xthickness; 507 gint yborder = style->ythickness; 508 gtk_alignment_set_padding(GTK_ALIGNMENT(content_alignment_), 509 yborder, yborder, xborder, xborder); 510 511 // We leave left padding on the left, even in GTK mode, as it's required 512 // for the left margin to be equivalent to the bottom margin. 513 gtk_alignment_set_padding(GTK_ALIGNMENT(border_bin_alignment_), 514 0, 0, 1, 0); 515 516 // We need this event box to have its own window in GTK mode for doing the 517 // hacky widget rendering. 518 gtk_event_box_set_visible_window(GTK_EVENT_BOX(border_bin_), TRUE); 519 gtk_widget_set_app_paintable(border_bin_, TRUE); 520 521 gtk_misc_set_alignment(GTK_MISC(match_count_label_), 0.5, 0.5); 522 } else { 523 gtk_widget_modify_cursor(text_entry_, &ui::kGdkBlack, &ui::kGdkGray); 524 gtk_widget_modify_base(text_entry_, GTK_STATE_NORMAL, 525 &kEntryBackgroundColor); 526 gtk_widget_modify_text(text_entry_, GTK_STATE_NORMAL, &kEntryTextColor); 527 528 // Until we switch to vector graphics, force the font size. 529 gtk_util::ForceFontSizePixels(text_entry_, 13.4); // 13.4px == 10pt @ 96dpi 530 gtk_util::ForceFontSizePixels(match_count_label_, 13.4); 531 532 // Force the text widget height so it lines up with the buttons regardless 533 // of font size. 534 gtk_widget_set_size_request(content_event_box_, -1, 20); 535 gtk_widget_modify_bg(content_event_box_, GTK_STATE_NORMAL, 536 &kEntryBackgroundColor); 537 538 gtk_alignment_set_padding(GTK_ALIGNMENT(content_alignment_), 539 0.0, 0.0, 0.0, 0.0); 540 541 gtk_alignment_set_padding(GTK_ALIGNMENT(border_bin_alignment_), 542 2, 2, 3, 0); 543 544 // We need this event box to be invisible because we're only going to draw 545 // on the background (but we can't take it out of the heiarchy entirely 546 // because we also need it to take up space). 547 gtk_event_box_set_visible_window(GTK_EVENT_BOX(border_bin_), FALSE); 548 gtk_widget_set_app_paintable(border_bin_, FALSE); 549 550 gtk_misc_set_alignment(GTK_MISC(match_count_label_), 0.5, 1.0); 551 552 // This is necessary to make the close button dark enough. 553 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 554 close_button_->SetBackground( 555 theme_service_->GetColor(ThemeProperties::COLOR_TAB_TEXT), 556 rb.GetImageNamed(IDR_CLOSE_1).AsBitmap(), 557 rb.GetImageNamed(IDR_CLOSE_1).AsBitmap()); 558 } 559 560 UpdateMatchLabelAppearance(match_label_failure_); 561 } 562 563 bool FindBarGtk::GetFindBarWindowInfo(gfx::Point* position, 564 bool* fully_visible) { 565 if (position) 566 *position = GetPosition(); 567 568 if (fully_visible) { 569 *fully_visible = !slide_widget_->IsAnimating() && 570 slide_widget_->IsShowing(); 571 } 572 return true; 573 } 574 575 string16 FindBarGtk::GetFindText() { 576 std::string contents(gtk_entry_get_text(GTK_ENTRY(text_entry_))); 577 return UTF8ToUTF16(contents); 578 } 579 580 string16 FindBarGtk::GetFindSelectedText() { 581 gint cursor_pos; 582 gint selection_bound; 583 g_object_get(G_OBJECT(text_entry_), "cursor-position", &cursor_pos, 584 NULL); 585 g_object_get(G_OBJECT(text_entry_), "selection-bound", &selection_bound, 586 NULL); 587 std::string contents(gtk_entry_get_text(GTK_ENTRY(text_entry_))); 588 return UTF8ToUTF16(contents.substr(cursor_pos, selection_bound)); 589 } 590 591 string16 FindBarGtk::GetMatchCountText() { 592 std::string contents(gtk_label_get_text(GTK_LABEL(match_count_label_))); 593 return UTF8ToUTF16(contents); 594 } 595 596 int FindBarGtk::GetWidth() { 597 GtkAllocation allocation; 598 gtk_widget_get_allocation(container_, &allocation); 599 return allocation.width; 600 } 601 602 void FindBarGtk::FindEntryTextInContents(bool forward_search) { 603 content::WebContents* web_contents = find_bar_controller_->web_contents(); 604 if (!web_contents) 605 return; 606 FindTabHelper* find_tab_helper = FindTabHelper::FromWebContents(web_contents); 607 608 std::string new_contents(gtk_entry_get_text(GTK_ENTRY(text_entry_))); 609 610 if (new_contents.length() > 0) { 611 find_tab_helper->StartFinding(UTF8ToUTF16(new_contents), forward_search, 612 false); // Not case sensitive. 613 } else { 614 // The textbox is empty so we reset. 615 find_tab_helper->StopFinding(FindBarController::kClearSelectionOnPage); 616 UpdateUIForFindResult(find_tab_helper->find_result(), string16()); 617 618 // Clearing the text box should also clear the prepopulate state so that 619 // when we close and reopen the Find box it doesn't show the search we 620 // just deleted. 621 FindBarState* find_bar_state = FindBarStateFactory::GetForProfile( 622 browser_->profile()); 623 find_bar_state->set_last_prepopulate_text(string16()); 624 } 625 } 626 627 void FindBarGtk::UpdateMatchLabelAppearance(bool failure) { 628 match_label_failure_ = failure; 629 bool use_gtk = theme_service_->UsingNativeTheme(); 630 631 if (use_gtk) { 632 GtkStyle* style = gtk_rc_get_style(text_entry_); 633 GdkColor normal_bg = style->base[GTK_STATE_NORMAL]; 634 GdkColor normal_text = gtk_util::AverageColors( 635 style->text[GTK_STATE_NORMAL], style->base[GTK_STATE_NORMAL]); 636 637 gtk_widget_modify_bg(match_count_event_box_, GTK_STATE_NORMAL, 638 failure ? &kFindFailureBackgroundColor : 639 &normal_bg); 640 gtk_widget_modify_fg(match_count_label_, GTK_STATE_NORMAL, 641 failure ? &kEntryTextColor : &normal_text); 642 } else { 643 gtk_widget_modify_bg(match_count_event_box_, GTK_STATE_NORMAL, 644 failure ? &kFindFailureBackgroundColor : 645 &kEntryBackgroundColor); 646 gtk_widget_modify_fg(match_count_label_, GTK_STATE_NORMAL, 647 failure ? &kEntryTextColor : &kFindSuccessTextColor); 648 } 649 } 650 651 void FindBarGtk::Reposition() { 652 if (!IsFindBarVisible()) 653 return; 654 655 // This will trigger an allocate, which allows us to reposition. 656 GtkWidget* parent = gtk_widget_get_parent(widget()); 657 if (parent) 658 gtk_widget_queue_resize(parent); 659 } 660 661 void FindBarGtk::StoreOutsideFocus() { 662 // |text_entry_| is the only widget in the find bar that can be focused, 663 // so it's the only one we have to check. 664 // TODO(estade): when we make the find bar buttons focusable, we'll have 665 // to change this (same above in RestoreSavedFocus). 666 if (!gtk_widget_is_focus(text_entry_)) 667 focus_store_.Store(text_entry_); 668 } 669 670 bool FindBarGtk::MaybeForwardKeyEventToRenderer(GdkEventKey* event) { 671 switch (event->keyval) { 672 case GDK_Down: 673 case GDK_Up: 674 case GDK_Page_Up: 675 case GDK_Page_Down: 676 break; 677 case GDK_Home: 678 case GDK_End: 679 if ((event->state & gtk_accelerator_get_default_mod_mask()) == 680 GDK_CONTROL_MASK) { 681 break; 682 } 683 // Fall through. 684 default: 685 return false; 686 } 687 688 content::WebContents* contents = find_bar_controller_->web_contents(); 689 if (!contents) 690 return false; 691 692 content::RenderViewHost* render_view_host = contents->GetRenderViewHost(); 693 694 // Make sure we don't have a text field element interfering with keyboard 695 // input. Otherwise Up and Down arrow key strokes get eaten. "Nom Nom Nom". 696 render_view_host->ClearFocusedNode(); 697 698 NativeWebKeyboardEvent wke(reinterpret_cast<GdkEvent*>(event)); 699 render_view_host->ForwardKeyboardEvent(wke); 700 return true; 701 } 702 703 void FindBarGtk::AdjustTextAlignment() { 704 PangoDirection content_dir = 705 pango_find_base_dir(gtk_entry_get_text(GTK_ENTRY(text_entry_)), -1); 706 707 GtkTextDirection widget_dir = gtk_widget_get_direction(text_entry_); 708 709 // Use keymap or widget direction if content does not have strong direction. 710 // It matches the behavior of GtkEntry. 711 if (content_dir == PANGO_DIRECTION_NEUTRAL) { 712 if (gtk_widget_has_focus(text_entry_)) { 713 content_dir = gdk_keymap_get_direction( 714 gdk_keymap_get_for_display(gtk_widget_get_display(text_entry_))); 715 } else { 716 if (widget_dir == GTK_TEXT_DIR_RTL) 717 content_dir = PANGO_DIRECTION_RTL; 718 else 719 content_dir = PANGO_DIRECTION_LTR; 720 } 721 } 722 723 if ((widget_dir == GTK_TEXT_DIR_RTL && content_dir == PANGO_DIRECTION_LTR) || 724 (widget_dir == GTK_TEXT_DIR_LTR && content_dir == PANGO_DIRECTION_RTL)) { 725 gtk_entry_set_alignment(GTK_ENTRY(text_entry_), 1.0); 726 } else { 727 gtk_entry_set_alignment(GTK_ENTRY(text_entry_), 0.0); 728 } 729 } 730 731 gfx::Point FindBarGtk::GetPosition() { 732 gfx::Point point; 733 734 GtkWidget* parent = gtk_widget_get_parent(widget()); 735 736 GValue value = { 0, }; 737 g_value_init(&value, G_TYPE_INT); 738 gtk_container_child_get_property(GTK_CONTAINER(parent), 739 widget(), "x", &value); 740 point.set_x(g_value_get_int(&value)); 741 742 gtk_container_child_get_property(GTK_CONTAINER(parent), 743 widget(), "y", &value); 744 point.set_y(g_value_get_int(&value)); 745 746 g_value_unset(&value); 747 748 return point; 749 } 750 751 // static 752 void FindBarGtk::OnParentSet(GtkWidget* widget, GtkObject* old_parent, 753 FindBarGtk* find_bar) { 754 if (!gtk_widget_get_parent(widget)) 755 return; 756 757 g_signal_connect(gtk_widget_get_parent(widget), "set-floating-position", 758 G_CALLBACK(OnSetFloatingPosition), find_bar); 759 } 760 761 // static 762 void FindBarGtk::OnSetFloatingPosition(GtkFloatingContainer* floating_container, 763 GtkAllocation* allocation, 764 FindBarGtk* find_bar) { 765 GtkWidget* findbar = find_bar->widget(); 766 767 int xposition = find_bar->GetDialogPosition(find_bar->selection_rect_).x(); 768 769 GValue value = { 0, }; 770 g_value_init(&value, G_TYPE_INT); 771 g_value_set_int(&value, xposition); 772 gtk_container_child_set_property(GTK_CONTAINER(floating_container), 773 findbar, "x", &value); 774 775 g_value_set_int(&value, 0); 776 gtk_container_child_set_property(GTK_CONTAINER(floating_container), 777 findbar, "y", &value); 778 g_value_unset(&value); 779 } 780 781 // static 782 gboolean FindBarGtk::OnChanged(GtkWindow* window, FindBarGtk* find_bar) { 783 find_bar->AdjustTextAlignment(); 784 785 if (!find_bar->ignore_changed_signal_) 786 find_bar->FindEntryTextInContents(true); 787 788 return FALSE; 789 } 790 791 // static 792 gboolean FindBarGtk::OnKeyPressEvent(GtkWidget* widget, GdkEventKey* event, 793 FindBarGtk* find_bar) { 794 if (find_bar->MaybeForwardKeyEventToRenderer(event)) { 795 return TRUE; 796 } else if (GDK_Escape == event->keyval) { 797 find_bar->find_bar_controller_->EndFindSession( 798 FindBarController::kKeepSelectionOnPage, 799 FindBarController::kKeepResultsInFindBox); 800 return TRUE; 801 } else if (GDK_Return == event->keyval || 802 GDK_KP_Enter == event->keyval) { 803 if ((event->state & gtk_accelerator_get_default_mod_mask()) == 804 GDK_CONTROL_MASK) { 805 find_bar->find_bar_controller_->EndFindSession( 806 FindBarController::kActivateSelectionOnPage, 807 FindBarController::kClearResultsInFindBox); 808 return TRUE; 809 } 810 811 bool forward = (event->state & gtk_accelerator_get_default_mod_mask()) != 812 GDK_SHIFT_MASK; 813 find_bar->FindEntryTextInContents(forward); 814 return TRUE; 815 } else if (GDK_F3 == event->keyval) { 816 // There is a bug in GTK+ version available with Ubuntu 12.04 which causes 817 // Shift+Fn key combination getting registered as Fn when used with 818 // GTK accelerators. And this broke the search backward functionality with 819 // Shift+F3. This is a workaround to fix the search functionality till we 820 // have the GTK+ fix available. The GTK+ issue is being tracked under 821 // https://bugzilla.gnome.org/show_bug.cgi?id=661973 822 bool forward = (event->state & gtk_accelerator_get_default_mod_mask()) != 823 GDK_SHIFT_MASK; 824 find_bar->FindEntryTextInContents(forward); 825 return TRUE; 826 } 827 return FALSE; 828 } 829 830 // static 831 gboolean FindBarGtk::OnKeyReleaseEvent(GtkWidget* widget, GdkEventKey* event, 832 FindBarGtk* find_bar) { 833 return find_bar->MaybeForwardKeyEventToRenderer(event); 834 } 835 836 void FindBarGtk::OnClicked(GtkWidget* button) { 837 if (button == close_button_->widget()) { 838 find_bar_controller_->EndFindSession( 839 FindBarController::kKeepSelectionOnPage, 840 FindBarController::kKeepResultsInFindBox); 841 } else if (button == find_previous_button_->widget() || 842 button == find_next_button_->widget()) { 843 FindEntryTextInContents(button == find_next_button_->widget()); 844 } else { 845 NOTREACHED(); 846 } 847 } 848 849 // static 850 gboolean FindBarGtk::OnContentEventBoxExpose(GtkWidget* widget, 851 GdkEventExpose* event, 852 FindBarGtk* bar) { 853 TRACE_EVENT0("ui::gtk", "FindBarGtk::OnContentEventBoxExpose"); 854 if (bar->theme_service_->UsingNativeTheme()) { 855 // Draw the text entry background around where we input stuff. Note the 856 // decrement to |width|. We do this because some theme engines 857 // (*cough*Clearlooks*cough*) don't do any blending and use thickness to 858 // make sure that widgets never overlap. 859 int padding = gtk_widget_get_style(widget)->xthickness; 860 GdkRectangle rec; 861 gtk_widget_get_allocation(widget, &rec); 862 rec.width -= padding; 863 864 gtk_util::DrawTextEntryBackground(bar->text_entry_, widget, 865 &event->area, &rec); 866 } 867 868 return FALSE; 869 } 870 871 // Used to handle custom painting of |container_|. 872 gboolean FindBarGtk::OnExpose(GtkWidget* widget, GdkEventExpose* e, 873 FindBarGtk* bar) { 874 TRACE_EVENT0("ui::gtk", "FindBarGtk::OnExpose"); 875 876 GtkAllocation allocation; 877 gtk_widget_get_allocation(widget, &allocation); 878 879 if (bar->theme_service_->UsingNativeTheme()) { 880 if (bar->container_width_ != allocation.width || 881 bar->container_height_ != allocation.height) { 882 std::vector<GdkPoint> mask_points = MakeFramePolygonPoints( 883 allocation.width, allocation.height, FRAME_MASK); 884 GdkRegion* mask_region = gdk_region_polygon(&mask_points[0], 885 mask_points.size(), 886 GDK_EVEN_ODD_RULE); 887 // Reset the shape. 888 GdkWindow* gdk_window = gtk_widget_get_window(widget); 889 gdk_window_shape_combine_region(gdk_window, NULL, 0, 0); 890 gdk_window_shape_combine_region(gdk_window, mask_region, 0, 0); 891 gdk_region_destroy(mask_region); 892 893 bar->container_width_ = allocation.width; 894 bar->container_height_ = allocation.height; 895 } 896 897 GdkDrawable* drawable = GDK_DRAWABLE(e->window); 898 GdkGC* gc = gdk_gc_new(drawable); 899 gdk_gc_set_clip_rectangle(gc, &e->area); 900 GdkColor color = bar->theme_service_->GetBorderColor(); 901 gdk_gc_set_rgb_fg_color(gc, &color); 902 903 // Stroke the frame border. 904 std::vector<GdkPoint> points = MakeFramePolygonPoints( 905 allocation.width, allocation.height, FRAME_STROKE); 906 gdk_draw_lines(drawable, gc, &points[0], points.size()); 907 908 g_object_unref(gc); 909 } else { 910 if (bar->container_width_ != allocation.width || 911 bar->container_height_ != allocation.height) { 912 // Reset the shape. 913 gdk_window_shape_combine_region(gtk_widget_get_window(widget), 914 NULL, 0, 0); 915 SetDialogShape(bar->container_); 916 917 bar->container_width_ = allocation.width; 918 bar->container_height_ = allocation.height; 919 } 920 921 cairo_t* cr = gdk_cairo_create(gtk_widget_get_window(widget)); 922 gdk_cairo_rectangle(cr, &e->area); 923 cairo_clip(cr); 924 925 gfx::Point tabstrip_origin = 926 bar->window_->tabstrip()->GetTabStripOriginForWidget(widget); 927 928 gtk_util::DrawThemedToolbarBackground(widget, cr, e, tabstrip_origin, 929 bar->theme_service_); 930 931 // During chrome theme mode, we need to draw the border around content_hbox 932 // now instead of when we render |border_bin_|. We don't use stacked event 933 // boxes to simulate the effect because we need to blend them with this 934 // background. 935 GtkAllocation border_allocation; 936 gtk_widget_get_allocation(bar->border_bin_, &border_allocation); 937 938 // Blit the left part of the background image once on the left. 939 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 940 941 gfx::CairoCachedSurface* background_left = rb.GetNativeImageNamed( 942 IDR_FIND_BOX_BACKGROUND_LEFT, 943 ui::ResourceBundle::RTL_ENABLED).ToCairo(); 944 background_left->SetSource(cr, widget, 945 border_allocation.x, border_allocation.y); 946 cairo_pattern_set_extend(cairo_get_source(cr), CAIRO_EXTEND_REPEAT); 947 cairo_rectangle(cr, border_allocation.x, border_allocation.y, 948 background_left->Width(), background_left->Height()); 949 cairo_fill(cr); 950 951 // Blit the center part of the background image in all the space between. 952 gfx::CairoCachedSurface* background = 953 rb.GetNativeImageNamed(IDR_FIND_BOX_BACKGROUND).ToCairo(); 954 background->SetSource(cr, widget, 955 border_allocation.x + background_left->Width(), 956 border_allocation.y); 957 cairo_pattern_set_extend(cairo_get_source(cr), CAIRO_EXTEND_REPEAT); 958 cairo_rectangle(cr, 959 border_allocation.x + background_left->Width(), 960 border_allocation.y, 961 border_allocation.width - background_left->Width(), 962 background->Height()); 963 cairo_fill(cr); 964 965 cairo_destroy(cr); 966 967 // Draw the border. 968 GetDialogBorder()->RenderToWidget(widget); 969 } 970 971 // Propagate to the container's child. 972 GtkWidget* child = gtk_bin_get_child(GTK_BIN(widget)); 973 if (child) 974 gtk_container_propagate_expose(GTK_CONTAINER(widget), child, e); 975 return TRUE; 976 } 977 978 // static 979 gboolean FindBarGtk::OnFocus(GtkWidget* text_entry, GtkDirectionType focus, 980 FindBarGtk* find_bar) { 981 find_bar->StoreOutsideFocus(); 982 983 // Continue propagating the event. 984 return FALSE; 985 } 986 987 // static 988 gboolean FindBarGtk::OnButtonPress(GtkWidget* text_entry, GdkEventButton* e, 989 FindBarGtk* find_bar) { 990 find_bar->StoreOutsideFocus(); 991 992 // Continue propagating the event. 993 return FALSE; 994 } 995 996 // static 997 void FindBarGtk::OnMoveCursor(GtkEntry* entry, GtkMovementStep step, gint count, 998 gboolean selection, FindBarGtk* bar) { 999 static guint signal_id = g_signal_lookup("move-cursor", GTK_TYPE_ENTRY); 1000 1001 GdkEvent* event = gtk_get_current_event(); 1002 if (event) { 1003 if ((event->type == GDK_KEY_PRESS || event->type == GDK_KEY_RELEASE) && 1004 bar->MaybeForwardKeyEventToRenderer(&(event->key))) { 1005 g_signal_stop_emission(entry, signal_id, 0); 1006 } 1007 1008 gdk_event_free(event); 1009 } 1010 } 1011 1012 void FindBarGtk::OnActivate(GtkWidget* entry) { 1013 FindEntryTextInContents(true); 1014 } 1015 1016 gboolean FindBarGtk::OnFocusIn(GtkWidget* entry, GdkEventFocus* event) { 1017 g_signal_connect(gdk_keymap_get_for_display(gtk_widget_get_display(entry)), 1018 "direction-changed", 1019 G_CALLBACK(&OnKeymapDirectionChanged), this); 1020 1021 AdjustTextAlignment(); 1022 1023 return FALSE; // Continue propagation. 1024 } 1025 1026 gboolean FindBarGtk::OnFocusOut(GtkWidget* entry, GdkEventFocus* event) { 1027 g_signal_handlers_disconnect_by_func( 1028 gdk_keymap_get_for_display(gtk_widget_get_display(entry)), 1029 reinterpret_cast<gpointer>(&OnKeymapDirectionChanged), this); 1030 1031 return FALSE; // Continue propagation. 1032 } 1033