File: VirtualSendToMenu.pas

package info (click to toggle)
mysql-query-browser 1.1.6-1sarge0
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 36,320 kB
  • ctags: 24,680
  • sloc: pascal: 203,479; xml: 136,561; ansic: 47,502; cpp: 28,926; sh: 12,433; objc: 4,823; java: 1,849; php: 1,485; python: 1,225; sql: 1,128; makefile: 872
file content (496 lines) | stat: -rw-r--r-- 15,349 bytes parent folder | download
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
unit VirtualSendToMenu;

// Version 1.2.0
//   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 expressed or
// implied. See the License for the specific language governing rights
// and limitations under the License.
//
//
//   Alternatively, the contents of this file may be used under
// the terms of the GNU General Public License Version 2 or later
// (the "GPL"), in which case the provisions of the GPL are applicable
// instead of those above. If you wish to allow use of your version of
// this file only under the terms of the GPL and not to allow others to
// use your version of this file under the MPL, indicate your decision
// by deleting the provisions above and replace them with the notice and
// other provisions required by the GPL. If you do not delete the provisions
// above, a recipient may use your version of this file under either the
// MPL or the GPL.
//
// The initial developer of this code is Jim Kueneman <jimdk@mindspring.com>
//
//----------------------------------------------------------------------------

interface

{$include ..\Include\VSToolsAddIns.inc}
{$include ..\Include\Compilers.inc}

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Menus, Registry, ShlObj, ShellAPI, ActiveX, VirtualShellUtilities, ImgList,
  CommCtrl, VirtualResources, VirtualWideStrings, VirtualSystemImageLists,
  VirtualShellContainers, VirtualUtilities,
  {$IFDEF TOOLBAR_2000} TB2Item, {$ENDIF TOOLBAR_2000}
  {$IFDEF TBX} TB2Item, TBX, {$ENDIF TBX}
  VirtualUnicodeDefines;

type
  TVirtualSendToMenu = class;  // Forward

  TSendToMenuOptions = class(TPersistent)
  private
    FImages: Boolean;
    FLargeImages: Boolean;
    FImageBorder: Integer;
    FMaxWidth: Integer;
    FEllipsisPlacement: TShortenStringEllipsis;
  public
    constructor Create;
  published
    property EllipsisPlacement: TShortenStringEllipsis read FEllipsisPlacement write FEllipsisPlacement default sseMiddle;
    property Images: Boolean read FImages write FImages default True;
    property LargeImages: Boolean read FLargeImages write FLargeImages default False;
    property ImageBorder: Integer read FImageBorder write FImageBorder default 1;
    property MaxWidth: Integer read FMaxWidth write FMaxWidth default -1;
  end;

  TVirtualSendToEvent = procedure(Sender: TVirtualSendToMenu;
    SendToTarget: TNamespace; var SourceData: IDataObject) of object;
  TVirtualSendToGetImageEvent = procedure(Sender: TVirtualSendToMenu;
    NS: TNamespace; var ImageList: TImageList; var ImageIndex: Integer) of object;

  TVirtualSendToMenuItem = class(TMenuItem)
  private
    FNamespace: TNamespace;
  protected
    property Namespace: TNamespace read FNamespace write FNamespace;
  public
    destructor Destroy; override;
    procedure Click; override;
  end;

  {$IFDEF TBX_OR_TB2K}
  {$IFDEF TBX}
  TVirtualSendToMenuItem_TB2000 = class(TTBXItem)
  {$ELSE}
  TVirtualSendToMenuItem_TB2000 = class(TTBItem)
  {$ENDIF TBX}
  private
    FNamespace: TNamespace;
  protected
    property Namespace: TNamespace read FNamespace write FNamespace;
  public
    destructor Destroy; override;
    procedure Click; override;
  end;
  {$ENDIF TBX_OR_TB2K}

  TVirtualSendToMenu = class(TPopupMenu)
  private
    FSendToItems: TVirtualNameSpaceList;
    FSendToEvent: TVirtualSendToEvent;
    FOptions: TSendToMenuOptions;
    FOnGetImage: TVirtualSendToGetImageEvent;
  protected
    procedure DoGetImage(NS: TNamespace; var ImageList: TImageList; var ImageIndex: Integer);
    procedure DoSendTo(SendToTarget: TNamespace; var SourceData: IDataObject); virtual;
    function EnumSendToCallback(APIDL: PItemIDList; AParent: TNamespace;
      Data: Pointer; var Terminate: Boolean): Boolean;
    procedure OnMenuItemDraw(Sender: TObject; ACanvas: TCanvas; ARect: TRect; Selected: Boolean); virtual;
    procedure OnMenuItemMeasure(Sender: TObject; ACanvas: TCanvas; var Width, Height: Integer); virtual;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Populate(MenuItem: TMenuItem); virtual;
    {$IFDEF TBX_OR_TB2K}
    procedure Populate_TB2000(MenuItem: TTBCustomItem); virtual;
    {$ENDIF TBX_OR_TB2K}
    procedure Popup(X, Y: Integer); override;
    property SendToItems: TVirtualNameSpaceList read FSendToItems;
  published
    property SendToEvent: TVirtualSendToEvent read FSendToEvent write FSendToEvent;
    property OnGetImage: TVirtualSendToGetImageEvent read FOnGetImage write FOnGetImage;
    property Options: TSendToMenuOptions read FOptions write FOptions;
  end;

