diff options
author | erg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-22 22:26:53 +0000 |
---|---|---|
committer | erg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-22 22:26:53 +0000 |
commit | 62cfbad7377b4db2497fec6e88821a36a1b767fe (patch) | |
tree | c7be44ea69ddb94285d7a779ade049411fb004e1 /ui/base | |
parent | 078ed5486f50579a03463961738f6d57759d10ce (diff) | |
download | chromium_src-62cfbad7377b4db2497fec6e88821a36a1b767fe.zip chromium_src-62cfbad7377b4db2497fec6e88821a36a1b767fe.tar.gz chromium_src-62cfbad7377b4db2497fec6e88821a36a1b767fe.tar.bz2 |
(shlib failure) Revert 97750 - content: Move render_widget_host_view_gtk to content/
This also moves some other files:
- OwnedWidgetGtk now goes in ui/base/gtk/
- TruncateString moved from l10n_util:: to ui::
- GtkIMContextWrapper has part of its code split into chrome/ (IDC using code goes in RenderViewContextMenu) and the rest go in content/ (gtk using code goes with GtkIMContextWrapper).
- gtk_key_bindings_handler[_unittest] now goes in content, as it's a utility class to RenderWidgetHostGtk.
BUG=93804
TEST=existing unit tests
Review URL: http://codereview.chromium.org/7669040
TBR=erg@google.com
Review URL: http://codereview.chromium.org/7708017
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@97756 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/base')
-rw-r--r-- | ui/base/gtk/owned_widget_gtk.cc | 41 | ||||
-rw-r--r-- | ui/base/gtk/owned_widget_gtk.h | 91 | ||||
-rw-r--r-- | ui/base/l10n/l10n_util.cc | 63 | ||||
-rw-r--r-- | ui/base/l10n/l10n_util.h | 7 | ||||
-rw-r--r-- | ui/base/l10n/l10n_util_unittest.cc | 30 | ||||
-rw-r--r-- | ui/base/text/text_elider.cc | 66 | ||||
-rw-r--r-- | ui/base/text/text_elider.h | 6 | ||||
-rw-r--r-- | ui/base/text/text_elider_unittest.cc | 27 |
8 files changed, 100 insertions, 231 deletions
diff --git a/ui/base/gtk/owned_widget_gtk.cc b/ui/base/gtk/owned_widget_gtk.cc deleted file mode 100644 index 51ee1cc..0000000 --- a/ui/base/gtk/owned_widget_gtk.cc +++ /dev/null @@ -1,41 +0,0 @@ -// 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. - -#include "ui/base/gtk/owned_widget_gtk.h" - -#include <gtk/gtk.h> - -#include "base/logging.h" - -OwnedWidgetGtk::~OwnedWidgetGtk() { - Destroy(); -} - -void OwnedWidgetGtk::Own(GtkWidget* widget) { - if (!widget) - return; - - DCHECK(!widget_); - // We want to make sure that Own() was called properly, right after the - // widget was created. There should be a floating reference. - DCHECK(g_object_is_floating(widget)); - - // Sink the floating reference, we should now own this reference. - g_object_ref_sink(widget); - widget_ = widget; -} - -void OwnedWidgetGtk::Destroy() { - if (!widget_) - return; - - GtkWidget* widget = widget_; - widget_ = NULL; - gtk_widget_destroy(widget); - - DCHECK(!g_object_is_floating(widget)); - // NOTE: Assumes some implementation details about glib internals. - DCHECK_EQ(G_OBJECT(widget)->ref_count, 1U); - g_object_unref(widget); -} diff --git a/ui/base/gtk/owned_widget_gtk.h b/ui/base/gtk/owned_widget_gtk.h deleted file mode 100644 index d98604e..0000000 --- a/ui/base/gtk/owned_widget_gtk.h +++ /dev/null @@ -1,91 +0,0 @@ -// 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. - -// This class assists you in dealing with a specific situation when managing -// ownership between a C++ object and a GTK widget. It is common to have a -// C++ object which encapsulates a GtkWidget, and that widget is exposed from -// the object for use outside of the class. In this situation, you commonly -// want the GtkWidget's lifetime to match its C++ object's lifetime. Using an -// OwnedWigetGtk will take ownership over the initial reference of the -// GtkWidget, so that it is "owned" by the C++ object. Example usage: -// -// class FooViewGtk() { -// public: -// FooViewGtk() { } -// ~FooViewGtk() { } -// void Init() { vbox_.Own(gtk_vbox_new()); } -// GtkWidget* widget() { return vbox_.get() }; // Host my widget! -// private: -// OwnedWidgetGtk vbox_; -// }; -// -// This design will ensure that the widget stays alive from the call to Own() -// until the call to Destroy(). -// -// - Details of the problem and OwnedWidgetGtk's solution: -// In order to make passing ownership more convenient for newly created -// widgets, GTK has a concept of a "floating" reference. All GtkObjects (and -// thus GtkWidgets) inherit from GInitiallyUnowned. When they are created, the -// object starts with a reference count of 1, but has its floating flag set. -// When it is put into a container for the first time, that container will -// "sink" the floating reference, and the count will still be 1. Now the -// container owns the widget, and if we remove the widget from the container, -// the widget is destroyed. This style of ownership often causes problems when -// you have an object encapsulating the widget. If we just use a raw -// GtkObject* with no specific ownership management, we push the widget's -// ownership onto the user of the class. Now the C++ object can't depend on -// the widget being valid, since it doesn't manage its lifetime. If the widget -// was removed from a container, removing its only reference, it would be -// destroyed (from the C++ object's perspective) unexpectedly destroyed. The -// solution is fairly simple, make sure that the C++ object owns the widget, -// and thus it is also responsible for destroying it. This boils down to: -// GtkWidget* widget = gtk_widget_new(); -// g_object_ref_sink(widget); // Claim the initial floating reference. -// ... -// gtk_destroy_widget(widget); // Ask all code to destroy their references. -// g_object_unref(widget); // Destroy the initial reference we had claimed. - -#ifndef UI_BASE_GTK_OWNED_WIDGET_GTK_H_ -#define UI_BASE_GTK_OWNED_WIDGET_GTK_H_ -#pragma once - -#include "base/basictypes.h" - -typedef struct _GtkWidget GtkWidget; - -class OwnedWidgetGtk { - public: - // Create an instance that isn't managing any ownership. - OwnedWidgetGtk() : widget_(NULL) { } - // Create an instance that owns |widget|. - explicit OwnedWidgetGtk(GtkWidget* widget) : widget_(NULL) { Own(widget); } - - ~OwnedWidgetGtk(); - - // Return the currently owned widget, or NULL if no widget is owned. - GtkWidget* get() const { return widget_; } - GtkWidget* operator->() const { return widget_; } - - // Takes ownership of a widget, by taking the initial floating reference of - // the GtkWidget. It is expected that Own() is called right after the widget - // has been created, and before any other references to the widget might have - // been added. It is valid to never call Own(), in which case Destroy() will - // do nothing. If Own() has been called, you must explicitly call Destroy(). - void Own(GtkWidget* widget); - - // You may call Destroy() after you have called Own(). Calling Destroy() - // will call gtk_widget_destroy(), and drop our reference to the widget. - // Destroy() is also called in this object's destructor. - // After a call to Destroy(), you may call Own() again. NOTE: It is expected - // that after gtk_widget_destroy we will be holding the only reference left - // on the object. We assert this in debug mode to help catch any leaks. - void Destroy(); - - private: - GtkWidget* widget_; - - DISALLOW_COPY_AND_ASSIGN(OwnedWidgetGtk); -}; - -#endif // UI_BASE_GTK_OWNED_WIDGET_GTK_H_ diff --git a/ui/base/l10n/l10n_util.cc b/ui/base/l10n/l10n_util.cc index cabda81..ae7e710 100644 --- a/ui/base/l10n/l10n_util.cc +++ b/ui/base/l10n/l10n_util.cc @@ -741,6 +741,69 @@ string16 GetStringFUTF16Int(int message_id, int64 a) { return GetStringFUTF16(message_id, UTF8ToUTF16(base::Int64ToString(a))); } +string16 TruncateString(const string16& string, size_t length) { + if (string.size() <= length) + // String fits, return it. + return string; + + if (length == 0) { + // No room for the elide string, return an empty string. + return string16(); + } + size_t max = length - 1; + + // Added to the end of strings that are too big. + static const char16 kElideString[] = { 0x2026, 0 }; + + if (max == 0) { + // Just enough room for the elide string. + return kElideString; + } + + // Use a line iterator to find the first boundary. + UErrorCode status = U_ZERO_ERROR; + scoped_ptr<icu::RuleBasedBreakIterator> bi( + static_cast<icu::RuleBasedBreakIterator*>( + icu::RuleBasedBreakIterator::createLineInstance( + icu::Locale::getDefault(), status))); + if (U_FAILURE(status)) + return string.substr(0, max) + kElideString; + bi->setText(string.c_str()); + int32_t index = bi->preceding(static_cast<int32_t>(max)); + if (index == icu::BreakIterator::DONE) { + index = static_cast<int32_t>(max); + } else { + // Found a valid break (may be the beginning of the string). Now use + // a character iterator to find the previous non-whitespace character. + icu::StringCharacterIterator char_iterator(string.c_str()); + if (index == 0) { + // No valid line breaks. Start at the end again. This ensures we break + // on a valid character boundary. + index = static_cast<int32_t>(max); + } + char_iterator.setIndex(index); + while (char_iterator.hasPrevious()) { + char_iterator.previous(); + if (!(u_isspace(char_iterator.current()) || + u_charType(char_iterator.current()) == U_CONTROL_CHAR || + u_charType(char_iterator.current()) == U_NON_SPACING_MARK)) { + // Not a whitespace character. Advance the iterator so that we + // include the current character in the truncated string. + char_iterator.next(); + break; + } + } + if (char_iterator.hasPrevious()) { + // Found a valid break point. + index = char_iterator.getIndex(); + } else { + // String has leading whitespace, return the elide string. + return kElideString; + } + } + return string.substr(0, index) + kElideString; +} + // Compares the character data stored in two different string16 strings by // specified Collator instance. UCollationResult CompareString16WithCollator(const icu::Collator* collator, diff --git a/ui/base/l10n/l10n_util.h b/ui/base/l10n/l10n_util.h index 48ec68a..8fd0240 100644 --- a/ui/base/l10n/l10n_util.h +++ b/ui/base/l10n/l10n_util.h @@ -131,6 +131,13 @@ UI_EXPORT string16 GetStringFUTF16(int message_id, UI_EXPORT string16 GetStringFUTF16Int(int message_id, int a); string16 GetStringFUTF16Int(int message_id, int64 a); +// Truncates the string to length characters. This breaks the string at +// the first word break before length, adding the horizontal ellipsis +// character (unicode character 0x2026) to render ... +// The supplied string is returned if the string has length characters or +// less. +UI_EXPORT string16 TruncateString(const string16& string, size_t length); + // In place sorting of string16 strings using collation rules for |locale|. UI_EXPORT void SortStrings16(const std::string& locale, std::vector<string16>* strings); diff --git a/ui/base/l10n/l10n_util_unittest.cc b/ui/base/l10n/l10n_util_unittest.cc index 778476c..dda3efe 100644 --- a/ui/base/l10n/l10n_util_unittest.cc +++ b/ui/base/l10n/l10n_util_unittest.cc @@ -65,6 +65,36 @@ TEST_F(L10nUtilTest, DISABLED_GetString) { } #endif // defined(OS_WIN) +TEST_F(L10nUtilTest, TruncateString) { + string16 string = ASCIIToUTF16("foooooey bxxxar baz"); + + // Make sure it doesn't modify the string if length > string length. + EXPECT_EQ(string, l10n_util::TruncateString(string, 100)); + + // Test no characters. + EXPECT_EQ(L"", UTF16ToWide(l10n_util::TruncateString(string, 0))); + + // Test 1 character. + EXPECT_EQ(L"\x2026", UTF16ToWide(l10n_util::TruncateString(string, 1))); + + // Test adds ... at right spot when there is enough room to break at a + // word boundary. + EXPECT_EQ(L"foooooey\x2026", + UTF16ToWide(l10n_util::TruncateString(string, 14))); + + // Test adds ... at right spot when there is not enough space in first word. + EXPECT_EQ(L"f\x2026", UTF16ToWide(l10n_util::TruncateString(string, 2))); + + // Test adds ... at right spot when there is not enough room to break at a + // word boundary. + EXPECT_EQ(L"foooooey\x2026", + UTF16ToWide(l10n_util::TruncateString(string, 11))); + + // Test completely truncates string if break is on initial whitespace. + EXPECT_EQ(L"\x2026", + UTF16ToWide(l10n_util::TruncateString(ASCIIToUTF16(" "), 2))); +} + void SetICUDefaultLocale(const std::string& locale_string) { icu::Locale locale(locale_string.c_str()); UErrorCode error_code = U_ZERO_ERROR; diff --git a/ui/base/text/text_elider.cc b/ui/base/text/text_elider.cc index 8cbfb3c..1a4b79d 100644 --- a/ui/base/text/text_elider.cc +++ b/ui/base/text/text_elider.cc @@ -10,7 +10,6 @@ #include "base/i18n/break_iterator.h" #include "base/i18n/char_iterator.h" #include "base/i18n/rtl.h" -#include "base/scoped_ptr.h" #include "base/string_split.h" #include "base/string_util.h" #include "base/sys_string_conversions.h" @@ -20,8 +19,6 @@ #include "net/base/net_util.h" #include "net/base/registry_controlled_domain.h" #include "ui/gfx/font.h" -#include "unicode/rbbi.h" -#include "unicode/uloc.h" namespace ui { @@ -676,67 +673,4 @@ bool ElideRectangleString(const string16& input, size_t max_rows, return rect.Finalize(); } -string16 TruncateString(const string16& string, size_t length) { - if (string.size() <= length) - // String fits, return it. - return string; - - if (length == 0) { - // No room for the elide string, return an empty string. - return string16(); - } - size_t max = length - 1; - - // Added to the end of strings that are too big. - static const char16 kElideString[] = { 0x2026, 0 }; - - if (max == 0) { - // Just enough room for the elide string. - return kElideString; - } - - // Use a line iterator to find the first boundary. - UErrorCode status = U_ZERO_ERROR; - scoped_ptr<icu::RuleBasedBreakIterator> bi( - static_cast<icu::RuleBasedBreakIterator*>( - icu::RuleBasedBreakIterator::createLineInstance( - icu::Locale::getDefault(), status))); - if (U_FAILURE(status)) - return string.substr(0, max) + kElideString; - bi->setText(string.c_str()); - int32_t index = bi->preceding(static_cast<int32_t>(max)); - if (index == icu::BreakIterator::DONE) { - index = static_cast<int32_t>(max); - } else { - // Found a valid break (may be the beginning of the string). Now use - // a character iterator to find the previous non-whitespace character. - icu::StringCharacterIterator char_iterator(string.c_str()); - if (index == 0) { - // No valid line breaks. Start at the end again. This ensures we break - // on a valid character boundary. - index = static_cast<int32_t>(max); - } - char_iterator.setIndex(index); - while (char_iterator.hasPrevious()) { - char_iterator.previous(); - if (!(u_isspace(char_iterator.current()) || - u_charType(char_iterator.current()) == U_CONTROL_CHAR || - u_charType(char_iterator.current()) == U_NON_SPACING_MARK)) { - // Not a whitespace character. Advance the iterator so that we - // include the current character in the truncated string. - char_iterator.next(); - break; - } - } - if (char_iterator.hasPrevious()) { - // Found a valid break point. - index = char_iterator.getIndex(); - } else { - // String has leading whitespace, return the elide string. - return kElideString; - } - } - return string.substr(0, index) + kElideString; -} - } // namespace ui diff --git a/ui/base/text/text_elider.h b/ui/base/text/text_elider.h index eb4f7a4..fe2a141 100644 --- a/ui/base/text/text_elider.h +++ b/ui/base/text/text_elider.h @@ -119,12 +119,6 @@ UI_EXPORT bool ElideRectangleString(const string16& input, size_t max_rows, size_t max_cols, bool strict, string16* output); -// Truncates the string to length characters. This breaks the string at -// the first word break before length, adding the horizontal ellipsis -// character (unicode character 0x2026) to render ... -// The supplied string is returned if the string has length characters or -// less. -UI_EXPORT string16 TruncateString(const string16& string, size_t length); } // namespace ui diff --git a/ui/base/text/text_elider_unittest.cc b/ui/base/text/text_elider_unittest.cc index 60ab1f7..0ff70e4 100644 --- a/ui/base/text/text_elider_unittest.cc +++ b/ui/base/text/text_elider_unittest.cc @@ -547,31 +547,4 @@ TEST(TextEliderTest, ElideRectangleWide32) { EXPECT_EQ(out, output); } -TEST(TextEliderTest, TruncateString) { - string16 string = ASCIIToUTF16("foooooey bxxxar baz"); - - // Make sure it doesn't modify the string if length > string length. - EXPECT_EQ(string, ui::TruncateString(string, 100)); - - // Test no characters. - EXPECT_EQ(L"", UTF16ToWide(ui::TruncateString(string, 0))); - - // Test 1 character. - EXPECT_EQ(L"\x2026", UTF16ToWide(ui::TruncateString(string, 1))); - - // Test adds ... at right spot when there is enough room to break at a - // word boundary. - EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(ui::TruncateString(string, 14))); - - // Test adds ... at right spot when there is not enough space in first word. - EXPECT_EQ(L"f\x2026", UTF16ToWide(ui::TruncateString(string, 2))); - - // Test adds ... at right spot when there is not enough room to break at a - // word boundary. - EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(ui::TruncateString(string, 11))); - - // Test completely truncates string if break is on initial whitespace. - EXPECT_EQ(L"\x2026", UTF16ToWide(ui::TruncateString(ASCIIToUTF16(" "), 2))); -} - } // namespace ui |