diff options
Diffstat (limited to 'chrome/browser/gtk/gtk_util.cc')
-rw-r--r-- | chrome/browser/gtk/gtk_util.cc | 79 |
1 files changed, 76 insertions, 3 deletions
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 |