]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/statbmp.cpp
Use symbolic constants instead of magic numbers in wxDataViewCtrl code.
[wxWidgets.git] / samples / widgets / statbmp.cpp
CommitLineData
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
7// Id: $Id$
8// Copyright: (c) 2008 Marcin Wojdyr
526954c5 9// Licence: wxWindows licence
a30e7029
VZ
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"
d8cda32e 32 #include "wx/image.h"
a30e7029
VZ
33 #include "wx/radiobox.h"
34 #include "wx/statbmp.h"
35 #include "wx/statbox.h"
36 #include "wx/textctrl.h"
37#endif
38
470ee714
VZ
39#include "wx/filename.h"
40
a30e7029
VZ
41#include "wx/generic/statbmpg.h"
42#include "wx/sizer.h"
43#include "wx/filepicker.h"
44
45#include "widgets.h"
46#include "icons/statbmp.xpm"
47
48
49class StatBmpWidgetsPage : public WidgetsPage
50{
51public:
52 StatBmpWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist)
53 : WidgetsPage(book, imaglist, statbmp_xpm) {}
54
55 virtual void CreateContent();
56 virtual wxControl *GetWidget() const { return m_statbmp; }
57 virtual void RecreateWidget();
58
59private:
60 void OnFileChange(wxFileDirPickerEvent &WXUNUSED(ev)) { RecreateWidget(); }
61 void OnRadioChange(wxCommandEvent &WXUNUSED(ev)) { RecreateWidget(); }
62
63 void OnMouseEvent(wxMouseEvent& WXUNUSED(event))
64 {
65 wxLogMessage(wxT("wxStaticBitmap clicked."));
66 }
67
68 wxStaticBitmapBase *m_statbmp;
69 wxFilePickerCtrl *m_filepicker;
70 wxRadioBox *m_radio;
71 wxStaticBoxSizer *m_sbsizer;
72
73 DECLARE_WIDGETS_PAGE(StatBmpWidgetsPage)
74};
75
76IMPLEMENT_WIDGETS_PAGE(StatBmpWidgetsPage, wxT("StaticBitmap"),
77 ALL_CTRLS);
78
79void StatBmpWidgetsPage::CreateContent()
80{
81
82 static const wxString choices[] = { "native", "generic" };
83 m_radio = new wxRadioBox(this, wxID_ANY, "implementation",
84 wxDefaultPosition, wxDefaultSize,
85 WXSIZEOF(choices), choices);
86
470ee714
VZ
87 wxString testImage;
88#if wxUSE_LIBPNG
89 wxFileName fn("../image/toucan.png");
90 if ( fn.FileExists() )
91 testImage = fn.GetFullPath();
92#endif // wxUSE_LIBPNG
93 m_filepicker = new wxFilePickerCtrl(this, wxID_ANY, testImage);
a30e7029
VZ
94
95 m_sbsizer = new wxStaticBoxSizer(wxVERTICAL, this, "wxStaticBitmap inside");
96
97 wxSizer *leftsizer = new wxBoxSizer(wxVERTICAL);
98 leftsizer->Add(m_radio, wxSizerFlags().Expand().Border());
99 leftsizer->Add(m_filepicker, wxSizerFlags().Expand().Border());
100 wxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
101 sizer->Add(leftsizer, wxSizerFlags().Border());
102 sizer->Add(m_sbsizer, wxSizerFlags().Center());
103 SetSizer(sizer);
104
105 wxInitAllImageHandlers();
106
470ee714 107 Connect(wxEVT_COMMAND_FILEPICKER_CHANGED,
a30e7029 108 wxFileDirPickerEventHandler(StatBmpWidgetsPage::OnFileChange));
470ee714 109 Connect(wxEVT_COMMAND_RADIOBOX_SELECTED,
a30e7029
VZ
110 wxCommandEventHandler(StatBmpWidgetsPage::OnRadioChange));
111
112 m_statbmp = NULL;
113 RecreateWidget();
114}
115
116void StatBmpWidgetsPage::RecreateWidget()
117{
5276b0a5 118 wxDELETE(m_statbmp);
470ee714 119
a30e7029 120 wxString filepath = m_filepicker->GetPath();
470ee714
VZ
121 if ( filepath.empty() )
122 return;
123
a30e7029 124 wxImage image(filepath);
a1b806b9 125 if (! image.IsOk() )
a30e7029
VZ
126 {
127 wxLogMessage("Reading image from file '%s' failed.", filepath.c_str());
a30e7029
VZ
128 return;
129 }
130 if (m_radio->GetSelection() == 0)
131 m_statbmp = new wxStaticBitmap(this, wxID_ANY, wxBitmap(image));
132 else
133 m_statbmp = new wxGenericStaticBitmap(this, wxID_ANY, wxBitmap(image));
134 m_sbsizer->Add(m_statbmp, wxSizerFlags(1).Expand());
135 GetSizer()->Layout();
470ee714 136 m_statbmp->Connect(wxEVT_LEFT_DOWN,
a30e7029
VZ
137 wxMouseEventHandler(StatBmpWidgetsPage::OnMouseEvent),
138 NULL, this);
470ee714 139 // When switching from generic to native control on wxMSW under Wine,
a30e7029 140 // the explicit Refresh() is necessary
470ee714 141 m_statbmp->Refresh();
a30e7029
VZ
142}
143