Menu

[r2017]: / trunk / Src / FmTestCompileDlg.pas  Maximize  Restore  History

Download this file

505 lines (452 with data), 15.9 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
{
* FmTestCompileDlg.pas
*
* Implements a dialog box which test compiles a snippet and displays the
* results.
*
* $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 FmTestCompileDlg.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2011 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit FmTestCompileDlg;
interface
uses
Classes, ActnList, StdCtrls, Forms, Controls, ExtCtrls, Messages,
Generics.Collections,
Compilers.UGlobals, DB.USnippet, FmGenericViewDlg, UBaseObjects, UCompileMgr,
ULEDImageList;
type
/// <summary>
/// Implements a dialog box that test compiles a snippet and displays the
/// results.
/// </summary>
TTestCompileDlg = class(TGenericViewDlg, INoPublicConstruct)
sbCompilers: TScrollBox;
lblSnippetName: TLabel;
lblSnippetNameDesc: TLabel;
alMain: TActionList;
actScrollPageUp: TAction;
actScrollPageDown: TAction;
actScrollLineUp: TAction;
actScrollLineDown: TAction;
btnViewErrors: TButton;
actViewErrors: TAction;
/// <summary>Instantiates owned object and notes dialog initialising.
/// </summary>
procedure FormCreate(Sender: TObject);
/// <summary>Tidies up form: frees owned object.</summary>
procedure FormDestroy(Sender: TObject);
/// <summary>Scrolls up one page in scroll box.</summary>
procedure actScrollPageUpExecute(Sender: TObject);
/// <summary>Scrolls down one page in scroll box.</summary>
procedure actScrollPageDownExecute(Sender: TObject);
/// <summary>Scrolls up one "line" in scroll box.</summary>
procedure actScrollLineUpExecute(Sender: TObject);
/// <summary>Scrolls down one "line" in scroll box.</summary>
procedure actScrollLineDownExecute(Sender: TObject);
/// <summary>Updates enabled state of View Errors action.</summary>
procedure actViewErrorsUpdate(Sender: TObject);
/// <summary>Displays any compile errors in dialog box.</summary>
procedure actViewErrorsExecute(Sender: TObject);
strict private
type
/// <summary>Component that displays information about a compiler and its
/// compile result.</summary>
TCompilerCtrl = class(TGraphicControl)
strict private
var
/// <summary>Value of Compiler property.</summary>
fCompiler: ICompiler;
/// <summary>Value of CompileResult property.</summary>
fCompileResult: TCompileResult;
class var
/// <summary>Reference to image list containing images of compile
/// result LED.</summary>
fLEDs: TLEDImageList;
strict private
/// <summary>Write accessor for Compiler property.</summary>
/// <remarks>Adjusts control height and invalidates it.</remarks>
procedure SetCompiler(Compiler: ICompiler);
/// <summary>Write accessor for CompileResult property.</summary>
/// <remarks>Causes control to be repainted.</remarks>
procedure SetCompileResult(CompileResult: TCompileResult);
/// <summary>Resizes control in response to changes in font.</summary>
procedure CMFontChanged(var Msg: TMessage); message CM_FONTCHANGED;
/// <summary>Calculates and returns required height of control.
/// </summary>
function GetHeight: Integer;
/// <summary>Updates height of control.</summary>
procedure UpdateHeight;
strict protected
/// <summary>Paints control, displaying compiler and compile result
/// information.</summary>
procedure Paint; override;
public
/// <summary>Creates LED image list shared between all instances.
/// </summary>
class constructor Create;
/// <summary>Frees shared LED image list instance.</summary>
class destructor Destroy;
/// <summary>Object constructor override. Sets default property values.
/// </summary>
constructor Create(AOwner: TComponent); override;
/// <summary>Override of inherited method to ignore any given height
/// and replace it with calculated height.</summary>
procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
/// <summary>Reference to compiler whose info is displayed.</summary>
property Compiler: ICompiler read fCompiler write SetCompiler;
/// <summary>Compiler result to be displayed.</summary>
property CompileResult: TCompileResult
read fCompileResult write SetCompileResult;
end;
type
/// <summary>List of compiler controls.</summary>
TCompilerCtrls = TList<TCompilerCtrl>;
strict private
var
/// <summary>Reference to snippet to be test compiled.</summary>
fSnippet: TSnippet;
/// <summary>Reference to compiler manager object used to perform and
/// record results of test compilation.</summary>
fCompileMgr: TCompileMgr;
/// <summary>List of compiler controls to be displayed.</summary>
fCompilerCtrlList: TCompilerCtrls;
/// <summary>Flag true if form is initialising. Set to false after
/// compilation complete and results displayed.</summary>
fInitialising: Boolean;
/// <summary>Creates all required compiler controls and stores in list.
/// </summary>
procedure CreateCompilerCtrls;
/// <summary>Displays results of test compilation.</summary>
procedure DisplayCompileResults(const Compilers: ICompilers);
strict protected
/// <summary>Arranges controls on form.</summary>
/// <remarks>Called from ancestor class.</remarks>
procedure ArrangeForm; override;
/// <summary>Creates and configures required controls.</summary>
/// <remarks>Called from ancestor class.</remarks>
procedure ConfigForm; override;
/// <summary>Performs test compilation after form is displayed and displays
/// results.</summary>
/// <remarks>Called from ancestor class.</remarks>
procedure AfterShowForm; override;
public
/// <summary>Displays dialog box that performs test compilation and
/// displays results.</summary>
/// <param name="AOwner">TComponent [in] Reference to any owning control.
/// </param>
/// <param name="ACompileMgr">TCompileMgr [in] Object used to perform test
/// compilation and record results.</param>
/// <param name="ASnippet">TSnippet [in] Snippet to be test compiled.
/// </param>
class procedure Execute(const AOwner: TComponent;
const ACompileMgr: TCompileMgr; const ASnippet: TSnippet);
end;
implementation
uses
// Delphi
Math, Windows, Graphics,
// Project
UColours, UCtrlArranger, UFontHelper;
{$R *.dfm}
{ TTestCompileDlg }
procedure TTestCompileDlg.actScrollLineDownExecute(Sender: TObject);
begin
sbCompilers.Perform(WM_VSCROLL, SB_LINEDOWN, 0);
end;
procedure TTestCompileDlg.actScrollLineUpExecute(Sender: TObject);
begin
sbCompilers.Perform(WM_VSCROLL, SB_LINEUP, 0);
end;
procedure TTestCompileDlg.actScrollPageDownExecute(Sender: TObject);
begin
sbCompilers.Perform(WM_VSCROLL, SB_PAGEDOWN, 0);
end;
procedure TTestCompileDlg.actScrollPageUpExecute(Sender: TObject);
begin
sbCompilers.Perform(WM_VSCROLL, SB_PAGEUP, 0);
end;
procedure TTestCompileDlg.actViewErrorsExecute(Sender: TObject);
begin
fCompileMgr.ShowErrors;
end;
procedure TTestCompileDlg.actViewErrorsUpdate(Sender: TObject);
begin
actViewErrors.Enabled := not fInitialising and fCompileMgr.HaveErrors;
end;
procedure TTestCompileDlg.AfterShowForm;
begin
inherited;
Enabled := False;
try
fCompileMgr.Compile(pnlBody, fSnippet, DisplayCompileResults);
finally
Enabled := True;
end;
fInitialising := False;
end;
procedure TTestCompileDlg.ArrangeForm;
const
// Various margins, offsets and sizes
MaxBodyPanelWidth = 760;
ViewErrorsBtnRightMargin = 16;
SnippetLblTop = 0;
SnippetLblBottomMargin = 8;
CompilerCtrlListTop = 0;
var
CompCtrl: TCompilerCtrl; // each compiler control
NextTop: Integer; // top of next compiler control in scroll box
begin
TCtrlArranger.SetLabelHeight(lblSnippetNameDesc);
// Set horizontal alignments
TCtrlArranger.MoveToRightOf(lblSnippetNameDesc, lblSnippetName);
btnViewErrors.Left := pnlBody.Left;
// Set widths
pnlBody.ClientWidth := Min(
Max(
pnlBody.ClientWidth,
Max(
// allow room for long snippet names
TCtrlArranger.RightOf(lblSnippetName),
// allow room for button at bottom
btnViewErrors.Width + ViewErrorsBtnRightMargin
+ TCtrlArranger.RightOf(btnHelp)- btnClose.Left
)
),
MaxBodyPanelWidth // body panel to be no more this any case!
);
sbCompilers.Width := pnlBody.ClientWidth;
// Set vertical alignments
TCtrlArranger.AlignVCentres(
SnippetLblTop, [lblSnippetName, lblSnippetNameDesc]
);
sbCompilers.Top := TCtrlArranger.BottomOf(
lblSnippetName, SnippetLblBottomMargin
);
sbCompilers.Height := pnlBody.ClientHeight - sbCompilers.Top;
btnViewErrors.Top := btnHelp.Top;
// Arrange compiler controls vertically within scroll box
NextTop := CompilerCtrlListTop;
for CompCtrl in fCompilerCtrlList do
begin
CompCtrl.Top := NextTop;
Inc(NextTop, CompCtrl.Height);
end;
// Set compiler controls' widths: must do this after above loop so that any
// vertical scroll bar in scroll box has been created
for CompCtrl in fCompilerCtrlList do
CompCtrl.Width := sbCompilers.ClientWidth;
inherited;
end;
procedure TTestCompileDlg.ConfigForm;
begin
inherited;
CreateCompilerCtrls;
// Set required label fonts and captions
TFontHelper.SetDefaultBaseFont(lblSnippetName.Font, False);
if fSnippet.UserDefined then
lblSnippetName.Font.Color := clUserSnippet;
lblSnippetName.Caption := fSnippet.Name;
end;
procedure TTestCompileDlg.CreateCompilerCtrls;
var
Compiler: ICompiler; // each supported compiler
Ctrl: TCompilerCtrl; // compiler control
begin
for Compiler in fCompileMgr.Compilers do
begin
Ctrl := TCompilerCtrl.Create(Self);
Ctrl.Parent := sbCompilers;
Ctrl.Compiler := Compiler;
fCompilerCtrlList.Add(Ctrl);
end;
end;
procedure TTestCompileDlg.DisplayCompileResults(const Compilers: ICompilers);
var
Ctrl: TCompilerCtrl; // each compiler control in list
begin
for Ctrl in fCompilerCtrlList do
Ctrl.CompileResult := Compilers[Ctrl.Compiler.GetID].GetLastCompileResult;
end;
class procedure TTestCompileDlg.Execute(const AOwner: TComponent;
const ACompileMgr: TCompileMgr; const ASnippet: TSnippet);
begin
Assert(Assigned(ACompileMgr), ClassName + '.Execute: ACompileMgr is nil');
Assert(Assigned(ASnippet), ClassName + '.Execute: ASnippet is nil');
with InternalCreate(AOwner) do
try
fCompileMgr := ACompileMgr;
fSnippet := ASnippet;
ShowModal;
finally
Free;
end;
end;
procedure TTestCompileDlg.FormCreate(Sender: TObject);
begin
fInitialising := True;
inherited;
fCompilerCtrlList := TCompilerCtrls.Create;
end;
procedure TTestCompileDlg.FormDestroy(Sender: TObject);
begin
fCompilerCtrlList.Free;
inherited;
end;
{ TTestCompileDlg.TCompilerCtrl }
procedure TTestCompileDlg.TCompilerCtrl.CMFontChanged(var Msg: TMessage);
begin
inherited;
UpdateHeight;
end;
constructor TTestCompileDlg.TCompilerCtrl.Create(AOwner: TComponent);
begin
inherited;
ParentColor := True;
ParentFont := True;
fCompileResult := crQuery;
Width := 180;
end;
class constructor TTestCompileDlg.TCompilerCtrl.Create;
begin
fLEDs := TLEDImageList.Create(nil);
end;
class destructor TTestCompileDlg.TCompilerCtrl.Destroy;
begin
fLEDS.Free;
end;
function TTestCompileDlg.TCompilerCtrl.GetHeight: Integer;
const
CompilerGlyphHeight = 24; // estimated height of compiler glyph
Padding = 4; // padding applied to calculated height
begin
if not Assigned(fCompiler) then
Exit(0);
Result := Max(
CompilerGlyphHeight, Max(Canvas.TextHeight(fCompiler.GetName), fLEDs.Height)
) + Padding;
end;
procedure TTestCompileDlg.TCompilerCtrl.Paint;
const
GlyphLeftOffset = 4; // left offset of any compiler glyph
NameLeftOffset = 28; // left offset of compiler name text
LEDLeftMargin = 4; // margin before LED image (after divider)
LEDRightMargin = 6; // margin after LED image
DividerLeftMargin = 2; // margin before divider bar (after name)
DividerWidth = 2; // width of divider bar
var
MaxNameWidth: Integer; // max width for displaying compiler name text
NameRect: TRect; // rectangle containing compiler name text
LEDLeftOffset: Integer; // left offset of LEDs in control
DividerLeftOffset: Integer; // left offset of divider bar in control
DividerRect: TRect; // rectangle used to display divider bar
BoundsRect: TRect; // control's bounding rectangle (control coords)
Glyph: TBitmap; // reference to any compiler glyph
Name: string; // name of compiler
begin
inherited;
if not Assigned(fCompiler) then
Exit;
Glyph := fCompiler.GetGlyph;
Name := fCompiler.GetName;
// Calculate widths and offsets that depend on control width
LEDLeftOffset := Width - LEDRightMargin - fLEDs.Width;
DividerLeftOffset := LEDLeftOffset - LEDLeftMargin - DividerWidth;
MaxNameWidth := DividerLeftOffset - DividerLeftMargin - NameLeftOffset;
Canvas.Font := Font;
Canvas.Brush.Color := Color;
// Draw compiler glyph, if any
if Assigned(Glyph) then
Canvas.BrushCopy(
Bounds(
GlyphLeftOffset,
(Height - Glyph.Height) div 2,
Glyph.Width,
Glyph.Height
),
Glyph,
Rect(0, 0, Glyph.Width, Glyph.Height),
clFuchsia
);
// Draw compiler name
if fCompiler.IsAvailable then
Canvas.Font.Color := clWindowText
else
Canvas.Font.Color := clGrayText;
NameRect := Bounds(
NameLeftOffset,
(Height - Canvas.TextHeight(Name)) div 2,
MaxNameWidth,
Canvas.TextHeight(Name)
);
Canvas.TextRect(NameRect, Name, [tfLeft, tfNoPrefix]);
// Draw divider
DividerRect := Bounds(DividerLeftOffset, 0, DividerWidth, Height - 3);
Windows.DrawEdge(
Canvas.Handle,
DividerRect,
EDGE_RAISED,
BF_LEFT or BF_RIGHT
);
// Draw compiler result
fLEDs.Draw(
Canvas,
Point(LEDLeftOffset, (Height - fLEDs.Height) div 2),
fCompileResult
);
// Draw bottom ruling
BoundsRect := Bounds(0, Height - 2, Width, 2);
Windows.DrawEdge(
Canvas.Handle,
BoundsRect,
EDGE_RAISED,
BF_BOTTOM or BF_TOP
);
end;
procedure TTestCompileDlg.TCompilerCtrl.SetBounds(ALeft, ATop, AWidth,
AHeight: Integer);
begin
AHeight := GetHeight;
inherited;
end;
procedure TTestCompileDlg.TCompilerCtrl.SetCompiler(Compiler: ICompiler);
begin
fCompiler := Compiler;
UpdateHeight;
Invalidate;
end;
procedure TTestCompileDlg.TCompilerCtrl.SetCompileResult(
CompileResult: TCompileResult);
begin
fCompileResult := CompileResult;
Invalidate;
end;
procedure TTestCompileDlg.TCompilerCtrl.UpdateHeight;
begin
SetBounds(Left, Top, Width, 0); // Height is set automatically
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.