Menu

[r2042]: / trunk / Src / FmCompilersDlg.pas  Maximize  Restore  History

Download this file

393 lines (356 with data), 11.7 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
{
* FmCompilersDlg.pas
*
* Implements a dialog box where the user can configure which Pascal compilers
* installed on the local system can be used by CodeSnip.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is FmCompilersDlg.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2005-2012 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit FmCompilersDlg;
interface
uses
// Delphi
SysUtils, ComCtrls, Controls, StdCtrls, ExtCtrls, Classes, Forms,
// Project
Compilers.UGlobals, FmCompilersDlg.FrBase, FmCompilersDlg.FrCompiler,
FmCompilersDlg.FrSearchDirs, FmCompilersDlg.FrLog, FmCompilersDlg.FrSwitches,
FmCompilersDlg.UBannerMgr, FmCompilersDlg.UCompilerListMgr, FmGenericOKDlg,
UBaseObjects;
type
{
TCompilersDlg:
Implements a dialog box where the user can configure which Pascal compilers
installed on the local system can be used by CodeSnip.
}
TCompilersDlg = class(TGenericOKDlg, INoPublicConstruct)
btnDetect: TButton;
lbCompilers: TListBox;
pbBanner: TPaintBox;
pcCompiler: TPageControl;
tsCompiler: TTabSheet;
tsLog: TTabSheet;
tsSwitches: TTabSheet;
frmCompiler: TCompilersDlgCompilerFrame;
frmSwitches: TCompilersDlgSwitchesFrame;
frmLog: TCompilersDlgLogFrame;
tsSearchDirs: TTabSheet;
frmSearchDirs: TCompilersDlgSearchDirsFrame;
procedure btnDetectClick(Sender: TObject);
procedure btnOKClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
/// <summary>Handles event triggered when user clicks on one of page
/// control tabs. Ensures page control has focus.</summary>
/// <remarks>Without this fix, page control does not always get focus when
/// a tab is clicked.</remarks>
procedure pcCompilerMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
strict private
var
fCompListMgr: TCompilerListMgr;
fBannerMgr: TCompilerBannerMgr;
fCurCompiler: ICompiler; // Reference to currently selected compiler
fLocalCompilers: ICompilers; // Copy of Compilers that is edited
fFrames: TArray<TCompilersDlgBaseFrame>;
procedure CompilerSelectHandler(Sender: TObject);
procedure CompilerChangeHandler(Sender: TObject);
procedure IterateFrames(Proc: TProc<TCompilersDlgBaseFrame>);
procedure SelectCompiler;
{Stores reference to currently selected local compiler and updates dialog
box controls with details of the compiler.
}
procedure UpdateCurrentCompiler;
{Updates local copy of currently selected compiler with entries in dialog
box.
}
procedure UpdateEditFrames;
function CheckCompilerExes: Boolean;
{Checks that all paths assigned as executable files for compilers are
valid Windows 32 executables.
@return True if all compiler exes are valid, False if an error is found.
}
strict protected
procedure InitForm; override;
{Populates and initialises controls.
}
procedure ArrangeForm; override;
{Dynamically sizes and aligns controls to allow for Vista UI font. Also
adjusts position of "Auto Detect Compilers" button on bottom button line.
}
public
class function Execute(AOwner: TComponent;
const ACompilers: ICompilers): Boolean;
{Displays the dialog box. The dialog updates a compilers object if user
OKs
@param AOwner [in] Control that owns this dialog.
@param ACompilers [in] Compilers object to be updated.
@return True if user OKs and compiler information is updated or False
if user cancels and compiler information is unchanged.
}
end;
implementation
uses
// Project
Compilers.UCompilers, IntfCommon, UCtrlArranger, UExeFileType, UMessageBox;
{$R *.dfm}
{ TCompilersDlg }
procedure TCompilersDlg.ArrangeForm;
{Dynamically sizes and aligns controls to allow for Vista UI font. Also
adjusts position of "Auto Detect Compilers" button on bottom button line.
}
begin
TCtrlArranger.SetLabelHeights(Self);
IterateFrames(
procedure (Frame: TCompilersDlgBaseFrame)
begin
Frame.ArrangeControls;
end
);
// size dialog and arrange inherited controls
inherited;
// arrange extra button in bottom button line
btnDetect.Left := pnlBody.Left;
btnDetect.Top := btnHelp.Top;
end;
procedure TCompilersDlg.btnDetectClick(Sender: TObject);
{Handles click on Auto Detect Compilers button. Sets executbale program path
for each compiler present that can detect its own path.
@param Sender [in] Not used.
}
var
Compiler: ICompiler; // refers to each compiler
resourcestring
// Text displayed in confirmation box
sOKToDetect = 'Detected compiler file names will overwrite any existing '
+ 'paths. Do you wish to continue?';
begin
if not TMessageBox.Confirm(Self, sOKToDetect) then
Exit;
// Record any changes to current compiler
UpdateCurrentCompiler;
// Loop thru all compilers attempting to detect exe files
for Compiler in fLocalCompilers do
begin
if Supports(Compiler, ICompilerAutoDetect) then
begin
if (Compiler as ICompilerAutoDetect).DetectExeFile then
begin
// Update currently displayed compiler details
if Compiler.GetID = fCurCompiler.GetID then
UpdateEditFrames;
end;
end;
end;
// Redisplay compiler list and current compiler title to reflect any changes
fCompListMgr.Refresh;
fBannerMgr.Refresh;
end;
procedure TCompilersDlg.btnOKClick(Sender: TObject);
{Handles OK button click. Update globals Compilers object and saves details to
persistent storage.
@param Sender [in] Not used.
}
begin
inherited;
// Ensure compiler object is up to date
UpdateCurrentCompiler;
// Check assigned exe files
if not CheckCompilerExes then
begin
ModalResult := mrNone;
Exit;
end;
ModalResult := mrOK;
end;
function TCompilersDlg.CheckCompilerExes: Boolean;
{Checks that all paths assigned as executable files for compilers are valid
Windows 32 executables.
@return True if all compiler exes are valid, False if an error is found.
}
var
Compiler: ICompiler; // refers to each compiler
resourcestring
// Error messages
sFileDoesNotExist = 'File specified for %s compiler doesn''t exist.';
sFileNotExe = 'File specified for %s compiler is not a valid executable file';
begin
Result := False;
// Scan through compilers to see if paths are valid
for Compiler in fLocalCompilers do
begin
if Compiler.GetExecFile = '' then
Continue;
if not FileExists(Compiler.GetExecFile) then
begin
TMessageBox.Error(Self, Format(sFileDoesNotExist, [Compiler.GetName]));
Exit;
end;
if ExeFileType(Compiler.GetExecFile) <> fkExe32 then
begin
TMessageBox.Error(Self, Format(sFileNotExe, [Compiler.GetName]));
Exit;
end;
end;
Result := True;
end;
procedure TCompilersDlg.CompilerChangeHandler(Sender: TObject);
begin
UpdateCurrentCompiler;
end;
procedure TCompilersDlg.CompilerSelectHandler(Sender: TObject);
{Handles a compiler selection in list box. Updates compiler that is being
deselected with details in dialog box controls then displays details of newly
selected compiler.
@param Sender [in] Not used.
}
begin
UpdateCurrentCompiler;
SelectCompiler;
end;
class function TCompilersDlg.Execute(AOwner: TComponent;
const ACompilers: ICompilers): Boolean;
{Displays the dialog box. The dialog updates a compilers object if user OKs
@param AOwner [in] Control that owns this dialog.
@param ACompilers [in] Compilers object to be updated.
@return True if user OKs and compiler information is updated or False if
user cancels and compiler information is unchanged.
}
var
Persister: IPersistCompilers; // object used to save object to storage
begin
with InternalCreate(AOwner) do
try
(fLocalCompilers as IAssignable).Assign(ACompilers);
Result := ShowModal = mrOK;
if Result then
begin
(ACompilers as IAssignable).Assign(fLocalCompilers);
Persister := TPersistCompilers.Create;
Persister.Save(ACompilers);
end;
finally
Free;
end;
end;
procedure TCompilersDlg.FormCreate(Sender: TObject);
{Initialises compiler information and creates owned objects.
@param Sender [in] Not used.
}
begin
inherited;
// Take a copy of global compilers object: stores updates until OK clicked
fLocalCompilers := TCompilersFactory.CreateCompilers;
fCompListMgr := TCompilerListMgr.Create(lbCompilers, fLocalCompilers);
fCompListMgr.OnSelect := CompilerSelectHandler;
fBannerMgr := TCompilerBannerMgr.Create(pbBanner);
fFrames := TArray<TCompilersDlgBaseFrame>.Create(
frmCompiler, frmSwitches, frmSearchDirs, frmLog
);
IterateFrames(
procedure (Frame: TCompilersDlgBaseFrame)
begin
Frame.OnChange := CompilerChangeHandler
end
);
end;
procedure TCompilersDlg.FormDestroy(Sender: TObject);
{Frees owned objects.
@param Sender [in] Not used.
}
begin
SetLength(fFrames, 0);
fBannerMgr.Free;
fCompListMgr.Free;
inherited;
end;
procedure TCompilersDlg.InitForm;
{Populates and initialises controls.
}
begin
inherited;
fCompListMgr.Initialise;
end;
procedure TCompilersDlg.IterateFrames(Proc: TProc<TCompilersDlgBaseFrame>);
var
Frame: TCompilersDlgBaseFrame;
begin
for Frame in fFrames do
Proc(Frame);
end;
procedure TCompilersDlg.pcCompilerMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if htOnItem in pcCompiler.GetHitTestInfoAt(X, Y) then
pcCompiler.SetFocus;
end;
procedure TCompilersDlg.SelectCompiler;
{Stores reference to currently selected local compiler and updates dialog box
controls with details of the compiler.
}
begin
fCurCompiler := fCompListMgr.Selected;
fBannerMgr.Compiler := fCurCompiler;
UpdateEditFrames;
end;
procedure TCompilersDlg.UpdateCurrentCompiler;
{Updates local copy of currently selected compiler with entries in dialog box.
}
var
WasAvailable: Boolean; // whether compiler was available before update
begin
if Assigned(fCurCompiler) then
begin
// Record whether compiler was available before update
WasAvailable := fCurCompiler.IsAvailable;
// Update compiler from info entered in frames
IterateFrames(
procedure(Frame: TCompilersDlgBaseFrame)
begin
Frame.UpdateCompiler;
end
);
// If availability has changed redraw selected list item and compiler title
// to give visual feedback of changed state
if WasAvailable <> fCurCompiler.IsAvailable then
begin
fCompListMgr.Refresh(fCurCompiler);
fBannerMgr.Refresh;
end;
end;
end;
procedure TCompilersDlg.UpdateEditFrames;
begin
IterateFrames(
procedure(Frame: TCompilersDlgBaseFrame)
begin
Frame.Compiler := fCurCompiler;
end
);
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.