mirror of
https://github.com/wxWidgets/wxWidgets.git
synced 2026-09-24 07:24:31 +08:00
Show frames of animated images in image sample
This commit is contained in:
+61
-14
@@ -129,6 +129,8 @@ enum
|
||||
ID_ROTATE_LEFT = wxID_HIGHEST,
|
||||
ID_ROTATE_RIGHT,
|
||||
ID_RESIZE,
|
||||
ID_FRAME_NEXT,
|
||||
ID_FRAME_PREV,
|
||||
ID_ZOOM_x2,
|
||||
ID_ZOOM_DC,
|
||||
ID_ZOOM_NEAREST,
|
||||
@@ -144,7 +146,6 @@ public:
|
||||
MyImageFrame(wxFrame *parent, const wxString& desc, const wxImage& image, double scale = 1.0)
|
||||
{
|
||||
// Retrieve image info
|
||||
wxString info;
|
||||
int xres, yres;
|
||||
switch ( GetResolutionFromOptions(image, &xres, &yres) )
|
||||
{
|
||||
@@ -158,7 +159,7 @@ public:
|
||||
wxFALLTHROUGH;
|
||||
|
||||
case wxIMAGE_RESOLUTION_INCHES:
|
||||
info = wxString::Format("DPI %i x %i", xres, yres);
|
||||
m_imgResInfo = wxString::Format("DPI %i x %i", xres, yres);
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -166,16 +167,18 @@ public:
|
||||
break;
|
||||
}
|
||||
|
||||
int numImages = desc.StartsWith("Clipboard") ? 1 : image.GetImageCount(desc);
|
||||
if ( numImages > 1 )
|
||||
m_imgIndex = 1;
|
||||
m_imgScale = scale;
|
||||
m_imgSrc = desc;
|
||||
m_imgCount = desc.StartsWith("Clipboard") ? 1 : image.GetImageCount(desc);
|
||||
if (m_imgCount > 1 )
|
||||
{
|
||||
if ( !info.empty() )
|
||||
info += ", ";
|
||||
|
||||
info += wxString::Format("%d images", numImages);
|
||||
if (!m_imgResInfo.empty())
|
||||
m_imgResInfo += ", ";
|
||||
m_imgCountInfo = wxString::Format("%d/%d frames", m_imgIndex, m_imgCount);
|
||||
}
|
||||
|
||||
Create(parent, desc, wxBitmap(image, wxBITMAP_SCREEN_DEPTH, scale), info);
|
||||
Create(parent, desc, wxBitmap(image, wxBITMAP_SCREEN_DEPTH, scale));
|
||||
}
|
||||
|
||||
MyImageFrame(wxFrame *parent, const wxString& desc, const wxBitmap& bitmap)
|
||||
@@ -184,10 +187,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
bool Create(wxFrame *parent,
|
||||
const wxString& desc,
|
||||
const wxBitmap& bitmap,
|
||||
wxString info = wxString())
|
||||
bool Create(wxFrame *parent, const wxString& desc, const wxBitmap& bitmap)
|
||||
{
|
||||
if ( !wxFrame::Create(parent, wxID_ANY,
|
||||
wxString::Format("Image from %s", desc),
|
||||
@@ -207,6 +207,9 @@ private:
|
||||
menu->AppendSeparator();
|
||||
menu->Append(ID_RESIZE, "&Fit to window\tCtrl-F");
|
||||
menu->AppendSeparator();
|
||||
menu->Append(ID_FRAME_NEXT, "&Next frame\tCtrl->");
|
||||
menu->Append(ID_FRAME_PREV, "P&revious frame\tCtrl-<");
|
||||
menu->AppendSeparator();
|
||||
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");
|
||||
@@ -226,9 +229,12 @@ private:
|
||||
SetMenuBar(mbar);
|
||||
|
||||
mbar->Check(ID_PAINT_BG, true);
|
||||
mbar->Enable(ID_FRAME_NEXT, m_imgCount > 1);
|
||||
mbar->Enable(ID_FRAME_PREV, m_imgCount > 1);
|
||||
|
||||
CreateStatusBar(2);
|
||||
SetStatusText(info, 1);
|
||||
int widths[] = { -2, -1 };
|
||||
SetStatusWidths(2, widths);
|
||||
|
||||
SetClientSize(bitmap.GetWidth(), bitmap.GetHeight());
|
||||
|
||||
@@ -276,6 +282,37 @@ private:
|
||||
);
|
||||
}
|
||||
|
||||
void LoadBitmap() {
|
||||
if (m_imgSrc.empty() || m_imgSrc.StartsWith("Clipboard"))
|
||||
return;
|
||||
|
||||
wxImage image;
|
||||
if (!image.LoadFile(m_imgSrc, wxBITMAP_TYPE_ANY, m_imgIndex - 1))
|
||||
{
|
||||
wxLogError("Couldn't load image %d from '%s'.", m_imgIndex, m_imgSrc);
|
||||
return;
|
||||
}
|
||||
|
||||
m_bitmap = wxBitmap(image, wxBITMAP_SCREEN_DEPTH, m_imgScale);
|
||||
|
||||
m_imgCountInfo = wxString::Format("%d/%d frames", m_imgIndex, m_imgCount);
|
||||
|
||||
UpdateStatusBar();
|
||||
}
|
||||
|
||||
void OnFrame(wxCommandEvent& evt) {
|
||||
if (evt.GetId() == ID_FRAME_NEXT) {
|
||||
if (++m_imgIndex > m_imgCount)
|
||||
m_imgIndex = 1;
|
||||
LoadBitmap();
|
||||
}
|
||||
else if (evt.GetId() == ID_FRAME_PREV) {
|
||||
if (--m_imgIndex <= 0)
|
||||
m_imgIndex = m_imgCount;
|
||||
LoadBitmap();
|
||||
}
|
||||
}
|
||||
|
||||
void OnSave(wxCommandEvent& WXUNUSED(event))
|
||||
{
|
||||
#if wxUSE_FILEDLG
|
||||
@@ -560,6 +597,7 @@ private:
|
||||
m_bitmap.GetWidth(),
|
||||
m_bitmap.GetHeight(),
|
||||
m_zoom);
|
||||
SetStatusText(m_imgResInfo + m_imgCountInfo, 1);
|
||||
Refresh();
|
||||
}
|
||||
|
||||
@@ -606,6 +644,13 @@ private:
|
||||
bool m_useImageForZoom;
|
||||
wxImageResizeQuality m_resizeQuality;
|
||||
|
||||
int m_imgScale;
|
||||
int m_imgCount;
|
||||
int m_imgIndex;
|
||||
wxString m_imgSrc;
|
||||
wxString m_imgResInfo;
|
||||
wxString m_imgCountInfo;
|
||||
|
||||
wxDECLARE_EVENT_TABLE();
|
||||
};
|
||||
|
||||
@@ -1040,6 +1085,8 @@ wxBEGIN_EVENT_TABLE(MyImageFrame, wxFrame)
|
||||
EVT_MENU(wxID_SAVEAS, MyImageFrame::OnSave)
|
||||
EVT_MENU_RANGE(ID_ROTATE_LEFT, ID_ROTATE_RIGHT, MyImageFrame::OnRotate)
|
||||
EVT_MENU(ID_RESIZE, MyImageFrame::OnResize)
|
||||
EVT_MENU(ID_FRAME_NEXT, MyImageFrame::OnFrame)
|
||||
EVT_MENU(ID_FRAME_PREV, MyImageFrame::OnFrame)
|
||||
|
||||
EVT_MENU(wxID_ZOOM_IN, MyImageFrame::OnZoom)
|
||||
EVT_MENU(wxID_ZOOM_OUT, MyImageFrame::OnZoom)
|
||||
|
||||
Reference in New Issue
Block a user