forked from elha/CDex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDIBStatic.cpp
More file actions
223 lines (184 loc) · 5.04 KB
/
DIBStatic.cpp
File metadata and controls
223 lines (184 loc) · 5.04 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
** CDex - Open Source Digital Audio CD Extractor
**
** Copyright (C) 2006 - 2007 Georgy Berdyshev
** Copyright (C) 1998 Jorge Lodos (lodos@cigb.edu.cu)
**
** http://cdexos.sourceforge.net/
** http://sourceforge.net/projects/cdexos
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// DIBStatic.cpp : implementation file
//
#include "stdafx.h"
#include "Dib.h"
#include "DIBStatic.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDIBStatic
CDIBStatic::CDIBStatic()
{
}
CDIBStatic::~CDIBStatic()
{
}
BEGIN_MESSAGE_MAP(CDIBStatic, CStatic)
//{{AFX_MSG_MAP(CDIBStatic)
ON_WM_CTLCOLOR_REFLECT()
ON_WM_QUERYNEWPALETTE()
ON_WM_PALETTECHANGED()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CDIBStatic::LoadDib(LPCTSTR lpszFileName)
{
try
{
CFile file(lpszFileName, CFile::modeRead);
return LoadDib(file);
}
catch (CFileException* e)
{
e->Delete();
return FALSE;
}
}
BOOL CDIBStatic::LoadDib(CFile& file)
{
ASSERT_VALID(this);
BOOL bResult = TRUE;
if (!m_DIB.Read(file))
bResult = FALSE;
DoRealizePalette(FALSE);
UpdateDib();
return bResult;
}
BOOL CDIBStatic::LoadFromResource(LPCTSTR lpszResourceName)
{
ASSERT_VALID(this);
BOOL bResult = TRUE;
if (!m_DIB.LoadFromResource(lpszResourceName))
bResult = FALSE;
DoRealizePalette(FALSE);
UpdateDib();
return bResult;
}
void CDIBStatic::ClearDib()
{
ASSERT_VALID(this);
CClientDC dc(this);
CRect rectPaint;
GetClientRect(&rectPaint);
rectPaint.InflateRect(-1,-1);
CBrush* pBrushWhite;
pBrushWhite = CBrush::FromHandle((HBRUSH)::GetStockObject(LTGRAY_BRUSH)); // WHITE_BRUSH
dc.FillRect(&rectPaint, pBrushWhite);
}
void CDIBStatic::PaintDib(BOOL bDibValid)
{
ASSERT_VALID(this);
ClearDib();
CRect PaintRect;
GetClientRect(&PaintRect);
PaintRect.InflateRect(-1, -1);
CClientDC dc(this);
if (bDibValid)
{
int nDestX, nDestY, nDestWidth, nDestHeight;
if (m_DIB.Width() < (DWORD)PaintRect.Width() && m_DIB.Height() < (DWORD)PaintRect.Height())
{ // If the image fits, just center it
nDestX = PaintRect.left + (PaintRect.Width() - m_DIB.Width())/2;
nDestY = PaintRect.top + (PaintRect.Height() - m_DIB.Height())/2;
// AF, Width and Height are mixed up
// nDestWidth = m_DIB.Height();
// nDestHeight = m_DIB.Width();
// this seems to be the correct code
nDestHeight = m_DIB.Height();
nDestWidth = m_DIB.Width();
}
else
{ // The bitmap doesn't fit, scale to fit
if ((PaintRect.Width()/(float)m_DIB.Width()) <= (PaintRect.Height()/(float)m_DIB.Height()))
{ // Width is constraint
nDestWidth = PaintRect.Width();
nDestHeight = (nDestWidth*m_DIB.Height()) / m_DIB.Width();
nDestX = PaintRect.left;
nDestY = PaintRect.top + (PaintRect.Height() - nDestHeight) /2;
}
else
{ // Height is constraint
nDestHeight = PaintRect.Height();
nDestWidth = (nDestHeight*m_DIB.Width()) / m_DIB.Height();
nDestX = PaintRect.left + (PaintRect.Width() - nDestWidth) /2;
nDestY = PaintRect.top;
}
}
CRect RectDest(nDestX, nDestY, nDestX+nDestWidth, nDestY+nDestHeight);
CRect RectDib(0, 0, m_DIB.Width(), m_DIB.Height());
m_DIB.Paint(dc, &RectDest, &RectDib);
}
/*
Trozo eliminado por mi ya que no me gusta que pinten estas lineas so desgraciado.
else
{
dc.MoveTo(PaintRect.TopLeft());
dc.LineTo(PaintRect.BottomRight());
dc.MoveTo(PaintRect.right, PaintRect.top);
dc.LineTo(PaintRect.left, PaintRect.bottom);
}
*/
return;
}
void CDIBStatic::UpdateDib()
{
ASSERT_VALID(this);
PaintDib(IsValidDib());
}
/////////////////////////////////////////////////////////////////////////////
// CDIBStatic message handlers
HBRUSH CDIBStatic::CtlColor(CDC* pDC, UINT nCtlColor)
{
UpdateDib();
return (HBRUSH)GetStockObject(NULL_BRUSH);
}
BOOL CDIBStatic::OnQueryNewPalette()
{
return DoRealizePalette(FALSE);
}
void CDIBStatic::OnPaletteChanged(CWnd* pFocusWnd)
{
DoRealizePalette(TRUE);
}
BOOL CDIBStatic::DoRealizePalette(BOOL bForceBackGround)
{
if (IsValidDib())
{
CClientDC dc(this);
if (!m_DIB.m_pPalette)
return FALSE;
HPALETTE hPal = (HPALETTE)m_DIB.m_pPalette->m_hObject;
HPALETTE hOldPalette = SelectPalette(dc, hPal, bForceBackGround);
UINT nChanged = dc.RealizePalette();
SelectPalette(dc, hOldPalette, TRUE);
if (nChanged == 0) // no change to our mapping
return FALSE;
// some changes have been made; invalidate
UpdateDib();
}
return TRUE;
}