| 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 | // Licence: wxWindows licence |
| 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/filename.h" |
| 40 | |
| 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 | |
| 49 | class StatBmpWidgetsPage : public WidgetsPage |
| 50 | { |
| 51 | public: |
| 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 | |
| 59 | private: |
| 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 | |
| 76 | IMPLEMENT_WIDGETS_PAGE(StatBmpWidgetsPage, wxT("StaticBitmap"), |
| 77 | ALL_CTRLS); |
| 78 | |
| 79 | void 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 | |
| 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); |
| 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 | |
| 107 | Connect(wxEVT_COMMAND_FILEPICKER_CHANGED, |
| 108 | wxFileDirPickerEventHandler(StatBmpWidgetsPage::OnFileChange)); |
| 109 | Connect(wxEVT_COMMAND_RADIOBOX_SELECTED, |
| 110 | wxCommandEventHandler(StatBmpWidgetsPage::OnRadioChange)); |
| 111 | |
| 112 | m_statbmp = NULL; |
| 113 | RecreateWidget(); |
| 114 | } |
| 115 | |
| 116 | void StatBmpWidgetsPage::RecreateWidget() |
| 117 | { |
| 118 | wxDELETE(m_statbmp); |
| 119 | |
| 120 | wxString filepath = m_filepicker->GetPath(); |
| 121 | if ( filepath.empty() ) |
| 122 | return; |
| 123 | |
| 124 | wxImage image(filepath); |
| 125 | if (! image.Ok() ) |
| 126 | { |
| 127 | wxLogMessage("Reading image from file '%s' failed.", filepath.c_str()); |
| 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(); |
| 136 | m_statbmp->Connect(wxEVT_LEFT_DOWN, |
| 137 | wxMouseEventHandler(StatBmpWidgetsPage::OnMouseEvent), |
| 138 | NULL, this); |
| 139 | // When switching from generic to native control on wxMSW under Wine, |
| 140 | // the explicit Refresh() is necessary |
| 141 | m_statbmp->Refresh(); |
| 142 | } |
| 143 | |