Recent

Author Topic: ssMeta  (Read 2820 times)

VTwin

  • Hero Member
  • *****
  • Posts: 1224
  • Former Turbo Pascal 3 user
ssMeta
« on: December 18, 2022, 09:28:30 pm »
I have always written MainMenu in code, using a conditional define "ssCmd" for ssMeta on Macintosh, and ssCtrl on Windows and Linux. For a new project, however, I decided to try the TMainMenu editor in the Form Designer.

It works very nicely, but with an apparent catch for Mac. I initially wrote the menu using ssMeta, but discovered that does not work on Windows or Linux. So, I rewrote the menu using ssCtrl, and added the following code to the Form.OnShow event:

Code: Pascal  [Select][+][-]
  1.  {$IFDEF DARWIN}
  2.   for mnu0 in mnuMain.Items do begin
  3.     for j := 0 to mnu0.Count-1 do begin
  4.       mnu1 := mnu0.Items[j];
  5.       w := mnu1.ShortCut;
  6.       if w.TestBit(14) then begin // 14 ($4000) = ctrl, 12 ($1000) = meta
  7.         w := w.ClearBit(14);
  8.         w := w.SetBit(12);
  9.         mnu1.ShortCut := w;
  10.       end;
  11.     end;
  12.   end;
  13.  {$ENDIF}

It works fine, but I wonder if I am doing it the hard way. By the way, it does not seem possible in Windows to choose or delete ssMeta in the TMainMenu editor.

Am I missing something easier? If not, maybe someone will find this code useful. Note that it is written for a simple menu. It would need another loop, or recursion, to account for submenus.

EDIT: I left out the types, w is a word.

« Last Edit: December 18, 2022, 09:33:52 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 14.5: Lazarus 3.4 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 3.4 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 3.4 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 1224
  • Former Turbo Pascal 3 user
Re: ssMeta
« Reply #1 on: December 18, 2022, 09:52:03 pm »
As a procedure:

Code: Pascal  [Select][+][-]
  1. procedure MenuCtrlToMeta(mnuMain: TMainMenu);
  2. var
  3.   w: word;
  4.   j: integer;
  5.   mnu0, mnu1: TMenuItem;
  6. begin
  7.   {$IFDEF DARWIN}
  8.   for mnu0 in mnuMain.Items do begin
  9.     for j := 0 to mnu0.Count-1 do begin
  10.       mnu1 := mnu0.Items[j];
  11.       w := mnu1.ShortCut;
  12.       if w.TestBit(14) then begin // 14 ($4000) = ctrl, 12 ($1000) = meta
  13.         w := w.ClearBit(14);
  14.         w := w.SetBit(12);
  15.         mnu1.ShortCut := w;
  16.       end;
  17.     end;
  18.   end;
  19.   {$ENDIF}
  20. end;
  21.  
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 14.5: Lazarus 3.4 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 3.4 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 3.4 (64 bit on VBox)

jwdietrich

  • Hero Member
  • *****
  • Posts: 1236
    • formatio reticularis
Re: ssMeta
« Reply #2 on: December 23, 2022, 04:31:37 pm »
I use a slightly different approach in my programs:

Code: Pascal  [Select][+][-]
  1. procedure AdaptMenus;
  2. { Adapts Menus and Shortcuts to the interface style guidelines
  3.   of the respective operating system }
  4. var
  5.   modifierKey: TShiftState;
  6. begin
  7.   {$IFDEF LCLcarbon}
  8.   modifierKey := [ssMeta];
  9.   ToolbarWindow.WinAboutItem.Visible := False;
  10.   ToolbarWindow.AppleMenu.Visible := True;
  11.   {$ELSE}
  12.   {$IFDEF LCLCocoa}
  13.   modifierKey := [ssMeta];
  14.   ToolbarWindow.WinAboutItem.Visible := False;
  15.   ToolbarWindow.AppleMenu.Visible := True;
  16.   {$ELSE}
  17.   modifierKey := [ssCtrl];
  18.   ToolbarWindow.WinAboutItem.Visible := True;
  19.   ToolbarWindow.AppleMenu.Visible := False;
  20.   {$ENDIF}
  21.   {$ENDIF}
  22.   ToolbarWindow.NewMenuItem.ShortCut := ShortCut(VK_N, modifierKey);
  23.   ToolbarWindow.OpenMenuItem.ShortCut := ShortCut(VK_O, modifierKey);
  24.   ToolbarWindow.CloseMenuItem.ShortCut := ShortCut(VK_W, modifierKey);
  25.   ToolbarWindow.SaveMenuItem.ShortCut := ShortCut(VK_S, modifierKey);
  26.   ToolbarWindow.QuitMenuItem.ShortCut := ShortCut(VK_Q, modifierKey);
  27.   ToolbarWindow.UndoMenuItem.ShortCut := ShortCut(VK_Z, modifierKey);
  28.   ToolbarWindow.RedoMenuItem.ShortCut := ShortCut(VK_Z, modifierKey + [ssShift]);
  29.   ToolbarWindow.CutMenuItem.ShortCut := ShortCut(VK_X, modifierKey);
  30.   ToolbarWindow.CopyMenuItem.ShortCut := ShortCut(VK_C, modifierKey);
  31.   ToolbarWindow.PasteMenuItem.ShortCut := ShortCut(VK_V, modifierKey);
  32.   ToolbarWindow.RunItem.ShortCut  := ShortCut(VK_R, modifierKey);
  33. end;
  34.  

This procedure also adapts the position of the "About" menu item according to the standards of the respective platform. I learned from your posts, however, that it could be more efficient by using a loop.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 3.4.0 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

VTwin

  • Hero Member
  • *****
  • Posts: 1224
  • Former Turbo Pascal 3 user
Re: ssMeta
« Reply #3 on: December 27, 2022, 05:05:27 pm »
Thanks jwdietrich. It is good to have multiple cross-platform examples, there is always a hitch.

This website

https://wiki.freepascal.org/Mac_Preferences_and_About_Menu

has a great tutorial on making a Mac menu, but for the life of me, I can not make it work. I attached my example project in case anyone can troubleshoot it.

“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 14.5: Lazarus 3.4 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 3.4 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 3.4 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 1224
  • Former Turbo Pascal 3 user
Re: ssMeta
« Reply #4 on: December 27, 2022, 05:16:15 pm »
Screenshot...
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 14.5: Lazarus 3.4 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 3.4 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 3.4 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 1224
  • Former Turbo Pascal 3 user
Re: ssMeta
« Reply #5 on: December 27, 2022, 05:31:20 pm »
Attached is a working example, thanks to Phil:

https://macpgmr.github.io/MacXPlatform/MacXPlatform_Part8.html#AppMenu

Unfortunately he has not posted here in ages.

“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 14.5: Lazarus 3.4 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 3.4 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 3.4 (64 bit on VBox)

 

TinyPortal © 2005-2018