diff options
author | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-10 18:32:14 +0000 |
---|---|---|
committer | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-10 18:32:14 +0000 |
commit | 6e64343accab0d5b5436645e40a36f5ff20d17ec (patch) | |
tree | cbf33d783e15f8fcd27bd1c27b79046fac0fb4be /app | |
parent | ede729d4d7a1e6a1dc07d58e0fff2ca5cc6e7131 (diff) | |
download | chromium_src-6e64343accab0d5b5436645e40a36f5ff20d17ec.zip chromium_src-6e64343accab0d5b5436645e40a36f5ff20d17ec.tar.gz chromium_src-6e64343accab0d5b5436645e40a36f5ff20d17ec.tar.bz2 |
Final removal of the bad dependency of chrome/common on chrome/browser
Also convert app/gtk_dnd_util.h from a class to a namespace for consistency
with added app/gtk_util.h.
TEST=none
BUG=none
Review URL: http://codereview.chromium.org/669268
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@41177 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app')
-rw-r--r-- | app/app_base.gypi | 4 | ||||
-rw-r--r-- | app/gtk_dnd_util.cc | 120 | ||||
-rw-r--r-- | app/gtk_dnd_util.h | 133 | ||||
-rw-r--r-- | app/gtk_util.cc | 66 | ||||
-rw-r--r-- | app/gtk_util.h | 34 | ||||
-rw-r--r-- | app/os_exchange_data_provider_gtk.cc | 4 |
6 files changed, 232 insertions, 129 deletions
diff --git a/app/app_base.gypi b/app/app_base.gypi index ccf194e..c69593b 100644 --- a/app/app_base.gypi +++ b/app/app_base.gypi @@ -49,6 +49,8 @@ 'gfx/gtk_util.h', 'gtk_dnd_util.cc', 'gtk_dnd_util.h', + 'gtk_util.cc', + 'gtk_util.h', ], }], ], @@ -158,6 +160,8 @@ 'gfx/text_elider.h', 'gtk_dnd_util.cc', 'gtk_dnd_util.h', + 'gtk_util.cc', + 'gtk_util.h', 'l10n_util.cc', 'l10n_util.h', 'l10n_util_mac.h', diff --git a/app/gtk_dnd_util.cc b/app/gtk_dnd_util.cc index 32c5150..4d8694d 100644 --- a/app/gtk_dnd_util.cc +++ b/app/gtk_dnd_util.cc @@ -11,8 +11,53 @@ static const int kBitsPerByte = 8; -// static -GdkAtom GtkDndUtil::GetAtomForTarget(int target) { +namespace { + +void AddTargetToList(GtkTargetList* targets, int target_code) { + switch (target_code) { + case gtk_dnd_util::TEXT_PLAIN: + gtk_target_list_add_text_targets(targets, gtk_dnd_util::TEXT_PLAIN); + break; + + case gtk_dnd_util::TEXT_URI_LIST: + gtk_target_list_add_uri_targets(targets, gtk_dnd_util::TEXT_URI_LIST); + break; + + case gtk_dnd_util::TEXT_HTML: + gtk_target_list_add( + targets, gtk_dnd_util::GetAtomForTarget(gtk_dnd_util::TEXT_PLAIN), + 0, gtk_dnd_util::TEXT_HTML); + break; + + case gtk_dnd_util::NETSCAPE_URL: + gtk_target_list_add(targets, + gtk_dnd_util::GetAtomForTarget(gtk_dnd_util::NETSCAPE_URL), + 0, gtk_dnd_util::NETSCAPE_URL); + break; + + case gtk_dnd_util::CHROME_TAB: + case gtk_dnd_util::CHROME_BOOKMARK_ITEM: + case gtk_dnd_util::CHROME_NAMED_URL: + gtk_target_list_add(targets, gtk_dnd_util::GetAtomForTarget(target_code), + GTK_TARGET_SAME_APP, target_code); + break; + + case gtk_dnd_util::DIRECT_SAVE_FILE: + gtk_target_list_add(targets, + gtk_dnd_util::GetAtomForTarget(gtk_dnd_util::DIRECT_SAVE_FILE), + 0, gtk_dnd_util::DIRECT_SAVE_FILE); + break; + + default: + NOTREACHED() << " Unexpected target code: " << target_code; + } +} + +} // namespace + +namespace gtk_dnd_util { + +GdkAtom GetAtomForTarget(int target) { switch (target) { case CHROME_TAB: static GdkAtom tab_atom = gdk_atom_intern( @@ -66,8 +111,7 @@ GdkAtom GtkDndUtil::GetAtomForTarget(int target) { return NULL; } -// static -GtkTargetList* GtkDndUtil::GetTargetListFromCodeMask(int code_mask) { +GtkTargetList* GetTargetListFromCodeMask(int code_mask) { GtkTargetList* targets = gtk_target_list_new(NULL, 0); for (size_t i = 1; i < INVALID_TARGET; i = i << 1) { @@ -81,16 +125,13 @@ GtkTargetList* GtkDndUtil::GetTargetListFromCodeMask(int code_mask) { return targets; } -// static -void GtkDndUtil::SetSourceTargetListFromCodeMask(GtkWidget* source, - int code_mask) { +void SetSourceTargetListFromCodeMask(GtkWidget* source, int code_mask) { GtkTargetList* targets = GetTargetListFromCodeMask(code_mask); gtk_drag_source_set_target_list(source, targets); gtk_target_list_unref(targets); } -// static -void GtkDndUtil::SetDestTargetList(GtkWidget* dest, const int* target_codes) { +void SetDestTargetList(GtkWidget* dest, const int* target_codes) { GtkTargetList* targets = gtk_target_list_new(NULL, 0); for (size_t i = 0; target_codes[i] != -1; ++i) { @@ -101,48 +142,10 @@ void GtkDndUtil::SetDestTargetList(GtkWidget* dest, const int* target_codes) { gtk_target_list_unref(targets); } -// static -void GtkDndUtil::AddTargetToList(GtkTargetList* targets, int target_code) { - switch (target_code) { - case TEXT_PLAIN: - gtk_target_list_add_text_targets(targets, TEXT_PLAIN); - break; - - case TEXT_URI_LIST: - gtk_target_list_add_uri_targets(targets, TEXT_URI_LIST); - break; - - case TEXT_HTML: - gtk_target_list_add(targets, GetAtomForTarget(TEXT_PLAIN), 0, TEXT_HTML); - break; - - case NETSCAPE_URL: - gtk_target_list_add(targets, GetAtomForTarget(NETSCAPE_URL), 0, - NETSCAPE_URL); - break; - - case CHROME_TAB: - case CHROME_BOOKMARK_ITEM: - case CHROME_NAMED_URL: - gtk_target_list_add(targets, GetAtomForTarget(target_code), - GTK_TARGET_SAME_APP, target_code); - break; - - case DIRECT_SAVE_FILE: - gtk_target_list_add(targets, GetAtomForTarget(DIRECT_SAVE_FILE), 0, - DIRECT_SAVE_FILE); - break; - - default: - NOTREACHED() << " Unexpected target code: " << target_code; - } -} - -// static -void GtkDndUtil::WriteURLWithName(GtkSelectionData* selection_data, - const GURL& url, - const string16& title, - int type) { +void WriteURLWithName(GtkSelectionData* selection_data, + const GURL& url, + const string16& title, + int type) { switch (type) { case TEXT_PLAIN: { gtk_selection_data_set_text(selection_data, url.spec().c_str(), @@ -163,7 +166,7 @@ void GtkDndUtil::WriteURLWithName(GtkSelectionData* selection_data, pickle.WriteString(url.spec()); gtk_selection_data_set( selection_data, - GetAtomForTarget(GtkDndUtil::CHROME_NAMED_URL), + GetAtomForTarget(gtk_dnd_util::CHROME_NAMED_URL), kBitsPerByte, reinterpret_cast<const guchar*>(pickle.data()), pickle.size()); @@ -187,10 +190,9 @@ void GtkDndUtil::WriteURLWithName(GtkSelectionData* selection_data, } } -// static -bool GtkDndUtil::ExtractNamedURL(GtkSelectionData* selection_data, - GURL* url, - string16* title) { +bool ExtractNamedURL(GtkSelectionData* selection_data, + GURL* url, + string16* title) { Pickle data(reinterpret_cast<char*>(selection_data->data), selection_data->length); void* iter = NULL; @@ -209,9 +211,7 @@ bool GtkDndUtil::ExtractNamedURL(GtkSelectionData* selection_data, return true; } -// static -bool GtkDndUtil::ExtractURIList(GtkSelectionData* selection_data, - std::vector<GURL>* urls) { +bool ExtractURIList(GtkSelectionData* selection_data, std::vector<GURL>* urls) { gchar** uris = gtk_selection_data_get_uris(selection_data); if (!uris) return false; @@ -225,3 +225,5 @@ bool GtkDndUtil::ExtractURIList(GtkSelectionData* selection_data, g_strfreev(uris); return true; } + +} // namespace gtk_dnd_util diff --git a/app/gtk_dnd_util.h b/app/gtk_dnd_util.h index 2c323e9..4471174 100644 --- a/app/gtk_dnd_util.h +++ b/app/gtk_dnd_util.h @@ -6,80 +6,77 @@ #define APP_GTK_DND_UTIL_H_ #include <gtk/gtk.h> + #include <vector> #include "base/string16.h" class GURL; -class GtkDndUtil { - public: - // Registry of all internal int codes for drag and drop. - enum { - // Intra-application types. - CHROME_TAB = 1 << 0, - CHROME_BOOKMARK_ITEM = 1 << 1, - CHROME_WEBDROP_FILE_CONTENTS = 1 << 2, - CHROME_NAMED_URL = 1 << 3, - - // Standard types. - TEXT_PLAIN = 1 << 4, - TEXT_URI_LIST = 1 << 5, - TEXT_HTML = 1 << 6, - - // Other types. NETSCAPE_URL is provided for compatibility with other - // apps. - NETSCAPE_URL = 1 << 7, - - // Used for drag-out download. - TEXT_PLAIN_NO_CHARSET = 1 << 8, - DIRECT_SAVE_FILE = 1 << 9, - - INVALID_TARGET = 1 << 10, - }; - - // Get the atom for a given target (of the above enum type). Will return NULL - // for non-custom targets, such as CHROME_TEXT_PLAIN. - static GdkAtom GetAtomForTarget(int target); - - // Creates a target list from the given mask. The mask should be an OR of - // CHROME_* values. The target list is returned with ref count 1; the caller - // is responsible for calling gtk_target_list_unref() when it is no longer - // needed. - // Since the MIME type for WEBDROP_FILE_CONTENTS depends on the file's - // contents, that flag is ignored by this function. It is the responsibility - // of the client code to do the right thing. - static GtkTargetList* GetTargetListFromCodeMask(int code_mask); - - // Set the drag target list for |source| with the target list that - // corresponds to |code_mask|. - static void SetSourceTargetListFromCodeMask(GtkWidget* source, int code_mask); - - // Set the accepted targets list for |dest|. The |target_codes| array should - // be sorted in preference order and should be terminated with -1. - static void SetDestTargetList(GtkWidget* dest, const int* target_codes); - - // Write a URL to the selection in the given type. - static void WriteURLWithName(GtkSelectionData* selection_data, - const GURL& url, - const string16& title, - int type); - - // Extracts data of type CHROME_NAMED_URL from |selection_data| into - // |url| and |title|. Returns true if the url/title were safely extracted - // and the url is valid. - static bool ExtractNamedURL(GtkSelectionData* selection_data, - GURL* url, - string16* title); - - // Extracts data of type TEXT_URI_LIST from |selection_data| into |urls|. - static bool ExtractURIList(GtkSelectionData* selection_data, - std::vector<GURL>* urls); - - private: - GtkDndUtil(); - - static void AddTargetToList(GtkTargetList* targets, int target_code); +namespace gtk_dnd_util { + +// Registry of all internal int codes for drag and drop. +enum { + // Intra-application types. + CHROME_TAB = 1 << 0, + CHROME_BOOKMARK_ITEM = 1 << 1, + CHROME_WEBDROP_FILE_CONTENTS = 1 << 2, + CHROME_NAMED_URL = 1 << 3, + + // Standard types. + TEXT_PLAIN = 1 << 4, + TEXT_URI_LIST = 1 << 5, + TEXT_HTML = 1 << 6, + + // Other types. NETSCAPE_URL is provided for compatibility with other + // apps. + NETSCAPE_URL = 1 << 7, + + // Used for drag-out download. + TEXT_PLAIN_NO_CHARSET = 1 << 8, + DIRECT_SAVE_FILE = 1 << 9, + + INVALID_TARGET = 1 << 10, }; +// Get the atom for a given target (of the above enum type). Will return NULL +// for non-custom targets, such as CHROME_TEXT_PLAIN. +GdkAtom GetAtomForTarget(int target); + +// Creates a target list from the given mask. The mask should be an OR of +// CHROME_* values. The target list is returned with ref count 1; the caller +// is responsible for calling gtk_target_list_unref() when it is no longer +// needed. +// Since the MIME type for WEBDROP_FILE_CONTENTS depends on the file's +// contents, that flag is ignored by this function. It is the responsibility +// of the client code to do the right thing. +GtkTargetList* GetTargetListFromCodeMask(int code_mask); + +// Set the drag target list for |source| with the target list that +// corresponds to |code_mask|. +void SetSourceTargetListFromCodeMask(GtkWidget* source, int code_mask); + +// Set the accepted targets list for |dest|. The |target_codes| array should +// be sorted in preference order and should be terminated with -1. +void SetDestTargetList(GtkWidget* dest, const int* target_codes); + +// Write a URL to the selection in the given type. +void WriteURLWithName(GtkSelectionData* selection_data, + const GURL& url, + const string16& title, + int type); + +// Extracts data of type CHROME_NAMED_URL from |selection_data| into +// |url| and |title|. Returns true if the url/title were safely extracted +// and the url is valid. +bool ExtractNamedURL(GtkSelectionData* selection_data, + GURL* url, + string16* title); + +// Extracts data of type TEXT_URI_LIST from |selection_data| into |urls|. +bool ExtractURIList(GtkSelectionData* selection_data, + std::vector<GURL>* urls); + +} // namespace gtk_dnd_util + #endif // APP_GTK_DND_UTIL_H_ diff --git a/app/gtk_util.cc b/app/gtk_util.cc new file mode 100644 index 0000000..b2aab1a --- /dev/null +++ b/app/gtk_util.cc @@ -0,0 +1,66 @@ +// Copyright (c) 2010 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 "app/gtk_util.h" + +#include <gtk/gtk.h> + +#include "app/l10n_util.h" +#include "base/linux_util.h" +#include "base/logging.h" +#include "base/string_util.h" + +namespace gtk_util { + +void GetWidgetSizeFromResources( + GtkWidget* widget, int width_chars, int height_lines, + int* width, int* height) { + DCHECK(GTK_WIDGET_REALIZED(widget)) + << " widget must be realized to compute font metrics correctly"; + + double chars = 0; + if (width) + StringToDouble(l10n_util::GetStringUTF8(width_chars), &chars); + + double lines = 0; + if (height) + StringToDouble(l10n_util::GetStringUTF8(height_lines), &lines); + + GetWidgetSizeFromCharacters(widget, chars, lines, width, height); +} + +void GetWidgetSizeFromCharacters( + GtkWidget* widget, double width_chars, double height_lines, + int* width, int* height) { + DCHECK(GTK_WIDGET_REALIZED(widget)) + << " widget must be realized to compute font metrics correctly"; + PangoContext* context = gtk_widget_create_pango_context(widget); + PangoFontMetrics* metrics = pango_context_get_metrics(context, + widget->style->font_desc, pango_context_get_language(context)); + if (width) { + *width = static_cast<int>( + pango_font_metrics_get_approximate_char_width(metrics) * + width_chars / PANGO_SCALE); + } + if (height) { + *height = static_cast<int>( + (pango_font_metrics_get_ascent(metrics) + + pango_font_metrics_get_descent(metrics)) * + height_lines / PANGO_SCALE); + } + pango_font_metrics_unref(metrics); + g_object_unref(context); +} + +void ApplyMessageDialogQuirks(GtkWidget* dialog) { + if (gtk_window_get_modal(GTK_WINDOW(dialog))) { + // Work around a KDE 3 window manager bug. + scoped_ptr<base::EnvironmentVariableGetter> env( + base::EnvironmentVariableGetter::Create()); + if (base::DESKTOP_ENVIRONMENT_KDE3 == GetDesktopEnvironment(env.get())) + gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), FALSE); + } +} + +} // namespace gtk_util diff --git a/app/gtk_util.h b/app/gtk_util.h new file mode 100644 index 0000000..22ab7d2 --- /dev/null +++ b/app/gtk_util.h @@ -0,0 +1,34 @@ +// Copyright (c) 2010 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 APP_GTK_UTIL_H_ +#define APP_GTK_UTIL_H_ + +typedef struct _GtkWidget GtkWidget; + +namespace gtk_util { + +// Calculates the size of given widget based on the size specified in +// number of characters/lines (in locale specific resource file) and +// font metrics. +// NOTE: Make sure to realize |widget| before using this method, or a +// default font size will be used instead of the actual font size. +void GetWidgetSizeFromResources( + GtkWidget* widget, int width_chars, int height_lines, + int* width, int* height); + +// As above, but uses number of characters/lines directly rather than looking +// up a resource. +void GetWidgetSizeFromCharacters( + GtkWidget* widget, double width_chars, double height_lines, + int* width, int* height); + +// A helper function for gtk_message_dialog_new() to work around a KDE 3 +// window manager bugs. You should always call it after creating a dialog +// with gtk_message_dialog_new. +void ApplyMessageDialogQuirks(GtkWidget* dialog); + +} // namespace gtk_util + +#endif // APP_GTK_UTIL_H_ diff --git a/app/os_exchange_data_provider_gtk.cc b/app/os_exchange_data_provider_gtk.cc index db4c249..6cc3899b 100644 --- a/app/os_exchange_data_provider_gtk.cc +++ b/app/os_exchange_data_provider_gtk.cc @@ -54,7 +54,7 @@ GtkTargetList* OSExchangeDataProviderGtk::GetTargetList() const { gtk_target_list_add_uri_targets(targets, OSExchangeData::URL); gtk_target_list_add( targets, - GtkDndUtil::GetAtomForTarget(GtkDndUtil::CHROME_NAMED_URL), + gtk_dnd_util::GetAtomForTarget(gtk_dnd_util::CHROME_NAMED_URL), 0, OSExchangeData::URL); } @@ -88,7 +88,7 @@ void OSExchangeDataProviderGtk::WriteFormatToSelection( pickle.WriteString(url_.spec()); gtk_selection_data_set( selection, - GtkDndUtil::GetAtomForTarget(GtkDndUtil::CHROME_NAMED_URL), + gtk_dnd_util::GetAtomForTarget(gtk_dnd_util::CHROME_NAMED_URL), 8, reinterpret_cast<const guchar*>(pickle.data()), pickle.size()); |