Menu

[f90d0d]: / Source / NewClassFrm.pas  Maximize  Restore  History

Download this file

366 lines (315 with data), 11.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
{
This file is part of Dev-C++
Copyright (c) 2004 Bloodshed Software
Dev-C++ is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Dev-C++ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Dev-C++; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
}
unit NewClassFrm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, CBUtils, StatementList;
type
TNewClassForm = class(TForm)
lblClassName: TLabel;
txtName: TEdit;
grpInheritance: TGroupBox;
lblAccess: TLabel;
lblInherit: TLabel;
cmbClass: TComboBox;
lblCppFile: TLabel;
txtCppFile: TEdit;
lblHFile: TLabel;
txtHFile: TEdit;
btnBrowseCpp: TSpeedButton;
btnBrowseH: TSpeedButton;
chkAddToProject: TCheckBox;
btnCreate: TButton;
btnCancel: TButton;
cmbScope: TComboBox;
chkInherit: TCheckBox;
lblHeaderFile: TLabel;
txtIncFile: TEdit;
lblArguments: TLabel;
txtArgs: TEdit;
chkConstruct: TCheckBox;
chkDestruct: TCheckBox;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormShow(Sender: TObject);
procedure chkInheritClick(Sender: TObject);
procedure txtNameChange(Sender: TObject);
procedure txtCppFileChange(Sender: TObject);
procedure btnBrowseCppClick(Sender: TObject);
procedure btnCreateClick(Sender: TObject);
procedure cmbClassChange(Sender: TObject);
procedure txtNameKeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
procedure LoadText;
public
{ Public declarations }
end;
implementation
uses
main, CppParser, MultiLangSupport, version, editor, devcfg;
{$R *.dfm}
procedure TNewClassForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
procedure TNewClassForm.FormShow(Sender: TObject);
var
sl: TStringList;
begin
LoadText;
sl := TStringList.Create;
try
MainForm.CppParser.GetClassesList(sl);
cmbClass.Items.Assign(sl);
finally
sl.Free;
end;
// public is used most of the time?
cmbScope.ItemIndex := cmbScope.Items.IndexOf('public');
// Check if the statement the user selected is a class...
if Assigned(MainForm.ClassBrowser.Selected) and
Assigned(MainForm.ClassBrowser.Selected.Data) and
(PStatement(MainForm.ClassBrowser.Selected.Data)^._Kind = skClass) then begin
// If we are spawned from the class browser, set inheritcance to selected class
cmbClass.ItemIndex := cmbClass.Items.IndexOf(PStatement(MainForm.ClassBrowser.Selected.Data)^._Command);
if cmbClass.ItemIndex <> -1 then
chkInherit.Checked := True;
end else begin
cmbClass.Text := '';
chkInherit.Checked := False;
end;
txtName.SetFocus;
end;
procedure TNewClassForm.chkInheritClick(Sender: TObject);
begin
cmbScope.Enabled := chkInherit.Checked;
cmbClass.Enabled := chkInherit.Checked;
txtIncFile.Enabled := chkInherit.Checked;
end;
procedure TNewClassForm.txtNameChange(Sender: TObject);
begin
if txtName.Text <> '' then begin
txtCppFile.Text := MainForm.Project.Directory + txtName.Text + '.cpp';
txtHFile.Text := MainForm.Project.Directory + txtName.Text + '.h';
// Make sure one can actually see what is going on
txtCppFile.SelStart := Length(txtCppFile.Text) - 1;
txtHFile.SelStart := Length(txtHFile.Text) - 1;
end else begin
txtCppFile.Text := '';
txtHFile.Text := '';
end;
btnCreate.Enabled := (txtName.Text <> '') and (txtCppFile.Text <> '') and (txtHFile.Text <> '');
end;
procedure TNewClassForm.txtCppFileChange(Sender: TObject);
begin
btnCreate.Enabled := (txtName.Text <> '') and
(txtCppFile.Text <> '') and
(txtHFile.Text <> '');
end;
procedure TNewClassForm.btnBrowseCppClick(Sender: TObject);
begin
with TOpenDialog.Create(self) do try
if Sender = btnBrowseCpp then begin
FileName := ExtractFileName(txtCppFile.Text);
InitialDir := ExtractFilePath(txtCppFile.Text);
Filter := FLT_CPPS;
DefaultExt := 'cpp';
end else begin
FileName := ExtractFileName(txtHFile.Text);
InitialDir := ExtractFilePath(txtHFile.Text);
Filter := FLT_HEADS;
DefaultExt := 'h';
end;
if Execute then begin
if Sender = btnBrowseCpp then
txtCppFile.Text := FileName
else
txtHFile.Text := FileName;
end;
finally
Free;
end;
end;
procedure TNewClassForm.btnCreateClick(Sender: TObject);
var
Node: PStatementNode;
Statement, InheritStatement: PStatement;
idx: integer;
e, headere: TEditor;
hfName: AnsiString;
hFile: integer;
begin
// HEADER FILE IMPLEMENTATION
if chkAddToProject.Checked then begin
idx := MainForm.Project.NewUnit(False, nil, txtHFile.Text);
e := MainForm.Project.OpenUnit(idx);
if idx = -1 then begin
MessageDlg('Cannot add header file to project...', mtError, [mbOk], 0);
Exit;
end;
end else begin
hFile := FileCreate(txtHFile.Text);
if hFile > 0 then begin
FileClose(hFile);
e := MainForm.EditorList.GetEditorFromFileName(txtHFile.Text);
end else begin
MessageDlg('Cannot create header file...', mtError, [mbOk], 0);
Exit;
end;
end;
if not Assigned(e) then begin
MessageDlg('Cannot open header file in editor...', mtError, [mbOk], 0);
Exit;
end;
hfName := UpperCase(ExtractFileName(txtHFile.Text));
hfName := StringReplace(hfName, '.', '_', [rfReplaceAll]);
hfName := StringReplace(hfName, ' ', '_', [rfReplaceAll]);
e.Text.Lines.BeginUpdate;
try
// Add header guard
e.Text.Lines.Add('#ifndef ' + hfName);
e.Text.Lines.Add('#define ' + hfName);
e.Text.Lines.Add('');
// Add inherited piece ": public foo"
if chkInherit.Checked and (txtIncFile.Text <> '') then begin
InheritStatement := nil;
// Find class we inherit from
Node := MainForm.CppParser.Statements.FirstNode;
while Assigned(Node) do begin
Statement := Node^.Data;
if (Statement^._Kind = skClass) and (Statement^._Command = cmbClass.Text) and
(MainForm.Project.Units.Indexof(Statement^._DefinitionFileName) <> -1) then begin
InheritStatement := Statement;
break;
end;
Node := Node^.NextNode;
end;
if Assigned(InheritStatement) then
e.Text.Lines.Add('#include "' + txtIncFile.Text + '"')
else
e.Text.Lines.Add('#include <' + txtIncFile.Text + '>');
e.Text.Lines.Add('');
end;
// Include inheritance or not, assume it's valid code
if chkInherit.Checked and (cmbClass.Text <> '') then
e.Text.Lines.Add('class ' + txtName.Text + ' : ' + cmbScope.Text + ' ' + cmbClass.Text)
else
e.Text.Lines.Add('class ' + txtName.Text);
e.Text.Lines.Add('{');
e.Text.Lines.Add(#9'public:');
// Create one optional constructor with args
if chkConstruct.Checked then
e.Text.Lines.Add(#9#9 + txtName.Text + '(' + txtArgs.Text + ');');
// Create one optional destructor
if chkDestruct.Checked then
e.Text.Lines.Add(#9#9'~' + txtName.Text + '();');
e.Text.Lines.Add(#9'protected:');
e.Text.Lines.Add('};');
e.Text.Lines.Add('');
e.Text.Lines.Add('#endif');
finally
e.Text.Lines.EndUpdate;
end;
e.Text.Modified := True;
headere := e; // keep pointer so we can activate
// CPP FILE IMPLEMENTATION
if chkAddToProject.Checked then begin
idx := MainForm.Project.NewUnit(False, nil, txtCppFile.Text);
e := MainForm.Project.OpenUnit(idx);
if idx = -1 then begin
MessageDlg('Cannot add implementation file to project...', mtError, [mbOk], 0);
Exit;
end;
end else begin
hFile := FileCreate(txtCppFile.Text);
if hFile > 0 then begin
FileClose(hFile);
e := MainForm.EditorList.GetEditorFromFileName(txtCppFile.Text);
end else begin
MessageDlg('Cannot create implementation file...', mtError, [mbOk], 0);
Exit;
end;
end;
if not Assigned(e) then begin
MessageDlg('Cannot open implementation file in editor...', mtError, [mbOk], 0);
Exit;
end;
e.Text.Lines.BeginUpdate;
e.Text.Lines.Add('#include "' + ExtractFileName(txtHFile.Text) + '"');
if chkConstruct.Checked then begin
e.Text.Lines.Add('');
e.Text.Lines.Add(txtName.Text + '::' + txtName.Text + '(' + txtArgs.Text + ')');
e.Text.Lines.Add('{');
e.Text.Lines.Add('}');
end;
if chkDestruct.Checked then begin
e.Text.Lines.Add('');
e.Text.Lines.Add(txtName.Text + '::~' + txtName.Text + '()');
e.Text.Lines.Add('{');
e.Text.Lines.Add('}');
end;
e.Text.Lines.EndUpdate;
e.Text.Modified := True;
// Open CPP file by default
headere.Activate;
end;
procedure TNewClassForm.cmbClassChange(Sender: TObject);
var
st: PStatement;
begin
if cmbClass.Items.IndexOf(cmbClass.Text) <> -1 then begin
st := PStatement(cmbClass.Items.Objects[cmbClass.Items.IndexOf(cmbClass.Text)]);
if Assigned(st) then
txtIncFile.Text := ExtractFileName(st^._DefinitionFileName)
else
txtIncFile.Text := LowerCase(cmbClass.Text) + '.h';
end else begin
if cmbClass.Text <> '' then
txtIncFile.Text := LowerCase(cmbClass.Text) + '.h'
else
txtIncFile.Text := '';
end;
end;
procedure TNewClassForm.txtNameKeyPress(Sender: TObject; var Key: Char);
begin
if not (Key in ['_', 'a'..'z', 'A'..'Z', '0'..'9', #1..#31]) then begin
Key := #0;
Abort;
end;
end;
procedure TNewClassForm.LoadText;
begin
// Set interface font
Font.Name := devData.InterfaceFont;
Font.Size := devData.InterfaceFontSize;
Caption := Lang[ID_POP_NEWCLASS];
lblClassName.Caption := Lang[ID_NEWCLASS_NAME];
lblArguments.Caption := Lang[ID_NEWMEMB_ARGS];
chkConstruct.Caption := Lang[ID_NEWCLASS_CONSTRUCTOR];
chkDestruct.Caption := Lang[ID_NEWCLASS_DESTRUCTOR];
chkInherit.Caption := Lang[ID_NEWCLASS_INHERIT];
grpInheritance.Caption := Lang[ID_NEWCLASS_INHERITANCE];
lblAccess.Caption := Lang[ID_NEWVAR_SCOPE] + ':';
lblInherit.Caption := Lang[ID_NEWCLASS_INHERIT_FROM];
lblHeaderFile.Caption := Lang[ID_NEWCLASS_INHERIT_HDR];
lblCppFile.Caption := Lang[ID_NEWCLASS_CPPFILE];
lblHFile.Caption := Lang[ID_NEWCLASS_HFILE];
chkAddToProject.Caption := Lang[ID_NEWCLASS_ADDTOPROJ];
btnCreate.Caption := Lang[ID_NEWVAR_BTN_CREATE];
btnCancel.Caption := Lang[ID_NEWVAR_BTN_CANCEL];
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.