OSDN Git Service

This commit was manufactured by cvs2svn to create branch 'Bb50'.
[gikonavigoeson/gikonavi.git] / KeySetting.pas
1 unit KeySetting;
2
3 interface
4
5 uses
6         Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7         Dialogs, ComCtrls, StdCtrls, ExtCtrls, ActnList, Menus, GikoSystem, GikoUtil,
8     Editor;
9
10 type
11         TKeySettingItem = class(TObject)
12         private
13                 FAction: TAction;
14                 FShortCut: TShortCut;
15                 FGesture        : string;
16         public
17                 property Action: TAction read FAction write FAction;
18                 property ShortCut: TShortCut read FShortCut write FShortCut;
19                 property Gesture : string read FGesture write FGesture;
20         end;
21
22         TKeySettingForm = class(TForm)
23     Panel1: TPanel;
24     Panel2: TPanel;
25     Panel3: TPanel;
26     OkBotton: TButton;
27     CancelBotton: TButton;
28     Label1: TLabel;
29     Label2: TLabel;
30     HotKey: THotKey;
31     SetButton: TButton;
32     StatusBar: TStatusBar;
33     Panel4: TPanel;
34     PageControl1: TPageControl;
35     TabSheet1: TTabSheet;
36     TabSheet2: TTabSheet;
37     ListView: TListView;
38     ListView1: TListView;
39     GestureLabel: TLabel;
40     GestureEdit: TEdit;
41     GestureSetButton: TButton;
42     GestureCheckBox: TCheckBox;
43                 procedure FormCreate(Sender: TObject);
44                 procedure FormDestroy(Sender: TObject);
45     procedure ListViewSelectItem(Sender: TObject; Item: TListItem;
46       Selected: Boolean);
47     procedure HotKeyEnter(Sender: TObject);
48     procedure HotKeyExit(Sender: TObject);
49     procedure FormKeyDown(Sender: TObject; var Key: Word;
50       Shift: TShiftState);
51     procedure SetButtonClick(Sender: TObject);
52     procedure OkBottonClick(Sender: TObject);
53     procedure ListViewCompare(Sender: TObject; Item1, Item2: TListItem;
54       Data: Integer; var Compare: Integer);
55     procedure GestureSetButtonClick(Sender: TObject);
56                 procedure OnGestureStart(Sender: TObject);
57                 procedure OnGestureMove(Sender: TObject);
58                 procedure OnGestureEnd(Sender: TObject);
59     procedure GestureCheckBoxClick(Sender: TObject);
60     procedure GestureEditChange(Sender: TObject);
61     procedure GestureEditKeyDown(Sender: TObject; var Key: Word;
62       Shift: TShiftState);
63         private
64                 { Private \90é\8c¾ }
65         public
66                 { Public \90é\8c¾ }
67         EditorForm: TEditorForm;
68         end;
69
70 //var
71 //      KeySettingForm: TKeySettingForm;
72
73 implementation
74
75 uses Giko, Gesture;
76 const
77         GUESTURE_NOTHING        = '\82È\82µ';
78
79 {$R *.dfm}
80
81 procedure TKeySettingForm.FormCreate(Sender: TObject);
82 var
83         i: Integer;
84         ListItem: TListItem;
85         KeyItem: TKeySettingItem;
86
87 begin
88         for i := 0 to GikoForm.ActionList.ActionCount - 1 do begin
89                 if GikoForm.ActionList.Actions[i] is TAction then begin
90                         if GikoForm.ActionList.Actions[i].Tag <> 0 then
91                                 Continue;
92                         ListItem := ListView.Items.Add;
93                         ListItem.Caption := TAction(GikoForm.ActionList.Actions[i]).Hint;
94                         ListItem.SubItems.Add(TAction(GikoForm.ActionList.Actions[i]).Category);
95                         ListItem.SubItems.Add(ShortCutToText(TAction(GikoForm.ActionList.Actions[i]).ShortCut));
96                         ListItem.SubItems.Add( GikoSys.Setting.Gestures.GetActionGesture(
97                                 TAction( GikoForm.ActionList.Actions[i] ) ) );
98                         ListItem.ImageIndex := TAction(GikoForm.ActionList.Actions[i]).ImageIndex;
99                         KeyItem := TKeySettingItem.Create;
100                         KeyItem.Action := TAction(GikoForm.ActionList.Actions[i]);
101                         KeyItem.ShortCut := TAction(GikoForm.ActionList.Actions[i]).ShortCut;
102                         KeyItem.Gesture := GikoSys.Setting.Gestures.GetActionGesture(
103                                 TAction( GikoForm.ActionList.Actions[i] ) );
104                         ListItem.Data := KeyItem;
105                 end;
106         end;
107         if ListView.Items.Count > 0 then
108                 ListView.Selected := ListView.Items[0];
109         EditorForm := TEditorForm.Create(Self);
110         GikoSys.LoadEditorKeySetting(EditorForm.ActionList);
111         try
112                 for i := 0 to EditorForm.ActionList.ActionCount - 1 do begin
113                         if EditorForm.ActionList.Actions[i] is TAction then begin
114                                 if EditorForm.ActionList.Actions[i].Tag <> 0 then
115                                         Continue;
116                                 ListItem := ListView1.Items.Add;
117                                 ListItem.Caption := TAction(EditorForm.ActionList.Actions[i]).Hint;
118                                 ListItem.SubItems.Add(TAction(EditorForm.ActionList.Actions[i]).Category);
119                                 ListItem.SubItems.Add(ShortCutToText(TAction(EditorForm.ActionList.Actions[i]).ShortCut));
120                                 ListItem.ImageIndex := TAction(EditorForm.ActionList.Actions[i]).ImageIndex;
121                                 ListItem.SubItems.Add( GikoSys.Setting.Gestures.GetActionGesture(
122                                         TAction( GikoForm.ActionList.Actions[i] ) ) );
123                                 KeyItem := TKeySettingItem.Create;
124                                 KeyItem.Action := TAction(EditorForm.ActionList.Actions[i]);
125                                 KeyItem.ShortCut := TAction(EditorForm.ActionList.Actions[i]).ShortCut;
126                                 KeyItem.Gesture := GikoSys.Setting.Gestures.GetActionGesture(
127                                         TAction( GikoForm.ActionList.Actions[i] ) );
128                                 ListItem.Data := KeyItem;
129                         end;
130                 end;
131                 ListView1.SmallImages := EditorForm.HotToobarImageList;
132         finally
133
134         end;
135 //      ActionListView.SortType := stText;
136         StatusBar.Height := 21;
137         StatusBar.Width := 21;
138         PageControl1.ActivePageIndex := 0;
139         GestureCheckBox.Checked := GikoSys.Setting.GestureEnabled;
140         GestureCheckBoxClick( Sender );
141
142         MouseGesture.UnHook;
143         MouseGesture.OnGestureStart := OnGestureStart;
144         MouseGesture.OnGestureMove := OnGestureMove;
145         MouseGesture.OnGestureEnd := OnGestureEnd;
146         MouseGesture.SetHook( Handle );
147 end;
148
149 procedure TKeySettingForm.FormDestroy(Sender: TObject);
150 var
151         i: Integer;
152 begin
153         for i := 0 to ListView.Items.Count - 1 do begin
154                 if TObject(ListView.Items[i].Data) is TKeySettingItem then
155                         TKeySettingItem(ListView.Items[i].Data).Free;
156         end;
157         for i := 0 to ListView1.Items.Count - 1 do begin
158                 if TObject(ListView1.Items[i].Data) is TKeySettingItem then
159                         TKeySettingItem(ListView1.Items[i].Data).Free;
160         end;
161          EditorForm.Release;
162 end;
163
164 procedure TKeySettingForm.ListViewSelectItem(Sender: TObject;
165   Item: TListItem; Selected: Boolean);
166 var
167         KeyItem: TKeySettingItem;
168 begin
169         if not Selected then Exit;
170
171         if TObject(Item.Data) is TKeySettingItem then begin
172                 KeyItem := TKeySettingItem(Item.Data);
173                 HotKey.HotKey := KeyItem.ShortCut;
174                 GestureEdit.Text := KeyItem.Gesture;
175         end;
176 end;
177
178 procedure TKeySettingForm.HotKeyEnter(Sender: TObject);
179 begin
180         OkBotton.Default := False;
181         CancelBotton.Cancel := False;
182 end;
183
184 procedure TKeySettingForm.HotKeyExit(Sender: TObject);
185 begin
186         OkBotton.Default := True;
187         CancelBotton.Cancel := True;
188 end;
189
190 procedure TKeySettingForm.FormKeyDown(Sender: TObject; var Key: Word;
191   Shift: TShiftState);
192 begin
193         if Key in [8, 27, 32, 46] then begin
194                 HotKey.HotKey := ShortCut(Key, Shift);
195                 Key := 0;
196         end;
197 end;
198
199 procedure TKeySettingForm.SetButtonClick(Sender: TObject);
200 const
201         ERR_ENT = 'Enter\83L\81[\82Í\83V\83\87\81[\83g\83J\83b\83g\82Æ\82µ\82Ä\8eg\97p\82Å\82«\82Ü\82¹\82ñ';
202         ERR_MSG = '\93ü\97Í\82µ\82½\83V\83\87\81[\83g\83J\83b\83g\82Í\8aù\82É\8eg\97p\82³\82ê\82Ä\82¢\82Ü\82·';
203         ERR_TITLE = '\83G\83\89\81[';
204 var
205         i: Integer;
206         Item: TListItem;
207         KeyItem: TKeySettingItem;
208     ActiveListView: TListView;
209 begin
210                 if PageControl1.ActivePage.TabIndex <> 0 then
211         ActiveListView := ListView1
212     else
213         ActiveListView := ListView;
214
215         if ActiveListView.Selected = nil then Exit;
216         if HotKey.HotKey = 13 then begin
217                 MsgBox(Handle, ERR_ENT, ERR_TITLE, MB_OK or MB_ICONSTOP);
218                 HotKey.SetFocus;
219                 Exit;
220         end;
221
222         //\8c»\8dÝ\91I\91ð\82³\82ê\82Ä\82¢\82éAction\88È\8aO\82Å\93¯\82\83V\83\87\81[\83g\83J\83b\83g\82ª\82 \82ê\82Î\83G\83\89\81[\82Æ\82·\82é
223         for i := 0 to ActiveListView.Items.Count - 1 do begin
224                 if ActiveListView.Selected = ActiveListView.Items[i] then
225                         Continue;
226                 Item := ActiveListView.Items[i];
227                 if TObject(Item.Data) is TKeySettingItem then begin
228                         KeyItem := TKeySettingItem(Item.Data);
229                         if (HotKey.HotKey <> 0) and (KeyItem.ShortCut = HotKey.HotKey) then begin
230                                 MsgBox(Handle, ERR_MSG, ERR_TITLE, MB_OK or MB_ICONSTOP);
231                                 HotKey.SetFocus;
232                                 Exit;
233                         end;
234                 end;
235         end;
236         //\83V\83\87\81[\83g\83J\83b\83g\90Ý\92è
237         if TObject(ActiveListView.Selected.Data) is TKeySettingItem then begin
238                 KeyItem := TKeySettingItem(ActiveListView.Selected.Data);
239                 KeyItem.ShortCut := HotKey.HotKey;
240                 ActiveListView.Selected.SubItems[1] := ShortCutToText(HotKey.HotKey);
241         end;
242 end;
243
244 procedure TKeySettingForm.OkBottonClick(Sender: TObject);
245 var
246         i: Integer;
247         Item: TListItem;
248         KeyItem: TKeySettingItem;
249 begin
250
251         GikoSys.Setting.GestureEnabled := GestureCheckBox.Checked;
252         GikoSys.Setting.Gestures.ClearGesture;
253         for i := 0 to ListView.Items.Count - 1 do begin
254                 Item := ListView.Items[i];
255                 if TObject(Item.Data) is TKeySettingItem then begin
256                         KeyItem := TKeySettingItem(Item.Data);
257                         KeyItem.Action.ShortCut := KeyItem.ShortCut;
258                         GikoSys.Setting.Gestures.AddGesture( KeyItem.Gesture, KeyItem.Action );
259                 end;
260         end;
261         for i := 0 to ListView1.Items.Count - 1 do begin
262                 Item := ListView1.Items[i];
263                 if TObject(Item.Data) is TKeySettingItem then begin
264                         KeyItem := TKeySettingItem(Item.Data);
265                         KeyItem.Action.ShortCut := KeyItem.ShortCut;
266                         GikoSys.Setting.Gestures.AddGesture( KeyItem.Gesture, KeyItem.Action );
267                 end;
268         end;
269
270 end;
271
272 procedure TKeySettingForm.ListViewCompare(Sender: TObject; Item1,
273   Item2: TListItem; Data: Integer; var Compare: Integer);
274 begin
275         if Item1.SubItems[0] > Item2.SubItems[0] then
276                 Compare := 1
277         else if Item1.SubItems[0] < Item2.SubItems[0] then
278                 Compare := -1
279         else
280                 Compare := 0;
281 end;
282
283 procedure TKeySettingForm.GestureSetButtonClick(Sender: TObject);
284 const
285         ERR_MSG = '\93ü\97Í\82µ\82½\83W\83F\83X\83`\83\83\81[\82Í\8aù\82É\8eg\97p\82³\82ê\82Ä\82¢\82Ü\82·';
286         ERR_TITLE = '\83G\83\89\81[';
287 var
288         i: Integer;
289         Item: TListItem;
290         KeyItem: TKeySettingItem;
291         ActiveListView: TListView;
292 begin
293         if PageControl1.ActivePage.TabIndex <> 0 then
294                 ActiveListView := ListView1
295         else
296                 ActiveListView := ListView;
297
298         if ActiveListView.Selected = nil then Exit;
299         if (GetKeyState( VK_SHIFT ) and $80000000) <> 0 then begin
300                 GestureEdit.Text := '';
301                 Item := ActiveListView.Selected;
302                 Item.SubItems[2] := '';
303                 if TObject(Item.Data) is TKeySettingItem then begin
304                         KeyItem := TKeySettingItem(Item.Data);
305                         KeyItem.Gesture := '';
306                 end;
307                 Exit;
308         end;
309
310         // \8c»\8dÝ\91I\91ð\82³\82ê\82Ä\82¢\82éAction\88È\8aO\82Å\93¯\82\83W\83F\83X\83`\83\83\81[\82ª\82 \82ê\82Î\83G\83\89\81[\82Æ\82·\82é
311         for i := 0 to ActiveListView.Items.Count - 1 do begin
312                 if ActiveListView.Selected = ActiveListView.Items[i] then
313                         Continue;
314                 Item := ActiveListView.Items[i];
315                 if TObject(Item.Data) is TKeySettingItem then begin
316                         KeyItem := TKeySettingItem(Item.Data);
317                         if (GestureEdit.Text <> GUESTURE_NOTHING)
318                         and (KeyItem.Gesture = GestureEdit.Text) then begin
319                                 MsgBox(Handle, ERR_MSG, ERR_TITLE, MB_OK or MB_ICONSTOP);
320                                 HotKey.SetFocus;
321                                 Exit;
322                         end;
323                 end;
324         end;
325         // \83W\83F\83X\83`\83\83\81[\90Ý\92è
326         if TObject(ActiveListView.Selected.Data) is TKeySettingItem then begin
327                 KeyItem := TKeySettingItem(ActiveListView.Selected.Data);
328                 KeyItem.Gesture := GestureEdit.Text;
329                 if GestureEdit.Text = GUESTURE_NOTHING then
330                         ActiveListView.Selected.SubItems[2] := ''
331                 else
332                         ActiveListView.Selected.SubItems[2] := GestureEdit.Text;
333         end;
334 end;
335
336 procedure TKeySettingForm.OnGestureStart(Sender: TObject);
337 begin
338         GestureEdit.Text := '';
339 end;
340
341 procedure TKeySettingForm.OnGestureMove(Sender: TObject);
342 begin
343         GestureEdit.Text := MouseGesture.GetGestureStr;
344 end;
345
346 procedure TKeySettingForm.OnGestureEnd(Sender: TObject);
347 begin
348         GestureEdit.Text := MouseGesture.GetGestureStr;
349 end;
350
351 procedure TKeySettingForm.GestureCheckBoxClick(Sender: TObject);
352 begin
353         GestureEdit.Enabled := GestureCheckBox.Checked;
354         GestureSetButton.Enabled := GestureCheckBox.Checked;
355 end;
356
357 procedure TKeySettingForm.GestureEditChange(Sender: TObject);
358 begin
359
360         if GestureEdit.Text = '' then
361                 GestureEdit.Text := GUESTURE_NOTHING;
362
363 end;
364
365 procedure TKeySettingForm.GestureEditKeyDown(Sender: TObject;
366   var Key: Word; Shift: TShiftState);
367 begin
368         if (ssShift in Shift) and (MouseGesture.GetGestureStr = '') then
369         GestureEdit.Text := '';
370 end;
371
372 end.