OSDN Git Service

9d3467f58ec021aaac590b697efa0c3a8f553322
[gikonavigoeson/gikonavi.git] / ToolBarSetting.pas
1 unit ToolBarSetting;
2
3 interface
4
5 uses
6         Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7         Dialogs, StdCtrls, ExtCtrls, ComCtrls, ActnList, IniFiles,
8         GikoSystem, ToolBarUtil;
9
10 type
11         TGikoToolType = (gttStandard, gttList, gttBrowser);
12
13         TToolBarItem = class
14         private
15                 FToolBar: TToolBar;
16                 FButtonActionList: TList;
17                 FToolType: TGikoToolType;
18         public
19                 constructor Create;
20                 destructor Destroy; override;
21                 property ToolBar: TToolBar read FToolBar write FToolBar;
22                 property ButtonActionList: TList read FButtonActionList write FButtonActionList;
23                 property ToolType: TGikoToolType read FToolType write FToolType;
24         end;
25
26         TToolBarSettingDialog = class(TForm)
27                 Label1: TLabel;
28                 AllListView: TListView;
29                 AddButton: TButton;
30                 RemoveButton: TButton;
31                 CurrentListView: TListView;
32                 Label2: TLabel;
33                 UpButton: TButton;
34                 DownButton: TButton;
35                 OKButton: TButton;
36                 CancelButton: TButton;
37                 Bevel1: TBevel;
38                 Label3: TLabel;
39                 ToolBarComboBox: TComboBox;
40                 SeparatorAddButton: TButton;
41                 ResetButton: TButton;
42                 procedure FormCreate(Sender: TObject);
43                 procedure FormDestroy(Sender: TObject);
44                 procedure ToolBarComboBoxChange(Sender: TObject);
45                 procedure OKButtonClick(Sender: TObject);
46                 procedure CurrentListViewData(Sender: TObject; Item: TListItem);
47                 procedure AllListViewData(Sender: TObject; Item: TListItem);
48                 procedure UpButtonClick(Sender: TObject);
49                 procedure DownButtonClick(Sender: TObject);
50                 procedure ResetButtonClick(Sender: TObject);
51                 procedure CurrentListViewChange(Sender: TObject; Item: TListItem;
52                         Change: TItemChange);
53                 procedure AllListViewChange(Sender: TObject; Item: TListItem;
54                         Change: TItemChange);
55                 procedure AddButtonClick(Sender: TObject);
56                 procedure RemoveButtonClick(Sender: TObject);
57                 procedure SeparatorAddButtonClick(Sender: TObject);
58                 procedure FormShow(Sender: TObject);
59         private
60                 { Private \90é\8c¾ }
61                 FActionList: TActionList;
62                 FAllList: TList;
63                 procedure CreateListData(ToolBarItem: TToolBarItem);
64                 procedure MoveItem(Offset: Integer);
65                 procedure Sort;
66                 function SetDefaultItem(deflist: array of string; ToolBarItem: TToolBarItem): Integer;
67         public
68                 { Public \90é\8c¾ }
69                 constructor Create(AOwner: TComponent; ActionList: TActionList); reintroduce; overload; virtual;
70                 procedure AddToolBar(ToolBar: TToolBar; ToolType: TGikoToolType);
71         end;
72
73 var
74         ToolBarSettingDialog: TToolBarSettingDialog;
75
76 function CompareCategory(Item1, Item2: Pointer): Integer;
77
78 implementation
79
80 //const
81 //      //\8bæ\90Ø\82è\95\8e\9a
82 //      SEPARATOR_TEXT = '- \8bæ\90Ø\82è -';
83
84 {$R *.dfm}
85
86 //
87 // TToolBarItem
88 //
89 constructor TToolBarItem.Create;
90 begin
91         inherited Create;
92         FButtonActionList := TList.Create;
93 end;
94
95 destructor TToolBarItem.Destroy;
96 begin
97         FButtonActionList.Free;
98         inherited Destroy;
99 end;
100
101 //
102 // TToolBarSettingDialog
103 //
104 constructor TToolBarSettingDialog.Create(AOwner: TComponent; ActionList: TActionList);
105 begin
106         inherited Create(AOwner);
107         FActionList := ActionList;
108 end;
109
110 //\83t\83H\81[\83\80\8dì\90¬
111 procedure TToolBarSettingDialog.FormCreate(Sender: TObject);
112 begin
113         FAllList := TList.Create;
114 end;
115
116 //\83t\83H\81[\83\80\94j\8aü
117 procedure TToolBarSettingDialog.FormDestroy(Sender: TObject);
118 var
119         i: Integer;
120 begin
121         FAllList.Free;
122         for i := 0 to ToolBarComboBox.Items.Count - 1 do
123                 ToolBarComboBox.Items.Objects[i].Free;
124 end;
125
126 //\95\\8e¦\82µ\82½\82Æ\82«
127 procedure TToolBarSettingDialog.FormShow(Sender: TObject);
128 begin
129         ToolBarComboBox.ItemIndex := 0;
130         ToolBarComboBoxChange(Self);
131         AllListViewChange(Sender, nil, ctState);
132         CurrentListViewChange(Sender, nil, ctState);
133 end;
134
135 //\82±\82Ì\83_\83C\83A\83\8d\83O\82Å\83J\83X\83^\83}\83C\83Y\82·\82é\83c\81[\83\8b\83o\81[\82ð\93o\98^\82·\82é
136 procedure TToolBarSettingDialog.AddToolBar(ToolBar: TToolBar; ToolType: TGikoToolType);
137 var
138         ToolBarItem: TToolBarItem;
139         i: Integer;
140 begin
141         ToolBarItem := TToolBarItem.Create;
142         ToolBarItem.ToolBar := ToolBar;
143         ToolBarItem.ToolType := ToolType;
144         for i := 0 to ToolBar.ControlCount - 1 do
145                 if ToolBar.Controls[ i ] is TToolButton Then
146                         ToolBarITem.ButtonActionList.Add(ToolBar.Controls[i].Action);
147         ToolBarComboBox.Items.AddObject(ToolBar.Caption, ToolBarItem);
148 end;
149
150
151 //\83R\83\93\83{\83{\83b\83N\83X\95Ï\8dX\8e\9e
152 procedure TToolBarSettingDialog.ToolBarComboBoxChange(Sender: TObject);
153 var
154         ToolBarItem: TToolBarItem;
155 begin
156         ToolBarItem := TToolBarItem(ToolBarComboBox.Items.Objects[ToolBarComboBox.ItemIndex]);
157         CreateListData(ToolBarItem);
158         AllListView.Items.Count := FAllList.Count;
159         CurrentListView.Items.Count := ToolBarItem.ButtonActionList.Count;
160         Sort;
161         AllListView.Refresh;
162         CurrentListView.Refresh;
163 end;
164
165 //OK\83{\83^\83\93\89\9f\82µ\82½\82Æ\82«
166 procedure TToolBarSettingDialog.OKButtonClick(Sender: TObject);
167 var
168         i: Integer;
169         j: Integer;
170         ToolButton: TToolButton;
171         ToolBarItem: TToolBarItem;
172 begin
173         for i := 0 to ToolBarComboBox.Items.Count - 1 do begin
174                 ToolBarItem := TToolBarItem(ToolBarComboBox.Items.Objects[i]);
175
176                 for j := ToolBarItem.ToolBar.ButtonCount - 1 downto 0 do
177                         ToolBarItem.ToolBar.RemoveControl(ToolBarItem.ToolBar.Buttons[j]);
178
179                 for j := 0 to ToolBarItem.ButtonActionList.Count - 1 do begin
180                         if ToolBarItem.ButtonActionList[j] = nil then begin
181                                 ToolButton := TToolButton.Create(ToolBarItem.ToolBar);
182                                 ToolButton.Style := tbsSeparator;
183                                 ToolButton.Width := 8;
184                                 ToolButton.Left := 10000;
185                                 ToolBarItem.ToolBar.InsertControl(ToolButton);
186                         end else if TObject(ToolBarItem.ButtonActionList[j]) is TCustomAction then begin
187                                 ToolButton := TToolButton.Create(ToolBarItem.ToolBar);
188                                 ToolButton.Action := TCustomAction(ToolBarItem.ButtonActionList[j]);
189                                 ToolButton.Left := 10000;
190                                 SetButtonStyle(ToolBarItem.ButtonActionList[j], ToolButton);
191                                 ToolBarItem.ToolBar.InsertControl(ToolButton);
192                         end;
193                 end;
194         end;
195 end;
196
197 //\91S\83c\81[\83\8b\83{\83^\83\93\83\8a\83X\83g\83r\83\85\81[\82Ì\83f\81[\83^\97v\8b\81\8e\9e
198 procedure TToolBarSettingDialog.AllListViewData(Sender: TObject; Item: TListItem);
199 var
200         Action: TCustomAction;
201 begin
202         if (FAllList.Count <= 0) or (FAllList.Count <= Item.Index) then
203                 Exit;
204         if FAllList[Item.Index] = nil then begin
205                 Item.Caption := SEPARATOR_TEXT;
206                 Item.ImageIndex := -1;
207                 Item.Data := nil;
208         end else if TObject(FAllList[Item.Index]) is TCustomAction then begin
209                 Action := TCustomAction(FAllList[Item.Index]);
210                 Item.Caption := Action.Hint;
211                 Item.ImageIndex := Action.ImageIndex;
212                 Item.Data := Action;
213         end;
214 end;
215
216 //\8c»\8dÝ\83c\81[\83\8b\83{\83^\83\93\83\8a\83X\83g\83r\83\85\81[\82Ì\83f\81[\83^\97v\8b\81\8e\9e
217 procedure TToolBarSettingDialog.CurrentListViewData(Sender: TObject; Item: TListItem);
218 var
219         Action: TCustomAction;
220         ToolBarItem: TToolBarItem;
221 begin
222         ToolBarItem := TToolBarItem(ToolBarComboBox.Items.Objects[ToolBarComboBox.ItemIndex]);
223
224         if (ToolBarItem.ButtonActionList.Count <= 0) or (ToolBarItem.ButtonActionList.Count <= Item.Index) then
225                 Exit;
226         if ToolBarItem.ButtonActionList[Item.Index] = nil then begin
227                 Item.Caption := SEPARATOR_TEXT;
228                 Item.ImageIndex := -1;
229                 Item.Data := nil;
230         end else if TObject(ToolBarItem.ButtonActionList[Item.Index]) is TCustomAction then begin
231                 Action := TCustomAction(ToolBarItem.ButtonActionList[Item.Index]);
232                 Item.Caption := Action.Hint;
233                 Item.ImageIndex := Action.ImageIndex;
234                 Item.Data := Action;
235         end;
236 end;
237
238 //\8eg\97p\82Å\82«\82é\83c\81[\83\8b\83{\83^\83\93\83\8a\83X\83g\83r\83\85\81[\82É\95\\8e¦\82·\82é\83f\81[\83^\82ð\8dì\90¬
239 procedure TToolBarSettingDialog.CreateListData(ToolBarItem: TToolBarItem);
240 var
241         i: Integer;
242         Category: string;
243 begin
244         FAllList.Clear;
245         for i := 0 to FActionList.ActionCount - 1 do begin
246                 if FActionList.Actions[i].Tag = -1 then
247                         Continue;
248                 Category := FActionList.Actions[i].Category;
249                 //\95W\8f\80\83c\81[\83\8b\83o\81[\82Í\81u\94Â\81v\81u\83X\83\8c\83b\83h\81v\88È\8aO\82Ì\82Ý\91Î\8fÛ
250                 if (ToolBarItem.ToolType = gttStandard) and ((Category = '\94Â') or (Category = '\83X\83\8c\83b\83h')) then
251                         Continue;
252                 //\83\8a\83X\83g\83c\81[\83\8b\83o\81[\82Í\81u\94Â\81v\82Ì\82Ý\91Î\8fÛ
253                 if (ToolBarItem.ToolType = gttList) and (Category <> '\94Â') then
254                         Continue;
255                 //\83u\83\89\83E\83U\83c\81[\83\8b\83o\81[\82Í\81u\83X\83\8c\83b\83h\81v\82Ì\82Ý\91Î\8fÛ
256                 if (ToolBarItem.ToolType = gttBrowser) and (Category <> '\83X\83\8c\83b\83h') then
257                         Continue;
258
259                 if ToolBarItem.ButtonActionList.IndexOf(FActionList.Actions[i]) = -1 then
260                         FAllList.Add(FActionList.Actions[i]);
261         end;
262 end;
263
264 //\8fã\82Ö\83{\83^\83\93\89\9f\82µ\82½\82Æ\82«
265 procedure TToolBarSettingDialog.UpButtonClick(Sender: TObject);
266 begin
267         MoveItem(-1);
268 end;
269
270 //\89º\82Ö\83{\83^\83\93\89\9f\82µ\82½\82Æ\82«
271 procedure TToolBarSettingDialog.DownButtonClick(Sender: TObject);
272 begin
273         MoveItem(1);
274 end;
275
276 //\83\8a\83Z\83b\83g\83{\83^\83\93\89\9f\82µ\82½\82Æ\82«
277 procedure TToolBarSettingDialog.ResetButtonClick(Sender: TObject);
278 var
279         ToolBarItem: TToolBarItem;
280         cnt: Integer;
281 begin
282         cnt := 0;
283         ToolBarItem := TToolBarItem(ToolBarComboBox.Items.Objects[ToolBarComboBox.ItemIndex]);
284         AllListView.Items.Count := 0;
285         CurrentListView.Items.Count := 0;
286         ToolBarItem.ButtonActionList.Clear;
287         case ToolBarComboBox.ItemIndex of
288                 0: cnt := SetDefaultItem(DEF_STANDARD, ToolBarItem);
289                 1: cnt := SetDefaultItem(DEF_LIST, ToolBarItem);
290                 2: cnt := SetDefaultItem(DEF_BROWSER, ToolBarItem);
291         end;
292         CreateListData(ToolBarItem);
293         AllListView.Items.Count := FAllList.Count;
294         CurrentListView.Items.Count := cnt;
295         Sort;
296         AllListView.Refresh;
297         CurrentListView.Refresh;
298 end;
299
300 function TToolBarSettingDialog.SetDefaultItem(deflist: array of string; ToolBarItem: TToolBarItem): Integer;
301 var
302         i: Integer;
303         Action: TCustomAction;
304 begin
305         Result := 0;
306         for i := 0 to Length(deflist) - 1 do begin
307                 if deflist[i] = '' then begin
308                         ToolBarItem.ButtonActionList.Add(nil);
309                         Inc(Result);
310                 end else begin
311                         Action := GetActionItem(FActionList, deflist[i]);
312                         if Action <> nil then begin
313                                 ToolBarItem.ButtonActionList.Add(Action);
314                                 Inc(Result);
315                         end;
316                 end;
317         end;
318 end;
319
320 //\83\8a\83X\83g\83r\83\85\81[\82Ì\83A\83C\83e\83\80\82ð\88Ú\93®\82·\82é
321 procedure TToolBarSettingDialog.MoveItem(Offset: Integer);
322 var
323         Item: TListItem;
324         ToolBarItem: TToolBarItem;
325 begin
326         Item := CurrentListView.Selected;
327         if (Item = nil) or (Item.Index + Offset < 0) then
328                 Exit;
329
330         ToolBarItem := TToolBarItem(ToolBarComboBox.Items.Objects[ToolBarComboBox.ItemIndex]);
331         if Item.Index + Offset >= ToolBarItem.ButtonActionList.Count then
332                 Exit;
333
334         ToolBarItem.ButtonActionList.Move(Item.Index, Item.Index + Offset);
335         CurrentListView.ItemIndex := Item.Index + Offset;
336         CurrentListView.Refresh;
337 end;
338
339 //\91S\83c\81[\83\8b\83{\83^\83\93\83\8a\83X\83g\83r\83\85\81[\82Ì\91I\91ð\95Ï\8dX\8e\9e
340 procedure TToolBarSettingDialog.AllListViewChange(Sender: TObject;
341         Item: TListItem; Change: TItemChange);
342 begin
343         AddButton.Enabled := not (Item = nil);
344 end;
345
346 //\8c»\8dÝ\83c\81[\83\8b\83{\83^\83\93\83\8a\83X\83g\83r\83\85\81[\82Ì\91I\91ð\95Ï\8dX\8e\9e
347 procedure TToolBarSettingDialog.CurrentListViewChange(Sender: TObject;
348         Item: TListItem; Change: TItemChange);
349 begin
350         UpButton.Enabled := not (Item = nil);
351         DownButton.Enabled := not (Item = nil);
352         RemoveButton.Enabled := not (Item = nil);
353         if Item = nil then
354                 Exit;
355         UpButton.Enabled := Item.Index > 0;
356         DownButton.Enabled := Item.Index < CurrentListView.Items.Count - 1;
357 end;
358
359 //\92Ç\89Á\83{\83^\83\93\89\9f\82µ\82½\82Æ\82«
360 procedure TToolBarSettingDialog.AddButtonClick(Sender: TObject);
361 var
362 //      List: TList;
363         ToolBarItem: TToolBarItem;
364         Item: TListItem;
365         Action: TCustomAction;
366 begin
367         Item := AllListView.Selected;
368         if Item = nil then
369                 Exit;
370
371         ToolBarItem := TToolBarItem(ToolBarComboBox.Items.Objects[ToolBarComboBox.ItemIndex]);
372         Action := Item.Data;
373         FAllList.Delete(Item.Index);
374         if Action <> nil then begin
375                 Item := CurrentListView.Selected;
376                 if Item = nil then
377                         ToolBarItem.ButtonActionList.Add(Action)
378                 else
379                         ToolBarItem.ButtonActionList.Insert(Item.Index + 1, Action);
380                 AllListView.Items.Count := AllListView.Items.Count - 1;
381                 AllListView.Refresh;
382                 CurrentListView.Items.Count := CurrentListView.Items.Count + 1;
383                 CurrentListView.Refresh;
384         end;
385 end;
386
387 //\8dí\8f\9c\83{\83^\83\93\89\9f\82µ\82½\82Æ\82«
388 procedure TToolBarSettingDialog.RemoveButtonClick(Sender: TObject);
389 var
390         ToolBarItem: TToolBarItem;
391         Item: TListItem;
392         Action: TCustomAction;
393 begin
394         Item := CurrentListView.Selected;
395         if Item = nil then
396                 Exit;
397
398         ToolBarItem := TToolBarItem(ToolBarComboBox.Items.Objects[ToolBarComboBox.ItemIndex]);
399         Action := Item.Data;
400         ToolBarItem.ButtonActionList.Delete(Item.Index);
401         if Action <> nil then begin
402                 FAllList.Add(Action);
403                 AllListView.Items.Count := AllListView.Items.Count + 1;
404                 Sort;
405                 AllListView.Refresh;
406         end;
407         CurrentListView.Items.Count := CurrentListView.Items.Count - 1;
408         CurrentListView.Refresh;
409 end;
410
411 //\8bæ\90Ø\82è\92Ç\89Á\83{\83^\83\93\89\9f\82µ\82½\82Æ\82«
412 procedure TToolBarSettingDialog.SeparatorAddButtonClick(Sender: TObject);
413 var
414         idx: Integer;
415         ToolBarItem: TToolBarItem;
416 begin
417         ToolBarItem := TToolBarItem(ToolBarComboBox.Items.Objects[ToolBarComboBox.ItemIndex]);
418         if CurrentListView.Selected = nil then
419                 idx := CurrentListView.Items.Count - 1
420         else
421                 idx := CurrentListView.Selected.Index;
422         ToolBarItem.ButtonActionList.Insert(idx + 1, nil);
423         CurrentListView.Items.Count := CurrentListView.Items.Count + 1;
424         CurrentListView.Refresh;
425 end;
426
427 //\91S\83c\81[\83\8b\83{\83^\83\93\83\8a\83X\83g\83r\83\85\81[\82Ì\83\\81[\83g
428 procedure TToolBarSettingDialog.Sort;
429 begin
430         FAllList.Sort(@CompareCategory);
431 end;
432
433 //\83\\81[\83g\82·\82é\82Æ\82«\82Ì\94ä\8ar
434 function CompareCategory(Item1, Item2: Pointer): Integer;
435 var
436         Action1: TCustomAction;
437         Action2: TCustomAction;
438 begin
439         if (Item1 = nil) and (Item2 = nil) then
440                 Result := 0
441         else if (Item1 = nil) and (Item2 <> nil) then
442                 Result := -1
443         else if (Item1 <> nil) and (Item2 = nil) then
444                 Result := 1
445         else begin
446                 if (TObject(Item1) is TCustomAction) and (TObject(Item1) is TCustomAction) then begin
447                         Action1 := TCustomAction(Item1);
448                         Action2 := TCustomAction(Item2);
449                         Result := AnsiCompareStr(Action1.Category + Action1.Caption, Action2.Category + Action2.Caption);
450                 end else
451                         Result := 0;
452         end;
453 end;
454
455 end.