OSDN Git Service

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