Menu

[r2009]: / trunk / Src / FrPrintingPrefs.pas  Maximize  Restore  History

Download this file

431 lines (388 with data), 13.4 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
{
* FrPrintingPrefs.pas
*
* Implements a frame that allows user to set printing preferences. Designed for
* use as one of the tabs in the preferences dialog box.
*
* $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 FrPrintingPrefs.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2007-2012 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit FrPrintingPrefs;
interface
uses
// Delphi
StdCtrls, Controls, Forms, Classes,
// Project
FrPrefsBase, FrRTFShowCase, Hiliter.UGlobals, UMeasurement, UPreferences;
type
{
TPrintingPrefsFrame:
Frame that allows user to set printing preferences. Can persist preferences
entered by user. Note: Designed for use in preferences dialog box.
}
TPrintingPrefsFrame = class(TPrefsBaseFrame)
chkSyntaxPrint: TCheckBox;
chkUseColor: TCheckBox;
frmPreview: TRTFShowCaseFrame;
gpMargins: TGroupBox;
gpOutputOptions: TGroupBox;
lblBottom: TLabel;
lblLeft: TLabel;
lblRight: TLabel;
lblTop: TLabel;
edBottom: TEdit;
edLeft: TEdit;
edRight: TEdit;
edTop: TEdit;
stInfo: TStaticText;
procedure CheckboxClick(Sender: TObject);
procedure NumEditKeyPress(Sender: TObject; var Key: Char);
strict private
fHiliteAttrs: IHiliteAttrs;
{Style of syntax highlighting to use in sample output}
fCurrentUnits: TMeasurementUnits;
{Current measurement units}
procedure DisplayPreview;
{Displays preview of appearance of document according to current state of
controls.
}
public
constructor Create(AOwner: TComponent); override;
{Class constructor. Sets up frame object.
}
procedure Activate(const Prefs: IPreferences); override;
{Called when page activated. Updates controls.
@param Prefs [in] Object that provides info used to update controls.
}
procedure Deactivate(const Prefs: IPreferences); override;
{Called when page is deactivated. Stores information entered by user.
@param Prefs [in] Object used to store information.
}
/// <summary>Checks if preference changes require that main window UI is
/// updated.</summary>
/// <remarks>Called when dialog box containing frame is closing. Always
/// returns False because these preferences never affect UI.</remarks>
function UIUpdated: Boolean; override;
procedure ArrangeControls; override;
{Arranges controls on frame. Called after frame has been sized.
}
function DisplayName: string; override;
{Caption that is displayed in the tab sheet that contains this frame when
displayed in the preference dialog box.
@return Required display name.
}
class function Index: Byte; override;
{Index number that determines the location of the tab containing this
frame when displayed in the preferences dialog box.
@return Required index number.
}
end;
implementation
uses
// Delphi
SysUtils, Windows, Graphics, Math, ComCtrls,
// Project
FmPreferencesDlg, Hiliter.UAttrs, Hiliter.UHiliters, IntfCommon, UConsts,
UEncodings, UKeysHelper, UPrintInfo, URTFBuilder, URTFStyles, URTFUtils,
UStrUtils, UUtils;
{$R *.dfm}
type
{
TPrintingPrefsPreview:
Class that renders a preview of effect of printing preferences changes on a
printed document.
}
TPrintingPrefsPreview = class(TObject)
strict private
fRE: TRichEdit;
{Reference to richedit control used to render preview}
fHiliteAttrs: IHiliteAttrs;
{Attributes of syntax highlighter to use to render preview}
procedure HiliteSource(const UseColor, SyntaxPrint: Boolean;
Builder: TRTFBuilder);
{Generates sample highlighted source code.
@param UseColor [in] Whether to use colour or mono highlighter.
@param SyntaxPrint [in] Whether source code to be highlighted.
@param Builder [in] Object that receives highlighted source code.
}
public
constructor Create(const RE: TRichEdit; const HiliteAttrs: IHiliteAttrs);
{Class constructor. Sets up object.
@param RE [in] Rich edit control used to render preview.
@param HiliteAttrs [in] Current user defined highlighting.
}
procedure Generate(const UseColor, SyntaxPrint: Boolean);
{Generates RTF source code for preview.
@param UseColor [in] Whether preview to be in colour or monochrome.
@param SyntaxPrint [in] Whether preview source code to be syntax
hilited.
}
end;
{ TPrintingPrefsFrame }
procedure TPrintingPrefsFrame.Activate(const Prefs: IPreferences);
{Called when page activated. Updates controls.
@param Prefs [in] Object that provides info used to update controls.
}
resourcestring
// Caption of group box
sMarginCaption = 'Page Margins (%s)';
var
Margins: TPageMargins; // stores page margins
begin
// Update the caption to show current units
gpMargins.Caption := Format(
' ' + sMarginCaption + ' ',
[StrToLower(UMeasurement.UnitName(Prefs.MeasurementUnits))]
);
// Update entries in margins edit boxes
// record margins in mm
Margins := Prefs.PrinterPageMargins;
// adjust margins as required
fCurrentUnits := Prefs.MeasurementUnits;
case fCurrentUnits of
muInches:
// inches: convert mm to inches with max three decimal places
Margins := TPageMargins.Create(
RoundTo(MMToInches(Margins.Left), -3),
RoundTo(MMToInches(Margins.Top), -3),
RoundTo(MMToInches(Margins.Right), -3),
RoundTo(MMToInches(Margins.Bottom), -3)
);
muMillimeters:
// millimeters: max two decimal places
Margins := TPageMargins.Create(
RoundTo(Margins.Left, -2),
RoundTo(Margins.Top, -2),
RoundTo(Margins.Right, -2),
RoundTo(Margins.Bottom, -2)
);
end;
// write (converted) values into edit box
edLeft.Text := FloatToStr(Margins.Left);
edTop.Text := FloatToStr(Margins.Top);
edRight.Text := FloatToStr(Margins.Right);
edBottom.Text := FloatToStr(Margins.Bottom);
// Update print options
chkSyntaxPrint.Checked := (poSyntaxPrint in Prefs.PrinterOptions);
chkUseColor.Checked := (poUseColor in Prefs.PrinterOptions);
// Record current user highlighting choices and display initial preview
(fHiliteAttrs as IAssignable).Assign(Prefs.HiliteAttrs);
DisplayPreview;
end;
procedure TPrintingPrefsFrame.ArrangeControls;
{Arranges controls on frame. Called after frame has been sized.
}
begin
// Do nothing: all controls arrange themselves using Anchors property
end;
procedure TPrintingPrefsFrame.CheckboxClick(Sender: TObject);
{Handles clicks on all check boxes. Updates preview to reflect changes caused
by changing check box state.
@param Sender [in] Not used.
}
begin
DisplayPreview;
end;
constructor TPrintingPrefsFrame.Create(AOwner: TComponent);
{Class constructor. Sets up frame object.
}
begin
inherited;
HelpKeyword := 'PrintingPrefs';
// Create syntax highlighter object for use in sample output
fHiliteAttrs := THiliteAttrsFactory.CreateDefaultAttrs;
end;
procedure TPrintingPrefsFrame.Deactivate(const Prefs: IPreferences);
{Called when page is deactivated. Stores information entered by user.
@param Prefs [in] Object used to store information.
}
var
Margins: TPageMargins; // stores page margins
Options: TPrintOptions; // stores printer options
begin
inherited;
// Record print options
Options := [];
if chkSyntaxPrint.Checked then
Include(Options, poSyntaxPrint);
if chkUseColor.Checked then
Include(Options, poUseColor);
Prefs.PrinterOptions := Options;
// Record page margins
// store page margins per edit controls
Margins := TPageMargins.Create(
StrToFloatDef(edLeft.Text, 0.0),
StrToFloatDef(edTop.Text, 0.0),
StrToFloatDef(edRight.Text, 0.0),
StrToFloatDef(edBottom.Text, 0.0)
);
// if margins entered in inches, convert back to inches
if fCurrentUnits = muInches then
begin
// entered as inches: convert to millimeters
Margins := TPageMargins.Create(
InchesToMM(Margins.Left),
InchesToMM(Margins.Top),
InchesToMM(Margins.Right),
InchesToMM(Margins.Bottom)
);
end;
// store margins in mm
Prefs.PrinterPageMargins := Margins;
end;
function TPrintingPrefsFrame.DisplayName: string;
{Caption that is displayed in the tab sheet that contains this frame when
displayed in the preference dialog box.
@return Required display name.
}
resourcestring
sDisplayName = 'Printing'; // display name
begin
Result := sDisplayName;
end;
procedure TPrintingPrefsFrame.DisplayPreview;
{Displays preview of appearance of document according to current state of
controls.
}
var
Preview: TPrintingPrefsPreview; // object that renders preview
begin
Preview := TPrintingPrefsPreview.Create(frmPreview.RichEdit, fHiliteAttrs);
try
Preview.Generate(chkUseColor.Checked, chkSyntaxPrint.Checked);
finally
FreeAndNil(Preview);
end;
end;
class function TPrintingPrefsFrame.Index: Byte;
{Index number that determines the location of the tab containing this frame
when displayed in the preferences dialog box.
@return Required index number.
}
begin
Result := 40;
end;
procedure TPrintingPrefsFrame.NumEditKeyPress(Sender: TObject;
var Key: Char);
{Called whenever a key is pressed in a margin edit control. We prevent all
but valid number keys, a single decimal point and backspace.
@param Sender [in] Not used.
@param Key [in/out] Key that was pressed. Set to #0 to inhibit if not valid.
}
begin
if not IsValidDecimalNumberKey((Sender as TEdit).Text, Key) then
KeyErrorBeep;
end;
function TPrintingPrefsFrame.UIUpdated: Boolean;
begin
Result := False;
end;
{ TPrintingPrefsPreview }
constructor TPrintingPrefsPreview.Create(const RE: TRichEdit;
const HiliteAttrs: IHiliteAttrs);
{Class constructor. Sets up object.
@param RE [in] Rich edit control used to render preview.
@param HiliteAttrs [in] Current user defined highlighting.
}
begin
Assert(Assigned(RE),
ClassName + '.Create: RE is nil');
inherited Create;
fRE := RE;
fHiliteAttrs := HiliteAttrs;
end;
procedure TPrintingPrefsPreview.Generate(const UseColor, SyntaxPrint: Boolean);
{Generates RTF source code for preview.
@param UseColor [in] Whether preview to be in colour or monochrome.
@param SyntaxPrint [in] Whether preview source code to be syntax hilited.
}
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(UseColor, SyntaxPrint, Builder);
Builder.EndPara;
// Load document into rich edit
TRichEditHelper.Load(fRe, Builder.Render);
finally
FreeAndNil(Builder);
end;
end;
procedure TPrintingPrefsPreview.HiliteSource(const UseColor,
SyntaxPrint: Boolean; Builder: TRTFBuilder);
{Generates sample highlighted source code.
@param UseColor [in] Whether to use colour or mono highlighter.
@param SyntaxPrint [in] Whether source code to be highlighted.
@param Builder [in] Object that receives highlighted source code.
}
const
// Sample source code displayed in preview
cSourceCode = 'procedure Foo;' + EOL
+ 'begin' + EOL
+ ' // sample comment' + EOL
+ ' ShowMessage(''Bar'');' + EOL
+ 'end;';
var
Attrs: IHiliteAttrs; // highlighter attributes
Renderer: IHiliteRenderer; // renders highlighted code as RTF
begin
// Determine which highlighter to use depending on options
if not SyntaxPrint then
// default highlighter - no syntax highlighting
Attrs := THiliteAttrsFactory.CreatePrintAttrs(nil, False)
else
// user-defined highlighter, maybe in mono
Attrs := THiliteAttrsFactory.CreatePrintAttrs(fHiliteAttrs, UseColor);
// Perform highlighting
Renderer := TRTFHiliteRenderer.Create(Builder, Attrs);
TSyntaxHiliter.Hilite(cSourceCode, Renderer);
end;
initialization
// Register frame with preferences dialog box
TPreferencesDlg.RegisterPage(TPrintingPrefsFrame);
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.