-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWinRect.cpp
More file actions
277 lines (213 loc) · 7.96 KB
/
WinRect.cpp
File metadata and controls
277 lines (213 loc) · 7.96 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
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
/*******************************************************************
// Copyright (c) 2002, Robert Umbehant
// mailto:rumbehant@wheresjames.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later
// version.
//
// This library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
// MA 02111-1307 USA
//
*******************************************************************/
// WinRect.cpp: implementation of the CWinRect class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CWinRect::CWinRect()
{_STT();
}
CWinRect::~CWinRect()
{_STT();
}
BOOL CWinRect::AddRectToList(LPRECT pRect, LPRECT pRectList, DWORD dwRectListSize, LONG xThreshold, LONG yThreshold )
{_STT();
// Add if possible
RECT dst;
DWORD i = 0, blank = MAXDWORD;
for ( i = 0; i < dwRectListSize; i++ )
{
// If valid area
if ( RW( pRectList[ i ] ) != 0 && RH( pRectList[ i ] ) != 0 )
{ if ( IsAdjacentRect( &pRectList[ i ], pRect, xThreshold, yThreshold ) )
if ( UnionRect( &dst, &pRectList[ i ], pRect ) )
{ CopyRect( &pRectList[ i ], &dst ); return TRUE; }
} // end if
// Save blank location
else if ( blank == MAXDWORD )
blank = i;
} // end for
// Any more blank slots?
if ( blank == MAXDWORD )
{
// Attempt to create an empty slot
if ( !CombineOverlapping( pRectList, dwRectListSize, xThreshold, yThreshold, 1, &blank ) )
return FALSE;
} // end if
// Add to slot
CopyRect( &pRectList[ blank ], pRect );
return TRUE;
}
BOOL CWinRect::IsAdjacentRect(LPRECT pRect1, LPRECT pRect2, LONG xThreshold, LONG yThreshold)
{_STT();
// Sanity check
if ( pRect1 == NULL || pRect2 == NULL )
return FALSE;
// Rect to the left?
if ( pRect1->right < ( pRect2->left - xThreshold ) )
return FALSE;
// Rect to the right?
if ( pRect1->left > ( pRect2->right + xThreshold ) )
return FALSE;
// Rect above
if ( pRect1->bottom < ( pRect2->top - yThreshold ) )
return FALSE;
// Rect below
if ( pRect1->top > ( pRect2->bottom + yThreshold ) )
return FALSE;
return TRUE;
}
DWORD CWinRect::CombineOverlapping(LPRECT pRectList, DWORD dwRectListSize, LONG xThreshold, LONG yThreshold, DWORD max, LPDWORD pdwFree)
{_STT();
// Combine overlapping rects
BOOL dwCombined = 0;
for ( DWORD i = 0; i < dwRectListSize; i++ )
for ( DWORD c = i + 1; c < dwRectListSize; c++ )
{ RECT dst;
if ( ( RW( pRectList[ i ] ) != 0 && RH( pRectList[ i ] ) != 0 ) &&
( RW( pRectList[ c ] ) != 0 && RH( pRectList[ c ] ) != 0 ) &&
IsAdjacentRect( &pRectList[ i ], &pRectList[ c ] ) )
if ( UnionRect( &dst, &pRectList[ i ], &pRectList[ c ] ) )
{ dwCombined++;
// Copy the new rect
CopyRect( &pRectList[ i ], &dst );
// Lose the old rect
ZeroMemory( &pRectList[ c ], sizeof( pRectList[ c ] ) );
// Save pointer to blank if needed
if ( dwCombined == 1 && pdwFree != NULL ) *pdwFree = c;
// Punt if user doesn't want any more
if ( !max ) return dwCombined;
i = 0; c = 1; max--;
} // end if
} // end for
return dwCombined;
}
BOOL CWinRect::CalculateIdealLayout( SPanelLayout *pSl, LPRECT pRect, long lNumPanels )
{_STT();
// Sanity check
if ( !pSl || !pRect || 0 >= lNumPanels ||
PRW( pRect ) <= 0 || PRH( pRect ) <= 0 )
return FALSE;
// Calculate aspect ratio
double dAspectRatio = (double)PRH( pRect ) / (double)PRW( pRect );
RECT rect;
SetRect( &rect, 0, 0, 10000, 10000 );
CGrDC::AspectCorrect( &rect, pRect );
// Get a layout that works
CalculateLayout( pSl, &rect, lNumPanels, dAspectRatio );
// Now adjust to a size we would want
pSl->lPanelWidth = PRW( pRect );
pSl->lPanelHeight = PRH( pRect );
// In the ideal case, there is no margin
pSl->lxMargin = 0;
pSl->lyMargin = 0;
// Set minimal bounding rectangle
SetRect( pRect, 0, 0, pSl->lxPanels * pSl->lPanelWidth,
pSl->lyPanels * pSl->lPanelHeight );
return TRUE;
}
BOOL CWinRect::CalculateLayout( SPanelLayout *pSl, LPRECT pRect, long lNumPanels, double dPanelAspectRatio )
{_STT();
// Sanity check
if ( !pSl || !pRect || 0 >= lNumPanels ) return FALSE;
// Ensure valid aspect ratio
if ( dPanelAspectRatio == 0 ) dPanelAspectRatio = (double)3 / (double)4;
// Ensure valid number of panels
pSl->lNumPanels = lNumPanels;
pSl->dPanelAspectRatio = dPanelAspectRatio;
// Initialize
pSl->lxPanels = 1;
pSl->lyPanels = 1;
// Get width / height
long w = PRW( pRect );
long h = PRH( pRect );
if ( w == 0 || h == 0 ) return FALSE;
// Ideal ratio (4:3)
double delta = w * h;
for ( long rows = 1; rows <= pSl->lNumPanels; rows++ )
{
long cols = pSl->lNumPanels / rows;
if ( cols < 1 ) cols = 1;
// Verify this is enough rows
while ( ( cols * rows ) < pSl->lNumPanels ) cols++;
// Track the closest ratio
double cr = ( (double)h / (double)rows ) / ( (double)w / (double)cols );
double dl = ( cr > dPanelAspectRatio ) ? cr - dPanelAspectRatio : dPanelAspectRatio - cr;
if ( dl < delta ) { delta = dl; pSl->lxPanels = cols; pSl->lyPanels = rows; }
} // end for
// Ensure proper panel size
while ( pSl->lyPanels && pSl->lxPanels && ( pSl->lyPanels - 1 ) * pSl->lxPanels >= pSl->lNumPanels ) pSl->lyPanels--;
while ( pSl->lyPanels && pSl->lxPanels && ( pSl->lxPanels - 1 ) * pSl->lyPanels >= pSl->lNumPanels ) pSl->lxPanels--;
// Must be larger than one
if ( pSl->lxPanels < 1 ) pSl->lxPanels = 1;
if ( pSl->lyPanels < 1 ) pSl->lyPanels = 1;
// Get panel metrics
pSl->lPanelWidth = w / pSl->lxPanels;
pSl->lPanelHeight = h / pSl->lyPanels;
// Calculate margin
pSl->lxMargin = ( w - ( pSl->lPanelWidth * pSl->lxPanels ) ) / 2;
pSl->lyMargin = ( h - ( pSl->lPanelHeight * pSl->lyPanels ) ) / 2;
return TRUE;
}
BOOL CWinRect::GetPanelPosition( SPanelLayout *pSl, long lView, LPRECT pRect )
{_STT();
// Sanity checks
if ( !pSl || !pRect || lView < 0 || lView >= pSl->lNumPanels )
return FALSE;
// Must be larger than one
if ( pSl->lxPanels < 1 ) pSl->lxPanels = 1;
if ( pSl->lyPanels < 1 ) pSl->lyPanels = 1;
try
{
// Calculate box position and size
long x = pSl->lxMargin + ( ( lView % pSl->lxPanels ) * pSl->lPanelWidth );
long y = pSl->lyMargin + ( ( lView / pSl->lxPanels ) * pSl->lPanelHeight );
// Set the position
SetRect( pRect, x, y, x + pSl->lPanelWidth, y + pSl->lPanelHeight );
}
catch( ... ) { ASSERT( 0 ); return FALSE;}
return TRUE;
}
long CWinRect::HitTest( SPanelLayout *pSl, LPPOINT pPt )
{_STT();
// Sanity checks
if ( !pSl || !pPt ) return -1;
try
{
// Verify that the click lies within our arena
if ( pPt->x < pSl->lxMargin || pPt->y < pSl->lyMargin ||
pPt->x > ( pSl->lxMargin + ( pSl->lxPanels * pSl->lPanelWidth ) ) ||
pPt->y > ( pSl->lyMargin + ( pSl->lyPanels * pSl->lPanelHeight ) ) )
return -1;
// Calculate the view this point falls in
long i = ( ( ( pPt->y - pSl->lyMargin ) / pSl->lPanelHeight ) * pSl->lxPanels ) +
( ( pPt->x - pSl->lxMargin ) / pSl->lPanelWidth );
// Is our calculation valid?
if ( i < 0 || i >= pSl->lNumPanels ) return i;
return i;
} // end try
catch( ... ) { ASSERT( 0 ); }
return -1;
}