OSDN Git Service

ジェスチャーを解除した際に、表示上解除されたように見えて実は解除されていなかったバグを修正。
[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         private
62                 { Private \90é\8c¾ }
63         public
64                 { Public \90é\8c¾ }
65         EditorForm: TEditorForm;
66         end;
67
68 //var
69 //      KeySettingForm: TKeySettingForm;
70
71 implementation
72
73 uses Giko, Gesture;
74 const
75         GUESTURE_NOTHING        = '\82È\82µ';
76
77 {$R *.dfm}
78
79 procedure TKeySettingForm.FormCreate(Sender: TObject);
80 var
81         i: Integer;
82         ListItem: TListItem;
83         KeyItem: TKeySettingItem;
84
85 begin
86         for i := 0 to GikoForm.ActionList.ActionCount - 1 do begin
87                 if GikoForm.ActionList.Actions[i] is TAction then begin
88                         if GikoForm.ActionList.Actions[i].Tag <> 0 then
89                                 Continue;
90                         ListItem := ListView.Items.Add;
91                         ListItem.Caption := TAction(GikoForm.ActionList.Actions[i]).Hint;
92                         ListItem.SubItems.Add(TAction(GikoForm.ActionList.Actions[i]).Category);
93                         ListItem.SubItems.Add(ShortCutToText(TAction(GikoForm.ActionList.Actions[i]).ShortCut));
94                         ListItem.SubItems.Add( GikoSys.Setting.Gestures.GetActionGesture(
95                                 TAction( GikoForm.ActionList.Actions[i] ) ) );
96                         ListItem.ImageIndex := TAction(GikoForm.ActionList.Actions[i]).ImageIndex;
97                         KeyItem := TKeySettingItem.Create;
98                         KeyItem.Action := TAction(GikoForm.ActionList.Actions[i]);
99                         KeyItem.ShortCut := TAction(GikoForm.ActionList.Actions[i]).ShortCut;
100                         KeyItem.Gesture := GikoSys.Setting.Gestures.GetActionGesture(
101                                 TAction( GikoForm.ActionList.Actions[i] ) );
102                         ListItem.Data := KeyItem;
103                 end;
104         end;
105         if ListView.Items.Count > 0 then
106                 ListView.Selected := ListView.Items[0];
107         EditorForm := TEditorForm.Create(Self);
108         GikoSys.LoadEditorKeySetting(EditorForm.ActionList);
109         try
110                 for i := 0 to EditorForm.ActionList.ActionCount - 1 do begin
111                         if EditorForm.ActionList.Actions[i] is TAction then begin
112                                 if EditorForm.ActionList.Actions[i].Tag <> 0 then
113                                         Continue;
114                                 ListItem := ListView1.Items.Add;
115                                 ListItem.Caption := TAction(EditorForm.ActionList.Actions[i]).Hint;
116                                 ListItem.SubItems.Add(TAction(EditorForm.ActionList.Actions[i]).Category);
117                                 ListItem.SubItems.Add(ShortCutToText(TAction(EditorForm.ActionList.Actions[i]).ShortCut));
118                                 ListItem.ImageIndex := TAction(EditorForm.ActionList.Actions[i]).ImageIndex;
119                                 ListItem.SubItems.Add( GikoSys.Setting.Gestures.GetActionGesture(
120                                         TAction( GikoForm.ActionList.Actions[i] ) ) );
121                                 KeyItem := TKeySettingItem.Create;
122                                 KeyItem.Action := TAction(EditorForm.ActionList.Actions[i]);
123                                 KeyItem.ShortCut := TAction(EditorForm.ActionList.Actions[i]).ShortCut;
124                                 KeyItem.Gesture := GikoSys.Setting.Gestures.GetActionGesture(
125                                         TAction( GikoForm.ActionList.Actions[i] ) );
126                                 ListItem.Data := KeyItem;
127                         end;
128                 end;
129                 ListView1.SmallImages := EditorForm.HotToobarImageList;
130         finally
131
132         end;
133 //      ActionListView.SortType := stText;
134         StatusBar.Height := 21;
135         StatusBar.Width := 21;
136         PageControl1.ActivePageIndex := 0;
137         GestureCheckBox.Checked := GikoSys.Setting.GestureEnabled;
138         GestureCheckBoxClick( Sender );
139
140         MouseGesture.UnHook;
141         MouseGesture.OnGestureStart := OnGestureStart;
142         MouseGesture.OnGestureMove := OnGestureMove;
143         MouseGesture.OnGestureEnd := OnGestureEnd;
144         MouseGesture.SetHook( Handle );
145 end;
146
147 procedure TKeySettingForm.FormDestroy(Sender: TObject);
148 var
149         i: Integer;
150 begin
151         for i := 0 to ListView.Items.Count - 1 do begin
152                 if TObject(ListView.Items[i].Data) is TKeySettingItem then
153                         TKeySettingItem(ListView.Items[i].Data).Free;
154         end;
155         for i := 0 to ListView1.Items.Count - 1 do begin
156                 if TObject(ListView1.Items[i].Data) is TKeySettingItem then
157                         TKeySettingItem(ListView1.Items[i].Data).Free;
158         end;
159          EditorForm.Release;
160 end;
161
162 procedure TKeySettingForm.ListViewSelectItem(Sender: TObject;
163   Item: TListItem; Selected: Boolean);
164 var
165         KeyItem: TKeySettingItem;
166 begin
167         if not Selected then Exit;
168
169         if TObject(Item.Data) is TKeySettingItem then begin
170                 KeyItem := TKeySettingItem(Item.Data);
171                 HotKey.HotKey := KeyItem.ShortCut;
172                 GestureEdit.Text := KeyItem.Gesture;
173         end;
174 end;
175
176 procedure TKeySettingForm.HotKeyEnter(Sender: TObject);
177 begin
178         OkBotton.Default := False;
179         CancelBotton.Cancel := False;
180 end;
181
182 procedure TKeySettingForm.HotKeyExit(Sender: TObject);
183 begin
184         OkBotton.Default := True;
185         CancelBotton.Cancel := True;
186 end;
187
188 procedure TKeySettingForm.FormKeyDown(Sender: TObject; var Key: Word;
189   Shift: TShiftState);
190 begin
191         if Key in [8, 27, 32, 46] then begin
192                 HotKey.HotKey := ShortCut(Key, Shift);
193                 Key := 0;
194         end;
195 end;
196
197 procedure TKeySettingForm.SetButtonClick(Sender: TObject);
198 const
199         ERR_ENT = 'Enter\83L\81[\82Í\83V\83\87\81[\83g\83J\83b\83g\82Æ\82µ\82Ä\8eg\97p\82Å\82«\82Ü\82¹\82ñ';
200         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·';
201         ERR_TITLE = '\83G\83\89\81[';
202 var
203         i: Integer;
204         Item: TListItem;
205         KeyItem: TKeySettingItem;
206     ActiveListView: TListView;
207 begin
208                 if PageControl1.ActivePage.TabIndex <> 0 then
209         ActiveListView := ListView1
210     else
211         ActiveListView := ListView;
212
213         if ActiveListView.Selected = nil then Exit;
214         if HotKey.HotKey = 13 then begin
215                 MsgBox(Handle, ERR_ENT, ERR_TITLE, MB_OK or MB_ICONSTOP);
216                 HotKey.SetFocus;
217                 Exit;
218         end;
219
220         //\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é
221         for i := 0 to ActiveListView.Items.Count - 1 do begin
222                 if ActiveListView.Selected = ActiveListView.Items[i] then
223                         Continue;
224                 Item := ActiveListView.Items[i];
225                 if TObject(Item.Data) is TKeySettingItem then begin
226                         KeyItem := TKeySettingItem(Item.Data);
227                         if (HotKey.HotKey <> 0) and (KeyItem.ShortCut = HotKey.HotKey) then begin
228                                 MsgBox(Handle, ERR_MSG, ERR_TITLE, MB_OK or MB_ICONSTOP);
229                                 HotKey.SetFocus;
230                                 Exit;
231                         end;
232                 end;
233         end;
234         //\83V\83\87\81[\83g\83J\83b\83g\90Ý\92è
235         if TObject(ActiveListView.Selected.Data) is TKeySettingItem then begin
236                 KeyItem := TKeySettingItem(ActiveListView.Selected.Data);
237                 KeyItem.ShortCut := HotKey.HotKey;
238                 ActiveListView.Selected.SubItems[1] := ShortCutToText(HotKey.HotKey);
239         end;
240 end;
241
242 procedure TKeySettingForm.OkBottonClick(Sender: TObject);
243 var
244         i: Integer;
245         Item: TListItem;
246         KeyItem: TKeySettingItem;
247 begin
248
249         GikoSys.Setting.GestureEnabled := GestureCheckBox.Checked;
250         GikoSys.Setting.Gestures.ClearGesture;
251         for i := 0 to ListView.Items.Count - 1 do begin
252                 Item := ListView.Items[i];
253                 if TObject(Item.Data) is TKeySettingItem then begin
254                         KeyItem := TKeySettingItem(Item.Data);
255                         KeyItem.Action.ShortCut := KeyItem.ShortCut;
256                         GikoSys.Setting.Gestures.AddGesture( KeyItem.Gesture, KeyItem.Action );
257                 end;
258         end;
259         for i := 0 to ListView1.Items.Count - 1 do begin
260                 Item := ListView1.Items[i];
261                 if TObject(Item.Data) is TKeySettingItem then begin
262                         KeyItem := TKeySettingItem(Item.Data);
263                         KeyItem.Action.ShortCut := KeyItem.ShortCut;
264                         GikoSys.Setting.Gestures.AddGesture( KeyItem.Gesture, KeyItem.Action );
265                 end;
266         end;
267
268 end;
269
270 procedure TKeySettingForm.ListViewCompare(Sender: TObject; Item1,
271   Item2: TListItem; Data: Integer; var Compare: Integer);
272 begin
273         if Item1.SubItems[0] > Item2.SubItems[0] then
274                 Compare := 1
275         else if Item1.SubItems[0] < Item2.SubItems[0] then
276                 Compare := -1
277         else
278                 Compare := 0;
279 end;
280
281 procedure TKeySettingForm.GestureSetButtonClick(Sender: TObject);
282 const
283         ERR_MSG = '\93ü\97Í\82µ\82½\83W\83F\83X\83`\83\83\81[\82Í\8aù\82É\8eg\97p\82³\82ê\82Ä\82¢\82Ü\82·';
284         ERR_TITLE = '\83G\83\89\81[';
285 var
286         i: Integer;
287         Item: TListItem;
288         KeyItem: TKeySettingItem;
289         ActiveListView: TListView;
290 begin
291         if PageControl1.ActivePage.TabIndex <> 0 then
292                 ActiveListView := ListView1
293         else
294                 ActiveListView := ListView;
295
296         if ActiveListView.Selected = nil then Exit;
297         if (GetKeyState( VK_SHIFT ) and $80000000) <> 0 then begin
298                 GestureEdit.Text := '';
299                 Item := ActiveListView.Selected;
300                 Item.SubItems[2] := '';
301                 if TObject(Item.Data) is TKeySettingItem then begin
302                         KeyItem := TKeySettingItem(Item.Data);
303                         KeyItem.Gesture := '';
304                 end;
305                 Exit;
306         end;
307
308         // \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é
309         for i := 0 to ActiveListView.Items.Count - 1 do begin
310                 if ActiveListView.Selected = ActiveListView.Items[i] then
311                         Continue;
312                 Item := ActiveListView.Items[i];
313                 if TObject(Item.Data) is TKeySettingItem then begin
314                         KeyItem := TKeySettingItem(Item.Data);
315                         if (GestureEdit.Text <> '')
316                         and (KeyItem.Gesture = GestureEdit.Text) then begin
317                                 MsgBox(Handle, ERR_MSG, ERR_TITLE, MB_OK or MB_ICONSTOP);
318                                 HotKey.SetFocus;
319                                 Exit;
320                         end;
321                 end;
322         end;
323         // \83W\83F\83X\83`\83\83\81[\90Ý\92è
324         if TObject(ActiveListView.Selected.Data) is TKeySettingItem then begin
325                 KeyItem := TKeySettingItem(ActiveListView.Selected.Data);
326                 KeyItem.Gesture := GestureEdit.Text;
327                 if GestureEdit.Text = GUESTURE_NOTHING then
328                         ActiveListView.Selected.SubItems[2] := ''
329                 else
330                         ActiveListView.Selected.SubItems[2] := GestureEdit.Text;
331         end;
332 end;
333
334 procedure TKeySettingForm.OnGestureStart(Sender: TObject);
335 begin
336         GestureEdit.Text := '';
337 end;
338
339 procedure TKeySettingForm.OnGestureMove(Sender: TObject);
340 begin
341         GestureEdit.Text := MouseGesture.GetGestureStr;
342 end;
343
344 procedure TKeySettingForm.OnGestureEnd(Sender: TObject);
345 begin
346         GestureEdit.Text := MouseGesture.GetGestureStr;
347 end;
348
349 procedure TKeySettingForm.GestureCheckBoxClick(Sender: TObject);
350 begin
351         GestureEdit.Enabled := GestureCheckBox.Checked;
352         GestureSetButton.Enabled := GestureCheckBox.Checked;
353 end;
354
355 procedure TKeySettingForm.GestureEditChange(Sender: TObject);
356 begin
357
358         if GestureEdit.Text = '' then
359                 GestureEdit.Text := GUESTURE_NOTHING;
360
361 end;
362
363 end.