implementation

function SendToMenuSort(Item1, Item2: Pointer): Integer;
begin
  if Assigned(Item1) and Assigned(Item2) then
    Result := TNamespace(Item2).ComparePIDL(TNamespace(Item1).RelativePIDL, False)
  else
    Result := 0
end;

{ TVirtualSendToMenuItem }

procedure TVirtualSendToMenuItem.Click;
var
  Menu: TVirtualSendToMenu;
  DataObject: IDataObject;
  DropTarget: IDropTarget;
  DropEffect: Longint;
begin
  inherited;
  Menu := Owner as TVirtualSendToMenu;
  Menu.DoSendTo(Namespace, DataObject);
  if Assigned(DataObject) then
  begin
    DropEffect := DROPEFFECT_COPY or DROPEFFECT_MOVE or DROPEFFECT_LINK;
    DropTarget := Namespace.DropTargetInterface;
    if Assigned(DropTarget) then
      if Succeeded(DropTarget.DragEnter(DataObject, MK_LBUTTON, Point(0, 0), DropEffect)) then
        (DropTarget.Drop(DataObject, MK_LBUTTON, Point(0, 0), DropEffect))
  end
end;

destructor TVirtualSendToMenuItem.Destroy;
begin
  Namespace.Free;
  inherited;
end;

{$IFDEF TBX_OR_TB2K}
{ TVirtualSendToMenuItem_TB2000 }

procedure TVirtualSendToMenuItem_TB2000.Click;
var
  Menu: TVirtualSendToMenu;
  DataObject: IDataObject;
  DropTarget: IDropTarget;
  DropEffect: Longint;
begin
  inherited;
  Menu := Owner as TVirtualSendToMenu;
  Menu.DoSendTo(Namespace, DataObject);
  if Assigned(DataObject) then
  begin
    DropEffect := DROPEFFECT_COPY or DROPEFFECT_MOVE or DROPEFFECT_LINK;
    DropTarget := Namespace.DropTargetInterface;
    if Assigned(DropTarget) then
      if Succeeded(DropTarget.DragEnter(DataObject, MK_LBUTTON, Point(0, 0), DropEffect)) then
        (DropTarget.Drop(DataObject, MK_LBUTTON, Point(0, 0), DropEffect))
  end
end;

destructor TVirtualSendToMenuItem_TB2000.Destroy;
begin
  Namespace.Free;
  inherited;
end;

{$ENDIF TBX_OR_TB2K}

{ TVirtualSendToMenu }

constructor TVirtualSendToMenu.Create(AOwner: TComponent);
begin
  inherited;
  FSendToItems := TVirtualNameSpaceList.Create(False);
  FOptions := TSendToMenuOptions.Create;
end;

destructor TVirtualSendToMenu.Destroy;
begin
  SendToItems.Free;
  FOptions.Free;
  inherited;
end;

procedure TVirtualSendToMenu.DoGetImage(NS: TNamespace;
  var ImageList: TImageList; var ImageIndex: Integer);
begin
  if Assigned(OnGetImage) then
    OnGetImage(Self, NS, ImageList, ImageIndex);
end;

procedure TVirtualSendToMenu.DoSendTo(SendToTarget: TNamespace;
   var SourceData: IDataObject);
begin
  SourceData := nil;
  if Assigned(SendToEvent) then
    SendToEvent(Self, SendToTarget, SourceData);
end;

function TVirtualSendToMenu.EnumSendToCallback(APIDL: PItemIDList;
  AParent: TNamespace; Data: Pointer; var Terminate: Boolean): Boolean;
var
  NS: TNamespace;
begin
  if AParent.IsMyComputer then
  begin
    NS := TNamespace.Create(PIDLMgr.AppendPIDL(AParent.AbsolutePIDL, APIDL), nil);
    if NS.Removable then
      TVirtualNameSpaceList(Data).Add(NS)
    else
      NS.Free
  end else
    TVirtualNameSpaceList(Data).Add(TNamespace.Create(PIDLMgr.AppendPIDL(AParent.AbsolutePIDL, APIDL), nil));
  Result := True
end;

