custombgwin.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/generic/custombgwin.h
  3. // Purpose: Generic implementation of wxCustomBackgroundWindow.
  4. // Author: Vadim Zeitlin
  5. // Created: 2011-10-10
  6. // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
  7. // Licence: wxWindows licence
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_GENERIC_CUSTOMBGWIN_H_
  10. #define _WX_GENERIC_CUSTOMBGWIN_H_
  11. #include "wx/bitmap.h"
  12. // A helper to avoid template bloat: this class contains all type-independent
  13. // code of wxCustomBackgroundWindow<> below.
  14. class wxCustomBackgroundWindowGenericBase : public wxCustomBackgroundWindowBase
  15. {
  16. public:
  17. wxCustomBackgroundWindowGenericBase() { }
  18. protected:
  19. void DoEraseBackground(wxEraseEvent& event, wxWindow* win)
  20. {
  21. wxDC& dc = *event.GetDC();
  22. const wxSize clientSize = win->GetClientSize();
  23. const wxSize bitmapSize = m_bitmapBg.GetSize();
  24. for ( int x = 0; x < clientSize.x; x += bitmapSize.x )
  25. {
  26. for ( int y = 0; y < clientSize.y; y += bitmapSize.y )
  27. {
  28. dc.DrawBitmap(m_bitmapBg, x, y);
  29. }
  30. }
  31. }
  32. // The bitmap used for painting the background if valid.
  33. wxBitmap m_bitmapBg;
  34. wxDECLARE_NO_COPY_CLASS(wxCustomBackgroundWindowGenericBase);
  35. };
  36. // ----------------------------------------------------------------------------
  37. // wxCustomBackgroundWindow
  38. // ----------------------------------------------------------------------------
  39. template <class W>
  40. class wxCustomBackgroundWindow : public W,
  41. public wxCustomBackgroundWindowGenericBase
  42. {
  43. public:
  44. typedef W BaseWindowClass;
  45. wxCustomBackgroundWindow() { }
  46. protected:
  47. virtual void DoSetBackgroundBitmap(const wxBitmap& bmp)
  48. {
  49. m_bitmapBg = bmp;
  50. if ( m_bitmapBg.IsOk() )
  51. {
  52. BaseWindowClass::Connect
  53. (
  54. wxEVT_ERASE_BACKGROUND,
  55. wxEraseEventHandler(wxCustomBackgroundWindow::OnEraseBackground)
  56. );
  57. }
  58. else
  59. {
  60. BaseWindowClass::Disconnect
  61. (
  62. wxEVT_ERASE_BACKGROUND,
  63. wxEraseEventHandler(wxCustomBackgroundWindow::OnEraseBackground)
  64. );
  65. }
  66. }
  67. private:
  68. // Event handler for erasing the background which is only used when we have
  69. // a valid background bitmap.
  70. void OnEraseBackground(wxEraseEvent& event)
  71. {
  72. DoEraseBackground(event, this);
  73. }
  74. wxDECLARE_NO_COPY_TEMPLATE_CLASS(wxCustomBackgroundWindow, W);
  75. };
  76. #endif // _WX_GENERIC_CUSTOMBGWIN_H_