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 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
|
{
*****************************************************************************
* QtWSMenus.pp *
* ------------ *
* *
* *
*****************************************************************************
*****************************************************************************
This file is part of the Lazarus Component Library (LCL)
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
}
unit QtWSMenus;
{$mode objfpc}{$H+}
interface
{$I qtdefines.inc}
uses
// Bindings
qt5,
qtwidgets, qtobjects, qtproc, QtWsControls,
// LCL
SysUtils, Classes, Types, LCLType, LCLProc, Graphics, Controls, Forms, Menus,
ImgList,
// Widgetset
WSMenus, WSLCLClasses;
type
{ TQtWSMenuItem }
TQtWSMenuItem = class(TWSMenuItem)
protected
class function CreateMenuFromMenuItem(const AMenuItem: TMenuItem): TQtMenu;
published
class procedure AttachMenu(const AMenuItem: TMenuItem); override;
class function CreateHandle(const AMenuItem: TMenuItem): HMENU; override;
class procedure DestroyHandle(const AMenuItem: TMenuItem); override;
class procedure SetCaption(const AMenuItem: TMenuItem; const ACaption: string); override;
class procedure SetShortCut(const AMenuItem: TMenuItem; const ShortCutK1, ShortCutK2: TShortCut); override;
class procedure SetVisible(const AMenuItem: TMenuItem; const Visible: boolean); override;
class function SetCheck(const AMenuItem: TMenuItem; const Checked: boolean): boolean; override;
class function SetEnable(const AMenuItem: TMenuItem; const Enabled: boolean): boolean; override;
class function SetRadioItem(const AMenuItem: TMenuItem; const RadioItem: boolean): boolean; override;
class function SetRightJustify(const AMenuItem: TMenuItem; const Justified: boolean): boolean; override;
class procedure UpdateMenuIcon(const AMenuItem: TMenuItem; const HasIcon: Boolean; const AIcon: TBitmap); override;
end;
{ TQtWSMenu }
TQtWSMenu = class(TWSMenu)
published
class function CreateHandle(const AMenu: TMenu): HMENU; override;
class procedure SetBiDiMode(const AMenu: TMenu; UseRightToLeftAlign, UseRightToLeftReading : Boolean); override;
end;
{ TQtWSMainMenu }
TQtWSMainMenu = class(TWSMainMenu)
published
end;
{ TQtWSPopupMenu }
TQtWSPopupMenu = class(TWSPopupMenu)
published
class procedure Popup(const APopupMenu: TPopupMenu; const X, Y: integer); override;
end;
implementation
{ TQtWSMenuItem }
{------------------------------------------------------------------------------
Function: TQtWSMenuItem.AttachMenu
Params: None
Returns: Nothing
------------------------------------------------------------------------------}
class procedure TQtWSMenuItem.AttachMenu(const AMenuItem: TMenuItem);
var
Widget: TQtWidget;
begin
if not WSCheckMenuItem(AMenuItem, 'AttachMenu') or (AMenuItem.Parent = nil) then
Exit;
Widget := TQtWidget(AMenuItem.Parent.Handle);
if Widget is TQtMenuBar then
TQtMenuBar(Widget).insertMenu(AMenuItem.Parent.VisibleIndexOf(AMenuItem),
QMenuH(TQtMenu(AMenuItem.Handle).Widget))
else
if Widget is TQtMenu then
TQtMenu(Widget).insertMenu(AMenuItem.Parent.VisibleIndexOf(AMenuItem),
QMenuH(TQtMenu(AMenuItem.Handle).Widget), AMenuItem);
end;
class function TQtWSMenuItem.CreateMenuFromMenuItem(const AMenuItem: TMenuItem): TQtMenu;
var
ImgList: TCustomImageList;
AImage: TQtImage;
Bmp: TBitmap;
AImgList: TImageList;
begin
Result := TQtMenu.Create(AMenuItem);
Result.FDeleteLater := False;
Result.setSeparator(AMenuItem.IsLine);
Result.setHasSubmenu(AMenuItem.Count > 0);
if not AMenuItem.IsLine then
begin
Result.setText(AMenuItem.Caption{%H-});
Result.setEnabled(AMenuItem.Enabled);
Result.setCheckable(AMenuItem.RadioItem or AMenuItem.ShowAlwaysCheckable);
Result.BeginUpdate;
Result.setChecked(AMenuItem.Checked);
Result.EndUpdate;
Result.setShortcut(AMenuItem.ShortCut, AMenuItem.ShortCutKey2);
if AMenuItem.HasIcon then
begin
ImgList := AMenuItem.GetImageList;
// we must check so because AMenuItem.HasIcon can return true
// if Bitmap is setted up but not ImgList.
if (ImgList <> nil) and (AMenuItem.ImageIndex >= 0) and
(AMenuItem.ImageIndex < ImgList.Count) then
begin
Bmp := TBitmap.Create;
try
ImgList.ResolutionForPPI[16, ScreenInfo.PixelsPerInchX, 1].GetBitmap(AMenuItem.ImageIndex, Bmp); // Qt bindings support only 16px icons for menu items
Result.setImage(TQtImage(Bmp.Handle));
finally
Bmp.Free;
end;
end else
if Assigned(AMenuItem.Bitmap) then
begin
AImage := TQtImage(AMenuItem.Bitmap.Handle);
if not AMenuItem.Bitmap.Transparent then
begin
AImgList := TImageList.Create(nil);
AImgList.Width := AMenuItem.Bitmap.Width;
AImgList.Height := AMenuItem.Bitmap.Height;
AImgList.AddMasked(AMenuItem.Bitmap, AMenuItem.Bitmap.Canvas.Pixels[0, AMenuItem.Bitmap.Height -1]);
Bmp := TBitmap.Create;
AImgList.GetBitmap(0, Bmp);
AImage := TQtImage(Bmp.Handle);
Result.setImage(AImage);
Bmp.Free;
AImgList.Free;
end else
Result.setImage(AImage);
end;
end else
Result.setImage(nil);
end;
end;
{------------------------------------------------------------------------------
Function: TQtWSMenuItem.CreateHandle
Params: None
Returns: Nothing
Creates a Menu Item
------------------------------------------------------------------------------}
class function TQtWSMenuItem.CreateHandle(const AMenuItem: TMenuItem): HMENU;
var
Menu: TQtMenu;
begin
{$ifdef VerboseQt}
WriteLn('trace:> [TQtWSMenuItem.CreateHandle] Caption: ', AMenuItem.Caption,
' Subitems: ' + IntToStr(AMenuItem.Count) +' HandleAllocated ? ', AMenuItem.HandleAllocated);
Write('trace:< [TQtWSMenuItem.CreateHandle]');
{$endif}
Menu := nil;
{------------------------------------------------------------------------------
This case should not happen. A menu item must have a parent, but it seams LCL
will sometimes create a menu item prior to creating it's parent.
So, if we arrive here, we must create this item as if it was a TMenu
------------------------------------------------------------------------------}
if (not AMenuItem.HasParent) then
begin
{$ifdef VerboseQt}
Write(' Parent: Menu without parent');
{$endif}
Result := TQtWSMenu.CreateHandle(AMenuItem.GetParentMenu);
end
{------------------------------------------------------------------------------
If the parent has no parent, then this item is directly owned by a TMenu
In this case we have to detect if the parent is a TMainMenu or a TPopUpMenu
because TMainMenu uses the special Handle QMenuBar while TPopUpMenu can be
treat like if this menu item was a subitem of another item
------------------------------------------------------------------------------}
else
if ((not AMenuItem.Parent.HasParent) and (AMenuItem.GetParentMenu is TMainMenu)) then
begin
Menu := CreateMenuFromMenuItem(AMenuItem);
Result := HMENU(Menu);
end
{------------------------------------------------------------------------------
If the parent has a parent, then that item's Handle is necessarely a TQtMenu
------------------------------------------------------------------------------}
else
begin
Menu := CreateMenuFromMenuItem(AMenuItem);
Result := HMENU(Menu);
end;
if Menu <> nil then
Menu.AttachEvents;
{$ifdef VerboseQt}
WriteLn(' Result: ', dbghex(Result));
{$endif}
end;
{------------------------------------------------------------------------------
Function: TQtWSMenuItem.DestroyHandle
Params: None
Returns: Nothing
Dealocates a Menu Item
------------------------------------------------------------------------------}
class procedure TQtWSMenuItem.DestroyHandle(const AMenuItem: TMenuItem);
var
Obj: TObject;
begin
{$ifdef VerboseQt}
WriteLn('[TQtWSMenuItem.DestroyHandle] Caption: ' + AMenuItem.Caption);
{$endif}
if Assigned(AMenuItem.Owner) then
begin
if (AMenuItem.Owner is TMainMenu) and
Assigned(TMainMenu(AMenuItem.Owner).Parent) and
(
(TMainMenu(AMenuItem.Owner).Parent is TCustomForm) or
(TMainMenu(AMenuItem.Owner).Parent is TCustomFrame)
)
then
begin
{do not destroy menuitem handle if parent form handle = 0 - it's
already destroyed (TCustomForm.DestroyWnd isn't called when
LM_DESTROY is sent from TQtWidget.SlotDestroy() }
if not TWinControl(TMainMenu(AMenuItem.Owner).Parent).HandleAllocated then
exit;
end;
end;
Obj := TObject(AMenuItem.Handle);
if Obj is TQtMenu then
TQtMenu(Obj).Release;
end;
{------------------------------------------------------------------------------
Function: TQtWSMenuItem.SetCaption
Params: None
Returns: Nothing
------------------------------------------------------------------------------}
class procedure TQtWSMenuItem.SetCaption(const AMenuItem: TMenuItem; const ACaption: string);
var
Widget: TQtWidget;
begin
{$ifdef VerboseQt}
WriteLn('[TQtWSMenuItem.SetCaption] Caption: ' + AMenuItem.Caption + ' NewCaption: ', ACaption);
{$endif}
if not WSCheckMenuItem(AMenuItem, 'SetCaption') then
Exit;
Widget := TQtWidget(AMenuItem.Handle);
if Widget is TQtMenu then
begin
TQtMenu(Widget).setSeparator(ACaption = cLineCaption);
if ACaption = cLineCaption then
TQtMenu(Widget).setText('')
else
TQtMenu(Widget).setText(ACaption{%H-});
end;
end;
{------------------------------------------------------------------------------
Function: TQtWSMenuItem.SetShortCut
Params: None
Returns: Nothing
------------------------------------------------------------------------------}
class procedure TQtWSMenuItem.SetShortCut(const AMenuItem: TMenuItem;
const ShortCutK1, ShortCutK2: TShortCut);
var
Widget: TQtWidget;
begin
{$ifdef VerboseQt}
WriteLn('[TQtWSMenuItem.SetCaption] SetShortCut: ' + AMenuItem.Caption);
{$endif}
if not WSCheckMenuItem(AMenuItem, 'SetShortCut') then
Exit;
Widget := TQtWidget(AMenuItem.Handle);
if Widget is TQtMenu then
TQtMenu(Widget).setShortcut(ShortCutK1, ShortCutK2);
end;
{------------------------------------------------------------------------------
Function: TQtWSMenuItem.SetVisible
Params: None
Returns: Nothing
------------------------------------------------------------------------------}
class procedure TQtWSMenuItem.SetVisible(const AMenuItem: TMenuItem; const Visible: boolean);
begin
{$ifdef VerboseQt}
WriteLn('[TQtWSMenuItem.SetVisible] SetShortCut: ' + AMenuItem.Caption + ' Visible: ', Visible);
{$endif}
if not WSCheckMenuItem(AMenuItem, 'SetVisible') then
Exit;
TQtMenu(AMenuItem.Handle).setVisible(Visible);
end;
{------------------------------------------------------------------------------
Function: TQtWSMenuItem.SetCheck
Params: None
Returns: Nothing
------------------------------------------------------------------------------}
class function TQtWSMenuItem.SetCheck(const AMenuItem: TMenuItem; const Checked: boolean): boolean;
begin
Result := False;
if not WSCheckMenuItem(AMenuItem, 'SetCheck') then
Exit;
TQtMenu(AMenuItem.Handle).BeginUpdate;
TQtMenu(AMenuItem.Handle).setChecked(Checked);
TQtMenu(AMenuItem.Handle).EndUpdate;
Result := True;
end;
{------------------------------------------------------------------------------
Function: TQtWSMenuItem.SetEnable
Params: None
Returns: Nothing
------------------------------------------------------------------------------}
class function TQtWSMenuItem.SetEnable(const AMenuItem: TMenuItem; const Enabled: boolean): boolean;
begin
Result := False;
if not WSCheckMenuItem(AMenuItem, 'SetEnable') then
Exit;
TQtMenu(AMenuItem.Handle).setEnabled(Enabled);
Result := True;
end;
{------------------------------------------------------------------------------
Function: TQtWSMenuItem.SetRadioItem
Params: None
Returns: Nothing
------------------------------------------------------------------------------}
class function TQtWSMenuItem.SetRadioItem(const AMenuItem: TMenuItem; const RadioItem: boolean): boolean;
begin
Result := False;
if not WSCheckMenuItem(AMenuItem, 'SetRadioItem') then
Exit;
{$ifdef VerboseQt}
WriteLn('[TQtWSMenuItem.SetRadioItem] AMenuItem: ' + AMenuItem.Name +
' Radio ? ',RadioItem);
{$endif}
if not RadioItem then
TQtMenu(AMenuItem.Handle).removeActionGroup;
TQtMenu(AMenuItem.Handle).setCheckable(RadioItem or AMenuItem.ShowAlwaysCheckable);
SetCheck(AMenuItem, AMenuItem.Checked);
Result := True;
end;
{------------------------------------------------------------------------------
Function: TQtWSMenuItem.SetRightJustify
Params: None
Returns: Nothing
------------------------------------------------------------------------------}
class function TQtWSMenuItem.SetRightJustify(const AMenuItem: TMenuItem; const Justified: boolean): boolean;
begin
if not WSCheckMenuItem(AMenuItem, 'SetRightJustify') then
Exit(False);
// what should be done here? maybe this?
TQtMenu(AMenuItem.Handle).setAttribute(QtWA_RightToLeft, Justified);
Result := True;
end;
class procedure TQtWSMenuItem.UpdateMenuIcon(const AMenuItem: TMenuItem;
const HasIcon: Boolean; const AIcon: TBitmap);
begin
if not WSCheckMenuItem(AMenuItem, 'UpdateMenuIcon') then
Exit;
if AMenuItem.HasParent then
begin
if Assigned(TQtMenu(AMenuItem.handle).actionHandle) then
QAction_setVisible(TQtMenu(AMenuItem.handle).actionHandle, False);
AMenuItem.RecreateHandle;
end;
end;
{ TQtWSMenu }
{------------------------------------------------------------------------------
Function: TQtWSMenu.CreateHandle
Params: None
Returns: Nothing
Creates a Menu
------------------------------------------------------------------------------}
class function TQtWSMenu.CreateHandle(const AMenu: TMenu): HMENU;
var
MenuBar: TQtMenuBar;
Menu: TQtMenu;
AParent: TComponent;
begin
Result := 0;
{ If the menu is a main menu, there is no need to create a handle for it.
It's already created on the window }
if (AMenu is TMainMenu) then
begin
AParent := AMenu.Parent;
if AParent = nil then
AParent := AMenu.Owner;
if Assigned(AParent) and
((AParent is TCustomForm) or (AParent is TCustomFrame)) then
begin
if (AParent is TCustomForm) then
MenuBar := TQtMainWindow(TCustomForm(AParent).Handle).MenuBarNeeded
else
MenuBar := TQtMainWindow(TCustomFrame(AParent).Handle).MenuBarNeeded;
Result := HMENU(MenuBar);
end else
begin
Menu := TQtMenu.Create(AMenu.Items);
Menu.AttachEvents;
Result := HMENU(Menu);
end;
end else
if (AMenu is TPopUpMenu) then
begin
Menu := TQtMenu.Create(AMenu.Items);
Menu.AttachEvents;
Result := HMENU(Menu);
end;
{$ifdef VerboseQt}
Write('[TQtWSMenu.CreateHandle] ');
if (AMenu is TMainMenu) then Write('IsMainMenu ');
WriteLn(' Handle: ', dbghex(Result), ' Name: ', AMenu.Name);
{$endif}
end;
class procedure TQtWSMenu.SetBiDiMode(const AMenu : TMenu; UseRightToLeftAlign,
UseRightToLeftReading : Boolean);
begin
TQtWidget(AMenu.Handle).setLayoutDirection(TLayoutDirectionMap[UseRightToLeftAlign]);
end;
{ TQtWSPopupMenu }
{------------------------------------------------------------------------------
Function: TQtWSPopupMenu.Popup
Params: None
Returns: Nothing
Creates a PopUp menu
------------------------------------------------------------------------------}
class procedure TQtWSPopupMenu.Popup(const APopupMenu: TPopupMenu; const X, Y: integer);
var
Point: TQtPoint;
Size: TSize;
Alignment: TPopupAlignment;
begin
{$ifdef VerboseQt}
WriteLn('[TQtWSPopupMenu.Popup] APopupMenu.Handle ' + dbghex(APopupMenu.Handle)
+ ' FirstItemName: ' + APopupMenu.Items.Name
+ ' FirstItemWND: ' + IntToStr(APopupMenu.Items.Handle)
+ ' FirstItemCount: ' + IntToStr(APopupMenu.Items.Count));
{$endif}
Point.X := X;
Point.Y := Y;
Alignment := APopupMenu.Alignment;
if APopupMenu.IsRightToLeft then
begin
if Alignment = paLeft then
Alignment := paRight
else
if Alignment = paRight then
Alignment := paLeft;
end;
case Alignment of
paCenter:
begin
QMenu_sizeHint(QMenuH(TQtMenu(APopupMenu.Handle).Widget), @Size);
Point.X := Point.X - (Size.cx div 2);
end;
paRight:
begin
QMenu_sizeHint(QMenuH(TQtMenu(APopupMenu.Handle).Widget), @Size);
Point.X := Point.X - Size.cx;
end;
end;
if APopupMenu.TrackButton = tbLeftButton then
TQtMenu(APopupMenu.Handle).trackButton := QtLeftButton
else
TQtMenu(APopupMenu.Handle).trackButton := QtRightButton;
// for win32 compatibility do a blocking call
TQtMenu(APopupMenu.Handle).Exec(@Point);
end;
end.
|