]>
git.saurik.com Git - wxWidgets.git/blob - samples/widgets/statbmp.cpp
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 // Licence: wxWindows licence
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/filename.h"
41 #include "wx/generic/statbmpg.h"
43 #include "wx/filepicker.h"
46 #include "icons/statbmp.xpm"
49 class StatBmpWidgetsPage
: public WidgetsPage
52 StatBmpWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
53 : WidgetsPage(book
, imaglist
, statbmp_xpm
) {}
55 virtual void CreateContent();
56 virtual wxControl
*GetWidget() const { return m_statbmp
; }
57 virtual void RecreateWidget();
60 void OnFileChange(wxFileDirPickerEvent
&WXUNUSED(ev
)) { RecreateWidget(); }
61 void OnRadioChange(wxCommandEvent
&WXUNUSED(ev
)) { RecreateWidget(); }
63 void OnMouseEvent(wxMouseEvent
& WXUNUSED(event
))
65 wxLogMessage(wxT("wxStaticBitmap clicked."));
68 wxStaticBitmapBase
*m_statbmp
;
69 wxFilePickerCtrl
*m_filepicker
;
71 wxStaticBoxSizer
*m_sbsizer
;
73 DECLARE_WIDGETS_PAGE(StatBmpWidgetsPage
)
76 IMPLEMENT_WIDGETS_PAGE(StatBmpWidgetsPage
, wxT("StaticBitmap"),
79 void StatBmpWidgetsPage::CreateContent()
82 static const wxString choices
[] = { "native", "generic" };
83 m_radio
= new wxRadioBox(this, wxID_ANY
, "implementation",
84 wxDefaultPosition
, wxDefaultSize
,
85 WXSIZEOF(choices
), choices
);
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
);
95 m_sbsizer
= new wxStaticBoxSizer(wxVERTICAL
, this, "wxStaticBitmap inside");
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());
105 wxInitAllImageHandlers();
107 Connect(wxEVT_FILEPICKER_CHANGED
,
108 wxFileDirPickerEventHandler(StatBmpWidgetsPage::OnFileChange
));
109 Connect(wxEVT_RADIOBOX
,
110 wxCommandEventHandler(StatBmpWidgetsPage::OnRadioChange
));
116 void StatBmpWidgetsPage::RecreateWidget()
120 wxString filepath
= m_filepicker
->GetPath();
121 if ( filepath
.empty() )
124 wxImage
image(filepath
);
127 wxLogMessage("Reading image from file '%s' failed.", filepath
.c_str());
130 if (m_radio
->GetSelection() == 0)
131 m_statbmp
= new wxStaticBitmap(this, wxID_ANY
, wxBitmap(image
));
133 m_statbmp
= new wxGenericStaticBitmap(this, wxID_ANY
, wxBitmap(image
));
134 m_sbsizer
->Add(m_statbmp
, wxSizerFlags(1).Expand());
135 GetSizer()->Layout();
136 m_statbmp
->Connect(wxEVT_LEFT_DOWN
,
137 wxMouseEventHandler(StatBmpWidgetsPage::OnMouseEvent
),
139 // When switching from generic to native control on wxMSW under Wine,
140 // the explicit Refresh() is necessary
141 m_statbmp
->Refresh();