OSDN Git Service

パネルの配置についてコメント追加
[winbottle/winbottle.git] / bottleclient / ConstEditor.pas
1 unit ConstEditor;
2
3 interface
4
5 uses
6   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
7   StdCtrls, Buttons, ImgList, ComCtrls, Menus, BottleDef,
8   ActnList, ToolWin, ExtCtrls, ScriptConsts;
9
10 type
11   TfrmConstEditor = class(TForm)
12     tvwConst: TTreeView;
13     imgIcon: TImageList;
14     ToolBar1: TToolBar;
15     tbtnNewGroup: TToolButton;
16     tbtnAddConst: TToolButton;
17     tbtnDelete: TToolButton;
18     btbnUp: TToolButton;
19     tbtnDown: TToolButton;
20     ActionList1: TActionList;
21     actNewGroup: TAction;
22     actNewConst: TAction;
23     actDelete: TAction;
24     actMoveUp: TAction;
25     actMoveDown: TAction;
26     PopUpMenu: TPopupMenu;
27     mnNewGroup: TMenuItem;
28     mnAddConst: TMenuItem;
29     mnUp: TMenuItem;
30     mnDown: TMenuItem;
31     pnlConstText: TPanel;
32     pageConstText: TPageControl;
33     tstMenu: TTabSheet;
34     tstConst: TTabSheet;
35     Label1: TLabel;
36     edtMenuCaption: TEdit;
37     Label2: TLabel;
38     edtMenuIfGhost: TEdit;
39     Label3: TLabel;
40     Label4: TLabel;
41     Label5: TLabel;
42     edtConstCaption: TEdit;
43     edtConstScript: TEdit;
44     cbxConstShortCut: TComboBox;
45     btnClose: TButton;
46     pnlConstFile: TPanel;
47     Label6: TLabel;
48     cbxConstFile: TComboBox;
49     mnDeleteConst: TMenuItem;
50     procedure actDeleteExecute(Sender: TObject);
51     procedure actMoveUpExecute(Sender: TObject);
52     procedure actMoveDownExecute(Sender: TObject);
53     procedure tvwConstChange(Sender: TObject; Node: TTreeNode);
54     procedure FormCreate(Sender: TObject);
55     procedure PopUpMenuPopup(Sender: TObject);
56     procedure btnCloseClick(Sender: TObject);
57     procedure tvwConstChanging(Sender: TObject; Node: TTreeNode;
58       var AllowChange: Boolean);
59     procedure cbxConstFileChange(Sender: TObject);
60     procedure actNewGroupExecute(Sender: TObject);
61     procedure actNewConstExecute(Sender: TObject);
62     procedure edtExit(Sender: TObject);
63   private
64     { Private \90é\8c¾ }
65     FTargetConstFile: TScriptConstFile;
66     FTargetItemID: integer;
67     procedure UpdateList;
68     function GetNodeByID(ID: integer): TTreeNode;
69     procedure ChangeActionStatus;
70   public
71     { Public \90é\8c¾ }
72     function Execute: boolean;
73   end;
74
75 var
76   frmConstEditor: TfrmConstEditor;
77
78 implementation
79
80 const
81   IcoGroup = 0;
82   IcoConst = 1;
83   IcoIfGhostGroup = 7;
84
85
86 {$R *.DFM}
87
88 { TfrmConstEditor }
89
90
91
92 function TfrmConstEditor.Execute: boolean;
93 var i: integer;
94 begin
95   cbxConstFile.Items.Clear;
96   for i := 0 to ScriptConstList.Count-1 do begin
97     cbxConstFile.Items.Add(ExtractFileName(ScriptConstList.Files[i].FileName));
98   end;
99   if ScriptConstList.Count > 0 then begin
100     FTargetConstFile := ScriptConstList[0];
101     cbxConstFile.ItemIndex := 0;
102   end else begin
103     FTargetConstFile := nil;
104     cbxConstFile.Text := '';
105   end;
106   UpdateList;
107   ChangeActionStatus;
108   ShowModal;
109   Result := true;
110 end;
111
112 procedure TfrmConstEditor.actDeleteExecute(Sender: TObject);
113 var Node: TTreeNode;
114 begin
115   if tvwConst.Selected = nil then Exit;
116   Node := tvwConst.Selected;
117   if Node.Level = 0 then begin
118     if Node.HasChildren then begin
119       if MessageDlg(Node.Text + '\82É\91®\82·\82é' + IntToStr(Node.Count) + '\8cÂ\82Ì\92è\8c^\8bå\82ð\8dí\8f\9c\82µ\82Ü\82·\82©?',
120                     mtConfirmation, mbOkCancel, 0) = mrCancel then Exit;
121     end;
122   end;
123   FTargetConstFile.Delete(integer(Node.Data));
124   tvwConst.OnChanging := nil;
125   tvwConst.Items.Delete(Node);
126   tvwConst.OnChanging := tvwConstChanging;
127   ChangeActionStatus;
128 end;
129
130 procedure TfrmConstEditor.actMoveUpExecute(Sender: TObject);
131 var Node, DestNode: TTreeNode;
132 begin
133   Node := tvwConst.Selected;
134   if Node = nil then
135     Exit;
136   FTargetConstFile.MoveUp(integer(Node.Data));
137   if Node.getPrevSibling <> nil then begin
138     DestNode := Node.getPrevSibling;
139     Node.MoveTo(DestNode, naInsert);
140   end else if Node.Level = 1 then begin
141     //\91O\82Ì\83O\83\8b\81[\83v\82Ì\96\96\94ö\82É\88Ú\93®\82·\82é
142     if Node.Parent.getPrevSibling = nil then Exit;
143     DestNode := Node.Parent.getPrevSibling;
144     Node.MoveTo(DestNode, naAddChild);
145   end;
146 end;
147
148 procedure TfrmConstEditor.actMoveDownExecute(Sender: TObject);
149 var Node, DestNode, NextNode: TTreeNode;
150 begin
151   Node := tvwConst.Selected;
152   if Node = nil then
153     Exit;
154   FTargetConstFile.MoveDown(integer(Node.Data));
155   if Node.getNextSibling <> nil then begin
156     NextNode := Node.getNextSibling;
157     NextNode.MoveTo(Node, naInsert);
158   end else if Node.Level = 1 then begin
159     //\8e\9f\82Ì\83O\83\8b\81[\83v\82Ì\90æ\93ª\82É\88Ú\93®\82·\82é
160     if Node.Parent.getNextSibling = nil then Exit;
161     DestNode := Node.Parent.getNextSibling;
162     Node.MoveTo(DestNode, naAddChildFirst);
163   end;
164 end;
165
166 procedure TfrmConstEditor.tvwConstChange(Sender: TObject; Node: TTreeNode);
167 var ConstData: TScriptConst;
168     MenuData: TScriptConstMenu;
169     ID: integer;
170 begin
171   ChangeActionStatus;
172   if Node <> nil then
173   begin
174     pageConstText.Enabled := true;
175     ID := integer(Node.Data); // \96{\93\96\82Í\82â\82è\82½\82­\82È\82¢\83L\83\83\83X\83g
176     if Node.Level = 0 then begin
177       MenuData := ScriptConstList.GetMenuByID(ID);
178       pageConstText.ActivePage := tstMenu;
179       edtMenuCaption.Text := MenuData.Caption;
180       edtMenuIfGhost.Text := MenuData.IfGhost;
181     end else begin
182       ConstData := ScriptConstList.GetConstByID(ID);
183       pageConstText.ActivePage := tstConst;
184       edtConstCaption.Text := ConstData.Caption;
185       edtConstScript.Text := ConstData.ConstText;
186       cbxConstShortCut.Text := ShortCutToText(ConstData.ShortCut);
187     end;
188     FTargetItemID := ID;
189   end;
190 end;
191
192 procedure TfrmConstEditor.FormCreate(Sender: TObject);
193 begin
194   if Application.Terminated then Exit;
195   //\83\81\83j\83\85\81[\8dì\90¬
196   BuildShortCutList(cbxConstShortCut.Items);
197   tvwConst.DoubleBuffered := true;
198 end;
199
200 procedure TfrmConstEditor.PopUpMenuPopup(Sender: TObject);
201 begin
202   ChangeActionStatus;
203   // \88È\89º\82Í\89E\83N\83\8a\83b\83N\8e\9e\82É\83G\83\89\81[\82ð\8bN\82±\82³\82È\82¢\82½\82ß\82Ì
204   // \83g\83\8a\83b\83N\93I\82È\83R\81[\83h
205   if tvwConst.Selected <> nil then
206     tvwConst.Selected.Selected := true;
207 end;
208
209 procedure TfrmConstEditor.btnCloseClick(Sender: TObject);
210 begin
211   ModalResult := mrOk;
212 end;
213
214 procedure TfrmConstEditor.UpdateList;
215 var i, j: integer;
216     MenuNode, ConstNode: TTreeNode;
217 begin
218   tvwConst.OnChanging := nil;
219   tvwConst.Items.BeginUpdate;
220   for i := tvwConst.Items.Count-1 downto 0 do begin
221     tvwConst.Items[i].Delete;
222   end;
223   if FTargetConstFile <> nil then begin
224     MenuNode := nil;
225     for i := 0 to FTargetConstFile.Count-1 do begin
226       MenuNode := tvwConst.Items.Add(MenuNode, FTargetConstFile[i].Caption);
227       if FTargetConstFile[i].IfGhost <> '' then begin
228         MenuNode.ImageIndex := IcoIfGhostGroup;
229         MenuNode.SelectedIndex := IcoIfGhostGroup;
230       end else begin
231         MenuNode.ImageIndex := IcoGroup;
232         MenuNode.SelectedIndex := IcoGroup;
233       end;
234       MenuNode.Data := Pointer(FTargetConstFile[i].ID);
235       for j := 0 to FTargetConstFile[i].Count-1 do begin
236         ConstNode := tvwConst.Items.AddChild(MenuNode, FTargetConstFile[i][j].Caption);
237         ConstNode.ImageIndex := IcoConst;
238         ConstNode.SelectedIndex := IcoConst;
239         ConstNode.Data := Pointer(FTargetConstFile[i][j].ID);
240       end;
241       MenuNode.Expand(false);
242     end;
243   end;
244   tvwConst.Items.EndUpdate;
245   if tvwConst.Items.Count > 0 then tvwConst.Items[0].Selected := true;
246   tvwConst.OnChanging := tvwConstChanging;
247 end;
248
249 procedure TfrmConstEditor.tvwConstChanging(Sender: TObject;
250   Node: TTreeNode; var AllowChange: Boolean);
251 var ConstData: TScriptConst;
252     MenuData:  TScriptConstMenu;
253     CanUpdate: boolean;
254 begin
255   // \91I\91ð\82ª\88Ú\93®\82·\82é\82Ü\82¦\82Ì\83A\83C\83e\83\80\82Ì\83f\81[\83^\82ð\8dX\90V\82·\82é
256   if FTargetItemID = 0 then Exit; // \88È\91O\82É\89½\82à\91I\91ð\82µ\82Ä\82¢\82È\82¢\82Ì\82È\82ç\89½\82à\82µ\82È\82¢
257
258   ConstData := ScriptConstList.GetConstByID(FTargetItemID);
259   CanUpdate := false; // \83t\83\89\83O
260   if ConstData <> nil then begin
261     if edtConstCaption.Text <> ConstData.Caption then CanUpdate := true;
262     if edtConstScript.Text <> ConstData.ConstText then CanUpdate := true;
263     if TextToShortCut(cbxConstShortCut.Text) <> ConstData.ShortCut then CanUpdate := true;
264     if edtConstCaption.Text = '' then CanUpdate := false;
265     if edtConstScript.Text = '' then CanUpdate := false;
266     if CanUpdate then begin
267       ConstData.Caption := edtConstCaption.Text;
268       ConstData.ConstText := edtConstScript.Text;
269       try
270         ConstData.ShortCut := TextToShortCut(cbxConstShortCut.Text);
271       except
272         ConstData.ShortCut := 0;
273       end;
274       GetNodeByID(FTargetItemID).Text := ConstData.Caption;
275     end;
276   end else begin
277     MenuData := ScriptConstList.GetMenuByID(FTargetItemId);
278     if edtMenuCaption.Text <> MenuData.Caption then CanUpdate := true;
279     if edtMenuIfGhost.Text <> MenuData.IfGhost then CanUpdate := true;
280     if edtMenuCaption.Text = '' then CanUpdate := false;
281     if CanUpdate then begin
282       MenuData.Caption := edtMenuCaption.Text;
283       MenuData.IfGhost := edtMenuIfGhost.Text;
284       with GetNodeByID(FTargetItemID) do begin
285         Text := MenuData.Caption;
286         if MenuData.IfGhost <> '' then begin
287           ImageIndex := IcoIfGhostGroup;
288           SelectedIndex := IcoIfGhostGroup;
289         end else begin
290           ImageIndex := IcoGroup;
291           SelectedIndex := IcoGroup;
292         end;
293       end;
294     end;
295   end;
296 end;
297
298 function TfrmConstEditor.GetNodeByID(ID: integer): TTreeNode;
299 var i: integer;
300 begin
301   Result := nil;
302   for i := 0 to tvwConst.Items.Count-1 do
303     if integer(tvwConst.Items[i].Data) = ID then begin
304       Result := tvwConst.Items[i];
305       Exit;
306     end;
307 end;
308
309 procedure TfrmConstEditor.cbxConstFileChange(Sender: TObject);
310 begin
311   FTargetConstFile := ScriptConstList[cbxConstFile.ItemIndex];
312   ChangeActionStatus;
313   UpdateList;
314 end;
315
316 procedure TfrmConstEditor.actNewGroupExecute(Sender: TObject);
317 var MenuData: TScriptConstMenu;
318     Node: TTreeNode;
319 begin
320   MenuData := FTargetConstFile.AddMenu;
321   Node := tvwConst.Items.Add(nil, MenuData.Caption);
322   Node.Data := Pointer(MenuData.ID);
323   Node.Selected := true;
324   edtMenuCaption.SetFocus;
325   edtMenuCaption.SelectAll;
326 end;
327
328 procedure TfrmConstEditor.actNewConstExecute(Sender: TObject);
329 var ConstData: TScriptConst;
330     MenuData: TScriptConstMenu;
331     MenuNode, NewNode: TTreeNode;
332     ID: integer;
333 begin
334   if tvwConst.Selected = nil then
335   begin
336     ShowMessage('\83O\83\8b\81[\83v\82ð\8dì\90¬\81E\91I\91ð\82µ\82Ä\82­\82¾\82³\82¢');
337     Exit;
338   end;
339   if tvwConst.Selected.Level = 0 then
340     MenuNode := tvwConst.Selected
341   else
342     MenuNode := tvwConst.Selected.Parent;
343   ID := integer(MenuNode.Data);
344   MenuData := FTargetConstFile.GetMenuByID(ID);
345   ConstData := MenuData.AddConst;
346   NewNode := tvwConst.Items.AddChild(MenuNode, ConstData.Caption);
347   NewNode.ImageIndex := 1;
348   NewNode.SelectedIndex := 1;
349   NewNode.Data := Pointer(ConstData.ID);
350   NewNode.Selected := true;
351   edtConstCaption.SetFocus;
352   edtConstCaption.SelectAll;
353 end;
354
355 procedure TfrmConstEditor.edtExit(Sender: TObject);
356 var dummy: boolean;
357 begin
358   tvwConstChanging(self, nil, dummy);
359 end;
360
361 procedure TfrmConstEditor.ChangeActionStatus;
362 var
363   Selected: TTreeNode;
364 begin
365   // \8fó\91Ô\82É\89\9e\82\82Ä\83\81\83j\83\85\81[\93\99\82Ì\97L\8cø\81E\96³\8cø\82ð\90Ø\91Ö\82¦\82é
366   Selected := tvwConst.Selected;
367   pageConstText.Enabled := Selected <> nil;
368   actMoveDown.Enabled := Selected <> nil;
369   actNewConst.Enabled := Selected <> nil;
370   actDelete.Enabled   := Selected <> nil;
371   actMoveUp.Enabled   := Selected <> nil;
372 end;
373
374 end.