dcbuffer.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: dcbuffer.h
  3. // Purpose: interface of wxBufferedDC
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. // Assumes the buffer bitmap covers the entire scrolled window,
  8. // and prepares the window DC accordingly
  9. #define wxBUFFER_VIRTUAL_AREA 0x01
  10. // Assumes the buffer bitmap only covers the client area;
  11. // does not prepare the window DC
  12. #define wxBUFFER_CLIENT_AREA 0x02
  13. // Set when not using specific buffer bitmap. Note that this
  14. // is private style and not returned by GetStyle.
  15. #define wxBUFFER_USES_SHARED_BUFFER 0x04
  16. /**
  17. @class wxBufferedDC
  18. This class provides a simple way to avoid flicker: when drawing on it,
  19. everything is in fact first drawn on an in-memory buffer (a wxBitmap) and
  20. then copied to the screen, using the associated wxDC, only once, when this
  21. object is destroyed. wxBufferedDC itself is typically associated with
  22. wxClientDC, if you want to use it in your @c EVT_PAINT handler, you should
  23. look at wxBufferedPaintDC instead.
  24. When used like this, a valid @e DC must be specified in the constructor
  25. while the @e buffer bitmap doesn't have to be explicitly provided, by
  26. default this class will allocate the bitmap of required size itself.
  27. However using a dedicated bitmap can speed up the redrawing process by
  28. eliminating the repeated creation and destruction of a possibly big bitmap.
  29. Otherwise, wxBufferedDC can be used in the same way as any other device
  30. context.
  31. There is another possible use for wxBufferedDC is to use it to maintain a
  32. backing store for the window contents. In this case, the associated @e DC
  33. may be @NULL but a valid backing store bitmap should be specified.
  34. Finally, please note that GTK+ 2.0 as well as OS X provide double buffering
  35. themselves natively. You can either use wxWindow::IsDoubleBuffered() to
  36. determine whether you need to use buffering or not, or use
  37. wxAutoBufferedPaintDC to avoid needless double buffering on the systems
  38. which already do it automatically.
  39. @library{wxcore}
  40. @category{dc}
  41. @see wxDC, wxMemoryDC, wxBufferedPaintDC, wxAutoBufferedPaintDC
  42. */
  43. class wxBufferedDC : public wxMemoryDC
  44. {
  45. public:
  46. /**
  47. Default constructor. You must call one of the Init() methods later in
  48. order to use the device context.
  49. */
  50. wxBufferedDC();
  51. /**
  52. Creates a buffer for the provided @a dc. Init() must not be called when
  53. using this constructor.
  54. @param dc
  55. The underlying DC: everything drawn to this object will be flushed
  56. to this DC when this object is destroyed. You may pass @NULL in
  57. order to just initialize the buffer, and not flush it.
  58. @param area
  59. The size of the bitmap to be used for buffering (this bitmap is
  60. created internally when it is not given explicitly).
  61. @param style
  62. wxBUFFER_CLIENT_AREA to indicate that just the client area of the
  63. window is buffered, or wxBUFFER_VIRTUAL_AREA to indicate that the
  64. buffer bitmap covers the virtual area.
  65. */
  66. wxBufferedDC(wxDC* dc, const wxSize& area,
  67. int style = wxBUFFER_CLIENT_AREA);
  68. /**
  69. Creates a buffer for the provided dc. Init() must not be called when
  70. using this constructor.
  71. @param dc
  72. The underlying DC: everything drawn to this object will be flushed
  73. to this DC when this object is destroyed. You may pass @NULL in
  74. order to just initialize the buffer, and not flush it.
  75. @param buffer
  76. Explicitly provided bitmap to be used for buffering: this is the
  77. most efficient solution as the bitmap doesn't have to be recreated
  78. each time but it also requires more memory as the bitmap is never
  79. freed. The bitmap should have appropriate size, anything drawn
  80. outside of its bounds is clipped.
  81. @param style
  82. wxBUFFER_CLIENT_AREA to indicate that just the client area of the
  83. window is buffered, or wxBUFFER_VIRTUAL_AREA to indicate that the
  84. buffer bitmap covers the virtual area.
  85. */
  86. wxBufferedDC(wxDC* dc, wxBitmap& buffer = wxNullBitmap,
  87. int style = wxBUFFER_CLIENT_AREA);
  88. /**
  89. Copies everything drawn on the DC so far to the underlying DC
  90. associated with this object, if any.
  91. */
  92. virtual ~wxBufferedDC();
  93. //@{
  94. /**
  95. Initializes the object created using the default constructor. Please
  96. see the constructors for parameter details.
  97. */
  98. void Init(wxDC* dc, const wxSize& area,
  99. int style = wxBUFFER_CLIENT_AREA);
  100. void Init(wxDC* dc, wxBitmap& buffer = wxNullBitmap,
  101. int style = wxBUFFER_CLIENT_AREA);
  102. //@}
  103. /**
  104. Blits the buffer to the dc, and detaches the dc from the buffer (so it
  105. can be effectively used once only).
  106. Usually only called in the destructor or by the destructor of derived
  107. classes if the BufferedDC must blit before the derived class (which may
  108. own the dc it's blitting to) is destroyed.
  109. */
  110. void UnMask();
  111. /**
  112. Set the style.
  113. */
  114. void SetStyle(int style);
  115. /**
  116. Get the style.
  117. */
  118. int GetStyle() const;
  119. };
  120. /**
  121. @class wxAutoBufferedPaintDC
  122. This wxDC derivative can be used inside of an @c EVT_PAINT() event handler
  123. to achieve double-buffered drawing. Just use this class instead of
  124. wxPaintDC and make sure wxWindow::SetBackgroundStyle() is called with
  125. wxBG_STYLE_PAINT somewhere in the class initialization code, and that's
  126. all you have to do to (mostly) avoid flicker.
  127. The difference between wxBufferedPaintDC and this class is that this class
  128. won't double-buffer on platforms which have native double-buffering
  129. already, avoiding any unnecessary buffering to avoid flicker.
  130. wxAutoBufferedPaintDC is simply a typedef of wxPaintDC on platforms that
  131. have native double-buffering, otherwise, it is a typedef of
  132. wxBufferedPaintDC.
  133. @library{wxcore}
  134. @category{dc}
  135. @see wxDC, wxBufferedPaintDC, wxPaintDC
  136. */
  137. class wxAutoBufferedPaintDC : public wxBufferedPaintDC
  138. {
  139. public:
  140. /**
  141. Constructor. Pass a pointer to the window on which you wish to paint.
  142. */
  143. wxAutoBufferedPaintDC(wxWindow* window);
  144. };
  145. /**
  146. * Check if the window is natively double buffered and will return a wxPaintDC
  147. * if it is, a wxBufferedPaintDC otherwise. It is the caller's responsibility
  148. * to delete the wxDC pointer when finished with it.
  149. */
  150. wxDC* wxAutoBufferedPaintDCFactory(wxWindow* window);
  151. /**
  152. @class wxBufferedPaintDC
  153. This is a subclass of wxBufferedDC which can be used inside of an
  154. @c EVT_PAINT() event handler to achieve double-buffered drawing. Just use
  155. this class instead of wxPaintDC and make sure
  156. wxWindow::SetBackgroundStyle() is called with wxBG_STYLE_PAINT somewhere
  157. in the class initialization code, and that's all you have to do to (mostly)
  158. avoid flicker. The only thing to watch out for is that if you are using
  159. this class together with wxScrolled, you probably do @b not want to call
  160. wxScrolled::PrepareDC() on it as it already does this internally for the
  161. real underlying wxPaintDC.
  162. @library{wxcore}
  163. @category{dc}
  164. @see wxDC, wxBufferedDC, wxAutoBufferedPaintDC, wxPaintDC
  165. */
  166. class wxBufferedPaintDC : public wxBufferedDC
  167. {
  168. public:
  169. //@{
  170. /**
  171. As with wxBufferedDC, you may either provide the bitmap to be used for
  172. buffering or let this object create one internally (in the latter case,
  173. the size of the client part of the window is used).
  174. Pass wxBUFFER_CLIENT_AREA for the @a style parameter to indicate that
  175. just the client area of the window is buffered, or
  176. wxBUFFER_VIRTUAL_AREA to indicate that the buffer bitmap covers the
  177. virtual area.
  178. */
  179. wxBufferedPaintDC(wxWindow* window, wxBitmap& buffer,
  180. int style = wxBUFFER_CLIENT_AREA);
  181. wxBufferedPaintDC(wxWindow* window,
  182. int style = wxBUFFER_CLIENT_AREA);
  183. //@}
  184. /**
  185. Copies everything drawn on the DC so far to the window associated with
  186. this object, using a wxPaintDC.
  187. */
  188. virtual ~wxBufferedPaintDC();
  189. };