diff options
author | sail@chromium.org <sail@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-12 23:41:53 +0000 |
---|---|---|
committer | sail@chromium.org <sail@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-12 23:41:53 +0000 |
commit | ec015c8457dd6978ad0d86fd04738aa4a12867a4 (patch) | |
tree | ddd3873f745e968db94c1cc7b3dae16ab27545e6 /ui/base/resource/resource_bundle.h | |
parent | 7ad2d26cf0d1eb496f3372ad02ae21b93778fe78 (diff) | |
download | chromium_src-ec015c8457dd6978ad0d86fd04738aa4a12867a4.zip chromium_src-ec015c8457dd6978ad0d86fd04738aa4a12867a4.tar.gz chromium_src-ec015c8457dd6978ad0d86fd04738aa4a12867a4.tar.bz2 |
Metro/HiDPI: Cleanup ResourceBundle
For Metro and HiDPI support we need to conditionally load resource pak files. For example, on Windows I'd like to have something like this:
..ResourceBundle::LoadCommonResources() {
....if (IsMetroMode()) {
......switch (DPIScale()) {
........case 1.0: AddDataPack("theme_resources_metro_1_0x.pak"); break;
........case 1.4: AddDataPack("theme_resources_metro_1_4x.pak"); break;
........case 1.8: AddDataPack("theme_resources_metro_1_8x.pak"); break;
......}
....} else {
......if (DPIScale() < 1.5)
........AddDataPack("theme_resources_desktop_1_0x.pak");
......else
........AddDataPack("theme_resources_desktop_2_0x.pak");
....}
..}
As a first step this CL does the following:
- create a new ResourceHandle abstract class that can be backed by a DLL on Windows.
- manage all resource packs through the data_packs_ vector
- clean up ResourceBundle interface by removing all the #ifdefs
BUG=114311
TEST=Built and ran on Mac, Linux, Windows, Linux Arua.
Review URL: http://codereview.chromium.org/10051012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@132091 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/base/resource/resource_bundle.h')
-rw-r--r-- | ui/base/resource/resource_bundle.h | 127 |
1 files changed, 24 insertions, 103 deletions
diff --git a/ui/base/resource/resource_bundle.h b/ui/base/resource/resource_bundle.h index 668823b..e91cab1 100644 --- a/ui/base/resource/resource_bundle.h +++ b/ui/base/resource/resource_bundle.h @@ -8,19 +8,15 @@ #include "build/build_config.h" -#if defined(OS_WIN) -#include <windows.h> -#endif - #include <map> #include <string> -#include <vector> #include "base/basictypes.h" #include "base/file_path.h" #include "base/gtest_prod_util.h" #include "base/memory/ref_counted_memory.h" #include "base/memory/scoped_ptr.h" +#include "base/memory/scoped_vector.h" #include "base/string16.h" #include "base/string_piece.h" #include "ui/base/ui_export.h" @@ -37,17 +33,12 @@ class Font; class Image; } -#if defined(USE_X11) -typedef struct _GdkPixbuf GdkPixbuf; -#endif - namespace ui { -class DataPack; +class ResourceHandle; // ResourceBundle is a central facility to load images and other resources, -// such as theme graphics. -// Every resource is loaded only once. +// such as theme graphics. Every resource is loaded only once. class UI_EXPORT ResourceBundle { public: // An enumeration of the various font styles used throughout Chrome. @@ -65,6 +56,13 @@ class UI_EXPORT ResourceBundle { LargeBoldFont, }; + enum ImageRTL { + // Images are flipped in RTL locales. + RTL_ENABLED, + // Images are never flipped. + RTL_DISABLED, + }; + // Initialize the ResourceBundle for this process. Returns the language // selected. // NOTE: Mac ignores this and always loads up resources for the language @@ -75,16 +73,6 @@ class UI_EXPORT ResourceBundle { // Initialize the ResourceBundle using given data pack path for testing. static void InitSharedInstanceWithPakFile(const FilePath& path); - // Load a .pak file. Returns NULL if we fail to load |path|. The caller - // is responsible for deleting up this pointer. - static DataPack* LoadResourcesDataPak(const FilePath& path); - - // Registers additional data pack files with the global ResourceBundle. When - // looking for a DataResource, we will search these files after searching the - // main module. This method is not thread safe! You should call it - // immediately after calling InitSharedInstance. - static void AddDataPackToSharedInstance(const FilePath& path); - // Delete the ResourceBundle for this process if it exists. static void CleanupSharedInstance(); @@ -97,6 +85,12 @@ class UI_EXPORT ResourceBundle { // Check if the .pak for the given locale exists. static bool LocaleDataPakExists(const std::string& locale); + // Registers additional data pack files with the global ResourceBundle. When + // looking for a DataResource, we will search these files after searching the + // main module. This method is not thread safe! You should call it + // immediately after calling InitSharedInstance. + void AddDataPack(const FilePath& path); + // Changes the locale for an already-initialized ResourceBundle, returning the // name of the newly-loaded locale. Future calls to get strings will return // the strings for this new locale. This has no effect on existing or future @@ -123,6 +117,11 @@ class UI_EXPORT ResourceBundle { // Note that if the same resource has already been loaded in GetImageNamed(), // gfx::Image will perform a conversion, rather than using the native image // loading code of ResourceBundle. + // + // If |rtl| is RTL_ENABLED then the image is flipped in RTL locales. + gfx::Image& GetNativeImageNamed(int resource_id, ImageRTL rtl); + + // Same as GetNativeImageNamed() except that RTL is not enabled. gfx::Image& GetNativeImageNamed(int resource_id); // Loads the raw bytes of a data resource into |bytes|, @@ -148,68 +147,9 @@ class UI_EXPORT ResourceBundle { // loaded. Pass an empty path to undo. void OverrideLocalePakForTest(const FilePath& pak_path); -#if defined(OS_WIN) - // NOTE: This needs to be called before initializing the shared instance if - // your resources are not stored in the executable. - static void SetResourcesDataDLL(HINSTANCE handle); - - // Loads and returns an icon from the app module. - HICON LoadThemeIcon(int icon_id); - - // Loads and returns a cursor from the app module. - HCURSOR LoadCursor(int cursor_id); -#elif defined(TOOLKIT_GTK) - // Gets the GdkPixbuf with the specified resource_id from the main data pak - // file. Returns a pointer to a shared instance of the GdkPixbuf. This - // shared GdkPixbuf is owned by the resource bundle and should not be freed. - // - // The bitmap is assumed to exist. This function will log in release, and - // assert in debug mode if it does not. On failure, this will return a - // pointer to a shared empty placeholder bitmap so it will be visible what - // is missing. - // This function flips it in RTL locales. - GdkPixbuf* GetRTLEnabledPixbufNamed(int resource_id); - - // Same as above, but returns a gfx::Image wrapping the GdkPixbuf. - gfx::Image& GetRTLEnabledImageNamed(int resource_id); - - private: - // Shared implementation for the above two functions. - gfx::Image* GetPixbufImpl(int resource_id, bool rtl_enabled); - - public: -#endif - private: FRIEND_TEST_ALL_PREFIXES(ResourceBundle, LoadDataResourceBytes); - // Helper class for managing data packs. - class LoadedDataPack { - public: - explicit LoadedDataPack(const FilePath& path); - ~LoadedDataPack(); - bool GetStringPiece(int resource_id, base::StringPiece* data) const; - RefCountedStaticMemory* GetStaticMemory(int resource_id) const; - - private: - void Load(); - - scoped_ptr<DataPack> data_pack_; - FilePath path_; - - DISALLOW_COPY_AND_ASSIGN(LoadedDataPack); - }; - - // We define a DataHandle typedef to abstract across how data is stored - // across platforms. -#if defined(OS_WIN) - // Windows stores resources in DLLs, which are managed by HINSTANCE. - typedef HINSTANCE DataHandle; -#elif defined(OS_POSIX) - // Everyone else uses base::DataPack. - typedef DataPack* DataHandle; -#endif - // Ctor/dtor are private, since we're a singleton. ResourceBundle(); ~ResourceBundle(); @@ -234,29 +174,14 @@ class UI_EXPORT ResourceBundle { // Initialize all the gfx::Font members if they haven't yet been initialized. void LoadFontsIfNecessary(); -#if defined(OS_POSIX) - // Returns the full pathname of the main resources file to load. May return - // an empty string if no main resources data files are found. - static FilePath GetResourcesFilePath(); - - static FilePath GetLargeIconResourcesFilePath(); -#endif - // Returns the full pathname of the locale file to load. May return an empty // string if no locale data files are found. static FilePath GetLocaleFilePath(const std::string& app_locale); - // Returns a handle to bytes from the resource |module|, without doing any - // processing or interpretation of the resource. Returns whether we - // successfully read the resource. Caller does not own the data returned - // through this method and must not modify the data pointed to by |bytes|. - static RefCountedStaticMemory* LoadResourceBytes(DataHandle module, - int resource_id); - // Creates and returns a new SkBitmap given the data file to look in and the // resource id. It's up to the caller to free the returned bitmap when // done. - static SkBitmap* LoadBitmap(DataHandle dll_inst, int resource_id); + SkBitmap* LoadBitmap(const ResourceHandle& dll_inst, int resource_id); // Returns an empty image for when a resource cannot be loaded. This is a // bright red bitmap. @@ -271,12 +196,8 @@ class UI_EXPORT ResourceBundle { scoped_ptr<base::Lock> locale_resources_data_lock_; // Handles for data sources. - DataHandle resources_data_; - DataHandle large_icon_resources_data_; - scoped_ptr<DataPack> locale_resources_data_; - - // References to extra data packs loaded via AddDataPackToSharedInstance. - std::vector<LoadedDataPack*> data_packs_; + scoped_ptr<ResourceHandle> locale_resources_data_; + ScopedVector<ResourceHandle> data_packs_; // Cached images. The ResourceBundle caches all retrieved images and keeps // ownership of the pointers. |