diff options
author | tc@google.com <tc@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-17 22:43:02 +0000 |
---|---|---|
committer | tc@google.com <tc@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-17 22:43:02 +0000 |
commit | b7125fd56addf151194b09314c1f7f2c9d03a5b9 (patch) | |
tree | ca6a7f9d67fcc1dfda7feac741fe632843e563f7 /chrome | |
parent | 7aea7093469f7c88b7a58bbe7c475b225802c852 (diff) | |
download | chromium_src-b7125fd56addf151194b09314c1f7f2c9d03a5b9.zip chromium_src-b7125fd56addf151194b09314c1f7f2c9d03a5b9.tar.gz chromium_src-b7125fd56addf151194b09314c1f7f2c9d03a5b9.tar.bz2 |
Hook up theme images and chrome resource data on linux.
Go ahead and convert our toolbar buttons to using the theme data
pack file.
Review URL: http://codereview.chromium.org/20433
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9911 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/gtk/browser_toolbar_view_gtk.cc | 129 | ||||
-rw-r--r-- | chrome/browser/gtk/browser_toolbar_view_gtk.h | 5 | ||||
-rw-r--r-- | chrome/common/resource_bundle.cc | 8 | ||||
-rw-r--r-- | chrome/common/resource_bundle_linux.cc | 23 |
4 files changed, 87 insertions, 78 deletions
diff --git a/chrome/browser/gtk/browser_toolbar_view_gtk.cc b/chrome/browser/gtk/browser_toolbar_view_gtk.cc index b7ce06e..28a40dc 100644 --- a/chrome/browser/gtk/browser_toolbar_view_gtk.cc +++ b/chrome/browser/gtk/browser_toolbar_view_gtk.cc @@ -12,42 +12,32 @@ #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" #include "chromium_strings.h" #include "generated_resources.h" +#include "grit/theme_resources.h" const int BrowserToolbarGtk::kToolbarHeight = 38; -static GdkPixbuf* LoadThemeImage(const std::string& filename) { - FilePath path; - bool ok = PathService::Get(base::DIR_SOURCE_ROOT, &path); - DCHECK(ok); - path = path.Append("chrome/app/theme").Append(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(path.value().c_str(), NULL); -} - // 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. + // 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 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); + // 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, @@ -61,10 +51,20 @@ class BrowserToolbarGtk::CustomDrawButton { GdkPixbuf* pixbufs_[GTK_STATE_INSENSITIVE + 1]; }; -BrowserToolbarGtk::CustomDrawButton::CustomDrawButton( - const std::string& filename) { +BrowserToolbarGtk::CustomDrawButton::CustomDrawButton(int normal_id, + int active_id, int highlight_id, int depressed_id) { widget_ = gtk_button_new(); - LoadImages(filename); + + // 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", @@ -78,25 +78,31 @@ BrowserToolbarGtk::CustomDrawButton::~CustomDrawButton() { } } -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. - pixbufs_[GTK_STATE_NORMAL] = LoadThemeImage(filename + ".png"); - pixbufs_[GTK_STATE_ACTIVE] = LoadThemeImage(filename + "_p.png"); - pixbufs_[GTK_STATE_PRELIGHT] = LoadThemeImage(filename + "_h.png"); - pixbufs_[GTK_STATE_SELECTED] = NULL; - pixbufs_[GTK_STATE_INSENSITIVE] = LoadThemeImage(filename + "_d.png"); - - gtk_widget_set_size_request(widget_, - gdk_pixbuf_get_width(pixbufs_[0]), - gdk_pixbuf_get_height(pixbufs_[0])); +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 @@ -153,41 +159,36 @@ void BrowserToolbarGtk::Init(Profile* profile) { toolbar_tooltips_ = gtk_tooltips_new(); - back_.reset(BuildToolbarButton("back", - l10n_util::GetString(IDS_TOOLTIP_BACK), - false)); - forward_.reset(BuildToolbarButton("forward", - l10n_util::GetString(IDS_TOOLTIP_FORWARD), - false)); + back_.reset(BuildToolbarButton(IDR_BACK, IDR_BACK_P, IDR_BACK_H, IDR_BACK_D, + l10n_util::GetString(IDS_TOOLTIP_BACK), false)); + forward_.reset(BuildToolbarButton(IDR_FORWARD, IDR_FORWARD_P, IDR_FORWARD_H, + IDR_FORWARD_D, l10n_util::GetString(IDS_TOOLTIP_FORWARD), false)); gtk_box_pack_start(GTK_BOX(toolbar_), gtk_label_new(" "), FALSE, FALSE, 0); - reload_.reset(BuildToolbarButton("reload", - l10n_util::GetString(IDS_TOOLTIP_RELOAD), - false)); - home_.reset(BuildToolbarButton("home", - l10n_util::GetString(IDS_TOOLTIP_HOME), - false)); + reload_.reset(BuildToolbarButton(IDR_RELOAD, IDR_RELOAD_P, IDR_RELOAD_H, 0, + l10n_util::GetString(IDS_TOOLTIP_RELOAD), false)); + home_.reset(BuildToolbarButton(IDR_HOME, IDR_HOME_P, IDR_HOME_H, 0, + l10n_util::GetString(IDS_TOOLTIP_HOME), false)); gtk_box_pack_start(GTK_BOX(toolbar_), gtk_label_new(" "), FALSE, FALSE, 0); - star_.reset(BuildToolbarButton("star", - l10n_util::GetString(IDS_TOOLTIP_STAR), - false)); + star_.reset(BuildToolbarButton(IDR_STAR, IDR_STAR_P, IDR_STAR_H, IDR_STAR_D, + l10n_util::GetString(IDS_TOOLTIP_STAR), false)); 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"", false)); + go_.reset(BuildToolbarButton(IDR_GO, IDR_GO_P, IDR_GO_H, 0, L"", false)); // TODO(port): these buttons need even stranger drawing than the others. - page_menu_button_.reset(BuildToolbarButton("menu_page", + page_menu_button_.reset(BuildToolbarButton(IDR_MENU_PAGE, 0, 0, 0, l10n_util::GetString(IDS_PAGEMENU_TOOLTIP), true)); // TODO(port): Need to get l10n_util::GetStringF working under linux to get // the correct string here. - app_menu_button_.reset(BuildToolbarButton("menu_chrome", + app_menu_button_.reset(BuildToolbarButton(IDR_MENU_CHROME, 0, 0, 0, l10n_util::GetString(IDS_APPMENU_TOOLTIP), true)); // TODO(erg): wchar_t mismatch on linux. Fix later. @@ -252,10 +253,10 @@ void BrowserToolbarGtk::SetProfile(Profile* profile) { // TODO(port): This needs to deal with our styled pixmaps. BrowserToolbarGtk::CustomDrawButton* BrowserToolbarGtk::BuildToolbarButton( - const std::string& filename, - const std::wstring& localized_tooltip, - bool menu_button) { - CustomDrawButton* button = new CustomDrawButton(filename); + 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, + highlight_id, depressed_id); // TODO(erg): Mismatch between wstring and string. // gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tooltips_), diff --git a/chrome/browser/gtk/browser_toolbar_view_gtk.h b/chrome/browser/gtk/browser_toolbar_view_gtk.h index 2ef52f7..e958f6a 100644 --- a/chrome/browser/gtk/browser_toolbar_view_gtk.h +++ b/chrome/browser/gtk/browser_toolbar_view_gtk.h @@ -48,7 +48,10 @@ class BrowserToolbarGtk : public CommandUpdater::CommandObserver, class CustomDrawButton; // Defined in the .cc file. // Builds a toolbar button with all the properties set. - CustomDrawButton* BuildToolbarButton(const std::string& filename, + CustomDrawButton* BuildToolbarButton(int normal_id, + int active_id, + int highlight_id, + int depressed_id, const std::wstring& localized_tooltip, bool menu_button); diff --git a/chrome/common/resource_bundle.cc b/chrome/common/resource_bundle.cc index 69318fd..785dd29 100644 --- a/chrome/common/resource_bundle.cc +++ b/chrome/common/resource_bundle.cc @@ -85,7 +85,6 @@ bool ResourceBundle::LoadDataResourceBytes(int resource_id, } SkBitmap* ResourceBundle::GetBitmapNamed(int resource_id) { -#if defined(OS_WIN) // Check to see if we already have the Skia image in the cache. { AutoLock lock_scope(lock_); @@ -115,17 +114,10 @@ SkBitmap* ResourceBundle::GetBitmapNamed(int resource_id) { return bitmap.release(); } -#else - NOTIMPLEMENTED() << "image resource loading disabled; need data files"; -#endif // We failed to retrieve the bitmap, show a debugging red square. { LOG(WARNING) << "Unable to load bitmap with id " << resource_id; -#if defined(OS_WIN) NOTREACHED(); // Want to assert in debug mode. -#else - // TODO(port): remove this exception -#endif AutoLock lock_scope(lock_); // Guard empty_bitmap initialization. diff --git a/chrome/common/resource_bundle_linux.cc b/chrome/common/resource_bundle_linux.cc index 1696402..1cb8307 100644 --- a/chrome/common/resource_bundle_linux.cc +++ b/chrome/common/resource_bundle_linux.cc @@ -4,7 +4,9 @@ #include "chrome/common/resource_bundle.h" +#include "base/base_paths.h" #include "base/data_pack.h" +#include "base/file_path.h" #include "base/file_util.h" #include "base/logging.h" #include "base/path_service.h" @@ -26,9 +28,16 @@ ResourceBundle::~ResourceBundle() { } void ResourceBundle::LoadResources(const std::wstring& pref_locale) { - // TODO(tc): Load the .pak files to locale_resources_data_ and - // resources_data_. - NOTIMPLEMENTED(); + FilePath resources_data_path; + PathService::Get(base::DIR_EXE, &resources_data_path); + resources_data_path = resources_data_path.Append( + FILE_PATH_LITERAL("chrome.pak")); + DCHECK(resources_data_ == NULL) << "resource data already loaded!"; + resources_data_ = new base::DataPack; + bool success = resources_data_->Load(resources_data_path); + DCHECK(success) << "failed to load chrome.pak"; + + // TODO(tc): Load the .pak file for locale_resources_data_. } FilePath ResourceBundle::GetLocaleFilePath(const std::wstring& pref_locale) { @@ -43,8 +52,12 @@ FilePath ResourceBundle::GetLocaleFilePath(const std::wstring& pref_locale) { } void ResourceBundle::LoadThemeResources() { - // TODO(tc): Load the theme .pak file. - NOTIMPLEMENTED(); + FilePath theme_data_path; + PathService::Get(chrome::DIR_THEMES, &theme_data_path); + theme_data_path = theme_data_path.Append(FILE_PATH_LITERAL("default.pak")); + theme_data_ = new base::DataPack; + bool success = theme_data_->Load(theme_data_path); + DCHECK(success) << "failed to load theme data"; } /* static */ |