diff options
author | evan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-19 22:47:53 +0000 |
---|---|---|
committer | evan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-19 22:47:53 +0000 |
commit | 72cf811b3b85841866dddeb0605810f0bcd705e4 (patch) | |
tree | 0797f0bb3a08555fda2fed59e3a005f09e11db2d /chrome/browser | |
parent | 812bfe6b685d7367cb9e255ec71b7b0b720e1235 (diff) | |
download | chromium_src-72cf811b3b85841866dddeb0605810f0bcd705e4.zip chromium_src-72cf811b3b85841866dddeb0605810f0bcd705e4.tar.gz chromium_src-72cf811b3b85841866dddeb0605810f0bcd705e4.tar.bz2 |
Relocate custom button code to its own files, in anticipation of more changes.
(Next is to fix the menu buttons.)
Review URL: http://codereview.chromium.org/20509
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@10053 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r-- | chrome/browser/browser.scons | 1 | ||||
-rw-r--r-- | chrome/browser/gtk/browser_toolbar_view_gtk.cc | 115 | ||||
-rw-r--r-- | chrome/browser/gtk/browser_toolbar_view_gtk.h | 3 | ||||
-rw-r--r-- | chrome/browser/gtk/custom_button.cc | 88 | ||||
-rw-r--r-- | chrome/browser/gtk/custom_button.h | 44 |
5 files changed, 137 insertions, 114 deletions
diff --git a/chrome/browser/browser.scons b/chrome/browser/browser.scons index 1061973..16adc5d 100644 --- a/chrome/browser/browser.scons +++ b/chrome/browser/browser.scons @@ -780,6 +780,7 @@ if env.Bit('linux'): 'gtk/browser_toolbar_view_gtk.cc', 'gtk/browser_window_factory_gtk.cc', 'gtk/browser_window_gtk.cc', + 'gtk/custom_button.cc', 'gtk/menu_gtk.cc', 'gtk/nine_box.cc', 'gtk/standard_menus.cc', diff --git a/chrome/browser/gtk/browser_toolbar_view_gtk.cc b/chrome/browser/gtk/browser_toolbar_view_gtk.cc index 4188803..f22c4d4 100644 --- a/chrome/browser/gtk/browser_toolbar_view_gtk.cc +++ b/chrome/browser/gtk/browser_toolbar_view_gtk.cc @@ -9,8 +9,8 @@ #include "base/path_service.h" #include "chrome/app/chrome_dll_resource.h" #include "chrome/browser/browser.h" +#include "chrome/browser/gtk/custom_button.h" #include "chrome/browser/gtk/back_forward_menu_model_gtk.h" -#include "chrome/browser/gtk/menu_gtk.h" #include "chrome/browser/gtk/standard_menus.h" #include "chrome/common/l10n_util.h" #include "chrome/common/resource_bundle.h" @@ -24,114 +24,6 @@ const int BrowserToolbarGtk::kToolbarHeight = 38; // when the user clicks and the popup menu appears. static const int kMenuTimerDelay = 500; -// CustomDrawButton manages the lifetimes of some resources used to make a -// custom-drawn Gtk button. We use them on the toolbar. -class BrowserToolbarGtk::CustomDrawButton { - public: - // The constructor takes 4 resource ids. If a resource doesn't exist for a - // button, pass in 0. - CustomDrawButton(int normal_id, - int active_id, - int highlight_id, - int depressed_id); - explicit CustomDrawButton(const std::string& filename); - ~CustomDrawButton(); - - GtkWidget* widget() const { return widget_; } - - private: - // Load an image given a resource id. - GdkPixbuf* LoadImage(int resource_id); - - // Callback for expose, used to draw the custom graphics. - static gboolean OnExpose(GtkWidget* widget, GdkEventExpose* e, - CustomDrawButton* obj); - - // The actual button widget. - GtkWidget* widget_; - - // We store one GdkPixbuf* for each possible state of the button; - // INSENSITIVE is the last available state; - GdkPixbuf* pixbufs_[GTK_STATE_INSENSITIVE + 1]; -}; - -BrowserToolbarGtk::CustomDrawButton::CustomDrawButton(int normal_id, - int active_id, int highlight_id, int depressed_id) { - widget_ = gtk_button_new(); - - // Load the button images from the theme resources .pak file. - pixbufs_[GTK_STATE_NORMAL] = LoadImage(normal_id); - pixbufs_[GTK_STATE_ACTIVE] = LoadImage(active_id); - pixbufs_[GTK_STATE_PRELIGHT] = LoadImage(highlight_id); - pixbufs_[GTK_STATE_SELECTED] = NULL; - pixbufs_[GTK_STATE_INSENSITIVE] = LoadImage(depressed_id); - - gtk_widget_set_size_request(widget_, - gdk_pixbuf_get_width(pixbufs_[0]), - gdk_pixbuf_get_height(pixbufs_[0])); - - gtk_widget_set_app_paintable(widget_, TRUE); - g_signal_connect(G_OBJECT(widget_), "expose-event", - G_CALLBACK(OnExpose), this); -} - -BrowserToolbarGtk::CustomDrawButton::~CustomDrawButton() { - for (size_t i = 0; i < arraysize(pixbufs_); ++i) { - if (pixbufs_[i]) - gdk_pixbuf_unref(pixbufs_[i]); - } -} - -GdkPixbuf* BrowserToolbarGtk::CustomDrawButton::LoadImage(int resource_id) { - if (0 == resource_id) - return NULL; - - ResourceBundle& rb = ResourceBundle::GetSharedInstance(); - std::vector<unsigned char> data; - rb.LoadImageResourceBytes(resource_id, &data); - - GdkPixbufLoader* loader = gdk_pixbuf_loader_new(); - bool ok = gdk_pixbuf_loader_write(loader, static_cast<guint8*>(data.data()), - data.size(), NULL); - DCHECK(ok) << "failed to write " << resource_id; - // Calling gdk_pixbuf_loader_close forces the data to be parsed by the - // loader. We must do this before calling gdk_pixbuf_loader_get_pixbuf. - ok = gdk_pixbuf_loader_close(loader, NULL); - DCHECK(ok) << "close failed " << resource_id; - GdkPixbuf* pixbuf = gdk_pixbuf_loader_get_pixbuf(loader); - DCHECK(pixbuf) << "failed to load " << resource_id << " " << data.size(); - - // The pixbuf is owned by the loader, so add a ref so when we delete the - // loader, the pixbuf still exists. - g_object_ref(pixbuf); - g_object_unref(loader); - - return pixbuf; -} - -// static -gboolean BrowserToolbarGtk::CustomDrawButton::OnExpose( - GtkWidget* widget, - GdkEventExpose* e, - CustomDrawButton* button) { - GdkPixbuf* pixbuf = button->pixbufs_[GTK_WIDGET_STATE(widget)]; - - // Fall back to the default image if we don't have one for this state. - if (!pixbuf) - pixbuf = button->pixbufs_[GTK_STATE_NORMAL]; - - if (!pixbuf) - return FALSE; - - gdk_draw_pixbuf(widget->window, - widget->style->fg_gc[GTK_WIDGET_STATE(widget)], - pixbuf, - 0, 0, - widget->allocation.x, widget->allocation.y, -1, -1, - GDK_RGB_DITHER_NONE, 0, 0); - return TRUE; -} - BrowserToolbarGtk::BrowserToolbarGtk(Browser* browser) : toolbar_(NULL), entry_(NULL), @@ -267,8 +159,7 @@ void BrowserToolbarGtk::UpdateTabContents(TabContents* contents, contents->GetURL().possibly_invalid_spec().c_str()); } -// TODO(port): This needs to deal with our styled pixmaps. -BrowserToolbarGtk::CustomDrawButton* BrowserToolbarGtk::BuildToolbarButton( +CustomDrawButton* BrowserToolbarGtk::BuildToolbarButton( int normal_id, int active_id, int highlight_id, int depressed_id, const std::wstring& localized_tooltip, bool menu_button) { CustomDrawButton* button = new CustomDrawButton(normal_id, active_id, @@ -344,7 +235,7 @@ gboolean BrowserToolbarGtk::OnMenuButtonPressEvent(GtkWidget* button, return FALSE; } -BrowserToolbarGtk::CustomDrawButton* BrowserToolbarGtk::BuildBackForwardButton( +CustomDrawButton* BrowserToolbarGtk::BuildBackForwardButton( int normal_id, int active_id, int highlight_id, diff --git a/chrome/browser/gtk/browser_toolbar_view_gtk.h b/chrome/browser/gtk/browser_toolbar_view_gtk.h index c000e9b..f137cd4 100644 --- a/chrome/browser/gtk/browser_toolbar_view_gtk.h +++ b/chrome/browser/gtk/browser_toolbar_view_gtk.h @@ -16,6 +16,7 @@ class BackForwardMenuModelGtk; class Browser; +class CustomDrawButton; class Profile; class TabContents; class ToolbarModel; @@ -51,8 +52,6 @@ class BrowserToolbarGtk : public CommandUpdater::CommandObserver, void UpdateTabContents(TabContents* contents, bool should_restore_state); private: - class CustomDrawButton; // Defined in the .cc file. - // Builds a toolbar button with all the properties set. CustomDrawButton* BuildToolbarButton(int normal_id, int active_id, diff --git a/chrome/browser/gtk/custom_button.cc b/chrome/browser/gtk/custom_button.cc new file mode 100644 index 0000000..8020464 --- /dev/null +++ b/chrome/browser/gtk/custom_button.cc @@ -0,0 +1,88 @@ +// Copyright (c) 2009 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 "chrome/browser/gtk/custom_button.h" + +#include "base/basictypes.h" +#include "base/logging.h" +#include "chrome/common/resource_bundle.h" + +CustomDrawButton::CustomDrawButton(int normal_id, + int active_id, int highlight_id, int depressed_id) { + widget_ = gtk_button_new(); + + // Load the button images from the theme resources .pak file. + pixbufs_[GTK_STATE_NORMAL] = LoadImage(normal_id); + pixbufs_[GTK_STATE_ACTIVE] = LoadImage(active_id); + pixbufs_[GTK_STATE_PRELIGHT] = LoadImage(highlight_id); + pixbufs_[GTK_STATE_SELECTED] = NULL; + pixbufs_[GTK_STATE_INSENSITIVE] = LoadImage(depressed_id); + + gtk_widget_set_size_request(widget_, + gdk_pixbuf_get_width(pixbufs_[0]), + gdk_pixbuf_get_height(pixbufs_[0])); + + gtk_widget_set_app_paintable(widget_, TRUE); + // We effectively double-buffer by virtue of having only one image... + gtk_widget_set_double_buffered(widget_, FALSE); + g_signal_connect(G_OBJECT(widget_), "expose-event", + G_CALLBACK(OnExpose), this); +} + +CustomDrawButton::~CustomDrawButton() { + for (size_t i = 0; i < arraysize(pixbufs_); ++i) { + if (pixbufs_[i]) + gdk_pixbuf_unref(pixbufs_[i]); + } +} + +GdkPixbuf* CustomDrawButton::LoadImage(int resource_id) { + if (0 == resource_id) + return NULL; + + ResourceBundle& rb = ResourceBundle::GetSharedInstance(); + std::vector<unsigned char> data; + rb.LoadImageResourceBytes(resource_id, &data); + + GdkPixbufLoader* loader = gdk_pixbuf_loader_new(); + bool ok = gdk_pixbuf_loader_write(loader, static_cast<guint8*>(data.data()), + data.size(), NULL); + DCHECK(ok) << "failed to write " << resource_id; + // Calling gdk_pixbuf_loader_close forces the data to be parsed by the + // loader. We must do this before calling gdk_pixbuf_loader_get_pixbuf. + ok = gdk_pixbuf_loader_close(loader, NULL); + DCHECK(ok) << "close failed " << resource_id; + GdkPixbuf* pixbuf = gdk_pixbuf_loader_get_pixbuf(loader); + DCHECK(pixbuf) << "failed to load " << resource_id << " " << data.size(); + + // The pixbuf is owned by the loader, so add a ref so when we delete the + // loader, the pixbuf still exists. + g_object_ref(pixbuf); + g_object_unref(loader); + + return pixbuf; +} + +// static +gboolean CustomDrawButton::OnExpose( + GtkWidget* widget, + GdkEventExpose* e, + CustomDrawButton* button) { + GdkPixbuf* pixbuf = button->pixbufs_[GTK_WIDGET_STATE(widget)]; + + // Fall back to the default image if we don't have one for this state. + if (!pixbuf) + pixbuf = button->pixbufs_[GTK_STATE_NORMAL]; + + if (!pixbuf) + return FALSE; + + gdk_draw_pixbuf(widget->window, + widget->style->fg_gc[GTK_WIDGET_STATE(widget)], + pixbuf, + 0, 0, + widget->allocation.x, widget->allocation.y, -1, -1, + GDK_RGB_DITHER_NONE, 0, 0); + return TRUE; +} diff --git a/chrome/browser/gtk/custom_button.h b/chrome/browser/gtk/custom_button.h new file mode 100644 index 0000000..d5f7721 --- /dev/null +++ b/chrome/browser/gtk/custom_button.h @@ -0,0 +1,44 @@ +// Copyright (c) 2009 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 CHROME_BROWSER_GTK_CUSTOM_BUTTON_H_ +#define CHROME_BROWSER_GTK_CUSTOM_BUTTON_H_ + +#include <gtk/gtk.h> + +#include <string> + +// These classes implement custom-drawn buttons. They're used on the toolbar +// and the bookmarks bar. + +class CustomDrawButton { + public: + // The constructor takes 4 resource ids. If a resource doesn't exist for a + // button, pass in 0. + CustomDrawButton(int normal_id, + int active_id, + int highlight_id, + int depressed_id); + explicit CustomDrawButton(const std::string& filename); + ~CustomDrawButton(); + + GtkWidget* widget() const { return widget_; } + + private: + // Load an image given a resource id. + GdkPixbuf* LoadImage(int resource_id); + + // Callback for expose, used to draw the custom graphics. + static gboolean OnExpose(GtkWidget* widget, GdkEventExpose* e, + CustomDrawButton* obj); + + // The actual button widget. + GtkWidget* widget_; + + // We store one GdkPixbuf* for each possible state of the button; + // INSENSITIVE is the last available state; + GdkPixbuf* pixbufs_[GTK_STATE_INSENSITIVE + 1]; +}; + +#endif // CHROME_BROWSER_GTK_CUSTOM_BUTTON_H_ |