Find Menu Bar height?

Mac OSX specific forum
WilliamL
Addict
Addict
Posts: 1224
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Find Menu Bar height?

Post by WilliamL »

[changed topic to reflect better what the conversation is about]

---solved below---

I have found that the Menu bar height is different on the Silicon machine (66) and the Intel machine (54). I need a way to tell which machine I'm on and adjust accordingly.

Is there a way to tell which machine I'm on or even find the Menu bar height directly?
Last edited by WilliamL on Mon Mar 14, 2022 12:49 pm, edited 2 times in total.
MacBook Pro-M1 (2021), Sonoma 14.4.1, PB 6.10LTS M1
User avatar
mk-soft
Always Here
Always Here
Posts: 5389
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Is there a way to tell if it is Silicon or Intel?

Post by mk-soft »

I guess it depends on the monitor. For me it's only 24 pixels. Full HD 24"

Code: Select all

; TOP
; MacOS: Get menubar height

Procedure GetMenuBarHeight()
  Protected NSApp, NSMenu, Value.d
  
  NSApp = CocoaMessage(0, 0, "NSApplication sharedApplication")
  NSMenu = CocoaMessage(0, NSApp, "mainMenu")
  CocoaMessage(@Value, NSMenu, "menuBarHeight")
  ProcedureReturn Value
EndProcedure

Height = GetMenuBarHeight()

Debug Height
Last edited by mk-soft on Sun Mar 13, 2022 8:33 am, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
BarryG
Addict
Addict
Posts: 3318
Joined: Thu Apr 18, 2019 8:17 am

Re: Is there a way to tell if it is Silicon or Intel?

Post by BarryG »

As a non-Mac user, I see the manual for MenuHeight() says the menu bar is not part of the window itself, and that it's at the top of the screen. So I'm wondering why it would matter how high it is?
User avatar
mk-soft
Always Here
Always Here
Posts: 5389
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Is there a way to tell if it is Silicon or Intel?

Post by mk-soft »

For example, if you want to draw your own icon for the SysTray
or to place a separate menu under the systray
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
WilliamL
Addict
Addict
Posts: 1224
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Is there a way to tell if it is Silicon or Intel?

Post by WilliamL »

Thanks mk-soft,

Unfortunately, the code gives me 37 (should be 66 using trial-and-error or 55 on my Air). [added] I forgot to add in the Window Title Bar area. :oops:

This how I fit a window on the screen if it has to be full height like when a list is too long to fit on the desktop or I have a picture that I want to fill the screen.

