Menu

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

Download this file

284 lines (260 with data), 8.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
{
* FmViewExtraDlg.pas
*
* Implements a dialog box that displays active text rendered from REML markup
* entered in snippets editor. Active text is rendered as HTML. Purpose is to
* allow users to test the extra information markup before saving. Can also
* display urls of any links and allow them to be displayed.
*
* $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 FmViewExtraDlg.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2009-2012 Peter
* Johnson. All Rights Reserved.
*
* Contributors:
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit FmViewExtraDlg;
interface
uses
// Delphi
Classes, Forms, StdCtrls, Controls, ExtCtrls,
// Project
ActiveText.UMain, Browser.UHTMLEvents, FmGenericViewDlg, FrBrowserBase,
FrHTMLDlg, FrHTMLTpltDlg, UBaseObjects, UCSSBuilder;
type
{
TViewExtraDlg:
Dialog box that displays active text rendered from REML markup entered in
snippets editor. Active text is rendered as HTML.
}
TViewExtraDlg = class(TGenericViewDlg, INoPublicConstruct)
frmExtraInfo: THTMLTpltDlgFrame;
strict private
fActiveText: IActiveText; // Active text to be displayed.
procedure UpdateCSS(Sender: TObject; const CSSBuilder: TCSSBuilder);
{Modifies CSS used to display dialog box body to achieve required
appearance.
@param Sender [in] Not used.
@param CSSBuilder [in] Object used to modify CSS.
}
procedure HTMLEventHandler(Sender: TObject;
const EventInfo: THTMLEventInfo);
{Handles HTML frame's OnHTMLEvent event. Cancels any clicks on links and
handles these by displaying dialog and acting on user's response.
@param Sender [in] Not used.
@param EventInfo [in] Object providing information about the event.
}
function ExtraContainsLinks: Boolean;
{Checks if the "extra" active text contains any link (<a>) tags.
@return True if links found, False if not.
}
strict protected
procedure ArrangeForm; override;
{Sizes dialog box to fit content.
}
procedure ConfigForm; override;
{Initialises HTML frame, loads HTML template and inserts HTML
representation of Extra Text REML.
}
public
class procedure Execute(const AOwner: TComponent;
const ActiveText: IActiveText);
{Displays dialog box to display HTML representation of some active text.
@param AOwner [in] Component that owns this dialog box.
@param ActiveText [in] Active text to be displayed as HTML.
}
end;
implementation
uses
// Delphi
SysUtils, Graphics,
// Project
ActiveText.UHTMLRenderer, UAnchors, UColours, UConsts, UCSSUtils, UFontHelper,
UHTMLTemplate, UMessageBox, UProtocols, USystemInfo;
{$R *.dfm}
{ TViewExtraDlg }
procedure TViewExtraDlg.ArrangeForm;
{Sizes dialog box to fit content.
}
begin
pnlBody.Height := frmExtraInfo.DocHeight;
inherited;
end;
procedure TViewExtraDlg.ConfigForm;
{Initialises HTML frame, loads HTML template and inserts HTML representation
of Extra Text REML.
}
var
Renderer: TActiveTextHTML;
begin
inherited;
frmExtraInfo.OnBuildCSS := UpdateCSS;
frmExtraInfo.OnHTMLEvent := HTMLEventHandler;
Renderer := TActiveTextHTML.Create;
try
frmExtraInfo.Initialise(
'dlg-viewextra-tplt.html',
procedure(Tplt: THTMLTemplate)
begin
Tplt.ResolvePlaceholderHTML(
'Content', Renderer.Render(fActiveText)
);
end
);
finally
Renderer.Free;
end;
end;
class procedure TViewExtraDlg.Execute(const AOwner: TComponent;
const ActiveText: IActiveText);
{Displays dialog box to display HTML representation of some active text.
@param AOwner [in] Component that owns this dialog box.
@param ActiveText [in] Active text to be displayed as HTML.
}
begin
with InternalCreate(AOwner) do
try
fActiveText := ActiveText;
ShowModal;
finally
Free;
end;
end;
function TViewExtraDlg.ExtraContainsLinks: Boolean;
{Checks if the "extra" active text contains any link (<a>) tags.
@return True if links found, False if not.
}
var
Elem: IActiveTextElem; // each element in active text
ActionElem: IActiveTextActionElem; // ref to am action element
begin
Result := False;
for Elem in fActiveText do
if Supports(Elem, IActiveTextActionElem, ActionElem) and
(ActionElem.Kind = ekLink) then
begin
Result := True;
Exit;
end;
end;
procedure TViewExtraDlg.HTMLEventHandler(Sender: TObject;
const EventInfo: THTMLEventInfo);
{Handles HTML frame's OnHTMLEvent event. Cancels any clicks on links and
handles these by displaying dialog and acting on user's response.
@param Sender [in] Not used.
@param EventInfo [in] Object providing information about the event.
}
var
ALink: IDispatch; // reference to the any link that was clicked
resourcestring
// Button captions for choice dialog box
sClose = 'Close';
sViewLink = 'View Link';
// Text to display in choice dialog box
sDlgText = 'Link URL:' + EOL2 + '%s';
const
cCloseRes = mrCancel; // modal result of choice dialog's close button
cViewLinkRes = $FF; // modal result of choice dialog's view button
begin
if EventInfo.IsEvent(
THTMLDocumentEvents2Sink.EventIntf, THTMLDocumentEvents2Sink.DISPID_OnClick
) then
begin
// Mouse click: check for click on a link and handle it
ALink := TAnchors.FindEnclosingAnchor(EventInfo.Args.srcElement);
if not Assigned(ALink) then
Exit;
// Cancel event: no further action needed
EventInfo.Cancelled := True;
// Give user info about link and option to display it
if TMessageBox.Custom(
Self,
Format(sDlgText, [TAnchors.GetURL(ALink)]),
[
TMessageBoxButton.Create(sClose, cCloseRes, True, True),
TMessageBoxButton.Create(sViewLink, cViewLinkRes)
]
) = cViewLinkRes then
begin
// User wants to view link: use protocol handler to display it
with TProtocolFactory.CreateHandler(TAnchors.GetURL(ALink)) do
try
Execute;
finally
Free;
end;
end;
end;
end;
procedure TViewExtraDlg.UpdateCSS(Sender: TObject;
const CSSBuilder: TCSSBuilder);
{Modifies CSS used to display dialog box body to achieve required appearance.
@param Sender [in] Not used.
@param CSSBuilder [in] Object used to modify CSS.
}
const
cMaxExtraHTMLHeight = 240; // max height of Extra HTML
var
ContentFont: TFont; // font used for #content tab
begin
ContentFont := TFont.Create;
try
TFontHelper.SetContentFont(ContentFont, True);
// Set rendered REML container
with CSSBuilder.AddSelector('#content') do
begin
AddProperty(TCSS.FontProps(ContentFont));
AddProperty(TCSS.BackgroundColorProp(clWindow));
AddProperty(TCSS.PaddingProp(0, 6, 6, 6));
AddProperty(TCSS.MarginProp(cssTop, 6));
AddProperty(TCSS.BorderProp(cssAll, 1, cbsSolid, clBorder));
AddProperty(TCSS.OverflowProp(covAuto));
AddProperty(TCSS.WidthProp(cluAuto, 0));
// Use height instead of maxheight if IE 6 or lower
if TOSInfo.BrowserVer > 6 then
AddProperty(TCSS.MaxHeightProp(cMaxExtraHTMLHeight))
else
AddProperty(TCSS.HeightProp(cMaxExtraHTMLHeight));
end;
with CSSBuilder.AddSelector('.active-text h2') do
begin
AddProperty(TCSS.MarginProp(4, 0, 0, 0));
AddProperty(TCSS.FontWeightProp(cfwBold));
AddProperty(TCSS.FontSizeProp(ContentFont.Size + 1));
end;
with CSSBuilder.AddSelector('.active-text p') do
AddProperty(TCSS.MarginProp(4, 0, 0, 0));
// Show or hide text about links depending on if links in Extra HTML
with CSSBuilder.AddSelector('#linktext') do
begin
if ExtraContainsLinks then
AddProperty(TCSS.DisplayProp(cdsInline))
else
AddProperty(TCSS.DisplayProp(cdsNone));
end;
finally
ContentFont.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.