1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing wxStaticBitmap
5 // Author: Marcin Wojdyr
8 // Copyright: (c) 2008 Marcin Wojdyr
9 // License: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers
31 #include "wx/button.h"
33 #include "wx/radiobox.h"
34 #include "wx/statbmp.h"
35 #include "wx/statbox.h"
36 #include "wx/textctrl.h"
39 #include "wx/generic/statbmpg.h"
41 #include "wx/filepicker.h"
44 #include "icons/statbmp.xpm"
47 class StatBmpWidgetsPage
: public WidgetsPage
50 StatBmpWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
51 : WidgetsPage(book
, imaglist
, statbmp_xpm
) {}
53 virtual void CreateContent();
54 virtual wxControl
*GetWidget() const { return m_statbmp
; }
55 virtual void RecreateWidget();
58 void OnFileChange(wxFileDirPickerEvent
&WXUNUSED(ev
)) { RecreateWidget(); }
59 void OnRadioChange(wxCommandEvent
&WXUNUSED(ev
)) { RecreateWidget(); }
61 void OnMouseEvent(wxMouseEvent
& WXUNUSED(event
))
63 wxLogMessage(wxT("wxStaticBitmap clicked."));
66 wxStaticBitmapBase
*m_statbmp
;
67 wxFilePickerCtrl
*m_filepicker
;
69 wxStaticBoxSizer
*m_sbsizer
;
71 DECLARE_WIDGETS_PAGE(StatBmpWidgetsPage
)
74 IMPLEMENT_WIDGETS_PAGE(StatBmpWidgetsPage
, wxT("StaticBitmap"),
77 void StatBmpWidgetsPage::CreateContent()
80 static const wxString choices
[] = { "native", "generic" };
81 m_radio
= new wxRadioBox(this, wxID_ANY
, "implementation",
82 wxDefaultPosition
, wxDefaultSize
,
83 WXSIZEOF(choices
), choices
);
85 m_filepicker
= new wxFilePickerCtrl(this, wxID_ANY
, "../image/toucan.png");
87 m_sbsizer
= new wxStaticBoxSizer(wxVERTICAL
, this, "wxStaticBitmap inside");
89 wxSizer
*leftsizer
= new wxBoxSizer(wxVERTICAL
);
90 leftsizer
->Add(m_radio
, wxSizerFlags().Expand().Border());
91 leftsizer
->Add(m_filepicker
, wxSizerFlags().Expand().Border());
92 wxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
93 sizer
->Add(leftsizer
, wxSizerFlags().Border());
94 sizer
->Add(m_sbsizer
, wxSizerFlags().Center());
97 wxInitAllImageHandlers();
99 Connect(wxEVT_COMMAND_FILEPICKER_CHANGED
,
100 wxFileDirPickerEventHandler(StatBmpWidgetsPage::OnFileChange
));
101 Connect(wxEVT_COMMAND_RADIOBOX_SELECTED
,
102 wxCommandEventHandler(StatBmpWidgetsPage::OnRadioChange
));
108 void StatBmpWidgetsPage::RecreateWidget()
111 wxString filepath
= m_filepicker
->GetPath();
112 wxImage
image(filepath
);
115 wxLogMessage("Reading image from file '%s' failed.", filepath
.c_str());
119 if (m_radio
->GetSelection() == 0)
120 m_statbmp
= new wxStaticBitmap(this, wxID_ANY
, wxBitmap(image
));
122 m_statbmp
= new wxGenericStaticBitmap(this, wxID_ANY
, wxBitmap(image
));
123 m_sbsizer
->Add(m_statbmp
, wxSizerFlags(1).Expand());
124 GetSizer()->Layout();
125 m_statbmp
->Connect(wxEVT_LEFT_DOWN
,
126 wxMouseEventHandler(StatBmpWidgetsPage::OnMouseEvent
),
128 // When switching from generic to native control on wxMSW under Wine,
129 // the explicit Refresh() is necessary
130 m_statbmp
->Refresh();