Platforms to show: All Mac Windows Linux Cross-Platform

/MacCocoa/NSStatusItem/StatusItem with NSView


Required plugins for this example: MBS MacOSX Plugin, MBS MacControls Plugin, MBS MacCocoa Plugin, MBS MacBase Plugin, MBS Main Plugin, MBS Picture Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCocoa/NSStatusItem/StatusItem with NSView

This example is the version from Thu, 9th Aug 2023.

Project "StatusItem with NSView.xojo_binary_project"
Class Window1 Inherits Window
Control CreateMenuButton Inherits PushButton
ControlInstance CreateMenuButton Inherits PushButton
EventHandler Sub Action() dim p,m as Picture dim s as String dim img as NSImageMBS System.DebugLog "Create" call e.CreateMenu(-1) view=new MyNSViewMBS(0,0,30,20) view.needsDisplay=True view.statusitem=e e.HighlightMode=true // clickable e.view = view const NSLeftMouseDownMask = &h00002 const NSLeftMouseUpMask = &H00004 const NSLeftMouseDraggedMask = &h00040 const NSPeriodicMask = &h10000 e.SendActionOn NSLeftMouseDownMask+NSLeftMouseUpMask End EventHandler
End Control
Control DeleteMenuButton Inherits PushButton
ControlInstance DeleteMenuButton Inherits PushButton
EventHandler Sub Action() e.Close End EventHandler
End Control
Control EditField2 Inherits TextField
ControlInstance EditField2 Inherits TextField
End Control
Control SetTooltipButton Inherits PushButton
ControlInstance SetTooltipButton Inherits PushButton
EventHandler Sub Action() if view=nil then MsgBox "please create a menu first" else view.ToolTip=EditField2.text end if End EventHandler
End Control
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Control StaticText2 Inherits Label
ControlInstance StaticText2 Inherits Label
End Control
Control Lastclick Inherits Label
ControlInstance Lastclick Inherits Label
End Control
Control PushButton3 Inherits PushButton
ControlInstance PushButton3 Inherits PushButton
EventHandler Sub Action() msgbox "left="+str(e.Left)+EndOfLine+"top="+str(e.top)+EndOfLine+"width="+str(e.width)+EndOfLine+"height="+str(e.height) End EventHandler
End Control
EventHandler Function CancelClose(appQuitting as Boolean) As Boolean // no longer needed with NSStatusItemMBS class: 'if e.Handle<>0 then 'MsgBox "RB will crash if you close the application without deleting the menu." ' 'Return true 'end if End EventHandler
EventHandler Sub Open() dim f as FolderItem e=new MyCocoaStatusItemMBS if not e.Available then MsgBox "Cocoa not loaded." end if End EventHandler
Property Protected e As MyCocoaStatusItemMBS
Property i As integer
Property Protected items(0) As NSMenuitemMBS
Property Protected view As MyNSViewMBS
End Class
MenuBar Menu
MenuItem UntitledMenu3 = ""
MenuItem UntitledMenu2 = "File"
MenuItem FileQuit = "Quit"
MenuItem UntitledMenu7 = ""
MenuItem UntitledMenu0 = "Edit"
MenuItem EditUndo = "Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cut"
MenuItem EditCopy = "Copy"
MenuItem EditPaste = "Paste"
MenuItem EditClear = "Clear"
MenuItem UntitledMenu6 = ""
MenuItem UntitledMenu5 = ""
MenuItem UntitledMenu4 = ""
End MenuBar
Class MyCocoaStatusItemMBS Inherits NSStatusItemMBS
EventHandler Sub Action() System.DebugLog "Statusitem.Action" run End EventHandler
EventHandler Sub DoubleAction() System.DebugLog "Statusitem.DoubleAction" MsgBox "Double Click!" End EventHandler
Sub run() dim d as date d=new date window1.Lastclick.text=d.LongTime End Sub
End Class
Class App Inherits Application
EventHandler Sub Open() if TargetMachO=false then MsgBox "This example needs a MachO target running on Mac OS X." quit end if End EventHandler
End Class
Class MyNSViewMBS Inherits CustomNSViewMBS
EventHandler Sub DrawRect(g as NSGraphicsMBS, left as double, top as double, width as double, height as double) System.DebugLog "Realbasic drawRect event" if me.clicked then statusitem.DrawStatusBarBackground left,top,width,height,true else statusitem.DrawStatusBarBackground left,top,width,height,false end if // draw something to track mouse events g.SetColorRGB(0,0,0,0.05) g.fillRect left,top,width,height // draw icon g.drawAtPoint Image,0,2,0,0,16,16,g.NSCompositeSourceOver,1.0 End EventHandler
EventHandler Sub Open() image=GetSafariIcon End EventHandler
EventHandler Function mouseDown(e as NSEventMBS, x as double, y as double) As boolean System.DebugLog "Realbasic mousedown event" clicked=True needsDisplay=true // will make the view redraw End EventHandler
EventHandler Function mouseUp(e as NSEventMBS, x as double, y as double) As boolean System.DebugLog "Realbasic mouseup event" clicked=false needsDisplay=true // will make the view redraw if statusitem<>nil and menu<>Nil then statusitem.popUpStatusItemMenu(Menu) end if End EventHandler
Protected Function GetSafariIcon() As nsimagembs dim file as FolderItem dim p,m as Picture file=LaunchServicesFindApplicationForInfoMBS("","com.apple.Safari","") if file<>nil then p=file.IconImageMBS(16) m=file.IconMaskMBS(16) if p<>Nil and m<>nil then Return new NSImageMBS(p,m) end if end if End Function
Property Protected clicked As boolean
Property Protected image As nsimagembs
Property menu As nsmenumbs
Property statusitem As mycocoastatusItemMBS
End Class
Class MyNSMenuMBS Inherits NSMenuMBS
End Class
End Project

See also:

The items on this page are in the following plugins: MBS MacCocoa Plugin.


The biggest plugin in space...