OSDN Git Service

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