diff options
author | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-28 19:37:29 +0000 |
---|---|---|
committer | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-28 19:37:29 +0000 |
commit | 7110c24868971986f9e5cbd22a8dd1e87bda021f (patch) | |
tree | 0d650181d24330c4f72be140224af4a17fed4b51 | |
parent | cf34891f6a2391419f416356350a542aa4c1cc0f (diff) | |
download | chromium_src-7110c24868971986f9e5cbd22a8dd1e87bda021f.zip chromium_src-7110c24868971986f9e5cbd22a8dd1e87bda021f.tar.gz chromium_src-7110c24868971986f9e5cbd22a8dd1e87bda021f.tar.bz2 |
Cleanup: Get rid of app/gtk_util.h.
* Most functions moved to chrome/browser/gtk/gtk_util.h because they're only
used within chrome/browser.
* BGRAToRGBA() is used within chrome/ and app/ so moved it to base/gtk_util.h.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/4170002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@64295 0039d316-1c4b-4281-b951-d872f2087c98
23 files changed, 126 insertions, 176 deletions
diff --git a/app/app_base.gypi b/app/app_base.gypi index 2c04d38..e19c8c6 100644 --- a/app/app_base.gypi +++ b/app/app_base.gypi @@ -44,8 +44,6 @@ 'gtk_signal.h', 'gtk_signal_registrar.cc', 'gtk_signal_registrar.h', - 'gtk_util.cc', - 'gtk_util.h', 'x11_util.cc', 'x11_util.h', 'x11_util_internal.h', @@ -143,8 +141,6 @@ 'gtk_signal.h', 'gtk_signal_registrar.cc', 'gtk_signal_registrar.h', - 'gtk_util.cc', - 'gtk_util.h', 'keyboard_code_conversion.cc', 'keyboard_code_conversion.h', 'keyboard_code_conversion_gtk.cc', diff --git a/app/clipboard/clipboard_linux.cc b/app/clipboard/clipboard_linux.cc index 7fa633e..19fbfc5 100644 --- a/app/clipboard/clipboard_linux.cc +++ b/app/clipboard/clipboard_linux.cc @@ -11,9 +11,9 @@ #include <utility> #include "base/file_path.h" +#include "base/gtk_util.h" #include "base/logging.h" #include "base/scoped_ptr.h" -#include "app/gtk_util.h" #include "base/utf_string_conversions.h" #include "gfx/size.h" diff --git a/app/gtk_util.cc b/app/gtk_util.cc deleted file mode 100644 index e56656f..0000000 --- a/app/gtk_util.cc +++ /dev/null @@ -1,107 +0,0 @@ -// 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/environment.h" -#include "base/logging.h" -#include "base/string_number_conversions.h" -#include "base/nix/xdg_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) - base::StringToDouble(l10n_util::GetStringUTF8(width_chars), &chars); - - double lines = 0; - if (height) - base::StringToDouble(l10n_util::GetStringUTF8(height_lines), &lines); - - GetWidgetSizeFromCharacters(widget, chars, lines, width, height); -} - -uint8_t* BGRAToRGBA(const uint8_t* pixels, int width, int height, int stride) { - if (stride == 0) - stride = width * 4; - - uint8_t* new_pixels = static_cast<uint8_t*>(malloc(height * stride)); - - // We have to copy the pixels and swap from BGRA to RGBA. - for (int i = 0; i < height; ++i) { - for (int j = 0; j < width; ++j) { - int idx = i * stride + j * 4; - new_pixels[idx] = pixels[idx + 2]; - new_pixels[idx + 1] = pixels[idx + 1]; - new_pixels[idx + 2] = pixels[idx]; - new_pixels[idx + 3] = pixels[idx + 3]; - } - } - - return new_pixels; -} - -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); -} - -int GetCharacterWidthForPixels(GtkWidget* widget, int pixel_width) { - 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)); - - // This technique (max of char and digit widths) matches the code in - // gtklabel.c. - int char_width = pixel_width * PANGO_SCALE / - std::max(pango_font_metrics_get_approximate_char_width(metrics), - pango_font_metrics_get_approximate_digit_width(metrics)); - - pango_font_metrics_unref(metrics); - g_object_unref(context); - - return char_width; -} - -void ApplyMessageDialogQuirks(GtkWidget* dialog) { - if (gtk_window_get_modal(GTK_WINDOW(dialog))) { - // Work around a KDE 3 window manager bug. - scoped_ptr<base::Environment> env(base::Environment::Create()); - if (base::nix::DESKTOP_ENVIRONMENT_KDE3 == - base::nix::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 deleted file mode 100644 index cbb91e9..0000000 --- a/app/gtk_util.h +++ /dev/null @@ -1,46 +0,0 @@ -// 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_ -#pragma once - -#include <stdint.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); - -// Returns the approximate number of characters that can horizontally -// fit in |pixel_width| pixels. -int GetCharacterWidthForPixels(GtkWidget* widget, int pixel_width); - -// 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); - -// Makes a copy of |pixels| with the ordering changed from BGRA to RGBA. -// The caller is responsible for free()ing the data. If |stride| is 0, -// it's assumed to be 4 * |width|. -uint8_t* BGRAToRGBA(const uint8_t* pixels, int width, int height, int stride); - -} // namespace gtk_util - -#endif // APP_GTK_UTIL_H_ diff --git a/base/gtk_util.cc b/base/gtk_util.cc index 79fc485..b526aca 100644 --- a/base/gtk_util.cc +++ b/base/gtk_util.cc @@ -4,6 +4,8 @@ #include "base/gtk_util.h" +#include <stdlib.h> + namespace gtk_util { namespace { @@ -46,4 +48,24 @@ std::string RemoveWindowsStyleAccelerators(const std::string& label) { return ConvertAmperstandsTo(label, ""); } +uint8_t* BGRAToRGBA(const uint8_t* pixels, int width, int height, int stride) { + if (stride == 0) + stride = width * 4; + + uint8_t* new_pixels = static_cast<uint8_t*>(malloc(height * stride)); + + // We have to copy the pixels and swap from BGRA to RGBA. + for (int i = 0; i < height; ++i) { + for (int j = 0; j < width; ++j) { + int idx = i * stride + j * 4; + new_pixels[idx] = pixels[idx + 2]; + new_pixels[idx + 1] = pixels[idx + 1]; + new_pixels[idx + 2] = pixels[idx]; + new_pixels[idx + 3] = pixels[idx + 3]; + } + } + + return new_pixels; +} + } // namespace gtk_util diff --git a/base/gtk_util.h b/base/gtk_util.h index 435780d..912bfbd 100644 --- a/base/gtk_util.h +++ b/base/gtk_util.h @@ -7,6 +7,7 @@ #pragma once #include <string> +#include <stdint.h> namespace gtk_util { @@ -17,6 +18,11 @@ std::string ConvertAcceleratorsFromWindowsStyle(const std::string& label); // Removes the "&" accelerators from a Windows label. std::string RemoveWindowsStyleAccelerators(const std::string& label); +// Makes a copy of |pixels| with the ordering changed from BGRA to RGBA. +// The caller is responsible for free()ing the data. If |stride| is 0, it's +// assumed to be 4 * |width|. +uint8_t* BGRAToRGBA(const uint8_t* pixels, int width, int height, int stride); + } // namespace gtk_util #endif // BASE_GTK_UTIL_H_ diff --git a/chrome/browser/dom_ui/options/advanced_options_utils_gtk.cc b/chrome/browser/dom_ui/options/advanced_options_utils_gtk.cc index 094701e..66dbe27 100644 --- a/chrome/browser/dom_ui/options/advanced_options_utils_gtk.cc +++ b/chrome/browser/dom_ui/options/advanced_options_utils_gtk.cc @@ -7,7 +7,6 @@ #include "chrome/browser/dom_ui/options/advanced_options_utils.h" #include "app/gtk_signal.h" -#include "app/gtk_util.h" #include "base/file_util.h" #include "base/environment.h" #include "base/process_util.h" diff --git a/chrome/browser/gtk/browser_window_gtk.cc b/chrome/browser/gtk/browser_window_gtk.cc index 2ce9370..c40419f 100644 --- a/chrome/browser/gtk/browser_window_gtk.cc +++ b/chrome/browser/gtk/browser_window_gtk.cc @@ -8,7 +8,6 @@ #include <string> -#include "app/gtk_util.h" #include "app/keyboard_codes.h" #include "app/l10n_util.h" #include "base/base_paths.h" diff --git a/chrome/browser/gtk/create_application_shortcuts_dialog_gtk.cc b/chrome/browser/gtk/create_application_shortcuts_dialog_gtk.cc index dbde270..d748622 100644 --- a/chrome/browser/gtk/create_application_shortcuts_dialog_gtk.cc +++ b/chrome/browser/gtk/create_application_shortcuts_dialog_gtk.cc @@ -6,7 +6,6 @@ #include <string> -#include "app/gtk_util.h" #include "app/l10n_util.h" #include "base/environment.h" #include "base/utf_string_conversions.h" diff --git a/chrome/browser/gtk/extension_install_prompt2_gtk.cc b/chrome/browser/gtk/extension_install_prompt2_gtk.cc index 6d7008c..9c66c20 100644 --- a/chrome/browser/gtk/extension_install_prompt2_gtk.cc +++ b/chrome/browser/gtk/extension_install_prompt2_gtk.cc @@ -4,7 +4,6 @@ #include <gtk/gtk.h> -#include "app/gtk_util.h" #include "app/l10n_util.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" diff --git a/chrome/browser/gtk/first_run_bubble.cc b/chrome/browser/gtk/first_run_bubble.cc index fb23e145..6102180 100644 --- a/chrome/browser/gtk/first_run_bubble.cc +++ b/chrome/browser/gtk/first_run_bubble.cc @@ -6,7 +6,6 @@ #include <gtk/gtk.h> -#include "app/gtk_util.h" #include "app/l10n_util.h" #include "base/command_line.h" #include "base/i18n/rtl.h" diff --git a/chrome/browser/gtk/gtk_util.cc b/chrome/browser/gtk/gtk_util.cc index c03e5ba..ac45a7d 100644 --- a/chrome/browser/gtk/gtk_util.cc +++ b/chrome/browser/gtk/gtk_util.cc @@ -11,14 +11,16 @@ #include <cstdarg> #include <map> -#include "app/gtk_util.h" #include "app/l10n_util.h" #include "app/resource_bundle.h" #include "app/x11_util.h" +#include "base/environment.h" #include "base/gtk_util.h" #include "base/i18n/rtl.h" #include "base/linux_util.h" #include "base/logging.h" +#include "base/nix/xdg_util.h" +#include "base/string_number_conversions.h" #include "base/utf_string_conversions.h" #include "chrome/browser/autocomplete/autocomplete.h" #include "chrome/browser/autocomplete/autocomplete_match.h" @@ -81,11 +83,32 @@ gboolean OnMouseButtonReleased(GtkWidget* widget, GdkEventButton* event, return TRUE; } +// Returns the approximate number of characters that can horizontally fit in +// |pixel_width| pixels. +int GetCharacterWidthForPixels(GtkWidget* widget, int pixel_width) { + 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)); + + // This technique (max of char and digit widths) matches the code in + // gtklabel.c. + int char_width = pixel_width * PANGO_SCALE / + std::max(pango_font_metrics_get_approximate_char_width(metrics), + pango_font_metrics_get_approximate_digit_width(metrics)); + + pango_font_metrics_unref(metrics); + g_object_unref(context); + + return char_width; +} + void OnLabelRealize(GtkWidget* label, gpointer pixel_width) { gtk_label_set_width_chars( GTK_LABEL(label), - gtk_util::GetCharacterWidthForPixels(label, - GPOINTER_TO_INT(pixel_width))); + GetCharacterWidthForPixels(label,GPOINTER_TO_INT(pixel_width))); } // Ownership of |icon_list| is passed to the caller. @@ -225,6 +248,46 @@ GtkWidget* CreateBoldLabel(const std::string& text) { return LeftAlignMisc(label); } +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 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) + base::StringToDouble(l10n_util::GetStringUTF8(width_chars), &chars); + + double lines = 0; + if (height) + base::StringToDouble(l10n_util::GetStringUTF8(height_lines), &lines); + + GetWidgetSizeFromCharacters(widget, chars, lines, width, height); +} + void SetWindowSizeFromResources(GtkWindow* window, int width_id, int height_id, bool resizable) { int width = -1; @@ -1137,4 +1200,14 @@ WebDragOperationsMask GdkDragActionToWebDragOp(GdkDragAction action) { return op; } +void ApplyMessageDialogQuirks(GtkWidget* dialog) { + if (gtk_window_get_modal(GTK_WINDOW(dialog))) { + // Work around a KDE 3 window manager bug. + scoped_ptr<base::Environment> env(base::Environment::Create()); + if (base::nix::DESKTOP_ENVIRONMENT_KDE3 == + base::nix::GetDesktopEnvironment(env.get())) + gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), FALSE); + } +} + } // namespace gtk_util diff --git a/chrome/browser/gtk/gtk_util.h b/chrome/browser/gtk/gtk_util.h index 0ecafee..f7a87e1 100644 --- a/chrome/browser/gtk/gtk_util.h +++ b/chrome/browser/gtk/gtk_util.h @@ -84,6 +84,20 @@ GtkWidget* LeftAlignMisc(GtkWidget* misc); // Create a left-aligned label with the given text in bold. GtkWidget* CreateBoldLabel(const std::string& text); +// 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); + +// 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 a convenience method for configuring dialog size. // |width_id| and |height_id| are resource IDs for the size. If either of these // are set to -1, the respective size will be set to the widget default. @@ -341,6 +355,11 @@ void InitLabelSizeRequestAndEllipsizeMode(GtkWidget* label); GdkDragAction WebDragOpToGdkDragAction(WebKit::WebDragOperationsMask op); WebKit::WebDragOperationsMask GdkDragActionToWebDragOp(GdkDragAction action); +// A helper function for gtk_message_dialog_new() to work around a few 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 // CHROME_BROWSER_GTK_GTK_UTIL_H_ diff --git a/chrome/browser/gtk/js_modal_dialog_gtk.cc b/chrome/browser/gtk/js_modal_dialog_gtk.cc index 3689356..b4a0953 100644 --- a/chrome/browser/gtk/js_modal_dialog_gtk.cc +++ b/chrome/browser/gtk/js_modal_dialog_gtk.cc @@ -6,7 +6,6 @@ #include <gtk/gtk.h> -#include "app/gtk_util.h" #include "app/l10n_util.h" #include "app/message_box_flags.h" #include "base/logging.h" diff --git a/chrome/browser/gtk/options/advanced_contents_gtk.cc b/chrome/browser/gtk/options/advanced_contents_gtk.cc index fa1c1c9..f98490e 100644 --- a/chrome/browser/gtk/options/advanced_contents_gtk.cc +++ b/chrome/browser/gtk/options/advanced_contents_gtk.cc @@ -11,7 +11,6 @@ #include <vector> #include "app/gtk_signal.h" -#include "app/gtk_util.h" #include "app/l10n_util.h" #include "base/basictypes.h" #include "base/command_line.h" diff --git a/chrome/browser/gtk/options/advanced_page_gtk.cc b/chrome/browser/gtk/options/advanced_page_gtk.cc index 71c8052..8351322 100644 --- a/chrome/browser/gtk/options/advanced_page_gtk.cc +++ b/chrome/browser/gtk/options/advanced_page_gtk.cc @@ -4,7 +4,6 @@ #include "chrome/browser/gtk/options/advanced_page_gtk.h" -#include "app/gtk_util.h" #include "app/l10n_util.h" #include "chrome/browser/gtk/gtk_util.h" #include "chrome/browser/options_util.h" diff --git a/chrome/browser/gtk/options/content_page_gtk.cc b/chrome/browser/gtk/options/content_page_gtk.cc index a119904..c75028a 100644 --- a/chrome/browser/gtk/options/content_page_gtk.cc +++ b/chrome/browser/gtk/options/content_page_gtk.cc @@ -6,7 +6,6 @@ #include <string> -#include "app/gtk_util.h" #include "app/l10n_util.h" #include "base/command_line.h" #include "base/utf_string_conversions.h" diff --git a/chrome/browser/gtk/options/languages_page_gtk.cc b/chrome/browser/gtk/options/languages_page_gtk.cc index 443cca8..eda179f 100644 --- a/chrome/browser/gtk/options/languages_page_gtk.cc +++ b/chrome/browser/gtk/options/languages_page_gtk.cc @@ -9,7 +9,6 @@ #include <vector> #include "app/gtk_signal.h" -#include "app/gtk_util.h" #include "app/l10n_util.h" #include "base/command_line.h" #include "base/message_loop.h" diff --git a/chrome/browser/gtk/options/passwords_page_gtk.cc b/chrome/browser/gtk/options/passwords_page_gtk.cc index a1aa597..2051eb6 100644 --- a/chrome/browser/gtk/options/passwords_page_gtk.cc +++ b/chrome/browser/gtk/options/passwords_page_gtk.cc @@ -6,7 +6,6 @@ #include <string> -#include "app/gtk_util.h" #include "app/l10n_util.h" #include "base/stl_util-inl.h" #include "base/utf_string_conversions.h" diff --git a/chrome/browser/gtk/options/url_picker_dialog_gtk.cc b/chrome/browser/gtk/options/url_picker_dialog_gtk.cc index 30059ba..e9cef31 100644 --- a/chrome/browser/gtk/options/url_picker_dialog_gtk.cc +++ b/chrome/browser/gtk/options/url_picker_dialog_gtk.cc @@ -4,7 +4,6 @@ #include <gtk/gtk.h> -#include "app/gtk_util.h" #include "app/l10n_util.h" #include "base/message_loop.h" #include "base/utf_string_conversions.h" diff --git a/chrome/browser/gtk/process_singleton_dialog.cc b/chrome/browser/gtk/process_singleton_dialog.cc index cdd4eab..26fc1b4 100644 --- a/chrome/browser/gtk/process_singleton_dialog.cc +++ b/chrome/browser/gtk/process_singleton_dialog.cc @@ -4,9 +4,9 @@ #include "chrome/browser/gtk/process_singleton_dialog.h" -#include "app/gtk_util.h" #include "app/l10n_util.h" #include "base/message_loop.h" +#include "chrome/browser/gtk/gtk_util.h" #include "grit/chromium_strings.h" // static diff --git a/chrome/browser/icon_loader_linux.cc b/chrome/browser/icon_loader_linux.cc index de26857..1f8eff2 100644 --- a/chrome/browser/icon_loader_linux.cc +++ b/chrome/browser/icon_loader_linux.cc @@ -9,8 +9,8 @@ #include <gtk/gtk.h> #include "base/file_util.h" +#include "base/gtk_util.h" #include "base/logging.h" -#include "app/gtk_util.h" #include "base/message_loop.h" #include "base/mime_util.h" #include "base/thread.h" diff --git a/chrome/browser/platform_util_common_linux.cc b/chrome/browser/platform_util_common_linux.cc index 1f0ea42..61c92eb 100644 --- a/chrome/browser/platform_util_common_linux.cc +++ b/chrome/browser/platform_util_common_linux.cc @@ -6,7 +6,6 @@ #include <gtk/gtk.h> -#include "app/gtk_util.h" #include "base/file_util.h" #include "base/message_loop.h" #include "base/process_util.h" |