Menu

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

Download this file

271 lines (238 with data), 8.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
{
* FmPreviewDlg.pas
*
* Implements a dialog box that is used to preview or display text, HTML and
* Rich text documents.
*
* $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 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-2011 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit FmPreviewDlg;
interface
uses
// Delphi
ImgList, Controls, Classes, ActnList, Menus, Forms, ComCtrls, StdCtrls,
ExtCtrls,
// Project
FmGenericViewDlg, FrBrowserBase, FrHTMLPreview, FrMemoPreview, FrRTFPreview,
FrTextPreview, UBaseObjects, UEncodings;
type
/// <summary>
/// Enumeration of types of document that can be displayed by preview dialog
/// box.
/// </summary>
TPreviewDocType = (
dtPlainText, // plain text document
dtHTML, // HTML document
dtRTF // rich text format document
);
type
/// <summary>
/// Dialog box used to preview text, HTML and Rich text documents.
/// </summary>
TPreviewDlg = class(TGenericViewDlg, INoPublicConstruct)
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;
/// <summary>Copies preview text to clipboard.</summary>
procedure actCopyExecute(Sender: TObject);
/// <summary>Enables / disables Copy action depending or whether copying is
/// supported in current view.</summary>
procedure actCopyUpdate(Sender: TObject);
/// <summary>Selects all preview text.</summary>
procedure actSelectAllExecute(Sender: TObject);
/// <summary>Enables / disables Select All action depending on whether
/// selection is supported in current view.</summary>
procedure actSelectAllUpdate(Sender: TObject);
/// <summary>Frees viewer object when form is closed.</summary>
procedure FormClose(Sender: TObject; var Action: TCloseAction);
strict private
var
/// <summary>Reference to viewer frame's IInterface.</summary>
fViewer: IInterface;
/// <summary>Content of document being displayed.</summary>
fDocContent: TEncodedData;
/// <summary>Type (format) of document to be displayed.</summary>
fDocType: TPreviewDocType;
/// <summary>Dialog box title.</summary>
fDlgTitle: string;
/// <summary>Gets information about required document viewer and tab sheet
/// that contains it.</summary>
/// <param name="Viewer">IInterface [out] Interface to viewer frame.
/// </param>
/// <param name="TabSheet">TTabSheet [out] Tab sheet containing viewer
/// frame.</param>
procedure GetViewerInfo(out Viewer: IInterface; out TabSheet: TTabSheet);
/// <summary>Checks if current view supports copying to clipboard.
/// </summary>
function CanCopy: Boolean;
/// <summary>Checks if current view supports text selection.</summary>
function CanSelectAll: Boolean;
/// <summary>Copies selected text to clipboard from current view if viewer
/// supports copying.</summary>
procedure CopyToClipboard;
/// <summary>Selects all text in current view if viewer supports selection.
/// </summary>
procedure SelectAll;
/// <summary>Finds tab sheet that is parent of a given frame.</summary>
class function FindParentTabSheet(const Frame: TFrame): TTabSheet;
strict protected
/// <summary>Loads and displays the document being previewed.</summary>
procedure InitForm; override;
public
/// <summary>Displays a document in preview dialog box.</summary>
/// <param name="AOwner">TComponent [in] Component that owns dialog box.
/// </param>
/// <param name="ADocContent">TEncodedData [in] Content of document to be
/// displayed.</param>
/// <param name="ADocType">TPreviewDocType [in] Type or format of document
/// to be displayed: HTML, RTF or plain text.</param>
/// <param name="ADlgTitle">string [in] Optional dialog box title. If not
/// supplied default title is used.</param>
class procedure Execute(AOwner: TComponent; const ADocContent: TEncodedData;
const ADocType: TPreviewDocType; const ADlgTitle: string = '');
end;
implementation
uses
// Delphi
SysUtils,
// Project
IntfFrameMgrs, IntfPreview;
{$R *.dfm}
{ TPreviewDlg }
procedure TPreviewDlg.actCopyExecute(Sender: TObject);
begin
CopyToClipboard;
end;
procedure TPreviewDlg.actCopyUpdate(Sender: TObject);
begin
actCopy.Enabled := CanCopy;
end;
procedure TPreviewDlg.actSelectAllExecute(Sender: TObject);
begin
SelectAll;
end;
procedure TPreviewDlg.actSelectAllUpdate(Sender: TObject);
begin
actSelectAll.Enabled := CanSelectAll;
end;
function TPreviewDlg.CanCopy: Boolean;
begin
Result := False;
if Supports(fViewer, IClipboardMgr) then
Result := (fViewer as IClipboardMgr).CanCopy;
end;
function TPreviewDlg.CanSelectAll: Boolean;
begin
Result := False;
if Supports(fViewer, ISelectionMgr) then
Result := (fViewer as ISelectionMgr).CanSelectAll;
end;
procedure TPreviewDlg.CopyToClipboard;
begin
if Supports(fViewer, IClipboardMgr) then
(fViewer as IClipboardMgr).CopyToClipboard;
end;
class procedure TPreviewDlg.Execute(AOwner: TComponent;
const ADocContent: TEncodedData; const ADocType: TPreviewDocType;
const ADlgTitle: string);
begin
with InternalCreate(AOwner) do
try
fDlgTitle := ADlgTitle;
fDocContent := TEncodedData.Create(ADocContent);
fDocType := ADocType;
ShowModal;
finally
Free;
end;
end;
class function TPreviewDlg.FindParentTabSheet(const Frame: TFrame): TTabSheet;
var
ParentCtrl: TWinControl; // moves up tree of parent controls
begin
ParentCtrl := Frame.Parent;
while Assigned(ParentCtrl) and not (ParentCtrl is TTabSheet) do
ParentCtrl := ParentCtrl.Parent;
Assert(Assigned(ParentCtrl),
ClassName + '.FindParentTabSheet: Tab sheet not found.');
Result := ParentCtrl as TTabSheet;
end;
procedure TPreviewDlg.FormClose(Sender: TObject; var Action: TCloseAction);
begin
inherited;
fViewer := nil; // required to prevent access violation
end;
procedure TPreviewDlg.GetViewerInfo(out Viewer: IInterface;
out TabSheet: TTabSheet);
var
Frame: TFrame;
begin
case fDocType of
dtPlainText: Frame := frText;
dtHTML: Frame := frHTML;
dtRTF: Frame := frRTF;
else Frame := nil;
end;
Assert(Assigned(Frame), ClassName + '.GetViewerInfo: No frame assigned');
Viewer := Frame as IInterface;
TabSheet := FindParentTabSheet(Frame);
end;
procedure TPreviewDlg.InitForm;
var
TabSheet: TTabSheet; // tab sheet containing preview frame
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(fDocContent);
if fDlgTitle <> '' then
Caption := fDlgTitle; // caller specified title - use it
end;
procedure TPreviewDlg.SelectAll;
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.