-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageView.cpp
More file actions
123 lines (123 loc) · 7.36 KB
/
Copy pathImageView.cpp
File metadata and controls
123 lines (123 loc) · 7.36 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// ImageView.cpp
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright (c) 2019-2023 Adrian Maurer. All rights reserved.
// Distributed under the GPL-3.0 software license (https://opensource.org/licenses/GPL-3.0).
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include "pch.h"
#include "framework.h"
#include "ImageView.h"
#include "HspGradatorApp.h"
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// CImageView
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// CImageView - CONSTRUCTION / DESTRUCTION / ASSIGNMENT
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CImageView::CImageView() :
p_image(nullptr),
wndSize(0, 0),
imageOrigin(0, 0),
imageSize(0, 0)
{
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IMPLEMENT_DYNCREATE(CImageView, CView)
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// CImageView - ELEMENT FUNCTIONS
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void CImageView::update(const CTrueColorImage* const p_mi)
{
p_image = p_mi;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void CImageView::recalcLayout()
{
if (p_image && !p_image->IsNull())
{
const float iar = static_cast<float>(p_image->calcAspectRatio());
const CSize& ws = wndSize;
const float war = ws.cx / static_cast<float>(ws.cy);
constexpr double isf = 0.95; // Image size factor
if (iar < war)
{
imageSize.cx = ama::round<int>(ws.cy * isf * iar);
imageSize.cy = ama::round<int>(ws.cy * isf);
}
else
{
imageSize.cx = ama::round<int>(ws.cx * isf);
imageSize.cy = ama::round<int>(ws.cx * isf / iar);
}
imageOrigin.x = ama::round((ws.cx - imageSize.cx) / 2.0F);
imageOrigin.y = ama::round((ws.cy - imageSize.cy) / 2.0F);
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void CImageView::refresh()
{
Invalidate(FALSE);
UpdateWindow();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// CImageView - OVERRIDES
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
BOOL CImageView::PreCreateWindow(CREATESTRUCT& cs)
{
if (CView::PreCreateWindow(cs))
{
cs.dwExStyle |= WS_EX_CLIENTEDGE;
cs.style &= ~WS_BORDER;
cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS, ::LoadCursor(nullptr, IDC_ARROW));
return TRUE;
}
return FALSE;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void CImageView::OnDraw(CDC* pDC)
{
const COLORREF bc = ::GetSysColor(COLOR_3DLIGHT);
if (pDC)
{
if (p_image && !p_image->IsNull())
{
pDC->FillSolidRect(0, 0, imageOrigin.x, wndSize.cy, bc);
pDC->FillSolidRect(imageOrigin.x + imageSize.cx, 0, wndSize.cx - imageOrigin.x - imageSize.cx, wndSize.cy, bc);
pDC->FillSolidRect(imageOrigin.x, 0, imageSize.cx, imageOrigin.y, bc);
pDC->FillSolidRect(imageOrigin.x, imageOrigin.y + imageSize.cy, imageSize.cx, wndSize.cy - imageOrigin.y - imageSize.cy, bc);
pDC->SetStretchBltMode(COLORONCOLOR);
p_image->StretchBlt(pDC->GetSafeHdc(), imageOrigin.x, imageOrigin.y, imageSize.cx, imageSize.cy);
}
else
{
CRect wr(0, 0, wndSize.cx, wndSize.cy);
pDC->FillSolidRect(wr, bc);
pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
pDC->DrawText(L"Double click to load image", &wr, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
}
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// CImageView - MESSAGE MAP FUNCTIONS
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
BEGIN_MESSAGE_MAP(CImageView, CView)
ON_WM_SIZE()
ON_WM_LBUTTONDBLCLK()
END_MESSAGE_MAP()
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void CImageView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
wndSize = {cx, cy};
recalcLayout();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void CImageView::OnLButtonDblClk(UINT nFlags, CPoint pnt)
{
sendMessageToMainWnd(WM_COMMAND, ID_FILE_LOAD_IMAGE);
CView::OnLButtonDblClk(nFlags, pnt);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------