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
|
{%MainUnit ../controls.pp}
{******************************************************************************
TSizeConstraints
Simple class to hold size constraints for a control.
******************************************************************************
*****************************************************************************
This file is part of the Lazarus Component Library (LCL)
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
}
{------------------------------------------------------------------------------
Method: TSizeConstraints.Create
Params: AControl: the owner of the class
Returns: Nothing
Constructor for the class.
------------------------------------------------------------------------------}
constructor TSizeConstraints.Create(AControl : TControl);
begin
inherited Create;
FControl:= AControl;
FMaxWidth:= 0;
FMaxHeight:= 0;
FMinWidth:= 0;
FMinHeight:= 0;
UpdateInterfaceConstraints;
end;
{------------------------------------------------------------------------------
procedure TSizeConstraints.UpdateInterfaceConstraints;
Asks interface for constraints.
------------------------------------------------------------------------------}
procedure TSizeConstraints.UpdateInterfaceConstraints;
begin
if (Control is TWinControl) and TWinControl(Control).HandleAllocated then
TWSControlClass(Control.WidgetSetClass).GetConstraints(Control, Self);
end;
{------------------------------------------------------------------------------
procedure TSizeConstraints.SetInterfaceConstraints(MinW, MinH,
MaxW, MaxH: integer);
Used by the interface to set the interface constraints.
Should only be used by custom components, not by applications.
------------------------------------------------------------------------------}
procedure TSizeConstraints.SetInterfaceConstraints(MinW, MinH,
MaxW, MaxH: integer);
begin
if (FMinInterfaceWidth=MinW)
and (FMinInterfaceHeight=MinH)
and (FMaxInterfaceWidth=MaxW)
and (FMaxInterfaceHeight=MaxH) then exit;
FMinInterfaceWidth:=MinW;
FMinInterfaceHeight:=MinH;
FMaxInterfaceWidth:=MaxW;
FMaxInterfaceHeight:=MaxH;
if (FControl.Width<>MinMaxWidth(FControl.Width))
or (FControl.Height<>MinMaxHeight(FControl.Height)) then
FControl.RequestAlign;
end;
function TSizeConstraints.EffectiveMinWidth: integer;
begin
if csLoading in Control.ComponentState then
exit(0);
if (MinWidth>MinInterfaceWidth) then begin
Result:=MinWidth;
if (MaxInterfaceWidth>0) and (MaxInterfaceWidth<MinWidth) then
Result:=MaxInterfaceWidth;
end else
Result:=MinInterfaceWidth;
end;
function TSizeConstraints.EffectiveMinHeight: integer;
begin
if csLoading in Control.ComponentState then
exit(0);
if (MinHeight>MinInterfaceHeight) then begin
Result:=MinHeight;
if (MaxInterfaceHeight>0) and (Result>MaxInterfaceHeight) then
Result:=MaxInterfaceHeight;
end else
Result:=MinInterfaceHeight;
end;
// The EffectiveMaxWidth is the minumum of MaxWidth and MaxInterfaceWidth,
// but it is at least MinInterfaceWidth.
// A zero value is interpreted as unconstraint.
function TSizeConstraints.EffectiveMaxWidth: integer;
begin
if csLoading in Control.ComponentState then
exit(0);
if (MaxInterfaceWidth>0) and
((MaxWidth=0) or (MaxInterfaceWidth<MaxWidth)) then
Result := MaxInterfaceWidth
else
Result:=MaxWidth;
if (Result>0) and (MinInterfaceWidth>0) and (Result<MinInterfaceWidth) then
Result:=MinInterfaceWidth;
end;
// The EffectiveMaxHeight is the minumum of MaxHeight and MaxInterfaceHeight,
// but it is at least the MinInterfaceHeight.
// A zero value is interpreted as unconstraint.
function TSizeConstraints.EffectiveMaxHeight: integer;
begin
if csLoading in Control.ComponentState then
exit(0);
if (MaxInterfaceHeight>0) and
((MaxHeight=0) or (MaxInterfaceHeight<MaxHeight)) then
Result := MaxInterfaceHeight
else
Result:=MaxHeight;
if (Result>0) and (MinInterfaceHeight>0) and (Result<MinInterfaceHeight) then
Result:=MinInterfaceHeight;
end;
function TSizeConstraints.MinMaxWidth(Width: integer): integer;
var
MinW: LongInt;
MaxW: LongInt;
begin
Result:=Width;
MinW:=EffectiveMinWidth;
if (Result<MinW) then Result:=MinW;
MaxW:=EffectiveMaxWidth;
if (MaxW>0) and (Result>MaxW) then Result:=MaxW;
if (Control is TWinControl) and TWinControl(Control).HandleAllocated then
TWSControlClass(Control.WidgetSetClass).ConstraintWidth(Control, Self, Result);
end;
function TSizeConstraints.MinMaxHeight(Height: integer): integer;
var
MinH: LongInt;
MaxH: LongInt;
begin
Result:=Height;
MinH:=EffectiveMinHeight;
if (Result<MinH) then Result:=MinH;
MaxH:=EffectiveMaxHeight;
if (MaxH>0) and (Result>MaxH) then Result:=MaxH;
if (Control is TWinControl) and TWinControl(Control).HandleAllocated then
TWSControlClass(Control.WidgetSetClass).ConstraintHeight(Control, Self, Result);
end;
{------------------------------------------------------------------------------
Method: TSizeConstraints.SetMaxWidth
Params: Value: the new value of the property
Returns: Nothing
Sets a new value of its property.
------------------------------------------------------------------------------}
procedure TSizeConstraints.SetMaxWidth(Value: TConstraintSize);
begin
if Value <> FMaxWidth then begin
FMaxWidth:= Value;
if (FMinWidth > 0) and (FMaxWidth>0) and (FMaxWidth < FMinWidth) then
FMinWidth:= FMaxWidth;
Change;
end;
end;
{------------------------------------------------------------------------------
Method: TSizeConstraints.SetMaxHeight
Params: Value: the new value of the property
Returns: Nothing
Sets a new value of its property.
------------------------------------------------------------------------------}
procedure TSizeConstraints.SetMaxHeight(Value: TConstraintSize);
begin
if Value <> FMaxHeight then begin
FMaxHeight:= Value;
if (FMinHeight > 0) and (FMaxHeight>0) and (FMaxHeight < FMinHeight) then
FMinHeight:= FMaxHeight;
Change;
end;
end;
{------------------------------------------------------------------------------
Method: TSizeConstraints.SetMinWidth
Params: Value: the new value of the property
Returns: Nothing
Sets a new value of its property.
------------------------------------------------------------------------------}
procedure TSizeConstraints.SetMinWidth(Value: TConstraintSize);
begin
if Value <> FMinWidth then begin
FMinWidth:= Value;
if (FMaxWidth > 0) and (FMinWidth > FMaxWidth) then FMaxWidth:= FMinWidth;
Change;
end;
end;
{------------------------------------------------------------------------------
Method: TSizeConstraints.SetMinHeight
Params: Value: the new value of the property
Returns: Nothing
Sets a new value of its property.
------------------------------------------------------------------------------}
procedure TSizeConstraints.SetMinHeight(Value: TConstraintSize);
begin
if Value <> FMinHeight then begin
FMinHeight:= Value;
if (FMaxHeight > 0) and (FMinHeight > FMaxHeight) then FMaxHeight:= FMinHeight;
Change;
end;
end;
procedure TSizeConstraints.SetOptions(const AValue: TSizeConstraintsOptions);
begin
if FOptions=AValue then exit;
FOptions:=AValue;
end;
{------------------------------------------------------------------------------
Method: TSizeConstraints.Change
Params: none
Returns: Nothing
Calls a change handler if assigned.
------------------------------------------------------------------------------}
procedure TSizeConstraints.Change;
begin
FControl.DoConstraintsChange(Self);
if Assigned(FOnChange) then FOnChange(Self);
end;
{------------------------------------------------------------------------------
Method: TSizeConstraints.AssignTo
Params: Dest: Destination constraints to be assigned
Returns: Nothing
Calls a change handler if assigned.
------------------------------------------------------------------------------}
procedure TSizeConstraints.AssignTo(Dest: TPersistent);
begin
if Dest is TSizeConstraints then begin
with TSizeConstraints(Dest) do begin
if (FMinWidth<>Self.FMinWidth)
or (FMaxWidth<>Self.FMaxWidth)
or (FMinHeight<>Self.FMinHeight)
or (FMaxHeight<>Self.FMaxHeight) then begin
FMinWidth:= Self.FMinWidth;
FMaxWidth:= Self.FMaxWidth;
FMinHeight:= Self.FMinHeight;
FMaxHeight:= Self.FMaxHeight;
Change;
end;
end;
end else begin
inherited AssignTo(Dest);
end;
end;
procedure TSizeConstraints.AutoAdjustLayout(const AXProportion,
AYProportion: Double);
procedure Scale(var Value: Integer; const Proportion: Double; var Changed: Boolean);
begin
if Value<>0 then
begin
Value := Round(Value * Proportion);
Changed := True;
end;
end;
var
Changed: Boolean;
begin
Changed := False;
Scale(FMaxWidth, AXProportion, Changed);
Scale(FMinWidth, AXProportion, Changed);
Scale(FMaxHeight, AYProportion, Changed);
Scale(FMinHeight, AYProportion, Changed);
if Changed then
Change;
end;
// included by controls.pp
|