Menu

[r4761]: / branches / parsnip / Src / FmPrintDlg.pas  Maximize  Restore  History

Download this file

433 lines (392 with data), 12.2 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
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2007-2014, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements a print dialogue box.
}
unit FmPrintDlg;
interface
uses
// Delphi
Controls,
StdCtrls,
ExtCtrls,
Classes,
ImgList,
Windows,
ComCtrls,
Forms,
// Project
CS.SourceCode.Hiliter.Themes,
FmGenericOKDlg,
FrRTFShowCase,
URTFBuilder;
type
{
TPrintDlg:
Class that implements a print dialogue box.
}
TPrintDlg = class(TGenericOKDlg)
btnSetup: TButton;
cbPrinters: TComboBox;
gpPrinter: TGroupBox;
ilPrinters: TImageList;
lblPrinters: TLabel;
gpFormatOptions: TGroupBox;
chkSyntaxHighlight: TCheckBox;
chkUseColor: TCheckBox;
frmPreview: TRTFShowCaseFrame;
procedure btnOKClick(Sender: TObject);
procedure btnSetupClick(Sender: TObject);
procedure cbPrintersDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure FormCreate(Sender: TObject);
procedure FormatCheckBoxClick(Sender: TObject);
strict private
type
{
TDocFormatPreview
Class that renders a preview of effect of document formatting changes
on a printed document.
}
TDocFormatPreview = class(TObject)
strict private
fRE: TRichEdit;
{Reference to richedit control used to render preview}
procedure HiliteSource(const UseColour, SyntaxHighlight: Boolean;
Builder: TRTFBuilder);
{Generates sample highlighted source code.
@param UseColour [in] Whether to use colour or mono highlighter.
@param SyntaxHighlight [in] Whether source code to be highlighted.
@param Builder [in] Object that receives highlighted source code.
}
public
constructor Create(const RE: TRichEdit);
{Class constructor. Sets up object.
@param RE [in] Rich edit control used to render preview.
}
procedure Generate(const UseColour, SyntaxHighlight: Boolean);
{Generates RTF source code for preview.
@param UseColour [in] Whether preview to be in colour or monochrome.
@param SyntaxHighlight [in] Whether preview source code to be syntax
hilited.
}
end;
strict private
procedure PopulatePrinterList;
{Stores name of each installed printer in combo box and selects default
printer.
}
procedure InitOptions;
{Initialises options controls to default values.
}
procedure DisplayPreview;
{Displays document formatting options preview.
}
strict protected
procedure InitForm; override;
{Initialises dialogue box controls.
}
public
class function Execute(const AOwner: TComponent): Boolean;
{Displays print dialogue box and gets information entered by user.
@param AOwner [in] Owner of dialogue box.
@return True if user OKs dialogue box and False if user cancels.
}
end;
implementation
uses
// Delphi
Printers, Graphics,
// Project
CS.Config,
CS.SourceCode.Hiliter.Brushes,
CS.SourceCode.Hiliter.Renderers,
UClassHelpers,
UConsts,
UMessageBox,
UPageSetupDlgMgr,
UPreferences,
UPrintInfo,
URTFStyles,
URTFUtils,
UStructs,
UStrUtils;
{$R *.dfm}
{ TPrintDlg }
procedure TPrintDlg.btnOKClick(Sender: TObject);
{OK button click handler. Records results of user input.
@param Sender [in] Not used.
}
var
Options: TPrintOptions; // printing options
begin
inherited;
// Record user options
Options := [];
if chkUseColor.Checked then
Include(Options, poUseColour);
if chkSyntaxHighlight.Checked then
Include(Options, poSyntaxHilite);
PrintInfo.PrintOptions := Options;
// Update selected printer
if cbPrinters.ItemIndex >= 0 then
Printer.PrinterIndex := cbPrinters.ItemIndex;
end;
procedure TPrintDlg.btnSetupClick(Sender: TObject);
{Positions Page Setup button to right of window and vertically level with OK,
Cancel and Help buttons.
@param Sender [in] Not used.
}
begin
TPageSetupDlgMgr.Execute(Self);
end;
procedure TPrintDlg.cbPrintersDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
{Handles draw item event for printers combo box. We display printer name and
a glyph indicating if default printer.
@param Control [in] Reference to combo box.
@param Index [in] Index of list item being drawn.
@param Rect [in] Bounding rectangle of list item being drawn.
@param State [in] State of control: focussed, selected etc.
}
// ---------------------------------------------------------------------------
function IsDefaultPrinter(const PrnName: string): Boolean;
{Checks if named printer is default.
@param PrnName [in] Name of printer to be checked.
@return True if printer is default, False if not.
}
var
OldPrinterIdx: Integer; // saves current printer
begin
try
// Save currently selected printer
OldPrinterIdx := Printer.PrinterIndex;
try
// Check printer name against default
Printer.PrinterIndex := -1; // this selects default printer
Result := StrSameText(PrnName, Printer.Printers[Printer.PrinterIndex]);
finally
// Restore selected printer index
Printer.PrinterIndex := OldPrinterIdx;
end;
except
on EPrinter do
// Checks for exception: will be because no default printer available
// Fixes bug 2875857
Result := False;
end;
end;
// ---------------------------------------------------------------------------
var
Combo: TComboBox; // reference to combo box
Canvas: TCanvas; // reference to combo's canvas
ItemRect: TRectEx; // rectangle bounding the item being drawn
ImgRect: TRectEx; // rectangle bounding the printer glyph
TxtRect: TRect; // rectangle bounding the printer name text
PrnName: string; // name of printer being displayed
TextH: Integer; // height of printer name text in combo canvas
const
// Map of normal and default printer images indexes in image list.
PrinterImgIds: array[Boolean] of Integer = (24, 40);
begin
// Record various useful values
Combo := Control as TComboBox;
PrnName := Combo.Items[Index];
Canvas := Combo.Canvas;
ItemRect := Rect;
// Set font style: bold if printer is default
if IsDefaultPrinter(PrnName) then
Canvas.Font.Style := [fsBold]
else
Canvas.Font.Style := [];
// Erase background
Canvas.Pen.Color := Combo.Color;
Canvas.Brush.Color := Combo.Color;
Canvas.FillRect(ItemRect);
// Draw any highlighting
if (odSelected in State) then
begin
// selected: draw highlight in highlight colour
Canvas.Brush.Color := clHighlight;
Canvas.Font.Color := clHighlightText;
Canvas.Rectangle(ItemRect);
end
else
// no highlighting: just ensure font colour correct
Canvas.Font.Color := Combo.Font.Color;
// Draw printer glyph
ImgRect := TRectEx.CreateBounds(
ItemRect.Left + 2,
ItemRect.Top + (ItemRect.Height - ilPrinters.Height) div 2,
ilPrinters.Width,
ilPrinters.Height
);
// standard printer glyph is at index 0: Ord(False)
// default printer glyph is at index 1: Ord(True)
ilPrinters.Draw(
Canvas, ImgRect.Left, ImgRect.Top, PrinterImgIds[IsDefaultPrinter(PrnName)]
);
// Draw printer name
TextH := Canvas.TextHeight(PrnName);
TxtRect := TRectEx.Create(
ImgRect.Right + 4,
ItemRect.Top + (ItemRect.Height - TextH) div 2,
ItemRect.Right - 2,
ItemRect.Top + (ItemRect.Height - TextH) div 2 + TextH
);
Canvas.TextRect(TxtRect, PrnName, [tfLeft, tfEndEllipsis, tfTop]);
end;
procedure TPrintDlg.DisplayPreview;
{Displays document formatting options preview.
}
var
Preview: TDocFormatPreview; // object that renders preview
begin
Preview := TDocFormatPreview.Create(frmPreview.RichEdit);
try
Preview.Generate(chkUseColor.Checked, chkSyntaxHighlight.Checked);
finally
Preview.Free;
end;
end;
class function TPrintDlg.Execute(const AOwner: TComponent): Boolean;
{Displays print dialogue box and gets information entered by user.
@param AOwner [in] Owner of dialogue box.
@return True if user OKs dialogue box and False if user cancels.
}
begin
with Create(AOwner) do
try
Result := ShowModal = mrOK;
finally
Free;
end;
end;
procedure TPrintDlg.FormatCheckBoxClick(Sender: TObject);
begin
DisplayPreview;
end;
procedure TPrintDlg.FormCreate(Sender: TObject);
begin
inherited;
ilPrinters.LoadFromResource(RT_RCDATA, 'ACTIONIMAGES', 16, clFuchsia);
end;
procedure TPrintDlg.InitForm;
{Initialises dialogue box controls.
}
begin
inherited;
PopulatePrinterList;
InitOptions;
DisplayPreview;
end;
procedure TPrintDlg.InitOptions;
{Initialises options controls to default values.
}
begin
chkUseColor.Checked := poUseColour in PrintInfo.PrintOptions;
chkSyntaxHighlight.Checked := poSyntaxHilite in PrintInfo.PrintOptions;
end;
procedure TPrintDlg.PopulatePrinterList;
{Stores name of each installed printer in combo box and selects default
printer.
}
var
PrinterName: string;
begin
for PrinterName in Printer.Printers do
cbPrinters.Items.Add(PrinterName);
try
cbPrinters.ItemIndex := Printer.PrinterIndex;
except
on EPrinter do
// Exception can occur if no default printer: part of fix for bug 2875857
cbPrinters.ItemIndex := -1;
end;
end;
{ TPrintDlg.TDocFormatPreview }
constructor TPrintDlg.TDocFormatPreview.Create(const RE: TRichEdit);
begin
Assert(Assigned(RE), ClassName + '.Create: RE is nil');
inherited Create;
fRE := RE;
end;
procedure TPrintDlg.TDocFormatPreview.Generate(const UseColour,
SyntaxHighlight: Boolean);
resourcestring
// Heading and dummy paragraph text
sHeading = 'Sample';
sBodyText = 'Lorem ipsum dolor sit diam amet.';
var
Builder: TRTFBuilder; // object used to assemble required RTF code
begin
Builder := TRTFBuilder.Create(0); // use default code page
try
// Set global document font and paragraph spacing
Builder.FontTable.Add('Tahoma', rgfSwiss, 0);
Builder.SetParaSpacing(TRTFParaSpacing.Create(0.0, 2.0));
// Add heading text
Builder.BeginGroup;
Builder.SetFontSize(10);
Builder.SetFontStyle([fsBold]);
Builder.AddText(sHeading);
Builder.EndGroup;
Builder.EndPara;
// Add dummy paragraph
Builder.SetFontSize(9);
Builder.AddText(sBodyText);
Builder.EndPara;
// Add highlighted source code
HiliteSource(UseColour, SyntaxHighlight, Builder);
Builder.EndPara;
// Load document into rich edit
TRichEditHelper.Load(fRE, Builder.Render);
finally
Builder.Free;
end;
end;
procedure TPrintDlg.TDocFormatPreview.HiliteSource(const UseColour,
SyntaxHighlight: Boolean; Builder: TRTFBuilder);
const
// Sample source code displayed in preview
cSourceCode = 'procedure Foo;' + EOL
+ 'begin' + EOL
+ ' // sample comment' + EOL
+ ' ShowMessage(''Bar'');' + EOL
+ 'end;';
var
Renderer: IHiliteRenderer; // renders highlighted code as RTF
Brush: TSyntaxHiliterBrush; // performs syntax highlighting
Theme: TSyntaxHiliteTheme; // determines syntax highlighting style
begin
if not SyntaxHighlight then
// null theme - no syntax highlighting
Theme := TSyntaxHiliteThemes.NullTheme.Clone
else
// proper theme - with or without colour
Theme := TConfig.Instance.HiliterThemes[
Preferences.CurrentHiliteThemeIds[htkPrint]
].Clone(not UseColour);
try
// using "pascal" brush here since sample source is Pascal.
Brush := TSyntaxHiliterBrushes.CreateBrush(
TSyntaxHiliterBrushes.PascalBrushID
);
try
Renderer := TRTFHiliteRenderer.Create(Builder, Brush, Theme);
TSyntaxHiliter.Hilite(cSourceCode, Brush, Renderer);
finally
Brush.Free;
end;
finally
Theme.Free;
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.