license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #ifndef CHROME_COMMON_RESOURCE_BUNDLE_H__ |
| 6 | #define CHROME_COMMON_RESOURCE_BUNDLE_H__ |
| 7 | |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 8 | #include "build/build_config.h" |
| 9 | |
| 10 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | #include <windows.h> |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 12 | #endif |
| 13 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include <map> |
| 15 | #include <string> |
| 16 | |
| 17 | #include "base/basictypes.h" |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 18 | #include "base/file_path.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | #include "base/lock.h" |
| 20 | #include "base/scoped_ptr.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 21 | |
[email protected] | 1a14188 | 2009-01-13 21:35:32 | [diff] [blame] | 22 | #if defined(OS_LINUX) |
| 23 | namespace base { |
| 24 | class DataPack; |
| 25 | }; |
[email protected] | e6ba540 | 2009-02-20 19:15:02 | [diff] [blame^] | 26 | typedef struct _GdkPixbuf GdkPixbuf; |
[email protected] | 1a14188 | 2009-01-13 21:35:32 | [diff] [blame] | 27 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | class ChromeFont; |
| 29 | class SkBitmap; |
| 30 | class StringPiece; |
| 31 | |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 32 | // ResourceBundle is a central facility to load images and other resources, |
| 33 | // such as theme graphics. |
| 34 | // Every resource is loaded only once. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | class ResourceBundle { |
| 36 | public: |
| 37 | // An enumeration of the various font styles used throughout Chrome. |
| 38 | // The following holds true for the font sizes: |
| 39 | // Small <= Base <= Medium <= MediumBold <= Large. |
| 40 | enum FontStyle { |
| 41 | SmallFont, |
| 42 | BaseFont, |
| 43 | MediumFont, |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 44 | // NOTE: depending upon the locale, this may *not* result in a bold font. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | MediumBoldFont, |
| 46 | LargeFont, |
| 47 | WebFont |
| 48 | }; |
| 49 | |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 50 | // Initialize the ResourceBundle for this process. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 51 | static void InitSharedInstance(const std::wstring& pref_locale); |
| 52 | |
| 53 | // Delete the ResourceBundle for this process if it exists. |
| 54 | static void CleanupSharedInstance(); |
| 55 | |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 56 | // Return the global resource loader instance. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 57 | static ResourceBundle& GetSharedInstance(); |
| 58 | |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 59 | // Load the data file that contains theme resources if present. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 60 | void LoadThemeResources(); |
| 61 | |
| 62 | // Gets the bitmap with the specified resource_id, first by looking into the |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 63 | // theme data, than in the current module data if applicable. |
| 64 | // Returns a pointer to a shared instance of the SkBitmap in the given out |
| 65 | // parameter. This shared bitmap is owned by the resource bundle and should |
| 66 | // not be freed. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 67 | // |
| 68 | // The bitmap is assumed to exist. This function will log in release, and |
| 69 | // assert in debug mode if it does not. On failure, this will return a |
| 70 | // pointer to a shared empty placeholder bitmap so it will be visible what |
| 71 | // is missing. |
| 72 | SkBitmap* GetBitmapNamed(int resource_id); |
| 73 | |
| 74 | // Loads the raw bytes of an image resource into |bytes|, |
| 75 | // without doing any processing or interpretation of |
| 76 | // the resource. Returns whether we successfully read the resource. |
| 77 | bool LoadImageResourceBytes(int resource_id, |
| 78 | std::vector<unsigned char>* bytes); |
| 79 | |
| 80 | // Loads the raw bytes of a data resource into |bytes|, |
| 81 | // without doing any processing or interpretation of |
| 82 | // the resource. Returns whether we successfully read the resource. |
| 83 | bool LoadDataResourceBytes(int resource_id, |
| 84 | std::vector<unsigned char>* bytes); |
| 85 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 86 | // Return the contents of a file in a string given the resource id. |
| 87 | // This will copy the data from the resource and return it as a string. |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 88 | // TODO(port): deprecate this and replace with GetRawDataResource to avoid |
| 89 | // needless copying. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 90 | std::string GetDataResource(int resource_id); |
| 91 | |
| 92 | // Like GetDataResource(), but avoids copying the resource. Instead, it |
| 93 | // returns a StringPiece which points into the actual resource in the image. |
| 94 | StringPiece GetRawDataResource(int resource_id); |
| 95 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 96 | // Get a localized string given a message id. Returns an empty |
| 97 | // string if the message_id is not found. |
| 98 | std::wstring GetLocalizedString(int message_id); |
| 99 | |
| 100 | // Returns the font for the specified style. |
| 101 | ChromeFont GetFont(FontStyle style); |
| 102 | |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 103 | #if defined(OS_WIN) |
| 104 | // Loads and returns an icon from the theme dll. |
| 105 | HICON LoadThemeIcon(int icon_id); |
| 106 | |
| 107 | // Loads and returns the global accelerators. |
| 108 | HACCEL GetGlobalAccelerators(); |
| 109 | |
| 110 | // Loads and returns a cursor from the app module. |
| 111 | HCURSOR LoadCursor(int cursor_id); |
[email protected] | e6ba540 | 2009-02-20 19:15:02 | [diff] [blame^] | 112 | #elif defined(OS_LINUX) |
| 113 | // Load a theme image as a GdkPixbuf. |
| 114 | GdkPixbuf* LoadPixbuf(int resource_id); |
| 115 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 116 | |
| 117 | private: |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 118 | // We define a DataHandle typedef to abstract across how data is stored |
| 119 | // across platforms. |
| 120 | #if defined(OS_WIN) |
| 121 | // Windows stores resources in DLLs, which are managed by HINSTANCE. |
| 122 | typedef HINSTANCE DataHandle; |
| 123 | #elif defined(OS_LINUX) |
[email protected] | 2aad8bb | 2009-02-04 19:00:50 | [diff] [blame] | 124 | // Linux uses base::DataPack. |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 125 | typedef base::DataPack* DataHandle; |
[email protected] | 0bc2a57b | 2009-02-04 20:32:54 | [diff] [blame] | 126 | #elif defined(OS_MACOSX) |
| 127 | // TODO(port): Implement resource loading on OS X. |
| 128 | typedef void* DataHandle; |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 129 | #endif |
| 130 | |
| 131 | // Ctor/dtor are private, since we're a singleton. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 132 | ResourceBundle(); |
| 133 | ~ResourceBundle(); |
| 134 | |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 135 | // Free skia_images_. |
| 136 | void FreeImages(); |
| 137 | |
[email protected] | 2aad8bb | 2009-02-04 19:00:50 | [diff] [blame] | 138 | // Try to load the main resources and the locale specific strings from an |
| 139 | // external data module. |
| 140 | void LoadResources(const std::wstring& pref_locale); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 141 | |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 142 | // Initialize all the ChromeFont members if they haven't yet been initialized. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 143 | void LoadFontsIfNecessary(); |
| 144 | |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 145 | // Returns the full pathname of the locale file to load. May return an empty |
| 146 | // string if no locale data files are found. |
| 147 | FilePath GetLocaleFilePath(const std::wstring& pref_locale); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 148 | |
| 149 | // Loads the raw bytes of a resource from |module| into |bytes|, |
| 150 | // without doing any processing or interpretation of |
| 151 | // the resource. Returns whether we successfully read the resource. |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 152 | static bool LoadResourceBytes(DataHandle module, |
| 153 | int resource_id, |
| 154 | std::vector<unsigned char>* bytes); |
| 155 | |
| 156 | // Creates and returns a new SkBitmap given the data file to look in and the |
| 157 | // resource id. It's up to the caller to free the returned bitmap when |
| 158 | // done. |
| 159 | static SkBitmap* LoadBitmap(DataHandle dll_inst, int resource_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 160 | |
| 161 | // Class level lock. Used to protect internal data structures that may be |
| 162 | // accessed from other threads (e.g., skia_images_). |
| 163 | Lock lock_; |
| 164 | |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 165 | // Handles for data sources. |
[email protected] | 2aad8bb | 2009-02-04 19:00:50 | [diff] [blame] | 166 | DataHandle resources_data_; |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 167 | DataHandle locale_resources_data_; |
| 168 | DataHandle theme_data_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 169 | |
| 170 | // Cached images. The ResourceBundle caches all retrieved bitmaps and keeps |
| 171 | // ownership of the pointers. |
| 172 | typedef std::map<int, SkBitmap*> SkImageMap; |
| 173 | SkImageMap skia_images_; |
| 174 | |
| 175 | // The various fonts used. Cached to avoid repeated GDI creation/destruction. |
| 176 | scoped_ptr<ChromeFont> base_font_; |
| 177 | scoped_ptr<ChromeFont> small_font_; |
| 178 | scoped_ptr<ChromeFont> medium_font_; |
| 179 | scoped_ptr<ChromeFont> medium_bold_font_; |
| 180 | scoped_ptr<ChromeFont> large_font_; |
| 181 | scoped_ptr<ChromeFont> web_font_; |
| 182 | |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 183 | static ResourceBundle* g_shared_instance_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 184 | |
| 185 | DISALLOW_EVIL_CONSTRUCTORS(ResourceBundle); |
| 186 | }; |
| 187 | |
| 188 | #endif // CHROME_COMMON_RESOURCE_BUNDLE_H__ |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 189 | |