]> git.saurik.com Git - wxWidgets.git/blob - interface/wx/bitmap.h
ff402220129949bede1f0111f786f13e37a8a476
[wxWidgets.git] / interface / wx / bitmap.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: bitmap.h
3 // Purpose: interface of wxBitmap* classes
4 // Author: wxWidgets team
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
7
8
9 /**
10 In wxBitmap and wxBitmapHandler context this value means: "use the screen depth".
11 */
12 #define wxBITMAP_SCREEN_DEPTH (-1)
13
14 /**
15 @class wxBitmapHandler
16
17 This is the base class for implementing bitmap file loading/saving, and
18 bitmap creation from data.
19 It is used within wxBitmap and is not normally seen by the application.
20
21 If you wish to extend the capabilities of wxBitmap, derive a class from
22 wxBitmapHandler and add the handler using wxBitmap::AddHandler() in your
23 application initialization.
24
25 Note that all wxBitmapHandlers provided by wxWidgets are part of the
26 @ref page_libs_wxcore library.
27 For details about the default handlers, please see the note in the
28 wxBitmap class documentation.
29
30 @library{wxcore}
31 @category{gdi}
32
33 @see @ref overview_bitmap, wxBitmap, wxIcon, wxCursor
34 */
35 class wxBitmapHandler : public wxObject
36 {
37 public:
38 /**
39 Default constructor.
40
41 In your own default constructor, initialise the members m_name,
42 m_extension and m_type.
43 */
44 wxBitmapHandler();
45
46 /**
47 Destroys the wxBitmapHandler object.
48 */
49 virtual ~wxBitmapHandler();
50
51 /**
52 Creates a bitmap from the given data, which can be of arbitrary type.
53 The wxBitmap object @a bitmap is manipulated by this function.
54
55 @param bitmap
56 The wxBitmap object.
57 @param width
58 The width of the bitmap in pixels.
59 @param height
60 The height of the bitmap in pixels.
61 @param depth
62 The depth of the bitmap in pixels.
63 If this is ::wxBITMAP_SCREEN_DEPTH, the screen depth is used.
64 @param data
65 Data whose type depends on the value of type.
66 @param type
67 A bitmap type identifier - see ::wxBitmapType for a list
68 of possible values.
69
70 @return @true if the call succeeded, @false otherwise (the default).
71 */
72 virtual bool Create(wxBitmap* bitmap, const void* data, wxBitmapType type,
73 int width, int height, int depth = 1);
74
75 /**
76 Gets the file extension associated with this handler.
77 */
78 const wxString& GetExtension() const;
79
80 /**
81 Gets the name of this handler.
82 */
83 const wxString& GetName() const;
84
85 /**
86 Gets the bitmap type associated with this handler.
87 */
88 wxBitmapType GetType() const;
89
90 /**
91 Loads a bitmap from a file or resource, putting the resulting data into
92 @a bitmap.
93
94 @param bitmap
95 The bitmap object which is to be affected by this operation.
96 @param name
97 Either a filename or a Windows resource name.
98 The meaning of name is determined by the type parameter.
99 @param type
100 See ::wxBitmapType for values this can take.
101 @param desiredWidth
102 The desired width for the loaded bitmap.
103 @param desiredHeight
104 The desired height for the loaded bitmap.
105
106 @return @true if the operation succeeded, @false otherwise.
107
108 @see wxBitmap::LoadFile, wxBitmap::SaveFile, SaveFile()
109 */
110 virtual bool LoadFile(wxBitmap* bitmap, const wxString& name, wxBitmapType type,
111 int desiredWidth, int desiredHeight);
112
113 /**
114 Saves a bitmap in the named file.
115
116 @param bitmap
117 The bitmap object which is to be affected by this operation.
118 @param name
119 A filename. The meaning of name is determined by the type parameter.
120 @param type
121 See ::wxBitmapType for values this can take.
122 @param palette
123 An optional palette used for saving the bitmap.
124
125 @return @true if the operation succeeded, @false otherwise.
126
127 @see wxBitmap::LoadFile, wxBitmap::SaveFile, LoadFile()
128 */
129 virtual bool SaveFile(const wxBitmap* bitmap, const wxString& name, wxBitmapType type,
130 const wxPalette* palette = NULL) const;
131
132 /**
133 Sets the handler extension.
134
135 @param extension
136 Handler extension.
137 */
138 void SetExtension(const wxString& extension);
139
140 /**
141 Sets the handler name.
142
143 @param name
144 Handler name.
145 */
146 void SetName(const wxString& name);
147
148 /**
149 Sets the handler type.
150
151 @param type
152 Handler type.
153 */
154 void SetType(wxBitmapType type);
155 };
156
157
158 /**
159 @class wxBitmap
160
161 This class encapsulates the concept of a platform-dependent bitmap,
162 either monochrome or colour or colour with alpha channel support.
163
164 If you need direct access the bitmap data instead going through
165 drawing to it using wxMemoryDC you need to use the wxPixelData
166 class (either wxNativePixelData for RGB bitmaps or wxAlphaPixelData
167 for bitmaps with an additionally alpha channel).
168
169 Note that many wxBitmap functions take a @e type parameter, which is a
170 value of the ::wxBitmapType enumeration.
171 The validity of those values depends however on the platform where your program
172 is running and from the wxWidgets configuration.
173 If all possible wxWidgets settings are used:
174 - wxMSW supports BMP and ICO files, BMP and ICO resources;
175 - wxGTK supports XPM files;
176 - wxMac supports PICT resources;
177 - wxX11 supports XPM files, XPM data, XBM data;
178
179 In addition, wxBitmap can load and save all formats that wxImage can; see wxImage
180 for more info. Of course, you must have loaded the wxImage handlers
181 (see ::wxInitAllImageHandlers() and wxImage::AddHandler).
182 Note that all available wxBitmapHandlers for a given wxWidgets port are
183 automatically loaded at startup so you won't need to use wxBitmap::AddHandler.
184
185 More on the difference between wxImage and wxBitmap: wxImage is just a
186 buffer of RGB bytes with an optional buffer for the alpha bytes. It is all
187 generic, platform independent and image file format independent code. It
188 includes generic code for scaling, resizing, clipping, and other manipulations
189 of the image data. OTOH, wxBitmap is intended to be a wrapper of whatever is
190 the native image format that is quickest/easiest to draw to a DC or to be the
191 target of the drawing operations performed on a wxMemoryDC. By splitting the
192 responsibilities between wxImage/wxBitmap like this then it's easier to use
193 generic code shared by all platforms and image types for generic operations and
194 platform specific code where performance or compatibility is needed.
195
196 @library{wxcore}
197 @category{gdi}
198
199 @stdobjects
200 ::wxNullBitmap
201
202 @see @ref overview_bitmap, @ref overview_bitmap_supportedformats,
203 wxDC::Blit, wxIcon, wxCursor, wxMemoryDC, wxImage, wxPixelData
204 */
205 class wxBitmap : public wxGDIObject
206 {
207 public:
208 /**
209 Default constructor.
210
211 Constructs a bitmap object with no data; an assignment or another member
212 function such as Create() or LoadFile() must be called subsequently.
213 */
214 wxBitmap();
215
216 /**
217 Copy constructor, uses @ref overview_refcount "reference counting".
218 To make a real copy, you can use:
219
220 @code
221 wxBitmap newBitmap = oldBitmap.GetSubBitmap(
222 wxRect(0, 0, oldBitmap.GetWidth(), oldBitmap.GetHeight()));
223 @endcode
224 */
225 wxBitmap(const wxBitmap& bitmap);
226
227
228 /*
229 Creates a bitmap from the given @a data which is interpreted in
230 platform-dependent manner.
231
232 @param data
233 Specifies the bitmap data in a platform-dependent format.
234 @param type
235 May be one of the ::wxBitmapType values and indicates which type of
236 bitmap does @a data contains. See the note in the class
237 detailed description.
238 @param width
239 Specifies the width of the bitmap.
240 @param height
241 Specifies the height of the bitmap.
242 @param depth
243 Specifies the depth of the bitmap.
244 If this is omitted, the display depth of the screen is used.
245 wxBitmap(const void* data, int type, int width, int height, int depth = -1);
246
247
248 NOTE: this ctor is not implemented by all ports, is somewhat useless
249 without further description of the "data" supported formats and
250 uses 'int type' instead of wxBitmapType, so don't document it.
251 */
252
253 /**
254 Creates a bitmap from the given array @a bits.
255 You should only use this function for monochrome bitmaps (depth 1) in
256 portable programs: in this case the bits parameter should contain an XBM image.
257
258 For other bit depths, the behaviour is platform dependent: under Windows,
259 the data is passed without any changes to the underlying CreateBitmap() API.
260 Under other platforms, only monochrome bitmaps may be created using this
261 constructor and wxImage should be used for creating colour bitmaps from
262 static data.
263
264 @param bits
265 Specifies an array of pixel values.
266 @param width
267 Specifies the width of the bitmap.
268 @param height
269 Specifies the height of the bitmap.
270 @param depth
271 Specifies the depth of the bitmap.
272 If this is omitted, then a value of 1 (monochrome bitmap) is used.
273
274 @beginWxPerlOnly
275 In wxPerl use Wx::Bitmap->newFromBits(bits, width, height, depth).
276 @endWxPerlOnly
277 */
278 wxBitmap(const char bits[], int width, int height, int depth = 1);
279
280 /**
281 Creates a new bitmap. A depth of ::wxBITMAP_SCREEN_DEPTH indicates the
282 depth of the current screen or visual.
283
284 Some platforms only support 1 for monochrome and ::wxBITMAP_SCREEN_DEPTH for
285 the current colour setting.
286
287 A depth of 32 including an alpha channel is supported under MSW, Mac and GTK+.
288 */
289 wxBitmap(int width, int height, int depth = wxBITMAP_SCREEN_DEPTH);
290
291 /**
292 @overload
293 */
294 wxBitmap(const wxSize& sz, int depth = wxBITMAP_SCREEN_DEPTH);
295
296 /**
297 Creates a bitmap from XPM data.
298
299 @beginWxPerlOnly
300 In wxPerl use Wx::Bitmap->newFromXPM(data).
301 @endWxPerlOnly
302 */
303 wxBitmap(const char* const* bits);
304
305 /**
306 Loads a bitmap from a file or resource.
307
308 @param name
309 This can refer to a resource name or a filename under MS Windows and X.
310 Its meaning is determined by the @a type parameter.
311 @param type
312 May be one of the ::wxBitmapType values and indicates which type of
313 bitmap should be loaded. See the note in the class detailed description.
314 Note that the wxBITMAP_DEFAULT_TYPE constant has different value under
315 different wxWidgets ports. See the bitmap.h header for the value it takes
316 for a specific port.
317
318 @see LoadFile()
319 */
320 wxBitmap(const wxString& name, wxBitmapType type = wxBITMAP_DEFAULT_TYPE);
321
322 /**
323 Creates this bitmap object from the given image.
324 This has to be done to actually display an image as you cannot draw an
325 image directly on a window.
326
327 The resulting bitmap will use the provided colour depth (or that of the
328 current system if depth is ::wxBITMAP_SCREEN_DEPTH) which entails that a
329 colour reduction may take place.
330
331 When in 8-bit mode (PseudoColour mode), the GTK port will use a color cube
332 created on program start-up to look up colors. This ensures a very fast conversion,
333 but the image quality won't be perfect (and could be better for photo images using
334 more sophisticated dithering algorithms).
335
336 On Windows, if there is a palette present (set with SetPalette), it will be
337 used when creating the wxBitmap (most useful in 8-bit display mode).
338 On other platforms, the palette is currently ignored.
339
340 @param img
341 Platform-independent wxImage object.
342 @param depth
343 Specifies the depth of the bitmap.
344 If this is omitted, the display depth of the screen is used.
345 */
346 wxBitmap(const wxImage& img, int depth = wxBITMAP_SCREEN_DEPTH);
347
348 /**
349 Destructor.
350 See @ref overview_refcount_destruct for more info.
351
352 If the application omits to delete the bitmap explicitly, the bitmap will be
353 destroyed automatically by wxWidgets when the application exits.
354
355 @warning
356 Do not delete a bitmap that is selected into a memory device context.
357 */
358 virtual ~wxBitmap();
359
360 /**
361 Adds a handler to the end of the static list of format handlers.
362
363 @param handler
364 A new bitmap format handler object. There is usually only one instance
365 of a given handler class in an application session.
366
367 Note that unlike wxImage::AddHandler, there's no documented list of
368 the wxBitmapHandlers available in wxWidgets.
369 This is because they are platform-specific and most important, they are
370 all automatically loaded at startup.
371
372 If you want to be sure that wxBitmap can load a certain type of image,
373 you'd better use wxImage::AddHandler.
374
375 @see wxBitmapHandler
376 */
377 static void AddHandler(wxBitmapHandler* handler);
378
379 /**
380 Deletes all bitmap handlers.
381 This function is called by wxWidgets on exit.
382 */
383 static void CleanUpHandlers();
384
385 /**
386 Creates an image from a platform-dependent bitmap. This preserves
387 mask information so that bitmaps and images can be converted back
388 and forth without loss in that respect.
389 */
390 virtual wxImage ConvertToImage() const;
391
392 /**
393 Creates the bitmap from an icon.
394 */
395 virtual bool CopyFromIcon(const wxIcon& icon);
396
397 /**
398 Creates a fresh bitmap.
399 If the final argument is omitted, the display depth of the screen is used.
400
401 @return @true if the creation was successful.
402 */
403 virtual bool Create(int width, int height, int depth = wxBITMAP_SCREEN_DEPTH);
404
405 /**
406 @overload
407 */
408 virtual bool Create(const wxSize& sz, int depth = wxBITMAP_SCREEN_DEPTH);
409
410 /*
411 Creates a bitmap from the given data, which can be of arbitrary type.
412
413 @param data
414 Data whose type depends on the value of type.
415 @param type
416 A bitmap type identifier; see ::wxBitmapType for the list of values.
417 See the note in the class detailed description for more info.
418 @param width
419 The width of the bitmap in pixels.
420 @param height
421 The height of the bitmap in pixels.
422 @param depth
423 The depth of the bitmap in pixels. If this is -1, the screen depth is used.
424
425 @return @true if the call succeeded, @false otherwise.
426
427 This overload depends on the @a type of data.
428
429 virtual bool Create(const void* data, int type, int width,
430 int height, int depth = -1);
431
432 NOTE: leave this undoc for the same reason of the relative ctor.
433 */
434
435 /**
436 Finds the handler with the given @a name.
437
438 @return A pointer to the handler if found, @NULL otherwise.
439 */
440 static wxBitmapHandler* FindHandler(const wxString& name);
441
442 /**
443 Finds the handler associated with the given @a extension and @a type.
444
445 @param extension
446 The file extension, such as "bmp" (without the dot).
447 @param bitmapType
448 The bitmap type managed by the handler, see ::wxBitmapType.
449
450 @return A pointer to the handler if found, @NULL otherwise.
451 */
452 static wxBitmapHandler* FindHandler(const wxString& extension,
453 wxBitmapType bitmapType);
454
455 /**
456 Finds the handler associated with the given bitmap type.
457
458 @param bitmapType
459 The bitmap type managed by the handler, see ::wxBitmapType.
460
461 @return A pointer to the handler if found, @NULL otherwise.
462
463 @see wxBitmapHandler
464 */
465
466 static wxBitmapHandler* FindHandler(wxBitmapType bitmapType);
467
468 /**
469 Gets the colour depth of the bitmap.
470 A value of 1 indicates a monochrome bitmap.
471 */
472 virtual int GetDepth() const;
473
474 /**
475 Returns the static list of bitmap format handlers.
476
477 @see wxBitmapHandler
478 */
479 static wxList& GetHandlers();
480
481 /**
482 Gets the height of the bitmap in pixels.
483
484 @see GetWidth(), GetSize()
485 */
486 virtual int GetHeight() const;
487
488 /**
489 Gets the associated mask (if any) which may have been loaded from a file
490 or set for the bitmap.
491
492 @see SetMask(), wxMask
493 */
494 virtual wxMask* GetMask() const;
495
496 /**
497 Gets the associated palette (if any) which may have been loaded from a file
498 or set for the bitmap.
499
500 @see wxPalette
501 */
502 virtual wxPalette* GetPalette() const;
503
504 /**
505 Returns a sub bitmap of the current one as long as the rect belongs entirely to
506 the bitmap. This function preserves bit depth and mask information.
507 */
508 virtual wxBitmap GetSubBitmap(const wxRect& rect) const;
509
510 /**
511 Returns the size of the bitmap in pixels.
512
513 @since 2.9.0
514
515 @see GetHeight(), GetWidth()
516 */
517 wxSize GetSize() const;
518
519 /**
520 Returns disabled (dimmed) version of the bitmap.
521
522 This method is not available when <code>wxUSE_IMAGE == 0</code>.
523
524 @since 2.9.0
525 */
526 wxBitmap ConvertToDisabled(unsigned char brightness = 255) const;
527
528 /**
529 Gets the width of the bitmap in pixels.
530
531 @see GetHeight(), GetSize()
532 */
533 virtual int GetWidth() const;
534
535 /**
536 Adds the standard bitmap format handlers, which, depending on wxWidgets
537 configuration, can be handlers for Windows bitmap, Windows bitmap resource,
538 and XPM.
539
540 This function is called by wxWidgets on startup.
541
542 @see wxBitmapHandler
543 */
544 static void InitStandardHandlers();
545
546 /**
547 Adds a handler at the start of the static list of format handlers.
548
549 @param handler
550 A new bitmap format handler object. There is usually only one instance
551 of a given handler class in an application session.
552
553 @see wxBitmapHandler
554 */
555 static void InsertHandler(wxBitmapHandler* handler);
556
557 /**
558 Returns @true if bitmap data is present.
559 */
560 virtual bool IsOk() const;
561
562 /**
563 Loads a bitmap from a file or resource.
564
565 @param name
566 Either a filename or a Windows resource name.
567 The meaning of name is determined by the @a type parameter.
568 @param type
569 One of the ::wxBitmapType values; see the note in the class
570 detailed description.
571 Note that the wxBITMAP_DEFAULT_TYPE constant has different value under
572 different wxWidgets ports. See the bitmap.h header for the value it takes
573 for a specific port.
574
575 @return @true if the operation succeeded, @false otherwise.
576
577 @remarks A palette may be associated with the bitmap if one exists
578 (especially for colour Windows bitmaps), and if the
579 code supports it. You can check if one has been created
580 by using the GetPalette() member.
581
582 @see SaveFile()
583 */
584 virtual bool LoadFile(const wxString& name, wxBitmapType type = wxBITMAP_DEFAULT_TYPE);
585
586 /**
587 Loads a bitmap from the memory containing image data in PNG format.
588
589 This helper function provides the simplest way to create a wxBitmap
590 from PNG image data. On most platforms, it's simply a wrapper around
591 wxImage loading functions and so requires the PNG image handler to be
592 registered by either calling wxInitAllImageHandlers() which also
593 registers all the other image formats or including the necessary
594 header:
595 @code
596 #include <wx/imagpng.h>
597 @endcode
598 and calling
599 @code
600 wxImage::AddHandler(new wxPNGHandler);
601 @endcode
602 in your application startup code.
603
604 However under OS X this function uses native image loading and so
605 doesn't require wxWidgets PNG support.
606
607 @since 2.9.5
608 */
609 static wxBitmap NewFromPNGData(const void* data, size_t size);
610
611 /**
612 Finds the handler with the given name, and removes it.
613 The handler is not deleted.
614
615 @param name
616 The handler name.
617
618 @return @true if the handler was found and removed, @false otherwise.
619
620 @see wxBitmapHandler
621 */
622 static bool RemoveHandler(const wxString& name);
623
624 /**
625 Saves a bitmap in the named file.
626
627 @param name
628 A filename. The meaning of name is determined by the type parameter.
629 @param type
630 One of the ::wxBitmapType values; see the note in the class
631 detailed description.
632 @param palette
633 An optional palette used for saving the bitmap.
634
635 @return @true if the operation succeeded, @false otherwise.
636
637 @remarks Depending on how wxWidgets has been configured, not all formats
638 may be available.
639
640 @see LoadFile()
641 */
642 virtual bool SaveFile(const wxString& name, wxBitmapType type,
643 const wxPalette* palette = NULL) const;
644
645 /**
646 Sets the depth member (does not affect the bitmap data).
647
648 @todo since these functions do not affect the bitmap data,
649 why they exist??
650
651 @param depth
652 Bitmap depth.
653 */
654 virtual void SetDepth(int depth);
655
656 /**
657 Sets the height member (does not affect the bitmap data).
658
659 @param height
660 Bitmap height in pixels.
661 */
662 virtual void SetHeight(int height);
663
664 /**
665 Sets the mask for this bitmap.
666
667 @remarks The bitmap object owns the mask once this has been called.
668
669 @see GetMask(), wxMask
670 */
671 virtual void SetMask(wxMask* mask);
672
673 /**
674 Sets the associated palette. (Not implemented under GTK+).
675
676 @param palette
677 The palette to set.
678
679 @see wxPalette
680 */
681 virtual void SetPalette(const wxPalette& palette);
682
683 /**
684 Sets the width member (does not affect the bitmap data).
685
686 @param width
687 Bitmap width in pixels.
688 */
689 virtual void SetWidth(int width);
690 };
691
692 /**
693 An empty wxBitmap object.
694 */
695 wxBitmap wxNullBitmap;
696
697
698
699
700 /**
701 @class wxMask
702
703 This class encapsulates a monochrome mask bitmap, where the masked area is
704 black and the unmasked area is white.
705
706 When associated with a bitmap and drawn in a device context, the unmasked
707 area of the bitmap will be drawn, and the masked area will not be drawn.
708
709 @library{wxcore}
710 @category{gdi}
711
712 @see wxBitmap, wxDC::Blit, wxMemoryDC
713 */
714 class wxMask : public wxObject
715 {
716 public:
717
718 /**
719 Default constructor.
720 */
721 wxMask();
722
723 /**
724 Constructs a mask from a bitmap and a palette index that indicates the
725 background.
726 Not yet implemented for GTK.
727
728 @param bitmap
729 A valid bitmap.
730 @param index
731 Index into a palette, specifying the transparency colour.
732 */
733 wxMask(const wxBitmap& bitmap, int index);
734
735 /**
736 Constructs a mask from a monochrome bitmap.
737 */
738 wxMask(const wxBitmap& bitmap);
739
740 /**
741 Constructs a mask from a bitmap and a colour that indicates the background.
742 */
743 wxMask(const wxBitmap& bitmap, const wxColour& colour);
744
745 /**
746 Destroys the wxMask object and the underlying bitmap data.
747 */
748 virtual ~wxMask();
749
750 /**
751 Constructs a mask from a bitmap and a palette index that indicates the
752 background.
753 Not yet implemented for GTK.
754
755 @param bitmap
756 A valid bitmap.
757 @param index
758 Index into a palette, specifying the transparency colour.
759 */
760 bool Create(const wxBitmap& bitmap, int index);
761
762 /**
763 Constructs a mask from a monochrome bitmap.
764 */
765 bool Create(const wxBitmap& bitmap);
766
767 /**
768 Constructs a mask from a bitmap and a colour that indicates the background.
769 */
770 bool Create(const wxBitmap& bitmap, const wxColour& colour);
771
772 /**
773 Returns the mask as a monochrome bitmap.
774 Currently this method is implemented in wxMSW, wxGTK and wxOSX.
775
776 @since 2.9.5
777 */
778 wxBitmap GetBitmap() const;
779 };
780