Menu

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

Download this file

394 lines (361 with data), 12.4 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
{
* 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-2009 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UWBCommandBars;
interface
uses
// Delphi
ImgList, Menus,
// Project
UCommandBars, UDispatchList, UGIFImageList;
type
{
TWBCommandBarMgr:
Command bar manager for use with command bars associated with web browser
controls. Uses a fixed image list.
}
TWBCommandBarMgr = class(TCommandBarMgr, ICommandBarConfig)
strict private
class var fImages: TGIFImageList; // Static image list
class var fGC: IInterface; // Garbage collector for staic image list
strict protected
class function GetImages: TGIFImageList;
{Gets reference to static image list, creating it if necessary.
@return Reference to image list.
}
procedure SetImages(const Images: TCustomImageList); override;
{Specifies image list to be used by all command bars.
@param Images [in] Image list to be used.
}
end;
{
TWBPopupMenuWrapper:
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.
}
TWBPopupMenuWrapper = class(TPopupMenuWrapper)
strict private
function HasVisibleItems: Boolean;
{Checks if menu has any visible non-spacer menu items with captions.
Assumes there are no sub-menus.
@return True if menu has displayable items, False if not.
}
procedure QueryPopupHandler(Sender: TObject; var Cancel: Boolean);
{Handles wrapped menu's OnPopupQuery event. Prevents display of menu if
there are no displayable items.
@param Sender [in] Not used.
@param Cancel [in/out] Set to True if menu is not to be displayed.
}
strict protected
procedure InitMenu; override;
{Initialises menu. Records details of HTML link element to any link
related menu items.
}
public
constructor Create(const Menu: TPopupMenu); override;
{Class constructor. Sets up object.
@param Menu [in] Menu component being wrapped.
}
end;
{
TWBDefaultPopupMenuWrapper:
Wrapper for popup menus associated with a web browser control that extends
TWBPopupMenuWrapper to add some menu items based on links in underlying
document in browser control.
}
TWBDefaultPopupMenuWrapper = class(TWBPopupMenuWrapper)
strict private
procedure ClearTempMenuItems;
{Clears temporary menu items from menu.
}
procedure GetLinkMenuItems(const Doc: IDispatch; out CommandItems,
HelpItems: IDispatchList);
{Gets all command and help links from document that are designated as menu
items.
@param Doc [in] IDispatch interface of document containing links.
@param CommandItems [out] List of command links.
@param HelpItems [out] List of help links.
}
procedure AddLinksToMenu(const Links: IDispatchList);
{Adds menu items to menu that can trigger links from a link list.
@param Links [in] List of links to be added to menu.
}
function GetImageIndex(const Link: IDispatch): Integer;
{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.
@param Link [in] Link for which image needed.
@return Index of image in image or -1 if there is no associated image.
}
strict protected
procedure InitMenu; override;
{Initialises menu. Clears temporary menu items and adds any required menu
items for links in HTML document.
}
end;
implementation
uses
// Delphi
SysUtils, StrUtils,
// Project
UAnchors, UGC, UHTMLDocHelper, UImageTags, ULinkAction, UMenuHelper,
UWBPopupMenus;
{ TWBCommandBarMgr }
class function TWBCommandBarMgr.GetImages: TGIFImageList;
{Gets reference to static image list, creating it if necessary.
@return Reference to image list.
}
begin
if not Assigned(fImages) then
begin
fImages := TGIFImageList.Create(nil);
TGC.GCLocalObj(fGC, fImages);
end;
Result := fImages;
end;
procedure TWBCommandBarMgr.SetImages(const Images: TCustomImageList);
{Specifies image list to be used by all command bars.
@param Images [in] Image list to be used.
}
begin
GetImages.Clear;
GetImages.AddImages(Images);
inherited SetImages(GetImages);
end;
{ TWBPopupMenuWrapper }
constructor TWBPopupMenuWrapper.Create(const Menu: TPopupMenu);
{Class constructor. Sets up object.
@param Menu [in] Menu component being wrapped.
}
begin
inherited;
// Assign popup query handler if possible
if (Menu is TWBPopupMenu) then
(Menu as TWBPopupMenu).OnQueryPopup := QueryPopupHandler;
end;
function TWBPopupMenuWrapper.HasVisibleItems: Boolean;
{Checks if menu has any visible non-spacer menu items with captions. Assumes
there are no sub-menus.
@return True if menu has displayable items, False if not.
}
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;
{Initialises menu. Records details of HTML link element to any link related
menu items.
}
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);
{Handles wrapped menu's OnPopupQuery event. Prevents display of menu if there
are no displayable items.
@param Sender [in] Not used.
@param Cancel [in/out] Set to True if menu is not to be displayed.
}
begin
PrepareMenu;
Cancel := not HasVisibleItems;
end;
{ TWBDefaultPopupMenuWrapper }
procedure TWBDefaultPopupMenuWrapper.AddLinksToMenu(const Links: IDispatchList);
{Adds menu items to menu that can trigger links from a link list.
@param Links [in] List of links to be added to menu.
}
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 THTMLDocHelper.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;
{Clears temporary menu items from menu.
}
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;
{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.
@param Link [in] Link for which image needed.
@return Index of image in image or -1 if there is no associated image.
}
// ---------------------------------------------------------------------------
function URLBaseName(const URL: string): string;
{Extracts a base resource name from a URL.
@param URL [in] URL containing resource name.
@return Required base name.
}
var
Pos: Integer; // position of last path delimiter in URL
begin
Pos := LastDelimiter('/', URL);
if Pos > 0 then
Result := AnsiRightStr(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 := THTMLDocHelper.ParentElem(Link, 'div', 'option');
if not Assigned(ParentDiv) then
ParentDiv := THTMLDocHelper.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 THTMLDocHelper.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);
{Gets all command and help links from document that are designated as menu
items.
@param Doc [in] IDispatch interface of document containing links.
@param CommandItems [out] List of command links.
@param HelpItems [out] List of help links.
}
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 THTMLDocHelper.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;
{Initialises menu. Clears temporary menu items and adds any required menu
items for links in HTML document.
}
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(
THTMLDocHelper.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.