Xojo Conferences
MBSSep2018MunichDE
XDCMay2019MiamiUSA

Platforms to show: All Mac Windows Linux Cross-Platform

/MacCocoa/NSStatusItem/StatusItem with NSView
Function:
Required plugins for this example: MBS MacBase Plugin, MBS MacCocoa Plugin, MBS MacControls Plugin, MBS MacOSX 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, 16th Nov 2016.
Project "StatusItem with NSView.rbp"
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 AddMenuButton Inherits PushButton
ControlInstance AddMenuButton Inherits PushButton
EventHandler Sub Action() dim a,b,c,d as MyCocoaMenuItemMBS dim m as MyNSMenuMBS dim p as Picture m=new MyNSMenuMBS a=new MyCocoaMenuItemMBS a.CreateMenuItem "Top menu entry" a.Enabled=true a.Tag=1 items.Append a // so RB doesn't free the object. m.AddItem a b=new MyCocoaMenuItemMBS b.CreateSeparator items.Append b m.AddItem b c=new MyCocoaMenuItemMBS c.CreateMenuItem "Middle menu entry" c.Enabled=true c.Tag=2 c.state=1 items.Append c m.AddItem c d=new MyCocoaMenuItemMBS d.CreateMenuItem "Bottom menu entry" d.Enabled=false d.tag=3 items.Append d m.AddItem d const NSShiftKeyMask=131072 const NSControlKeyMask=262144 const NSAlternateKeyMask=524288 const NSCommandKeyMask=1048576 const NSFunctionKeyMask=8388608 const NSF1FunctionKey=&hF704 // From NSEvents.h d=new MyCocoaMenuItemMBS d.CreateMenuItem "Function key test","" d.Enabled=true d.KeyEquivalent=encodings.UTF16.chr(NSF1FunctionKey) d.KeyEquivalentModifierMask=NSCommandKeyMask+NSFunctionKeyMask d.tag=4 items.Append d m.AddItem d d=new MyCocoaMenuItemMBS d.CreateMenuItem "Last menu entry","" d.Enabled=true d.KeyEquivalent="A" d.KeyEquivalentModifierMask=NSShiftKeyMask+NSCommandKeyMask+NSAlternateKeyMask // command-option-shift d.tag=5 p = new picture(10,10,32) p.Graphics.ForeColor=rgb(0,128,0) p.Graphics.fillrect 0,0,10,10 p.Graphics.ForeColor=rgb(255,0,0) p.Graphics.filloval 0,0,10,10 d.Image=new NSImageMBS(p) items.Append d m.AddItem d e.Menu=m view.menu=m End EventHandler
End Control
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Control AddSubmenuButton Inherits PushButton
ControlInstance AddSubmenuButton Inherits PushButton
EventHandler Sub Action() dim f as NSMenuItemMBS dim a,b,c,d as MyCocoaMenuItemMBS dim m,mm as NSMenuMBS m=new NSMenuMBS a=new MyCocoaMenuItemMBS a.CreateMenuItem "Top submenu entry","" a.Enabled=true a.tag=10 items.Append a m.AddItem a b=new MyCocoaMenuItemMBS b.CreateSeparator items.Append b m.AddItem b c=new MyCocoaMenuItemMBS c.CreateMenuItem "Middle submenu entry","" c.Enabled=true c.tag=11 items.Append c m.AddItem c d=new MyCocoaMenuItemMBS d.CreateMenuItem "Bottom submenu entry","" d.Enabled=false d.tag=12 items.Append d m.AddItem d mm=e.menu f=mm.Item(2) f.title="Hello?" f.Submenu=m if f.HasSubmenu then // ok else MsgBox "failed to create menu!?" end if Exception MsgBox "First, create a menu and add a menu to it, before adding the submenu." End EventHandler
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 UntitledMenu0 = "Edit"
MenuItem EditUndo = "Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cut"
MenuItem EditCopy = "Copy"
MenuItem EditPaste = "Paste"
MenuItem EditClear = "Clear"
End MenuBar
Class MyCocoaMenuItemMBS Inherits NSMenuItemMBS
EventHandler Sub Action() MsgBox "You chose menu item number "+str(tag) End EventHandler
End Class
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:

Feedback, Comments & Corrections

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





Links
MBS FileMaker Plugins