2008-06-21 11:32:33 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Program: wxWidgets Widgets Sample
|
|
|
|
// Name: statbmp.cpp
|
|
|
|
// Purpose: Part of the widgets sample showing wxStaticBitmap
|
|
|
|
// Author: Marcin Wojdyr
|
|
|
|
// Created: 2008-06-19
|
|
|
|
// Copyright: (c) 2008 Marcin Wojdyr
|
2010-07-13 09:29:13 -04:00
|
|
|
// Licence: wxWindows licence
|
2008-06-21 11:32:33 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// declarations
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// for compilers that support precompilation, includes "wx/wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
|
|
|
|
// for all others, include the necessary headers
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/log.h"
|
|
|
|
|
|
|
|
#include "wx/button.h"
|
2008-06-26 13:25:28 -04:00
|
|
|
#include "wx/image.h"
|
2008-06-21 11:32:33 -04:00
|
|
|
#include "wx/radiobox.h"
|
|
|
|
#include "wx/statbmp.h"
|
|
|
|
#include "wx/statbox.h"
|
|
|
|
#include "wx/textctrl.h"
|
|
|
|
#endif
|
|
|
|
|
2018-09-22 08:33:29 -04:00
|
|
|
#include "wx/artprov.h"
|
2009-12-03 11:26:25 -05:00
|
|
|
#include "wx/filename.h"
|
|
|
|
|
2008-06-21 11:32:33 -04:00
|
|
|
#include "wx/generic/statbmpg.h"
|
|
|
|
#include "wx/sizer.h"
|
|
|
|
#include "wx/filepicker.h"
|
|
|
|
|
|
|
|
#include "widgets.h"
|
|
|
|
#include "icons/statbmp.xpm"
|
|
|
|
|
|
|
|
|
|
|
|
class StatBmpWidgetsPage : public WidgetsPage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
StatBmpWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist)
|
|
|
|
: WidgetsPage(book, imaglist, statbmp_xpm) {}
|
|
|
|
|
2014-03-29 20:02:23 -04:00
|
|
|
virtual void CreateContent() wxOVERRIDE;
|
2015-07-31 11:53:55 -04:00
|
|
|
virtual wxWindow *GetWidget() const wxOVERRIDE { return m_statbmp; }
|
2014-03-29 20:02:23 -04:00
|
|
|
virtual void RecreateWidget() wxOVERRIDE;
|
2008-06-21 11:32:33 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
void OnFileChange(wxFileDirPickerEvent &WXUNUSED(ev)) { RecreateWidget(); }
|
|
|
|
void OnRadioChange(wxCommandEvent &WXUNUSED(ev)) { RecreateWidget(); }
|
|
|
|
|
|
|
|
void OnMouseEvent(wxMouseEvent& WXUNUSED(event))
|
|
|
|
{
|
2018-09-28 21:45:15 -04:00
|
|
|
wxLogMessage("wxStaticBitmap clicked.");
|
2008-06-21 11:32:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
wxStaticBitmapBase *m_statbmp;
|
|
|
|
wxFilePickerCtrl *m_filepicker;
|
|
|
|
wxRadioBox *m_radio;
|
2016-01-19 12:52:50 -05:00
|
|
|
wxRadioBox *m_scaleRadio;
|
2008-06-21 11:32:33 -04:00
|
|
|
wxStaticBoxSizer *m_sbsizer;
|
|
|
|
|
|
|
|
DECLARE_WIDGETS_PAGE(StatBmpWidgetsPage)
|
|
|
|
};
|
|
|
|
|
2018-09-28 21:45:15 -04:00
|
|
|
IMPLEMENT_WIDGETS_PAGE(StatBmpWidgetsPage, "StaticBitmap",
|
2008-06-21 11:32:33 -04:00
|
|
|
ALL_CTRLS);
|
|
|
|
|
|
|
|
void StatBmpWidgetsPage::CreateContent()
|
|
|
|
{
|
|
|
|
|
|
|
|
static const wxString choices[] = { "native", "generic" };
|
|
|
|
m_radio = new wxRadioBox(this, wxID_ANY, "implementation",
|
|
|
|
wxDefaultPosition, wxDefaultSize,
|
|
|
|
WXSIZEOF(choices), choices);
|
2016-01-19 12:52:50 -05:00
|
|
|
static const wxString scaleChoices[] = { "None", "Fill", "Aspect Fit", "Aspect Fill" };
|
|
|
|
m_scaleRadio = new wxRadioBox(this, wxID_ANY, "Scale Mode",
|
|
|
|
wxDefaultPosition, wxDefaultSize,
|
|
|
|
WXSIZEOF(scaleChoices), scaleChoices);
|
2008-06-21 11:32:33 -04:00
|
|
|
|
2009-12-03 11:26:25 -05:00
|
|
|
wxString testImage;
|
|
|
|
#if wxUSE_LIBPNG
|
2018-05-17 13:42:43 -04:00
|
|
|
wxPathList pathlist;
|
2018-09-28 21:45:15 -04:00
|
|
|
pathlist.Add(".");
|
|
|
|
pathlist.Add("..");
|
|
|
|
pathlist.Add("../image");
|
|
|
|
pathlist.Add("../../../samples/image");
|
2018-05-17 13:42:43 -04:00
|
|
|
|
2018-09-28 21:45:15 -04:00
|
|
|
wxFileName fn(pathlist.FindValidPath("toucan.png"));
|
2009-12-03 11:26:25 -05:00
|
|
|
if ( fn.FileExists() )
|
|
|
|
testImage = fn.GetFullPath();
|
|
|
|
#endif // wxUSE_LIBPNG
|
|
|
|
m_filepicker = new wxFilePickerCtrl(this, wxID_ANY, testImage);
|
2008-06-21 11:32:33 -04:00
|
|
|
|
|
|
|
m_sbsizer = new wxStaticBoxSizer(wxVERTICAL, this, "wxStaticBitmap inside");
|
|
|
|
|
|
|
|
wxSizer *leftsizer = new wxBoxSizer(wxVERTICAL);
|
|
|
|
leftsizer->Add(m_radio, wxSizerFlags().Expand().Border());
|
2016-01-19 12:52:50 -05:00
|
|
|
leftsizer->Add(m_scaleRadio, wxSizerFlags().Expand().Border());
|
2008-06-21 11:32:33 -04:00
|
|
|
leftsizer->Add(m_filepicker, wxSizerFlags().Expand().Border());
|
|
|
|
wxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
|
|
|
|
sizer->Add(leftsizer, wxSizerFlags().Border());
|
|
|
|
sizer->Add(m_sbsizer, wxSizerFlags().Center());
|
|
|
|
SetSizer(sizer);
|
|
|
|
|
|
|
|
wxInitAllImageHandlers();
|
|
|
|
|
2018-05-29 11:32:07 -04:00
|
|
|
Bind(wxEVT_FILEPICKER_CHANGED, &StatBmpWidgetsPage::OnFileChange, this);
|
|
|
|
Bind(wxEVT_RADIOBOX, &StatBmpWidgetsPage::OnRadioChange, this);
|
2008-06-21 11:32:33 -04:00
|
|
|
|
|
|
|
m_statbmp = NULL;
|
|
|
|
RecreateWidget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StatBmpWidgetsPage::RecreateWidget()
|
|
|
|
{
|
2010-06-20 14:18:23 -04:00
|
|
|
wxDELETE(m_statbmp);
|
2009-12-03 11:26:25 -05:00
|
|
|
|
2018-09-22 08:33:29 -04:00
|
|
|
wxBitmap bmp;
|
|
|
|
|
2008-06-21 11:32:33 -04:00
|
|
|
wxString filepath = m_filepicker->GetPath();
|
2018-09-22 08:33:29 -04:00
|
|
|
if ( !filepath.empty() )
|
|
|
|
{
|
|
|
|
wxImage image(filepath);
|
|
|
|
if ( image.IsOk() )
|
|
|
|
{
|
|
|
|
bmp = image;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-01 19:32:02 -04:00
|
|
|
wxLogMessage("Reading image from file '%s' failed.", filepath);
|
2018-09-22 08:33:29 -04:00
|
|
|
}
|
|
|
|
}
|
2009-12-03 11:26:25 -05:00
|
|
|
|
2018-09-22 08:33:29 -04:00
|
|
|
if ( !bmp.IsOk() )
|
2008-06-21 11:32:33 -04:00
|
|
|
{
|
2018-09-22 08:33:29 -04:00
|
|
|
// Show at least something.
|
|
|
|
bmp = wxArtProvider::GetBitmap(wxART_MISSING_IMAGE);
|
2008-06-21 11:32:33 -04:00
|
|
|
}
|
2018-03-17 12:50:56 -04:00
|
|
|
|
|
|
|
long style = GetAttrs().m_defaultFlags;
|
|
|
|
|
2008-06-21 11:32:33 -04:00
|
|
|
if (m_radio->GetSelection() == 0)
|
2018-03-17 12:50:56 -04:00
|
|
|
{
|
2018-09-22 08:33:29 -04:00
|
|
|
m_statbmp = new wxStaticBitmap(this, wxID_ANY, bmp,
|
2018-03-17 12:50:56 -04:00
|
|
|
wxDefaultPosition, wxDefaultSize,
|
|
|
|
style);
|
|
|
|
}
|
2008-06-21 11:32:33 -04:00
|
|
|
else
|
2018-03-17 12:50:56 -04:00
|
|
|
{
|
2018-09-22 08:33:29 -04:00
|
|
|
m_statbmp = new wxGenericStaticBitmap(this, wxID_ANY, bmp,
|
2018-03-17 12:50:56 -04:00
|
|
|
wxDefaultPosition, wxDefaultSize,
|
|
|
|
style);
|
|
|
|
}
|
2016-01-19 12:52:50 -05:00
|
|
|
|
|
|
|
wxStaticBitmapBase::ScaleMode scaleMode = (wxStaticBitmapBase::ScaleMode) m_scaleRadio->GetSelection();
|
|
|
|
m_statbmp->SetScaleMode(scaleMode);
|
|
|
|
if ( m_statbmp->GetScaleMode() != scaleMode )
|
|
|
|
wxLogError("Scale mode not supported by current implementation");
|
|
|
|
wxSizerItem* sbsizerItem = GetSizer()->GetItem(m_sbsizer);
|
|
|
|
if ( scaleMode == wxStaticBitmapBase::Scale_None )
|
|
|
|
{
|
|
|
|
sbsizerItem->SetProportion(0);
|
|
|
|
sbsizerItem->SetFlag(wxCENTER);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sbsizerItem->SetProportion(1);
|
|
|
|
sbsizerItem->SetFlag(wxEXPAND);
|
|
|
|
}
|
2008-06-21 11:32:33 -04:00
|
|
|
m_sbsizer->Add(m_statbmp, wxSizerFlags(1).Expand());
|
|
|
|
GetSizer()->Layout();
|
2018-05-29 11:32:07 -04:00
|
|
|
m_statbmp->Bind(wxEVT_LEFT_DOWN, &StatBmpWidgetsPage::OnMouseEvent, this);
|
|
|
|
|
2009-12-03 11:26:25 -05:00
|
|
|
// When switching from generic to native control on wxMSW under Wine,
|
2008-06-21 11:32:33 -04:00
|
|
|
// the explicit Refresh() is necessary
|
2009-12-03 11:26:25 -05:00
|
|
|
m_statbmp->Refresh();
|
2008-06-21 11:32:33 -04:00
|
|
|
}
|
|
|
|
|