blob: 983f34bdc690d1189e629f113fdab159dd9fb72f [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.commit09911bf2008-07-26 23:55:294
5#ifndef CHROME_COMMON_RESOURCE_BUNDLE_H__
6#define CHROME_COMMON_RESOURCE_BUNDLE_H__
7
[email protected]11c2fe82009-01-13 01:52:598#include "build/build_config.h"
9
10#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2911#include <windows.h>
[email protected]11c2fe82009-01-13 01:52:5912#endif
13
initial.commit09911bf2008-07-26 23:55:2914#include <map>
15#include <string>
16
17#include "base/basictypes.h"
[email protected]11c2fe82009-01-13 01:52:5918#include "base/file_path.h"
initial.commit09911bf2008-07-26 23:55:2919#include "base/lock.h"
20#include "base/scoped_ptr.h"
initial.commit09911bf2008-07-26 23:55:2921
[email protected]1a141882009-01-13 21:35:3222#if defined(OS_LINUX)
23namespace base {
24 class DataPack;
25};
[email protected]e6ba5402009-02-20 19:15:0226typedef struct _GdkPixbuf GdkPixbuf;
[email protected]1a141882009-01-13 21:35:3227#endif
initial.commit09911bf2008-07-26 23:55:2928class ChromeFont;
29class SkBitmap;
30class StringPiece;
31
[email protected]11c2fe82009-01-13 01:52:5932// ResourceBundle is a central facility to load images and other resources,
33// such as theme graphics.
34// Every resource is loaded only once.
initial.commit09911bf2008-07-26 23:55:2935class 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]11c2fe82009-01-13 01:52:5944 // NOTE: depending upon the locale, this may *not* result in a bold font.
initial.commit09911bf2008-07-26 23:55:2945 MediumBoldFont,
46 LargeFont,
47 WebFont
48 };
49
[email protected]11c2fe82009-01-13 01:52:5950 // Initialize the ResourceBundle for this process.
initial.commit09911bf2008-07-26 23:55:2951 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]11c2fe82009-01-13 01:52:5956 // Return the global resource loader instance.
initial.commit09911bf2008-07-26 23:55:2957 static ResourceBundle& GetSharedInstance();
58
[email protected]11c2fe82009-01-13 01:52:5959 // Load the data file that contains theme resources if present.
initial.commit09911bf2008-07-26 23:55:2960 void LoadThemeResources();
61
62 // Gets the bitmap with the specified resource_id, first by looking into the
[email protected]11c2fe82009-01-13 01:52:5963 // 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.commit09911bf2008-07-26 23:55:2967 //
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.commit09911bf2008-07-26 23:55:2986 // 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]11c2fe82009-01-13 01:52:5988 // TODO(port): deprecate this and replace with GetRawDataResource to avoid
89 // needless copying.
initial.commit09911bf2008-07-26 23:55:2990 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.commit09911bf2008-07-26 23:55:2996 // 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]11c2fe82009-01-13 01:52:59103#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]e6ba5402009-02-20 19:15:02112#elif defined(OS_LINUX)
113 // Load a theme image as a GdkPixbuf.
114 GdkPixbuf* LoadPixbuf(int resource_id);
115#endif
initial.commit09911bf2008-07-26 23:55:29116
117 private:
[email protected]11c2fe82009-01-13 01:52:59118 // 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]2aad8bb2009-02-04 19:00:50124 // Linux uses base::DataPack.
[email protected]11c2fe82009-01-13 01:52:59125 typedef base::DataPack* DataHandle;
[email protected]0bc2a57b2009-02-04 20:32:54126#elif defined(OS_MACOSX)
127 // TODO(port): Implement resource loading on OS X.
128 typedef void* DataHandle;
[email protected]11c2fe82009-01-13 01:52:59129#endif
130
131 // Ctor/dtor are private, since we're a singleton.
initial.commit09911bf2008-07-26 23:55:29132 ResourceBundle();
133 ~ResourceBundle();
134
[email protected]11c2fe82009-01-13 01:52:59135 // Free skia_images_.
136 void FreeImages();
137
[email protected]2aad8bb2009-02-04 19:00:50138 // 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.commit09911bf2008-07-26 23:55:29141
[email protected]11c2fe82009-01-13 01:52:59142 // Initialize all the ChromeFont members if they haven't yet been initialized.
initial.commit09911bf2008-07-26 23:55:29143 void LoadFontsIfNecessary();
144
[email protected]11c2fe82009-01-13 01:52:59145 // 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.commit09911bf2008-07-26 23:55:29148
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]11c2fe82009-01-13 01:52:59152 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.commit09911bf2008-07-26 23:55:29160
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]11c2fe82009-01-13 01:52:59165 // Handles for data sources.
[email protected]2aad8bb2009-02-04 19:00:50166 DataHandle resources_data_;
[email protected]11c2fe82009-01-13 01:52:59167 DataHandle locale_resources_data_;
168 DataHandle theme_data_;
initial.commit09911bf2008-07-26 23:55:29169
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]11c2fe82009-01-13 01:52:59183 static ResourceBundle* g_shared_instance_;
initial.commit09911bf2008-07-26 23:55:29184
185 DISALLOW_EVIL_CONSTRUCTORS(ResourceBundle);
186};
187
188#endif // CHROME_COMMON_RESOURCE_BUNDLE_H__
license.botbf09a502008-08-24 00:55:55189