Skip to content

Commit

Permalink
I'm working on autocomplete edit view which does not use native contr…
Browse files Browse the repository at this point in the history
…ols for Chromeos and Touch UI.

The goal of this refactoring is to make it easy to swap  AutocompleteEditView implementation between Gtk based impl and Views based one. This is 1st phrase and there will be more refacotring needed to implement AutocompleteEditViewViews.

- Added factory method AutocompleteEditViewGtk::Create,
  which hides initialization details for gtk based impl.
- Moved gtk specific accessibilty code to AutocompleteEditViewGtk.
- Added TOOLKIT_VIWES only methods to AutocompleteEditView
  to hide platform specific implementation. (AddToView, CommitInstatntSuggestion/SetInstantSuggestion)

BUG=none
TEST=none no functional change. all tests should pass.

Review URL: http://codereview.chromium.org/6036004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@70746 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
oshima@chromium.org committed Jan 7, 2011
1 parent 4e7c37d commit caa62f4
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 100 deletions.
9 changes: 9 additions & 0 deletions chrome/browser/autocomplete/autocomplete_edit_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ class TestingAutocompleteEditView : public AutocompleteEditView {
virtual gfx::NativeView GetNativeView() const { return 0; }
virtual CommandUpdater* GetCommandUpdater() { return NULL; }

#if defined(TOOLKIT_VIEWS)
virtual views::View* AddToView(views::View* parent) { return NULL; }
virtual int TextWidth() const { return 0; }
virtual bool CommitInstantSuggestion(
const std::wstring& typed_text,
const std::wstring& suggested_text) { return false;}
virtual void SetInstantSuggestion(const string16& input) {}
#endif

private:
DISALLOW_COPY_AND_ASSIGN(TestingAutocompleteEditView);
};
Expand Down
24 changes: 23 additions & 1 deletion chrome/browser/autocomplete/autocomplete_edit_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class CommandUpdater;
class GURL;
class TabContents;

#if defined(TOOLKIT_VIEWS)
namespace views {
class View;
} // namespace views
#endif

