Remove all lines containing cvs/svn "$Id$" keyword.
[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 // Copyright: (c) 2008 Marcin Wojdyr
8 // Licence: wxWindows licence
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"
31 #include "wx/image.h"
32 #include "wx/radiobox.h"
33 #include "wx/statbmp.h"
34 #include "wx/statbox.h"
35 #include "wx/textctrl.h"
36 #endif
37
38 #include "wx/filename.h"
39
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
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);
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
106 Connect(wxEVT_FILEPICKER_CHANGED,
107 wxFileDirPickerEventHandler(StatBmpWidgetsPage::OnFileChange));
108 Connect(wxEVT_RADIOBOX,
109 wxCommandEventHandler(StatBmpWidgetsPage::OnRadioChange));
110
111 m_statbmp = NULL;
112 RecreateWidget();
113 }
114
115 void StatBmpWidgetsPage::RecreateWidget()
116 {
117 wxDELETE(m_statbmp);
118
119 wxString filepath = m_filepicker->GetPath();
120 if ( filepath.empty() )
121 return;
122
123 wxImage image(filepath);
124 if (! image.IsOk() )
125 {
126 wxLogMessage("Reading image from file '%s' failed.", filepath.c_str());
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();
135 m_statbmp->Connect(wxEVT_LEFT_DOWN,
136 wxMouseEventHandler(StatBmpWidgetsPage::OnMouseEvent),
137 NULL, this);
138 // When switching from generic to native control on wxMSW under Wine,
139 // the explicit Refresh() is necessary
140 m_statbmp->Refresh();
141 }
142