File: editortoolbar_impl.pas

package info (click to toggle)
lazarus 1.2.4%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 170,220 kB
  • ctags: 115,165
  • sloc: pascal: 1,386,898; xml: 257,878; sh: 2,935; java: 603; makefile: 549; perl: 297; sql: 174; ansic: 137
file content (380 lines) | stat: -rw-r--r-- 8,600 bytes parent folder | download | duplicates (3)
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
{
  Copyright (C) 2007 Graeme Geldenhuys (graemeg@gmail.com)

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}

unit editortoolbar_impl;

{$mode objfpc}{$H+}

interface

uses
  Classes
  ,CodeToolManager
  ,CodeTree
  ,jumpto_impl
  ,Forms
  ,ComCtrls
  ,Controls
  ,Menus
  ,MenuIntf
  ,IDEImagesIntf
  ,SrcEditorIntf
  ,editortoolbar_str
  ;


const
  cSettingsFile = 'editortoolbar.xml';
  cDivider      = '---------------';


type

  { TEditorToolbar }

  TEditorToolbar = class(TComponent)
  private
    FJumpHandler: TJumpHandler;
    FWindow: TSourceEditorWindowInterface;
    TB: TToolbar;
    PM: TPopupMenu;
    CfgButton: TToolButton;
    procedure   CreateEditorToolbar(AW: TForm; var ATB: TToolbar);
    function    CreateJumpItem(AJumpType: TJumpType; O: TComponent): TMenuItem;
    procedure   DoConfigureToolbar(Sender: TObject);
  protected
    procedure   AddButton(AMenuItem: TIDEMenuItem);
    procedure   PositionAtEnd(AToolbar: TToolbar; AButton: TToolButton);
    procedure   Reload;
  public
    constructor Create(AOwner: TComponent); override;
    destructor  Destroy; override;
    procedure   InitEditorToolBar;
    procedure   AddCustomItems;
    procedure   AddDivider;
    procedure   AddStaticItems;
    procedure   ClearToolbar;
  end;
  

  { TEditorToolbarList }

  TEditorToolbarList = class
  private
    FToolBarList: TFPList;
  protected
    procedure SourceWindowCreated(Sender: TObject);
    procedure AddBar(ABar: TEditorToolbar);
    procedure DelBar(ABar: TEditorToolbar);
    procedure ReloadAll;
  public
    constructor Create;
    destructor Destroy; override;
  end;


procedure Register;

implementation

uses
  LazIDEIntf
  ,CustomCodeTool
  ,Dialogs
  ,SysUtils
  ,LResources
  ,EdtTbConfigFrm
  ,LazConfigStorage
  ,BaseIDEIntf
  ;

type

  { TEditToolBarToolButton }

  TEditToolBarToolButton = class(TToolButton)
  private
    FMenuItem: TIDEMenuItem;
  protected
    procedure Click; override;
  public
    property MenuItem: TIDEMenuItem read FMenuItem write FMenuItem;
  end;

var
  uEditorToolbarList: TEditorToolbarList;

procedure TEditToolBarToolButton.Click;
begin
  inherited Click;
  if assigned(FMenuItem) then
    FMenuItem.TriggerClick;
end;

{ TEditorToolbarList }

procedure TEditorToolbarList.SourceWindowCreated(Sender: TObject);
begin
  TEditorToolbar.Create(Sender as TSourceEditorWindowInterface);
end;

procedure TEditorToolbarList.AddBar(ABar: TEditorToolbar);
begin
  FToolBarList.Add(ABar);
end;

procedure TEditorToolbarList.DelBar(ABar: TEditorToolbar);
begin
  FToolBarList.Remove(ABar);
end;

procedure TEditorToolbarList.ReloadAll;
var
  i: Integer;
begin
  for i := 0 to FToolBarList.Count - 1 do
    TEditorToolbar(FToolBarList[i]).Reload;
end;

constructor TEditorToolbarList.Create;
begin
  inherited;
  uEditorToolbarList := self;
  FToolBarList := TFPList.Create;

  if SourceEditorManagerIntf <> nil then
    SourceEditorManagerIntf.RegisterChangeEvent(semWindowCreate, @SourceWindowCreated);

end;

destructor TEditorToolbarList.Destroy;
begin
  while FToolBarList.Count > 0 do
    TEditorToolbar(FToolBarList[0]).Free;
  FreeAndNil(FToolBarList);
  inherited Destroy;
end;

{ TEditorToolbar }

procedure TEditorToolbar.CreateEditorToolbar(AW: TForm; var ATB: TToolbar);
begin
  ATB := TToolbar.Create(AW);
  ATB.Parent   := AW;
  ATB.Height   := 26;
  ATB.Align    := alTop;
  ATB.Flat     := True;
  ATB.Images   := IDEImages.Images_16;
  ATB.ShowHint := True;
end;

function TEditorToolbar.CreateJumpItem(AJumpType: TJumpType; O: TComponent): TMenuItem;
begin
  Result := TMenuItem.Create(O);
  Result.Tag      := Ord(AJumpType);
  Result.OnClick  := @FJumpHandler.DoJump;
  Result.Caption  := cJumpNames[AJumpType];
end;

procedure TEditorToolbar.DoConfigureToolbar(Sender: TObject);
begin
  if TEdtTbConfigForm.Execute then
    uEditorToolbarList.ReloadAll;
