diff options
-rw-r--r-- | app/gfx/canvas_linux.cc | 28 | ||||
-rw-r--r-- | app/gfx/font.h | 5 | ||||
-rw-r--r-- | app/gfx/font_gtk.cc | 27 | ||||
-rw-r--r-- | app/resources/app_resources.grd | 3 | ||||
-rw-r--r-- | chrome/browser/gtk/browser_window_gtk.cc | 5 | ||||
-rw-r--r-- | chrome/browser/gtk/gtk_theme_provider.cc | 7 | ||||
-rw-r--r-- | chrome/browser/views/bookmark_bubble_view.cc | 5 | ||||
-rw-r--r-- | chrome/browser/views/find_bar_view.cc | 13 | ||||
-rw-r--r-- | chrome/browser/views/find_bar_view.h | 2 | ||||
-rw-r--r-- | chrome/chrome.gyp | 2 | ||||
-rw-r--r-- | views/controls/native_control_gtk.cc | 2 | ||||
-rw-r--r-- | views/controls/textfield/native_textfield_gtk.cc | 68 | ||||
-rw-r--r-- | views/controls/textfield/native_textfield_gtk.h | 9 | ||||
-rw-r--r-- | views/controls/textfield/native_textfield_win.cc | 6 | ||||
-rw-r--r-- | views/controls/textfield/native_textfield_win.h | 6 | ||||
-rw-r--r-- | views/controls/textfield/native_textfield_wrapper.h | 9 | ||||
-rw-r--r-- | views/controls/textfield/textfield.cc | 20 | ||||
-rw-r--r-- | views/controls/textfield/textfield.h | 16 | ||||
-rw-r--r-- | views/view_unittest.cc | 40 | ||||
-rw-r--r-- | views/widget/root_view.cc | 10 | ||||
-rw-r--r-- | views/widget/widget_gtk.cc | 4 |
21 files changed, 178 insertions, 109 deletions
diff --git a/app/gfx/canvas_linux.cc b/app/gfx/canvas_linux.cc index 521f54f..7ed1904 100644 --- a/app/gfx/canvas_linux.cc +++ b/app/gfx/canvas_linux.cc @@ -20,32 +20,6 @@ namespace { // DrawStringInt(). static cairo_font_options_t* cairo_font_options = NULL; -// Returns a new pango font, free with pango_font_description_free(). -PangoFontDescription* PangoFontFromGfxFont(const gfx::Font& gfx_font) { - gfx::Font font = gfx_font; // Copy so we can call non-const methods. - PangoFontDescription* pfd = pango_font_description_new(); - pango_font_description_set_family(pfd, WideToUTF8(font.FontName()).c_str()); - pango_font_description_set_size(pfd, font.FontSize() * PANGO_SCALE); - - switch (font.style()) { - case gfx::Font::NORMAL: - // Nothing to do, should already be PANGO_STYLE_NORMAL. - break; - case gfx::Font::BOLD: - pango_font_description_set_weight(pfd, PANGO_WEIGHT_BOLD); - break; - case gfx::Font::ITALIC: - pango_font_description_set_style(pfd, PANGO_STYLE_ITALIC); - break; - case gfx::Font::UNDERLINED: - // TODO(deanm): How to do underlined? Where do we use it? Probably have - // to paint it ourselves, see pango_font_metrics_get_underline_position. - break; - } - - return pfd; -} - // Update |cairo_font_options| based on GtkSettings, allocating it if needed. static void UpdateCairoFontOptions() { if (!cairo_font_options) @@ -156,7 +130,7 @@ static void SetupPangoLayout(PangoLayout* layout, PANGO_WRAP_WORD_CHAR : PANGO_WRAP_WORD); } - PangoFontDescription* desc = PangoFontFromGfxFont(font); + PangoFontDescription* desc = gfx::Font::PangoFontFromGfxFont(font); pango_layout_set_font_description(layout, desc); pango_font_description_free(desc); } diff --git a/app/gfx/font.h b/app/gfx/font.h index ef03d99..e0e075a 100644 --- a/app/gfx/font.h +++ b/app/gfx/font.h @@ -27,6 +27,7 @@ class NSFont; #endif typedef NSFont* NativeFont; #elif defined(OS_LINUX) +typedef struct _PangoFontDescription PangoFontDescription; class SkTypeface; typedef SkTypeface* NativeFont; #else // null port. @@ -130,6 +131,10 @@ class Font { Font& operator=(const Font& other); // Setup a Skia context to use the current typeface void PaintSetup(SkPaint* paint) const; + + // Converts |gfx_font| to a new pango font. Free the returned font with + // pango_font_description_free(). + static PangoFontDescription* PangoFontFromGfxFont(const gfx::Font& gfx_font); #endif private: diff --git a/app/gfx/font_gtk.cc b/app/gfx/font_gtk.cc index 6c5f367..522c568 100644 --- a/app/gfx/font_gtk.cc +++ b/app/gfx/font_gtk.cc @@ -81,4 +81,31 @@ Font::Font() { CopyFont(*default_font_); } +// static +PangoFontDescription* Font::PangoFontFromGfxFont( + const gfx::Font& gfx_font) { + gfx::Font font = gfx_font; // Copy so we can call non-const methods. + PangoFontDescription* pfd = pango_font_description_new(); + pango_font_description_set_family(pfd, WideToUTF8(font.FontName()).c_str()); + pango_font_description_set_size(pfd, font.FontSize() * PANGO_SCALE); + + switch (font.style()) { + case gfx::Font::NORMAL: + // Nothing to do, should already be PANGO_STYLE_NORMAL. + break; + case gfx::Font::BOLD: + pango_font_description_set_weight(pfd, PANGO_WEIGHT_BOLD); + break; + case gfx::Font::ITALIC: + pango_font_description_set_style(pfd, PANGO_STYLE_ITALIC); + break; + case gfx::Font::UNDERLINED: + // TODO(deanm): How to do underlined? Where do we use it? Probably have + // to paint it ourselves, see pango_font_metrics_get_underline_position. + break; + } + + return pfd; +} + } // namespace gfx diff --git a/app/resources/app_resources.grd b/app/resources/app_resources.grd index d24c91e..086ebb4 100644 --- a/app/resources/app_resources.grd +++ b/app/resources/app_resources.grd @@ -36,6 +36,9 @@ <include name="IDR_CLOSE" file="linux_close.png" type="BINDATA" /> <include name="IDR_CLOSE_H" file="linux_close_h.png" type="BINDATA" /> <include name="IDR_CLOSE_P" file="linux_close_p.png" type="BINDATA" /> + <include name="IDR_CLOSE_SA" file="close_sa.png" type="BINDATA" /> + <include name="IDR_CLOSE_SA_H" file="close_sa_h.png" type="BINDATA" /> + <include name="IDR_CLOSE_SA_P" file="close_sa_p.png" type="BINDATA" /> <include name="IDR_RESTORE" file="linux_restore.png" type="BINDATA" /> <include name="IDR_RESTORE_H" file="linux_restore_h.png" type="BINDATA" /> <include name="IDR_RESTORE_P" file="linux_restore_p.png" type="BINDATA" /> diff --git a/chrome/browser/gtk/browser_window_gtk.cc b/chrome/browser/gtk/browser_window_gtk.cc index a91d07b..190cc74 100644 --- a/chrome/browser/gtk/browser_window_gtk.cc +++ b/chrome/browser/gtk/browser_window_gtk.cc @@ -71,6 +71,7 @@ #include "grit/app_resources.h" #include "grit/theme_resources.h" #include "skia/ext/skia_utils.h" +#include "skia/ext/skia_utils_gtk.h" #if defined(OS_CHROMEOS) #include "chrome/browser/chromeos/compact_navigation_bar.h" @@ -479,9 +480,7 @@ GdkCursorType GdkWindowEdgeToGdkCursorType(GdkWindowEdge edge) { } GdkColor SkColorToGdkColor(const SkColor& color) { - GdkColor color_gdk = GDK_COLOR_RGB(SkColorGetR(color), SkColorGetG(color), - SkColorGetB(color)); - return color_gdk; + return skia::SkColorToGdkColor(color); } } // namespace diff --git a/chrome/browser/gtk/gtk_theme_provider.cc b/chrome/browser/gtk/gtk_theme_provider.cc index 8c76c6d..6b37ed5 100644 --- a/chrome/browser/gtk/gtk_theme_provider.cc +++ b/chrome/browser/gtk/gtk_theme_provider.cc @@ -16,6 +16,7 @@ #include "chrome/common/notification_source.h" #include "chrome/common/notification_type.h" #include "grit/theme_resources.h" +#include "skia/ext/skia_utils_gtk.h" #include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkCanvas.h" #include "third_party/skia/include/core/SkColor.h" @@ -139,11 +140,7 @@ bool GtkThemeProvider::UseGtkTheme() { } GdkColor GtkThemeProvider::GetGdkColor(int id) { - SkColor color = GetColor(id); - GdkColor gdkcolor = - GDK_COLOR_RGB(SkColorGetR(color), SkColorGetG(color), - SkColorGetB(color)); - return gdkcolor; + return skia::SkColorToGdkColor(GetColor(id)); } GdkColor GtkThemeProvider::GetBorderColor() { diff --git a/chrome/browser/views/bookmark_bubble_view.cc b/chrome/browser/views/bookmark_bubble_view.cc index 545eb4a6..0ecf1b9 100644 --- a/chrome/browser/views/bookmark_bubble_view.cc +++ b/chrome/browser/views/bookmark_bubble_view.cc @@ -8,6 +8,7 @@ #include "app/l10n_util.h" #include "app/resource_bundle.h" #include "base/keyboard_codes.h" +#include "base/string_util.h" #include "chrome/app/chrome_dll_resource.h" #include "chrome/browser/bookmarks/bookmark_editor.h" #include "chrome/browser/bookmarks/bookmark_model.h" @@ -290,7 +291,7 @@ void BookmarkBubbleView::Init() { layout->AddView( new Label(l10n_util::GetString(IDS_BOOMARK_BUBBLE_TITLE_TEXT))); title_tf_ = new views::Textfield(); - title_tf_->SetText(GetTitle()); + title_tf_->SetText(WideToUTF16(GetTitle())); layout->AddView(title_tf_); layout->AddPaddingRow(0, kRelatedControlSmallVerticalSpacing); @@ -420,7 +421,7 @@ void BookmarkBubbleView::ApplyEdits() { BookmarkModel* model = profile_->GetBookmarkModel(); const BookmarkNode* node = model->GetMostRecentlyAddedNodeForURL(url_); if (node) { - const std::wstring new_title = title_tf_->text(); + const std::wstring new_title = UTF16ToWide(title_tf_->text()); if (new_title != node->GetTitle()) { model->SetTitle(node, new_title); UserMetrics::RecordAction(L"BookmarkBubble_ChangeTitleInBubble", diff --git a/chrome/browser/views/find_bar_view.cc b/chrome/browser/views/find_bar_view.cc index f747b2b..da1acd1 100644 --- a/chrome/browser/views/find_bar_view.cc +++ b/chrome/browser/views/find_bar_view.cc @@ -164,7 +164,7 @@ FindBarView::~FindBarView() { } void FindBarView::SetFindText(const string16& find_text) { - find_text_->SetText(UTF16ToWide(find_text)); + find_text_->SetText(find_text); } void FindBarView::UpdateForResult(const FindNotificationDetails& result, @@ -175,9 +175,9 @@ void FindBarView::UpdateForResult(const FindNotificationDetails& result, // If we don't have any results and something was passed in, then that means // someone pressed F3 while the Find box was closed. In that case we need to // repopulate the Find box with what was passed in. - std::wstring search_string = find_text_->text(); + string16 search_string = find_text_->text(); if (search_string.empty() && !find_text.empty()) { - find_text_->SetText(UTF16ToWide(find_text)); + find_text_->SetText(find_text); find_text_->SelectAll(); } @@ -412,7 +412,7 @@ void FindBarView::ButtonPressed(views::Button* sender) { case FIND_NEXT_TAG: if (!find_text_->text().empty()) { container_->GetFindBarController()->tab_contents()->StartFinding( - WideToUTF16(find_text_->text()), + find_text_->text(), sender->tag() == FIND_NEXT_TAG, false); // Not case sensitive. } @@ -435,7 +435,7 @@ void FindBarView::ButtonPressed(views::Button* sender) { // FindBarView, views::Textfield::Controller implementation: void FindBarView::ContentsChanged(views::Textfield* sender, - const std::wstring& new_contents) { + const string16& new_contents) { FindBarController* controller = container_->GetFindBarController(); DCHECK(controller); // We must guard against a NULL tab_contents, which can happen if the text @@ -449,8 +449,7 @@ void FindBarView::ContentsChanged(views::Textfield* sender, // initiate search (even though old searches might be in progress). if (!new_contents.empty()) { // The last two params here are forward (true) and case sensitive (false). - controller->tab_contents()->StartFinding(WideToUTF16(new_contents), - true, false); + controller->tab_contents()->StartFinding(new_contents, true, false); } else { // The textbox is empty so we reset. true = clear selection on page. controller->tab_contents()->StopFinding(true); diff --git a/chrome/browser/views/find_bar_view.h b/chrome/browser/views/find_bar_view.h index f83b175..8e3a664 100644 --- a/chrome/browser/views/find_bar_view.h +++ b/chrome/browser/views/find_bar_view.h @@ -67,7 +67,7 @@ class FindBarView : public views::View, // Overridden from views::Textfield::Controller: virtual void ContentsChanged(views::Textfield* sender, - const std::wstring& new_contents); + const string16& new_contents); virtual bool HandleKeystroke(views::Textfield* sender, const views::Textfield::Keystroke& key); diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index f8c8485..d174b01 100644 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -4199,6 +4199,7 @@ # on linux, though. 'browser/download/download_manager_unittest.cc', 'browser/views/bookmark_context_menu_test.cc', + 'browser/gtk/options/cookies_view_unittest.cc', # Compact Language Detection (cld) is not supported in linux yet. '../third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/compact_lang_det_unittest_small.cc', ], @@ -4217,6 +4218,7 @@ ['exclude', 'browser/gtk/bookmark_editor_gtk_unittest\\.cc$'], ['exclude', 'browser/gtk/go_button_gtk_unittest\\.cc$'], ['exclude', 'browser/gtk/tabs/tab_renderer_gtk_unittest\\.cc$'], + ['exclude', 'browser/gtk/options/cookies_view_unittest\\.cc$'], ], }], ['OS=="mac"', { 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( - >k_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_; diff --git a/views/view_unittest.cc b/views/view_unittest.cc index 6778135..64b5848 100644 --- a/views/view_unittest.cc +++ b/views/view_unittest.cc @@ -9,6 +9,7 @@ #include "base/clipboard.h" #include "base/keyboard_codes.h" #include "base/message_loop.h" +#include "base/string_util.h" #include "testing/gtest/include/gtest/gtest.h" #include "views/background.h" #include "views/controls/button/checkbox.h" @@ -18,6 +19,7 @@ #include "views/controls/scroll_view.h" #include "views/controls/textfield/textfield.h" #include "views/event.h" +#include "views/focus/accelerator_handler.h" #include "views/focus/view_storage.h" #include "views/view.h" #include "views/views_delegate.h" @@ -26,6 +28,7 @@ #include "views/widget/widget_win.h" #elif defined(OS_LINUX) #include "views/widget/widget_gtk.h" +#include "views/window/window_gtk.h" #endif #include "views/window/dialog_delegate.h" #include "views/window/window.h" @@ -205,7 +208,6 @@ TEST_F(ViewTest, DidChangeBounds) { delete v; } - //////////////////////////////////////////////////////////////////////////////// // AddRemoveNotifications //////////////////////////////////////////////////////////////////////////////// @@ -616,6 +618,42 @@ TEST_F(ViewTest, HitTestMasks) { EXPECT_EQ(root_view, root_view->GetViewForPoint(v2_origin)); } +TEST_F(ViewTest, Textfield) { + const string16 kText = ASCIIToUTF16("Reality is that which, when you stop " + "believing it, doesn't go away."); + const string16 kExtraText = ASCIIToUTF16("Pretty deep, Philip!"); + const string16 kEmptyString; + + Clipboard clipboard; + + Widget* window = CreateWidget(); +#if defined(OS_WIN) + static_cast<WidgetWin*>(window)->Init(NULL, gfx::Rect(0, 0, 100, 100)); +#else + static_cast<WidgetGtk*>(window)->Init(NULL, gfx::Rect(0, 0, 100, 100)); +#endif + RootView* root_view = window->GetRootView(); + + Textfield* textfield = new Textfield(); + root_view->AddChildView(textfield); + + // Test setting, appending text. + textfield->SetText(kText); + EXPECT_EQ(kText, textfield->text()); + textfield->AppendText(kExtraText); + EXPECT_EQ(kText + kExtraText, textfield->text()); + textfield->SetText(string16()); + EXPECT_EQ(kEmptyString, textfield->text()); + + // Test selection related methods. + textfield->SetText(kText); + EXPECT_EQ(kEmptyString, textfield->GetSelectedText()); + textfield->SelectAll(); + EXPECT_EQ(kText, textfield->text()); + textfield->ClearSelection(); + EXPECT_EQ(kEmptyString, textfield->GetSelectedText()); +} + #if defined(OS_WIN) class TestViewsDelegate : public views::ViewsDelegate { public: diff --git a/views/widget/root_view.cc b/views/widget/root_view.cc index 526e6d1..a0d19ca 100644 --- a/views/widget/root_view.cc +++ b/views/widget/root_view.cc @@ -8,6 +8,7 @@ #include "app/drag_drop_types.h" #include "app/gfx/canvas.h" +#include "base/keyboard_codes.h" #include "base/logging.h" #include "base/message_loop.h" #include "views/fill_layout.h" @@ -780,19 +781,14 @@ bool RootView::ProcessKeyEvent(const KeyEvent& event) { bool consumed = false; View* v = GetFocusedView(); -#if defined(OS_WIN) // Special case to handle right-click context menus triggered by the // keyboard. - if (v && v->IsEnabled() && ((event.GetCharacter() == VK_APPS) || - (event.GetCharacter() == VK_F10 && event.IsShiftDown()))) { + if (v && v->IsEnabled() && ((event.GetCharacter() == base::VKEY_APPS) || + (event.GetCharacter() == base::VKEY_F10 && event.IsShiftDown()))) { gfx::Point screen_loc = v->GetKeyboardContextMenuLocation(); v->ShowContextMenu(screen_loc.x(), screen_loc.y(), false); return true; } -#else - // TODO(port): The above block needs the VK_* refactored out. - NOTIMPLEMENTED(); -#endif for (; v && v != this && !consumed; v = v->GetParent()) { consumed = (event.GetType() == Event::ET_KEY_PRESSED) ? diff --git a/views/widget/widget_gtk.cc b/views/widget/widget_gtk.cc index bf086ad..7ab2c31 100644 --- a/views/widget/widget_gtk.cc +++ b/views/widget/widget_gtk.cc @@ -221,7 +221,6 @@ void WidgetGtk::Init(GtkWidget* parent, GDK_POINTER_MOTION_MASK | GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK); - SetRootViewForWidget(widget_, root_view_.get()); MessageLoopForUI::current()->AddObserver(this); @@ -241,8 +240,6 @@ void WidgetGtk::Init(GtkWidget* parent, G_CALLBACK(CallButtonPress), NULL); g_signal_connect(G_OBJECT(window_contents_), "button_release_event", G_CALLBACK(CallButtonRelease), NULL); - g_signal_connect(G_OBJECT(window_contents_), "focus_out_event", - G_CALLBACK(CallFocusOut), NULL); g_signal_connect(G_OBJECT(window_contents_), "grab_broken_event", G_CALLBACK(CallGrabBrokeEvent), NULL); g_signal_connect(G_OBJECT(window_contents_), "grab_notify", @@ -263,7 +260,6 @@ void WidgetGtk::Init(GtkWidget* parent, G_CALLBACK(CallFocusIn), NULL); g_signal_connect(G_OBJECT(widget_), "focus_out_event", G_CALLBACK(CallFocusOut), NULL); - g_signal_connect(G_OBJECT(widget_), "destroy", G_CALLBACK(CallDestroy), NULL); if (transparent_) { |