procedure TVirtualSendToMenu.OnMenuItemDraw(Sender: TObject;
  ACanvas: TCanvas; ARect: TRect; Selected: Boolean);
var
  WS: WideString;
  S: string;
  i, Border: integer;
  ImageRect: TRect;
  TargetImageIndex: Integer;
  TargetImageList: TImageList;
  RTL: Boolean; // Left to Right reading
  OldMode: Longint;
  MenuItem: TVirtualSendToMenuItem;
begin
  if Sender is TMenuItem then
  begin
    RTL := Application.UseRightToLeftReading;
    i := TMenuItem(Sender).MenuIndex;

    if IsWinXP and Selected then
    begin
      ACanvas.Brush.Color := clHighlight;
      ACanvas.Font.Color := clBtnHighlight;
      ACanvas.FillRect(ARect);
    end else
    begin
      ACanvas.Brush.Color := clMenu;
      ACanvas.Font.Color := clMenuText;
      ACanvas.FillRect(ARect);
    end;

    MenuItem := TVirtualSendToMenuItem( Items[i]);

    if i > -1 then
      WS := MenuItem.Namespace.NameInFolder
    else
      WS := TMenuItem(Sender).Caption;

    if Options.Images then
    begin
      TargetImageIndex := -1;
      Border := Options.ImageBorder;
      if Options.LargeImages then
      begin
        TargetImageList := LargeSysImages;
        if i > -1 then
          TargetImageIndex := MenuItem.Namespace.GetIconIndex(False, icLarge)
      end else
      begin
        TargetImageList := SmallSysImages;
        if i > -1 then
          TargetImageIndex := MenuItem.Namespace.GetIconIndex(False, icSmall);
      end;
      // Allow custom icons
      if i > -1 then
        DoGetImage(MenuItem.Namespace, TargetImageList, TargetImageIndex);


      if RTL then
        ImageRect := Rect(ARect.Right - (TargetImageList.Width + 2 * Border), ARect.Top, ARect.Right, ARect.Bottom)
      else
        ImageRect := Rect(ARect.Left, ARect.Top, ARect.Left + TargetImageList.Width + 2 * Border, ARect.Bottom);

      if Selected and not IsWinXP then
        DrawEdge(ACanvas.Handle, ImageRect, BDR_RAISEDINNER, BF_RECT);

      OffsetRect(ImageRect, Border, ((ImageRect.Bottom - ImageRect.Top) - TargetImageList.Height) div 2);

      ImageList_Draw(TargetImageList.Handle, TargetImageIndex, ACanvas.Handle,
        ImageRect.Left, ImageRect.Top, ILD_TRANSPARENT);

      if RTL then
        ARect.Right := ARect.Right - (TargetImageList.Width + (2 * Border) + 1)
      else
        ARect.Left := ARect.Left + TargetImageList.Width + (2 * Border) + 1
    end;

    if Selected and not IsWinXP then
    begin
      ACanvas.Brush.Color := clHighlight;
      ACanvas.Font.Color := clBtnHighlight;
      ACanvas.FillRect(ARect);
    end;

    Inc(ARect.Left, 2);
    if TextExtentW(WS, ACanvas).cx > ARect.Right-ARect.Left then
      WS := ShortenStringEx(ACanvas.Handle, WS, ARect.Right-ARect.Left, RTL, Options.EllipsisPlacement);
    OldMode := SetBkMode(ACanvas.Handle, TRANSPARENT);
    // The are WideString compatible
    //Note: it seems that DrawTextW doesn't draw the prefix.
    i := Pos('&', WS);
    System.Delete(WS, i, 1);
    if IsUnicode then
      DrawTextW_VST(ACanvas.handle, PWideChar(WS), StrLenW(PWideChar(WS)), ARect, DT_SINGLELINE or DT_VCENTER)
    else begin
      S := WS;
      DrawText(ACanvas.handle, PChar(S), Length(S), ARect, DT_SINGLELINE or DT_VCENTER)
    end;
    SetBkMode(ACanvas.Handle, OldMode);
  end;
end;

procedure TVirtualSendToMenu.OnMenuItemMeasure(Sender: TObject;
  ACanvas: TCanvas; var Width, Height: Integer);
var
  WS: WideString;
  i: integer;
  Border: Integer;
