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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
|
{
*****************************************************************************
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
Author: Ondrej Pokorny
Abstract:
Interface to the IDE toolbars.
}
unit ToolBarIntf;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils,
// LCL
Controls, ComCtrls, Menus, Forms,
// IdeIntf
IDECommands, MenuIntf, IDEImagesIntf, SrcEditorIntf;
type
TIDEToolButton = class;
TIDEToolButtonClass = class of TIDEToolButton;
TIDEToolButtons = class;
TIDEButtonCommand = class(TIDESpecialCommand)
private
FToolButtonClass: TIDEToolButtonClass;
FToolButtons: TIDEToolButtons;
protected
procedure SetName(const aName: string); override;
procedure SetEnabled(const aEnabled: Boolean); override;
procedure SetChecked(const aChecked: Boolean); override;
procedure SetCaption(aCaption: string); override;
procedure SetHint(const aHint: string); override;
procedure SetImageIndex(const aImageIndex: Integer); override;
procedure SetResourceName(const aResourceName: string); override;
procedure ShortCutsUpdated(const aShortCut, aShortCutKey2: TShortCut); override;
public
procedure ToolButtonAdded(const aBtn: TIDEToolButton);
public
constructor Create(const TheName: string); override;
destructor Destroy; override;
public
property ToolButtonClass: TIDEToolButtonClass read FToolButtonClass write FToolButtonClass;
property ToolButtons: TIDEToolButtons read FToolButtons;
end;
{ TIDEToolButton }
TIDEToolButton = class(TToolButton)
private
FItem: TIDEButtonCommand;
protected
procedure DoOnShowHint(HintInfo: PHintInfo); override;
public
procedure DoOnAdded; virtual;
procedure Click; override;
property Item: TIDEButtonCommand read FItem write FItem;
end;
{ TIDEToolButtonWithArrow }
TIDEToolButtonWithArrow = class(TIDEToolButton)
protected
procedure AddMenuItem(ACommand: TIDEMenuCommand); virtual;
procedure AddMenuItems(ACommands: array of TIDEMenuCommand);
procedure DoOnMenuItemClick(Sender: TObject);
procedure DoOnMenuPopup(Sender: TObject);
procedure RefreshMenu; virtual;
public
constructor Create(AOwner: TComponent); override;
end;
{ TIDEToolButton_ButtonDrop }
TIDEToolButton_ButtonDrop = class(TIDEToolButtonWithArrow)
protected
procedure PopUpAloneMenu;
public
procedure DoOnAdded; override;
end;
{ TIDEToolButton_DropDown }
TIDEToolButton_DropDown = class(TIDEToolButtonWithArrow)
public
procedure DoOnAdded; override;
end;
TIDEToolButtonCategory = class
private
FButtons: TFPList;
FDescription: string;
FName: string;
function GetButtons(Index: Integer): TIDEButtonCommand;
public
constructor Create;
destructor Destroy; override;
public
property Description: string read FDescription write FDescription;
property Name: string read FName write FName;
function ButtonCount: Integer;
property Buttons[Index: Integer]: TIDEButtonCommand read GetButtons; default;
end;
TIDEToolButtonCategories = class
private
FButtonNames: TStringList;
FCategories: TStringList;
function GetItems(Index: Integer): TIDEToolButtonCategory;
public
constructor Create;
destructor Destroy; override;
public
function Count: Integer;
function AddButton(const aCategory: TIDEToolButtonCategory; const aName: string;
const aCommand: TIDECommand): TIDEButtonCommand; overload;
function AddButton(const aCommand: TIDECommand): TIDEButtonCommand; overload;
function FindCategory(const aName: string): TIDEToolButtonCategory;
function FindCreateCategory(const aName, aDescription: string): TIDEToolButtonCategory;
function FindItemByName(const aName: string): TIDEButtonCommand;
function FindItemByMenuPathOrName(var aName: string): TIDEButtonCommand;
function FindItemByCommand(const aCommand: TIDECommand): TIDEButtonCommand;
function FindItemByCommand(const aCommand: Word): TIDEButtonCommand;
property Items[Index: Integer]: TIDEToolButtonCategory read GetItems; default;
end;
TIDEToolButtonsEnumerator = class
private
FList: TIDEToolButtons;
FPosition: Integer;
public
constructor Create(AButtons: TIDEToolButtons);
function GetCurrent: TIDEToolButton;
function MoveNext: Boolean;
property Current: TIDEToolButton read GetCurrent;
end;
TIDEToolButtons = class(TComponent)
private
FList: TFPList;
function GetCount: Integer;
function GetItems(Index: Integer): TIDEToolButton;
protected
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
public
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
public
function GetEnumerator: TIDEToolButtonsEnumerator;
procedure Add(const aBtn: TIDEToolButton);
property Count: Integer read GetCount;
property Items[Index: Integer]: TIDEToolButton read GetItems; default;
end;
var
IDEToolButtonCategories: TIDEToolButtonCategories = nil;// created by the IDE
function RegisterIDEButtonCategory(const aName, aDescription: string): TIDEToolButtonCategory;
function RegisterIDEButtonCommand(const aCategory: TIDEToolButtonCategory; const aName: string;
const aCommand: TIDECommand): TIDEButtonCommand;
function RegisterIDEButtonCommand(const aCommand: TIDECommand): TIDEButtonCommand;
implementation
function RegisterIDEButtonCategory(const aName, aDescription: string): TIDEToolButtonCategory;
begin
Result := IDEToolButtonCategories.FindCreateCategory(aName, aDescription);
end;
function RegisterIDEButtonCommand(const aCategory: TIDEToolButtonCategory;
const aName: string; const aCommand: TIDECommand): TIDEButtonCommand;
begin
Result := IDEToolButtonCategories.AddButton(aCategory, aName, aCommand);
end;
function RegisterIDEButtonCommand(const aCommand: TIDECommand): TIDEButtonCommand;
begin
Result := IDEToolButtonCategories.AddButton(aCommand);
end;
{ TIDEToolButtonWithArrow }
constructor TIDEToolButtonWithArrow.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
DropdownMenu := TPopupMenu.Create(Self);
DropdownMenu.Images := IDEImages.Images_16;
DropdownMenu.OnPopup := @DoOnMenuPopup;
end;
procedure TIDEToolButtonWithArrow.AddMenuItem(ACommand: TIDEMenuCommand);
var
Itm: TMenuItem;
begin
Itm := TMenuItem.Create(DropdownMenu);
Itm.Caption := ACommand.Caption;
Itm.ShortCut := ACommand.Command.AsShortCut;
Itm.ImageIndex := ACommand.ImageIndex;
Itm.Enabled := ACommand.Enabled;
Itm.OnClick := @DoOnMenuItemClick;
Itm.Tag := PtrInt(ACommand);
DropdownMenu.Items.Add(Itm);
end;
procedure TIDEToolButtonWithArrow.AddMenuItems(ACommands: array of TIDEMenuCommand);
var
Cmd: TIDEMenuCommand;
begin
for Cmd in ACommands do
AddMenuItem(Cmd);
end;
procedure TIDEToolButtonWithArrow.DoOnMenuPopup(Sender: TObject);
begin
DropdownMenu.Items.Clear;
RefreshMenu;
end;
procedure TIDEToolButtonWithArrow.RefreshMenu;
begin
{ Override this method in descendants.
DropdownMenu fully regenerates for every showing on OnPopup event.
So, RefreshMenu calling happens:
- On click to arrow (Style=tbsDropDown)
- On click to button (Style=tbsButtonDrop)
- On popup alone menu in static methods (on shortcuts)
of TIDEToolButton_ButtonDrop descendants (Style is not matter)
At calling time:
- Instance of DropdownMenu exists
- DropdownMenu is empty }
end;
procedure TIDEToolButtonWithArrow.DoOnMenuItemClick(Sender: TObject);
var
Cmd: TIDEMenuCommand;
begin
Cmd:=TIDEMenuCommand((Sender as TMenuItem).Tag);
Cmd.DoOnClick; // Sender in handler should be a command but not a menu item
end;
{ TIDEToolButton_ButtonDrop }
procedure TIDEToolButton_ButtonDrop.DoOnAdded;
begin
Style := tbsButtonDrop; // not in constructor
end;
procedure TIDEToolButton_ButtonDrop.PopUpAloneMenu;
var
ActiveEditor: TSourceEditorInterface;
ScreenXY: TPoint;
begin
ActiveEditor := SourceEditorManagerIntf.ActiveEditor;
if ActiveEditor=nil then
Exit;
ScreenXY := ActiveEditor.EditorControl.ClientToScreen(Point(0, 0));
DropdownMenu.PopUp(ScreenXY.X, ScreenXY.Y);
end;
{ TIDEToolButton_DropDown }
procedure TIDEToolButton_DropDown.DoOnAdded;
begin
Style := tbsDropDown; // not in constructor
end;
{ TIDEToolButtonsEnumerator }
constructor TIDEToolButtonsEnumerator.Create(AButtons: TIDEToolButtons);
begin
inherited Create;
FList := AButtons;
FPosition := -1;
end;
function TIDEToolButtonsEnumerator.GetCurrent: TIDEToolButton;
begin
Result := FList[FPosition];
end;
function TIDEToolButtonsEnumerator.MoveNext: Boolean;
begin
Inc(FPosition);
Result := FPosition < FList.Count;
end;
{ TIDEToolButtons }
procedure TIDEToolButtons.Add(const aBtn: TIDEToolButton);
begin
FList.Add(aBtn);
aBtn.FreeNotification(Self);
end;
constructor TIDEToolButtons.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
FList := TFPList.Create;
end;
destructor TIDEToolButtons.Destroy;
var
I: Integer;
begin
for I := 0 to Count-1 do
Items[I].RemoveFreeNotification(Self);
FList.Free;
inherited Destroy;
end;
function TIDEToolButtons.GetCount: Integer;
begin
Result := FList.Count;
end;
function TIDEToolButtons.GetEnumerator: TIDEToolButtonsEnumerator;
begin
Result := TIDEToolButtonsEnumerator.Create(Self);
end;
function TIDEToolButtons.GetItems(Index: Integer): TIDEToolButton;
begin
Result := TIDEToolButton(FList[Index]);
end;
procedure TIDEToolButtons.Notification(AComponent: TComponent; Operation: TOperation);
var
xIndex: Integer;
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) then
begin
xIndex := FList.IndexOf(AComponent);
if xIndex >= 0 then
FList.Delete(xIndex);
end;
end;
{ TIDEButtonCommand }
constructor TIDEButtonCommand.Create(const TheName: string);
begin
inherited Create(TheName);
FToolButtonClass := TIDEToolButton;
FToolButtons := TIDEToolButtons.Create(nil);
end;
destructor TIDEButtonCommand.Destroy;
begin
FToolButtons.Free;
inherited Destroy;
end;
procedure TIDEButtonCommand.SetEnabled(const aEnabled: Boolean);
var
xBtn: TIDEToolButton;
begin
inherited SetEnabled(aEnabled);
for xBtn in FToolButtons do
xBtn.Enabled:=Enabled;
end;
procedure TIDEButtonCommand.SetHint(const aHint: string);
var
xBtn: TIDEToolButton;
begin
inherited SetHint(aHint);
for xBtn in FToolButtons do
xBtn.Hint:=GetHintOrCaptionWithShortCut;
end;
procedure TIDEButtonCommand.SetImageIndex(const aImageIndex: Integer);
var
xBtn: TIDEToolButton;
begin
inherited SetImageIndex(aImageIndex);
for xBtn in FToolButtons do
xBtn.ImageIndex:=ImageIndex;
end;
procedure TIDEButtonCommand.SetName(const aName: string);
var
i: Integer;
begin
if Name=aName then Exit;
i:=IDEToolButtonCategories.FButtonNames.IndexOf(Name);
inherited SetName(aName);
if (i>=0) and (IDEToolButtonCategories.FButtonNames.Objects[i] = Self) then
begin
IDEToolButtonCategories.FButtonNames.Delete(i);
IDEToolButtonCategories.FButtonNames.AddObject(aName, Self);
end;
end;
procedure TIDEButtonCommand.SetCaption(aCaption: string);
var
xBtn: TIDEToolButton;
begin
inherited SetCaption(aCaption);
for xBtn in FToolButtons do
xBtn.Hint:=GetHintOrCaptionWithShortCut;
end;
procedure TIDEButtonCommand.SetChecked(const aChecked: Boolean);
var
xBtn: TIDEToolButton;
begin
inherited SetChecked(aChecked);
for xBtn in FToolButtons do
xBtn.Down:=Checked;
end;
procedure TIDEButtonCommand.SetResourceName(const aResourceName: string);
var
xBtn: TIDEToolButton;
begin
inherited SetResourceName(aResourceName);
for xBtn in FToolButtons do
xBtn.ImageIndex:=ImageIndex;
end;
procedure TIDEButtonCommand.ShortCutsUpdated(const aShortCut, aShortCutKey2: TShortCut);
var
xBtn: TIDEToolButton;
begin
inherited ShortCutsUpdated(aShortCut, aShortCutKey2);
for xBtn in FToolButtons do
xBtn.Hint:=GetHintOrCaptionWithShortCut;
end;
procedure TIDEButtonCommand.ToolButtonAdded(const aBtn: TIDEToolButton);
begin
FToolButtons.Add(aBtn);
aBtn.DoOnAdded;
end;
{ TIDEToolButtonCategory }
function TIDEToolButtonCategory.ButtonCount: Integer;
begin
Result := FButtons.Count;
end;
constructor TIDEToolButtonCategory.Create;
begin
FButtons := TFPList.Create;
end;
destructor TIDEToolButtonCategory.Destroy;
var
i: Integer;
begin
for i := 0 to ButtonCount-1 do
Buttons[i].Free;
FButtons.Free;
inherited Destroy;
end;
function TIDEToolButtonCategory.GetButtons(Index: Integer): TIDEButtonCommand;
begin
Result := TIDEButtonCommand(FButtons[Index]);
end;
{ TIDEToolButtonCategories }
function TIDEToolButtonCategories.AddButton(const aCommand: TIDECommand): TIDEButtonCommand;
var
xCategory: TIDEToolButtonCategory;
begin
Assert(aCommand<>nil, 'TIDEToolButtonCategories.AddButton: aCommand=nil');
xCategory := RegisterIDEButtonCategory(aCommand.Category.Name, aCommand.Category.Description);
Result := RegisterIDEButtonCommand(xCategory, aCommand.Name, aCommand);
end;
function TIDEToolButtonCategories.AddButton(
const aCategory: TIDEToolButtonCategory; const aName: string;
const aCommand: TIDECommand): TIDEButtonCommand;
begin
Result := FindItemByName(aName);
if Result=nil then
begin
Result := TIDEButtonCommand.Create(aName);
FButtonNames.AddObject(aName, Result);
aCategory.FButtons.Add(Result);
Result.Command:=aCommand;
end;
end;
function TIDEToolButtonCategories.Count: Integer;
begin
Result := FCategories.Count;
end;
constructor TIDEToolButtonCategories.Create;
begin
FButtonNames := TStringList.Create;
FButtonNames.Sorted := True;
FButtonNames.Duplicates := dupIgnore;
FCategories := TStringList.Create;
FCategories.Sorted := True;
FCategories.Duplicates := dupIgnore;
FCategories.OwnsObjects := True;
end;
destructor TIDEToolButtonCategories.Destroy;
begin
FButtonNames.Free;
FCategories.Free;
inherited Destroy;
end;
function TIDEToolButtonCategories.FindCategory(const aName: string
): TIDEToolButtonCategory;
var
i: Integer;
begin
i := FCategories.IndexOf(aName);
if (i>=0) and (FCategories.Objects[i]<>nil) then
Result := FCategories.Objects[i] as TIDEToolButtonCategory
else
Result := nil;
end;
function TIDEToolButtonCategories.FindItemByMenuPathOrName(var aName: string
): TIDEButtonCommand;
var
xMI: TIDEMenuItem;
begin
Result := FindItemByName(aName);
if Result<>nil then Exit;
//find by path from aName (backwards compatibility)
xMI := IDEMenuRoots.FindByPath(aName, False);
if Assigned(xMI) and Assigned(xMI.Command) then
begin
Result := FindItemByCommand(xMI.Command);
if Assigned(Result) then
aName := xMI.Command.Name;
end;
end;
function TIDEToolButtonCategories.FindCreateCategory(const aName,
aDescription: string): TIDEToolButtonCategory;
var
i: Integer;
begin
i := FCategories.IndexOf(aName);
if (i>=0) and (FCategories.Objects[i]<>nil) then
Result := FCategories.Objects[i] as TIDEToolButtonCategory
else
begin
Result := TIDEToolButtonCategory.Create;
Result.Name := aName;
Result.Description := aDescription;
FCategories.AddObject(aName, Result);
end;
end;
function TIDEToolButtonCategories.FindItemByCommand(const aCommand: TIDECommand
): TIDEButtonCommand;
var
i, l: Integer;
begin
for i := 0 to Count-1 do
for l := 0 to Items[i].ButtonCount-1 do
if Items[i].Buttons[l].Command = aCommand then
Exit(Items[i].Buttons[l]);
Result := nil;
end;
function TIDEToolButtonCategories.FindItemByCommand(const aCommand: Word
): TIDEButtonCommand;
var
i, l: Integer;
begin
for i := 0 to Count-1 do
for l := 0 to Items[i].ButtonCount-1 do
if Items[i].Buttons[l].Command.Command = aCommand then
Exit(Items[i].Buttons[l]);
Result := nil;
end;
function TIDEToolButtonCategories.FindItemByName(const aName: string
): TIDEButtonCommand;
var
i: Integer;
begin
i := FButtonNames.IndexOf(aName);
if (i>=0) and (FButtonNames.Objects[i]<>nil) then
Result := FButtonNames.Objects[i] as TIDEButtonCommand
else
Result := nil;
end;
function TIDEToolButtonCategories.GetItems(Index: Integer): TIDEToolButtonCategory;
begin
Result := TIDEToolButtonCategory(FCategories.Objects[Index]);
end;
{ TIDEToolButton }
procedure TIDEToolButton.Click;
begin
inherited Click;
if Assigned(FItem) then
FItem.DoOnClick;
end;
procedure TIDEToolButton.DoOnAdded;
begin
//override in descendants
end;
procedure TIDEToolButton.DoOnShowHint(HintInfo: PHintInfo);
begin
inherited DoOnShowHint(HintInfo);
if Assigned(FItem) and FItem.DoOnRequestCaption(Self) then
HintInfo^.HintStr := FItem.GetHintOrCaptionWithShortCut;
end;
end.
|