OSDN Git Service

1.60.2.794
[gikonavigoeson/gikonavi.git] / InputAssist.pas
1 unit InputAssist;
2
3 interface
4
5 uses
6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7   Dialogs, StdCtrls, ComCtrls, GikoListView, Menus, ExtCtrls, ImgList,
8   InputAssistDataModule, StdActns, ActnList, GikoSystem;
9
10 type
11   TInputAssistForm = class(TForm)
12     Panel1: TPanel;
13     Panel2: TPanel;
14     GikoListView1: TGikoListView;
15     Panel3: TPanel;
16         Panel4: TPanel;
17         TextMemo: TMemo;
18     ColumnImageList: TImageList;
19     InputAssistFormActionList: TActionList;
20     EditCut1: TEditCut;
21     EditCopy1: TEditCopy;
22     EditPaste1: TEditPaste;
23     EditSelectAll1: TEditSelectAll;
24     EditUndo1: TEditUndo;
25     EditDelete1: TEditDelete;
26     GroupBox1: TGroupBox;
27     Panel5: TPanel;
28     CloseButton: TButton;
29     ApplyButton: TButton;
30     DeleteButton: TButton;
31     AddButton: TButton;
32     Panel6: TPanel;
33     Panel7: TPanel;
34     CategoryComboBox: TComboBox;
35     CategoryComboLabel: TLabel;
36     InsertButton: TButton;
37     InsertButtonAction: TAction;
38     CloseAction: TAction;
39     KeyPanel: TPanel;
40     KeyNameEdit: TLabeledEdit;
41     Splitter: TSplitter;
42     CategoryPanel: TPanel;
43     CategoryNameComboBox: TComboBox;
44     CategoryNameLabel: TLabel;
45     procedure FormCreate(Sender: TObject);
46     procedure GikoListView1SelectItem(Sender: TObject; Item: TListItem;
47       Selected: Boolean);
48     procedure AddButtonClick(Sender: TObject);
49     procedure DeleteButtonClick(Sender: TObject);
50     procedure ApplyButtonClick(Sender: TObject);
51     procedure FormClose(Sender: TObject; var Action: TCloseAction);
52     procedure GikoListView1Compare(Sender: TObject; Item1,
53       Item2: TListItem; Data: Integer; var Compare: Integer);
54     procedure GikoListView1ColumnClick(Sender: TObject;
55       Column: TListColumn);
56     procedure CloseButtonClick(Sender: TObject);
57     procedure CategoryComboBoxKeyPress(Sender: TObject; var Key: Char);
58     procedure CategoryComboBoxChange(Sender: TObject);
59     procedure InsertButtonActionUpdate(Sender: TObject);
60     procedure InsertButtonActionExecute(Sender: TObject);
61     procedure CloseActionExecute(Sender: TObject);
62   private
63         { Private \90é\8c¾ }
64         FSortColumn : Integer;
65         FInsertText : String;
66         procedure AddListViewItem(ResWord : TResistWord);
67     procedure SetCategory(combo: TComboBox; selected: String);
68     function ValidateKey(key, category: String): boolean;
69   public
70         { Public \90é\8c¾ }
71         procedure SetUpFromEditor();
72         procedure SetUpFromMain();
73         function GetInsertText(): String;
74   end;
75
76 var
77   InputAssistForm: TInputAssistForm;
78
79 implementation
80
81 uses Setting, MojuUtils;
82
83
84 {$R *.dfm}
85 //! \83t\83H\81[\83\80\90\90¬\82Ì\83C\83x\83\93\83g
86 procedure TInputAssistForm.FormCreate(Sender: TObject);
87 var
88         wp: TWindowPlacement;
89         i : Integer;
90         column: TListColumn;
91 begin
92         //\83E\83B\83\93\83h\83E\82Ì\88Ê\92u\90Ý\92è
93         wp.length := sizeof(wp);
94         wp.rcNormalPosition.Top := GikoSys.Setting.InputAssistFormTop;
95         wp.rcNormalPosition.Left := GikoSys.Setting.InputAssistFormLeft;
96
97         wp.rcNormalPosition.Bottom := GikoSys.Setting.InputAssistFormTop
98                                                                         + GikoSys.Setting.InputAssistFormHeight;
99         wp.rcNormalPosition.Right := GikoSys.Setting.InputAssistFormLeft
100                                                                         + GikoSys.Setting.InputAssistFormWidth;
101         wp.showCmd := SW_HIDE;
102         SetWindowPlacement(Handle, @wp);
103
104         FSortColumn := 0;
105         GikoListView1.Columns.Clear;
106         column := GikoListView1.Columns.Add;
107         column.ImageIndex := 0;
108         column.Caption := '\83L\81[';
109         column.Width := 150;
110         column := GikoListView1.Columns.Add;
111         column.Caption := '\83J\83e\83S\83\8a';
112         column.Width := 80;
113         for i := 0 to InputAssistDM.ResistWordCount - 1 do begin
114                 AddListViewItem(InputAssistDM.GetResistWord(i));
115         end;
116         //\83\\81[\83g\8fó\91Ô\82ð\89ð\8f\9c (\89ð\8f\9c\82µ\82È\82¢\82Æ\83A\83C\83e\83\80\82Ì\83L\81[\96¼\82ð\95Ï\8dX\82Å\82«\82È\82¢)
117         InputAssistDM.Sorted := False;
118 end;
119 //! \88ê\97\97\82É\83A\83C\83e\83\80\82ð\92Ç\89Á\82·\82é\8f\88\97\9d
120 procedure TInputAssistForm.AddListViewItem(ResWord : TResistWord);
121 var
122         item: TListItem;
123 begin
124         item := GikoListView1.Items.Add;
125         item.ImageIndex := -1;
126         item.Caption := resWord.GetKey;
127         item.SubItems.Add(resWord.GetCategory);
128         item.Data := resWord;
129 end;
130 //! \93o\98^\92P\8cê\88ê\97\97\82©\82ç\83A\83C\83e\83\80\82ð\91I\91ð\82µ\82½\82Æ\82«\82Ì\83C\83x\83\93\83g
131 procedure TInputAssistForm.GikoListView1SelectItem(Sender: TObject;
132   Item: TListItem; Selected: Boolean);
133 begin
134         if (Item <> nil) and (Item.Data <> nil) then begin
135                 KeyNameEdit.Text := TResistWord(Item.Data).GetKey;
136         CategoryNameComboBox.Text := TResistWord(Item.Data).GetCategory;
137                 TextMemo.Lines.Text := TResistWord(Item.Data).GetText;
138         end else begin
139                 TextMemo.Lines.Text := '';
140         end;
141 end;
142 //! \92Ç\89Á\83{\83^\83\93\89\9f\89º\8e\9e\82Ì\83C\83x\83\93\83g
143 procedure TInputAssistForm.AddButtonClick(Sender: TObject);
144 var
145         resWord : TResistWord;
146 begin
147         if (ValidateKey(KeyNameEdit.Text, CategoryNameComboBox.Text)) then begin
148                 if (not InputAssistDM.IsDupulicate(
149                         KeyNameEdit.Text, CategoryNameComboBox.Text) ) then begin
150                         resWord := InputAssistDM.Add(KeyNameEdit.Text);
151                         resWord.SetCategory(CategoryNameComboBox.Text);
152                         resWord.SetText(TextMemo.Text);
153                         AddListViewItem(resWord);
154             SetCategory(CategoryNameComboBox, resWord.GetCategory);
155                         GikoListView1.AlphaSort;
156                 end else begin
157                         ShowMessage('\93¯\88ê\82Ì\83L\81[\96¼\81E\83J\83e\83S\83\8a\96¼\82Å\8aù\82É\93o\98^\8dÏ\82Ý\82Å\82·\81B');
158                 end;
159         end;
160 end;
161 //! \83L\81[\96¼\81E\83J\83e\83S\83\8a\97L\8cø\83`\83F\83b\83N
162 function TInputAssistForm.ValidateKey(key, category: String): boolean;
163 begin
164     Result := True;
165     if (Length(key) = 0) then begin
166         ShowMessage('\83L\81[\96¼\82ð\90Ý\92è\82µ\82Ä\82­\82¾\82³\82¢\81B');
167         Result := False;
168     end else begin
169         if (Length(category) = 0) then begin
170             ShowMessage('\83J\83e\83S\83\8a\82ð\90Ý\92è\82µ\82Ä\82­\82¾\82³\82¢\81B');
171             Result := False;
172         end;
173     end;
174 end;
175
176 //! \8dí\8f\9c\83{\83^\83\93\89\9f\89º\8e\9e\82Ì\83C\83x\83\93\83g
177 procedure TInputAssistForm.DeleteButtonClick(Sender: TObject);
178 begin
179         if GikoListView1.Selected <> nil then begin
180                 InputAssistDM.DeleteResistWord(GikoListView1.Selected.Data);
181                 GikoListView1.Selected.Data := nil;
182                 GikoListView1.DeleteSelected;
183         end;
184 end;
185 //! \93K\97p\83{\83^\83\93\89\9f\89º\8e\9e\82Ì\83C\83x\83\93\83g
186 procedure TInputAssistForm.ApplyButtonClick(Sender: TObject);
187 var
188         resWord : TResistWord;
189 begin
190         if GikoListView1.Selected <> nil then begin
191         if (ValidateKey(KeyNameEdit.Text, CategoryNameComboBox.Text)) then begin
192             resWord := TResistWord(GikoListView1.Selected.Data);
193             // \95Ï\8dX\91O\82Ì\83L\81[\81^\83J\83e\83S\83\8a\82Æ\93¯\88ê\82à\82µ\82­\82Í\81A\91¼\82Æ\8fd\95¡\96³\82µ
194             if ((resWord.GetKey = KeyNameEdit.Text)
195                 and (resWord.GetCategory = CategoryNameComboBox.Text)) or
196                 (not InputAssistDM.IsDupulicate(
197                         KeyNameEdit.Text, CategoryNameComboBox.Text) ) then begin
198                 resWord.SetCategory(CategoryNameComboBox.Text);
199                 resWord.SetText(TextMemo.Text);
200                 // \83L\81[\82ª\95Ï\82í\82é\82Æ\82«\82Í\81AChangeKey\82ð\8cÄ\82Ô
201                 if (resWord.GetKey <> KeyNameEdit.Text) then begin
202                         resWord.SetKey(KeyNameEdit.Text);
203                     InputAssistDM.ChangeKey(resWord);
204                 end;
205                         // \88ê\97\97\82Ì\8dX\90V
206                             GikoListView1.Selected.Caption := resWord.GetKey;
207                         GikoListView1.Selected.SubItems[0] := resWord.GetCategory;
208                 SetCategory(CategoryNameComboBox, resWord.GetCategory);
209                         GikoListView1.AlphaSort;
210                 end else begin
211                         ShowMessage('\93¯\88ê\82Ì\83L\81[\96¼\81E\83J\83e\83S\83\8a\96¼\82Å\8aù\82É\93o\98^\8dÏ\82Ý\82Å\82·\81B');
212                     end;
213                 end;
214         end;
215 end;
216 //! \83t\83H\81[\83\80\82ð\95Â\82\82é\82Æ\82«\82Ì\83C\83x\83\93\83g
217 procedure TInputAssistForm.FormClose(Sender: TObject;
218   var Action: TCloseAction);
219 begin
220         GikoSys.Setting.InputAssistFormTop := Self.Top;
221         GikoSys.Setting.InputAssistFormLeft := Self.Left;
222         GikoSys.Setting.InputAssistFormHeight := Self.Height;
223         GikoSys.Setting.InputAssistFormWidth := Self.Width;
224         //\83\\81[\83g\8fó\91Ô\82Ì\90Ý\92è
225         InputAssistDM.Sorted := True;
226 end;
227 //! \93o\98^\92P\8cê\88ê\97\97\82Ì\83\\81[\83g\97p\82Ì\94ä\8ar\8f\88\97\9d
228 procedure TInputAssistForm.GikoListView1Compare(Sender: TObject; Item1,
229   Item2: TListItem; Data: Integer; var Compare: Integer);
230 begin
231         if ((FSortColumn and 2) > 0) then begin
232                 // \83J\83e\83S\83\8a\82Å\83\\81[\83g
233                 Compare := CompareStr(
234                         ZenToHan(Item1.SubItems[0]), ZenToHan(Item2.SubItems[0]));
235                 if (Compare = 0) then begin
236                         Compare := CompareStr(
237                                 ZenToHan(Item1.Caption), ZenToHan(Item2.Caption));
238                 end;
239         end else begin
240                 // \83L\81[\82Å\83\\81[\83g
241                 Compare := CompareStr(
242                         ZenToHan(Item1.Caption), ZenToHan(Item2.Caption));
243                 if (Compare = 0) then begin
244                         Compare := CompareStr(
245                                 ZenToHan(Item1.SubItems[0]), ZenToHan(Item2.SubItems[0]));
246                 end;
247         end;
248         // \8f¸\8f\87\8d~\8f\87\82Ì\94½\93]
249         if ((FSortColumn and 1) > 0) then begin
250                 Compare := Compare * -1;
251         end;
252         ;
253 end;
254 //! \93o\98^\92P\8cê\88ê\97\97\82Ì\83\8a\83X\83g\82Ì\83J\83\89\83\80\83N\83\8a\83b\83N\83C\83x\83\93\83g
255 procedure TInputAssistForm.GikoListView1ColumnClick(Sender: TObject;
256   Column: TListColumn);
257 var
258         i : Integer;
259 begin
260         if Column <> nil then begin
261                 // \83C\83\81\81[\83W\82Ì\8dí\8f\9c
262                 for i := 0 to GikoListView1.Columns.Count - 1 do begin
263                         GikoListView1.Column[i].ImageIndex := -1;
264                 end;
265
266                 // FSortColumn \8bô\90\94:\8f¸\8f\87 \8aï\90\94\81F\8d~\8f\87
267                 if Column.Caption = '\83L\81[' then begin
268                         if FSortColumn = 0 then begin
269                                 FSortColumn := 1;
270                         end else begin
271                                 FSortColumn := 0;
272                         end;
273                 end else begin
274                         if FSortColumn = 2 then begin
275                                 FSortColumn := 3;
276                         end else begin
277                                 FSortColumn := 2;
278                         end;
279                 end;
280                 Column.ImageIndex := (FSortColumn and 1);
281                 GikoListView1.AlphaSort;
282         end;
283
284 end;
285 //! \8fI\97¹\83{\83^\83\93\82ð\89\9f\89º\8e\9e\82Ì\83C\83x\83\93\83g
286 procedure TInputAssistForm.CloseButtonClick(Sender: TObject);
287 begin
288         Close();
289 end;
290 //! \83J\83e\83S\83\8a\8di\8d\9e\82Ý\83R\83\93\83{\83{\83b\83N\83X\82ð\93Ç\8eæ\90ê\97p\82É\82·\82é\82½\82ß\82Ì\83C\83x\83\93\83g\8f\88\97\9d
291 procedure TInputAssistForm.CategoryComboBoxKeyPress(Sender: TObject;
292   var Key: Char);
293 begin
294         Key := #0;
295 end;
296 //! \83J\83e\83S\83\8a\8di\8d\9e\82Ý\83R\83\93\83{\83{\83b\83N\83X\82Å\82Ì\83J\83e\83S\83\8a\95Ï\8dX\8f\88\97\9d
297 procedure TInputAssistForm.CategoryComboBoxChange(Sender: TObject);
298 var
299         i : Integer;
300         key : String;
301 begin
302         LockWindowUpdate(GikoListView1.Handle);
303         GikoListView1.Clear;
304         if (CategoryComboBox.ItemIndex <= 0) then begin
305                 for i := 0 to InputAssistDM.ResistWordCount - 1 do begin
306                         AddListViewItem(InputAssistDM.GetResistWord(i));
307                 end;
308         end else begin
309                 key := CategoryComboBox.Items[CategoryComboBox.ItemIndex];
310                 for i := 0 to InputAssistDM.ResistWordCount - 1 do begin
311                         if (key = InputAssistDM.GetResistWord(i).GetCategory) then begin
312                                 AddListViewItem(InputAssistDM.GetResistWord(i));
313                         end;
314                 end;
315         end;
316         LockWindowUpdate(0);
317 end;
318 //! \83J\83e\83S\83\8a\83R\83\93\83{\83{\83b\83N\83X\90Ý\92è
319 procedure TInputAssistForm.SetCategory(combo: TComboBox; selected: String);
320 var
321         cat : TStringList;
322     i : Integer;
323 begin
324         // \8c\9f\8dõ\97p
325         cat := TStringList.Create;
326         try
327                 InputAssistDM.GetCategoryList(cat);
328         combo.Items.BeginUpdate;
329         combo.Items.Clear;
330                 combo.Items.Add('');
331                 combo.Items.AddStrings(cat);
332                 combo.ItemIndex := 0;
333         combo.Items.EndUpdate;
334         // \91I\91ð\8dÏ\82Ý\82Ì\83J\83e\83S\83\8a\82É\83C\83\93\83f\83b\83N\83X\82ð\95Ï\8dX
335         i := combo.Items.IndexOf(selected);
336         if (i <> -1) then begin
337             combo.ItemIndex := i;
338         end;
339         finally
340                 cat.Free;
341         end;
342 end;
343
344 procedure TInputAssistForm.SetUpFromMain();
345 begin
346         Self.Caption := '\93ü\97Í\83A\83V\83X\83g\90Ý\92è';
347         Panel3.Visible := True;
348         Panel5.Visible := True;
349         Panel7.Visible := False;
350         TextMemo.ReadOnly := False;
351         FInsertText := '';
352         CloseAction.ShortCut := TShortCut(0);
353         // \92Ç\89Á\97p\83L\81[\93ü\97ÍOK
354     SetCategory(CategoryNameComboBox, '');
355 end;
356 procedure TInputAssistForm.SetUpFromEditor();
357 begin
358         Self.Caption := '\93ü\97Í\83A\83V\83X\83g';
359         Panel3.Visible := False;
360         Panel5.Visible := False;
361         Panel7.Visible := True;
362         TextMemo.ReadOnly := True;
363         FInsertText := '';
364         CloseAction.ShortCut := ShortCut(VK_ESCAPE, []);
365         // \8c\9f\8dõ\97p
366     SetCategory(CategoryComboBox, '');
367 end;
368 function TInputAssistForm.GetInsertText(): String;
369 begin
370         Result := FInsertText;
371 end;
372
373 procedure TInputAssistForm.InsertButtonActionUpdate(Sender: TObject);
374 begin
375         InsertButtonAction.Enabled := (GikoListView1.Selected <> nil);
376 end;
377
378 procedure TInputAssistForm.InsertButtonActionExecute(Sender: TObject);
379 begin
380         if (GikoListView1.Selected = nil) then begin
381                 FInsertText := '';
382                 Self.ModalResult := mrNone;
383         end else begin
384                 FInsertText :=  TResistWord(GikoListView1.Selected.Data).GetText;
385                 Self.ModalResult := mrOk;
386         end;
387 end;
388
389 procedure TInputAssistForm.CloseActionExecute(Sender: TObject);
390 begin
391         Self.ModalResult := mrCancel;
392 end;
393 end.