Menu

[r2035]: / trunk / Src / UCtrlArranger.pas  Maximize  Restore  History

Download this file

384 lines (324 with data), 12.6 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
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
{
* UCtrlArranger.pas
*
* Implements a static class that provides methods to assist in aligning a the
* controls in forms, frames etc.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is UCtrlArranger.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2009-2012 Peter
* Johnson. All Rights Reserved.
*
* Contributors:
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UCtrlArranger;
interface
uses
// Delphi
Controls, StdCtrls,
// Projects
UBaseObjects;
type
/// <summary>Static class used to arrange and size controls in forms, frames
/// etc.</summary>
TCtrlArranger = class(TNoConstructObject)
public
/// <summary>Sets height of given label to accommodate its caption text in
/// its current font. The new height is returned.</summary>
class function SetLabelHeight(const Lbl: TLabel): Integer;
/// <summary>Sets heights of all labels owned by given container to
/// accomodate their captions in their current fonts, unless label is
/// auto-sized and IgnoreAutoSize flag is set.</summary>
class procedure SetLabelHeights(const Container: TControl;
const IgnoreAutoSize: Boolean = False);
/// <summary>Returns Y co-ordinate of bottom of given control, allowing
/// space for optional margin.</summary>
class function BottomOf(const Ctrl: TControl; const Margin: Integer = 0):
Integer; overload;
/// <summary>Returns Y co-ordinate of bottom of lower-most control in given
/// array, allowing space for optional margin.</summary>
class function BottomOf(const Ctrls: array of TControl;
const Margin: Integer = 0): Integer; overload;
/// <summary>Returns X co-ordinate of right hand side of given control,
/// allowing space for optional margin.</summary>
class function RightOf(const Ctrl: TControl; const Margin: Integer = 0):
Integer; overload;
/// <summary>Returns X co-ordinate of right-most right hand side of
/// controls in given array, allowing space for optional margin.</summary>
class function RightOf(const Ctrls: array of TControl;
const Margin: Integer = 0): Integer; overload;
/// <summary>Locates control Ctrl to the left of reference control RefCtrl,
/// optionally separated by given margin.</summary>
class procedure MoveToLeftOf(const RefCtrl, Ctrl: TControl;
const Margin: Integer = 0);
/// <summary>Locates control Ctrl to right of reference control RefCtrl,
/// optionally separated by given margin.</summary>
class procedure MoveToRightOf(const RefCtrl, Ctrl: TControl;
const Margin: Integer = 0); overload;
/// <summary>Locates control Ctrl below reference control RefCtrl,
/// optionally separated by given margin.</summary>
class procedure MoveBelow(const RefCtrl, Ctrl: TControl;
const Margin: Integer = 0); overload;
/// <summary>Locates control Ctrl below bottom-most of reference controls
/// in RefCtrls, optionally separated by given margin.</summary>
class procedure MoveBelow(const RefCtrls: array of TControl;
const Ctrl: TControl; const Margin: Integer = 0); overload;
/// <summary>Vertically centres all controls in Ctrls array aligned so that
/// top of top-most control is located at ATop. Returns total height
/// occupied by controls.</summary>
class function AlignVCentres(const ATop: Integer;
const Ctrls: array of TControl): Integer;
/// <summary>Aligns tops of all controls in Ctrls array with top of first
/// control in array and returns that top position.</summary>
/// <remarks>Array of controls must not be empty.</remarks>
class function AlignTops(const Ctrls: array of TControl): Integer; overload;
/// <summary>Aligns tops of all controls in Ctrls array at position given
/// by ATop.</summary>
class procedure AlignTops(const Ctrls: array of TControl;
const ATop: Integer); overload;
/// <summary>Aligns bottoms of all controls in Ctrls array at position
/// given by ABottom.</summary>
class procedure AlignBottoms(const Ctrls: array of TControl;
const ABottom: Integer); overload;
/// <summary>Aligns bottoms of all controls in Ctrls array with bottom of
/// first control in array and returns that bottom position.</summary>
/// <remarks>Array of controls must not be empty.</remarks>
class function AlignBottoms(const Ctrls: array of TControl): Integer;
overload;
/// <summary>Aligns left hand sides of all controls in Ctrls array at
/// position given by ALeft.</summary>
class procedure AlignLefts(const Ctrls: array of TControl;
const ALeft: Integer); overload;
/// <summary>Aligns left hand sides of all controls in Ctrls array with
/// left of first control in array and returns that left position.
/// </summary>
/// <remarks>Array of controls must not be empty.</remarks>
class function AlignLefts(const Ctrls: array of TControl): Integer;
overload;
/// <summary>Aligns right hand sides of all controls in Ctrls array at
/// position given by ARight.</summary>
class procedure AlignRights(const Ctrls: array of TControl;
const ARight: Integer); overload;
/// <summary>Aligns right hand sides of all controls in Ctrls array with
/// right of first control in array and returns that right position.
/// </summary>
/// <remarks>Array of controls must not be empty.</remarks>
class function AlignRights(const Ctrls: array of TControl): Integer;
overload;
/// <summary>Returns the total height needed to display all the controls
/// parented by given container.</summary>
class function TotalControlHeight(const Container: TWinControl): Integer;
/// <summary>Returns the total width needed to display all the controls
/// parented by given container.</summary>
class function TotalControlWidth(const Container: TWinControl): Integer;
/// <summary>Returns maximum height of controls parented by given set of
/// parent controls.</summary>
/// <remarks>Designed for use in determining the height of a control that
/// has to accomodate all the controls from any of the containers, for
/// example a page control where the containers are the pages.</remarks>
class function MaxContainerHeight(const Containers: array of TWinControl):
Integer;
end;
implementation
uses
// Delphi
Math,
// Project
UGraphicUtils;
{ TCtrlArranger }
class function TCtrlArranger.AlignBottoms(const Ctrls: array of TControl):
Integer;
begin
Assert(Length(Ctrls) > 0, ClassName + '.AlignBottoms: control array empty');
Result := Ctrls[0].Top + Ctrls[0].Height;
AlignBottoms(Ctrls, Result);
end;
class procedure TCtrlArranger.AlignBottoms(const Ctrls: array of TControl;
const ABottom: Integer);
var
Ctrl: TControl; // each control in Ctrls
begin
for Ctrl in Ctrls do
Ctrl.Top := ABottom - Ctrl.Height;
end;
class function TCtrlArranger.AlignLefts(const Ctrls: array of TControl):
Integer;
begin
Assert(Length(Ctrls) > 0, ClassName + '.AlignLefts: control array empty');
Result := Ctrls[0].Left;
AlignLefts(Ctrls, Result);
end;
class procedure TCtrlArranger.AlignLefts(const Ctrls: array of TControl;
const ALeft: Integer);
var
Ctrl: TControl; // each control in Ctrls
begin
for Ctrl in Ctrls do
Ctrl.Left := ALeft;
end;
class function TCtrlArranger.AlignRights(const Ctrls: array of TControl):
Integer;
begin
Assert(Length(Ctrls) > 0, ClassName + '.AlignRights: control array empty');
Result := Ctrls[0].Left + Ctrls[0].Width;
AlignRights(Ctrls, Result);
end;
class procedure TCtrlArranger.AlignRights(const Ctrls: array of TControl;
const ARight: Integer);
var
Ctrl: TControl; // each control in Ctrls
begin
for Ctrl in Ctrls do
Ctrl.Left := ARight - Ctrl.Width;
end;
class function TCtrlArranger.AlignTops(const Ctrls: array of TControl): Integer;
begin
Assert(Length(Ctrls) > 0, ClassName + '.AlignTops: control array empty');
Result := Ctrls[0].Top;
AlignTops(Ctrls, Result);
end;
class procedure TCtrlArranger.AlignTops(const Ctrls: array of TControl;
const ATop: Integer);
var
Ctrl: TControl; // each control in Ctrls
begin
for Ctrl in Ctrls do
Ctrl.Top := ATop;
end;
class function TCtrlArranger.AlignVCentres(const ATop: Integer;
const Ctrls: array of TControl): Integer;
var
Ctrl: TControl; // each control in Ctrls
begin
Result := 0;
for Ctrl in Ctrls do
Result := Max(Result, Ctrl.Height);
for Ctrl in Ctrls do
Ctrl.Top := ATop + (Result - Ctrl.Height) div 2;
end;
class function TCtrlArranger.BottomOf(const Ctrl: TControl;
const Margin: Integer): Integer;
begin
Result := Ctrl.Top + Ctrl.Height + Margin;
end;
class function TCtrlArranger.BottomOf(const Ctrls: array of TControl;
const Margin: Integer): Integer;
var
Ctrl: TControl; // each control in Ctrls
begin
Result := 0;
for Ctrl in Ctrls do
Result := Max(Result, BottomOf(Ctrl));
Inc(Result, Margin);
end;
class function TCtrlArranger.MaxContainerHeight(
const Containers: array of TWinControl): Integer;
var
Container: TWinControl; // each container in Containers
begin
Result := 0;
for Container in Containers do
Result := Max(Result, TotalControlHeight(Container));
end;
class procedure TCtrlArranger.MoveBelow(const RefCtrl, Ctrl: TControl;
const Margin: Integer);
begin
Ctrl.Top := BottomOf(RefCtrl, Margin);
end;
class procedure TCtrlArranger.MoveBelow(const RefCtrls: array of TControl;
const Ctrl: TControl; const Margin: Integer);
begin
Ctrl.Top := BottomOf(RefCtrls, Margin);
end;
class procedure TCtrlArranger.MoveToLeftOf(const RefCtrl, Ctrl: TControl;
const Margin: Integer);
begin
Ctrl.Left := RefCtrl.Left - Margin - Ctrl.Width;
end;
class procedure TCtrlArranger.MoveToRightOf(const RefCtrl, Ctrl: TControl;
const Margin: Integer);
begin
Ctrl.Left := RightOf(RefCtrl) + Margin;
end;
class function TCtrlArranger.RightOf(const Ctrls: array of TControl;
const Margin: Integer = 0): Integer;
var
Ctrl: TControl; // each control in Ctrls
begin
Result := 0;
for Ctrl in Ctrls do
Result := Max(Result, RightOf(Ctrl));
Inc(Result, Margin);
end;
class function TCtrlArranger.RightOf(const Ctrl: TControl;
const Margin: Integer = 0): Integer;
begin
Result := Ctrl.Left + Ctrl.Width + Margin;
end;
class function TCtrlArranger.SetLabelHeight(const Lbl: TLabel): Integer;
begin
Lbl.Height := StringExtent(Lbl.Caption, Lbl.Font, Lbl.Width).cy;
Result := Lbl.Height;
end;
class procedure TCtrlArranger.SetLabelHeights(const Container: TControl;
const IgnoreAutoSize: Boolean);
var
Idx: Integer; // loops through all components owned by container control
Lbl: TLabel; // references each label component
begin
for Idx := 0 to Pred(Container.ComponentCount) do
begin
if Container.Components[Idx] is TLabel then
begin
Lbl := Container.Components[Idx] as TLabel;
if IgnoreAutoSize or not Lbl.AutoSize then
SetLabelHeight(Lbl);
end;
end;
end;
class function TCtrlArranger.TotalControlHeight(const Container: TWinControl):
Integer;
var
CtrlIdx: Integer; // loops through all controls in Container
Ctrls: array of TControl; // array of controls contained in each container
begin
SetLength(Ctrls, Container.ControlCount);
for CtrlIdx := 0 to Pred(Container.ControlCount) do
Ctrls[CtrlIdx + Low(Ctrls)] := Container.Controls[CtrlIdx];
Result := BottomOf(Ctrls);
end;
class function TCtrlArranger.TotalControlWidth(
const Container: TWinControl): Integer;
var
CtrlIdx: Integer; // loops through all controls in Container
Ctrl: TControl; // references each control in Container
begin
Result := 0;
for CtrlIdx := 0 to Pred(Container.ControlCount) do
begin
Ctrl := Container.Controls[CtrlIdx];
Result := Max(Result, Ctrl.Left + Ctrl.Width);
end;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.