end;

constructor TEditorToolbar.Create(AOwner: TComponent);
var
  T: TJumpType;
begin
  uEditorToolbarList.AddBar(Self);
  if assigned(TB) then exit;

  FJumpHandler := TJumpHandler.Create(nil);
  FWindow := TSourceEditorWindowInterface(AOwner);
  CreateEditorToolBar(FWindow, TB);

  PM := TPopupMenu.Create(FWindow);
  for T := Low(TJumpType) to High(TJumpType) do
    PM.Items.Add(CreateJumpItem(T, FWindow));

  AddStaticItems;
  AddCustomItems;
end;

destructor TEditorToolbar.Destroy;
begin
  uEditorToolbarList.DelBar(Self);
  FJumpHandler.Free;
  inherited Destroy;
end;

procedure TEditorToolbar.InitEditorToolBar;
begin
  TB := nil;
  CfgButton := nil;
end;

procedure TEditorToolbar.AddButton(AMenuItem: TIDEMenuItem);
var
  B: TEditToolBarToolButton;
begin
  B := TEditToolBarToolButton.Create(TB);
  B.Caption     := AMenuItem.Caption;
  B.Hint        := AMenuItem.Caption; // or should we use AMenuItem.Hint?
  // If we have a image, us it. Otherwise supply a default.
  if AMenuItem.ImageIndex <> -1 then
    B.ImageIndex := AMenuItem.ImageIndex
  else
    B.ImageIndex  := IDEImages.LoadImage(16, 'execute16');

  B.Style       := tbsButton;
  B.MenuItem := AMenuItem;
  //B.OnClick     := AMenuItem.OnClick;
  PositionAtEnd(TB, B);
end;

// position the button next to the last button
procedure TEditorToolbar.PositionAtEnd(AToolbar: TToolbar; AButton: TToolButton);
var
  SiblingButton: TToolButton;
begin
  if AToolbar.ButtonCount > 0 then
  begin
    SiblingButton := AToolbar.Buttons[AToolbar.ButtonCount-1];
    AButton.SetBounds(SiblingButton.Left + SiblingButton.Width,
      SiblingButton.Top, AButton.Width, AButton.Height);
  end;
  AButton.Parent := AToolbar;
end;

procedure TEditorToolbar.Reload;
begin
  ClearToolbar;
  AddStaticItems;
  AddCustomItems;
end;

procedure TEditorToolbar.AddCustomItems;
var
  i: integer;
  c: integer;
  cfg: TConfigStorage;
  value: string;
  mi: TIDEMenuItem;
begin
  cfg := GetIDEConfigStorage(cSettingsFile, True);
  TB.BeginUpdate;
  try
    c := cfg.GetValue('Count', 0);
    for i := 1 to c do
    begin
      value := cfg.GetValue('Button' + Format('%2.2d', [i]) + '/Value', '');
       if value <> '' then
       begin
        if value = cDivider then
          AddDivider
        else
        begin
          mi := IDEMenuRoots.FindByPath(value,false);
          if Assigned(mi) then
            AddButton(mi);
        end;
       end;
  end;
  finally
    cfg.Free;
    TB.EndUpdate;
  end;
end;

procedure TEditorToolbar.AddDivider;
var
  B: TToolButton;
begin
  B := TToolbutton.Create(TB);
  B.Style := tbsDivider;
  PositionAtEnd(TB, B);
end;

procedure TEditorToolbar.AddStaticItems;
var
  B: TToolButton;  
begin
  TB.BeginUpdate;
  try
    // Config Button
    if CfgButton = nil then
      CfgButton := TToolbutton.Create(TB);
    CfgButton.Caption     := rsConfigureToo;
    CfgButton.Hint        := CfgButton.Caption;
    CfgButton.ImageIndex  := IDEImages.LoadImage(16, 'preferences16');
    CfgButton.Style       := tbsButton;
    CfgButton.OnClick     := @DoConfigureToolbar;
    PositionAtEnd(TB, CfgButton);
    
    AddDivider;
    
    // JumpTo Button
    B := TToolbutton.Create(TB);
    B.Caption       := rsJumpTo;
    B.Hint          := B.Caption;
    B.ImageIndex    := IDEImages.LoadImage(16, 'jumpto16');
    B.Style         := tbsDropDown;
    B.OnClick       := @FJumpHandler.DoJumpToImplementation;
    B.DropdownMenu  := PM;
    PositionAtEnd(TB, B);

    if TB.ButtonCount <> 0 then
      AddDivider;
  finally
    TB.EndUpdate;
  end;
end;

procedure TEditorToolbar.ClearToolbar;
var
  i: integer;
begin
  TB.BeginUpdate;
  try
    for i := TB.ButtonCount - 1 downto 0 do
      if TB.Buttons[i] <> CfgButton then
        TB.Buttons[i].Free
      else
        TB.Buttons[i].Parent := nil;
  finally
    TB.EndUpdate;
  end;
end;

procedure Register;
begin
  if uEditorToolbarList = nil then
    TEditorToolbarList.Create;
end;


initialization
  uEditorToolbarList := nil;
  {$I toolbar.lrs}    // all required images

finalization
  uEditorToolbarList.Free;

end.