]>
Commit | Line | Data |
---|---|---|
a30e7029 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Program: wxWidgets Widgets Sample | |
3 | // Name: statbmp.cpp | |
4 | // Purpose: Part of the widgets sample showing wxStaticBitmap | |
5 | // Author: Marcin Wojdyr | |
6 | // Created: 2008-06-19 | |
a30e7029 | 7 | // Copyright: (c) 2008 Marcin Wojdyr |
526954c5 | 8 | // Licence: wxWindows licence |
a30e7029 VZ |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | // for compilers that support precompilation, includes "wx/wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | // for all others, include the necessary headers | |
27 | #ifndef WX_PRECOMP | |
28 | #include "wx/log.h" | |
29 | ||
30 | #include "wx/button.h" | |
d8cda32e | 31 | #include "wx/image.h" |
a30e7029 VZ |
32 | #include "wx/radiobox.h" |
33 | #include "wx/statbmp.h" | |
34 | #include "wx/statbox.h" | |
35 | #include "wx/textctrl.h" | |
36 | #endif | |
37 | ||
470ee714 VZ |
38 | #include "wx/filename.h" |
39 | ||
a30e7029 VZ |
40 | #include "wx/generic/statbmpg.h" |
41 | #include "wx/sizer.h" | |
42 | #include "wx/filepicker.h" | |
43 | ||
44 | #include "widgets.h" | |
45 | #include "icons/statbmp.xpm" | |
46 | ||
47 | ||
48 | class StatBmpWidgetsPage : public WidgetsPage | |
49 | { | |
50 | public: | |
51 | StatBmpWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist) | |
52 | : WidgetsPage(book, imaglist, statbmp_xpm) {} | |
53 | ||
54 | virtual void CreateContent(); | |
55 | virtual wxControl *GetWidget() const { return m_statbmp; } | |
56 | virtual void RecreateWidget(); | |
57 | ||
58 | private: | |
59 | void OnFileChange(wxFileDirPickerEvent &WXUNUSED(ev)) { RecreateWidget(); } | |
60 | void OnRadioChange(wxCommandEvent &WXUNUSED(ev)) { RecreateWidget(); } | |
61 | ||
62 | void OnMouseEvent(wxMouseEvent& WXUNUSED(event)) | |
63 | { | |
64 | wxLogMessage(wxT("wxStaticBitmap clicked.")); | |
65 | } | |
66 | ||
67 | wxStaticBitmapBase *m_statbmp; | |
68 | wxFilePickerCtrl *m_filepicker; | |
69 | wxRadioBox *m_radio; | |
70 | wxStaticBoxSizer *m_sbsizer; | |
71 | ||
72 | DECLARE_WIDGETS_PAGE(StatBmpWidgetsPage) | |
73 | }; | |
74 | ||
75 | IMPLEMENT_WIDGETS_PAGE(StatBmpWidgetsPage, wxT("StaticBitmap"), | |
76 | ALL_CTRLS); | |
77 | ||
78 | void StatBmpWidgetsPage::CreateContent() | |
79 | { | |
80 | ||
81 | static const wxString choices[] = { "native", "generic" }; | |
82 | m_radio = new wxRadioBox(this, wxID_ANY, "implementation", | |
83 | wxDefaultPosition, wxDefaultSize, | |
84 | WXSIZEOF(choices), choices); | |
85 | ||
470ee714 VZ |
86 | wxString testImage; |
87 | #if wxUSE_LIBPNG | |
88 | wxFileName fn("../image/toucan.png"); | |
89 | if ( fn.FileExists() ) | |
90 | testImage = fn.GetFullPath(); | |
91 | #endif // wxUSE_LIBPNG | |
92 | m_filepicker = new wxFilePickerCtrl(this, wxID_ANY, testImage); | |
a30e7029 VZ |
93 | |
94 | m_sbsizer = new wxStaticBoxSizer(wxVERTICAL, this, "wxStaticBitmap inside"); | |
95 | ||
96 | wxSizer *leftsizer = new wxBoxSizer(wxVERTICAL); | |
97 | leftsizer->Add(m_radio, wxSizerFlags().Expand().Border()); | |
98 | leftsizer->Add(m_filepicker, wxSizerFlags().Expand().Border()); | |
99 | wxSizer *sizer = new wxBoxSizer(wxHORIZONTAL); | |
100 | sizer->Add(leftsizer, wxSizerFlags().Border()); | |
101 | sizer->Add(m_sbsizer, wxSizerFlags().Center()); | |
102 | SetSizer(sizer); | |
103 | ||
104 | wxInitAllImageHandlers(); | |
105 | ||
ce7fe42e | 106 | Connect(wxEVT_FILEPICKER_CHANGED, |
a30e7029 | 107 | wxFileDirPickerEventHandler(StatBmpWidgetsPage::OnFileChange)); |
ce7fe42e | 108 | Connect(wxEVT_RADIOBOX, |
a30e7029 VZ |
109 | wxCommandEventHandler(StatBmpWidgetsPage::OnRadioChange)); |
110 | ||
111 | m_statbmp = NULL; | |
112 | RecreateWidget(); | |
113 | } | |
114 | ||
115 | void StatBmpWidgetsPage::RecreateWidget() | |
116 | { | |
5276b0a5 | 117 | wxDELETE(m_statbmp); |
470ee714 | 118 | |
a30e7029 | 119 | wxString filepath = m_filepicker->GetPath(); |
470ee714 VZ |
120 | if ( filepath.empty() ) |
121 | return; | |
122 | ||
a30e7029 | 123 | wxImage image(filepath); |
a1b806b9 | 124 | if (! image.IsOk() ) |
a30e7029 VZ |
125 | { |
126 | wxLogMessage("Reading image from file '%s' failed.", filepath.c_str()); | |
a30e7029 VZ |
127 | return; |
128 | } | |
129 | if (m_radio->GetSelection() == 0) | |
130 | m_statbmp = new wxStaticBitmap(this, wxID_ANY, wxBitmap(image)); | |
131 | else | |
132 | m_statbmp = new wxGenericStaticBitmap(this, wxID_ANY, wxBitmap(image)); | |
133 | m_sbsizer->Add(m_statbmp, wxSizerFlags(1).Expand()); | |
134 | GetSizer()->Layout(); | |
470ee714 | 135 | m_statbmp->Connect(wxEVT_LEFT_DOWN, |
a30e7029 VZ |
136 | wxMouseEventHandler(StatBmpWidgetsPage::OnMouseEvent), |
137 | NULL, this); | |
470ee714 | 138 | // When switching from generic to native control on wxMSW under Wine, |
a30e7029 | 139 | // the explicit Refresh() is necessary |
470ee714 | 140 | m_statbmp->Refresh(); |
a30e7029 VZ |
141 | } |
142 |