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 
10 #ifndef _WX_GENERIC_CUSTOMBGWIN_H_
11 #define _WX_GENERIC_CUSTOMBGWIN_H_
12 
13 #include "wx/bitmap.h"
14 #include "wx/dc.h"
15 #include "wx/event.h"
16 #include "wx/window.h"
17 
18 // A helper to avoid template bloat: this class contains all type-independent
19 // code of wxCustomBackgroundWindow<> below.
20 class wxCustomBackgroundWindowGenericBase : public wxCustomBackgroundWindowBase
21 {
22 public:
wxCustomBackgroundWindowGenericBase()23     wxCustomBackgroundWindowGenericBase() { }
24 
25 protected:
DoEraseBackground(wxEraseEvent & event,wxWindow * win)26     void DoEraseBackground(wxEraseEvent& event, wxWindow* win)
27     {
28         wxDC& dc = *event.GetDC();
29 
30         const wxSize clientSize = win->GetClientSize();
31         const wxSize bitmapSize = m_bitmapBg.GetSize();
32 
33         for ( int x = 0; x < clientSize.x; x += bitmapSize.x )
34         {
35             for ( int y = 0; y < clientSize.y; y += bitmapSize.y )
36             {
37                 dc.DrawBitmap(m_bitmapBg, x, y);
38             }
39         }
40     }
41 
42 
43     // The bitmap used for painting the background if valid.
44     wxBitmap m_bitmapBg;
45 
46 
47     wxDECLARE_NO_COPY_CLASS(wxCustomBackgroundWindowGenericBase);
48 };
49 
50 // ----------------------------------------------------------------------------
51 // wxCustomBackgroundWindow
52 // ----------------------------------------------------------------------------
53 
54 template <class W>
55 class wxCustomBackgroundWindow : public W,
56                                  public wxCustomBackgroundWindowGenericBase
57 {
58 public:
59     typedef W BaseWindowClass;
60 
wxCustomBackgroundWindow()61     wxCustomBackgroundWindow() { }
62 
63 protected:
DoSetBackgroundBitmap(const wxBitmap & bmp)64     virtual void DoSetBackgroundBitmap(const wxBitmap& bmp) wxOVERRIDE
65     {
66         m_bitmapBg = bmp;
67 
68         if ( m_bitmapBg.IsOk() )
69         {
70             BaseWindowClass::Bind
71             (
72                 wxEVT_ERASE_BACKGROUND,
73                 &wxCustomBackgroundWindow::OnEraseBackground, this
74             );
75         }
76         else
77         {
78             BaseWindowClass::Unbind
79             (
80                 wxEVT_ERASE_BACKGROUND,
81                 &wxCustomBackgroundWindow::OnEraseBackground, this
82             );
83         }
84     }
85 
86 private:
87     // Event handler for erasing the background which is only used when we have
88     // a valid background bitmap.
OnEraseBackground(wxEraseEvent & event)89     void OnEraseBackground(wxEraseEvent& event)
90     {
91         DoEraseBackground(event, this);
92     }
93 
94 
95     wxDECLARE_NO_COPY_TEMPLATE_CLASS(wxCustomBackgroundWindow, W);
96 };
97 
98 #endif // _WX_GENERIC_CUSTOMBGWIN_H_
99