tn0015.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. How to add new bitmaps to wxWidgets UI elements
  2. ===============================================
  3. 0. Introduction
  4. ---------------
  5. Since the introduction of wxArtProvider class, it is no longer desired to
  6. hardcode art resources (e.g. icons and toolbar or button bitmaps) into the
  7. code. This was previously done either by including the bitmap in win32
  8. resource file (include/wx/msw/wx.rc) or by including XPM files in the code.
  9. wxArtProvider should be used instead, to allow users to customize the look of
  10. their wxWidgets app. This technote is a detailed description of steps needed
  11. when adding new bitmap/icon.
  12. 1. Adding new resource
  13. ----------------------
  14. (Please see wxArtProvider reference documentation for explanation of "art ID"
  15. and "art client" terms.)
  16. First of all, you have to add new wxArtID constant to include/wx/artprov.h.
  17. Look for "Art IDs" and add new definition to the list, e.g.
  18. #define wxART_MY_BITMAP wxART_MAKE_ART_ID(wxART_MY_BITMAP)
  19. Add it to interface/wx/artprov.h, too.
  20. It may happen that the intended use of the new resource doesn't fit into any
  21. of defined client categories (search for "Art clients" in the header). In case
  22. the new resource is part of a larger category, you need to define a new
  23. client. Just add it to the list of existing clients (and don't forget to
  24. update artprov.tex):
  25. #define wxART_MY_CLIENT wxART_MAKE_CLIENT_ID(wxART_MY_CLIENT)
  26. Alternatively, you may use wxART_OTHER when accessing the resource if the
  27. bitmap is standalone.
  28. Once the header is updated, it's time to add XPM file with the bitmap to
  29. $(wx)/art. Add it to $(wx)/art if it is platform-independent or to
  30. $(wx)/art/$(toolkit) if it is something specific to one of the toolkits. Note
  31. that "specific to one of the toolkits" doesn't mean that the bitmap is *used*
  32. by only one toolkit, but that it doesn't make sense for any of the others! For
  33. example, a GTK wxART_WARNING icon ($(wx)/art/gtk/warning.xpm) is specific to
  34. wxGTK, but new_dir.xpm makes sense even under wxMSW even though it is
  35. currently only used by the generic file dialog. Remember that wxArtProvider
  36. can be used by users, not only the library.
  37. Finally, wxDefaultArtProvider in $(wx)/src/common/artstd.cpp must be updated.
  38. This consists of two steps:
  39. a) add #include line for your XPM file, e.g. #include "../../art/my_bmp.xpm"
  40. b) add ART(...) line to wxDefaultArtProvider::CreateBitmap(). The first
  41. argument is wxArtID, the other is XPM file name (w/o extension), e.g.
  42. ART(wxART_MY_BITMAP, my_bmp)
  43. That's all. The bitmap is now available to wxArtProvider users.
  44. Note: there's no difference between icons and bitmaps, always treat them as
  45. bitmaps inside wx(Default)ArtProvider.
  46. 1b. Adding Tango version of the resource.
  47. -----------------------------------------
  48. While all the bitmaps are provided in XPM format so that they are available in
  49. all builds of wxWidgets, we also provide most of them in PNG format with full
  50. transparency support that is not available in XPM. Another advantage of the PNG
  51. versions is that the icons used are those of the Tango project and so have the
  52. consistent look, unlike the XPM ones.
  53. So if you an icon exists in http://tango.freedesktop.org/Tango_Icon_Gallery you
  54. should add it too. For this you need to:
  55. 1. Convert the PNG to a C array of bytes suitable for inclusion in the code.
  56. This is done using misc/scripts/png2c.py script, e.g. if the variable "f"
  57. contains the name of the icon you want to add and you have installed Tango
  58. icons in a standard location under a Linux system:
  59. ./misc/scripts/png2c.py -s /usr/share/icons/Tango/{16x16,24x24}/*/$f.png >
  60. art/tango/${f//-/_}.h
  61. Of course, the same command may be ran with different paths under Windows.
  62. Just remember to add both 16 and 24 pixel versions of the bitmap to the
  63. header and use the "-s" option to embed the image size in its array name.
  64. 2. Add #include for the newly created file to src/common/arttango.cpp.
  65. 3. Add an entry to s_allBitmaps array in the same file.
  66. 2. Accessing the resource
  67. -------------------------
  68. The file that will use the bitmap needs to include "wx/artprov.h". The code to
  69. access the bitmap (or icon) is trivial:
  70. wxBitmap bmp = wxArtProvider::GetBitmap(wxART_MY_BITMAP, wxART_MY_CLIENT);
  71. // this would be "wxBitmap bmp(my_bmp_xpm);" before
  72. wxIcon icon = wxArtProvider::GetIcon(wxART_MY_ICON, wxART_MY_CLIENT);
  73. Substitute wxART_MY_CLIENT in the example with a suitable client ID. If the
  74. client is wxART_OTHER you may write only
  75. wxArtProvider::GetBitmap(wxART_MY_BITMAP).
  76. 3. Providing a demo
  77. -------------------
  78. It is highly desirable to let the users know what stock bitmaps are available
  79. in wxWidgets. The "artprov" sample serves this purpose: it contains a browser
  80. dialog that displays all available art resources.
  81. It has to be updated to accommodate 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.
  84. Similarly, if you add a new client, please update FillClients() by adding new
  85. client to the end of the list.
  86. === EOF ===