Debian Codegraph @

components/KASToolBar/kastoolbar.pas

raw
1 {
2 Double Commander components
3 -------------------------------------------------------------------------
4 Toolbar panel class
5
6 Copyright (C) 2006-2023 Alexander Koblov ([email protected])
7
8 contributors:
9 2012 Przemyslaw Nagay ([email protected])
10
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 in a file called COPYING along with this program; if not, write to
23 the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
24 02139, USA.
25 }
26
27 unit KASToolBar;
28
29 {$mode objfpc}{$H+}
30
31 interface
32
33 uses
34 Classes, SysUtils, LResources, Forms, Controls, ComCtrls,
35 Graphics, Dialogs, ExtCtrls, Buttons, FileUtil, Menus,
36 DCXmlConfig, KASToolItems, LCLVersion, LMessages;
37
38 type
39 TOnToolButtonClick = procedure (Sender: TObject) of object;
40 TOnToolButtonMouseUpDown = procedure (Sender: TObject; Button: TMouseButton; Shift:TShiftState; X,Y:Integer) of object;
41 TOnToolButtonMouseMove = procedure (Sender: TObject; Shift:TShiftState; X,Y:Integer; NumberOfButton: Integer) of object;
42 TOnToolButtonDragOver = procedure(Sender, Source: TObject; X,Y: Integer;
43 State: TDragState; var Accept: Boolean; NumberOfButton: Integer) of object;
44 TOnToolButtonDragDrop = procedure(Sender, Source: TObject; X, Y: Integer) of object;
45 TOnToolButtonEndDrag = procedure(Sender, Target: TObject; X,Y: Integer) of object;
46 TOnLoadButtonGlyph = function (ToolItem: TKASToolItem; iIconSize: Integer; clBackColor: TColor): TBitmap of object;
47 TOnToolItemExecute = procedure (ToolItem: TKASToolItem) of object;
48 TOnConfigLoadItem = function (Config: TXmlConfig; Node: TXmlNode): TKASToolItem of object;
49 TOnToolItemShortcutsHint = function (Sender: TObject; ToolItem: TKASNormalItem): String of object;
50 TTypeOfConfigurationLoad = (tocl_FlushCurrentToolbarContent, tocl_AddToCurrentToolbarContent);
51
52 TKASToolBar = class;
53
54 { TKASToolButton }
55
56 TKASToolButton = class(TSpeedButton)
57 private
58 FOverlay: TBitmap;
59 FToolItem: TKASToolItem;
60 function GetToolBar: TKASToolBar;
61 protected
62 procedure CalculatePreferredSize(var PreferredWidth,
63 PreferredHeight: integer; WithThemeSpace: Boolean); override;
64 function DrawGlyph(ACanvas: TCanvas; const AClient: TRect; const AOffset: TPoint;
65 AState: TButtonState; ATransparent: Boolean; BiDiFlags: Longint): TRect; override;
66 procedure ActionChange(Sender: TObject; CheckDefaults: Boolean); override;
67 procedure CMHintShow(var Message: TLMessage); message CM_HINTSHOW;
68 public
69 constructor Create(AOwner: TComponent; Item: TKASToolItem); reintroduce;
70 destructor Destroy; override;
71 procedure Click; override;
72 public
73 property ToolBar: TKASToolBar read GetToolBar;
74 property ToolItem: TKASToolItem read FToolItem;
75 end;
76
77 { TKASToolDivider }
78
79 TKASToolDivider = class(TKASToolButton)
80 protected
81 procedure CalculatePreferredSize(var PreferredWidth,
82 PreferredHeight: integer; WithThemeSpace: Boolean); override;
83 procedure Paint; override;
84 end;
85
86 { TKASToolBar }
87
88 TKASToolBar = class(TToolBar, IToolOwner)
89 private
90 FButtonHeight: Integer;
91 FButtonWidth: Integer;
92 FFlat: Boolean;
93 FGlyphSize: Integer;
94 FRadioToolBar: Boolean;
95 FRowHeight, FRowWidth: Integer;
96 FShowDividerAsButton: Boolean;
97 FToolItemExecutors: TFPList;
98 FToolItems: TKASToolBarItems;
99 FToolPopupMenu: TPopupMenu;
100 FOwnsToolItems: Boolean;
101 {$if lcl_fullversion < 1010000}
102 FUpdateCount: Integer;
103 {$endif}
104 FOnToolButtonClick: TOnToolButtonClick;
105 FOnToolButtonMouseDown: TOnToolButtonMouseUpDown;
106 FOnToolButtonMouseUp: TOnToolButtonMouseUpDown;
107 FOnToolButtonMouseMove: TOnToolButtonMouseMove;
108 FOnToolButtonDragOver: TOnToolButtonDragOver;
109 FOnToolButtonDragDrop: TOnToolButtonDragDrop;
110 FOnToolButtonEndDrag: TOnToolButtonEndDrag;
111 FOnLoadButtonGlyph: TOnLoadButtonGlyph;
112 FOnLoadButtonOverlay: TOnLoadButtonGlyph;
113 FOnToolItemExecute: TOnToolItemExecute;
114 FOnToolItemShortcutsHint: TOnToolItemShortcutsHint;
115 FKASToolBarFlags: TToolBarFlags;
116 FResizeButtonsNeeded: Boolean;
117 procedure AssignToolButtonProperties(ToolButton: TKASToolButton);
118 procedure ClearExecutors;
119 function CreateButton(Item: TKASToolItem): TKASToolButton;
120 function ExecuteToolItem(Item: TKASToolItem): Boolean;
121 function FindButton(Button: TKASToolButton): Integer;
122 function GetToolItemShortcutsHint(Item: TKASToolItem): String;
123 function LoadBtnIcon(IconPath: String): TBitMap;
124 function GetButton(Index: Integer): TKASToolButton;
125 procedure InsertButton(InsertAt: Integer; ToolButton: TKASToolButton);
126 procedure SetButtonHeight(const AValue: Integer);
127 procedure SetButtonWidth(const AValue: Integer);
128 procedure SetChangePath(const {%H-}AValue: String);
129 procedure SetEnvVar(const {%H-}AValue: String);
130 procedure SetFlat(const AValue: Boolean);
131 procedure SetGlyphSize(const AValue: Integer);
132 procedure ShowMenu(ToolButton: TKASToolButton);
133 procedure ToolButtonClick(Sender: TObject);
134 procedure ToolButtonMouseDown(Sender: TObject; Button: TMouseButton; Shift:TShiftState; X,Y:Integer);
135 procedure ToolButtonMouseUp(Sender: TObject; Button: TMouseButton; Shift:TShiftState; X,Y:Integer);
136 procedure ToolButtonMouseMove(Sender: TObject; Shift:TShiftState; X,Y:Integer);
137 procedure ToolButtonDragOver(Sender, Source: TObject; X,Y: Integer; State: TDragState; var Accept: Boolean);
138 procedure ToolButtonDragDrop(Sender, Source: TObject; X,Y: Integer);
139 procedure ToolButtonEndDrag(Sender, Target: TObject; X, Y: Integer);
140 procedure ToolItemLoaded(Item: TKASToolItem);
141 procedure ToolMenuClicked(Sender: TObject);
142 procedure UpdateButtonsTags;
143 protected
144 procedure CalculatePreferredSize(var PreferredWidth,
145 PreferredHeight: Integer; WithThemeSpace: Boolean); override;
146 procedure AlignControls(AControl: TControl; var RemainingClientRect: TRect); override;
147 procedure FontChanged(Sender: TObject); override;
148 function WrapButtons(UseWidth: integer;
149 out NewWidth, NewHeight: Integer; Simulate: boolean): Boolean;
150 procedure ResizeButtons;
151 public
152 constructor Create(TheOwner: TComponent); override;
153 destructor Destroy; override;
154
155 function AddButton(Item: TKASToolItem): TKASToolButton;
156 procedure AddToolItemExecutor(ToolItemClass: TKASToolItemClass;
157 ExecuteFunction: TOnToolItemExecute);
158 procedure Clear;
159 procedure ClickItem(ToolItemID: String); overload;
160 function InsertButton(InsertAt: Integer; Item: TKASToolItem): TKASToolButton;
161 function InsertButton(InsertAt: TKASToolButton; Item: TKASToolItem): TKASToolButton;
162 procedure MoveButton(ButtonIndex, MovePosition: Integer);
163 procedure MoveButton(SourceButton: TKASToolButton; TargetToolBar: TKASToolBar; InsertAt: TKASToolButton);
164 procedure RemoveButton(Index: Integer);
165 procedure RemoveButton(Button: TKASToolButton);
166 procedure RemoveToolItemExecutor(ExecuteFunction: TOnToolItemExecute);
167 procedure UncheckAllButtons;
168 procedure UpdateIcon(ToolButton: TKASToolButton);
169 procedure UseItems(AItems: TKASToolBarItems);
170
171 procedure LoadConfiguration(Config: TXmlConfig; RootNode: TXmlNode;
172 Loader: TKASToolBarLoader; ConfigurationLoadType:TTypeOfConfigurationLoad);
173 procedure SaveConfiguration(Config: TXmlConfig; RootNode: TXmlNode);
174
175 procedure BeginUpdate; override;
176 procedure EndUpdate; override;
177 procedure SetButtonSize(NewButtonWidth, NewButtonHeight: Integer);
178 function PublicExecuteToolItem(Item: TKASToolItem): Boolean;
179
180 property Buttons[Index: Integer]: TKASToolButton read GetButton;
181
182 published
183 property OnLoadButtonGlyph : TOnLoadButtonGlyph read FOnLoadButtonGlyph write FOnLoadButtonGlyph;
184 property OnToolButtonClick: TOnToolButtonClick read FOnToolButtonClick write FOnToolButtonClick;
185 property OnLoadButtonOverlay: TOnLoadButtonGlyph read FOnLoadButtonOverlay write FOnLoadButtonOverlay;
186 property OnToolButtonMouseDown: TOnToolButtonMouseUpDown read FOnToolButtonMouseDown write FOnToolButtonMouseDown;
187 property OnToolButtonMouseUp: TOnToolButtonMouseUpDown read FOnToolButtonMouseUp write FOnToolButtonMouseUp;
188 property OnToolButtonMouseMove: TOnToolButtonMouseMove read FOnToolButtonMouseMove write FOnToolButtonMouseMove;
189 property OnToolButtonDragDrop: TOnToolButtonDragDrop read FOnToolButtonDragDrop write FOnToolButtonDragDrop;
190 property OnToolButtonEndDrag: TOnToolButtonEndDrag read FOnToolButtonEndDrag write FOnToolButtonEndDrag;
191 property OnToolButtonDragOver: TOnToolButtonDragOver read FOnToolButtonDragOver write FOnToolButtonDragOver;
192 property OnToolItemExecute: TOnToolItemExecute read FOnToolItemExecute write FOnToolItemExecute;
193 property OnToolItemShortcutsHint: TOnToolItemShortcutsHint read FOnToolItemShortcutsHint write FOnToolItemShortcutsHint;
194 property RadioToolBar: Boolean read FRadioToolBar write FRadioToolBar default False;
195 property Flat: Boolean read FFlat write SetFlat default False;
196 property GlyphSize: Integer read FGlyphSize write SetGlyphSize;
197 property ButtonHeight: Integer read FButtonHeight write SetButtonHeight default 22;
198 property ButtonWidth: Integer read FButtonWidth write SetButtonWidth default 23;
199 property ShowDividerAsButton: Boolean read FShowDividerAsButton write FShowDividerAsButton default False;
200 end;
201
202 procedure Register;
203
204 implementation
205
206 uses
207 Themes, Types, Math, ActnList, DCOSUtils;
208
209 type
210 PToolItemExecutor = ^TToolItemExecutor;
211 TToolItemExecutor = record
212 ToolItemClass: TKASToolItemClass;
213 ToolItemExecute: TOnToolItemExecute;
214 end;
215
216 procedure Register;
217 begin
218 RegisterComponents('KASComponents',[TKASToolBar]);
219 end;
220
221 { TKASToolBar }
222
223 procedure TKASToolBar.InsertButton(InsertAt: Integer; ToolButton: TKASToolButton);
224 begin
225 if InsertAt < 0 then
226 InsertAt:= 0;
227 if InsertAt > ButtonList.Count then
228 InsertAt:= ButtonList.Count;
229
230 ButtonList.Insert(InsertAt, ToolButton);
231 FToolItems.Insert(InsertAt, ToolButton.ToolItem);
232
233 UpdateButtonsTags;
234 ResizeButtons;
235 end;
236
237 function TKASToolBar.InsertButton(InsertAt: TKASToolButton; Item: TKASToolItem): TKASToolButton;
238 var
239 Index: Integer;
240 begin
241 Index := ButtonList.IndexOf(InsertAt);
242 if Index < 0 then
243 Index := ButtonCount;
244 Result := InsertButton(Index, Item);
245 end;
246
247 procedure TKASToolBar.CalculatePreferredSize(var PreferredWidth,
248 PreferredHeight: Integer; WithThemeSpace: Boolean);
249 begin
250 WrapButtons(Width, PreferredWidth, PreferredHeight, True);
251 end;
252
253 procedure TKASToolBar.AlignControls(AControl: TControl; var RemainingClientRect: TRect);
254 var
255 NewWidth, NewHeight: integer;
256 begin
257 if tbfPlacingControls in FKASToolBarFlags then exit;
258 Include(FKASToolBarFlags, tbfPlacingControls);
259 DisableAlign;
260 try
261 AdjustClientRect(RemainingClientRect);
262 if IsVertical then
263 WrapButtons(Height, NewWidth, NewHeight, False)
264 else
265 WrapButtons(Width, NewWidth, NewHeight, False);
266 finally
267 Exclude(FKASToolBarFlags, tbfPlacingControls);
268 EnableAlign;
269 end;
270 end;
271
272 procedure TKASToolBar.FontChanged(Sender: TObject);
273 begin
274 inherited FontChanged(Sender);
275 ResizeButtons;
276 end;
277
278 function TKASToolBar.WrapButtons(UseWidth: integer;
279 out NewWidth, NewHeight: Integer; Simulate: boolean): Boolean;
280 var
281 ARect: TRect;
282 x: Integer;
283 y: Integer;
284 CurControl: TControl;
285 StartX, StartY: Integer;
286
287 procedure CalculatePosition;
288 var
289 NewBounds: TRect;
290 begin
291 if IsVertical then
292 begin
293 NewBounds := Bounds(x, y, FRowWidth, CurControl.Height);
294 repeat
295 if (not Wrapable) or
296 (NewBounds.Bottom <= ARect.Bottom) or
297 (NewBounds.Top = StartY) then
298 begin
299 // control fits into the column
300 x := NewBounds.Left;
301 y := NewBounds.Top;
302 break;
303 end;
304
305 // try next column
306 NewBounds.Top := StartY;
307 NewBounds.Bottom := NewBounds.Top + CurControl.Height;
308 inc(NewBounds.Left, FRowWidth);
309 inc(NewBounds.Right, FRowWidth);
310 until false;
311 end
312 else begin
313 NewBounds := Bounds(x, y, CurControl.Width, FRowHeight);
314 repeat
315 if (not Wrapable) or
316 (NewBounds.Right <= ARect.Right) or
317 (NewBounds.Left = StartX) then
318 begin
319 // control fits into the row
320 x := NewBounds.Left;
321 y := NewBounds.Top;
322 break;
323 end;
324
325 // try next row
326 NewBounds.Left := StartX;
327 NewBounds.Right := NewBounds.Left + CurControl.Width;
328 inc(NewBounds.Top, FRowHeight);
329 inc(NewBounds.Bottom, FRowHeight);
330 until false;
331 end;
332 end;
333
334 var
335 CurClientRect: TRect;
336 AdjustClientFrame: TRect;
337 i: Integer;
338 w, h: Longint;
339 begin
340 Result := True;
341 NewWidth := 0;
342 NewHeight := 0;
343 DisableAlign;
344 BeginUpdate;
345 try
346 CurClientRect := ClientRect;
347 inc(CurClientRect.Right, UseWidth - Width);
348 ARect := CurClientRect;
349 AdjustClientRect(ARect);
350 AdjustClientFrame.Left := ARect.Left - CurClientRect.Left;
351 AdjustClientFrame.Top := ARect.Top - CurClientRect.Top;
352 AdjustClientFrame.Right := CurClientRect.Right - ARect.Right;
353 AdjustClientFrame.Bottom := CurClientRect.Bottom - ARect.Bottom;
354 //DebugLn(['TToolBar.WrapButtons ',DbgSName(Self),' ARect=',dbgs(ARect)]);
355 // important: top, left button must start in the AdjustClientRect top, left
356 // otherwise Toolbar.AutoSize=true will create an endless loop
357 StartX := ARect.Left;
358 StartY := ARect.Top;
359 x := StartX;
360 y := StartY;
361 for i := 0 to ButtonList.Count - 1 do
362 begin
363 CurControl := TControl(ButtonList[i]);
364 if not CurControl.IsControlVisible then
365 Continue;
366 CalculatePosition;
367
368 w := CurControl.Width;
369 h := CurControl.Height;
370
371 if (not Simulate) and ((CurControl.Left <> x) or (CurControl.Top <> y)) then
372 begin
373 CurControl.SetBounds(x,y,w,h); // Note: do not use SetBoundsKeepBase
374 end;
375
376 // adjust NewWidth, NewHeight
377 NewWidth := Max(NewWidth, x + w + AdjustClientFrame.Right);
378 NewHeight := Max(NewHeight, y + h + AdjustClientFrame.Bottom);
379
380 // step to next position
381 if IsVertical then
382 Inc(y, h)
383 else
384 Inc(x, w);
385 end;
386 finally
387 EndUpdate;
388 EnableAlign;
389 end;
390 end;
391
392 procedure TKASToolBar.ResizeButtons;
393 var
394 w, h: LongInt;
395 i: Integer;
396 CurControl: TControl;
397 begin
398 if FUpdateCount > 0 then
399 begin
400 FResizeButtonsNeeded := True;
401 Exit;
402 end;
403
404 InvalidatePreferredChildSizes;
405 FRowWidth := ButtonWidth;
406 FRowHeight := ButtonHeight; // Row height is at least initial button height
407
408 // First recalculate RowWidth & RowHeight
409 for i := 0 to ButtonList.Count - 1 do
410 begin
411 CurControl := TControl(ButtonList[i]);
412 w := ButtonWidth;
413 h := ButtonHeight;
414 CurControl.GetPreferredSize(w, h);
415 if FRowWidth < w then
416 FRowWidth := w;
417 if FRowHeight < h then
418 FRowHeight := h;
419 end;
420
421 FResizeButtonsNeeded := False;
422
423 // Now resize buttons.
424 DisableAlign;
425 BeginUpdate;
426 try
427 for i := 0 to ButtonList.Count - 1 do
428 begin
429 CurControl := TControl(ButtonList[i]);
430 if IsVertical then
431 begin
432 w := FRowWidth;
433 h := ButtonHeight;
434 end
435 else begin
436 w := ButtonWidth;
437 h := FRowHeight;
438 end;
439 CurControl.GetPreferredSize(w, h);
440 if (CurControl.Width <> w) or (CurControl.Height <> h) then
441 CurControl.SetBounds(CurControl.Left, CurControl.Top, w, h);
442 end;
443 InvalidatePreferredSize;
444 AdjustSize;
445 finally
446 EndUpdate;
447 EnableAlign;
448 end;
449 end;
450
451 procedure TKASToolBar.SaveConfiguration(Config: TXmlConfig; RootNode: TXmlNode);
452 var
453 Node: TXmlNode;
454 Item: TKASToolItem;
455 i: Integer;
456 begin
457 if ButtonCount > 0 then
458 begin
459 Node := Config.AddNode(RootNode, 'Row');
460 for i := 0 to ButtonCount - 1 do
461 begin
462 Item := TKASToolButton(Buttons[i]).ToolItem;
463 Item.Save(Config, Node);
464 end;
465 end;
466 end;
467
468 function TKASToolBar.LoadBtnIcon(IconPath: String): TBitMap;
469 var
470 picture: TPicture;
471 begin
472 if (IconPath = '') or (not mbFileExists(IconPath)) then Exit(nil);
473
474 Picture := TPicture.Create;
475 try
476 Picture.LoadFromFile(IconPath);
477 Result := TBitmap.Create;
478 Result.Assign(Picture.Bitmap);
479 finally
480 FreeAndNil(Picture);
481 end;
482 end;
483
484 procedure TKASToolBar.LoadConfiguration(Config: TXmlConfig; RootNode: TXmlNode;
485 Loader: TKASToolBarLoader; ConfigurationLoadType:TTypeOfConfigurationLoad);
486 var
487 Node: TXmlNode;
488 begin
489 BeginUpdate;
490 if ConfigurationLoadType=tocl_FlushCurrentToolbarContent then
491 begin
492 Clear;
493 end;
494 try
495 Node := Config.FindNode(RootNode, 'Row', False);
496 if Assigned(Node) then
497 Loader.Load(Config, Node, @ToolItemLoaded);
498 finally
499 EndUpdate;
500 end;
501 end;
502
503 procedure TKASToolBar.AssignToolButtonProperties(ToolButton: TKASToolButton);
504 begin
505 ToolButton.OnClick:= @ToolButtonClick;
506 ToolButton.OnMouseDown:= @ToolButtonMouseDown;
507 ToolButton.OnMouseUp:= @ToolButtonMouseUp;
508 ToolButton.OnMouseMove:= @ToolButtonMouseMove;
509 ToolButton.OnDragDrop:= @ToolButtonDragDrop;
510 ToolButton.OnDragOver:= @ToolButtonDragOver;
511 ToolButton.OnEndDrag:= @ToolButtonEndDrag;
512 end;
513
514 function TKASToolBar.GetToolItemShortcutsHint(Item: TKASToolItem): String;
515 begin
516 Result := '';
517 if Assigned(FOnToolItemShortcutsHint) and (Item is TKASNormalItem) then
518 Result := FOnToolItemShortcutsHint(Self, TKASNormalItem(Item));
519 end;
520
521 function TKASToolBar.GetButton(Index: Integer): TKASToolButton;
522 begin
523 Result:= TKASToolButton(ButtonList.Items[Index]);
524 end;
525
526 procedure TKASToolBar.SetChangePath(const AValue: String);
527 begin
528 end;
529
530 procedure TKASToolBar.SetEnvVar(const AValue: String);
531 begin
532 end;
533
534 procedure TKASToolBar.SetFlat(const AValue: Boolean);
535 var
536 I: Integer;
537 begin
538 FFlat:= AValue;
539 for I:= 0 to ButtonList.Count - 1 do
540 TKASToolButton(ButtonList.Items[I]).Flat:= FFlat;
541 end;
542
543 procedure TKASToolBar.SetGlyphSize(const AValue: Integer);
544 var
545 I: Integer;
546 begin
547 if FGlyphSize = AValue then Exit;
548 FGlyphSize:= AValue;
549
550 BeginUpdate;
551 try
552 for I := 0 to ButtonList.Count - 1 do
553 UpdateIcon(TKASToolButton(ButtonList[i]));
554 finally
555 EndUpdate;
556 end;
557 end;
558
559 procedure TKASToolBar.ShowMenu(ToolButton: TKASToolButton);
560 procedure MakeMenu(PopupMenu: TMenuItem; MenuItem: TKASMenuItem);
561 var
562 I: Integer;
563 Item: TKASToolItem;
564 PopupMenuItem: TMenuItem;
565 BitmapTmp: TBitmap = nil;
566 sText: String;
567 begin
568 for I := 0 to MenuItem.SubItems.Count - 1 do
569 begin
570 Item := MenuItem.SubItems.Items[I];
571 if Item is TKASSeparatorItem then
572 begin
573 PopupMenu.AddSeparator;
574 end
575 else
576 begin
577 PopupMenuItem := TMenuItem.Create(PopupMenu);
578 sText := Item.GetEffectiveText;
579 if sText = '' then
580 sText := Item.GetEffectiveHint;
581 PopupMenuItem.Caption := StringReplace(StringReplace(sText, #$0A, ' | ', [rfReplaceAll]), ' | ----', '', [rfReplaceAll]);
582
583 if Item is TKASNormalItem then
584 begin
585 if Assigned(FOnLoadButtonGlyph) then
586 BitmapTmp := FOnLoadButtonGlyph(Item, 16, clMenu);
587 if not Assigned(BitmapTmp) then
588 BitmapTmp := LoadBtnIcon(TKASNormalItem(Item).Icon);
589
590 PopupMenuItem.Bitmap := BitmapTmp;
591 FreeAndNil(BitmapTmp);
592 end;
593
594 PopupMenuItem.Tag := PtrInt(Item);
595 PopupMenuItem.OnClick := TNotifyEvent(@ToolMenuClicked);
596 PopupMenu.Add(PopupMenuItem);
597
598 if Item is TKASMenuItem then
599 MakeMenu(PopupMenuItem, TKASMenuItem(Item));
600 end;
601 end;
602 end;
603 var
604 Point: TPoint;
605 begin
606 FToolPopupMenu.Free;
607 FToolPopupMenu := TPopupMenu.Create(Self);
608 MakeMenu(FToolPopupMenu.Items, ToolButton.ToolItem as TKASMenuItem);
609 Point.x := ToolButton.Left;
610 Point.y := ToolButton.Top + ToolButton.Height;
611 Point := Self.ClientToScreen(Point);
612 FToolPopupMenu.PopUp(Point.x, Point.y);
613 end;
614
615 procedure TKASToolBar.ToolButtonClick(Sender: TObject);
616 var
617 Button: TKASToolButton;
618 begin
619 Button := Sender as TKASToolButton;
620
621 // Do not allow depressing down buttons.
622 if FRadioToolBar and not Button.Down then
623 Button.Down := True;
624
625 if not ExecuteToolItem(Button.ToolItem) then
626 begin
627 if Assigned(FOnToolButtonClick) then
628 FOnToolButtonClick(Button)
629 else if Button.ToolItem is TKASMenuItem then
630 begin
631 ShowMenu(Button);
632 end;
633 end;
634 end;
635
636 procedure TKASToolBar.ToolButtonMouseDown(Sender: TObject; Button: TMouseButton; Shift:TShiftState; X,Y:Integer);
637 begin
638 if Assigned(FOnToolButtonMouseDown) then
639 FOnToolButtonMouseDown(Sender, Button, Shift, X,Y);
640 end;
641
642 procedure TKASToolBar.ToolButtonMouseUp(Sender: TObject; Button: TMouseButton; Shift:TShiftState; X,Y:Integer);
643 begin
644 if Assigned(FOnToolButtonMouseUp) then
645 FOnToolButtonMouseUp(Sender, Button, Shift, X,Y);
646 end;
647
648 procedure TKASToolBar.ToolItemLoaded(Item: TKASToolItem);
649 begin
650 AddButton(Item);
651 end;
652
653 procedure TKASToolBar.ToolMenuClicked(Sender: TObject);
654 begin
655 ExecuteToolItem(TKASToolItem((Sender as TMenuItem).Tag));
656 end;
657
658 procedure TKASToolBar.ToolButtonMouseMove(Sender: TObject; Shift:TShiftState; X,Y:Integer);
659 begin
660 if Assigned(FOnToolButtonMouseMove) then
661 FOnToolButtonMouseMove(Sender, Shift, X,Y, (Sender as TSpeedButton).Tag);
662 end;
663
664 procedure TKASToolBar.ToolButtonDragOver(Sender, Source: TObject; X,Y: Integer; State: TDragState; var Accept: Boolean);
665 begin
666 if Assigned(FOnToolButtonDragOver) then
667 FOnToolButtonDragOver(Sender, Source, X,Y, State, Accept, (Sender as TSpeedButton).Tag);
668 end;
669
670 procedure TKASToolBar.ToolButtonDragDrop(Sender, Source: TObject; X,Y: Integer);
671 begin
672 if Assigned(FOnToolButtonDragDrop) then
673 FOnToolButtonDragDrop(Sender, Source, X, Y);
674 end;
675
676 procedure TKASToolBar.ToolButtonEndDrag(Sender, Target: TObject; X, Y: Integer);
677 begin
678 if Assigned(FOnToolButtonEndDrag) then
679 FOnToolButtonEndDrag(Sender, Target, X, Y);
680 end;
681
682 procedure TKASToolBar.MoveButton(ButtonIndex, MovePosition: Integer);
683 begin
684 ButtonList.Move(ButtonIndex, MovePosition);
685 FToolItems.Move(ButtonIndex, MovePosition);
686 UpdateButtonsTags;
687 ResizeButtons;
688 end;
689
690 procedure TKASToolBar.MoveButton(SourceButton: TKASToolButton; TargetToolBar: TKASToolBar; InsertAt: TKASToolButton);
691 var
692 Index: Integer;
693 begin
694 Index := FindButton(SourceButton);
695 if (Index <> -1) and (FToolItems[Index] = SourceButton.ToolItem) then
696 begin
697 SourceButton.FToolItem := nil;
698 TargetToolBar.InsertButton(InsertAt, FToolItems.ReleaseItem(Index));
699 ButtonList.Delete(Index);
700 Application.ReleaseComponent(SourceButton); // Free later
701 UpdateButtonsTags;
702 Resize;
703 end;
704 end;
705
706 procedure TKASToolBar.UpdateButtonsTags;
707 var
708 I: Integer;
709 begin
710 for I:= 0 to ButtonList.Count - 1 do
711 TKASToolButton(ButtonList.Items[I]).Tag:= I;
712 end;
713
714 procedure TKASToolBar.UpdateIcon(ToolButton: TKASToolButton);
715 var
716 Bitmap: TBitmap = nil;
717 begin
718 try
719 if Assigned(FOnLoadButtonGlyph) then
720 Bitmap := FOnLoadButtonGlyph(ToolButton.ToolItem, FGlyphSize, clBtnFace);
721
722 if not Assigned(Bitmap) and (ToolButton.ToolItem is TKASNormalItem) then
723 Bitmap := LoadBtnIcon(TKASNormalItem(ToolButton.ToolItem).Icon);
724
725 try
726 if Assigned(Bitmap) and Assigned(FOnLoadButtonOverlay) and (not (ToolButton.ToolItem is TKASSeparatorItem)) then
727 begin
728 FreeAndNil(ToolButton.FOverlay);
729 ToolButton.FOverlay := FOnLoadButtonOverlay(ToolButton.ToolItem, FGlyphSize div 2, clBtnFace);
730 end;
731
732 ToolButton.Glyph.Assign(Bitmap);
733 finally
734 Bitmap.Free;
735 end;
736 except
737 // Ignore
738 end;
739 end;
740
741 procedure TKASToolBar.UseItems(AItems: TKASToolBarItems);
742 var
743 i: Integer;
744 Button: TKASToolButton;
745 begin
746 if Assigned(AItems) then
747 begin
748 BeginUpdate;
749
750 Clear;
751 if FOwnsToolItems then
752 FToolItems.Free;
753 FToolItems := AItems;
754 FOwnsToolItems := False;
755
756 // Insert the existing items as buttons.
757 for i := 0 to FToolItems.Count - 1 do
758 begin
759 Button := CreateButton(FToolItems.Items[i]);
760 if Assigned(Button) then
761 ButtonList.Insert(ButtonCount, Button);
762 end;
763 UpdateButtonsTags;
764 ResizeButtons;
765
766 EndUpdate;
767 end;
768 end;
769
770 procedure TKASToolBar.Clear;
771 var
772 I: Integer;
773 begin
774 BeginUpdate;
775
776 for I := 0 to ButtonList.Count - 1 do
777 TKASToolButton(ButtonList.Items[I]).Free;
778 ButtonList.Clear;
779 if Assigned(FToolItems) then
780 FToolItems.Clear;
781
782 EndUpdate;
783 end;
784
785 procedure TKASToolBar.ClearExecutors;
786 var
787 I: Integer;
788 begin
789 for I := 0 to FToolItemExecutors.Count - 1 do
790 Dispose(PToolItemExecutor(FToolItemExecutors[I]));
791 FToolItemExecutors.Clear;
792 end;
793
794 procedure TKASToolBar.ClickItem(ToolItemID: String);
795 var
796 I: Integer;
797 Button: TKASToolButton;
798 NormalItem: TKASNormalItem;
799 begin
800 for I := 0 to ButtonList.Count - 1 do
801 begin
802 Button := TKASToolButton(ButtonList.Items[I]);
803 if Button.ToolItem is TKASNormalItem then
804 begin
805 NormalItem := TKASNormalItem(Button.ToolItem);
806 if NormalItem.ID = ToolItemID then
807 begin
808 Button.Click;
809 Break;
810 end;
811
812 if Button.ToolItem.CheckExecute(ToolItemID) then
813 Break;
814 end;
815 end;
816 end;
817
818 procedure TKASToolBar.SetButtonHeight(const AValue: Integer);
819 begin
820 SetButtonSize(ButtonWidth, AValue);
821 end;
822
823 procedure TKASToolBar.SetButtonWidth(const AValue: Integer);
824 begin
825 SetButtonSize(AValue, ButtonHeight);
826 end;
827
828 constructor TKASToolBar.Create(TheOwner: TComponent);
829 begin
830 inherited Create(TheOwner);
831 FGlyphSize:= 16; // by default
832 FUpdateCount:= 0;
833 FButtonWidth := 23;
834 FButtonHeight := 22;
835 FKASToolBarFlags := [];
836 FToolItemExecutors := TFPList.Create;
837 FToolItems := TKASToolBarItems.Create;
838 FOwnsToolItems := True;
839 end;
840
841 function TKASToolBar.CreateButton(Item: TKASToolItem): TKASToolButton;
842 begin
843 if Assigned(Item) then
844 begin
845 if FOwnsToolItems then
846 Item.SetToolOwner(Self);
847
848 if Item is TKASSeparatorItem then
849 begin
850 Result := TKASToolDivider.Create(Self, Item);
851 end
852 else
853 begin
854 Result := TKASToolButton.Create(Self, Item);
855 Result.ShowHint := True;
856 Result.Caption := Item.GetEffectiveText;
857 Result.Hint := Item.GetEffectiveHint;
858 if ShowCaptions and (Result.Caption = '') then
859 Result.Caption := Result.Hint;
860 if Assigned(Item.Action) then
861 begin
862 Result.AllowAllUp := True;
863 Result.Action := Item.Action;
864 end;
865 end;
866
867 Result.Flat := FFlat;
868 if FRadioToolBar then
869 begin
870 Result.GroupIndex := 1;
871 Result.AllowAllUp := True;
872 end;
873
874 Result.ShowCaption := ShowCaptions;
875 UpdateIcon(Result);
876 AssignToolButtonProperties(Result);
877
878 Result.Parent := Self;
879 end
880 else
881 Result := nil;
882 end;
883
884 destructor TKASToolBar.Destroy;
885 begin
886 if not FOwnsToolItems then
887 FToolItems := nil; // Unassign before Clear so that items are not cleared.
888 Clear;
889 inherited Destroy;
890 ClearExecutors;
891 FToolItemExecutors.Free;
892 if FOwnsToolItems then
893 FToolItems.Free;
894 end;
895
896 function TKASToolBar.ExecuteToolItem(Item: TKASToolItem): Boolean;
897 var
898 I: Integer;
899 Executor: PToolItemExecutor;
900 BestMatch: PToolItemExecutor = nil;
901 begin
902 for I := 0 to FToolItemExecutors.Count - 1 do
903 begin
904 Executor := PToolItemExecutor(FToolItemExecutors[I]);
905 if Assigned(Executor^.ToolItemExecute) and
906 Item.InheritsFrom(Executor^.ToolItemClass) and
907 (not Assigned(BestMatch) or
908 (Executor^.ToolItemClass.InheritsFrom(BestMatch^.ToolItemClass))) then
909 begin
910 BestMatch := Executor;
911 end;
912 end;
913 Result := Assigned(BestMatch);
914 if Result then
915 BestMatch^.ToolItemExecute(Item);
916 end;
917
918 { TKASToolBar.PublicExecuteToolItem }
919 function TKASToolBar.PublicExecuteToolItem(Item: TKASToolItem): Boolean;
920 begin
921 result:=ExecuteToolItem(Item);
922 end;
923
924 procedure TKASToolBar.BeginUpdate;
925 begin
926 {$if lcl_fullversion < 1010000}
927 Inc(FUpdateCount);
928 {$endif}
929 inherited BeginUpdate;
930 DisableAutoSizing;
931 end;
932
933 procedure TKASToolBar.EndUpdate;
934 begin
935 EnableAutoSizing;
936 inherited EndUpdate;
937 {$if lcl_fullversion < 1010000}
938 Dec(FUpdateCount);
939 {$endif}
940 if (FUpdateCount = 0) and FResizeButtonsNeeded then
941 ResizeButtons;
942 end;
943
944 function TKASToolBar.FindButton(Button: TKASToolButton): Integer;
945 var
946 I: Integer;
947 begin
948 for I := 0 to ButtonList.Count - 1 do
949 if TKASToolButton(ButtonList[I]) = Button then
950 Exit(I);
951 Result := -1;
952 end;
953
954 procedure TKASToolBar.SetButtonSize(NewButtonWidth, NewButtonHeight: Integer);
955 begin
956 FButtonWidth := NewButtonWidth;
957 FButtonHeight := NewButtonHeight;
958 ResizeButtons;
959 end;
960
961 function TKASToolBar.AddButton(Item: TKASToolItem): TKASToolButton;
962 begin
963 Result := InsertButton(ButtonCount, Item);
964 end;
965
966 procedure TKASToolBar.AddToolItemExecutor(ToolItemClass: TKASToolItemClass; ExecuteFunction: TOnToolItemExecute);
967 var
968 Executor: PToolItemExecutor;
969 begin
970 New(Executor);
971 FToolItemExecutors.Add(Executor);
972 Executor^.ToolItemClass := ToolItemClass;
973 Executor^.ToolItemExecute := ExecuteFunction;
974 end;
975
976 function TKASToolBar.InsertButton(InsertAt: Integer; Item: TKASToolItem): TKASToolButton;
977 begin
978 Result := CreateButton(Item);
979 if Assigned(Result) then
980 InsertButton(InsertAt, Result);
981 end;
982
983 procedure TKASToolBar.RemoveButton(Index: Integer);
984 var
985 Button: TKASToolButton;
986 begin
987 Button := TKASToolButton(ButtonList.Items[Index]);
988 ButtonList.Delete(Index);
989 Button.Free;
990 FToolItems.Remove(Index);
991 UpdateButtonsTags;
992 Resize;
993 end;
994
995 procedure TKASToolBar.RemoveButton(Button: TKASToolButton);
996 var
997 Index: Integer;
998 begin
999 Index := FindButton(Button);
1000 if Index <> -1 then
1001 RemoveButton(Index);
1002 end;
1003
1004 procedure TKASToolBar.RemoveToolItemExecutor(ExecuteFunction: TOnToolItemExecute);
1005 var
1006 Executor: PToolItemExecutor;
1007 I: Integer;
1008 begin
1009 for I := FToolItemExecutors.Count - 1 downto 0 do
1010 begin
1011 Executor := PToolItemExecutor(FToolItemExecutors[I]);
1012 if (TMethod(Executor^.ToolItemExecute).Code = TMethod(ExecuteFunction).Code) and
1013 (TMethod(Executor^.ToolItemExecute).Data = TMethod(ExecuteFunction).Data) then
1014 begin
1015 Dispose(Executor);
1016 FToolItemExecutors.Delete(I);
1017 end;
1018 end;
1019 end;
1020
1021 procedure TKASToolBar.UncheckAllButtons;
1022 var
1023 I: Integer;
1024 begin
1025 for I:= 0 to ButtonCount - 1 do
1026 Buttons[I].Down:= False;
1027 end;
1028
1029 { TKASToolButton }
1030
1031 procedure TKASToolButton.CalculatePreferredSize(var PreferredWidth,
1032 PreferredHeight: integer; WithThemeSpace: Boolean);
1033 var
1034 TextSize: TSize;
1035 begin
1036 if (Parent = nil) then
1037 inherited
1038 else begin
1039 if ToolBar.IsVertical then
1040 begin
1041 PreferredWidth := ToolBar.FRowWidth;
1042 PreferredHeight := ToolBar.ButtonHeight;
1043 end
1044 else begin
1045 PreferredWidth := ToolBar.ButtonWidth;
1046 PreferredHeight := ToolBar.FRowHeight;
1047 end;
1048 if ShowCaption and (Caption <> EmptyStr) then
1049 begin
1050 // Size to extent of the icon + caption.
1051 TextSize := Canvas.TextExtent(Caption);
1052 PreferredWidth := Max(TextSize.cx + Glyph.Width + 16, PreferredWidth);
1053 PreferredHeight := Max(TextSize.cy + 4, PreferredHeight);
1054 end;
1055 end;
1056 end;
1057
1058 function TKASToolButton.DrawGlyph(ACanvas: TCanvas; const AClient: TRect;
1059 const AOffset: TPoint; AState: TButtonState; ATransparent: Boolean;
1060 BiDiFlags: Longint): TRect;
1061 var
1062 X, Y: Integer;
1063 AWidth : Integer;
1064 begin
1065 Result := inherited DrawGlyph(ACanvas, AClient, AOffset, AState, ATransparent, BiDiFlags);
1066 if Assigned(FOverlay) then
1067 begin
1068 AWidth := FOverlay.Width;
1069 X := AClient.Left + AOffset.X + ToolBar.FGlyphSize - AWidth;
1070 Y := AClient.Top + AOffset.Y + ToolBar.FGlyphSize - AWidth;
1071 Canvas.Draw(X, Y, FOverlay);
1072 end;
1073 end;
1074
1075 procedure TKASToolButton.ActionChange(Sender: TObject; CheckDefaults: Boolean);
1076 begin
1077 if Sender is TCustomAction then
1078 begin
1079 with TCustomAction(Sender) do
1080 begin
1081 if CheckDefaults or (Self.GroupIndex = 0) then
1082 Self.GroupIndex := GroupIndex;
1083 if not CheckDefaults or Enabled then
1084 Self.Enabled := Enabled;
1085 if not CheckDefaults or Visible then
1086 Self.Visible := Visible;
1087 if not CheckDefaults or Checked then
1088 Self.Down := Checked;
1089 end;
1090 end;
1091 end;
1092
1093 procedure TKASToolButton.CMHintShow(var Message: TLMessage);
1094 begin
1095 if (ActionLink <> nil) and FToolItem.ActionHint then
1096 begin
1097 inherited CMHintShow(Message);
1098 end
1099 else begin
1100 DoOnShowHint(TCMHintShow(Message).HintInfo);
1101 end;
1102 end;
1103
1104 constructor TKASToolButton.Create(AOwner: TComponent; Item: TKASToolItem);
1105 begin
1106 inherited Create(AOwner);
1107 FToolItem := Item;
1108 end;
1109
1110 destructor TKASToolButton.Destroy;
1111 begin
1112 inherited Destroy;
1113 FOverlay.Free;
1114 end;
1115
1116 procedure TKASToolButton.Click;
1117 begin
1118 if Assigned(OnClick) then OnClick(Self);
1119 end;
1120
1121 function TKASToolButton.GetToolBar: TKASToolBar;
1122 begin
1123 Result := Parent as TKASToolBar;
1124 end;
1125
1126 { TKASToolDivider }
1127
1128 procedure TKASToolDivider.CalculatePreferredSize(var PreferredWidth,
1129 PreferredHeight: integer; WithThemeSpace: Boolean);
1130 begin
1131 if Assigned(Parent) and (Parent is TKASToolBar) and
1132 (not TKASSeparatorItem(FToolItem).Style) and
1133 (not TKASToolBar(Parent).FShowDividerAsButton) then
1134 begin
1135 if ToolBar.IsVertical then
1136 begin
1137 PreferredHeight := 5;
1138 PreferredWidth := ToolBar.FRowWidth;
1139 end
1140 else begin
1141 PreferredWidth := 5;
1142 PreferredHeight := ToolBar.FRowHeight;
1143 end;
1144 end
1145 else
1146 inherited;
1147 end;
1148
1149 procedure TKASToolDivider.Paint;
1150 var
1151 DividerRect: TRect;
1152 Details: TThemedElementDetails;
1153 begin
1154 if Assigned(Parent) and (Parent is TKASToolBar) and
1155 not TKASToolBar(Parent).FShowDividerAsButton then
1156 begin
1157 if TKASSeparatorItem(FToolItem).Style then Exit;
1158
1159 DividerRect:= ClientRect;
1160
1161 if ToolBar.IsVertical then
1162 begin
1163 Details:= ThemeServices.GetElementDetails(ttbSeparatorVertNormal);
1164 // Theme services have no strict rule to draw divider in the center,
1165 // so we should calculate rectangle here
1166 // on windows 7 divider can't be less than 4 pixels
1167 if (DividerRect.Bottom - DividerRect.Top) > 5 then
1168 begin
1169 DividerRect.Top := (DividerRect.Top + DividerRect.Bottom) div 2 - 3;
1170 DividerRect.Bottom := DividerRect.Top + 5;
1171 end;
1172
1173 if not ThemeServices.ThemesEnabled then
1174 begin
1175 InflateRect(DividerRect, -2, 0);
1176 Canvas.Pen.Color := clBtnShadow;
1177 Canvas.Line(DividerRect.Left, DividerRect.Top + 1, DividerRect.Right, DividerRect.Top + 1);
1178 Canvas.Pen.Color := clBtnHighlight;
1179 Canvas.Line(DividerRect.Left, DividerRect.Top + 2, DividerRect.Right, DividerRect.Top + 2);
1180 Exit;
1181 end;
1182 end
1183 else begin
1184 Details:= ThemeServices.GetElementDetails(ttbSeparatorNormal);
1185 // Theme services have no strict rule to draw divider in the center,
1186 // so we should calculate rectangle here
1187 // on windows 7 divider can't be less than 4 pixels
1188 if (DividerRect.Right - DividerRect.Left) > 5 then
1189 begin
1190 DividerRect.Left := (DividerRect.Left + DividerRect.Right) div 2 - 3;
1191 DividerRect.Right := DividerRect.Left + 5;
1192 end;
1193 end;
1194 ThemeServices.DrawElement(Canvas.GetUpdatedHandle([csBrushValid, csPenValid]), Details, DividerRect);
1195 end
1196 else
1197 inherited Paint;
1198 end;
1199
1200 end.
1201