Menu

[r2034]: / trunk / Src / UWBCommandBars.pas  Maximize  Restore  History

Download this file

349 lines (314 with data), 11.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
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
{
* UWBCommandBars.pas
*
* Defines various classes used to configure one or more command bars owned by
* a web browser container. Command bars are UI elements used to issue commands,
* e.g. menus & toolbars.
*
* $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 UWBCommandBars.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-2011 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UWBCommandBars;
interface
uses
// Delphi
ImgList, Menus,
// Project
UCommandBars, UDispatchList, UGIFImageList;
type
/// <summary>
/// Command bar manager for use with command bars associated with web browser
/// controls.
/// </summary>
/// <remarks>
/// All instances share the same, static, image list.
/// </remarks>
TWBCommandBarMgr = class sealed(TCommandBarMgr, ICommandBarConfig)
strict private
class var
/// <summary>Static image list.</summary>
fImages: TGIFImageList;
strict protected
/// <summary>Sets image list to be used by all command bars to given image
/// list.</summary>
/// <remarks>Assigns images from Images to static image list.</remarks>
procedure SetImages(const Images: TCustomImageList); override;
public
/// <summary>Creates empty static image list.</summary>
class constructor Create;
/// <summary>Frees static image list.</summary>
class destructor Destroy;
end;
type
/// <summary>
/// Wrapper for popup menus associated with a web browser control. Records
/// reference HTML element under mouse cursor and prevents display of menu if
/// there's nothing to display.
/// </summary>
TWBPopupMenuWrapper = class(TPopupMenuWrapper)
strict private
/// <summary>Checks if menu has any visible non-spacer menu items with
/// captions.</summary>
/// <remarks>Assumes there are no sub-menus.</remarks>
function HasVisibleItems: Boolean;
/// <summary>Handles wrapped menu's OnPopupQuery event. Prevents display of
/// menu if there are no displayable items.</summary>
/// <param name="Sender">TObject [in] Not used.</param>
/// <param name="Cancel">Boolean [in/out] False when method called. Set to
/// True if menu is not to be displayed.</param>
procedure QueryPopupHandler(Sender: TObject; var Cancel: Boolean);
strict protected
/// <summary>Initialises menu. Records details of HTML link element in any
/// link related menu items.</summary>
procedure InitMenu; override;
public
/// <summary>Sets up object for a given popup menu.</summary>
constructor Create(const Menu: TPopupMenu); override;
end;
type
/// <summary>
/// Wrapper for popup menus associated with a web browser control. Extends
/// TWBPopupMenuWrapper to add some menu items based on links in underlying
/// document in browser control.
/// </summary>
TWBDefaultPopupMenuWrapper = class sealed(TWBPopupMenuWrapper)
strict private
/// <summary>Clears temporary menu items from menu.</summary>
procedure ClearTempMenuItems;
/// <summary>Gets all command and help links from browser document that are
/// designated as menu items.</summary>
/// <param name="Doc">IDispatch [in] IDispatch interface of document
/// containing links.</param>
/// <param name="CommandItems">IDispatchLIst [out] List of command links.
/// </param>
/// <param name="HelpItems">IDispatchList [out] List of help links.</param>
procedure GetLinkMenuItems(const Doc: IDispatch; out CommandItems,
HelpItems: IDispatchList);
/// <summary>Adds menu items to menu that can trigger links from a link
/// list.</summary>
/// <param name="Links">IDispatchList [in] List of links to be added to
/// name.</param>
procedure AddLinksToMenu(const Links: IDispatchList);
/// <summary>Gets index of any image associated with a link in image list
/// used by menu. If image doesn't exist in list it is added to it.
/// </summary>
/// <param name="Link">IDispatch [in] Link for which image needed.</param>
/// <returns>Index of image in image list or -1 if there is no associated
/// image.</returns>
function GetImageIndex(const Link: IDispatch): Integer;
strict protected
/// <summary>Initialises menu. Clears temporary menu items and adds any
/// required menu items for links in HTML document.</summary>
procedure InitMenu; override;
end;
implementation
uses
// Project
UAnchors, UHTMLDOMHelper, UImageTags, ULinkAction, UMenuHelper, UStrUtils,
UWBPopupMenus;
{ TWBCommandBarMgr }
class constructor TWBCommandBarMgr.Create;
begin
fImages := TGIFImageList.Create(nil);
end;
class destructor TWBCommandBarMgr.Destroy;
begin
fImages.Free;
end;
procedure TWBCommandBarMgr.SetImages(const Images: TCustomImageList);
begin
fImages.Clear;
fImages.AddImages(Images);
inherited SetImages(fImages);
end;
{ TWBPopupMenuWrapper }
constructor TWBPopupMenuWrapper.Create(const Menu: TPopupMenu);
begin
inherited;
// Assign popup query handler if possible
if (Menu is TWBPopupMenu) then
(Menu as TWBPopupMenu).OnQueryPopup := QueryPopupHandler;
end;
function TWBPopupMenuWrapper.HasVisibleItems: Boolean;
var
MI: TMenuItem; // each menu item in menu
begin
Result := False;
for MI in Menu.Items do
if MI.Visible and (MI.Caption <> '') and (MI.Caption <> '-') then
begin
Result := True;
Exit;
end;
end;
procedure TWBPopupMenuWrapper.InitMenu;
var
MI: TMenuItem; // references a menu item
begin
for MI in Menu.Items do
if MI.Action is TLinkAction then
// menu item associated with a link action: store reference to Elem in it
(MI.Action as TLinkAction).Link := (Menu as TWBPopupMenu).HTMLElem;
end;
procedure TWBPopupMenuWrapper.QueryPopupHandler(Sender: TObject;
var Cancel: Boolean);
begin
PrepareMenu;
Cancel := not HasVisibleItems;
end;
{ TWBDefaultPopupMenuWrapper }
procedure TWBDefaultPopupMenuWrapper.AddLinksToMenu(const Links: IDispatchList);
var
Action: TLinkAction; // action to trigger a link
Link: IDispatch; // references all links in Links
begin
// Do nothing if list is empty
if Links.Count = 0 then
Exit;
// Start list with a spacer
Menu.Items.Add(CreateMenuSpacer(Menu, TWBTempMenuItem));
// Add menu items
for Link in Links do
begin
// Create action for menu item: store link reference and get caption from
// link text. If link is not visible it is not added to menu.
if THTMLDOMHelper.ElemIsVisible(Link) then
begin
Action := TLinkAction.Create(nil);
Action.Link := Link;
Action.ImageIndex := GetImageIndex(Link);
Action.Caption := TAnchors.GetInnerText(Link);
Menu.Items.Add(CreateMenuItem(Menu, TWBTempMenuItem, Action));
end;
end;
end;
procedure TWBDefaultPopupMenuWrapper.ClearTempMenuItems;
var
MI: TMenuItem; // references each menu item in menu
Idx: Integer; // loops through all menu items
begin
// NOTE: do not use enumerator here: goes horribly wrong since MI is freed.
for Idx := Pred(Menu.Items.Count) downto 0 do
begin
MI := Menu.Items[Idx];
if MI is TWBTempMenuItem then
MI.Free;
end;
end;
function TWBDefaultPopupMenuWrapper.GetImageIndex(
const Link: IDispatch): Integer;
/// Extracts a base resource name from a URL.
function URLBaseName(const URL: string): string;
var
Pos: Integer; // position of last path delimiter in URL
begin
Pos := StrLastDelimiterPos('/', URL);
if Pos > 0 then
Result := StrSliceRight(URL, Length(URL) - Pos)
else
Result := URL;
end;
var
ParentDiv: IDispatch; // parent <div> or <span> tag that contains Link
ImgTags: IDispatchList; // all <img> children of parent
ImgTag: IDispatch; // <img> child of parent that contains required GIF
Src: string; // resource URL of GIF file
begin
Result := -1;
// Check if parent elem is a <div> or <span> with class "option"
ParentDiv := THTMLDOMHelper.ParentElem(Link, 'div', 'option');
if not Assigned(ParentDiv) then
ParentDiv := THTMLDOMHelper.ParentElem(Link, 'span', 'option');
if not Assigned(ParentDiv) then
Exit;
// So see if there's an child <img> of parent with class "option-img"
ImgTags := TImageTags.GetAllImageTags(ParentDiv);
ImgTag := nil;
for ImgTag in ImgTags do
if THTMLDOMHelper.ElemHasClass(ImgTag, 'option-img') then
Break;
if not Assigned(ImgTag) then
Exit;
// Get resource name of image from <img> tag's "src" attribute
Src := URLBaseName(TImageTags.GetSrc(ImgTag));
// Get matching bitmap from image list: add one from GIF file if not found
Result := -1;
if Menu.Images is TGIFImageList then
with Menu.Images as TGIFImageList do
begin
Result := ImageIndex(Src);
if Result = -1 then
Result := AddGIFImage(Src);
end;
end;
procedure TWBDefaultPopupMenuWrapper.GetLinkMenuItems(const Doc: IDispatch;
out CommandItems, HelpItems: IDispatchList);
var
AllLinks: IDispatchList; // list of all links in document
Link: IDispatch; // referenced each link in AllLinks
begin
CommandItems := TDispatchList.Create;
HelpItems := TDispatchList.Create;
// Get all links from document
AllLinks := TAnchors.GetAllAnchors(Doc);
// Scan all links
for Link in AllLinks do
begin
// To have a link on menu it must have 'menu-item' class
if THTMLDOMHelper.ElemHasClass(Link, 'menu-item') then
begin
case TAnchors.AnchorKind(Link) of
akCommand:
CommandItems.Add(Link);
akHelp:
HelpItems.Add(Link);
end;
end;
end;
end;
procedure TWBDefaultPopupMenuWrapper.InitMenu;
var
CommandLinks: IDispatchList; // list of command links in document
HelpLinks: IDispatchList; // list of help links in document
begin
// Removes any pre-existing menu items from menu
ClearTempMenuItems;
inherited;
// Get list of command and help links from current document
GetLinkMenuItems(
THTMLDOMHelper.DocumentFromElem(
(Menu as TWBPopupMenu).HTMLElem
),
CommandLinks,
HelpLinks
);
// Add the required menu items to the menu
AddLinksToMenu(CommandLinks);
AddLinksToMenu(HelpLinks);
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.