diff options
author | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-10 00:55:13 +0000 |
---|---|---|
committer | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-10 00:55:13 +0000 |
commit | 823821512bc23506c8d8b90651cfebe72d314d20 (patch) | |
tree | 5cdc7a84d9029a94c8f1c77d985fb0955c4c704b /views | |
parent | 23a460aaf49d5b2a74ec49275344753d7ec723d1 (diff) | |
download | chromium_src-823821512bc23506c8d8b90651cfebe72d314d20.zip chromium_src-823821512bc23506c8d8b90651cfebe72d314d20.tar.gz chromium_src-823821512bc23506c8d8b90651cfebe72d314d20.tar.bz2 |
views: Moves TextfieldController/TextRange into their own headers.
BUG=None
TEST=existing unit_tests
Review URL: http://codereview.chromium.org/6628037
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@77571 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'views')
-rw-r--r-- | views/controls/textfield/native_textfield_gtk.cc | 8 | ||||
-rw-r--r-- | views/controls/textfield/native_textfield_gtk.h | 2 | ||||
-rw-r--r-- | views/controls/textfield/native_textfield_views.cc | 10 | ||||
-rw-r--r-- | views/controls/textfield/native_textfield_views.h | 3 | ||||
-rw-r--r-- | views/controls/textfield/native_textfield_views_unittest.cc | 9 | ||||
-rw-r--r-- | views/controls/textfield/native_textfield_win.cc | 8 | ||||
-rw-r--r-- | views/controls/textfield/text_range.h | 67 | ||||
-rw-r--r-- | views/controls/textfield/textfield.cc | 29 | ||||
-rw-r--r-- | views/controls/textfield/textfield.h | 88 | ||||
-rw-r--r-- | views/controls/textfield/textfield_controller.h | 33 | ||||
-rw-r--r-- | views/controls/textfield/textfield_views_model.cc | 3 | ||||
-rw-r--r-- | views/controls/textfield/textfield_views_model_unittest.cc | 1 | ||||
-rw-r--r-- | views/examples/textfield_example.cc | 1 | ||||
-rw-r--r-- | views/examples/textfield_example.h | 10 | ||||
-rw-r--r-- | views/views.gyp | 2 |
15 files changed, 153 insertions, 121 deletions
diff --git a/views/controls/textfield/native_textfield_gtk.cc b/views/controls/textfield/native_textfield_gtk.cc index 4e0b4cc..918b207 100644 --- a/views/controls/textfield/native_textfield_gtk.cc +++ b/views/controls/textfield/native_textfield_gtk.cc @@ -15,7 +15,9 @@ #include "views/controls/textfield/gtk_views_entry.h" #include "views/controls/textfield/gtk_views_textview.h" #include "views/controls/textfield/native_textfield_views.h" +#include "views/controls/textfield/text_range.h" #include "views/controls/textfield/textfield.h" +#include "views/controls/textfield/textfield_controller.h" #include "views/widget/widget_gtk.h" namespace views { @@ -383,7 +385,7 @@ gboolean NativeTextfieldGtk::OnKeyPressEventHandler( } gboolean NativeTextfieldGtk::OnKeyPressEvent(GdkEventKey* event) { - Textfield::Controller* controller = textfield_->GetController(); + TextfieldController* controller = textfield_->GetController(); if (controller) { KeyEvent key_event(reinterpret_cast<GdkEvent*>(event)); return controller->HandleKeyEvent(textfield_, key_event); @@ -406,7 +408,7 @@ gboolean NativeTextfieldGtk::OnActivate() { GdkEventKey* key_event = reinterpret_cast<GdkEventKey*>(event); gboolean handled = false; - Textfield::Controller* controller = textfield_->GetController(); + TextfieldController* controller = textfield_->GetController(); if (controller) { KeyEvent views_key_event(event); handled = controller->HandleKeyEvent(textfield_, views_key_event); @@ -428,7 +430,7 @@ gboolean NativeTextfieldGtk::OnChangedHandler( gboolean NativeTextfieldGtk::OnChanged() { textfield_->SyncText(); - Textfield::Controller* controller = textfield_->GetController(); + TextfieldController* controller = textfield_->GetController(); if (controller) controller->ContentsChanged(textfield_, GetText()); textfield_->NotifyAccessibilityEvent(AccessibilityTypes::EVENT_TEXT_CHANGED); diff --git a/views/controls/textfield/native_textfield_gtk.h b/views/controls/textfield/native_textfield_gtk.h index 6f211ad..802d1fb 100644 --- a/views/controls/textfield/native_textfield_gtk.h +++ b/views/controls/textfield/native_textfield_gtk.h @@ -14,6 +14,8 @@ namespace views { +class TextRange; + class NativeTextfieldGtk : public NativeControlGtk, public NativeTextfieldWrapper { public: diff --git a/views/controls/textfield/native_textfield_views.cc b/views/controls/textfield/native_textfield_views.cc index 775308a..d6214b2 100644 --- a/views/controls/textfield/native_textfield_views.cc +++ b/views/controls/textfield/native_textfield_views.cc @@ -18,7 +18,9 @@ #include "views/background.h" #include "views/border.h" #include "views/controls/menu/menu_2.h" +#include "views/controls/textfield/text_range.h" #include "views/controls/textfield/textfield.h" +#include "views/controls/textfield/textfield_controller.h" #include "views/controls/textfield/textfield_views_model.h" #include "views/events/event.h" #include "views/metrics.h" @@ -162,7 +164,7 @@ void NativeTextfieldViews::UpdateText() { UpdateCursorBoundsAndTextOffset(); SchedulePaint(); if (changed) { - Textfield::Controller* controller = textfield_->GetController(); + TextfieldController* controller = textfield_->GetController(); if (controller) controller->ContentsChanged(textfield_, GetText()); } @@ -175,7 +177,7 @@ void NativeTextfieldViews::AppendText(const string16& text) { UpdateCursorBoundsAndTextOffset(); SchedulePaint(); - Textfield::Controller* controller = textfield_->GetController(); + TextfieldController* controller = textfield_->GetController(); if (controller) controller->ContentsChanged(textfield_, GetText()); } @@ -291,7 +293,7 @@ size_t NativeTextfieldViews::GetCursorPosition() const { } bool NativeTextfieldViews::HandleKeyPressed(const views::KeyEvent& e) { - Textfield::Controller* controller = textfield_->GetController(); + TextfieldController* controller = textfield_->GetController(); bool handled = false; if (controller) handled = controller->HandleKeyEvent(textfield_, e); @@ -824,7 +826,7 @@ void NativeTextfieldViews::SetCursorForMouseClick(const views::MouseEvent& e) { void NativeTextfieldViews::PropagateTextChange() { textfield_->SyncText(); - Textfield::Controller* controller = textfield_->GetController(); + TextfieldController* controller = textfield_->GetController(); if (controller) controller->ContentsChanged(textfield_, GetText()); } diff --git a/views/controls/textfield/native_textfield_views.h b/views/controls/textfield/native_textfield_views.h index 5a21596..059e0f9bee 100644 --- a/views/controls/textfield/native_textfield_views.h +++ b/views/controls/textfield/native_textfield_views.h @@ -20,13 +20,14 @@ class Time; namespace gfx { class Canvas; -} // namespace +} namespace views { class KeyEvent; class Menu2; class TextfieldViewsModel; +class TextRange; // A views/skia only implementation of NativeTextfieldWrapper. // No platform specific code is used. diff --git a/views/controls/textfield/native_textfield_views_unittest.cc b/views/controls/textfield/native_textfield_views_unittest.cc index d8e8d1f..b284e6e 100644 --- a/views/controls/textfield/native_textfield_views_unittest.cc +++ b/views/controls/textfield/native_textfield_views_unittest.cc @@ -12,13 +12,14 @@ #include "views/controls/menu/menu_2.h" #include "views/controls/textfield/native_textfield_views.h" #include "views/controls/textfield/textfield.h" +#include "views/controls/textfield/textfield_controller.h" #include "views/controls/textfield/textfield_views_model.h" #include "views/events/event.h" #include "views/focus/focus_manager.h" #include "views/test/test_views_delegate.h" #include "views/test/views_test_base.h" -#include "views/widget/widget.h" #include "views/views_delegate.h" +#include "views/widget/widget.h" namespace views { @@ -33,7 +34,7 @@ namespace views { // textfield_unittests.cc once we move the test utility functions // from chrome/browser/automation/ to app/test/. class NativeTextfieldViewsTest : public ViewsTestBase, - public Textfield::Controller { + public TextfieldController { public: NativeTextfieldViewsTest() : widget_(NULL), @@ -42,7 +43,7 @@ class NativeTextfieldViewsTest : public ViewsTestBase, model_(NULL) { } - // ::testing::Test overrides. + // ::testing::Test: virtual void SetUp() { NativeTextfieldViews::SetEnableTextfieldViews(true); } @@ -54,7 +55,7 @@ class NativeTextfieldViewsTest : public ViewsTestBase, ViewsTestBase::TearDown(); } - // Textfield::Controller implementation: + // TextfieldController: virtual void ContentsChanged(Textfield* sender, const string16& new_contents){ last_contents_ = new_contents; diff --git a/views/controls/textfield/native_textfield_win.cc b/views/controls/textfield/native_textfield_win.cc index 60c5b56..8296e26 100644 --- a/views/controls/textfield/native_textfield_win.cc +++ b/views/controls/textfield/native_textfield_win.cc @@ -14,17 +14,19 @@ #include "skia/ext/skia_utils_win.h" #include "ui/base/clipboard/clipboard.h" #include "ui/base/clipboard/scoped_clipboard_writer.h" -#include "ui/base/keycodes/keyboard_codes.h" #include "ui/base/keycodes/keyboard_code_conversion_win.h" +#include "ui/base/keycodes/keyboard_codes.h" #include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util_win.h" #include "ui/gfx/native_theme_win.h" #include "views/controls/label.h" -#include "views/controls/menu/menu_win.h" #include "views/controls/menu/menu_2.h" +#include "views/controls/menu/menu_win.h" #include "views/controls/native/native_view_host.h" #include "views/controls/textfield/native_textfield_views.h" +#include "views/controls/textfield/text_range.h" #include "views/controls/textfield/textfield.h" +#include "views/controls/textfield/textfield_controller.h" #include "views/focus/focus_manager.h" #include "views/focus/focus_util_win.h" #include "views/metrics.h" @@ -934,7 +936,7 @@ void NativeTextfieldWin::HandleKeystroke() { const MSG* msg = GetCurrentMessage(); ScopedFreeze freeze(this, GetTextObjectModel()); - Textfield::Controller* controller = textfield_->GetController(); + TextfieldController* controller = textfield_->GetController(); bool handled = false; if (controller) { KeyEvent event(*msg); diff --git a/views/controls/textfield/text_range.h b/views/controls/textfield/text_range.h new file mode 100644 index 0000000..c367444 --- /dev/null +++ b/views/controls/textfield/text_range.h @@ -0,0 +1,67 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef VIEWS_CONTROLS_TEXTFIELD_TEXT_RANGE_H_ +#define VIEWS_CONTROLS_TEXTFIELD_TEXT_RANGE_H_ +#pragma once + +#include <stddef.h> + +#include <algorithm> + +namespace views { + +// TextRange specifies the range of text in the Textfield. This is used to +// specify selected text and will be used to change the attributes of characters +// in the textfield. When this is used for selection, the end is caret position, +// and the start is where selection started. The range preserves the direction, +// and selecting from the end to the begining is considered "reverse" order. +// (that is, start > end is reverse) +class TextRange { + public: + TextRange() : start_(0), end_(0) {} + TextRange(size_t start, size_t end) : start_(start), end_(end) {} + + // Allow copy so that the omnibox can save the view state for each tabs. + explicit TextRange(const TextRange& range) + : start_(range.start_), + end_(range.end_) {} + + // Returns the start position; + size_t start() const { return start_; } + + // Returns the end position. + size_t end() const { return end_; } + + // Returns true if the selected text is empty. + bool is_empty() const { return start_ == end_; } + + // Returns true if the selection is made in reverse order. + bool is_reverse() const { return start_ > end_; } + + // Returns the min of selected range. + size_t GetMin() const { return std::min(start_, end_); } + + // Returns the max of selected range. + size_t GetMax() const { return std::max(start_, end_); } + + // Returns true if the the selection range is same ignoring the direction. + bool EqualsIgnoringDirection(const TextRange& range) const { + return GetMin() == range.GetMin() && GetMax() == range.GetMax(); + } + + // Set the range with |start| and |end|. + void SetRange(size_t start, size_t end) { + start_ = start; + end_ = end; + } + + private: + size_t start_; + size_t end_; +}; + +} // namespace views + +#endif // VIEWS_CONTROLS_TEXTFIELD_TEXT_RANGE_H_ diff --git a/views/controls/textfield/textfield.cc b/views/controls/textfield/textfield.cc index 8c1003b..b5ab72a 100644 --- a/views/controls/textfield/textfield.cc +++ b/views/controls/textfield/textfield.cc @@ -16,6 +16,8 @@ #include "ui/gfx/insets.h" #include "views/controls/native/native_view_host.h" #include "views/controls/textfield/native_textfield_wrapper.h" +#include "views/controls/textfield/text_range.h" +#include "views/controls/textfield/textfield_controller.h" #include "views/widget/widget.h" #if defined(OS_LINUX) @@ -24,8 +26,8 @@ #include "base/win/win_util.h" // TODO(beng): this should be removed when the OS_WIN hack from // ViewHierarchyChanged is removed. -#include "views/controls/textfield/native_textfield_win.h" #include "views/controls/textfield/native_textfield_views.h" +#include "views/controls/textfield/native_textfield_win.h" #include "views/events/event_utils_win.h" #endif @@ -35,27 +37,6 @@ namespace views { const char Textfield::kViewClassName[] = "views/Textfield"; ///////////////////////////////////////////////////////////////////////////// -// TextRange - -TextRange::TextRange(size_t start, size_t end) - : start_(start), - end_(end) { -} - -size_t TextRange::GetMin() const { - return std::min(start_, end_); -} - -size_t TextRange::GetMax() const { - return std::max(start_, end_); -} - -void TextRange::SetRange(size_t start, size_t end) { - start_ = start; - end_ = end; -} - -///////////////////////////////////////////////////////////////////////////// // Textfield Textfield::Textfield() @@ -97,11 +78,11 @@ Textfield::Textfield(StyleFlags style) Textfield::~Textfield() { } -void Textfield::SetController(Controller* controller) { +void Textfield::SetController(TextfieldController* controller) { controller_ = controller; } -Textfield::Controller* Textfield::GetController() const { +TextfieldController* Textfield::GetController() const { return controller_; } diff --git a/views/controls/textfield/textfield.h b/views/controls/textfield/textfield.h index a4f2aec..23ec627 100644 --- a/views/controls/textfield/textfield.h +++ b/views/controls/textfield/textfield.h @@ -15,6 +15,7 @@ #include <string> #include "base/basictypes.h" +#include "base/compiler_specific.h" #include "base/string16.h" #include "third_party/skia/include/core/SkColor.h" #include "ui/base/keycodes/keyboard_codes.h" @@ -33,94 +34,29 @@ namespace views { class KeyEvent; class NativeTextfieldWrapper; +class TextfieldController; +class TextRange; -// TextRange specifies the range of text in the Textfield. This is -// used to specify selected text and will be used to change the -// attributes of characters in the textfield. When this is used for -// selection, the end is caret position, and the start is where -// selection started. The range preserves the direction, and -// selecting from the end to the begining is considered "reverse" -// order. (that is, start > end is reverse) -class TextRange { - public: - TextRange() : start_(0), end_(0) {} - TextRange(size_t start, size_t end); - - // Allow copy so that the omnibox can save the view state - // for each tabs. - explicit TextRange(const TextRange& range) - : start_(range.start_), end_(range.end_) {} - - // Returns the start position; - size_t start() const { return start_; } - - // Returns the end position. - size_t end() const { return end_; } - - // Returns true if the selected text is empty. - bool is_empty() const { return start_ == end_; } - - // Returns true if the selection is made in reverse order. - bool is_reverse() const { return start_ > end_; } - - // Returns the min of selected range. - size_t GetMin() const; - - // Returns the max of selected range. - size_t GetMax() const; - - // Returns true if the the selection range is same ignoring the direction. - bool EqualsIgnoringDirection(const TextRange& range) const { - return GetMin() == range.GetMin() && GetMax() == range.GetMax(); - } - - // Set the range with |start| and |end|. - void SetRange(size_t start, size_t end); - - private: - size_t start_; - size_t end_; - - // No assign. - void operator=(const TextRange&); -}; - -// This class implements a ChromeView that wraps a native text (edit) field. +// This class implements a View that wraps a native text (edit) field. class Textfield : public View { public: // The button's class name. static const char kViewClassName[]; - // This defines the callback interface for other code to be notified of - // changes in the state of a text field. - class Controller { - public: - // This method is called whenever the text in the field changes. - virtual void ContentsChanged(Textfield* sender, - const string16& new_contents) = 0; - - // This method is called to get notified about keystrokes in the edit. - // This method returns true if the message was handled and should not be - // processed further. If it returns false the processing continues. - virtual bool HandleKeyEvent(Textfield* sender, - const KeyEvent& key_event) = 0; - }; - enum StyleFlags { - STYLE_DEFAULT = 0, - STYLE_PASSWORD = 1<<0, - STYLE_MULTILINE = 1<<1, - STYLE_LOWERCASE = 1<<2 + STYLE_DEFAULT = 0, + STYLE_PASSWORD = 1 << 0, + STYLE_MULTILINE = 1 << 1, + STYLE_LOWERCASE = 1 << 2 }; Textfield(); explicit Textfield(StyleFlags style); virtual ~Textfield(); - - // Controller accessors - void SetController(Controller* controller); - Controller* GetController() const; + // TextfieldController accessors + void SetController(TextfieldController* controller); + TextfieldController* GetController() const; // Gets/Sets whether or not the Textfield is read-only. bool read_only() const { return read_only_; } @@ -287,7 +223,7 @@ class Textfield : public View { private: // This is the current listener for events from this Textfield. - Controller* controller_; + TextfieldController* controller_; // The mask of style options for this Textfield. StyleFlags style_; diff --git a/views/controls/textfield/textfield_controller.h b/views/controls/textfield/textfield_controller.h new file mode 100644 index 0000000..53fe97c --- /dev/null +++ b/views/controls/textfield/textfield_controller.h @@ -0,0 +1,33 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_CONTROLLER_H_ +#define VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_CONTROLLER_H_ +#pragma once + +#include "base/string16.h" + +namespace views { + +class KeyEvent; +class Textfield; + +// This defines the callback interface for other code to be notified of changes +// in the state of a text field. +class TextfieldController { + public: + // This method is called whenever the text in the field changes. + virtual void ContentsChanged(Textfield* sender, + const string16& new_contents) = 0; + + // This method is called to get notified about keystrokes in the edit. + // Returns true if the message was handled and should not be processed + // further. If it returns false the processing continues. + virtual bool HandleKeyEvent(Textfield* sender, + const KeyEvent& key_event) = 0; +}; + +} // namespace views + +#endif // VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_CONTROLLER_H_ diff --git a/views/controls/textfield/textfield_views_model.cc b/views/controls/textfield/textfield_views_model.cc index e63c28a..4d88ffb 100644 --- a/views/controls/textfield/textfield_views_model.cc +++ b/views/controls/textfield/textfield_views_model.cc @@ -12,8 +12,9 @@ #include "ui/base/clipboard/clipboard.h" #include "ui/base/clipboard/scoped_clipboard_writer.h" #include "ui/gfx/font.h" -#include "views/views_delegate.h" +#include "views/controls/textfield/text_range.h" #include "views/controls/textfield/textfield.h" +#include "views/views_delegate.h" namespace views { diff --git a/views/controls/textfield/textfield_views_model_unittest.cc b/views/controls/textfield/textfield_views_model_unittest.cc index b0a29c4..b44b487 100644 --- a/views/controls/textfield/textfield_views_model_unittest.cc +++ b/views/controls/textfield/textfield_views_model_unittest.cc @@ -9,6 +9,7 @@ #include "testing/gtest/include/gtest/gtest.h" #include "ui/base/clipboard/clipboard.h" #include "ui/base/clipboard/scoped_clipboard_writer.h" +#include "views/controls/textfield/text_range.h" #include "views/controls/textfield/textfield.h" #include "views/controls/textfield/textfield_views_model.h" #include "views/test/test_views_delegate.h" diff --git a/views/examples/textfield_example.cc b/views/examples/textfield_example.cc index 3266e56..61a3175 100644 --- a/views/examples/textfield_example.cc +++ b/views/examples/textfield_example.cc @@ -6,6 +6,7 @@ #include "base/utf_string_conversions.h" #include "views/controls/label.h" +#include "views/controls/textfield/textfield.h" #include "views/layout/grid_layout.h" #include "views/view.h" diff --git a/views/examples/textfield_example.h b/views/examples/textfield_example.h index a94797b..1535efb 100644 --- a/views/examples/textfield_example.h +++ b/views/examples/textfield_example.h @@ -12,31 +12,31 @@ #include "base/compiler_specific.h" #include "base/string16.h" #include "views/controls/button/text_button.h" -#include "views/controls/textfield/textfield.h" +#include "views/controls/textfield/textfield_controller.h" #include "views/examples/example_base.h" namespace examples { // TextfieldExample mimics login screen. class TextfieldExample : public ExampleBase, - public views::Textfield::Controller, + public views::TextfieldController, public views::ButtonListener { public: explicit TextfieldExample(ExamplesMain* main); virtual ~TextfieldExample(); - // Overridden from ExampleBase: + // ExampleBase: virtual std::wstring GetExampleTitle() OVERRIDE; virtual void CreateExampleView(views::View* container) OVERRIDE; private: - // Overridden from views::Textfield::Controller: + // views::TextfieldController: virtual void ContentsChanged(views::Textfield* sender, const string16& new_contents) OVERRIDE; virtual bool HandleKeyEvent(views::Textfield* sender, const views::KeyEvent& key_event) OVERRIDE; - // Overridden from views::ButtonListener: + // views::ButtonListener: virtual void ButtonPressed(views::Button* sender, const views::Event& event) OVERRIDE; diff --git a/views/views.gyp b/views/views.gyp index 5bb721b..0bda492 100644 --- a/views/views.gyp +++ b/views/views.gyp @@ -236,8 +236,10 @@ 'controls/textfield/gtk_views_entry.h', 'controls/textfield/gtk_views_textview.cc', 'controls/textfield/gtk_views_textview.h', + 'controls/textfield/text_range.h', 'controls/textfield/textfield.cc', 'controls/textfield/textfield.h', + 'controls/textfield/textfield_controller.h', 'controls/textfield/textfield_views_model.cc', 'controls/textfield/textfield_views_model.h', 'controls/textfield/native_textfield_gtk.cc', |