class AutocompleteEditView {
public:
// Used by the automation system for getting at the model from the view.
Expand Down Expand Up @@ -152,7 +158,23 @@ class AutocompleteEditView {
// Returns the command updater for this view.
virtual CommandUpdater* GetCommandUpdater() = 0;

protected:
#if defined(TOOLKIT_VIEWS)
// 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;

// Commits the suggested text.
virtual bool CommitInstantSuggestion(const std::wstring& typed_text,
const std::wstring& suggested_text) = 0;

// Shows the instant suggestion text.
virtual void SetInstantSuggestion(const string16& input) = 0;

// Returns the width in pixels needed to display the current text. The
// returned value includes margins.
virtual int TextWidth() const = 0;
#endif

virtual ~AutocompleteEditView() {}
};

Expand Down
140 changes: 100 additions & 40 deletions chrome/browser/autocomplete/autocomplete_edit_view_gtk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@
#include "ui/base/animation/multi_animation.h"

#if defined(TOOLKIT_VIEWS)
#include "chrome/browser/views/autocomplete/autocomplete_popup_contents_view.h"
#include "chrome/browser/views/location_bar/location_bar_view.h"
#include "chrome/browser/gtk/accessible_widget_helper_gtk.h"
#include "chrome/browser/ui/views/autocomplete/autocomplete_popup_contents_view.h"
#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
#else
#include "chrome/browser/autocomplete/autocomplete_popup_view_gtk.h"
#include "chrome/browser/gtk/gtk_theme_provider.h"
#include "chrome/browser/gtk/location_bar_view_gtk.h"
#include "views/controls/native/native_view_host.h"
#endif

namespace {
Expand Down Expand Up @@ -418,44 +420,6 @@ void AutocompleteEditViewGtk::SetFocus() {
gtk_widget_grab_focus(text_view_);
}

int AutocompleteEditViewGtk::TextWidth() {
int horizontal_border_size =
gtk_text_view_get_border_window_size(GTK_TEXT_VIEW(text_view_),
GTK_TEXT_WINDOW_LEFT) +
gtk_text_view_get_border_window_size(GTK_TEXT_VIEW(text_view_),
GTK_TEXT_WINDOW_RIGHT) +
gtk_text_view_get_left_margin(GTK_TEXT_VIEW(text_view_)) +
gtk_text_view_get_right_margin(GTK_TEXT_VIEW(text_view_));

GtkTextIter start, end;
GdkRectangle first_char_bounds, last_char_bounds;
gtk_text_buffer_get_start_iter(text_buffer_, &start);

// Use the real end iterator here to take the width of instant suggestion
// text into account, so that location bar can layout its children correctly.
gtk_text_buffer_get_end_iter(text_buffer_, &end);
gtk_text_view_get_iter_location(GTK_TEXT_VIEW(text_view_),
&start, &first_char_bounds);
gtk_text_view_get_iter_location(GTK_TEXT_VIEW(text_view_),
&end, &last_char_bounds);

gint first_char_start = first_char_bounds.x;
gint first_char_end = first_char_start + first_char_bounds.width;
gint last_char_start = last_char_bounds.x;
gint last_char_end = last_char_start + last_char_bounds.width;

// bounds width could be negative for RTL text.
if (first_char_start > first_char_end)
std::swap(first_char_start, first_char_end);
if (last_char_start > last_char_end)
std::swap(last_char_start, last_char_end);

gint text_width = first_char_start < last_char_start ?
last_char_end - first_char_start : first_char_end - last_char_start;

return text_width + horizontal_border_size;
}

int AutocompleteEditViewGtk::WidthOfTextAfterCursor() {
// Not used.
return -1;
Expand Down Expand Up @@ -790,6 +754,102 @@ CommandUpdater* AutocompleteEditViewGtk::GetCommandUpdater() {
return command_updater_;
}

#if defined(TOOLKIT_VIEWS)
views::View* AutocompleteEditViewGtk::AddToView(views::View* parent) {
views::NativeViewHost* host = new views::NativeViewHost;
parent->AddChildView(host);
host->set_focus_view(parent);
host->Attach(GetNativeView());
return host;
}

bool AutocompleteEditViewGtk::CommitInstantSuggestion(
const std::wstring& typed_text,
const std::wstring& suggestion) {
return CommitInstantSuggestion();
}

void AutocompleteEditViewGtk::EnableAccessibility() {
accessible_widget_helper_.reset(
new AccessibleWidgetHelper(text_view(), model_->profile()));
accessible_widget_helper_->SetWidgetName(
text_view(), l10n_util::GetStringUTF8(IDS_ACCNAME_LOCATION));
}

void AutocompleteEditViewGtk::SetInstantSuggestion(const string16& suggestion) {
SetInstantSuggestion(UTF16ToUTF8(suggestion));
}
#endif

int AutocompleteEditViewGtk::TextWidth() const {
int horizontal_border_size =
gtk_text_view_get_border_window_size(GTK_TEXT_VIEW(text_view_),
GTK_TEXT_WINDOW_LEFT) +
gtk_text_view_get_border_window_size(GTK_TEXT_VIEW(text_view_),
GTK_TEXT_WINDOW_RIGHT) +
gtk_text_view_get_left_margin(GTK_TEXT_VIEW(text_view_)) +
gtk_text_view_get_right_margin(GTK_TEXT_VIEW(text_view_));

GtkTextIter start, end;
GdkRectangle first_char_bounds, last_char_bounds;
gtk_text_buffer_get_start_iter(text_buffer_, &start);

// Use the real end iterator here to take the width of instant suggestion
// text into account, so that location bar can layout its children correctly.
gtk_text_buffer_get_end_iter(text_buffer_, &end);
gtk_text_view_get_iter_location(GTK_TEXT_VIEW(text_view_),
&start, &first_char_bounds);
gtk_text_view_get_iter_location(GTK_TEXT_VIEW(text_view_),
&end, &last_char_bounds);

gint first_char_start = first_char_bounds.x;
gint first_char_end = first_char_start + first_char_bounds.width;
gint last_char_start = last_char_bounds.x;
gint last_char_end = last_char_start + last_char_bounds.width;

// bounds width could be negative for RTL text.
if (first_char_start > first_char_end)
std::swap(first_char_start, first_char_end);
if (last_char_start > last_char_end)
std::swap(last_char_start, last_char_end);

gint text_width = first_char_start < last_char_start ?
last_char_end - first_char_start : first_char_end - last_char_start;

return text_width + horizontal_border_size;
}

#if defined(TOOLKIT_VIEWS)
// static
AutocompleteEditView* AutocompleteEditViewGtk::Create(
AutocompleteEditController* controller,
ToolbarModel* toolbar_model,
Profile* profile,
CommandUpdater* command_updater,
bool popup_window_mode,
const views::View* location_bar) {
AutocompleteEditViewGtk* autocomplete =
new AutocompleteEditViewGtk(controller,
toolbar_model,
profile,
command_updater,
popup_window_mode,
location_bar);
autocomplete->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(autocomplete->GetNativeView());
// Hide the widget. NativeViewHostGtk will make it visible again as
// necessary.
gtk_widget_hide(autocomplete->GetNativeView());

autocomplete->EnableAccessibility();

return autocomplete;
}
#endif

void AutocompleteEditViewGtk::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
Expand Down
38 changes: 30 additions & 8 deletions chrome/browser/autocomplete/autocomplete_edit_view_gtk.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "ui/base/animation/animation_delegate.h"
#include "webkit/glue/window_open_disposition.h"

class AccessibleWidgetHelper;
class AutocompleteEditController;
class AutocompleteEditModel;
class AutocompletePopupView;
Expand Down Expand Up @@ -74,19 +75,15 @@ class AutocompleteEditViewGtk : public AutocompleteEditView,
CommandUpdater* command_updater,
bool popup_window_mode,
#if defined(TOOLKIT_VIEWS)
const views::View* location_bar);
const views::View* location_bar
#else
GtkWidget* location_bar);
GtkWidget* location_bar
#endif
~AutocompleteEditViewGtk();
);
virtual ~AutocompleteEditViewGtk();

