Menu

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

Download this file

261 lines (238 with data), 8.0 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
{
* 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 Peter
* Johnson. All Rights Reserved.
*
* Contributors:
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit FmViewExtraDlg;
interface
uses
// Delphi
Forms, StdCtrls, Controls, ExtCtrls, Classes,
// Project
FmHTMLViewDlg, FrBrowserBase, FrHTMLDlg, FrHTMLTpltDlg, UActiveText,
UCSSBuilder, UHTMLEvents;
type
{
TViewExtraDlg:
Dialog box that displays active text rendered from REML markup entered in
snippets editor. Active text is rendered as HTML.
}
TViewExtraDlg = class(THTMLViewDlg)
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 InitHTMLFrame; override;
{Initialises HTML frame, loads HTML template and inserts HTML
representation of Extra Text REML.
}
function GetBodyPanelHeight: Integer; override;
{Gets height of body panel.
@return Required body panel height.
}
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
UActiveTextHTML, UAnchors, UColours, UConsts, UCSSUtils, UMessageBox,
UProtocols, USystemInfo;
{$R *.dfm}
{ TViewExtraDlg }
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;
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 Create(AOwner) do
try
fActiveText := ActiveText;
ShowModal;
finally
Free;
end;
end;
function TViewExtraDlg.GetBodyPanelHeight: Integer;
{Gets height of body panel.
@return Required body panel height.
}
begin
Result := frmExtraInfo.DocHeight;
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 = 1; // modal result of choice dialog's close button
cViewLinkRes = 2; // modal result of choice dialog's view button
begin
case EventInfo.DispatchId of
cDocEventOnClick:
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;
end;
procedure TViewExtraDlg.InitHTMLFrame;
{Initialises HTML frame, loads HTML template and inserts HTML
representation of Extra Text REML.
}
var
Values: TStringList; // values to insert in HTML template
begin
Values := TStringList.Create;
try
frmExtraInfo.OnBuildCSS := UpdateCSS;
frmExtraInfo.OnHTMLEvent := HTMLEventHandler;
Values.Values['Content'] := TActiveTextHTML.Render(fActiveText);
frmExtraInfo.Initialise('dlg-viewextra-tplt.html', Values);
finally
FreeAndNil(Values);
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
begin
// Set rendered REML container
with CSSBuilder.AddSelector('#content') do
begin
AddProperty(CSSBackgroundColorProp(clWindow));
AddProperty(CSSPaddingProp(0, 6, 6, 6));
AddProperty(CSSMarginProp(cssTop, 6));
AddProperty(CSSBorderProp(cssAll, 1, cbsSolid, clBorder));
AddProperty(CSSOverflowProp(covAuto));
AddProperty(CSSWidthProp(cltAuto, 0));
// Use height instead of maxheight if IE 6 or lower
if TOSInfo.BrowserVer > 6 then
AddProperty(CSSMaxHeightProp(cMaxExtraHTMLHeight))
else
AddProperty(CSSHeightProp(cMaxExtraHTMLHeight));
end;
// Show or hide text about links depending on if links in Extra HTML
with CSSBuilder.AddSelector('#linktext') do
begin
if ExtraContainsLinks then
AddProperty(CSSDisplayProp(cdsInline))
else
AddProperty(CSSDisplayProp(cdsNone));
end;
// Style the REML itself
TActiveTextHTML.Styles(Font, CSSBuilder);
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.