]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/statbmp.cpp
split wxEVT_GRID_CELL_CHANGE into wxEVT_GRID_CELL_CHANGING/ED pair for consistency...
[wxWidgets.git] / samples / widgets / statbmp.cpp
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
7 // Id: $Id$
8 // Copyright: (c) 2008 Marcin Wojdyr
9 // License: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers
28 #ifndef WX_PRECOMP
29 #include "wx/log.h"
30
31 #include "wx/button.h"
32 #include "wx/image.h"
33 #include "wx/radiobox.h"
34 #include "wx/statbmp.h"
35 #include "wx/statbox.h"
36 #include "wx/textctrl.h"
37 #endif
38
39 #include "wx/generic/statbmpg.h"
40 #include "wx/sizer.h"
41 #include "wx/filepicker.h"
42
43 #include "widgets.h"
44 #include "icons/statbmp.xpm"
45
46
47 class StatBmpWidgetsPage : public WidgetsPage
48 {
49 public:
50 StatBmpWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist)
51 : WidgetsPage(book, imaglist, statbmp_xpm) {}
52
53 virtual void CreateContent();
54 virtual wxControl *GetWidget() const { return m_statbmp; }
55 virtual void RecreateWidget();
56
57 private:
58 void OnFileChange(wxFileDirPickerEvent &WXUNUSED(ev)) { RecreateWidget(); }
59 void OnRadioChange(wxCommandEvent &WXUNUSED(ev)) { RecreateWidget(); }
60
61 void OnMouseEvent(wxMouseEvent& WXUNUSED(event))
62 {
63 wxLogMessage(wxT("wxStaticBitmap clicked."));
64 }
65
66 wxStaticBitmapBase *m_statbmp;
67 wxFilePickerCtrl *m_filepicker;
68 wxRadioBox *m_radio;
69 wxStaticBoxSizer *m_sbsizer;
70
71 DECLARE_WIDGETS_PAGE(StatBmpWidgetsPage)
72 };
73
74 IMPLEMENT_WIDGETS_PAGE(StatBmpWidgetsPage, wxT("StaticBitmap"),
75 ALL_CTRLS);
76
77 void StatBmpWidgetsPage::CreateContent()
78 {
79
80 static const wxString choices[] = { "native", "generic" };
81 m_radio = new wxRadioBox(this, wxID_ANY, "implementation",
82 wxDefaultPosition, wxDefaultSize,
83 WXSIZEOF(choices), choices);
84
85 m_filepicker = new wxFilePickerCtrl(this, wxID_ANY, "../image/toucan.png");
86
87 m_sbsizer = new wxStaticBoxSizer(wxVERTICAL, this, "wxStaticBitmap inside");
88
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());
95 SetSizer(sizer);
96
97 wxInitAllImageHandlers();
98
99 Connect(wxEVT_COMMAND_FILEPICKER_CHANGED,
100 wxFileDirPickerEventHandler(StatBmpWidgetsPage::OnFileChange));
101 Connect(wxEVT_COMMAND_RADIOBOX_SELECTED,
102 wxCommandEventHandler(StatBmpWidgetsPage::OnRadioChange));
103
104 m_statbmp = NULL;
105 RecreateWidget();
106 }
107
108 void StatBmpWidgetsPage::RecreateWidget()
109 {
110 delete m_statbmp;
111 wxString filepath = m_filepicker->GetPath();
112 wxImage image(filepath);
113 if (! image.Ok() )
114 {
115 wxLogMessage("Reading image from file '%s' failed.", filepath.c_str());
116 m_statbmp = NULL;
117 return;
118 }
119 if (m_radio->GetSelection() == 0)
120 m_statbmp = new wxStaticBitmap(this, wxID_ANY, wxBitmap(image));
121 else
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),
127 NULL, this);
128 // When switching from generic to native control on wxMSW under Wine,
129 // the explicit Refresh() is necessary
130 m_statbmp->Refresh();
131 }
132