begin
  if Sender is TMenuItem then
  begin
    i := TMenuItem(Sender).MenuIndex;

    if i > -1 then
    begin
      WS := TVirtualSendToMenuItem( Items[i]).Namespace.NameInFolder;
      Width := TextExtentW(WS, ACanvas).cx;
    end else
      Width := TextExtentW(TMenuItem(Sender).Caption, ACanvas).cx;

    if Options.Images then
    begin
      Border := 2 * Options.ImageBorder;
      if Options.LargeImages then
      begin
        Inc(Width, LargeSysImages.Width + Border);
        if LargeSysImages.Height + Border > Height then
          Height := LargeSysImages.Height + Border
      end else
      begin
        Inc(Width, SmallSysImages.Width + Border);
        if SmallSysImages.Height + Border > Height then
          Height := SmallSysImages.Height + Border
      end
    end;
  end;

  if Options.MaxWidth > 0 then
  begin
    if Width > Options.MaxWidth then
      Width := Options.MaxWidth;
  end;

  if Width > Screen.Width then
    Width := Screen.Width - 12  // Purely imperical value seen on XP for the unaccessable borders
end;

procedure TVirtualSendToMenu.Populate(MenuItem: TMenuItem);
var
  NS: TNamespace;
  M: TVirtualSendToMenuItem;
  i: integer;
  OldErrorMode: integer;
begin
  OldErrorMode := SetErrorMode(SEM_FAILCRITICALERRORS or SEM_NOOPENFILEERRORBOX);
  try
    SendToItems.Clear;
    NS := CreateSpecialNamespace(CSIDL_SENDTO);
    NS.EnumerateFolder(False, True, False, EnumSendToCallback, SendToItems);
    SendToItems.Sort(SendToMenuSort);
    for i := 0 to SendToItems.Count - 1 do
    begin
      M := TVirtualSendToMenuItem.Create(Self);
      M.Namespace := SendToItems[i];
      M.Caption := M.Namespace.NameNormal;
      M.ImageIndex := M.Namespace.GetIconIndex(False, icSmall);
      MenuItem.Add(M);
    end;
    SendToItems.Clear;
    NS.Free;

    DrivesFolder.EnumerateFolder(False, False, False, EnumSendToCallback, SendToItems);
    SendToItems.Sort(SendToMenuSort);
    for i := 0 to SendToItems.Count - 1 do
    begin
      M := TVirtualSendToMenuItem.Create(Self);
      M.Namespace := SendToItems[i];
      M.Caption := M.Namespace.NameNormal;
      M.ImageIndex := M.Namespace.GetIconIndex(False, icSmall);
      MenuItem.Add(M);
    end;
    SendToItems.Clear;
  finally
    SetErrorMode(OldErrorMode);
  end;
end;

{$IFDEF TBX_OR_TB2K}
procedure TVirtualSendToMenu.Populate_TB2000(MenuItem: TTBCustomItem);
var
  NS: TNamespace;
  M: TVirtualSendToMenuItem_TB2000;
  i: integer;
begin
  {$IFDEF TBX}
  if MenuItem is TTBXSubmenuItem then
    TTBXSubmenuItem(MenuItem).SubMenuImages := SmallSysImages;
  {$ELSE}
  if MenuItem is TTBSubmenuItem then
    TTBSubmenuItem(MenuItem).SubMenuImages := SmallSysImages;
  {$ENDIF TBX}
  MenuItem.Clear;

  SendToItems.Clear;
  NS := CreateSpecialNamespace(CSIDL_SENDTO);
  NS.EnumerateFolder(False, True, False, EnumSendToCallback, SendToItems);
  SendToItems.Sort(SendToMenuSort);
  for i := 0 to SendToItems.Count - 1 do
  begin
    M := TVirtualSendToMenuItem_TB2000.Create(Self);
    M.Namespace := SendToItems[i];
    M.Caption := M.Namespace.NameNormal;
    M.ImageIndex := M.Namespace.GetIconIndex(False, icSmall);
    MenuItem.Add(M);
  end;
  SendToItems.Clear;
  NS.Free;

  DrivesFolder.EnumerateFolder(False, False, False, EnumSendToCallback, SendToItems);
  SendToItems.Sort(SendToMenuSort);
  for i := 0 to SendToItems.Count - 1 do
  begin
    M := TVirtualSendToMenuItem_TB2000.Create(Self);
    M.Namespace := SendToItems[i];
    M.Caption := M.Namespace.NameNormal;
    M.ImageIndex := M.Namespace.GetIconIndex(False, icSmall);
    MenuItem.Add(M);
  end;
  SendToItems.Clear;
end;
{$ENDIF TBX_OR_TB2K}

procedure TVirtualSendToMenu.Popup(X, Y: Integer);
begin
  Images := SmallSysImages;
  {$IFNDEF DELPHI_5_UP}
  ClearMenuItems(Self);
  {$ELSE}
  Items.Clear;
  {$ENDIF DELPHI_5_UP}
  Populate(Items);
  inherited;
end;

{ TSendToMenuOptions }

constructor TSendToMenuOptions.Create;
begin
  Images := True;
  LargeImages := False;
  ImageBorder := 1;
  FEllipsisPlacement := sseMiddle;
  MaxWidth := -1;
end;

end.