diff options
author | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-08 20:32:08 +0000 |
---|---|---|
committer | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-08 20:32:08 +0000 |
commit | 8008f0a0e3242e638fc7209ace9311981de11d3b (patch) | |
tree | af7cd79b65d74aceb64c29249ee59eb897618741 | |
parent | 7a2d96a5c7776d4811eb6a8a38e3d24fcf688e05 (diff) | |
download | chromium_src-8008f0a0e3242e638fc7209ace9311981de11d3b.zip chromium_src-8008f0a0e3242e638fc7209ace9311981de11d3b.tar.gz chromium_src-8008f0a0e3242e638fc7209ace9311981de11d3b.tar.bz2 |
Refactor omnibox creation and max edit width determination, moving them from #ifdef'ed code in LocationBarView behind OmniboxView.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/8457006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@109088 0039d316-1c4b-4281-b951-d872f2087c98
10 files changed, 188 insertions, 130 deletions
diff --git a/chrome/browser/autocomplete/autocomplete_edit_unittest.cc b/chrome/browser/autocomplete/autocomplete_edit_unittest.cc index 07531da..a9a75fb 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_unittest.cc +++ b/chrome/browser/autocomplete/autocomplete_edit_unittest.cc @@ -16,54 +16,62 @@ class TestingOmniboxView : public OmniboxView { public: TestingOmniboxView() {} - virtual AutocompleteEditModel* model() { return NULL; } - virtual const AutocompleteEditModel* model() const { return NULL; } - virtual void SaveStateToTab(TabContents* tab) {} - virtual void Update(const TabContents* tab_for_state_restoring) {} + virtual AutocompleteEditModel* model() OVERRIDE { return NULL; } + virtual const AutocompleteEditModel* model() const OVERRIDE { return NULL; } + virtual void SaveStateToTab(TabContents* tab) OVERRIDE {} + virtual void Update(const TabContents* tab_for_state_restoring) OVERRIDE {} virtual void OpenMatch(const AutocompleteMatch& match, WindowOpenDisposition disposition, const GURL& alternate_nav_url, size_t selected_line, - const string16& keyword) {} - virtual string16 GetText() const { return string16(); } - virtual bool IsEditingOrEmpty() const { return true; } - virtual int GetIcon() const { return 0; } - virtual void SetUserText(const string16& text) {} + const string16& keyword) OVERRIDE {} + virtual string16 GetText() const OVERRIDE { return string16(); } + virtual bool IsEditingOrEmpty() const OVERRIDE { return true; } + virtual int GetIcon() const OVERRIDE { return 0; } + virtual void SetUserText(const string16& text) OVERRIDE {} virtual void SetUserText(const string16& text, const string16& display_text, - bool update_popup) {} + bool update_popup) OVERRIDE {} virtual void SetWindowTextAndCaretPos(const string16& text, - size_t caret_pos) {} - virtual void SetForcedQuery() {} - virtual bool IsSelectAll() { return false; } - virtual bool DeleteAtEndPressed() { return false; } - virtual void GetSelectionBounds(size_t* start, size_t* end) const {} - virtual void SelectAll(bool reversed) {} - virtual void RevertAll() {} - virtual void UpdatePopup() {} - virtual void ClosePopup() {} - virtual void SetFocus() {} - virtual void OnTemporaryTextMaybeChanged(const string16& display_text, - bool save_original_selection) {} + size_t caret_pos) OVERRIDE {} + virtual void SetForcedQuery() OVERRIDE {} + virtual bool IsSelectAll() OVERRIDE { return false; } + virtual bool DeleteAtEndPressed() OVERRIDE { return false; } + virtual void GetSelectionBounds(size_t* start, size_t* end) const OVERRIDE {} + virtual void SelectAll(bool reversed) OVERRIDE {} + virtual void RevertAll() OVERRIDE {} + virtual void UpdatePopup() OVERRIDE {} + virtual void ClosePopup() OVERRIDE {} + virtual void SetFocus() OVERRIDE {} + virtual void OnTemporaryTextMaybeChanged( + const string16& display_text, + bool save_original_selection) OVERRIDE {} virtual bool OnInlineAutocompleteTextMaybeChanged( - const string16& display_text, size_t user_text_length) { + const string16& display_text, size_t user_text_length) OVERRIDE { return false; } - virtual void OnRevertTemporaryText() {} - virtual void OnBeforePossibleChange() {} - virtual bool OnAfterPossibleChange() { return false; } - virtual gfx::NativeView GetNativeView() const { return NULL; } - virtual gfx::NativeView GetRelativeWindowForPopup() const { return NULL; } - virtual CommandUpdater* GetCommandUpdater() { return NULL; } + virtual void OnRevertTemporaryText() OVERRIDE {} + virtual void OnBeforePossibleChange() OVERRIDE {} + virtual bool OnAfterPossibleChange() OVERRIDE { return false; } + virtual gfx::NativeView GetNativeView() const OVERRIDE { return NULL; } + virtual gfx::NativeView GetRelativeWindowForPopup() const OVERRIDE { + return NULL; + } + virtual CommandUpdater* GetCommandUpdater() OVERRIDE { return NULL; } virtual void SetInstantSuggestion(const string16& input, - bool animate_to_complete) {} - virtual string16 GetInstantSuggestion() const { return string16(); } - virtual int TextWidth() const { return 0; } - virtual bool IsImeComposing() const { return false; } + bool animate_to_complete) OVERRIDE {} + virtual string16 GetInstantSuggestion() const OVERRIDE { return string16(); } + virtual int TextWidth() const OVERRIDE { return 0; } + virtual bool IsImeComposing() const OVERRIDE { return false; } #if defined(TOOLKIT_VIEWS) - virtual views::View* AddToView(views::View* parent) { return NULL; } - virtual int OnPerformDrop(const views::DropTargetEvent& event) { return 0; } + virtual int GetMaxEditWidth(int entry_width) const OVERRIDE { + return entry_width; + } + virtual views::View* AddToView(views::View* parent) OVERRIDE { return NULL; } + virtual int OnPerformDrop(const views::DropTargetEvent& event) OVERRIDE { + return 0; + } #endif private: diff --git a/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.cc b/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.cc index 9e62c5c..6aa2b33 100644 --- a/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.cc +++ b/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.cc @@ -856,6 +856,10 @@ bool OmniboxViewGtk::IsImeComposing() const { } #if defined(TOOLKIT_VIEWS) +int OmniboxViewGtk::GetMaxEditWidth(int entry_width) const OVERRIDE { + return entry_width; +} + views::View* OmniboxViewGtk::AddToView(views::View* parent) { views::NativeViewHost* host = new views::NativeViewHost; parent->AddChildView(host); @@ -884,42 +888,7 @@ int OmniboxViewGtk::OnPerformDrop( return ui::DragDropTypes::DRAG_NONE; } - -// static -OmniboxView* OmniboxViewGtk::Create(AutocompleteEditController* controller, - ToolbarModel* toolbar_model, - Profile* profile, - CommandUpdater* command_updater, - bool popup_window_mode, - views::View* location_bar) { - if (views::Widget::IsPureViews()) { - OmniboxViewViews* omnibox_view = new OmniboxViewViews(controller, - toolbar_model, - profile, - command_updater, - popup_window_mode, - location_bar); - omnibox_view->Init(); - return omnibox_view; - } - - OmniboxViewGtk* omnibox_view = new OmniboxViewGtk(controller, - toolbar_model, - profile, - command_updater, - popup_window_mode, - location_bar); - omnibox_view->Init(); - - // Make all the children of the widget visible. NOTE: this won't display - // anything, it just toggles the visible flag. - gtk_widget_show_all(omnibox_view->GetNativeView()); - // Hide the widget. NativeViewHostGtk will make it visible again as necessary. - gtk_widget_hide(omnibox_view->GetNativeView()); - - return omnibox_view; -} -#endif +#endif // defined(TOOLKIT_VIEWS) void OmniboxViewGtk::Observe(int type, const content::NotificationSource& source, @@ -2394,3 +2363,41 @@ void OmniboxViewGtk::AdjustVerticalAlignmentOfInstantView() { pango_layout_iter_free(iter); g_object_set(instant_anchor_tag_, "rise", baseline - height, NULL); } + +#if defined(TOOLKIT_VIEWS) +// static +OmniboxView* OmniboxView::CreateOmniboxView( + AutocompleteEditController* controller, + ToolbarModel* toolbar_model, + Profile* profile, + CommandUpdater* command_updater, + bool popup_window_mode, + LocationBarView* location_bar) { + if (views::Widget::IsPureViews()) { + OmniboxViewViews* omnibox_view = new OmniboxViewViews(controller, + toolbar_model, + profile, + command_updater, + popup_window_mode, + location_bar); + omnibox_view->Init(); + return omnibox_view; + } + + OmniboxViewGtk* omnibox_view = new OmniboxViewGtk(controller, + toolbar_model, + profile, + command_updater, + popup_window_mode, + location_bar); + omnibox_view->Init(); + + // Make all the children of the widget visible. NOTE: this won't display + // anything, it just toggles the visible flag. + gtk_widget_show_all(omnibox_view->GetNativeView()); + // Hide the widget. NativeViewHostGtk will make it visible again as necessary. + gtk_widget_hide(omnibox_view->GetNativeView()); + + return omnibox_view; +} +#endif diff --git a/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.h b/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.h index fc9ca4a..a7cdc88 100644 --- a/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.h +++ b/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.h @@ -147,19 +147,9 @@ class OmniboxViewGtk : public OmniboxView, virtual bool IsImeComposing() const OVERRIDE; #if defined(TOOLKIT_VIEWS) + virtual int GetMaxEditWidth(int entry_width) const OVERRIDE; virtual views::View* AddToView(views::View* parent) OVERRIDE; virtual int OnPerformDrop(const views::DropTargetEvent& event) OVERRIDE; - - // A factory method to create an OmniboxView instance initialized for - // linux_views. This currently returns an instance of OmniboxViewGtk only, - // but OmniboxViewViews will be added as an option when TextfieldViews is - // enabled. - static OmniboxView* Create(AutocompleteEditController* controller, - ToolbarModel* toolbar_model, - Profile* profile, - CommandUpdater* command_updater, - bool popup_window_mode, - views::View* location_bar); #endif // Overridden from content::NotificationObserver: diff --git a/chrome/browser/ui/omnibox/omnibox_view.h b/chrome/browser/ui/omnibox/omnibox_view.h index 5563e9c..5d2f533 100644 --- a/chrome/browser/ui/omnibox/omnibox_view.h +++ b/chrome/browser/ui/omnibox/omnibox_view.h @@ -28,14 +28,32 @@ class GURL; class TabContents; #if defined(TOOLKIT_VIEWS) + +// TODO(beng): Move all views-related code to a views-specific sub-interface. + +class AutocompleteEditController; +class LocationBarView; +class Profile; +class ToolbarModel; + namespace views { class DropTargetEvent; class View; } // namespace views + #endif class OmniboxView { public: +#if defined(TOOLKIT_VIEWS) + static OmniboxView* CreateOmniboxView(AutocompleteEditController* controller, + ToolbarModel* toolbar_model, + Profile* profile, + CommandUpdater* command_updater, + bool popup_window_mode, + LocationBarView* location_bar); +#endif + // Used by the automation system for getting at the model from the view. virtual AutocompleteEditModel* model() = 0; virtual const AutocompleteEditModel* model() const = 0; @@ -183,6 +201,8 @@ class OmniboxView { virtual bool IsImeComposing() const = 0; #if defined(TOOLKIT_VIEWS) + virtual int GetMaxEditWidth(int entry_width) const = 0; + // Adds the autocomplete edit view to view hierarchy and // returns the views::View of the edit view. virtual views::View* AddToView(views::View* parent) = 0; diff --git a/chrome/browser/ui/views/location_bar/location_bar_view.cc b/chrome/browser/ui/views/location_bar/location_bar_view.cc index 30814ed..6a13820 100644 --- a/chrome/browser/ui/views/location_bar/location_bar_view.cc +++ b/chrome/browser/ui/views/location_bar/location_bar_view.cc @@ -188,26 +188,13 @@ void LocationBarView::Init() { // URL edit field. // View container for URL edit field. Profile* profile = browser_->profile(); -#if defined(USE_AURA) - OmniboxViewViews* omnibox_view = new OmniboxViewViews(this, model_, profile, - browser_->command_updater(), mode_ == POPUP, this); - omnibox_view->Init(); - location_entry_.reset(omnibox_view); -#elif defined(OS_WIN) - if (views::Widget::IsPureViews()) { - OmniboxViewViews* omnibox_view = new OmniboxViewViews(this, model_, profile, - browser_->command_updater(), mode_ == POPUP, this); - omnibox_view->Init(); - location_entry_.reset(omnibox_view); - } else { - location_entry_.reset(new OmniboxViewWin(font_, this, model_, this, - GetWidget()->GetNativeView(), browser_->command_updater(), - mode_ == POPUP, this)); - } -#else - location_entry_.reset(OmniboxViewGtk::Create(this, model_, profile, - browser_->command_updater(), mode_ == POPUP, this)); -#endif + location_entry_.reset(OmniboxView::CreateOmniboxView( + this, + model_, + profile, + browser_->command_updater(), + mode_ == POPUP, + this)); location_entry_view_ = location_entry_->AddToView(this); location_entry_view_->set_id(VIEW_ID_AUTOCOMPLETE); @@ -553,24 +540,7 @@ void LocationBarView::Layout() { entry_width -= (total_padding + ev_bubble_width); } -#if defined(OS_WIN) - int max_edit_width = entry_width; - if (views::Widget::IsPureViews()) { - NOTIMPLEMENTED(); - } else { -#if !defined(USE_AURA) - RECT formatting_rect; - GetOmniboxViewWin()->GetRect(&formatting_rect); - RECT edit_bounds; - GetOmniboxViewWin()->GetClientRect(&edit_bounds); - max_edit_width = entry_width - formatting_rect.left - - (edit_bounds.right - formatting_rect.right); -#endif - } -#else - int max_edit_width = entry_width; -#endif - + int max_edit_width = location_entry_->GetMaxEditWidth(entry_width); if (max_edit_width < 0) return; const int available_width = AvailableWidth(max_edit_width); diff --git a/chrome/browser/ui/views/location_bar/location_bar_view.h b/chrome/browser/ui/views/location_bar/location_bar_view.h index 4524234..6719871 100644 --- a/chrome/browser/ui/views/location_bar/location_bar_view.h +++ b/chrome/browser/ui/views/location_bar/location_bar_view.h @@ -192,6 +192,8 @@ class LocationBarView : public LocationBar, // in the toolbar in full keyboard accessibility mode. virtual void SelectAll(); + const gfx::Font& font() const { return font_; } + #if defined(OS_WIN) && !defined(USE_AURA) // Event Handlers virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE; diff --git a/chrome/browser/ui/views/omnibox/omnibox_view_views.cc b/chrome/browser/ui/views/omnibox/omnibox_view_views.cc index 56a72dc..f621264 100644 --- a/chrome/browser/ui/views/omnibox/omnibox_view_views.cc +++ b/chrome/browser/ui/views/omnibox/omnibox_view_views.cc @@ -579,6 +579,10 @@ bool OmniboxViewViews::IsImeComposing() const { return false; } +int OmniboxViewViews::GetMaxEditWidth(int entry_width) const { + return entry_width; +} + views::View* OmniboxViewViews::AddToView(views::View* parent) { parent->AddChildView(this); AddChildView(textfield_); @@ -714,3 +718,23 @@ AutocompletePopupView* OmniboxViewViews::CreatePopupView( return new AutocompleteContentsView(gfx::Font(), this, model_.get(), location_bar); } + +#if defined(USE_AURA) +// static +OmniboxView* OmniboxView::CreateOmniboxView( + AutocompleteEditController* controller, + ToolbarModel* toolbar_model, + Profile* profile, + CommandUpdater* command_updater, + bool popup_window_mode, + LocationBarView* location_bar) { + OmniboxViewViews* omnibox_view = new OmniboxViewViews(controller, + toolbar_model, + profile, + command_updater, + popup_window_mode, + location_bar); + omnibox_view->Init(); + return omnibox_view; +} +#endif diff --git a/chrome/browser/ui/views/omnibox/omnibox_view_views.h b/chrome/browser/ui/views/omnibox/omnibox_view_views.h index 0be13c1..ec9f3e5 100644 --- a/chrome/browser/ui/views/omnibox/omnibox_view_views.h +++ b/chrome/browser/ui/views/omnibox/omnibox_view_views.h @@ -121,6 +121,7 @@ class OmniboxViewViews : public views::View, virtual string16 GetInstantSuggestion() const OVERRIDE; virtual int TextWidth() const OVERRIDE; virtual bool IsImeComposing() const OVERRIDE; + virtual int GetMaxEditWidth(int entry_width) const OVERRIDE; virtual views::View* AddToView(views::View* parent) OVERRIDE; virtual int OnPerformDrop(const views::DropTargetEvent& event) OVERRIDE; diff --git a/chrome/browser/ui/views/omnibox/omnibox_view_win.cc b/chrome/browser/ui/views/omnibox/omnibox_view_win.cc index af656c3..1c9b1dd 100644 --- a/chrome/browser/ui/views/omnibox/omnibox_view_win.cc +++ b/chrome/browser/ui/views/omnibox/omnibox_view_win.cc @@ -29,6 +29,7 @@ #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/views/location_bar/location_bar_view.h" +#include "chrome/browser/ui/views/omnibox/omnibox_view_views.h" #include "chrome/common/chrome_notification_types.h" #include "content/browser/tab_contents/tab_contents.h" #include "content/browser/user_metrics.h" @@ -423,17 +424,16 @@ const int kTwipsPerInch = 1440; } // namespace -OmniboxViewWin::OmniboxViewWin(const gfx::Font& font, - AutocompleteEditController* controller, +OmniboxViewWin::OmniboxViewWin(AutocompleteEditController* controller, ToolbarModel* toolbar_model, LocationBarView* parent_view, - HWND hwnd, CommandUpdater* command_updater, bool popup_window_mode, views::View* location_bar) : model_(new AutocompleteEditModel(this, controller, parent_view->browser()->profile())), - popup_view_(new AutocompletePopupContentsView(font, this, model_.get(), + popup_view_(new AutocompletePopupContentsView(parent_view->font(), this, + model_.get(), location_bar)), controller_(controller), parent_view_(parent_view), @@ -447,7 +447,7 @@ OmniboxViewWin::OmniboxViewWin(const gfx::Font& font, can_discard_mousemove_(false), ignore_ime_messages_(false), delete_at_end_pressed_(false), - font_(font), + font_(parent_view->font()), possible_drag_(false), in_drag_(false), initiated_drag_(false), @@ -465,7 +465,8 @@ OmniboxViewWin::OmniboxViewWin(const gfx::Font& font, g_paint_patcher.Pointer()->RefPatch(); - Create(hwnd, 0, 0, 0, l10n_util::GetExtendedStyles()); + Create(location_bar->GetWidget()->GetNativeView(), 0, 0, 0, + l10n_util::GetExtendedStyles()); SetReadOnly(popup_window_mode_); SetFont(font_.GetNativeFont()); @@ -969,6 +970,15 @@ bool OmniboxViewWin::IsImeComposing() const { return ime_composing; } +int OmniboxViewWin::GetMaxEditWidth(int entry_width) const { + RECT formatting_rect; + GetRect(&formatting_rect); + RECT edit_bounds; + GetClientRect(&edit_bounds); + return entry_width - formatting_rect.left - + (edit_bounds.right - formatting_rect.right); +} + views::View* OmniboxViewWin::AddToView(views::View* parent) { native_view_host_ = new OmniboxViewWrapper(this); parent->AddChildView(native_view_host_); @@ -2659,3 +2669,31 @@ bool OmniboxViewWin::IsCaretAtEnd() const { GetSelection(sel); return sel.cpMin == sel.cpMax && sel.cpMin == length; } + +#if !defined(USE_AURA) +// static +OmniboxView* OmniboxView::CreateOmniboxView( + AutocompleteEditController* controller, + ToolbarModel* toolbar_model, + Profile* profile, + CommandUpdater* command_updater, + bool popup_window_mode, + LocationBarView* location_bar) { + if (views::Widget::IsPureViews()) { + OmniboxViewViews* omnibox_view = new OmniboxViewViews(controller, + toolbar_model, + profile, + command_updater, + popup_window_mode, + location_bar); + omnibox_view->Init(); + return omnibox_view; + } + return new OmniboxViewWin(controller, + toolbar_model, + location_bar, + command_updater, + popup_window_mode, + location_bar); +} +#endif diff --git a/chrome/browser/ui/views/omnibox/omnibox_view_win.h b/chrome/browser/ui/views/omnibox/omnibox_view_win.h index cf0e302..53e5df5 100644 --- a/chrome/browser/ui/views/omnibox/omnibox_view_win.h +++ b/chrome/browser/ui/views/omnibox/omnibox_view_win.h @@ -60,11 +60,9 @@ class OmniboxViewWin DECLARE_WND_CLASS(L"Chrome_OmniboxView"); - OmniboxViewWin(const gfx::Font& font, - AutocompleteEditController* controller, + OmniboxViewWin(AutocompleteEditController* controller, ToolbarModel* toolbar_model, LocationBarView* parent_view, - HWND hwnd, CommandUpdater* command_updater, bool popup_window_mode, views::View* location_bar); @@ -143,7 +141,7 @@ class OmniboxViewWin virtual int TextWidth() const OVERRIDE; virtual string16 GetInstantSuggestion() const OVERRIDE; virtual bool IsImeComposing() const OVERRIDE; - + virtual int GetMaxEditWidth(int entry_width) const OVERRIDE; virtual views::View* AddToView(views::View* parent) OVERRIDE; virtual int OnPerformDrop(const views::DropTargetEvent& event) OVERRIDE; |