// Created: 1998
// RCS-ID: $Id$
// Copyright: (c) 1998-2005 Robert Roebling
+// (c) 2005-2009 Vadim Zeitlin
// License: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
class MyImageFrame : public wxFrame
{
public:
+ MyImageFrame(wxFrame *parent, const wxString& desc, const wxImage& image)
+ {
+ Create(parent, desc, wxBitmap(image), image.GetImageCount(desc));
+ }
+
MyImageFrame(wxFrame *parent, const wxString& desc, const wxBitmap& bitmap)
- : wxFrame(parent, wxID_ANY,
- wxString::Format(_T("Image from %s"), desc.c_str()),
- wxDefaultPosition, wxDefaultSize,
- wxDEFAULT_FRAME_STYLE | wxFULL_REPAINT_ON_RESIZE),
- m_bitmap(bitmap)
{
+ Create(parent, desc, bitmap);
+ }
+
+private:
+ bool Create(wxFrame *parent,
+ const wxString& desc,
+ const wxBitmap& bitmap,
+ int numImages = 1)
+ {
+ if ( !wxFrame::Create(parent, wxID_ANY,
+ wxString::Format(_T("Image from %s"), desc),
+ wxDefaultPosition, wxDefaultSize,
+ wxDEFAULT_FRAME_STYLE | wxFULL_REPAINT_ON_RESIZE) )
+ return false;
+
+ m_bitmap = bitmap;
+ m_zoom = 1.;
+
wxMenu *menu = new wxMenu;
menu->Append(wxID_SAVE);
menu->AppendSeparator();
- m_pClearBgMenu = menu->AppendCheckItem(ID_PAINT_BG, _T("&Paint background"));
+ menu->AppendCheckItem(ID_PAINT_BG, _T("&Paint background"),
+ "Uncheck this for transparent images");
menu->AppendSeparator();
menu->Append(ID_RESIZE, _T("&Fit to window\tCtrl-F"));
+ menu->Append(wxID_ZOOM_IN, "Zoom &in\tCtrl-+");
+ menu->Append(wxID_ZOOM_OUT, "Zoom &out\tCtrl--");
+ menu->Append(wxID_ZOOM_100, "Reset zoom to &100%\tCtrl-1");
menu->AppendSeparator();
menu->Append(ID_ROTATE_LEFT, _T("Rotate &left\tCtrl-L"));
menu->Append(ID_ROTATE_RIGHT, _T("Rotate &right\tCtrl-R"));
mbar->Append(menu, _T("&Image"));
SetMenuBar(mbar);
- CreateStatusBar();
+ mbar->Check(ID_PAINT_BG, true);
+
+ CreateStatusBar(2);
+ if ( numImages != 1 )
+ SetStatusText(wxString::Format("%d images", numImages), 1);
SetClientSize(bitmap.GetWidth(), bitmap.GetHeight());
UpdateStatusBar();
-// SetBackgroundColour(*wxWHITE);
+ Show();
+
+ return true;
}
void OnEraseBackground(wxEraseEvent& WXUNUSED(event))
{
wxPaintDC dc(this);
- if (m_pClearBgMenu->IsChecked())
+ if ( GetMenuBar()->IsChecked(ID_PAINT_BG) )
ClearBackground();
+ dc.SetUserScale(m_zoom, m_zoom);
+
const wxSize size = GetClientSize();
- dc.DrawBitmap(m_bitmap,
- (size.x - m_bitmap.GetWidth())/2,
- (size.y - m_bitmap.GetHeight())/2,
- true /* use mask */);
+ dc.DrawBitmap
+ (
+ m_bitmap,
+ dc.DeviceToLogicalX((size.x - m_zoom*m_bitmap.GetWidth())/2),
+ dc.DeviceToLogicalY((size.y - m_zoom*m_bitmap.GetHeight())/2),
+ true /* use mask */
+ );
}
void OnSave(wxCommandEvent& WXUNUSED(event))
m_bitmap = wxBitmap(img);
UpdateStatusBar();
- Refresh();
+ }
+
+ void OnZoom(wxCommandEvent& event)
+ {
+ if ( event.GetId() == wxID_ZOOM_IN )
+ m_zoom *= 1.2;
+ else if ( event.GetId() == wxID_ZOOM_OUT )
+ m_zoom /= 1.2;
+ else // wxID_ZOOM_100
+ m_zoom = 1.;
+
+ UpdateStatusBar();
}
void OnRotate(wxCommandEvent& event)
m_bitmap = wxBitmap(img);
UpdateStatusBar();
- Refresh();
}
-private:
void UpdateStatusBar()
{
- wxLogStatus(this, _T("Image size: (%d, %d)"),
+ wxLogStatus(this, _T("Image size: (%d, %d), zoom %.2f"),
m_bitmap.GetWidth(),
- m_bitmap.GetHeight());
+ m_bitmap.GetHeight(),
+ m_zoom);
+ Refresh();
}
wxBitmap m_bitmap;
- wxMenuItem* m_pClearBgMenu;
+ double m_zoom;
DECLARE_EVENT_TABLE()
};
EVT_MENU(wxID_SAVE, MyImageFrame::OnSave)
EVT_MENU_RANGE(ID_ROTATE_LEFT, ID_ROTATE_RIGHT, MyImageFrame::OnRotate)
EVT_MENU(ID_RESIZE, MyImageFrame::OnResize)
+
+ EVT_MENU(wxID_ZOOM_IN, MyImageFrame::OnZoom)
+ EVT_MENU(wxID_ZOOM_OUT, MyImageFrame::OnZoom)
+ EVT_MENU(wxID_ZOOM_100, MyImageFrame::OnZoom)
END_EVENT_TABLE()
//-----------------------------------------------------------------------------
void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
{
- (void)wxMessageBox( _T("wxImage demo\n")
- _T("Robert Roebling (c) 1998,2000"),
- _T("About wxImage Demo"), wxICON_INFORMATION | wxOK );
+ (void)wxMessageBox( "wxImage demo\n"
+ "(c) Robert Roebling 1998-2005"
+ "(c) Vadim Zeitlin 2005-2009",
+ "About wxImage Demo",
+ wxICON_INFORMATION | wxOK );
}
wxString MyFrame::LoadUserImage(wxImage& image)
wxImage image;
wxString filename = LoadUserImage(image);
if ( !filename.empty() )
- (new MyImageFrame(this, filename, wxBitmap(image)))->Show();
+ new MyImageFrame(this, filename, image);
}
void MyFrame::OnImageInfo( wxCommandEvent &WXUNUSED(event) )
}
else
{
- (new MyImageFrame(this, _T("Clipboard"), dobjBmp.GetBitmap()))->Show();
+ new MyImageFrame(this, _T("Clipboard"), dobjBmp.GetBitmap());
}
wxTheClipboard->Close();
}
const long loadTime = sw.Time();
- MyImageFrame * const
- frame = new MyImageFrame(this, filename, wxBitmap(image));
- frame->Show();
+ MyImageFrame * const frame = new MyImageFrame(this, filename, image);
wxLogStatus(frame, "Loaded \"%s\" in %ldms", filename, loadTime);
#else
wxLogError( _T("Couldn't create file selector dialog") );