OSDN Git Service

・スレタイの特定ワードを非表示にする機能に「©bbspink.com」も追加
[gikonavigoeson/gikonavi.git] / PopupMenuSetting.pas
1 unit PopupMenuSetting;
2
3 interface
4
5 uses
6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7   Dialogs, StdCtrls, ExtCtrls, ActnList, Menus, ComCtrls;
8
9 type
10   TPopupMenuSettingDialog = class(TForm)
11     TopPanel: TPanel;
12     PopupMenuComboLabel: TLabel;
13     PopupMenuComboBox: TComboBox;
14     MainPanel: TPanel;
15     ActionListBox: TListBox;
16     LabelButton: TButton;
17     AddMainButton: TButton;
18     AddSubButton: TButton;
19     AddSepButton: TButton;
20     RemoveButton: TButton;
21     UpButton: TButton;
22     DownButton: TButton;
23     MenuTreeView: TTreeView;
24     Panel1: TPanel;
25     Panel2: TPanel;
26     Panel4: TPanel;
27     Splitter1: TSplitter;
28     ApplyButton: TButton;
29     procedure PopupMenuComboBoxChange(Sender: TObject);
30     procedure FormCreate(Sender: TObject);
31     procedure FormDestroy(Sender: TObject);
32     procedure UpButtonClick(Sender: TObject);
33     procedure DownButtonClick(Sender: TObject);
34     procedure RemoveButtonClick(Sender: TObject);
35     procedure AddSepButtonClick(Sender: TObject);
36     procedure MenuTreeViewDblClick(Sender: TObject);
37     procedure MenuTreeViewEdited(Sender: TObject; Node: TTreeNode;
38       var S: String);
39     procedure MenuTreeViewEditing(Sender: TObject; Node: TTreeNode;
40       var AllowEdit: Boolean);
41     procedure AddMainButtonClick(Sender: TObject);
42     procedure AddSubButtonClick(Sender: TObject);
43     procedure LabelButtonClick(Sender: TObject);
44     procedure MenuTreeViewChange(Sender: TObject; Node: TTreeNode);
45     procedure ApplyButtonClick(Sender: TObject);
46   private
47     { Private \90é\8c¾ }
48     FActionList: TActionList;
49     FSpecialActionLists: array[0..0] of TStringList;
50     function GetMenuAction(var Item:TMenuItem; Idx:Integer): TBasicAction;
51     procedure AddMainMenu(const Caption: String; Action: TBasicAction);
52     procedure DeleteUsedActionList(Action: TBasicAction);
53     function GetIniString(Node: TTreeNode; Parent: String; Idx: Integer): String;
54     function GetIniValueString(Node: TTreeNode): String;
55   public
56     { Public \90é\8c¾ }
57     constructor Create(AOwner: TComponent; ActionList: TActionList); reintroduce; overload; virtual;
58   end;
59
60 var
61   PopupMenuSettingDialog: TPopupMenuSettingDialog;
62
63 implementation
64
65 uses
66     PopupMenuUtil, IniFiles, GikoSystem, ToolBarUtil, GikoUtil;
67 {$R *.dfm}
68 //! \83R\83\93\83X\83g\83\89\83N\83^\81i\88ø\90\94\92Ç\89Á\81j
69 constructor TPopupMenuSettingDialog.Create(AOwner: TComponent; ActionList: TActionList);
70     const
71     // \93Á\8eê\83A\83N\83V\83\87\83\93\96¼
72     // \8f\84\89ñ\83A\83C\83e\83\80
73     ROUNDITEM = 'RoundItem';
74     // \93¯\88ê\94Â\83X\83\8c\83b\83h\88ê\97\97\97p
75     SAMPETHREAD='BoardThreadItem';
76 var
77     act : TCustomAction;
78 begin
79         inherited Create(AOwner);
80         FActionList := ActionList;
81     FSpecialActionLists[0] := TStringList.Create();
82     // \8ae\83|\83b\83v\83A\83b\83v\82Ì\93Á\8eê\83A\83N\83V\83\87\83\93\82ð\92Ç\89Á
83     act := TCustomAction.Create(Self);
84     act.Name := ROUNDITEM;
85     act.Caption := '\83X\83\8c\83b\83h\8f\84\89ñ\97\\96ñ';
86     FSpecialActionLists[0].AddObject(act.Caption, act);
87     act := TCustomAction.Create(Self);
88     act.Name := SAMPETHREAD;
89     act.Caption := '\93¯\94Â\82Å\95\\8e¦\82µ\82Ä\82¢\82é\83X\83\8c\83b\83h';
90     FSpecialActionLists[0].AddObject(act.Caption, act);
91
92 end;
93 //! \83|\83b\83v\83A\83b\83v\83\81\83j\83\85\81[\83R\83\93\83{\83{\83b\83N\83X\95Ï\8dX\83C\83x\83\93\83g
94 procedure TPopupMenuSettingDialog.PopupMenuComboBoxChange(Sender: TObject);
95 var
96     act : TCustomAction;
97     bact: TBasicAction;
98     ini : TMemIniFile;
99     mkeys, skeys : TStringList;
100     i, j, idx: Integer;
101     value, subValue : String;
102     item, subItem : TMenuItem;
103     node : TTreeNode;
104 begin
105     ActionListBox.Items.Clear;
106     MenuTreeView.Items.Clear;
107     AddSubButton.Enabled := False;
108     case PopupMenuComboBox.ItemIndex of
109     0 : begin
110         for idx := 0 to Length(PopupMenuUtil.ACK_BROWSER) -1 do begin
111             act := PopupMenuUtil.GetActionItem(FActionList, PopupMenuUtil.ACK_BROWSER[idx]);
112             if (act <> nil) then begin
113                 ActionListBox.AddItem(act.Caption, act);
114             end;
115         end;
116     end
117     else
118         ;
119     end;
120     for idx := 0 to FSpecialActionLists[PopupMenuComboBox.ItemIndex].Count - 1 do begin
121         // \88ê\92U\82·\82×\82Ä\92Ç\89Á\82·\82é
122         act := TCustomAction(FSpecialActionLists[PopupMenuComboBox.ItemIndex].Objects[idx]);
123         if (act <> nil) then begin
124             ActionListBox.AddItem(act.Caption, act);
125         end;
126     end;
127
128     //ini\83t\83@\83C\83\8b\82Ì\93Ç\82Ý\8d\9e\82Ý
129     if (FileExists(GikoSys.Setting.GetConfigDir + PopupMenuUtil.INI_FILENAME)) Then begin
130         ini := TMemIniFile.Create(GikoSys.Setting.GetConfigDir + PopupMenuUtil.INI_FILENAME);
131         mkeys := TStringList.Create;
132         mkeys.Sorted := true;
133         skeys := TStringList.Create;
134         skeys.Sorted := true;
135         try
136             node := nil;
137             idx := PopupMenuComboBox.ItemIndex;
138             ini.ReadSection(PopupMenuUtil.SECTIONS[idx], mkeys);
139             // main\91æ\88ê\8aK\91w sub\91æ\93ñ\8aK\91w\82Ì\95ª\97£
140             for i := mkeys.Count - 1 downto 0 do begin
141                 if (Pos('sub', mkeys[i]) = 1) then begin
142                     skeys.Add(mkeys[i]);
143                     mkeys.Delete(i);
144                 end;
145             end;
146             // \91æ\88ê\8aK\91w\82Ì\8f\88\97\9d
147             for i := 0 to mkeys.Count - 1 do begin
148                 value := ini.ReadString(PopupMenuUtil.SECTIONS[idx], mkeys[i], '-');
149                 item := PopupMenuUtil.GetMenuItem(
150                     PopupMenuUtil.SECTIONS[idx], nil, FActionList, value);
151                 if (item <> nil) then begin
152                     bact := GetMenuAction(item, PopupMenuComboBox.ItemIndex);
153                     node := MenuTreeView.Items.AddObject(node, item.Caption, bact);
154                     DeleteUsedActionList(bact);
155                     // \83A\83N\83V\83\87\83\93\82ª\90Ý\92è\82³\82ê\82Ä\82¢\82é\82à\82Ì\82É\82Í\91æ\93ñ\91w\82Í\82Â\82¯\82È\82¢
156                     if (item.Action = nil) then begin
157                         // \91æ\93ñ\8aK\91w\82Ì\8f\88\97\9d(\82 \82ê\82Î)
158                         for j := 0 to skeys.Count - 1 do begin
159                             if (Pos('sub.' + mkeys[i], skeys[j]) = 1) then begin
160                                 subValue := ini.ReadString(PopupMenuUtil.SECTIONS[idx], skeys[j], '-');
161                                 subItem := GetMenuItem(PopupMenuUtil.SECTIONS[idx], nil, FActionList, subValue);
162                                 if (subItem <> nil) then begin
163                                     bact := GetMenuAction(item, PopupMenuComboBox.ItemIndex);
164                                     MenuTreeView.Items.AddChildObject(
165                                             node, subItem.Caption, bact);
166                                     DeleteUsedActionList(bact);
167                                     subItem.Clear;
168                                 end;
169                             end;
170                         end;
171                     end;
172                     item.Clear;
173                 end;
174             end;
175         finally
176             skeys.Free;
177             mkeys.Free;
178             ini.free;
179         end;
180     end;
181
182 end;
183 //! \83\81\83j\83\85\81[\83A\83N\83V\83\87\83\93\8eæ\93¾
184 function TPopupMenuSettingDialog.GetMenuAction(var Item:TMenuItem; Idx:Integer): TBasicAction;
185 var
186     i : Integer;
187 begin
188     Result := nil;
189     if (Item.Action <> nil) then begin
190         Result := Item.Action;
191     end else if (Item.Caption = '-') then begin
192         Item.Caption := ToolBarUtil.SEPARATOR_TEXT;
193     end else begin
194         for i := 0 to FSpecialActionLists[Idx].Count - 1 do begin
195             if Item.Name = TCustomAction(FSpecialActionLists[Idx].Objects[i]).Name then begin
196                 Result := TBasicAction(FSpecialActionLists[Idx].Objects[i]);
197                 break;
198             end;
199         end;
200     end;
201 end;
202 //! \83t\83H\81[\83\80\90\90¬
203 procedure TPopupMenuSettingDialog.FormCreate(Sender: TObject);
204 begin
205     PopupMenuComboBox.OnChange(nil);
206 end;
207 //! \83t\83H\81[\83\80\8dí\8f\9c
208 procedure TPopupMenuSettingDialog.FormDestroy(Sender: TObject);
209 var
210     idx, i : Integer;
211 begin
212     ActionListBox.Items.Clear;
213     // \93Á\8eê\83A\83N\83V\83\87\83\93\82Í\8dí\8f\9c\82·\82é
214     for i := 0 to Length(FSpecialActionLists) - 1 do begin
215         if (FSpecialActionLists[i] <> nil) then begin
216             for idx := FSpecialActionLists[i].Count - 1 downto 0 do begin
217                 TComponent(FSpecialActionLists[i].Objects[idx]).Free;
218             end;
219             FSpecialActionLists[i].Free;
220         end;
221     end;
222 end;
223 //! Up\83{\83^\83\93\89\9f\89º\8f\88\97\9d
224 procedure TPopupMenuSettingDialog.UpButtonClick(Sender: TObject);
225 begin
226     if (MenuTreeView.Selected <> nil) then begin
227         if (MenuTreeView.Selected.GetPrevSibling <> nil) then begin
228             MenuTreeView.Selected.MoveTo(
229                 MenuTreeView.Selected.GetPrevSibling,
230                 naInsert );
231         end;
232     end;
233 end;
234 //! Down\83{\83^\83\93\89\9f\89º\8f\88\97\9d
235 procedure TPopupMenuSettingDialog.DownButtonClick(Sender: TObject);
236 begin
237     if (MenuTreeView.Selected <> nil) then begin
238         if (MenuTreeView.Selected.GetNextSibling <> nil) then begin
239             MenuTreeView.Selected.GetNextSibling
240                 .MoveTo(
241                     MenuTreeView.Selected,
242                 naInsert );
243         end;
244     end;
245 end;
246 //! \8dí\8f\9c\83{\83^\83\93\89\9f\89º\8f\88\97\9d
247 procedure TPopupMenuSettingDialog.RemoveButtonClick(Sender: TObject);
248 var
249     act : TCustomAction;
250     idx : Integer;
251 begin
252     if (MenuTreeView.Selected <> nil) then begin
253         if (TObject(MenuTreeView.Selected.Data) is TCustomAction) then begin
254             act := TCustomAction(MenuTreeView.Selected.Data);
255             // \93Á\8eê\83A\83N\83V\83\87\83\93\82Ì\8fê\8d\87\81AAction\83\8a\83X\83g\82É\95\9c\8a\88\82³\82¹\82é
256             for idx := 0 to FSpecialActionLists[PopupMenuComboBox.ItemIndex].Count - 1 do begin
257                 if ( act = TCustomAction(FSpecialActionLists[PopupMenuComboBox.ItemIndex].Objects[idx]) )
258                 then begin
259                     ActionListBox.AddItem(act.Caption, act);
260                     break;
261                 end;
262             end;
263         end;
264         MenuTreeView.Items.Delete(MenuTreeView.Selected);
265     end;
266 end;
267
268 //! \8bæ\90Ø\82è\92Ç\89Á\83{\83^\83\93\83C\83x\83\93\83g
269 procedure TPopupMenuSettingDialog.AddSepButtonClick(Sender: TObject);
270 var
271     item : TMenuItem;
272 begin
273     item := TMenuItem.Create(nil);
274     item.Caption := '-';
275     AddMainMenu( item.Caption,
276         GetMenuAction(item, PopupMenuComboBox.ItemIndex));
277     item.Free;
278 end;
279 //! \83\81\83j\83\85\81[\83c\83\8a\81[\83_\83u\83\8b\83N\83\8a\83b\83N\8e\9e\83C\83x\83\93\83g
280 procedure TPopupMenuSettingDialog.MenuTreeViewDblClick(Sender: TObject);
281 begin
282     if (MenuTreeView.Selected <> nil) then begin
283         // \95Ò\8fW\89Â\94\\82È\83\89\83x\83\8b\82©\82Í\81AAction\82ª\90Ý\92è\82³\82ê\82Ä\82¢\82é\82©\82Å\94»\92è
284         if not (TObject(MenuTreeView.Selected.Data) is TBasicAction) then begin
285             MenuTreeView.ReadOnly := False;
286             MenuTreeView.Selected.EditText;
287         end;
288     end;
289 end;
290 //! \83\81\83j\83\85\81[\83c\83\8a\81[\95Ò\8fW\8a®\97¹\83C\83x\83\93\83g
291 procedure TPopupMenuSettingDialog.MenuTreeViewEdited(Sender: TObject;
292   Node: TTreeNode; var S: String);
293 begin
294     // \93Ç\82Ý\8eæ\82è\90ê\97p\82É\95Ï\8dX
295     MenuTreeView.ReadOnly := True;
296 end;
297 //! \83\81\83j\83\85\81[\83c\83\8a\81[\95Ò\8fW\83C\83x\83\93\83g
298 procedure TPopupMenuSettingDialog.MenuTreeViewEditing(Sender: TObject;
299   Node: TTreeNode; var AllowEdit: Boolean);
300 begin
301     if (Node <> nil) then begin
302         AllowEdit := not (TObject(Node.Data) is TBasicAction);
303     end;
304 end;
305 //! \83\81\83j\83\85\81[\92Ç\89Á\83{\83^\83\93\89\9f\89º\8f\88\97\9d
306 procedure TPopupMenuSettingDialog.AddMainButtonClick(Sender: TObject);
307 var
308     idx, idx2 : Integer;
309     act : TCustomAction;
310 begin
311
312     act := nil;
313     for idx := 0 to ActionListBox.Count - 1 do begin
314         if ( ActionListBox.Selected[idx] ) then begin
315             act := TCustomAction(ActionListBox.Items.Objects[idx]);
316             // \93Á\8eê\83A\83N\83V\83\87\83\93\82Ì\8fê\8d\87\81AAction\83\8a\83X\83g\82©\82ç\8dí\8f\9c\82·\82é
317             for idx2 := 0 to FSpecialActionLists[PopupMenuComboBox.ItemIndex].Count - 1 do begin
318                 if ( act = TCustomAction(FSpecialActionLists[PopupMenuComboBox.ItemIndex].Objects[idx2]) )
319                 then begin
320                     ActionListBox.Items.Delete(idx);
321                     break;
322                 end;
323             end;
324             break;
325         end;
326
327     end;
328
329     if ( act <> nil ) then begin
330         AddMainMenu( act.Caption, TBasicAction(act) );
331     end;
332 end;
333 //! \83T\83u\83\81\83j\83\85\81[\92Ç\89Á\83{\83^\83\93\89\9f\89º\8f\88\97\9d
334 procedure TPopupMenuSettingDialog.AddSubButtonClick(Sender: TObject);
335 var
336     idx, idx2 : Integer;
337     act : TCustomAction;
338     node : TTreeNode;
339 begin
340
341     node := MenuTreeView.Selected;
342
343     if (node <> nil) then begin
344         if not (TObject(node.Data) is TBasicAction) then begin
345             act := nil;
346             for idx := 0 to ActionListBox.Count - 1 do begin
347                 if ( ActionListBox.Selected[idx] ) then begin
348                     act := TCustomAction(ActionListBox.Items.Objects[idx]);
349                     // \93Á\8eê\83A\83N\83V\83\87\83\93\82Ì\8fê\8d\87\81AAction\83\8a\83X\83g\82©\82ç\8dí\8f\9c\82·\82é
350                     for idx2 := 0 to FSpecialActionLists[PopupMenuComboBox.ItemIndex].Count - 1 do begin
351                         if ( act = TCustomAction(FSpecialActionLists[PopupMenuComboBox.ItemIndex].Objects[idx2]) )
352                         then begin
353                             ActionListBox.Items.Delete(idx);
354                             break;
355                         end;
356                     end;
357                     break;
358                 end;
359             end;
360             if ( act <> nil ) then begin
361                 MenuTreeView.Items.AddChildObject(
362                     node, act.Caption, act);
363             end;
364         end;
365     end;
366 end;
367 //! \83\89\83x\83\8b\92Ç\89Á\83{\83^\83\93\89\9f\89º\8f\88\97\9d
368 procedure TPopupMenuSettingDialog.LabelButtonClick(Sender: TObject);
369 begin
370     if (MenuTreeView.Selected <> nil) then begin
371         // \83T\83u\83\81\83j\83\85\81[\82É\83\89\83x\83\8b\82Í\92Ç\89Á\82Å\82«\82È\82¢
372         if ( MenuTreeView.Selected.Parent <> nil) then begin
373             Exit;
374         end;
375     end;
376     AddMainMenu('\90V\8bK\83\89\83x\83\8b', TBasicAction(nil));
377 end;
378 //! \83\81\83j\83\85\81[\83c\83\8a\81[\83\81\83C\83\93\83\81\83j\83\85\81[\92Ç\89Á
379 procedure TPopupMenuSettingDialog.AddMainMenu(const Caption: String; Action: TBasicAction);
380 var
381     node : TTreeNode;
382 begin
383     node := nil;
384
385     if (MenuTreeView.Selected <> nil) then begin
386         // \83T\83u\83\81\83j\83\85\81[
387         if ( MenuTreeView.Selected.Parent <> nil) then begin
388             // \93¯\88ê\8aK\91w\82Ì\8e\9f\82Ì\83A\83C\83e\83\80\82ð\8eæ\93¾
389             if (MenuTreeView.Selected.getNextSibling <> nil) then begin
390                 node := MenuTreeView.Selected.getNextSibling;
391             end else begin
392                 node := MenuTreeView.Selected;
393             end;
394         end else begin
395             node := MenuTreeView.Selected.getNextSibling;
396         end;
397     end;
398     // node \82ªnil \82È\82ç\96\96\94ö\92Ç\89Á
399     if ( node <> nil ) then begin
400         MenuTreeView.Items.InsertObject(node,
401                 Caption, Action);
402     end else begin
403         MenuTreeView.Items.AddObject(node,
404                 Caption, Action);
405     end;
406 end;
407 //! \8eg\97p\8dÏ\82Ý\93Á\8eê\83A\83N\83V\83\87\83\93\82ð\83A\83N\83V\83\87\83\93\83\8a\83X\83g\82©\82ç\8dí\8f\9c\82·\82é
408 procedure TPopupMenuSettingDialog.DeleteUsedActionList(Action: TBasicAction);
409 var
410     idx, idx2 : Integer;
411     act : TCustomAction;
412 begin
413     if (Action is TCustomAction) then begin
414         act := TCustomAction(Action);
415         // \93Á\8eê\83A\83N\83V\83\87\83\93\82Ì\8fê\8d\87\81AAction\83\8a\83X\83g\82©\82ç\8dí\8f\9c\82·\82é
416         for idx2 := 0 to FSpecialActionLists[PopupMenuComboBox.ItemIndex].Count - 1 do begin
417             if ( act = TCustomAction(FSpecialActionLists[PopupMenuComboBox.ItemIndex].Objects[idx2]) )
418             then begin
419                 for idx := 0 to ActionListBox.Count - 1 do begin
420                     if ( act = TCustomAction(ActionListBox.Items.Objects[idx]) )
421                     then begin
422                         ActionListBox.Items.Delete(idx);
423                         break;
424                     end;
425                 end;
426                 break;
427             end;
428         end;
429     end;
430 end;
431
432 procedure TPopupMenuSettingDialog.MenuTreeViewChange(Sender: TObject;
433   Node: TTreeNode);
434 begin
435     if (Node <> nil) then begin
436         if (Node.Data = nil) and (Node.Text <> ToolBarUtil.SEPARATOR_TEXT) then begin
437             AddSubButton.Enabled := True;
438         end else begin
439             AddSubButton.Enabled := False;
440         end;
441     end else begin
442         AddSubButton.Enabled := False;
443
444     end;
445 end;
446 function TPopupMenuSettingDialog.GetIniString(Node: TTreeNode; Parent: String; Idx: Integer): String;
447 begin
448     Result := '';
449     if ( Node.Parent <> nil ) then begin
450         Result := Format('sub.%s.%2.2d', [Parent, idx + 1]);
451     end else begin
452         Result := Format('main.%2.2d', [idx + 1]);
453     end;
454 end;
455 function TPopupMenuSettingDialog.GetIniValueString(Node: TTreeNode): String;
456 begin
457     Result := '';
458     if (Node.Data = nil) then begin
459         if (Node.Text <> ToolBarUtil.SEPARATOR_TEXT) then begin
460             Result := '"' + Node.Text;
461         end else begin
462             Result := '-';
463         end;
464     end else begin
465         Result := TBasicAction(Node.Data).Name;
466     end;
467 end;
468 //! \93K\97p\83{\83^\83\93\89\9f\89º\8f\88\97\9d
469 procedure TPopupMenuSettingDialog.ApplyButtonClick(Sender: TObject);
470 const
471     MSG = '\90Ý\92è\82ð\95Û\91\82µ\82Ü\82µ\82½\81B\8dÄ\8bN\93®\8cã\82É\97L\8cø\82É\82È\82è\82Ü\82·\81B';
472 var
473     ini : TMemIniFile;
474     sec, key, subkey : String;
475     node : TTreeNode;
476     idx, mainIdx, subIdx : Integer;
477 begin
478     ini := TMemIniFile.Create(GikoSys.Setting.GetConfigDir + PopupMenuUtil.INI_FILENAME);
479     try
480         sec := PopupMenuUtil.SECTIONS[PopupMenuComboBox.ItemIndex];
481         // \83Z\83N\83V\83\87\83\93\82·\82×\82Ä\82ð\8fÁ\82·
482         ini.EraseSection(sec);
483         mainIdx := 0;
484         subIdx  := 0;
485
486         node := MenuTreeView.Items.GetFirstNode;
487         while node <> nil do begin
488             if (node.Parent = nil) then begin
489                 subIdx  := 0;
490                 key := GetIniString(node, key, mainIdx);
491                 ini.WriteString(sec, key, GetIniValueString(node));
492                 Inc(mainIdx);
493             end else begin
494                 subkey := GetIniString(node, key, subIdx);
495                 ini.WriteString(sec, subkey, GetIniValueString(node));
496                 Inc(subIdx);
497             end;
498             node := node.GetNext;
499         end;
500         ini.UpdateFile;
501
502                 MsgBox(Self.Handle, MSG, '\83M\83R\83i\83r', MB_OK or MB_ICONINFORMATION);
503
504     finally
505         ini.Free;
506     end;
507 end;
508 end.