]>
Commit | Line | Data |
---|---|---|
ea67ba0f VS |
1 | How to add new bitmaps to wxWindows UI elements |
2 | =============================================== | |
3 | ||
4 | 0. Introduction | |
5 | --------------- | |
6 | ||
a5823953 VZ |
7 | Since the introduction of wxArtProvider class, it is no longer desired to |
8 | hardcode art resources (e.g. icons and toolbar or button bitmaps) into the | |
9 | code. This was previously done either by including the bitmap in win32 | |
10 | resource file (include/wx/msw/wx.rc) or by including XPM files in the code. | |
ea67ba0f | 11 | |
a5823953 VZ |
12 | wxArtProvider should be used instead, to allow users to customize the look of |
13 | their wxWindows app. This technote is a detailed description of steps needed | |
14 | when adding new bitmap/icon. | |
ea67ba0f VS |
15 | |
16 | 1. Adding new resource | |
17 | ---------------------- | |
18 | ||
a5823953 VZ |
19 | (Please see wxArtProvider reference documentation for explanation of "art ID" |
20 | and "art client" terms.) | |
ea67ba0f | 21 | |
a5823953 VZ |
22 | First of all, you have to add new wxArtID constant to include/wx/artprov.h. |
23 | Look for "Art IDs" and add new definition to the list, e.g. | |
ea67ba0f VS |
24 | #define wxART_MY_BITMAP _T("my_bitmap") |
25 | ||
a5823953 VZ |
26 | It may happen that the intended use of the new resource doesn't fit into any |
27 | of defined client categories (search for "Art clients" in the header). In case | |
28 | the new resource is part of a larger category, you need to define a new | |
29 | client. Just add it to the list of existing clients: | |
30 | #define wxART_MY_CLIENT _T("my_client_C") | |
ea67ba0f VS |
31 | (Note that you *have* to add the trailing "_C"!) |
32 | ||
a5823953 VZ |
33 | Alternatively, you may use wxART_OTHER when accessing the resource if the |
34 | bitmap is standalone. | |
ea67ba0f | 35 | |
a5823953 VZ |
36 | Once the header is updated, it's time to add XPM file with the bitmap to |
37 | $(wx)/art. Add it to $(wx)/art if it is platform-independent or to | |
38 | $(wx)/art/$(toolkit) if it is something specific to one of the toolkits. Note | |
39 | that "specific to one of the toolkits" doesn't mean that the bitmap is *used* | |
40 | by only one toolkit, but that it doesn't make sense for any of the others! For | |
41 | example, a GTK wxART_WARNING icon ($(wx)/art/gtk/warning.xpm) is specific to | |
42 | wxGTK, but new_dir.xpm makes sense even under wxMSW even though it is | |
43 | currently only used by the generic file dialog. Remember that wxArtProvider | |
44 | can be used by users, not only the library. | |
ea67ba0f | 45 | |
a5823953 VZ |
46 | Finally, wxDefaultArtProvider in $(wx)/src/common/artstd.cpp must be updated. |
47 | This consists of two steps: | |
ea67ba0f | 48 | |
a5823953 VZ |
49 | a) add #include line for your XPM file, e.g. #include "../../art/my_bmp.xpm" |
50 | b) add ART(...) line to wxDefaultArtProvider::CreateBitmap(). The first | |
51 | argument is wxArtID, the other is XPM file name (w/o extension), e.g. | |
52 | ART(wxART_MY_BITMAP, my_bmp) | |
ea67ba0f VS |
53 | |
54 | That's all. The bitmap is now available to wxArtProvider users. | |
55 | ||
a5823953 VZ |
56 | Note: there's no difference between icons and bitmaps, always treat them as |
57 | bitmaps inside wx(Default)ArtProvider. | |
ea67ba0f VS |
58 | |
59 | 2. Accessing the resource | |
60 | ------------------------- | |
61 | ||
a5823953 VZ |
62 | The file that will use the bitmap needs to include "wx/artprov.h". The code to |
63 | access the bitmap (or icon) is trivial: | |
ea67ba0f VS |
64 | |
65 | wxBitmap bmp = wxArtProvider::GetBitmap(wxART_MY_BITMAP, wxART_MY_CLIENT); | |
66 | // this would be "wxBitmap bmp(my_bmp_xpm);" before | |
67 | wxIcon icon = wxArtProvider::GetIcon(wxART_MY_ICON, wxART_MY_CLIENT); | |
68 | ||
a5823953 VZ |
69 | Substitute wxART_MY_CLIENT in the example with a suitable client ID. If the |
70 | client is wxART_OTHER you may write only | |
71 | ||
72 | wxArtProvider::GetBitmap(wxART_MY_BITMAP). | |
ea67ba0f VS |
73 | |
74 | 3. Providing a demo | |
75 | ------------------- | |
76 | ||
a5823953 VZ |
77 | It is highly desirable to let the users know what stock bitmaps are available |
78 | in wxWindows. The "artprov" sample serves this purpose: it contains a browser | |
79 | dialog that displays all available art resources. | |
ea67ba0f | 80 | |
a5823953 VZ |
81 | It has to be updated to accomodate for new bitmaps. Fortunately, this is |
82 | trivial: open $(wx)/samples/artprov/artbrows.cpp in text editor and | |
83 | ART_ICON(wxART_MY_BITMAP) line to the FillBitmaps() function. | |
ea67ba0f VS |
84 | |
85 | Similarly, if you add a new client, please update FillClients() by adding new | |
86 | client to the end of the list. | |
87 | ||
88 | === EOF === | |
89 | ||
90 | Version: $Id$ |