// Initialize, create the underlying widgets, etc.
void Init();

// Returns the width in pixels needed to display the current text. The
// returned value includes margins.
int TextWidth();

// Returns the width in pixels needed to display the text from one character
// before the caret to the end of the string. See comments in
// LocationBarView::Layout as to why this uses -1.
Expand Down Expand Up @@ -146,6 +143,27 @@ class AutocompleteEditViewGtk : public AutocompleteEditView,
virtual bool OnAfterPossibleChange();
virtual gfx::NativeView GetNativeView() const;
virtual CommandUpdater* GetCommandUpdater();
#if defined(TOOLKIT_VIEWS)
virtual views::View* AddToView(views::View* parent);
virtual bool CommitInstantSuggestion(const std::wstring& typed_text,
const std::wstring& suggested_text);
virtual void SetInstantSuggestion(const string16& suggestion);

// Enables accessibility on AutocompleteEditView.
void EnableAccessibility();

// A factory method to create an AutocompleteEditView instance initialized for
// linux_views. This currently returns an instance of
// AutocompleteEditViewGtk only, but AutocompleteEditViewViews will
// be added as an option when TextfieldViews is enabled.
static AutocompleteEditView* Create(AutocompleteEditController* controller,
ToolbarModel* toolbar_model,
Profile* profile,
CommandUpdater* command_updater,
bool popup_window_mode,
const views::View* location_bar);
#endif
virtual int TextWidth() const;

// Overridden from NotificationObserver:
virtual void Observe(NotificationType type,
Expand Down Expand Up @@ -517,6 +535,10 @@ class AutocompleteEditViewGtk : public AutocompleteEditView,

GtkSignalRegistrar signals_;

#if defined(TOOLKIT_VIEWS)
scoped_ptr<AccessibleWidgetHelper> accessible_widget_helper_;
#endif

DISALLOW_COPY_AND_ASSIGN(AutocompleteEditViewGtk);
};

