OSDN Git Service

・スレタイの特定ワードを非表示にする機能に「©bbspink.com」も追加
[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, IniFiles;
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     DiffButton: TButton;
46     procedure FormCreate(Sender: TObject);
47     procedure GikoListView1SelectItem(Sender: TObject; Item: TListItem;
48       Selected: Boolean);
49     procedure AddButtonClick(Sender: TObject);
50     procedure DeleteButtonClick(Sender: TObject);
51     procedure ApplyButtonClick(Sender: TObject);
52     procedure FormClose(Sender: TObject; var Action: TCloseAction);
53     procedure GikoListView1Compare(Sender: TObject; Item1,
54       Item2: TListItem; Data: Integer; var Compare: Integer);
55     procedure GikoListView1ColumnClick(Sender: TObject;
56       Column: TListColumn);
57     procedure CloseButtonClick(Sender: TObject);
58     procedure CategoryComboBoxKeyPress(Sender: TObject; var Key: Char);
59     procedure CategoryComboBoxChange(Sender: TObject);
60     procedure InsertButtonActionUpdate(Sender: TObject);
61     procedure InsertButtonActionExecute(Sender: TObject);
62     procedure CloseActionExecute(Sender: TObject);
63     procedure DiffButtonClick(Sender: TObject);
64   private
65         { Private \90é\8c¾ }
66         FSortColumn : Integer;
67         FInsertText : String;
68         procedure AddListViewItem(ResWord : TResistWord);
69     procedure SetCategory(combo: TComboBox; selected: String);
70     function ValidateKey(key, category: String): boolean;
71   public
72         { Public \90é\8c¾ }
73         procedure SetUpFromEditor();
74         procedure SetUpFromMain();
75         function GetInsertText(): String;
76   end;
77
78 var
79   InputAssistForm: TInputAssistForm;
80
81 implementation
82
83 uses Setting, MojuUtils;
84
85
86 {$R *.dfm}
87 //! \83t\83H\81[\83\80\90\90¬\82Ì\83C\83x\83\93\83g
88 procedure TInputAssistForm.FormCreate(Sender: TObject);
89 var
90         wp: TWindowPlacement;
91         i : Integer;
92         column: TListColumn;
93 begin
94         //\83E\83B\83\93\83h\83E\82Ì\88Ê\92u\90Ý\92è
95         wp.length := sizeof(wp);
96         wp.rcNormalPosition.Top := GikoSys.Setting.InputAssistFormTop;
97         wp.rcNormalPosition.Left := GikoSys.Setting.InputAssistFormLeft;
98
99         wp.rcNormalPosition.Bottom := GikoSys.Setting.InputAssistFormTop
100                                                                         + GikoSys.Setting.InputAssistFormHeight;
101         wp.rcNormalPosition.Right := GikoSys.Setting.InputAssistFormLeft
102                                                                         + GikoSys.Setting.InputAssistFormWidth;
103         wp.showCmd := SW_HIDE;
104         SetWindowPlacement(Handle, @wp);
105
106         FSortColumn := 0;
107         GikoListView1.Columns.Clear;
108         column := GikoListView1.Columns.Add;
109         column.ImageIndex := 0;
110         column.Caption := '\83L\81[';
111         column.Width := 150;
112         column := GikoListView1.Columns.Add;
113         column.Caption := '\83J\83e\83S\83\8a';
114         column.Width := 80;
115         for i := 0 to InputAssistDM.ResistWordCount - 1 do begin
116                 AddListViewItem(InputAssistDM.GetResistWord(i));
117         end;
118         //\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¢)
119         InputAssistDM.Sorted := False;
120 end;
121 //! \88ê\97\97\82É\83A\83C\83e\83\80\82ð\92Ç\89Á\82·\82é\8f\88\97\9d
122 procedure TInputAssistForm.AddListViewItem(ResWord : TResistWord);
123 var
124         item: TListItem;
125 begin
126         item := GikoListView1.Items.Add;
127         item.ImageIndex := -1;
128         item.Caption := resWord.GetKey;
129         item.SubItems.Add(resWord.GetCategory);
130         item.Data := resWord;
131 end;
132 //! \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
133 procedure TInputAssistForm.GikoListView1SelectItem(Sender: TObject;
134   Item: TListItem; Selected: Boolean);
135 begin
136         if (Item <> nil) and (Item.Data <> nil) then begin
137                 KeyNameEdit.Text := TResistWord(Item.Data).GetKey;
138         CategoryNameComboBox.Text := TResistWord(Item.Data).GetCategory;
139                 TextMemo.Lines.Text := TResistWord(Item.Data).GetText;
140         end else begin
141                 TextMemo.Lines.Text := '';
142         end;
143 end;
144 //! \92Ç\89Á\83{\83^\83\93\89\9f\89º\8e\9e\82Ì\83C\83x\83\93\83g
145 procedure TInputAssistForm.AddButtonClick(Sender: TObject);
146 var
147         resWord : TResistWord;
148 begin
149         if (ValidateKey(KeyNameEdit.Text, CategoryNameComboBox.Text)) then begin
150                 if (not InputAssistDM.IsDupulicate(
151                         KeyNameEdit.Text, CategoryNameComboBox.Text) ) then begin
152                         resWord := InputAssistDM.Add(KeyNameEdit.Text);
153                         resWord.SetCategory(CategoryNameComboBox.Text);
154                         resWord.SetText(TextMemo.Text);
155                         AddListViewItem(resWord);
156             SetCategory(CategoryNameComboBox, resWord.GetCategory);
157                         GikoListView1.AlphaSort;
158                 end else begin
159                         ShowMessage('\93¯\88ê\82Ì\83L\81[\96¼\81E\83J\83e\83S\83\8a\96¼\82Å\8aù\82É\93o\98^\8dÏ\82Ý\82Å\82·\81B');
160                 end;
161         end;
162 end;
163 //! \83L\81[\96¼\81E\83J\83e\83S\83\8a\97L\8cø\83`\83F\83b\83N
164 function TInputAssistForm.ValidateKey(key, category: String): boolean;
165 begin
166     Result := True;
167     if (Length(key) = 0) then begin
168         ShowMessage('\83L\81[\96¼\82ð\90Ý\92è\82µ\82Ä\82­\82¾\82³\82¢\81B');
169         Result := False;
170     end else begin
171         if (Length(category) = 0) then begin
172             ShowMessage('\83J\83e\83S\83\8a\82ð\90Ý\92è\82µ\82Ä\82­\82¾\82³\82¢\81B');
173             Result := False;
174         end;
175     end;
176 end;
177
178 //! \8dí\8f\9c\83{\83^\83\93\89\9f\89º\8e\9e\82Ì\83C\83x\83\93\83g
179 procedure TInputAssistForm.DeleteButtonClick(Sender: TObject);
180 begin
181         if GikoListView1.Selected <> nil then begin
182                 InputAssistDM.DeleteResistWord(GikoListView1.Selected.Data);
183                 GikoListView1.Selected.Data := nil;
184                 GikoListView1.DeleteSelected;
185         end;
186 end;
187 //! \93K\97p\83{\83^\83\93\89\9f\89º\8e\9e\82Ì\83C\83x\83\93\83g
188 procedure TInputAssistForm.ApplyButtonClick(Sender: TObject);
189 var
190         resWord : TResistWord;
191 begin
192         if GikoListView1.Selected <> nil then begin
193         if (ValidateKey(KeyNameEdit.Text, CategoryNameComboBox.Text)) then begin
194             resWord := TResistWord(GikoListView1.Selected.Data);
195             // \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µ
196             if ((resWord.GetKey = KeyNameEdit.Text)
197                 and (resWord.GetCategory = CategoryNameComboBox.Text)) or
198                 (not InputAssistDM.IsDupulicate(
199                         KeyNameEdit.Text, CategoryNameComboBox.Text) ) then begin
200                 resWord.SetCategory(CategoryNameComboBox.Text);
201                 resWord.SetText(TextMemo.Text);
202                 // \83L\81[\82ª\95Ï\82í\82é\82Æ\82«\82Í\81AChangeKey\82ð\8cÄ\82Ô
203                 if (resWord.GetKey <> KeyNameEdit.Text) then begin
204                         resWord.SetKey(KeyNameEdit.Text);
205                     InputAssistDM.ChangeKey(resWord);
206                 end;
207                         // \88ê\97\97\82Ì\8dX\90V
208                             GikoListView1.Selected.Caption := resWord.GetKey;
209                         GikoListView1.Selected.SubItems[0] := resWord.GetCategory;
210                 SetCategory(CategoryNameComboBox, resWord.GetCategory);
211                         GikoListView1.AlphaSort;
212                 end else begin
213                         ShowMessage('\93¯\88ê\82Ì\83L\81[\96¼\81E\83J\83e\83S\83\8a\96¼\82Å\8aù\82É\93o\98^\8dÏ\82Ý\82Å\82·\81B');
214                     end;
215                 end;
216         end;
217 end;
218 //! \83t\83H\81[\83\80\82ð\95Â\82\82é\82Æ\82«\82Ì\83C\83x\83\93\83g
219 procedure TInputAssistForm.FormClose(Sender: TObject;
220   var Action: TCloseAction);
221 begin
222         GikoSys.Setting.InputAssistFormTop := Self.Top;
223         GikoSys.Setting.InputAssistFormLeft := Self.Left;
224         GikoSys.Setting.InputAssistFormHeight := Self.Height;
225         GikoSys.Setting.InputAssistFormWidth := Self.Width;
226         //\83\\81[\83g\8fó\91Ô\82Ì\90Ý\92è
227         InputAssistDM.Sorted := True;
228 end;
229 //! \93o\98^\92P\8cê\88ê\97\97\82Ì\83\\81[\83g\97p\82Ì\94ä\8ar\8f\88\97\9d
230 procedure TInputAssistForm.GikoListView1Compare(Sender: TObject; Item1,
231   Item2: TListItem; Data: Integer; var Compare: Integer);
232 begin
233         if ((FSortColumn and 2) > 0) then begin
234                 // \83J\83e\83S\83\8a\82Å\83\\81[\83g
235                 Compare := CompareStr(
236                         ZenToHan(Item1.SubItems[0]), ZenToHan(Item2.SubItems[0]));
237                 if (Compare = 0) then begin
238                         Compare := CompareStr(
239                                 ZenToHan(Item1.Caption), ZenToHan(Item2.Caption));
240                 end;
241         end else begin
242                 // \83L\81[\82Å\83\\81[\83g
243                 Compare := CompareStr(
244                         ZenToHan(Item1.Caption), ZenToHan(Item2.Caption));
245                 if (Compare = 0) then begin
246                         Compare := CompareStr(
247                                 ZenToHan(Item1.SubItems[0]), ZenToHan(Item2.SubItems[0]));
248                 end;
249         end;
250         // \8f¸\8f\87\8d~\8f\87\82Ì\94½\93]
251         if ((FSortColumn and 1) > 0) then begin
252                 Compare := Compare * -1;
253         end;
254         ;
255 end;
256 //! \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
257 procedure TInputAssistForm.GikoListView1ColumnClick(Sender: TObject;
258   Column: TListColumn);
259 var
260         i : Integer;
261 begin
262         if Column <> nil then begin
263                 // \83C\83\81\81[\83W\82Ì\8dí\8f\9c
264                 for i := 0 to GikoListView1.Columns.Count - 1 do begin
265                         GikoListView1.Column[i].ImageIndex := -1;
266                 end;
267
268                 // FSortColumn \8bô\90\94:\8f¸\8f\87 \8aï\90\94\81F\8d~\8f\87
269                 if Column.Caption = '\83L\81[' then begin
270                         if FSortColumn = 0 then begin
271                                 FSortColumn := 1;
272                         end else begin
273                                 FSortColumn := 0;
274                         end;
275                 end else begin
276                         if FSortColumn = 2 then begin
277                                 FSortColumn := 3;
278                         end else begin
279                                 FSortColumn := 2;
280                         end;
281                 end;
282                 Column.ImageIndex := (FSortColumn and 1);
283                 GikoListView1.AlphaSort;
284         end;
285
286 end;
287 //! \8fI\97¹\83{\83^\83\93\82ð\89\9f\89º\8e\9e\82Ì\83C\83x\83\93\83g
288 procedure TInputAssistForm.CloseButtonClick(Sender: TObject);
289 begin
290         Close();
291 end;
292 //! \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
293 procedure TInputAssistForm.CategoryComboBoxKeyPress(Sender: TObject;
294   var Key: Char);
295 begin
296         Key := #0;
297 end;
298 //! \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
299 procedure TInputAssistForm.CategoryComboBoxChange(Sender: TObject);
300 var
301         i : Integer;
302         key : String;
303 begin
304         LockWindowUpdate(GikoListView1.Handle);
305         GikoListView1.Clear;
306         if (CategoryComboBox.ItemIndex <= 0) then begin
307                 for i := 0 to InputAssistDM.ResistWordCount - 1 do begin
308                         AddListViewItem(InputAssistDM.GetResistWord(i));
309                 end;
310         end else begin
311                 key := CategoryComboBox.Items[CategoryComboBox.ItemIndex];
312                 for i := 0 to InputAssistDM.ResistWordCount - 1 do begin
313                         if (key = InputAssistDM.GetResistWord(i).GetCategory) then begin
314                                 AddListViewItem(InputAssistDM.GetResistWord(i));
315                         end;
316                 end;
317         end;
318         LockWindowUpdate(0);
319 end;
320 //! \83J\83e\83S\83\8a\83R\83\93\83{\83{\83b\83N\83X\90Ý\92è
321 procedure TInputAssistForm.SetCategory(combo: TComboBox; selected: String);
322 var
323         cat : TStringList;
324     i : Integer;
325 begin
326         // \8c\9f\8dõ\97p
327         cat := TStringList.Create;
328         try
329                 InputAssistDM.GetCategoryList(cat);
330         combo.Items.BeginUpdate;
331         combo.Items.Clear;
332                 combo.Items.Add('');
333                 combo.Items.AddStrings(cat);
334                 combo.ItemIndex := 0;
335         combo.Items.EndUpdate;
336         // \91I\91ð\8dÏ\82Ý\82Ì\83J\83e\83S\83\8a\82É\83C\83\93\83f\83b\83N\83X\82ð\95Ï\8dX
337         i := combo.Items.IndexOf(selected);
338         if (i <> -1) then begin
339             combo.ItemIndex := i;
340         end;
341         finally
342                 cat.Free;
343         end;
344 end;
345
346 procedure TInputAssistForm.SetUpFromMain();
347 begin
348         Self.Caption := '\93ü\97Í\83A\83V\83X\83g\90Ý\92è';
349         Panel3.Visible := True;
350         Panel5.Visible := True;
351         Panel7.Visible := False;
352         TextMemo.ReadOnly := False;
353         FInsertText := '';
354         CloseAction.ShortCut := TShortCut(0);
355         // \92Ç\89Á\97p\83L\81[\93ü\97ÍOK
356     SetCategory(CategoryNameComboBox, '');
357 end;
358 procedure TInputAssistForm.SetUpFromEditor();
359 begin
360         Self.Caption := '\93ü\97Í\83A\83V\83X\83g';
361         Panel3.Visible := False;
362         Panel5.Visible := False;
363         Panel7.Visible := True;
364         TextMemo.ReadOnly := True;
365         FInsertText := '';
366         CloseAction.ShortCut := ShortCut(VK_ESCAPE, []);
367         // \8c\9f\8dõ\97p
368     SetCategory(CategoryComboBox, '');
369 end;
370 function TInputAssistForm.GetInsertText(): String;
371 begin
372         Result := FInsertText;
373 end;
374
375 procedure TInputAssistForm.InsertButtonActionUpdate(Sender: TObject);
376 begin
377         InsertButtonAction.Enabled := (GikoListView1.Selected <> nil);
378 end;
379
380 procedure TInputAssistForm.InsertButtonActionExecute(Sender: TObject);
381 begin
382         if (GikoListView1.Selected = nil) then begin
383                 FInsertText := '';
384                 Self.ModalResult := mrNone;
385         end else begin
386                 FInsertText :=  TResistWord(GikoListView1.Selected.Data).GetText;
387                 Self.ModalResult := mrOk;
388         end;
389 end;
390
391 procedure TInputAssistForm.CloseActionExecute(Sender: TObject);
392 begin
393         Self.ModalResult := mrCancel;
394 end;
395
396 procedure TInputAssistForm.DiffButtonClick(Sender: TObject);
397 var
398         ini: TIniFile;
399     DefPath: String;
400     SecList: TStringList;
401     KeyList: TStringList;
402     Cnt: Integer;
403     Cnt2: Integer;
404     MaxCnt: Integer;
405     MaxCnt2: Integer;
406     RegCnt: Integer;
407     DatString: String;
408         resWord : TResistWord;
409     ResMsg: String;
410 begin
411         ini := TIniFile.Create(GikoSys.Setting.GetDefaultFilesFileName);
412     DefPath := ini.ReadString('InputAssist', 'FROM', '');
413     ini.Free;
414     if (DefPath = '') then begin
415         Application.MessageBox('default\83t\83@\83C\83\8b\82ª\92è\8b`\82³\82ê\82Ä\82¢\82Ü\82¹\82ñ\81B', '\8d·\95ª\93o\98^', MB_OK or MB_ICONERROR);
416         Exit;
417     end;
418
419     DefPath := GikoSys.GetAppDir + DefPath;
420     if (FileExists(DefPath) = False) then begin
421         Application.MessageBox('default\83t\83@\83C\83\8b\82ª\8c©\82Â\82©\82è\82Ü\82¹\82ñ\81B', '\8d·\95ª\93o\98^', MB_OK or MB_ICONERROR);
422         Exit;
423     end;
424
425     RegCnt := 0;
426     SecList := TStringList.Create;
427         ini := TIniFile.Create(DefPath);
428
429     ini.ReadSections(SecList);
430     if (SecList.Count > 0) then begin
431         KeyList := TStringList.Create;
432         MaxCnt := SecList.Count - 1;
433         for Cnt := 0 to MaxCnt do begin;
434             KeyList.Clear;
435             ini.ReadSection(SecList.Strings[Cnt], KeyList);
436             if (KeyList.Count > 0) then begin;
437                 MaxCnt2 := KeyList.Count - 1;
438                 for Cnt2 := 0 to MaxCnt2 do begin
439                     DatString := ini.ReadString(SecList.Strings[Cnt], KeyList.Strings[Cnt2], '');
440                     if (DatString = '') then
441                         Continue;
442
443                     if (not InputAssistDM.IsDupulicate(
444                             KeyList.Strings[Cnt2], SecList.Strings[Cnt]) ) then begin
445                         resWord := InputAssistDM.Add(KeyList.Strings[Cnt2]);
446                         resWord.SetCategory(SecList.Strings[Cnt]);
447                         resWord.SetText(DatString);
448                         AddListViewItem(resWord);
449                         RegCnt := RegCnt + 1;
450                     end;
451                 end;
452             end;
453         end;
454         KeyList.Free;
455     end;
456
457     ini.Free;
458     SecList.Free;
459
460     if (RegCnt > 0) then begin
461         SetCategory(CategoryNameComboBox, '');
462         GikoListView1.AlphaSort;
463     end;
464
465     ResMsg := IntToStr(RegCnt) + '\8c\8f\93o\98^\82µ\82Ü\82µ\82½\81B';
466     Application.MessageBox(PChar(ResMsg), '\8d·\95ª\93o\98^', MB_OK or MB_ICONINFORMATION);
467 end;
468
469 end.