-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEllepticalButton.cpp
422 lines (307 loc) · 12.4 KB
/
EllepticalButton.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// RoundButton.cpp : implementation file
//
// Round Buttons!
//
// Written by Chris Maunder ([email protected])
// Copyright (c) 1997,1998.
//
// Modified: 2 Feb 1998 - Fix vis problem, CRgn resource leak,
// button reposition code redone. CJM.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name is included. If
// the source code in this file is used in any commercial application
// then a simple email would be nice.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage to your
// computer, causes your pet cat to fall ill, increases baldness or
// makes you car start emitting strange noises when you start it up.
//
// Expect bugs.
//
// Please use and enjoy. Please let me know of any bugs/mods/improvements
// that you have found/implemented and I will fix/incorporate them into this
// file.
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "math.h"
#include "EllepticalButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// prototypes
COLORREF GetColour(double dAngle, COLORREF crBright, COLORREF crDark);
void DrawEllipse(CDC* pDC, CPoint p, LONG lHorizontalRadius,LONG lVerticalRadius, COLORREF crColour, BOOL bDashed = FALSE);
void DrawEllipse(CDC* pDC, CPoint p, LONG lHorizontalRadius, LONG lVerticalRadius,COLORREF crBright, COLORREF crDark);
// Calculate colour for a point at the given angle by performing a linear
// interpolation between the colours crBright and crDark based on the cosine
// of the angle between the light source and the point.
//
// Angles are measured from the +ve x-axis (i.e. (1,0) = 0 degrees, (0,1) = 90 degrees )
// But remember: +y points down!
COLORREF GetColour(double dAngle, COLORREF crBright, COLORREF crDark)
{
#define Rad2Deg 180.0/3.1415
#define LIGHT_SOURCE_ANGLE -2.356 // -2.356 radians = -135 degrees, i.e. From top left
ASSERT(dAngle > -3.1416 && dAngle < 3.1416);
double dAngleDifference = LIGHT_SOURCE_ANGLE - dAngle;
if (dAngleDifference < -3.1415) dAngleDifference = 6.293 + dAngleDifference;
else if (dAngleDifference > 3.1415) dAngleDifference = 6.293 - dAngleDifference;
double Weight = 0.5*(cos(dAngleDifference)+1.0);
BYTE Red = (BYTE) (Weight*GetRValue(crBright) + (1.0-Weight)*GetRValue(crDark));
BYTE Green = (BYTE) (Weight*GetGValue(crBright) + (1.0-Weight)*GetGValue(crDark));
BYTE Blue = (BYTE) (Weight*GetBValue(crBright) + (1.0-Weight)*GetBValue(crDark));
//TRACE("LightAngle = %0.0f, Angle = %3.0f, Diff = %3.0f, Weight = %0.2f, RGB %3d,%3d,%3d\n",
// LIGHT_SOURCE_ANGLE*Rad2Deg, dAngle*Rad2Deg, dAngleDifference*Rad2Deg, Weight,Red,Green,Blue);
return RGB(Red, Green, Blue);
}
void DrawEllipse(CDC* pDC, CPoint p, LONG lHorizontalRadius,LONG lVerticalRadius, COLORREF crColour, BOOL bDashed)
{
const int nDashLength = 1;
LONG lXoffset, lYoffset;
int nDash = 0;
BOOL bDashOn = TRUE;
//Check to see that the coordinates are valid
ASSERT( (p.x + lHorizontalRadius <= LONG_MAX) && (p.y + lVerticalRadius <= LONG_MAX) );
ASSERT( (p.x - lHorizontalRadius >= LONG_MIN) && (p.y - lVerticalRadius >= LONG_MIN) );
long d1,d2;
long t1,t2,t3,t4,t5,t6,t7,t8,t9;
lXoffset = lHorizontalRadius;
lYoffset= 0;
t1 = lHorizontalRadius*lHorizontalRadius; t2 = 4*t1; t3 = 2*t2;
t4 = lVerticalRadius*lVerticalRadius;
t5 = 4*t4;
t6 = 2*t5;
t7 = lHorizontalRadius*t5; t8 = 2*t7;
t9 = 0; d1 = t2 - t7 + t4;
d2 = t1 - t8 + t5;
while (d2 < 0)
{/* region 1 of ellipse*/
if (bDashOn) {
pDC->SetPixelV(p.x + lXoffset, p.y + lYoffset, crColour);
pDC->SetPixelV(p.x + lXoffset, p.y - lYoffset, crColour);
pDC->SetPixelV(p.x - lXoffset, p.y + lYoffset, crColour);
pDC->SetPixelV(p.x - lXoffset, p.y - lYoffset, crColour);
}
//Advance the error term and the constant X axis step
lYoffset++;
t9 = t9 + t3;
if (d1 < 0) { d1 = d1 + t9 + t2; d2 = d2 + t9; }
else
{ lXoffset --; t8 = t8 - t6; d1 = d1 - t8 + t9 + t2; d2 = d2 - t8 + t9 + t5;
}
if (bDashed && (++nDash == nDashLength)) {
nDash = 0;
bDashOn = !bDashOn;
}
}
do
{
if (bDashOn)
{
pDC->SetPixelV(p.x + lXoffset, p.y + lYoffset, crColour);
pDC->SetPixelV(p.x + lXoffset, p.y - lYoffset, crColour);
pDC->SetPixelV(p.x - lXoffset, p.y + lYoffset, crColour);
pDC->SetPixelV(p.x - lXoffset, p.y - lYoffset, crColour);
}
lXoffset--;
t8 = t8 - t6;
if (d2 < 0)
{
lYoffset ++; t9 = t9 + t3; d2 = d2 - t8 + t5 + t9;
}
else
d2 = d2 - t8 + t5;
if (bDashed && (++nDash == nDashLength)) {
nDash = 0;
bDashOn = !bDashOn;
}
} while (lXoffset>=0); //Continue until halfwaX point
}
void DrawEllipse(CDC* pDC, CPoint p, LONG lHorizontalRadius,LONG lVerticalRadius, COLORREF crBright, COLORREF crDark)
{
LONG lXoffset, lYoffset;
double dAngle ;
const double Pi = 3.141592654,
Pi_on_2 = Pi * 0.5,
Three_Pi_on_2 = Pi * 1.5;
COLORREF crColour;
//Check to see that the coordinates are valid
ASSERT( (p.x + lHorizontalRadius <= LONG_MAX) && (p.y + lVerticalRadius <= LONG_MAX) );
ASSERT( (p.x - lHorizontalRadius >= LONG_MIN) && (p.y - lVerticalRadius >= LONG_MIN) );
long d1,d2;
long t1,t2,t3,t4,t5,t6,t7,t8,t9;
lXoffset = lHorizontalRadius;
lYoffset= 0;
t1 = lHorizontalRadius*lHorizontalRadius; t2 = 4*t1; t3 = 2*t2;
t4 = lVerticalRadius*lVerticalRadius;
t5 = 4*t4;
t6 = 2*t5;
t7 = lHorizontalRadius*t5; t8 = 2*t7;
t9 = 0; d1 = t2 - t7 + t4;
d2 = t1 - t8 + t5;
while (d2 < 0)
{
dAngle = atan2(lYoffset, lXoffset);
//Draw the current pixel, reflected across all eight arcs
crColour = GetColour(dAngle, crBright, crDark);
pDC->SetPixelV(p.x + lXoffset, p.y + lYoffset, crColour);
crColour = GetColour(Pi_on_2 - dAngle, crBright, crDark);
//pDC->SetPixelV(p.x + lYoffset, p.y + lXoffset, crColour);
crColour = GetColour(Pi_on_2 + dAngle, crBright, crDark);
// pDC->SetPixelV(p.x - lYoffset, p.y + lXoffset, crColour);
crColour = GetColour(Pi - dAngle, crBright, crDark);
pDC->SetPixelV(p.x - lXoffset, p.y + lYoffset, crColour);
crColour = GetColour(-Pi + dAngle, crBright, crDark);
pDC->SetPixelV(p.x - lXoffset, p.y - lYoffset, crColour);
crColour = GetColour(-Pi_on_2 - dAngle, crBright, crDark);
//pDC->SetPixelV(p.x - lYoffset, p.y - lXoffset, crColour);
crColour = GetColour(-Pi_on_2 + dAngle, crBright, crDark);
// pDC->SetPixelV(p.x + lYoffset, p.y - lXoffset, crColour);
crColour = GetColour(-dAngle, crBright, crDark);
pDC->SetPixelV(p.x + lXoffset, p.y - lYoffset, crColour);
lYoffset++;
t9 = t9 + t3;
if (d1 < 0) { d1 = d1 + t9 + t2; d2 = d2 + t9; }
else
{ lXoffset --; t8 = t8 - t6; d1 = d1 - t8 + t9 + t2; d2 = d2 - t8 + t9 + t5;
}
}
do {
dAngle = atan2(lYoffset, lXoffset);
//Draw the current pixel, reflected across all eight arcs
crColour = GetColour(dAngle, crBright, crDark);
pDC->SetPixelV(p.x + lXoffset, p.y + lYoffset, crColour);
crColour = GetColour(Pi_on_2 - dAngle, crBright, crDark);
//pDC->SetPixelV(p.x + lYoffset, p.y + lXoffset, crColour);
crColour = GetColour(Pi_on_2 + dAngle, crBright, crDark);
// pDC->SetPixelV(p.x - lYoffset, p.y + lXoffset, crColour);
crColour = GetColour(Pi - dAngle, crBright, crDark);
pDC->SetPixelV(p.x - lXoffset, p.y + lYoffset, crColour);
crColour = GetColour(-Pi + dAngle, crBright, crDark);
pDC->SetPixelV(p.x - lXoffset, p.y - lYoffset, crColour);
crColour = GetColour(-Pi_on_2 - dAngle, crBright, crDark);
//pDC->SetPixelV(p.x - lYoffset, p.y - lXoffset, crColour);
crColour = GetColour(-Pi_on_2 + dAngle, crBright, crDark);
// pDC->SetPixelV(p.x + lYoffset, p.y - lXoffset, crColour);
crColour = GetColour(-dAngle, crBright, crDark);
pDC->SetPixelV(p.x + lXoffset, p.y - lYoffset, crColour);
lXoffset--;
t8 = t8 - t6;
if (d2 < 0)
{
lYoffset ++; t9 = t9 + t3; d2 = d2 - t8 + t5 + t9;
}
else
d2 = d2 - t8 + t5;
} while (lXoffset >=0);
}
/////////////////////////////////////////////////////////////////////////////
// CEllepticalButton
CEllepticalButton::CEllepticalButton()
{
m_bDrawDashedFocusCircle = TRUE;
}
CEllepticalButton::~CEllepticalButton()
{
m_rgn.DeleteObject();
}
BEGIN_MESSAGE_MAP(CEllepticalButton, CButton)
//{{AFX_MSG_MAP(CEllepticalButton)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEllepticalButton message handlers
void CEllepticalButton::PreSubclassWindow()
{
CButton::PreSubclassWindow();
m_bDefault=GetStyle() & (DWORD)BS_DEFPUSHBUTTON; //this is necessary as
// the default button style gets removed when u make it ownerdrawn
ModifyStyle(0, BS_OWNERDRAW);
CRect rect;
GetClientRect(rect);
// Resize the window to make it square
//commented by Raghav
//rect.bottom = rect.right = min(rect.bottom,rect.right);
// Get the vital statistics of the window
m_ptCentre = rect.CenterPoint();
m_nHorizontalRadius = rect.right/2-1;
m_nVerticalRadius = rect.bottom/2-1;
// Set the window region so mouse clicks only activate the round section
// of the button
m_rgn.DeleteObject();
SetWindowRgn(NULL, FALSE);
m_rgn.CreateEllipticRgnIndirect(rect);
SetWindowRgn(m_rgn, TRUE);
// Convert client coords to the parents client coords
ClientToScreen(rect);
CWnd* pParent = GetParent();
if (pParent) pParent->ScreenToClient(rect);
// Resize the window
MoveWindow(rect.left, rect.top, rect.Width(), rect.Height(), TRUE);
}
void CEllepticalButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
ASSERT(lpDrawItemStruct != NULL);
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rect = lpDrawItemStruct->rcItem;
UINT state = lpDrawItemStruct->itemState;
UINT nStyle = GetStyle();
int nHorizontalRadius = m_nHorizontalRadius;
int nVerticalRadius = m_nVerticalRadius;
int nSavedDC = pDC->SaveDC();
pDC->SelectStockObject(NULL_BRUSH);
pDC->FillSolidRect(rect, ::GetSysColor(COLOR_BTNFACE));
// Draw the focus circle around the button either if it has the focus
//or if it is the default button
if ((state & ODS_FOCUS || m_bDefault) && m_bDrawDashedFocusCircle)
DrawEllipse(pDC, m_ptCentre, nHorizontalRadius--, nVerticalRadius--, RGB(0,0,0));
// Draw the raised/sunken edges of the button (unless flat)
if (nStyle & BS_FLAT) {
DrawEllipse(pDC, m_ptCentre, nHorizontalRadius--, nVerticalRadius--, RGB(0,0,0));
DrawEllipse(pDC, m_ptCentre, nHorizontalRadius--, nVerticalRadius--, ::GetSysColor(COLOR_3DHIGHLIGHT));
} else {
if ((state & ODS_SELECTED)) {
DrawEllipse(pDC, m_ptCentre, nHorizontalRadius--, nVerticalRadius--,
::GetSysColor(COLOR_3DDKSHADOW), ::GetSysColor(COLOR_3DHIGHLIGHT));
DrawEllipse(pDC, m_ptCentre, nHorizontalRadius--, nVerticalRadius--,
::GetSysColor(COLOR_3DSHADOW), ::GetSysColor(COLOR_3DLIGHT));
} else {
DrawEllipse(pDC, m_ptCentre, nHorizontalRadius--, nVerticalRadius--,
::GetSysColor(COLOR_3DHIGHLIGHT), ::GetSysColor(COLOR_3DDKSHADOW));
//RGB(255,255,255), ::GetSysColor(COLOR_3DDKSHADOW));
DrawEllipse(pDC, m_ptCentre, nHorizontalRadius--, nVerticalRadius--,
::GetSysColor(COLOR_3DHIGHLIGHT), ::GetSysColor(COLOR_3DSHADOW));
}
}
// draw the text if there is any
CString strText;
GetWindowText(strText);
if (!strText.IsEmpty())
{
CRgn rgn;
rgn.CreateEllipticRgn(m_ptCentre.x-nHorizontalRadius, m_ptCentre.y-nVerticalRadius,
m_ptCentre.x+nHorizontalRadius, m_ptCentre.y+nVerticalRadius);
pDC->SelectClipRgn(&rgn);
CSize Extent = pDC->GetTextExtent(strText);
CPoint pt = CPoint( m_ptCentre.x - Extent.cx/2, m_ptCentre.y - Extent.cy/2 );
if (state & ODS_SELECTED) pt.Offset(1,1);
pDC->SetBkMode(TRANSPARENT);
if (state & ODS_DISABLED)
pDC->DrawState(pt, Extent, strText, DSS_DISABLED, TRUE, 0, (HBRUSH)NULL);
else
pDC->TextOut(pt.x, pt.y, strText);
pDC->SelectClipRgn(NULL);
rgn.DeleteObject();
}
// Draw the focus circle on the inside of the button
if ((state & ODS_FOCUS) && m_bDrawDashedFocusCircle)
DrawEllipse(pDC, m_ptCentre, nHorizontalRadius-2, nVerticalRadius-2, RGB(0,0,0), TRUE);
pDC->RestoreDC(nSavedDC);
}