summaryrefslogtreecommitdiffstats
path: root/views/controls
diff options
context:
space:
mode:
Diffstat (limited to 'views/controls')
-rw-r--r--views/controls/native_control_gtk.cc2
-rw-r--r--views/controls/textfield/native_textfield_gtk.cc68
-rw-r--r--views/controls/textfield/native_textfield_gtk.h9
-rw-r--r--views/controls/textfield/native_textfield_win.cc6
-rw-r--r--views/controls/textfield/native_textfield_win.h6
-rw-r--r--views/controls/textfield/native_textfield_wrapper.h9
-rw-r--r--views/controls/textfield/textfield.cc20
-rw-r--r--views/controls/textfield/textfield.h16
8 files changed, 84 insertions, 52 deletions
diff --git a/views/controls/native_control_gtk.cc b/views/controls/native_control_gtk.cc
index 656632b..e95278d 100644
--- a/views/controls/native_control_gtk.cc
+++ b/views/controls/native_control_gtk.cc
@@ -55,7 +55,7 @@ void NativeControlGtk::VisibilityChanged(View* starting_from, bool is_visible) {
void NativeControlGtk::Focus() {
DCHECK(native_view());
- NOTIMPLEMENTED();
+ gtk_widget_grab_focus(native_view());
}
void NativeControlGtk::NativeControlCreated(GtkWidget* native_control) {
diff --git a/views/controls/textfield/native_textfield_gtk.cc b/views/controls/textfield/native_textfield_gtk.cc
index 82cda2b..6ceaab4 100644
--- a/views/controls/textfield/native_textfield_gtk.cc
+++ b/views/controls/textfield/native_textfield_gtk.cc
@@ -2,9 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <gtk/gtk.h>
+
#include "views/controls/textfield/native_textfield_gtk.h"
+#include "base/gfx/gtk_util.h"
#include "base/string_util.h"
+#include "skia/ext/skia_utils_gtk.h"
#include "views/controls/textfield/textfield.h"
namespace views {
@@ -13,8 +17,7 @@ namespace views {
// NativeTextfieldGtk, public:
NativeTextfieldGtk::NativeTextfieldGtk(Textfield* textfield)
- : NativeControlGtk(),
- textfield_(textfield) {
+ : textfield_(textfield) {
if (textfield_->style() & Textfield::STYLE_MULTILINE)
NOTIMPLEMENTED(); // We don't support multiline yet.
}
@@ -25,45 +28,49 @@ NativeTextfieldGtk::~NativeTextfieldGtk() {
////////////////////////////////////////////////////////////////////////////////
// NativeTextfieldGtk, NativeTextfieldWrapper implementation:
-std::wstring NativeTextfieldGtk::GetText() const {
- if (!native_view())
- return std::wstring();
- return UTF8ToWide(gtk_entry_get_text(GTK_ENTRY(native_view())));
+string16 NativeTextfieldGtk::GetText() const {
+ return UTF8ToUTF16(gtk_entry_get_text(GTK_ENTRY(native_view())));
}
void NativeTextfieldGtk::UpdateText() {
if (!native_view())
return;
gtk_entry_set_text(GTK_ENTRY(native_view()),
- WideToUTF8(textfield_->text()).c_str());
+ UTF16ToUTF8(textfield_->text()).c_str());
}
-void NativeTextfieldGtk::AppendText(const std::wstring& text) {
+void NativeTextfieldGtk::AppendText(const string16& text) {
+ gint position = -1;
+ gtk_editable_insert_text(GTK_EDITABLE(native_view()),
+ UTF16ToUTF8(text).c_str(),
+ text.size(), &position);
if (!native_view())
return;
- gtk_entry_append_text(GTK_ENTRY(native_view()), WideToUTF8(text).c_str());
+ gtk_entry_append_text(GTK_ENTRY(native_view()), UTF16ToUTF8(text).c_str());
}
-std::wstring NativeTextfieldGtk::GetSelectedText() const {
+string16 NativeTextfieldGtk::GetSelectedText() const {
if (!native_view())
- return std::wstring();
+ return string16();
- int begin, end;
+ string16 result;
+ gint start_pos;
+ gint end_pos;
if (!gtk_editable_get_selection_bounds(GTK_EDITABLE(native_view()),
- &begin, &end))
- return std::wstring(); // Nothing selected.
+ &start_pos, &end_pos))
+ return result; // No selection.
- return UTF8ToWide(std::string(
- &gtk_entry_get_text(GTK_ENTRY(native_view()))[begin],
- end - begin));
+ UTF8ToUTF16(gtk_editable_get_chars(GTK_EDITABLE(native_view()),
+ start_pos, end_pos),
+ end_pos - start_pos, &result);
+ return result;
}
void NativeTextfieldGtk::SelectAll() {
if (!native_view())
return;
// -1 as the end position selects to the end of the text.
- gtk_editable_select_region(GTK_EDITABLE(native_view()),
- 0, -1);
+ gtk_editable_select_region(GTK_EDITABLE(native_view()), 0, -1);
}
void NativeTextfieldGtk::ClearSelection() {
@@ -75,38 +82,44 @@ void NativeTextfieldGtk::ClearSelection() {
void NativeTextfieldGtk::UpdateBorder() {
if (!native_view())
return;
- NOTIMPLEMENTED();
}
void NativeTextfieldGtk::UpdateBackgroundColor() {
- if (!native_view())
+ if (textfield_->use_default_background_color()) {
+ // Passing NULL as the color undoes the effect of previous calls to
+ // gtk_widget_modify_base.
+ gtk_widget_modify_base(native_view(), GTK_STATE_NORMAL, NULL);
return;
- NOTIMPLEMENTED();
+ }
+ GdkColor gdk_color = skia::SkColorToGdkColor(textfield_->background_color());
+ gtk_widget_modify_base(native_view(), GTK_STATE_NORMAL, &gdk_color);
}
void NativeTextfieldGtk::UpdateReadOnly() {
if (!native_view())
return;
gtk_editable_set_editable(GTK_EDITABLE(native_view()),
- textfield_->IsEnabled());
+ !textfield_->read_only());
}
void NativeTextfieldGtk::UpdateFont() {
if (!native_view())
return;
- NOTIMPLEMENTED();
+ gtk_widget_modify_font(native_view(),
+ gfx::Font::PangoFontFromGfxFont(textfield_->font()));
}
void NativeTextfieldGtk::UpdateEnabled() {
if (!native_view())
return;
- NOTIMPLEMENTED();
+ SetEnabled(textfield_->IsEnabled());
}
void NativeTextfieldGtk::SetHorizontalMargins(int left, int right) {
if (!native_view())
return;
- NOTIMPLEMENTED();
+ GtkBorder border = { left, right, 0, 0 };
+ gtk_entry_set_inner_border(GTK_ENTRY(native_view()), &border);
}
void NativeTextfieldGtk::SetFocus() {
@@ -125,10 +138,9 @@ gfx::NativeView NativeTextfieldGtk::GetTestingHandle() const {
// NativeTextfieldGtk, NativeControlGtk overrides:
void NativeTextfieldGtk::CreateNativeControl() {
- GtkWidget* widget = gtk_entry_new();
// TODO(brettw) hook in an observer to get text change events so we can call
// the controller.
- NativeControlCreated(widget);
+ NativeControlCreated(gtk_entry_new());
}
void NativeTextfieldGtk::NativeControlCreated(GtkWidget* widget) {
diff --git a/views/controls/textfield/native_textfield_gtk.h b/views/controls/textfield/native_textfield_gtk.h
index 83013f3..9e10116 100644
--- a/views/controls/textfield/native_textfield_gtk.h
+++ b/views/controls/textfield/native_textfield_gtk.h
@@ -5,6 +5,9 @@
#ifndef VIEWS_CONTROLS_TEXTFIELD_NATIVE_TEXTFIELD_GTK_H_
#define VIEWS_CONTROLS_TEXTFIELD_NATIVE_TEXTFIELD_GTK_H_
+#include <gtk/gtk.h>
+
+#include "base/string16.h"
#include "views/controls/native_control_gtk.h"
#include "views/controls/textfield/native_textfield_wrapper.h"
@@ -17,10 +20,10 @@ class NativeTextfieldGtk : public NativeControlGtk,
~NativeTextfieldGtk();
// Overridden from NativeTextfieldWrapper:
- virtual std::wstring GetText() const;
+ virtual string16 GetText() const;
virtual void UpdateText();
- virtual void AppendText(const std::wstring& text);
- virtual std::wstring GetSelectedText() const;
+ virtual void AppendText(const string16& text);
+ virtual string16 GetSelectedText() const;
virtual void SelectAll();
virtual void ClearSelection();
virtual void UpdateBorder();
diff --git a/views/controls/textfield/native_textfield_win.cc b/views/controls/textfield/native_textfield_win.cc
index e99c073..1e06816 100644
--- a/views/controls/textfield/native_textfield_win.cc
+++ b/views/controls/textfield/native_textfield_win.cc
@@ -113,7 +113,7 @@ void NativeTextfieldWin::AttachHack() {
////////////////////////////////////////////////////////////////////////////////
// NativeTextfieldWin, NativeTextfieldWrapper implementation:
-std::wstring NativeTextfieldWin::GetText() const {
+string16 NativeTextfieldWin::GetText() const {
int len = GetTextLength() + 1;
std::wstring str;
GetWindowText(WriteInto(&str, len), len);
@@ -132,14 +132,14 @@ void NativeTextfieldWin::UpdateText() {
SetWindowText(text_to_set.c_str());
}
-void NativeTextfieldWin::AppendText(const std::wstring& text) {
+void NativeTextfieldWin::AppendText(const string16& text) {
int text_length = GetWindowTextLength();
::SendMessage(m_hWnd, TBM_SETSEL, true, MAKELPARAM(text_length, text_length));
::SendMessage(m_hWnd, EM_REPLACESEL, false,
reinterpret_cast<LPARAM>(text.c_str()));
}
-std::wstring NativeTextfieldWin::GetSelectedText() const {
+string16 NativeTextfieldWin::GetSelectedText() const {
// Figure out the length of the selection.
long start;
long end;
diff --git a/views/controls/textfield/native_textfield_win.h b/views/controls/textfield/native_textfield_win.h
index aaf7e7c..99b2a3b 100644
--- a/views/controls/textfield/native_textfield_win.h
+++ b/views/controls/textfield/native_textfield_win.h
@@ -40,10 +40,10 @@ class NativeTextfieldWin
void AttachHack();
// Overridden from NativeTextfieldWrapper:
- virtual std::wstring GetText() const;
+ virtual string16 GetText() const;
virtual void UpdateText();
- virtual void AppendText(const std::wstring& text);
- virtual std::wstring GetSelectedText() const;
+ virtual void AppendText(const string16& text);
+ virtual string16 GetSelectedText() const;
virtual void SelectAll();
virtual void ClearSelection();
virtual void UpdateBorder();
diff --git a/views/controls/textfield/native_textfield_wrapper.h b/views/controls/textfield/native_textfield_wrapper.h
index d1aba9a..f11fc33a 100644
--- a/views/controls/textfield/native_textfield_wrapper.h
+++ b/views/controls/textfield/native_textfield_wrapper.h
@@ -5,6 +5,7 @@
#ifndef VIEWS_CONTROLS_TEXTFIELD_NATIVE_TEXTFIELD_WRAPPER_H_
#define VIEWS_CONTROLS_TEXTFIELD_NATIVE_TEXTFIELD_WRAPPER_H_
+#include "base/string16.h"
#include "base/gfx/native_widget_types.h"
namespace views {
@@ -21,17 +22,17 @@ class NativeTextfieldWrapper {
virtual ~NativeTextfieldWrapper() {}
// Gets the text displayed in the wrapped native text field.
- virtual std::wstring GetText() const = 0;
-
+ virtual string16 GetText() const = 0;
+
// Updates the text displayed with the text held by the Textfield.
virtual void UpdateText() = 0;
// Adds the specified text to the text already displayed by the wrapped native
// text field.
- virtual void AppendText(const std::wstring& text) = 0;
+ virtual void AppendText(const string16& text) = 0;
// Gets the text that is selected in the wrapped native text field.
- virtual std::wstring GetSelectedText() const = 0;
+ virtual string16 GetSelectedText() const = 0;
// Selects all the text in the edit. Use this in place of SetSelAll() to
// avoid selecting the "phantom newline" at the end of the edit.
diff --git a/views/controls/textfield/textfield.cc b/views/controls/textfield/textfield.cc
index 486ac19..3887697 100644
--- a/views/controls/textfield/textfield.cc
+++ b/views/controls/textfield/textfield.cc
@@ -81,13 +81,13 @@ bool Textfield::IsMultiLine() const {
return !!(style_ & STYLE_MULTILINE);
}
-void Textfield::SetText(const std::wstring& text) {
+void Textfield::SetText(const string16& text) {
text_ = text;
if (native_wrapper_)
native_wrapper_->UpdateText();
}
-void Textfield::AppendText(const std::wstring& text) {
+void Textfield::AppendText(const string16& text) {
text_ += text;
if (native_wrapper_)
native_wrapper_->AppendText(text);
@@ -98,6 +98,12 @@ void Textfield::SelectAll() {
native_wrapper_->SelectAll();
}
+string16 Textfield::GetSelectedText() const {
+ if (native_wrapper_)
+ return native_wrapper_->GetSelectedText();
+ return string16();
+}
+
void Textfield::ClearSelection() const {
if (native_wrapper_)
native_wrapper_->ClearSelection();
@@ -249,8 +255,16 @@ void Textfield::ViewHierarchyChanged(bool is_add, View* parent, View* child) {
// The native wrapper's lifetime will be managed by the view hierarchy after
// we call AddChildView.
- native_wrapper_ = CreateWrapper();
+ native_wrapper_ = NativeTextfieldWrapper::CreateWrapper(this);
AddChildView(native_wrapper_->GetView());
+ // TODO(beng): Move this initialization to NativeTextfieldWin once it
+ // subclasses NativeControlWin.
+ native_wrapper_->UpdateText();
+ native_wrapper_->UpdateBackgroundColor();
+ native_wrapper_->UpdateReadOnly();
+ native_wrapper_->UpdateFont();
+ native_wrapper_->UpdateEnabled();
+ native_wrapper_->UpdateBorder();
#if defined(OS_WIN)
// TODO(beng): remove this once NativeTextfieldWin subclasses
diff --git a/views/controls/textfield/textfield.h b/views/controls/textfield/textfield.h
index 68f3be5..ecd16169 100644
--- a/views/controls/textfield/textfield.h
+++ b/views/controls/textfield/textfield.h
@@ -5,10 +5,9 @@
#ifndef VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_H_
#define VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_H_
-#include <string>
-
#include "app/gfx/font.h"
#include "base/basictypes.h"
+#include "base/string16.h"
#include "views/view.h"
#include "third_party/skia/include/core/SkColor.h"
@@ -60,7 +59,7 @@ class Textfield : public View {
public:
// This method is called whenever the text in the field changes.
virtual void ContentsChanged(Textfield* sender,
- const std::wstring& new_contents) = 0;
+ 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
@@ -97,11 +96,14 @@ class Textfield : public View {
bool IsMultiLine() const;
// Gets/Sets the text currently displayed in the Textfield.
- const std::wstring& text() const { return text_; }
- void SetText(const std::wstring& text);
+ const string16& text() const { return text_; }
+ void SetText(const string16& text);
// Appends the given string to the previously-existing text in the field.
- void AppendText(const std::wstring& text);
+ void AppendText(const string16& text);
+
+ // Returns the text that is currently selected.
+ string16 GetSelectedText() const;
// Causes the edit field to be fully selected.
void SelectAll();
@@ -201,7 +203,7 @@ class Textfield : public View {
gfx::Font font_;
// The text displayed in the Textfield.
- std::wstring text_;
+ string16 text_;
// True if this Textfield cannot accept input and is read-only.
bool read_only_;