| 1 |
unit KASComCtrls; |
| 2 |
|
| 3 |
{$mode objfpc}{$H+} |
| 4 |
|
| 5 |
interface |
| 6 |
|
| 7 |
uses |
| 8 |
Classes, SysUtils, Controls, ComCtrls, Graphics, Dialogs; |
| 9 |
|
| 10 |
type |
| 11 |
|
| 12 |
{ TToolButtonClr } |
| 13 |
|
| 14 |
TToolButtonClr = class(TToolButton) |
| 15 |
private |
| 16 |
FButtonColor: TColor; |
| 17 |
FColorDialog: TColorDialog; |
| 18 |
procedure SetButtonColor(AValue: TColor); |
| 19 |
protected |
| 20 |
procedure Paint; override; |
| 21 |
procedure ShowColorDialog; |
| 22 |
public |
| 23 |
constructor Create(TheOwner: TComponent); override; |
| 24 |
procedure Click; override; |
| 25 |
property ButtonColor: TColor read FButtonColor write SetButtonColor; |
| 26 |
end; |
| 27 |
|
| 28 |
{ TToolBarAdv } |
| 29 |
|
| 30 |
TToolBarAdv = class(TToolBar) |
| 31 |
private |
| 32 |
FToolBarFlags: TToolBarFlags; |
| 33 |
protected |
| 34 |
procedure CalculatePreferredSize(var PreferredWidth, |
| 35 |
PreferredHeight: Integer; |
| 36 |
{%H-}WithThemeSpace: Boolean); override; |
| 37 |
procedure AlignControls({%H-}AControl: TControl; |
| 38 |
var RemainingClientRect: TRect); override; |
| 39 |
function WrapButtons(UseSize: Integer; out NewWidth, |
| 40 |
NewHeight: Integer; Simulate: Boolean): Boolean; |
| 41 |
end; |
| 42 |
|
| 43 |
procedure Register; |
| 44 |
|
| 45 |
implementation |
| 46 |
|
| 47 |
uses |
| 48 |
Math; |
| 49 |
|
| 50 |
{ TToolButtonClr } |
| 51 |
|
| 52 |
procedure TToolButtonClr.SetButtonColor(AValue: TColor); |
| 53 |
begin |
| 54 |
if FButtonColor <> AValue then |
| 55 |
begin |
| 56 |
FButtonColor:= AValue; |
| 57 |
Invalidate; |
| 58 |
end; |
| 59 |
end; |
| 60 |
|
| 61 |
procedure TToolButtonClr.Paint; |
| 62 |
var |
| 63 |
ARect, IconRect: TRect; |
| 64 |
begin |
| 65 |
inherited Paint; |
| 66 |
|
| 67 |
if (FToolBar <> nil) and (ClientWidth > 0) and (ClientHeight > 0) then |
| 68 |
begin |
| 69 |
ARect:= ClientRect; |
| 70 |
|
| 71 |
IconRect.Left:= (ARect.Width - FToolBar.ImagesWidth) div 2; |
| 72 |
IconRect.Top:= (ARect.Height - FToolBar.ImagesWidth) div 2; |
| 73 |
IconRect.Right:= IconRect.Left + FToolBar.ImagesWidth; |
| 74 |
IconRect.Bottom:= IconRect.Top + FToolBar.ImagesWidth; |
| 75 |
|
| 76 |
if Enabled then |
| 77 |
begin |
| 78 |
Canvas.Brush.Style:= bsSolid; |
| 79 |
Canvas.Brush.Color:= FButtonColor |
| 80 |
end |
| 81 |
else begin |
| 82 |
Canvas.Brush.Color:= clGrayText; |
| 83 |
Canvas.Brush.Style:= bsDiagCross; |
| 84 |
end; |
| 85 |
|
| 86 |
Canvas.Pen.Color:= clBtnText; |
| 87 |
Canvas.Rectangle(IconRect); |
| 88 |
end; |
| 89 |
end; |
| 90 |
|
| 91 |
procedure TToolButtonClr.ShowColorDialog; |
| 92 |
begin |
| 93 |
if not Enabled then Exit; |
| 94 |
|
| 95 |
if (FColorDialog = nil) then |
| 96 |
begin |
| 97 |
FColorDialog := TColorDialog.Create(Self); |
| 98 |
end; |
| 99 |
FColorDialog.Color := ButtonColor; |
| 100 |
|
| 101 |
if FColorDialog.Execute then |
| 102 |
begin |
| 103 |
ButtonColor := FColorDialog.Color; |
| 104 |
end; |
| 105 |
end; |
| 106 |
|
| 107 |
constructor TToolButtonClr.Create(TheOwner: TComponent); |
| 108 |
begin |
| 109 |
FButtonColor:= clRed; |
| 110 |
inherited Create(TheOwner); |
| 111 |
end; |
| 112 |
|
| 113 |
procedure TToolButtonClr.Click; |
| 114 |
begin |
| 115 |
inherited Click; |
| 116 |
ShowColorDialog; |
| 117 |
end; |
| 118 |
|
| 119 |
{ TToolBarAdv } |
| 120 |
|
| 121 |
procedure TToolBarAdv.CalculatePreferredSize(var PreferredWidth, |
| 122 |
PreferredHeight: Integer; WithThemeSpace: Boolean); |
| 123 |
begin |
| 124 |
if IsVertical then |
| 125 |
WrapButtons(Height, PreferredWidth, PreferredHeight, True) |
| 126 |
else |
| 127 |
WrapButtons(Width, PreferredWidth, PreferredHeight, True); |
| 128 |
end; |
| 129 |
|
| 130 |
procedure TToolBarAdv.AlignControls(AControl: TControl; |
| 131 |
var RemainingClientRect: TRect); |
| 132 |
var |
| 133 |
NewWidth, NewHeight: integer; |
| 134 |
begin |
| 135 |
if tbfPlacingControls in FToolBarFlags then exit; |
| 136 |
Include(FToolBarFlags, tbfPlacingControls); |
| 137 |
DisableAlign; |
| 138 |
try |
| 139 |
AdjustClientRect(RemainingClientRect); |
| 140 |
if IsVertical then |
| 141 |
WrapButtons(Height, NewWidth, NewHeight, False) |
| 142 |
else |
| 143 |
WrapButtons(Width, NewWidth, NewHeight, False); |
| 144 |
finally |
| 145 |
Exclude(FToolBarFlags, tbfPlacingControls); |
| 146 |
EnableAlign; |
| 147 |
end; |
| 148 |
end; |
| 149 |
|
| 150 |
function TToolBarAdv.WrapButtons(UseSize: Integer; out NewWidth, |
| 151 |
NewHeight: Integer; Simulate: Boolean): Boolean; |
| 152 |
var |
| 153 |
ARect: TRect; |
| 154 |
X, Y: Integer; |
| 155 |
Vertical: Boolean; |
| 156 |
LeftToRight: Boolean; |
| 157 |
CurControl: TControl; |
| 158 |
StartX, StartY: Integer; |
| 159 |
FRowWidth, FRowHeight: Integer; |
| 160 |
|
| 161 |
procedure CalculatePosition; |
| 162 |
var |
| 163 |
NewBounds: TRect; |
| 164 |
StartedAtRowStart: Boolean; |
| 165 |
begin |
| 166 |
if IsVertical then |
| 167 |
begin |
| 168 |
NewBounds := Bounds(X, Y, FRowWidth, CurControl.Height); |
| 169 |
repeat |
| 170 |
if (not Wrapable) or |
| 171 |
(NewBounds.Top = StartY) or |
| 172 |
(NewBounds.Bottom <= ARect.Bottom) then |
| 173 |
begin |
| 174 |
// control fits into the column |
| 175 |
X := NewBounds.Left; |
| 176 |
Y := NewBounds.Top; |
| 177 |
Break; |
| 178 |
end; |
| 179 |
|
| 180 |
// try next column |
| 181 |
NewBounds.Top := StartY; |
| 182 |
NewBounds.Bottom := NewBounds.Top + CurControl.Height; |
| 183 |
Inc(NewBounds.Left, FRowWidth); |
| 184 |
Inc(NewBounds.Right, FRowWidth); |
| 185 |
until False; |
| 186 |
end |
| 187 |
else begin |
| 188 |
StartedAtRowStart := (X = StartX); |
| 189 |
|
| 190 |
if LeftToRight then |
| 191 |
NewBounds := Bounds(X, Y, CurControl.Width, FRowHeight) |
| 192 |
else begin |
| 193 |
NewBounds := Bounds(X - CurControl.Width, Y, CurControl.Width, FRowHeight); |
| 194 |
end; |
| 195 |
|
| 196 |
repeat |
| 197 |
if (not Wrapable) or |
| 198 |
(StartedAtRowStart) or |
| 199 |
(LeftToRight and ((NewBounds.Left = StartX) or (NewBounds.Right <= ARect.Right))) or |
| 200 |
((not LeftToRight) and ((NewBounds.Right = StartX) or (NewBounds.Left >= ARect.Left))) then |
| 201 |
begin |
| 202 |
// control fits into the row |
| 203 |
X := NewBounds.Left; |
| 204 |
Y := NewBounds.Top; |
| 205 |
Break; |
| 206 |
end; |
| 207 |
StartedAtRowStart := True; |
| 208 |
|
| 209 |
// try next row |
| 210 |
if LeftToRight then |
| 211 |
begin |
| 212 |
NewBounds.Left := StartX; |
| 213 |
NewBounds.Right := NewBounds.Left + CurControl.Width; |
| 214 |
end else begin |
| 215 |
NewBounds.Right := StartX; |
| 216 |
NewBounds.Left := NewBounds.Right - CurControl.Width; |
| 217 |
end; |
| 218 |
Inc(NewBounds.Top, FRowHeight); |
| 219 |
Inc(NewBounds.Bottom, FRowHeight); |
| 220 |
until False; |
| 221 |
end; |
| 222 |
end; |
| 223 |
|
| 224 |
var |
| 225 |
I: Integer; |
| 226 |
W, H: Integer; |
| 227 |
CurClientRect: TRect; |
| 228 |
AdjustClientFrame: TRect; |
| 229 |
begin |
| 230 |
NewWidth := 0; |
| 231 |
NewHeight := 0; |
| 232 |
Result := True; |
| 233 |
Vertical := IsVertical; |
| 234 |
FRowWidth:= ButtonWidth; |
| 235 |
FRowHeight:= ButtonHeight; |
| 236 |
if Vertical then |
| 237 |
begin |
| 238 |
LeftToRight := True; |
| 239 |
end |
| 240 |
else begin |
| 241 |
LeftToRight := not UseRightToLeftAlignment; |
| 242 |
end; |
| 243 |
DisableAlign; |
| 244 |
BeginUpdate; |
| 245 |
try |
| 246 |
CurClientRect := ClientRect; |
| 247 |
if Vertical then |
| 248 |
Inc(CurClientRect.Bottom, UseSize - Height) |
| 249 |
else begin |
| 250 |
Inc(CurClientRect.Right, UseSize - Width); |
| 251 |
end; |
| 252 |
ARect := CurClientRect; |
| 253 |
AdjustClientRect(ARect); |
| 254 |
AdjustClientFrame.Left := ARect.Left - CurClientRect.Left; |
| 255 |
AdjustClientFrame.Top := ARect.Top - CurClientRect.Top; |
| 256 |
AdjustClientFrame.Right := CurClientRect.Right - ARect.Right; |
| 257 |
AdjustClientFrame.Bottom := CurClientRect.Bottom - ARect.Bottom; |
| 258 |
//DebugLn(['TToolBar.WrapButtons ',DbgSName(Self),' ARect=',dbgs(ARect)]); |
| 259 |
// important: top, left button must start in the AdjustClientRect top, left |
| 260 |
// otherwise Toolbar.AutoSize=true will create an endless loop |
| 261 |
if Vertical or LeftToRight then |
| 262 |
StartX := ARect.Left |
| 263 |
else begin |
| 264 |
StartX := ARect.Right; |
| 265 |
end; |
| 266 |
StartY := ARect.Top; |
| 267 |
X := StartX; |
| 268 |
Y := StartY; |
| 269 |
for I := 0 to ButtonList.Count - 1 do |
| 270 |
begin |
| 271 |
CurControl := TControl(ButtonList[I]); |
| 272 |
|
| 273 |
if not CurControl.IsControlVisible then |
| 274 |
Continue; |
| 275 |
|
| 276 |
CalculatePosition; |
| 277 |
W := CurControl.Width; |
| 278 |
H := CurControl.Height; |
| 279 |
|
| 280 |
if (not Simulate) and ((CurControl.Left <> X) or (CurControl.Top <> Y)) then |
| 281 |
begin |
| 282 |
CurControl.SetBounds(X, Y, W, H); // Note: do not use SetBoundsKeepBase |
| 283 |
end; |
| 284 |
|
| 285 |
// adjust NewWidth, NewHeight |
| 286 |
if LeftToRight then |
| 287 |
NewWidth := Max(NewWidth, X + W + AdjustClientFrame.Right) |
| 288 |
else begin |
| 289 |
NewWidth := Max(NewWidth, ARect.Right - X + ARect.Left + AdjustClientFrame.Right); |
| 290 |
end; |
| 291 |
NewHeight := Max(NewHeight, Y + H + AdjustClientFrame.Bottom); |
| 292 |
|
| 293 |
// step to next position |
| 294 |
if IsVertical then |
| 295 |
Inc(Y, H) |
| 296 |
else if LeftToRight then |
| 297 |
Inc(X, W); |
| 298 |
end; |
| 299 |
finally |
| 300 |
EndUpdate; |
| 301 |
EnableAlign; |
| 302 |
end; |
| 303 |
end; |
| 304 |
|
| 305 |
procedure Register; |
| 306 |
begin |
| 307 |
RegisterComponents('KASComponents', [TToolBarAdv]); |
| 308 |
RegisterNoIcon([TToolButtonClr]); |
| 309 |
end; |
| 310 |
|
| 311 |
end. |
| 312 |
|