summaryrefslogtreecommitdiffstats
path: root/chrome/browser
diff options
context:
space:
mode:
authorevan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-02-11 02:36:37 +0000
committerevan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-02-11 02:36:37 +0000
commit3431b8926e2332f47f5e5eb6f78f512ca6ba286c (patch)
tree6c70271b6e70a60caacf16a558c423db56e6e9a0 /chrome/browser
parent24ce7d31c745ce4b045f81f74fa2def3d7a18b34 (diff)
downloadchromium_src-3431b8926e2332f47f5e5eb6f78f512ca6ba286c.zip
chromium_src-3431b8926e2332f47f5e5eb6f78f512ca6ba286c.tar.gz
chromium_src-3431b8926e2332f47f5e5eb6f78f512ca6ba286c.tar.bz2
Draw custom buttons in toolbar.
The menu buttons aren't quite right yet, but the rest seem pretty good. Review URL: http://codereview.chromium.org/20252 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9548 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r--chrome/browser/gtk/browser_toolbar_view_gtk.cc172
-rw-r--r--chrome/browser/gtk/browser_toolbar_view_gtk.h21
2 files changed, 151 insertions, 42 deletions
diff --git a/chrome/browser/gtk/browser_toolbar_view_gtk.cc b/chrome/browser/gtk/browser_toolbar_view_gtk.cc
index 00c28d7..57f481a 100644
--- a/chrome/browser/gtk/browser_toolbar_view_gtk.cc
+++ b/chrome/browser/gtk/browser_toolbar_view_gtk.cc
@@ -5,6 +5,8 @@
#include "chrome/browser/gtk/browser_toolbar_view_gtk.h"
#include "base/logging.h"
+#include "base/base_paths_linux.h"
+#include "base/path_service.h"
#include "chrome/app/chrome_dll_resource.h"
#include "chrome/browser/browser.h"
#include "chrome/common/l10n_util.h"
@@ -12,13 +14,106 @@
#include "chromium_strings.h"
#include "generated_resources.h"
+// 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 a filename, which is used as the base filename
+ // in loading the theme graphics pngs. This will be replaced by the
+ // ResourceBundle graphics soon.
+ CustomDrawButton(const std::string& filename);
+ ~CustomDrawButton();
+
+ GtkWidget* widget() const { return widget_; }
+
+ private:
+ // Load an image from a path.
+ // TODO(port): Temporary until ResourceBundle works.
+ GdkPixbuf* LoadImage(const std::string& filename);
+
+ // Load all the button images from a base theme filename.
+ // TODO(port): Temporary until ResourceBundle works.
+ void LoadImages(const std::string& filename);
+
+ // 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(
+ const std::string& filename) {
+ widget_ = gtk_button_new();
+ LoadImages(filename);
+
+ 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(
+ const std::string& filename) {
+ // We intentionally ignore errors here, as some buttons don't have images
+ // for all states. This will all be removed once ResourceBundle works.
+ return gdk_pixbuf_new_from_file(filename.c_str(), NULL);
+}
+
+void BrowserToolbarGtk::CustomDrawButton::LoadImages(
+ const std::string& filename) {
+ // TODO(evanm): make this use ResourceBundle once that is ported.
+ FilePath path;
+ bool ok = PathService::Get(base::DIR_SOURCE_ROOT, &path);
+ DCHECK(ok);
+ path = path.Append("chrome/app/theme").Append(filename);
+ pixbufs_[GTK_STATE_NORMAL] = LoadImage(path.value() + ".png");
+ pixbufs_[GTK_STATE_ACTIVE] = LoadImage(path.value() + "_p.png");
+ pixbufs_[GTK_STATE_PRELIGHT] = LoadImage(path.value() + "_h.png");
+ pixbufs_[GTK_STATE_SELECTED] = NULL;
+ pixbufs_[GTK_STATE_INSENSITIVE] = LoadImage(path.value() + "_d.png");
+
+ gtk_widget_set_size_request(widget_,
+ gdk_pixbuf_get_width(pixbufs_[0]),
+ gdk_pixbuf_get_height(pixbufs_[0]));
+}
+
+// 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),
- back_(NULL),
- forward_(NULL),
- reload_(NULL),
- home_(NULL),
- star_(NULL),
model_(browser->toolbar_model()),
browser_(browser),
profile_(NULL) {
@@ -44,21 +139,37 @@ void BrowserToolbarGtk::Init(Profile* profile) {
toolbar_tooltips_ = gtk_tooltips_new();
- back_ = BuildToolbarButton(l10n_util::GetString(IDS_TOOLTIP_BACK));
- forward_ = BuildToolbarButton(l10n_util::GetString(IDS_TOOLTIP_FORWARD));
- reload_ = BuildToolbarButton(l10n_util::GetString(IDS_TOOLTIP_RELOAD));
- home_ = BuildToolbarButton(l10n_util::GetString(IDS_TOOLTIP_HOME));
- star_ = BuildToolbarButton(l10n_util::GetString(IDS_TOOLTIP_STAR));
+ back_.reset(BuildToolbarButton("back",
+ l10n_util::GetString(IDS_TOOLTIP_BACK)));
+ forward_.reset(BuildToolbarButton("forward",
+ l10n_util::GetString(IDS_TOOLTIP_FORWARD)));
+
+ gtk_box_pack_start(GTK_BOX(toolbar_), gtk_label_new(" "), FALSE, FALSE, 0);
+
+ reload_.reset(BuildToolbarButton("reload",
+ l10n_util::GetString(IDS_TOOLTIP_RELOAD)));
+ home_.reset(BuildToolbarButton("home",
+ l10n_util::GetString(IDS_TOOLTIP_HOME)));
gtk_box_pack_start(GTK_BOX(toolbar_), gtk_label_new(" "), FALSE, FALSE, 0);
- gtk_box_pack_start(GTK_BOX(toolbar_), gtk_entry_new(), TRUE, TRUE, 0);
- go_ = BuildToolbarButton(L"");
- page_menu_ = BuildToolbarButton(l10n_util::GetString(IDS_PAGEMENU_TOOLTIP));
+ star_.reset(BuildToolbarButton("star",
+ l10n_util::GetString(IDS_TOOLTIP_STAR)));
+
+ GtkWidget* entry = gtk_entry_new();
+ gtk_widget_set_size_request(entry, 0, 27);
+ gtk_box_pack_start(GTK_BOX(toolbar_), entry, TRUE, TRUE, 0);
+
+ go_.reset(BuildToolbarButton("go", L""));
+
+ // TODO(port): these buttons need even stranger drawing than the others.
+ page_menu_.reset(BuildToolbarButton("menu_page",
+ l10n_util::GetString(IDS_PAGEMENU_TOOLTIP)));
// TODO(port): Need to get l10n_util::GetStringF working under linux to get
// the correct string here.
- app_menu_ = BuildToolbarButton(l10n_util::GetString(IDS_APPMENU_TOOLTIP));
+ app_menu_.reset(BuildToolbarButton("menu_chrome",
+ l10n_util::GetString(IDS_APPMENU_TOOLTIP)));
// TODO(erg): wchar_t mismatch on linux. Fix later.
// show_home_button_.Init(prefs::kShowHomeButton, profile->GetPrefs(), this);
@@ -74,19 +185,19 @@ void BrowserToolbarGtk::EnabledStateChangedForCommand(int id, bool enabled) {
GtkWidget* widget = NULL;
switch (id) {
case IDC_BACK:
- widget = back_;
+ widget = back_->widget();
break;
case IDC_FORWARD:
- widget = forward_;
+ widget = forward_->widget();
break;
case IDC_RELOAD:
- widget = reload_;
+ widget = reload_->widget();
break;
case IDC_HOME:
- widget = home_;
+ widget = home_->widget();
break;
case IDC_STAR:
- widget = star_;
+ widget = star_->widget();
break;
}
if (widget)
@@ -104,36 +215,35 @@ void BrowserToolbarGtk::SetProfile(Profile* profile) {
}
// TODO(port): This needs to deal with our styled pixmaps.
-GtkWidget* BrowserToolbarGtk::BuildToolbarButton(
+BrowserToolbarGtk::CustomDrawButton* BrowserToolbarGtk::BuildToolbarButton(
+ const std::string& filename,
const std::wstring& localized_tooltip) {
- GtkWidget* b = gtk_button_new();
- gtk_widget_set_size_request(b, 29, 29);
+ CustomDrawButton* button = new CustomDrawButton(filename);
// TODO(erg): Mismatch between wstring and string.
// gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tooltips_),
// GTK_WIDGET(back_),
// localized_tooltip, localized_tooltip);
- g_signal_connect(G_OBJECT(b), "clicked",
+ g_signal_connect(G_OBJECT(button->widget()), "clicked",
G_CALLBACK(ButtonClickCallback), this);
- gtk_box_pack_start(GTK_BOX(toolbar_), b, FALSE, FALSE, 0);
+ gtk_box_pack_start(GTK_BOX(toolbar_), button->widget(), FALSE, FALSE, 0);
- // TODO(erg): Change this when pixmap loading is working.
- return b;
+ return button;
}
/* static */
void BrowserToolbarGtk::ButtonClickCallback(GtkWidget* button,
BrowserToolbarGtk* toolbar) {
int tag = -1;
- if (button == toolbar->back_)
+ if (button == toolbar->back_->widget())
tag = IDC_BACK;
- else if (button == toolbar->forward_)
+ else if (button == toolbar->forward_->widget())
tag = IDC_FORWARD;
- else if (button == toolbar->reload_)
+ else if (button == toolbar->reload_->widget())
tag = IDC_RELOAD;
- else if (button == toolbar->home_)
+ else if (button == toolbar->home_->widget())
tag = IDC_HOME;
- else if (button == toolbar->star_)
+ else if (button == toolbar->star_->widget())
tag = IDC_STAR;
if (tag != -1) {
diff --git a/chrome/browser/gtk/browser_toolbar_view_gtk.h b/chrome/browser/gtk/browser_toolbar_view_gtk.h
index 673bc10..36b9464f 100644
--- a/chrome/browser/gtk/browser_toolbar_view_gtk.h
+++ b/chrome/browser/gtk/browser_toolbar_view_gtk.h
@@ -33,8 +33,11 @@ class BrowserToolbarGtk : public CommandUpdater::CommandObserver {
void SetProfile(Profile* profile);
private:
- // Builds a GtkButton with all the properties set.
- GtkWidget* BuildToolbarButton(const std::wstring& localized_tooltip);
+ class CustomDrawButton; // Defined in the .cc file.
+
+ // Builds a toolbar button with all the properties set.
+ CustomDrawButton* BuildToolbarButton(const std::string& filename,
+ const std::wstring& localized_tooltip);
// Gtk callback for the "clicked" signal.
static void ButtonClickCallback(GtkWidget* button,
@@ -47,15 +50,11 @@ class BrowserToolbarGtk : public CommandUpdater::CommandObserver {
// Tooltip container for all GTK widgets in this class.
GtkTooltips* toolbar_tooltips_;
- // All the GTK buttons in the toolbar.
- GtkWidget* back_;
- GtkWidget* forward_;
- GtkWidget* reload_;
- GtkWidget* home_;
- GtkWidget* star_;
- GtkWidget* go_;
- GtkWidget* page_menu_;
- GtkWidget* app_menu_;
+ // All the buttons in the toolbar.
+ scoped_ptr<CustomDrawButton> back_, forward_;
+ scoped_ptr<CustomDrawButton> reload_, home_;
+ scoped_ptr<CustomDrawButton> star_, go_;
+ scoped_ptr<CustomDrawButton> page_menu_, app_menu_;
// The model that contains the security level, text, icon to display...
ToolbarModel* model_;