Menu

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

Download this file

323 lines (294 with data), 11.4 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
{
* UCtrlArranger.pas
*
* Implements a static class that provides methods to assist in aligning a the
* controls on a form or frame 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-2011 Peter
* Johnson. All Rights Reserved.
*
* Contributors:
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UCtrlArranger;
interface
uses
// Delphi
Controls, StdCtrls,
// Projects
UBaseObjects;
type
{
TCtrlArranger:
Static class used to arrange and size controls in forms and frames etc.
}
TCtrlArranger = class(TNoConstructObject)
public
class function SetLabelHeight(const Lbl: TLabel): Integer;
{Sets height of a label to accommodate the text it contains in its font.
@param Lbl [in] Label whose height is to be set.
@return New label height.
}
class procedure SetLabelHeights(const Container: TControl;
const IgnoreAutoSize: Boolean = False);
{Sets heights of labels owned by a container control such as a form to
accommodate the text they contain in the appropriate font.
@param Container [in] Control that owns the labels.
@param IgnoreAutoSize [in] Flagthat determines if labels with AutoSize
property set to true are sized. When False only labels with AutoSize
true are sized, otherwise all labels are sized.
}
class function BottomOf(const Ctrl: TControl; const BM: Integer = 0):
Integer; overload;
{Gets Y coordinate of bottom of a control, leaving space for an optional
margin under the control, relative to its parent controls in pixels.
@param Ctl [in] Control to check.
@param BM [in] Bottom margin to leave under control. Optional.
@return Required position.
}
class function BottomOf(const Ctrls: array of TControl;
const BM: Integer = 0): Integer; overload;
{Gets Y co-ordinate of bottom of the lowermost of a set of controls,
leaving space for an optional margin under the controls.
@param Ctrls [in] Controls to be examined.
@param BM [in] Bottom margin to leave under all controls. Optional.
@return Required position. This is maximum value of bottom of all
controls.
}
class function RightOf(const Ctrl: TControl): Integer;
{Returns horizontal position of right hand edge of given control.
@param Ctrl [in] Control for which location of right hand side required.
@return Required position.
}
class procedure MoveToLeftOf(const RefCtrl, Ctrl: TControl;
const Margin: Integer = 0);
{Moves a control to the left of a reference control optionally separated
by a margin.
@param RefCtrl [in] Control relative to which control is moved.
@param Ctrl [in] Control being moved.
@param Margin [in] Optional margin, in pixels, required between Ctrl and
RefCtrl.
}
class procedure MoveToRightOf(const RefCtrl, Ctrl: TControl;
const Margin: Integer = 0);
{Moves a control to the right of a reference control optionally separated
by a margin.
@param RefCtrl [in] Control relative to which control is moved.
@param Ctrl [in] Control being moved.
@param Margin [in] Optional margin, in pixels, required between Ctrl and
RefCtrl.
}
class function AlignVCentres(const ATop: Integer;
const Ctrls: array of TControl): Integer;
{Vertically centres a list of controls.
@param ATop [in] Top of tallest control to be aligned.
@param Ctrls [in] Array of controls to be aligned.
@return Height occupied by controls (= height of tallest control).
}
class function MaxContainerHeight(const Containers: array of TWinControl):
Integer;
{Checks the maximum height of controls parented by a set of controls. Can
be used to determine the height of a control that has to be able to
display all the controls from any of the containers.
@param Containers [in] Controls that parent the controls being measured.
@return Required height.
}
class function TotalControlHeight(const Container: TWinControl): Integer;
{Gets the height that a container needs to be to accommodate all its
contained controls.
@param Container [in] Container to be checked.
@return Required height.
}
class function TotalControlWidth(const Container: TWinControl): Integer;
{Gets the width that a container needs to be to accommodate all its
contained controls.
@param Container [in] Container to be checked.
@return Required width.
}
end;
implementation
uses
// Delphi
Math,
// Project
UGraphicUtils;
{ TCtrlArranger }
class function TCtrlArranger.AlignVCentres(const ATop: Integer;
const Ctrls: array of TControl): Integer;
{Vertically centres a list of controls.
@param ATop [in] Top of tallest control to be aligned.
@param Ctrls [in] Array of controls to be aligned.
@return Height occupied by controls (= height of tallest control).
}
var
Ctrl: TControl; // references each control in Ctrls array
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 BM: Integer): Integer;
{Gets Y coordinate of bottom of a control, leaving space for an optional
margin unser the control, relative to its parent controls in pixels.
@param Ctl [in] Control to check.
@param BM [in] Bottom margin to leave under control. Optional.
@return Required position.
}
begin
Result := Ctrl.Top + Ctrl.Height + BM;
end;
class function TCtrlArranger.BottomOf(const Ctrls: array of TControl;
const BM: Integer): Integer;
{Gets Y co-ordinate of bottom of the lowermost of a set of controls, leaving
space for an optional margin under the controls.
@param Ctrls [in] Controls to be examined.
@param BM [in] Bottom margin to leave under all controls. Optional.
@return Required position. This is maximum value of bottom of all controls.
}
var
Ctrl: TControl; // references each control in Ctrls array
begin
Result := 0;
for Ctrl in Ctrls do
Result := Max(Result, BottomOf(Ctrl));
Inc(Result, BM);
end;
class function TCtrlArranger.MaxContainerHeight(
const Containers: array of TWinControl): Integer;
{Checks the maximum height of controls parented by a set of controls. Can be
used to determine the height of a control that has to be able to display all
the controls from any of the containers.
@param Containers [in] Controls that parent the controls being measured.
@return Required height.
}
var
Container: TWinControl; // references each container in Containers array
begin
Result := 0;
for Container in Containers do
Result := Max(Result, TotalControlHeight(Container));
end;
class procedure TCtrlArranger.MoveToLeftOf(const RefCtrl, Ctrl: TControl;
const Margin: Integer);
{Moves a control to the left of a reference control optionally separated by a
margin.
@param RefCtrl [in] Control relative to which control is moved.
@param Ctrl [in] Control being moved.
@param Margin [in] Optional margin, in pixels, required between Ctrl and
RefCtrl.
}
begin
Ctrl.Left := RefCtrl.Left - Margin - Ctrl.Width;
end;
class procedure TCtrlArranger.MoveToRightOf(const RefCtrl, Ctrl: TControl;
const Margin: Integer);
{Moves a control to the right of a reference control optionally separated by a
margin.
@param RefCtrl [in] Control relative to which control is moved.
@param Ctrl [in] Control being moved.
@param Margin [in] Optional margin, in pixels, required between Ctrl and
RefCtrl.
}
begin
Ctrl.Left := RightOf(RefCtrl) + Margin;
end;
class function TCtrlArranger.RightOf(const Ctrl: TControl): Integer;
{Returns horizontal position of right hand edge of given control.
@param Ctrl [in] Control for which location of right hand side required.
@return Required position.
}
begin
Result := Ctrl.Left + Ctrl.Width;
end;
class function TCtrlArranger.SetLabelHeight(const Lbl: TLabel): Integer;
{Sets height of a label to accommodate the text it contains in its font.
@param Lbl [in] Label whose height is to be set.
@return New label height.
}
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);
{Sets heights of labels owned by a container control such as a form to
accommodate the text they contain in the appropriate font.
@param Container [in] Control that owns the labels.
@param IgnoreAutoSize [in] Flagthat determines if labels with AutoSize
property set to true are sized. When False only labels with AutoSize true
are sized, otherwise all labels are sized.
}
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;
{Gets the height that a container needs to be to accommodate all its contained
controls.
@param Container [in] Container to be checked.
@return Required height.
}
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;
{Gets the width that a container needs to be to accommodate all its contained
controls.
@param Container [in] Container to be checked.
@return Required width.
}
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.