Expand Down
33 changes: 27 additions & 6 deletions chrome/browser/autocomplete/autocomplete_edit_view_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -498,10 +498,6 @@ AutocompleteEditViewWin::~AutocompleteEditViewWin() {
g_paint_patcher.Pointer()->DerefPatch();
}

int AutocompleteEditViewWin::TextWidth() {
return WidthNeededToDisplay(GetText());
}

int AutocompleteEditViewWin::WidthOfTextAfterCursor() {
CHARRANGE selection;
GetSelection(selection);
Expand Down Expand Up @@ -913,6 +909,30 @@ CommandUpdater* AutocompleteEditViewWin::GetCommandUpdater() {
return command_updater_;
}

views::View* AutocompleteEditViewWin::AddToView(views::View* parent) {
views::NativeViewHost* host = new views::NativeViewHost;
parent->AddChildView(host);
host->set_focus_view(parent);
host->Attach(GetNativeView());
return host;
}

bool AutocompleteEditViewWin::CommitInstantSuggestion(
const std::wstring& typed_text,
const std::wstring& suggested_text) {
model_->FinalizeInstantQuery(typed_text, suggested_text);
return true;
}

void AutocompleteEditViewWin::SetInstantSuggestion(const string16& suggestion) {
// Win shows the suggestion in LocationBarView.
NOTREACHED();
}

int AutocompleteEditViewWin::TextWidth() const {
return WidthNeededToDisplay(GetText());
}

void AutocompleteEditViewWin::PasteAndGo(const std::wstring& text) {
if (CanPasteAndGo(text))
model_->PasteAndGo();
Expand Down Expand Up @@ -2570,15 +2590,16 @@ void AutocompleteEditViewWin::TrackMousePosition(MouseButton button,
}
}

int AutocompleteEditViewWin::GetHorizontalMargin() {
int AutocompleteEditViewWin::GetHorizontalMargin() const {
RECT rect;
GetRect(&rect);
RECT client_rect;
GetClientRect(&client_rect);
return (rect.left - client_rect.left) + (client_rect.right - rect.right);
}

int AutocompleteEditViewWin::WidthNeededToDisplay(const std::wstring& text) {
int AutocompleteEditViewWin::WidthNeededToDisplay(
const std::wstring& text) const {
// Use font_.GetStringWidth() instead of
// PosFromChar(location_entry_->GetTextLength()) because PosFromChar() is
// apparently buggy. In both LTR UI and RTL UI with left-to-right layout,
Expand Down
14 changes: 8 additions & 6 deletions chrome/browser/autocomplete/autocomplete_edit_view_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ class AutocompleteEditViewWin

views::View* parent_view() const { return parent_view_; }

// Returns the width in pixels needed to display the current text. The
// returned value includes margins.
int TextWidth();

// Returns the width in pixels needed to display the text from one character
// before the caret to the end of the string. See comments in
// LocationBarView::Layout as to why this uses -1.
Expand Down Expand Up @@ -137,6 +133,12 @@ class AutocompleteEditViewWin
virtual bool OnAfterPossibleChange();
virtual gfx::NativeView GetNativeView() const;
virtual CommandUpdater* GetCommandUpdater();
virtual views::View* AddToView(views::View* parent);
virtual bool CommitInstantSuggestion(const std::wstring& typed_text,
const std::wstring& suggested_text);
virtual void SetInstantSuggestion(const string16& suggestion);
virtual int TextWidth() const;

int GetPopupMaxYCoordinate();

// Exposes custom IAccessible implementation to the overall MSAA hierarchy.
Expand Down Expand Up @@ -401,10 +403,10 @@ class AutocompleteEditViewWin
void TrackMousePosition(MouseButton button, const CPoint& point);

// Returns the sum of the left and right margins.
int GetHorizontalMargin();
int GetHorizontalMargin() const;

// Returns the width in pixels needed to display |text|.
int WidthNeededToDisplay(const std::wstring& text);
int WidthNeededToDisplay(const std::wstring& text) const;

scoped_ptr<AutocompleteEditModel> model_;

Expand Down
Loading

0 comments on commit caa62f4

Please sign in to comment.