diff options
author | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-17 21:15:30 +0000 |
---|---|---|
committer | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-17 21:15:30 +0000 |
commit | a81542053c0652efa66cc13634946582732273b4 (patch) | |
tree | 29465ad5db1db724efa034cd15a3dd567dee55b5 /chrome/browser/views | |
parent | 41dc1a783dff2874c5fe8fb0a3abc72a1f7897ee (diff) | |
download | chromium_src-a81542053c0652efa66cc13634946582732273b4.zip chromium_src-a81542053c0652efa66cc13634946582732273b4.tar.gz chromium_src-a81542053c0652efa66cc13634946582732273b4.tar.bz2 |
Move the implementation of AutocompletePopupView from AutocompletePopupWin to AutocompletePopupContentsView, a more central location.
Review URL: http://codereview.chromium.org/77012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@13967 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/views')
5 files changed, 158 insertions, 161 deletions
diff --git a/chrome/browser/views/autocomplete/autocomplete_popup_contents_view.cc b/chrome/browser/views/autocomplete/autocomplete_popup_contents_view.cc index cf6e57f..15866a3 100644 --- a/chrome/browser/views/autocomplete/autocomplete_popup_contents_view.cc +++ b/chrome/browser/views/autocomplete/autocomplete_popup_contents_view.cc @@ -6,6 +6,7 @@ #include <dwmapi.h> +#include "chrome/browser/autocomplete/autocomplete_edit_view_win.h" #include "chrome/browser/autocomplete/autocomplete_popup_model.h" #include "chrome/browser/views/autocomplete/autocomplete_popup_win.h" #include "chrome/common/gfx/chrome_canvas.h" @@ -262,9 +263,16 @@ void PopupBorder::InitClass() { // AutocompletePopupContentsView, public: AutocompletePopupContentsView::AutocompletePopupContentsView( - AutocompletePopupWin* popup) - : popup_(popup) { - set_border(new PopupBorder); + const ChromeFont& font, + AutocompleteEditViewWin* edit_view, + AutocompleteEditModel* edit_model, + Profile* profile, + AutocompletePopupPositioner* popup_positioner) + : popup_(new AutocompletePopupWin(this)), + model_(new AutocompletePopupModel(this, edit_model, profile)), + edit_view_(edit_view), + popup_positioner_(popup_positioner) { + set_border(new PopupBorder); } void AutocompletePopupContentsView::SetAutocompleteResult( @@ -278,32 +286,89 @@ void AutocompletePopupContentsView::SetAutocompleteResult( Layout(); } -void AutocompletePopupContentsView::InvalidateLine(int index) { - GetChildViewAt(index)->SchedulePaint(); +gfx::Rect AutocompletePopupContentsView::GetPopupBounds() const { + gfx::Insets insets; + border()->GetInsets(&insets); + gfx::Rect contents_bounds = popup_positioner_->GetPopupBounds(); + contents_bounds.set_height(100); // TODO(beng): size to contents (once we + // have contents!) + contents_bounds.Inset(-insets.left(), -insets.top(), -insets.right(), + -insets.bottom()); + return contents_bounds; } +//////////////////////////////////////////////////////////////////////////////// +// AutocompletePopupContentsView, AutocompletePopupView overrides: + +bool AutocompletePopupContentsView::IsOpen() const { + return popup_->IsWindow() && popup_->IsVisible(); +} + +void AutocompletePopupContentsView::InvalidateLine(size_t line) { + GetChildViewAt(static_cast<int>(line))->SchedulePaint(); +} + +void AutocompletePopupContentsView::UpdatePopupAppearance() { + const AutocompleteResult& result = model_->result(); + if (result.empty()) { + // No matches, close any existing popup. + if (popup_->IsWindow()) + popup_->Hide(); + return; + } + + if (popup_->IsWindow()) + popup_->Show(); + else + popup_->Init(edit_view_, this); + SetAutocompleteResult(result); +} + +void AutocompletePopupContentsView::OnHoverEnabledOrDisabled(bool disabled) { + // TODO(beng): remove this from the interface. +} + +void AutocompletePopupContentsView::PaintUpdatesNow() { + // TODO(beng): remove this from the interface. +} + +AutocompletePopupModel* AutocompletePopupContentsView::GetModel() { + return model_.get(); +} + +//////////////////////////////////////////////////////////////////////////////// +// AutocompletePopupContentsView, AutocompleteResultViewModel implementation: + bool AutocompletePopupContentsView::IsSelectedIndex(size_t index) { - return index == popup_->GetModel()->selected_line(); + return index == model_->selected_line(); } AutocompleteMatch::Type AutocompletePopupContentsView::GetResultTypeAtIndex( size_t index) { - return popup_->GetModel()->result().match_at(index).type; + return model_->result().match_at(index).type; } void AutocompletePopupContentsView::OpenIndex( size_t index, WindowOpenDisposition disposition) { - popup_->OpenIndex(index, disposition); + const AutocompleteMatch& match = model_->result().match_at(index); + // OpenURL() may close the popup, which will clear the result set and, by + // extension, |match| and its contents. So copy the relevant strings out to + // make sure they stay alive until the call completes. + const GURL url(match.destination_url); + std::wstring keyword; + const bool is_keyword_hint = model_->GetKeywordForMatch(match, &keyword); + edit_view_->OpenURL(url, disposition, match.transition, GURL(), index, + is_keyword_hint ? std::wstring() : keyword); } void AutocompletePopupContentsView::SetHoveredLine(size_t index) { - popup_->SetHoveredLine(index); + model_->SetHoveredLine(index); } void AutocompletePopupContentsView::SetSelectedLine(size_t index, bool revert_to_default) { - popup_->SetSelectedLine(index, revert_to_default); + model_->SetSelectedLine(index, revert_to_default); } //////////////////////////////////////////////////////////////////////////////// diff --git a/chrome/browser/views/autocomplete/autocomplete_popup_contents_view.h b/chrome/browser/views/autocomplete/autocomplete_popup_contents_view.h index 33b04d5..30a4306 100644 --- a/chrome/browser/views/autocomplete/autocomplete_popup_contents_view.h +++ b/chrome/browser/views/autocomplete/autocomplete_popup_contents_view.h @@ -6,35 +6,71 @@ #define CHROME_BROWSER_VIEWS_AUTOCOMPLETE_AUTOCOMPLETE_POPUP_CONTENTS_VIEW_H_ #include "chrome/browser/autocomplete/autocomplete.h" +#include "chrome/browser/autocomplete/autocomplete_popup_model.h" +#include "chrome/browser/autocomplete/autocomplete_popup_view.h" #include "chrome/views/view.h" +class AutocompleteEditModel; +class AutocompleteEditViewWin; class AutocompletePopupWin; +class Profile; -// TODO(beng): documentation, finalize +// Interface to retrieve the position of the popup. +class AutocompletePopupPositioner { + public: + // Returns the bounds at which the popup should be shown, in screen + // coordinates. The height is ignored, since the popup is sized to its + // contents automatically. + virtual gfx::Rect GetPopupBounds() const = 0; +}; + +// An interface implemented by an object that provides data to populate +// individual result views. class AutocompleteResultViewModel { public: + // Returns true if the index is selected. virtual bool IsSelectedIndex(size_t index) = 0; + // Returns the type of match that the row corresponds to. virtual AutocompleteMatch::Type GetResultTypeAtIndex(size_t index) = 0; + // Called when the line at the specified index should be opened with the + // provided disposition. virtual void OpenIndex(size_t index, WindowOpenDisposition disposition) = 0; + // Called when the line at the specified index should be shown as hovered. virtual void SetHoveredLine(size_t index) = 0; + + // Called when the line at the specified index should be shown as selected. virtual void SetSelectedLine(size_t index, bool revert_to_default) = 0; }; -// TODO(beng): documentation +// A view representing the contents of the autocomplete popup. class AutocompletePopupContentsView : public views::View, - public AutocompleteResultViewModel { + public AutocompleteResultViewModel, + public AutocompletePopupView { public: - explicit AutocompletePopupContentsView(AutocompletePopupWin* popup); + AutocompletePopupContentsView(const ChromeFont& font, + AutocompleteEditViewWin* edit_view, + AutocompleteEditModel* edit_model, + Profile* profile, + AutocompletePopupPositioner* popup_positioner); virtual ~AutocompletePopupContentsView() {} // Update the presentation with the latest result. void SetAutocompleteResult(const AutocompleteResult& result); - // Schedule a repaint for the specified row. - void InvalidateLine(int index); + // Returns the bounds the popup should be shown at. This is the display bounds + // and includes offsets for the dropshadow which this view's border renders. + gfx::Rect GetPopupBounds() const; + + // Overridden from AutocompletePopupView: + virtual bool IsOpen() const; + virtual void InvalidateLine(size_t line); + virtual void UpdatePopupAppearance(); + virtual void OnHoverEnabledOrDisabled(bool disabled); + virtual void PaintUpdatesNow(); + virtual AutocompletePopupModel* GetModel(); // Overridden from AutocompleteResultViewModel: virtual bool IsSelectedIndex(size_t index); @@ -58,8 +94,18 @@ class AutocompletePopupContentsView : public views::View, // Makes the contents of the canvas slightly transparent. void MakeCanvasTransparent(ChromeCanvas* canvas); + // The popup that contains this view. AutocompletePopupWin* popup_; + // The provider of our result set. + scoped_ptr<AutocompletePopupModel> model_; + + // The edit view that invokes us. + AutocompleteEditViewWin* edit_view_; + + // An object that tells the popup how to position itself. + AutocompletePopupPositioner* popup_positioner_; + DISALLOW_COPY_AND_ASSIGN(AutocompletePopupContentsView); }; diff --git a/chrome/browser/views/autocomplete/autocomplete_popup_win.cc b/chrome/browser/views/autocomplete/autocomplete_popup_win.cc index 28ae0dc..baf9903 100644 --- a/chrome/browser/views/autocomplete/autocomplete_popup_win.cc +++ b/chrome/browser/views/autocomplete/autocomplete_popup_win.cc @@ -14,15 +14,8 @@ // AutocompletePopupWin, public: AutocompletePopupWin::AutocompletePopupWin( - const ChromeFont& font, - AutocompleteEditViewWin* edit_view, - AutocompleteEditModel* edit_model, - Profile* profile, - AutocompletePopupPositioner* popup_positioner) - : model_(new AutocompletePopupModel(this, edit_model, profile)), - edit_view_(edit_view), - popup_positioner_(popup_positioner), - contents_(new AutocompletePopupContentsView(this)) { + AutocompletePopupContentsView* contents) + : contents_(contents) { set_delete_on_destroy(false); set_window_style(WS_POPUP | WS_CLIPCHILDREN); set_window_ex_style(WS_EX_TOOLWINDOW | WS_EX_LAYERED); @@ -31,62 +24,30 @@ AutocompletePopupWin::AutocompletePopupWin( AutocompletePopupWin::~AutocompletePopupWin() { } -//////////////////////////////////////////////////////////////////////////////// -// AutocompletePopupWin, AutocompletePopupView overrides: - -bool AutocompletePopupWin::IsOpen() const { - return IsWindow() && IsVisible(); -} - -void AutocompletePopupWin::InvalidateLine(size_t line) { - contents_->InvalidateLine(static_cast<int>(line)); -} - -void AutocompletePopupWin::UpdatePopupAppearance() { - const AutocompleteResult& result = model_->result(); - if (result.empty()) { - // No matches, close any existing popup. - if (::IsWindow(GetNativeView())) - Hide(); - return; - } - - gfx::Rect popup_bounds = GetPopupBounds(); - if (!::IsWindow(GetNativeView())) { - // Create the popup - HWND parent_hwnd = edit_view_->parent_view()->GetWidget()->GetNativeView(); - Init(parent_hwnd, popup_bounds, false); - SetContentsView(contents_); - - // When an IME is attached to the rich-edit control, retrieve its window - // handle and show this popup window under the IME windows. - // Otherwise, show this popup window under top-most windows. - // TODO(hbono): http://b/1111369 if we exclude this popup window from the - // display area of IME windows, this workaround becomes unnecessary. - HWND ime_window = ImmGetDefaultIMEWnd(edit_view_->m_hWnd); - SetWindowPos(ime_window ? ime_window : HWND_NOTOPMOST, 0, 0, 0, 0, - SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_SHOWWINDOW); - } else { - // Move the popup - int flags = SWP_NOACTIVATE; - if (!IsVisible()) - flags |= SWP_SHOWWINDOW; - win_util::SetChildBounds(GetNativeView(), NULL, NULL, popup_bounds, 0, - flags); - } - contents_->SetAutocompleteResult(result); -} - -void AutocompletePopupWin::OnHoverEnabledOrDisabled(bool disabled) { - // TODO(beng): remove this from the interface. -} +void AutocompletePopupWin::Init(AutocompleteEditViewWin* edit_view, + views::View* contents) { + // Create the popup + WidgetWin::Init(edit_view->parent_view()->GetWidget()->GetNativeView(), + contents_->GetPopupBounds(), false); + SetContentsView(contents_); -void AutocompletePopupWin::PaintUpdatesNow() { - // TODO(beng): remove this from the interface. + // When an IME is attached to the rich-edit control, retrieve its window + // handle and show this popup window under the IME windows. + // Otherwise, show this popup window under top-most windows. + // TODO(hbono): http://b/1111369 if we exclude this popup window from the + // display area of IME windows, this workaround becomes unnecessary. + HWND ime_window = ImmGetDefaultIMEWnd(edit_view->m_hWnd); + SetWindowPos(ime_window ? ime_window : HWND_NOTOPMOST, 0, 0, 0, 0, + SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_SHOWWINDOW); } -AutocompletePopupModel* AutocompletePopupWin::GetModel() { - return model_.get(); +void AutocompletePopupWin::Show() { + // Move the popup + int flags = SWP_NOACTIVATE; + if (!IsVisible()) + flags |= SWP_SHOWWINDOW; + win_util::SetChildBounds(GetNativeView(), NULL, NULL, + contents_->GetPopupBounds(), 0, flags); } //////////////////////////////////////////////////////////////////////////////// @@ -96,38 +57,3 @@ LRESULT AutocompletePopupWin::OnMouseActivate(HWND window, UINT hit_test, UINT mouse_message) { return MA_NOACTIVATE; } - -//////////////////////////////////////////////////////////////////////////////// -// AutocompletePopupWin, private: - -gfx::Rect AutocompletePopupWin::GetPopupBounds() const { - gfx::Insets insets; - contents_->border()->GetInsets(&insets); - gfx::Rect contents_bounds = popup_positioner_->GetPopupBounds(); - contents_bounds.set_height(100); // TODO(beng): size to contents (once we have - // contents!) - contents_bounds.Inset(-insets.left(), -insets.top(), -insets.right(), - -insets.bottom()); - return contents_bounds; -} - -void AutocompletePopupWin::OpenIndex(int index, - WindowOpenDisposition disposition) { - const AutocompleteMatch& match = model_->result().match_at(index); - // OpenURL() may close the popup, which will clear the result set and, by - // extension, |match| and its contents. So copy the relevant strings out to - // make sure they stay alive until the call completes. - const GURL url(match.destination_url); - std::wstring keyword; - const bool is_keyword_hint = model_->GetKeywordForMatch(match, &keyword); - edit_view_->OpenURL(url, disposition, match.transition, GURL(), index, - is_keyword_hint ? std::wstring() : keyword); -} - -void AutocompletePopupWin::SetHoveredLine(int index) { - model_->SetHoveredLine(index); -} - -void AutocompletePopupWin::SetSelectedLine(int index, bool revert_to_default) { - model_->SetSelectedLine(index, revert_to_default); -} diff --git a/chrome/browser/views/autocomplete/autocomplete_popup_win.h b/chrome/browser/views/autocomplete/autocomplete_popup_win.h index 780eb84..91d8d88 100644 --- a/chrome/browser/views/autocomplete/autocomplete_popup_win.h +++ b/chrome/browser/views/autocomplete/autocomplete_popup_win.h @@ -5,48 +5,22 @@ #ifndef CHROME_BROWSER_VIEWS_AUTOCOMPLETE_AUTOCOMPLETE_POPUP_WIN_H_ #define CHROME_BROWSER_VIEWS_AUTOCOMPLETE_AUTOCOMPLETE_POPUP_WIN_H_ -#include "chrome/browser/autocomplete/autocomplete_popup_view.h" #include "chrome/views/widget/widget_win.h" -class AutocompleteEditModel; class AutocompleteEditViewWin; class AutocompletePopupContentsView; -class Profile; -// Interface to retrieve the position of the popup. -class AutocompletePopupPositioner { +class AutocompletePopupWin : public views::WidgetWin { public: - // Returns the bounds at which the popup should be shown, in screen - // coordinates. The height is ignored, since the popup is sized to its - // contents automatically. - virtual gfx::Rect GetPopupBounds() const = 0; -}; - -class AutocompletePopupWin : public views::WidgetWin, - public AutocompletePopupView { - public: - AutocompletePopupWin(const ChromeFont& font, - AutocompleteEditViewWin* edit_view, - AutocompleteEditModel* edit_model, - Profile* profile, - AutocompletePopupPositioner* popup_positioner); + explicit AutocompletePopupWin(AutocompletePopupContentsView* contents); virtual ~AutocompletePopupWin(); - // Performs the action associated with the result at the specified index. - // TODO(beng): get rid of this. - void OpenIndex(int index, WindowOpenDisposition disposition); - - // TODO(beng): get rid of these. - void SetHoveredLine(int index); - void SetSelectedLine(int index, bool revert_to_default); + // Creates the popup and shows it for the first time. |edit_view| is the edit + // that created us. + void Init(AutocompleteEditViewWin* edit_view, views::View* contents); - // Overridden from AutocompletePopupView: - virtual bool IsOpen() const; - virtual void InvalidateLine(size_t line); - virtual void UpdatePopupAppearance(); - virtual void OnHoverEnabledOrDisabled(bool disabled); - virtual void PaintUpdatesNow(); - virtual AutocompletePopupModel* GetModel(); + // Shows the popup and moves it to the right position. + void Show(); protected: // Overridden from WidgetWin: @@ -55,20 +29,6 @@ class AutocompletePopupWin : public views::WidgetWin, UINT mouse_message); private: - // Returns the bounds of the popup window, in screen coordinates, adjusted for - // the amount of drop shadow the contents view may wish to add. - gfx::Rect GetPopupBounds() const; - - // The provider of our result set. - scoped_ptr<AutocompletePopupModel> model_; - - // The edit view that invokes us. - AutocompleteEditViewWin* edit_view_; - - // An object that tells the popup how to position itself. - AutocompletePopupPositioner* popup_positioner_; - - // The view that holds the result views. AutocompletePopupContentsView* contents_; DISALLOW_COPY_AND_ASSIGN(AutocompletePopupWin); diff --git a/chrome/browser/views/toolbar_view.h b/chrome/browser/views/toolbar_view.h index a384de6..9cb14fc 100644 --- a/chrome/browser/views/toolbar_view.h +++ b/chrome/browser/views/toolbar_view.h @@ -13,7 +13,7 @@ #include "chrome/browser/command_updater.h" #include "chrome/browser/encoding_menu_controller_delegate.h" #include "chrome/browser/user_data_manager.h" -#include "chrome/browser/views/autocomplete/autocomplete_popup_win.h" +#include "chrome/browser/views/autocomplete/autocomplete_popup_contents_view.h" #include "chrome/browser/views/dom_view.h" #include "chrome/browser/views/go_button.h" #include "chrome/browser/views/location_bar_view.h" |