-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImagePanel.cpp
77 lines (67 loc) · 1.63 KB
/
ImagePanel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <wx/dcbuffer.h>
#include "ImagePanel.h"
BEGIN_EVENT_TABLE(wxImagePanel, wxPanel)
EVT_PAINT(wxImagePanel::paintEvent)
EVT_SIZE(wxImagePanel::OnSize)
EVT_MOUSEWHEEL(wxImagePanel::MouseWheel)
EVT_LEFT_DOWN(wxImagePanel::mouseDown)
END_EVENT_TABLE()
wxImagePanel::wxImagePanel(wxWindow* parent) :
wxPanel(parent)
{
w = -1;
h = -1;
}
void wxImagePanel::paintEvent(wxPaintEvent & evt)
{
wxPaintDC dc(this);
render(dc);
}
void wxImagePanel::paintNow()
{
wxClientDC dc(this);
render(dc);
}
void wxImagePanel::SetImage(wxImage origimage)
{
image = origimage;
Refresh();
}
void wxImagePanel::render(wxDC& dc)
{
int newWidth, newHeight, oldWidth, oldHeight;
dc.GetSize(&newWidth, &newHeight);
oldWidth = newWidth;
oldHeight = newHeight;
if (newWidth != w || newHeight != h)
{
float scaleX = ((float)newWidth) / ((float)image.GetWidth());
double scaleY = ((float)newHeight) / ((float)image.GetHeight());
if (scaleX < 1.0 || scaleY < 1.0)
{
double scale = wxMin(scaleX, scaleY);
newWidth = (int)(scale * image.GetWidth());
newHeight = (int)(scale * image.GetHeight());
resized = wxBitmap(image.Scale(newWidth, newHeight, wxIMAGE_QUALITY_HIGH));
w = newWidth;
h = newHeight;
}
else
{
newWidth = image.GetWidth();
newHeight = image.GetHeight();
resized = wxBitmap(image, wxIMAGE_QUALITY_NORMAL);
}
dc.DrawBitmap(resized, (oldWidth - newWidth) / 2, (oldHeight - newHeight) / 2, false);
}
}
void wxImagePanel::OnSize(wxSizeEvent& event) {
Refresh();
event.Skip();
}
void wxImagePanel::MouseWheel(wxMouseEvent& event) {
event.Skip();
}
void wxImagePanel::mouseDown(wxMouseEvent& event) {
event.Skip();
}