Menu

[r111]: / trunk / Src / FmPreviewDlg.pas  Maximize  Restore  History

Download this file

305 lines (273 with data), 9.6 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
{
* FmPreviewDlg.pas
*
* Implements a dialog box that is used to preview or display text, HTML and
* Rich text documents.
*
* v0.1 of 17 Mar 2005 - Original version.
* v1.0 of 24 May 2006 - Improved and corrected comments.
* - Removed unused unit reference.
* v1.1 of 11 Nov 2006 - Replaced code that detects HTML and RTF formats with
* calls to helper routines from RTF and HTML utility
* units.
* - Changed to set page control tabs invisible at design
* time rather than run time.
* v1.2 of 08 Feb 2007 - Moved code that displays document from FormShow event
* handler to new overridden InitForm method deleted
* FormShow.
* v1.3 of 13 May 2007 - Added popup menu for selection and copying of
* previewed text along with associated actions and image
* list.
* - Added code to call into viewer objects to perform
* selection and copying.
* v1.4 of 17 Jun 2009 - Added support for variable dialog box captions:
* - An optional parameter was to the dialog's Execute
* method to allow caller to specify a title.
* - Added support for getting caption from any title
* embedded in displayed document.
* - Made private and protected sections strict.
*
*
* ***** 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 FmPreviewDlg.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-2009 Peter
* Johnson. All Rights Reserved.
*
* ***** END LICENSE BLOCK *****
}
unit FmPreviewDlg;
interface
uses
// Delphi
ImgList, Controls, Classes, ActnList, Menus, Forms, ComCtrls, StdCtrls,
ExtCtrls,
// Project
FmGenericViewDlg, FrBrowserBase, FrHTMLPreview, FrMemoPreview, FrRTFPreview,
FrTextPreview, IntfPreview;
type
{
TPreviewDlg:
Dialog box used to preview text, HTML and Rich text documents.
}
TPreviewDlg = class(TGenericViewDlg)
actCopy: TAction;
actSelectAll: TAction;
alPreview: TActionList;
frRTF: TRTFPreviewFrame;
frHTML: THTMLPreviewFrame;
frText: TTextPreviewFrame;
ilPreview: TImageList;
miCopy: TMenuItem;
miSelectAll: TMenuItem;
mnuPreview: TPopupMenu;
pcViews: TPageControl;
tsHTML: TTabSheet;
tsRTF: TTabSheet;
tsText: TTabSheet;
procedure actCopyExecute(Sender: TObject);
procedure actCopyUpdate(Sender: TObject);
procedure actSelectAllExecute(Sender: TObject);
procedure actSelectAllUpdate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
strict private
fViewer: IInterface; // Interfaces with viewer frame
fDocContent: string; // Stores content of document we are displaying
fDlgTitle: string; // Dialog box title
procedure GetViewerInfo(out Viewer: IInterface; out TabSheet: TTabSheet);
{Gets information about required document viewer and tab sheet that
contains it.
@param Viewer [out] Interface to viewer frame.
@param TabSheet [out] Tab sheet containing viewer frame.
}
function CanCopy: Boolean;
{Checks if current view supports copying to clipboard.
@return True if copying supported.
}
function CanSelectAll: Boolean;
{Checks if current view supports text selection.
@return True if selection supported.
}
procedure CopyToClipboard;
{Copies selected text to clipboard from current view if view supports
copying.
}
procedure SelectAll;
{Selects all text in current view if view supports selection.
}
strict protected
procedure InitForm; override;
{Loads and displays the document being previewed.
}
public
class procedure Execute(AOwner: TComponent; const ADocContent: string;
const ADlgTitle: string = '');
{Displays a document in the preview dialog.
@param AOwner [in] Owning component.
@param ADocContent [in] Content of document to be displayed (HTML, RTF
or plain text).
@param ADlgTitle [in] Title of dialog box. Default is used if ''.
}
property DocContent: string read fDocContent write fDocContent;
{Content of document being displayed}
end;
implementation
uses
// Delphi
SysUtils,
// Project
IntfFrameMgrs, URTFUtils, UHTMLUtils;
{$R *.dfm}
{ TPreviewDlg }
procedure TPreviewDlg.actCopyExecute(Sender: TObject);
{Copies preview text to clipboard.
@param Sender [in] Not used.
}
begin
CopyToClipboard;
end;
procedure TPreviewDlg.actCopyUpdate(Sender: TObject);
{Enables / disables Copy action depending or whether copying is supported in
current view.
@param Sender [in] Not used.
}
begin
actCopy.Enabled := CanCopy;
end;
procedure TPreviewDlg.actSelectAllExecute(Sender: TObject);
{Selects all preview text.
@param Sender [in] Not used.
}
begin
SelectAll;
end;
procedure TPreviewDlg.actSelectAllUpdate(Sender: TObject);
{Enables / disables Select All action depending on whether selection is
supported in current view.
@param Sender [in] Not used.
}
begin
actSelectAll.Enabled := CanSelectAll;
end;
function TPreviewDlg.CanCopy: Boolean;
{Checks if current view supports copying to clipboard.
@return True if copying supported.
}
begin
Result := False;
if Supports(fViewer, IClipboardMgr) then
Result := (fViewer as IClipboardMgr).CanCopy;
end;
function TPreviewDlg.CanSelectAll: Boolean;
{Checks if current view supports text selection.
@return True if selection supported.
}
begin
Result := False;
if Supports(fViewer, ISelectionMgr) then
Result := (fViewer as ISelectionMgr).CanSelectAll;
end;
procedure TPreviewDlg.CopyToClipboard;
{Copies selected text to clipboard from current view if view supports copying.
}
begin
if Supports(fViewer, IClipboardMgr) then
(fViewer as IClipboardMgr).CopyToClipboard;
end;
class procedure TPreviewDlg.Execute(AOwner: TComponent;
const ADocContent: string; const ADlgTitle: string = '');
{Displays a document in the preview dialog.
@param AOwner [in] Owning component.
@param ADocContent [in] Content of document to be displayed (HTML, RTF or
plain text).
@param ADlgTitle [in] Title of dialog box. Default is used if ''.
}
begin
with TPreviewDlg.Create(AOwner) do
try
fDlgTitle := ADlgTitle;
DocContent := ADocContent;
ShowModal;
finally
Free;
end;
end;
procedure TPreviewDlg.FormClose(Sender: TObject; var Action: TCloseAction);
{Frees viewer object whether form is closed.
@param Sender [in] Not used.
@param Action [in] Not used.
}
begin
inherited;
fViewer := nil; // required to prevent access violation
end;
procedure TPreviewDlg.GetViewerInfo(out Viewer: IInterface;
out TabSheet: TTabSheet);
{Gets information about required document viewer and tab sheet that contains
it.
@param Viewer [out] Interface to viewer frame.
@param TabSheet [out] Tab sheet containing viewer frame.
}
begin
if URTFUtils.IsValidRTFCode(DocContent) then
begin
// RTF document
TabSheet := tsRTF;
Viewer := frRTF;
end
else if UHTMLUtils.IsValidHTMLCode(DocContent) then
begin
// HTML document
TabSheet := tsHTML;
Viewer := frHTML;
end
else
begin
// Plain text document
TabSheet := tsText;
Viewer := frText;
end;
end;
procedure TPreviewDlg.InitForm;
{Loads and displays the document being previewed.
}
var
TabSheet: TTabSheet; // tab sheet containing preview frame
Title: string; // document title
begin
inherited;
// Display document (select tab, load doc and set submenu on into frame)
GetViewerInfo(fViewer, TabSheet);
// select tab containing required frame
pcViews.ActivePage := TabSheet;
// update required frame's popup menu and display document in it
(fViewer as IPreview).SetPopupMenu(mnuPreview);
// load content into preview and set dialog caption
(fViewer as IPreview).Display(DocContent, Title);
if fDlgTitle <> '' then
Caption := fDlgTitle // caller specified title - use it
else if Title <> '' then
Caption := Caption + ': ' + Title; // use title extracted from document
end;
procedure TPreviewDlg.SelectAll;
{Selects all text in current view if view supports selection.
}
begin
if Supports(fViewer, ISelectionMgr) then
(fViewer as ISelectionMgr).SelectAll;
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.