Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(597)

Unified Diff: chrome/browser/views/find_bar_host.cc

Issue 200035: First cut at implementation of FindBar for views / gtk... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/views/find_bar_host.cc
===================================================================
--- chrome/browser/views/find_bar_host.cc (revision 25582)
+++ chrome/browser/views/find_bar_host.cc (working copy)
@@ -2,9 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "chrome/browser/views/find_bar_win.h"
+#include "chrome/browser/views/find_bar_host.h"
#include "app/slide_animation.h"
+#include "base/keyboard_codes.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/find_bar_controller.h"
@@ -19,71 +20,22 @@
#include "views/controls/scrollbar/native_scroll_bar.h"
#include "views/widget/root_view.h"
-#if defined(OS_WIN)
-#include "views/widget/widget_win.h"
-#else
-#include "views/widget/widget_gtk.h"
-#endif
-
// static
-bool FindBarWin::disable_animations_during_testing_ = false;
+bool FindBarHost::disable_animations_during_testing_ = false;
-// Host is the actual widget containing FindBarView.
-#if defined(OS_WIN)
-class FindBarWin::Host : public views::WidgetWin {
- public:
- explicit Host(FindBarWin* find_bar) : find_bar_(find_bar) {
- // Don't let WidgetWin manage our lifetime. We want our lifetime to
- // coincide with TabContents.
- set_delete_on_destroy(false);
- set_window_style(WS_CHILD | WS_CLIPCHILDREN);
- set_window_ex_style(WS_EX_TOPMOST);
- }
-
- void OnFinalMessage(HWND window) {
- find_bar_->OnFinalMessage();
- }
-
- private:
- FindBarWin* find_bar_;
-
- DISALLOW_COPY_AND_ASSIGN(Host);
-};
-#else
-class FindBarWin::Host : public views::WidgetGtk {
- public:
- explicit Host(FindBarWin* find_bar)
- : WidgetGtk(TYPE_CHILD),
- find_bar_(find_bar) {
- // Don't let WidgetWin manage our lifetime. We want our lifetime to
- // coincide with TabContents.
- set_delete_on_destroy(false);
- }
-
- void OnDestroy(GtkWidget* widget) {
- find_bar_->OnFinalMessage();
- }
-
- private:
- FindBarWin* find_bar_;
-
- DISALLOW_COPY_AND_ASSIGN(Host);
-};
-#endif
-
namespace browser {
// Declared in browser_dialogs.h so others don't have to depend on our header.
FindBar* CreateFindBar(BrowserView* browser_view) {
- return new FindBarWin(browser_view);
+ return new FindBarHost(browser_view);
}
} // namespace browser
////////////////////////////////////////////////////////////////////////////////
-// FindBarWin, public:
+// FindBarHost, public:
-FindBarWin::FindBarWin(BrowserView* browser_view)
+FindBarHost::FindBarHost(BrowserView* browser_view)
: browser_view_(browser_view),
find_dialog_animation_offset_(0),
esc_accel_target_registered_(false),
@@ -91,8 +43,8 @@
view_ = new FindBarView(this);
// Initialize the host.
- host_.reset(new Host(this));
- host_->Init(browser_view->GetWidget()->GetNativeView(), gfx::Rect());
+ host_.reset(CreateHost());
+ host_->Init(GetNativeView(browser_view), gfx::Rect());
host_->SetContentsView(view_);
// Start listening to focus changes, so we can register and unregister our
@@ -116,123 +68,10 @@
animation_.reset(new SlideAnimation(this));
}
-FindBarWin::~FindBarWin() {
+FindBarHost::~FindBarHost() {
}
-// TODO(brettw) this should not be so complicated. The view should really be in
-// charge of these regions. CustomFrameWindow will do this for us. It will also
-// let us set a path for the window region which will avoid some logic here.
-void FindBarWin::UpdateWindowEdges(const gfx::Rect& new_pos) {
-#if defined(OS_WIN)
- // |w| is used to make it easier to create the part of the polygon that curves
- // the right side of the Find window. It essentially keeps track of the
- // x-pixel position of the right-most background image inside the view.
- // TODO(finnur): Let the view tell us how to draw the curves or convert
- // this to a CustomFrameWindow.
- int w = new_pos.width() - 6; // -6 positions us at the left edge of the
- // rightmost background image of the view.
-
- // This polygon array represents the outline of the background image for the
- // dialog. Basically, it encompasses only the visible pixels of the
- // concatenated find_dlg_LMR_bg images (where LMR = [left | middle | right]).
- static const POINT polygon[] = {
- {0, 0}, {0, 1}, {2, 3}, {2, 29}, {4, 31},
- {4, 32}, {w+0, 32},
- {w+0, 31}, {w+1, 31}, {w+3, 29}, {w+3, 3}, {w+6, 0}
- };
-
- // Find the largest x and y value in the polygon.
- int max_x = 0, max_y = 0;
- for (int i = 0; i < arraysize(polygon); i++) {
- max_x = std::max(max_x, static_cast<int>(polygon[i].x));
- max_y = std::max(max_y, static_cast<int>(polygon[i].y));
- }
-
- // We then create the polygon and use SetWindowRgn to force the window to draw
- // only within that area. This region may get reduced in size below.
- HRGN region = CreatePolygonRgn(polygon, arraysize(polygon), ALTERNATE);
-
- // Are we animating?
- if (find_dialog_animation_offset_ > 0) {
- // The animation happens in two steps: First, we clip the window and then in
- // GetDialogPosition we offset the window position so that it still looks
- // attached to the toolbar as it grows. We clip the window by creating a
- // rectangle region (that gradually increases as the animation progresses)
- // and find the intersection between the two regions using CombineRgn.
-
- // |y| shrinks as the animation progresses from the height of the view down
- // to 0 (and reverses when closing).
- int y = find_dialog_animation_offset_;
- // |y| shrinking means the animation (visible) region gets larger. In other
- // words: the rectangle grows upward (when the dialog is opening).
- HRGN animation_region = CreateRectRgn(0, y, max_x, max_y);
- // |region| will contain the intersected parts after calling this function:
- CombineRgn(region, animation_region, region, RGN_AND);
- DeleteObject(animation_region);
-
- // Next, we need to increase the region a little bit to account for the
- // curved edges that the view will draw to make it look like grows out of
- // the toolbar.
- POINT left_curve[] = {
- {0, y+0}, {0, y+1}, {2, y+3}, {2, y+0}, {0, y+0}
- };
- POINT right_curve[] = {
- {w+3, y+3}, {w+6, y+0}, {w+3, y+0}, {w+3, y+3}
- };
-
- // Combine the region for the curve on the left with our main region.
- HRGN r = CreatePolygonRgn(left_curve, arraysize(left_curve), ALTERNATE);
- CombineRgn(region, r, region, RGN_OR);
- DeleteObject(r);
-
- // Combine the region for the curve on the right with our main region.
- r = CreatePolygonRgn(right_curve, arraysize(right_curve), ALTERNATE);
- CombineRgn(region, r, region, RGN_OR);
- DeleteObject(r);
- }
-
- // Now see if we need to truncate the region because parts of it obscures
- // the main window border.
- gfx::Rect dialog_bounds;
- GetDialogBounds(&dialog_bounds);
-
- // Calculate how much our current position overlaps our boundaries. If we
- // overlap, it means we have too little space to draw the whole dialog and
- // we allow overwriting the scrollbar before we start truncating our dialog.
- //
- // TODO(brettw) this constant is evil. This is the amount of room we've added
- // to the window size, when we set the region, it can change the size.
- static const int kAddedWidth = 7;
- int difference = (new_pos.right() - kAddedWidth) -
- dialog_bounds.width() -
- views::NativeScrollBar::GetVerticalScrollBarWidth() +
- 1;
- if (difference > 0) {
- POINT exclude[4] = {0};
- exclude[0].x = max_x - difference; // Top left corner.
- exclude[0].y = 0;
-
- exclude[1].x = max_x; // Top right corner.
- exclude[1].y = 0;
-
- exclude[2].x = max_x; // Bottom right corner.
- exclude[2].y = max_y;
-
- exclude[3].x = max_x - difference; // Bottom left corner.
- exclude[3].y = max_y;
-
- // Subtract this region from the original region.
- HRGN exclude_rgn = CreatePolygonRgn(exclude, arraysize(exclude), ALTERNATE);
- int result = CombineRgn(region, region, exclude_rgn, RGN_DIFF);
- DeleteObject(exclude_rgn);
- }
-
- // The system now owns the region, so we do not delete it.
- host_->SetWindowRgn(region, TRUE); // TRUE = Redraw.
-#endif
-}
-
-void FindBarWin::Show() {
+void FindBarHost::Show() {
if (disable_animations_during_testing_) {
animation_->Reset(1);
MoveWindowIfNecessary(gfx::Rect(), true);
@@ -242,15 +81,15 @@
}
}
-void FindBarWin::SetFocusAndSelection() {
+void FindBarHost::SetFocusAndSelection() {
view_->SetFocusAndSelection();
}
-bool FindBarWin::IsAnimating() {
+bool FindBarHost::IsAnimating() {
return animation_->IsAnimating();
}
-void FindBarWin::Hide(bool animate) {
+void FindBarHost::Hide(bool animate) {
if (animate && !disable_animations_during_testing_) {
animation_->Reset(1.0);
animation_->Hide();
@@ -259,23 +98,23 @@
}
}
-void FindBarWin::ClearResults(const FindNotificationDetails& results) {
+void FindBarHost::ClearResults(const FindNotificationDetails& results) {
view_->UpdateForResult(results, string16());
}
-void FindBarWin::StopAnimation() {
+void FindBarHost::StopAnimation() {
animation_->End();
}
-void FindBarWin::SetFindText(const string16& find_text) {
+void FindBarHost::SetFindText(const string16& find_text) {
view_->SetFindText(find_text);
}
-bool FindBarWin::IsFindBarVisible() {
+bool FindBarHost::IsFindBarVisible() {
return host_->IsVisible();
}
-void FindBarWin::MoveWindowIfNecessary(const gfx::Rect& selection_rect,
+void FindBarHost::MoveWindowIfNecessary(const gfx::Rect& selection_rect,
bool no_redraw) {
// We only move the window if one is active for the current TabContents. If we
// don't check this, then SetDialogPosition below will end up making the Find
@@ -292,46 +131,7 @@
view_->SchedulePaint();
}
-#if defined(OS_WIN)
-bool FindBarWin::MaybeForwardKeystrokeToWebpage(
- UINT message, TCHAR key, UINT flags) {
- // We specifically ignore WM_CHAR. See http://crbug.com/10509.
- if (message != WM_KEYDOWN && message != WM_KEYUP)
- return false;
-
- switch (key) {
- case VK_HOME:
- case VK_END:
- // Ctrl+Home and Ctrl+End should be forwarded to the page.
- if (GetKeyState(VK_CONTROL) >= 0)
- return false; // Ctrl not pressed: Abort. Otherwise fall through.
- case VK_UP:
- case VK_DOWN:
- case VK_PRIOR: // Page up
- case VK_NEXT: // Page down
- break; // The keys above are the ones we want to forward to the page.
- default:
- return false;
- }
-
- TabContents* contents = find_bar_controller_->tab_contents();
- if (!contents)
- return false;
-
- RenderViewHost* render_view_host = contents->render_view_host();
-
- // Make sure we don't have a text field element interfering with keyboard
- // input. Otherwise Up and Down arrow key strokes get eaten. "Nom Nom Nom".
- render_view_host->ClearFocusedNode();
-
- HWND hwnd = contents->GetContentNativeView();
- render_view_host->ForwardKeyboardEvent(
- NativeWebKeyboardEvent(hwnd, message, key, 0));
- return true;
-}
-#endif
-
-void FindBarWin::OnFinalMessage() {
+void FindBarHost::OnFinalMessage() {
// TODO(beng): Destroy the RootView before destroying the Focus Manager will
// allow us to remove this method.
@@ -345,14 +145,14 @@
focus_tracker_.reset(NULL);
};
-bool FindBarWin::IsVisible() {
+bool FindBarHost::IsVisible() {
return host_->IsVisible();
}
////////////////////////////////////////////////////////////////////////////////
-// FindBarWin, views::FocusChangeListener implementation:
+// FindBarHost, views::FocusChangeListener implementation:
-void FindBarWin::FocusWillChange(views::View* focused_before,
+void FindBarHost::FocusWillChange(views::View* focused_before,
views::View* focused_now) {
// First we need to determine if one or both of the views passed in are child
// views of our view.
@@ -378,7 +178,7 @@
////////////////////////////////////////////////////////////////////////////////
// FindBarWin, views::AcceleratorTarget implementation:
-bool FindBarWin::AcceleratorPressed(const views::Accelerator& accelerator) {
+bool FindBarHost::AcceleratorPressed(const views::Accelerator& accelerator) {
#if defined(OS_WIN)
DCHECK(accelerator.GetKeyCode() == VK_ESCAPE); // We only expect Escape key.
#endif
@@ -391,9 +191,9 @@
}
////////////////////////////////////////////////////////////////////////////////
-// FindBarWin, AnimationDelegate implementation:
+// FindBarHost, AnimationDelegate implementation:
-void FindBarWin::AnimationProgressed(const Animation* animation) {
+void FindBarHost::AnimationProgressed(const Animation* animation) {
// First, we calculate how many pixels to slide the window.
gfx::Size pref_size = view_->GetPreferredSize();
find_dialog_animation_offset_ =
@@ -411,7 +211,7 @@
view_->SchedulePaint();
}
-void FindBarWin::AnimationEnded(const Animation* animation) {
+void FindBarHost::AnimationEnded(const Animation* animation) {
// Place the find bar in its fully opened state.
find_dialog_animation_offset_ = 0;
@@ -423,7 +223,7 @@
}
}
-void FindBarWin::GetThemePosition(gfx::Rect* bounds) {
+void FindBarHost::GetThemePosition(gfx::Rect* bounds) {
*bounds = GetDialogPosition(gfx::Rect());
gfx::Rect toolbar_bounds = browser_view_->GetToolbarBounds();
gfx::Rect tab_strip_bounds = browser_view_->GetTabStripBounds();
@@ -433,7 +233,7 @@
////////////////////////////////////////////////////////////////////////////////
// FindBarTesting implementation:
-bool FindBarWin::GetFindBarWindowInfo(gfx::Point* position,
+bool FindBarHost::GetFindBarWindowInfo(gfx::Point* position,
bool* fully_visible) {
if (!find_bar_controller_ ||
#if defined(OS_WIN)
@@ -454,14 +254,14 @@
return true;
}
-void FindBarWin::GetDialogBounds(gfx::Rect* bounds) {
+void FindBarHost::GetDialogBounds(gfx::Rect* bounds) {
DCHECK(bounds);
// The BrowserView does Layout for the components that we care about
// positioning relative to, so we ask it to tell us where we should go.
*bounds = browser_view_->GetFindBarBoundingBox();
}
-gfx::Rect FindBarWin::GetDialogPosition(gfx::Rect avoid_overlapping_rect) {
+gfx::Rect FindBarHost::GetDialogPosition(gfx::Rect avoid_overlapping_rect) {
// Find the area we have to work with (after accounting for scrollbars, etc).
gfx::Rect dialog_bounds;
GetDialogBounds(&dialog_bounds);
@@ -486,16 +286,7 @@
// For comparison (with the Intersects function below) we need to account
// for the fact that we draw the Find dialog relative to the window,
// whereas the selection rect is relative to the page.
-#if defined(OS_WIN)
- RECT frame_rect = {0}, webcontents_rect = {0};
- ::GetWindowRect(host_->GetParent(), &frame_rect);
- ::GetWindowRect(
- find_bar_controller_->tab_contents()->view()->GetNativeView(),
- &webcontents_rect);
- avoid_overlapping_rect.Offset(0, webcontents_rect.top - frame_rect.top);
-#else
- NOTIMPLEMENTED();
-#endif
+ GetDialogPositionNative(&avoid_overlapping_rect);
}
gfx::Rect new_pos = FindBarController::GetLocationForFindbarView(
@@ -509,7 +300,7 @@
return new_pos;
}
-void FindBarWin::SetDialogPosition(const gfx::Rect& new_pos, bool no_redraw) {
+void FindBarHost::SetDialogPosition(const gfx::Rect& new_pos, bool no_redraw) {
if (new_pos.IsEmpty())
return;
@@ -518,25 +309,10 @@
// of it it doesn't look like the window crumbles into the toolbar.
UpdateWindowEdges(new_pos);
-#if defined(OS_WIN)
- gfx::Rect window_rect;
- host_->GetBounds(&window_rect, true);
- DWORD swp_flags = SWP_NOOWNERZORDER;
- if (!window_rect.IsEmpty())
- swp_flags |= SWP_NOSIZE;
- if (no_redraw)
- swp_flags |= SWP_NOREDRAW;
- if (!host_->IsVisible())
- swp_flags |= SWP_SHOWWINDOW;
-
- ::SetWindowPos(host_->GetNativeView(), HWND_TOP, new_pos.x(), new_pos.y(),
- new_pos.width(), new_pos.height(), swp_flags);
-#else
- host_->SetBounds(new_pos);
-#endif
+ SetDialogPositionNative(new_pos, no_redraw);
}
-void FindBarWin::RestoreSavedFocus() {
+void FindBarHost::RestoreSavedFocus() {
if (focus_tracker_.get() == NULL) {
// TODO(brettw) Focus() should be on TabContentsView.
find_bar_controller_->tab_contents()->Focus();
@@ -545,33 +321,11 @@
}
}
-FindBarTesting* FindBarWin::GetFindBarTesting() {
+FindBarTesting* FindBarHost::GetFindBarTesting() {
return this;
}
-void FindBarWin::RegisterEscAccelerator() {
-#if defined(OS_WIN)
- DCHECK(!esc_accel_target_registered_);
- views::Accelerator escape(VK_ESCAPE, false, false, false);
- focus_manager_->RegisterAccelerator(escape, this);
- esc_accel_target_registered_ = true;
-#else
- NOTIMPLEMENTED();
-#endif
-}
-
-void FindBarWin::UnregisterEscAccelerator() {
-#if defined(OS_WIN)
- DCHECK(esc_accel_target_registered_);
- views::Accelerator escape(VK_ESCAPE, false, false, false);
- focus_manager_->UnregisterAccelerator(escape, this);
- esc_accel_target_registered_ = false;
-#else
- NOTIMPLEMENTED();
-#endif
-}
-
-void FindBarWin::UpdateUIForFindResult(const FindNotificationDetails& result,
+void FindBarHost::UpdateUIForFindResult(const FindNotificationDetails& result,
const string16& find_text) {
view_->UpdateForResult(result, find_text);
@@ -585,10 +339,49 @@
focus_tracker_.reset(NULL);
}
-void FindBarWin::AudibleAlert() {
-#if defined(OS_WIN)
- MessageBeep(MB_OK);
-#else
- NOTIMPLEMENTED();
-#endif
+void FindBarHost::RegisterEscAccelerator() {
+ DCHECK(!esc_accel_target_registered_);
+ views::Accelerator escape(base::VKEY_ESCAPE, false, false, false);
+ focus_manager_->RegisterAccelerator(escape, this);
+ esc_accel_target_registered_ = true;
}
+
+void FindBarHost::UnregisterEscAccelerator() {
+ DCHECK(esc_accel_target_registered_);
+ views::Accelerator escape(base::VKEY_ESCAPE, false, false, false);
+ focus_manager_->UnregisterAccelerator(escape, this);
+ esc_accel_target_registered_ = false;
+}
+
+bool FindBarHost::MaybeForwardKeystrokeToWebpage(
+ const views::Textfield::Keystroke& key_stroke) {
+ switch (key_stroke.GetKeyboardCode()) {
+ case base::VKEY_DOWN:
+ case base::VKEY_UP:
+ case base::VKEY_PRIOR:
+ case base::VKEY_NEXT:
+ break;
+ case base::VKEY_HOME:
+ case base::VKEY_END:
+ if (key_stroke.IsControlHeld())
+ break;
+ // Fall through.
+ default:
+ return false;
+ }
+
+ TabContents* contents = find_bar_controller_->tab_contents();
+ if (!contents)
+ return false;
+
+ RenderViewHost* render_view_host = contents->render_view_host();
+
+ // Make sure we don't have a text field element interfering with keyboard
+ // input. Otherwise Up and Down arrow key strokes get eaten. "Nom Nom Nom".
+ render_view_host->ClearFocusedNode();
+ NativeWebKeyboardEvent event = GetKeyboardEvent(contents, key_stroke);
+ render_view_host->ForwardKeyboardEvent(event);
+ return true;
+}
+
+

Powered by Google App Engine
This is Rietveld 408576698