X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/176fe5af12b56eb9ba9b5d436556a42682f34916..015e69f36dfbc469eef59456f973d0567e865d70:/samples/image/image.cpp diff --git a/samples/image/image.cpp b/samples/image/image.cpp index 3db24946fa..55b68053ff 100644 --- a/samples/image/image.cpp +++ b/samples/image/image.cpp @@ -68,6 +68,7 @@ public: MyFrame(); void OnAbout( wxCommandEvent &event ); + void OnNewFrame( wxCommandEvent &event ); void OnQuit( wxCommandEvent &event ); MyCanvas *m_canvas; @@ -77,6 +78,30 @@ private: DECLARE_EVENT_TABLE() }; +class MyImageFrame : public wxFrame +{ +public: + MyImageFrame(wxFrame *parent, const wxBitmap& bitmap) + : wxFrame(parent, -1, _T("Frame with image"), + wxDefaultPosition, wxDefaultSize, + wxCAPTION | wxSYSTEM_MENU), + m_bitmap(bitmap) + { + SetClientSize(bitmap.GetWidth(), bitmap.GetHeight()); + } + + void OnPaint(wxPaintEvent& WXUNUSED(event)) + { + wxPaintDC dc( this ); + dc.DrawBitmap( m_bitmap, 0, 0 ); + } + +private: + wxBitmap m_bitmap; + + DECLARE_EVENT_TABLE() +}; + // MyApp class MyApp: public wxApp @@ -93,6 +118,10 @@ IMPLEMENT_APP(MyApp) IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow) +BEGIN_EVENT_TABLE(MyImageFrame, wxFrame) + EVT_PAINT(MyImageFrame::OnPaint) +END_EVENT_TABLE() + BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) EVT_PAINT(MyCanvas::OnPaint) END_EVENT_TABLE() @@ -143,12 +172,12 @@ MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id, #if wxUSE_LIBPNG if ( !image.SaveFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG )) wxLogError("Can't save file"); - + image.Destroy(); image.LoadFile( dir + wxString("test.png") ); my_square = new wxBitmap( image.ConvertToBitmap() ); - + image.Destroy(); if ( !image.LoadFile( dir + wxString("horse.png")) ) @@ -414,12 +443,14 @@ void MyCanvas::CreateAntiAliasedBitmap() const int ID_QUIT = 108; const int ID_ABOUT = 109; +const int ID_NEW = 110; IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame ) BEGIN_EVENT_TABLE(MyFrame,wxFrame) EVT_MENU (ID_ABOUT, MyFrame::OnAbout) EVT_MENU (ID_QUIT, MyFrame::OnQuit) + EVT_MENU (ID_NEW, MyFrame::OnNewFrame) END_EVENT_TABLE() MyFrame::MyFrame() @@ -427,7 +458,10 @@ MyFrame::MyFrame() wxPoint(20,20), wxSize(470,360) ) { wxMenu *file_menu = new wxMenu(); + file_menu->Append( ID_NEW, "&Show image..."); + file_menu->AppendSeparator(); file_menu->Append( ID_ABOUT, "&About..."); + file_menu->AppendSeparator(); file_menu->Append( ID_QUIT, "E&xit"); wxMenuBar *menu_bar = new wxMenuBar(); @@ -457,6 +491,23 @@ void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) ) "About wxImage Demo", wxICON_INFORMATION | wxOK ); } +void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) ) +{ + wxString filename = wxFileSelector(_T("Select image file")); + if ( !filename ) + return; + + wxImage image; + if ( !image.LoadFile(filename) ) + { + wxLogError(_T("Couldn't load image from '%s'."), filename.c_str()); + + return; + } + + (new MyImageFrame(this, image.ConvertToBitmap()))->Show(); +} + //----------------------------------------------------------------------------- // MyApp //-----------------------------------------------------------------------------