Code: Select all

    Define h=totlist*18 : If h>DesktopHeight(0)-#menubarheight-#wndtitlebarheight: h=DesktopHeight(0)-#menubarheight-#wndtitlebarheight : EndIf
    
    If OpenWindow(#ShowFilesWnd,0,0,w,h,"List of files",#PB_Window_SystemMenu | #PB_Window_ScreenCentered); | #PB_Window_SizeGadget)
Last edited by WilliamL on Mon Mar 14, 2022 12:43 pm, edited 1 time in total.
MacBook Pro-M1 (2021), Sonoma 14.4.1, PB 6.10LTS M1
WilliamL
Addict
Addict
Posts: 1224
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Find Menu Bar height?

Post by WilliamL »

[deleted]
Last edited by WilliamL on Mon Mar 14, 2022 12:35 pm, edited 1 time in total.
MacBook Pro-M1 (2021), Sonoma 14.4.1, PB 6.10LTS M1
User avatar
mk-soft
Always Here
Always Here
Posts: 5389
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Find Menu Bar height?

Post by mk-soft »

MenuBarHeight() must be zero so that the code also fits windows.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
WilliamL
Addict
Addict
Posts: 1224
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Find Menu Bar height?

Post by WilliamL »

mk-soft,

I think you gave me the answer and I just need to add in the window title bar height. I've always done that in the past and somehow I forgot that. With your code to find the (new) Menu Bar height + the hight of the window title bar I create I should come up with the required ~66 or ~55 pixels. I guess I will have to use your code to find the menu bar height in the future and not guess that it will always be the same. :)

[from post by mk-soft above]

Code: Select all

; MacOS: Get menubar height

Procedure GetMenuBarHeight()
  Protected NSApp, NSMenu, Value.d
  
  NSApp = CocoaMessage(0, 0, "NSApplication sharedApplication")
  NSMenu = CocoaMessage(0, NSApp, "mainMenu")
  CocoaMessage(@Value, NSMenu, "menuBarHeight")
  ProcedureReturn Value
EndProcedure
MacBook Pro-M1 (2021), Sonoma 14.4.1, PB 6.10LTS M1
User avatar
mk-soft
Always Here
Always Here
Posts: 5389
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Find Menu Bar height?

Post by mk-soft »

Actually, you don't need the MenuBarHeight at all.
- The MenuBar is not part of the calculation of the height of the window content.
- ToolBar increase the outer size of the window
- StatusBar belongs to the calculation of gadget positions.

When you create a window with a maximum desktop height, it is automatically limited by the menu height and dock height.
After that you can check the real inner height of the window and adjust your gadgets or create them afterwards.

Code: Select all

; TOP
; MacOS: Get menubar height

Procedure GetMenuBarHeight()
  Protected NSApp, NSMenu, Value.d
  
  NSApp = CocoaMessage(0, 0, "NSApplication sharedApplication")
  NSMenu = CocoaMessage(0, NSApp, "mainMenu")
  CocoaMessage(@Value, NSMenu, "menuBarHeight")
  ProcedureReturn Value
EndProcedure

; ********

CompilerIf #PB_Compiler_IsMainFile
  
  ;-TOP
  
  Enumeration Windows
    #Main
  EndEnumeration
  
  Enumeration MenuBars
    #MainMenu
  EndEnumeration
  
  Enumeration MenuItems
    ;
  EndEnumeration
  
  Enumeration Gadgets
    ;
  EndEnumeration
  
  Enumeration Status
    #MainStatusBar
  EndEnumeration
  
  Procedure UpdateWindow()
    Protected dx, dy
    dx = WindowWidth(#Main)
    dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar)
    ; Resize gadgets
  EndProcedure
  
  Procedure Main()
    Protected dx, dy
    
    #MainStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
    
    ExamineDesktops()
    dx = DesktopWidth(0)
    dy = DesktopHeight(0); - GetMenuBarHeight()
    
    If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, dx, dy, "Window" , #MainStyle)
      ; Menu
      CreateMenu(#MainMenu, WindowID(#Main))
      
      ; StatusBar
      CreateStatusBar(#MainStatusBar, WindowID(#Main))
      AddStatusBarField(#PB_Ignore)
      
      ; Gadgets
      dx = WindowWidth(#Main)
      dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar)
      Debug "dx = " + dx + " / dy = " + dy
      
      ; Bind Events
      BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
      
      Repeat
        Select WaitWindowEvent()
          Case #PB_Event_CloseWindow
            Break
            
          Case #PB_Event_Menu
            Select EventMenu()
                CompilerIf #PB_Compiler_OS = #PB_OS_MacOS   
                Case #PB_Menu_About
                  
                Case #PB_Menu_Preferences
                  
                Case #PB_Menu_Quit
                  PostEvent(#PB_Event_CloseWindow, #Main, #Null)
                  
                CompilerEndIf
                
            EndSelect
            
          Case #PB_Event_Gadget
            Select EventGadget()
                
            EndSelect
            
        EndSelect
      ForEver
      
    EndIf
    
  EndProcedure : Main()
  
CompilerEndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
WilliamL
Addict
Addict
Posts: 1224
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Find Menu Bar height?

Post by WilliamL »

mk-soft,

Your example is very interesting and works fine. I find that if I take out all the modifers except #PB_Window_SystemMenu the window will be too large. I have never used #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget and am not even sure what they do but I do use #PB_Window_SizeGadget but not in my recent example. So you are correct and I guess I just need to include one of these other modifiers. In fact, if you run any of the examples in WaitWindowEvent() of the Help file the windows will come out too large for the screen. (wrong modifiers)

I am really glad to have a way to get the right results.
MacBook Pro-M1 (2021), Sonoma 14.4.1, PB 6.10LTS M1
Post Reply