1 {
2  ***************************************************************************
3  *                                                                         *
4  *   This source is free software; you can redistribute it and/or modify   *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This code is distributed in the hope that it will be useful, but      *
10  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
12  *   General Public License for more details.                              *
13  *                                                                         *
14  *   A copy of the GNU General Public License is available on the World    *
15  *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
16  *   obtain it by writing to the Free Software Foundation,                 *
17  *   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.   *
18  *                                                                         *
19  ***************************************************************************
20 
21   Author: Ondrej Pokorny
22 
23   Abstract:
24     A simple dialog to select code creation options.
25 }
26 unit CodeCreationDlg;
27 
28 {$mode objfpc}{$H+}
29 
30 interface
31 
32 uses
33   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
34   ButtonPanel, SourceChanger, LazarusIDEStrConsts, EnvironmentOpts, CodeCompletionTool,
35   ExtCtrls;
36 
37 type
38   //this dialog can easily be reused.
39   //for now it is used only in the method event assignment code creation
40 
41   TCodeCreationDialog = class(TForm)
42     ButtonPanel: TButtonPanel;
43     NewIdentLabel: TLabel;
44     SectionRadioGroup: TRadioGroup;
45     LocationRadioGroup: TRadioGroup;
46     procedure FormKeyPress(Sender: TObject; var Key: char);
47     procedure LocationRadioGroupClick(Sender: TObject);
48     procedure SectionRadioGroupDblClick(Sender: TObject);
49     procedure SectionRadioGroupKeyPress(Sender: TObject; var Key: char);
50   protected
51     procedure DoCreate; override;
52     procedure DoShow; override;
53   public
54     procedure Init(const ANewIdent: string; const AIsMethod: Boolean;
55       const Options: TCodeCreationDlgResult);
56     procedure Final(const AIsMethod: Boolean; var Options: TCodeCreationDlgResult);
57   end;
58 
ShowCodeCreationDialognull59 function ShowCodeCreationDialog(const ANewIdent: string; const AIsMethod: Boolean;
60     out Options: TCodeCreationDlgResult): Boolean;
61 
62 implementation
63 
64 {$R *.lfm}
65 
ShowCodeCreationDialognull66 function ShowCodeCreationDialog(const ANewIdent: string; const AIsMethod: Boolean;
67   out Options: TCodeCreationDlgResult): Boolean;
68 var
69   Dlg: TCodeCreationDialog;
70 begin
71   if AIsMethod then
72     Options := EnvironmentOptions.LastEventMethodCCResult
73   else
74     Options := EnvironmentOptions.LastVariableCCResult;
75 
76   Dlg := TCodeCreationDialog.Create(Application);
77   try
78     Dlg.Init(ANewIdent, AIsMethod, Options);
79 
80     Result := Dlg.ShowModal = mrOK;
81 
82     if Result then
83       Dlg.Final(AIsMethod, Options);
84   finally
85     Dlg.Free;
86   end;
87 end;
88 
89 { TCodeCreationDialog }
90 
91 procedure TCodeCreationDialog.DoCreate;
92 var
93   S: TInsertClassSection;
94 begin
95   inherited DoCreate;
96 
97   KeyPreview := True;
98 
99   Hint := lisYouCanSelectItemsBySimplyPressingUnderscoredLetter;
100 
101   SectionRadioGroup.Items.Clear;
102   for S := Low(TInsertClassSection) to High(TInsertClassSection) do
103     SectionRadioGroup.Items.Add(InsertClassSectionAmpNames[S]);
104 
105   LocationRadioGroup.Items.Clear;
106   LocationRadioGroup.Items.Add(lisLocal);
107   LocationRadioGroup.Items.Add(lisClass);
108 end;
109 
110 procedure TCodeCreationDialog.DoShow;
111 begin
112   inherited DoShow;
113 
114   LocationRadioGroupClick(nil);
115 end;
116 
117 procedure TCodeCreationDialog.Final(const AIsMethod: Boolean;
118   var Options: TCodeCreationDlgResult);
119 begin
120   Options.ClassSection := TInsertClassSection(SectionRadioGroup.ItemIndex);
121   Options.Location := TCreateCodeLocation(LocationRadioGroup.ItemIndex);
122   if AIsMethod then
123     EnvironmentOptions.LastEventMethodCCResult := Options
124   else
125     EnvironmentOptions.LastVariableCCResult := Options;
126 end;
127 
128 procedure TCodeCreationDialog.FormKeyPress(Sender: TObject;
129   var Key: char);
130 begin
131   case Key of
132     #27: ModalResult := mrCancel;
133     'p':
134       if SectionRadioGroup.Enabled then
135       begin
136         SectionRadioGroup.ItemIndex := Ord(icsPrivate);
137         ModalResult := mrOK;
138       end;
139     'r':
140       if SectionRadioGroup.Enabled then
141       begin
142         SectionRadioGroup.ItemIndex := Ord(icsProtected);
143         ModalResult := mrOK;
144       end;
145     'u':
146       if SectionRadioGroup.Enabled then
147       begin
148         SectionRadioGroup.ItemIndex := Ord(icsPublic);
149         ModalResult := mrOK;
150       end;
151     's':
152       if SectionRadioGroup.Enabled then
153       begin
154         SectionRadioGroup.ItemIndex := Ord(icsPublished);
155         ModalResult := mrOK;
156       end;
157     'l':
158       if LocationRadioGroup.Enabled then
159       begin
160         LocationRadioGroup.ItemIndex := Ord(cclLocal);
161         ModalResult := mrOK;
162       end;
163     'c':
164       if LocationRadioGroup.Enabled then
165       begin
166         LocationRadioGroup.ItemIndex := Ord(cclClass);
167         // do not set ModalResult, the user has to set section
168       end;
169   end;
170 end;
171 
172 procedure TCodeCreationDialog.Init(const ANewIdent: string;
173   const AIsMethod: Boolean; const Options: TCodeCreationDlgResult);
174 begin
175   Caption := lisCodeCreationDialogCaption;
176   LocationRadioGroup.Caption := lisCodeCreationDialogLocation;
177   SectionRadioGroup.Caption := lisCodeCreationDialogClassSection;
178 
179   if ANewIdent<>'' then
180     NewIdentLabel.Caption := ANewIdent
181   else
182     NewIdentLabel.Visible := False;
183 
184   if Ord(Options.ClassSection) < SectionRadioGroup.Items.Count then
185     SectionRadioGroup.ItemIndex := Ord(Options.ClassSection)
186   else
187     SectionRadioGroup.ItemIndex := 0;
188 
189   if Ord(Options.Location) < LocationRadioGroup.Items.Count then
190     LocationRadioGroup.ItemIndex := Ord(Options.Location)
191   else
192     LocationRadioGroup.ItemIndex := 0;
193 
194   if AIsMethod then
195   begin
196     LocationRadioGroup.ItemIndex := Ord(cclClass);
197     LocationRadioGroup.Enabled := False;
198   end;
199 end;
200 
201 procedure TCodeCreationDialog.LocationRadioGroupClick(Sender: TObject);
202 begin
203   SectionRadioGroup.Enabled := LocationRadioGroup.ItemIndex = Ord(cclClass);
204 end;
205 
206 procedure TCodeCreationDialog.SectionRadioGroupDblClick(Sender: TObject);
207 begin
208   ModalResult := mrOK;
209 end;
210 
211 procedure TCodeCreationDialog.SectionRadioGroupKeyPress(
212   Sender: TObject; var Key: char);
213 begin
214   if Key = #13 then
215     ModalResult := mrOK;
216 end;
217 
218 initialization
219   ShowCodeCreationDlg := @ShowCodeCreationDialog;
220 
221 end.
222 
223