Xojo Conferences
MBSSep2018MunichDE
XDCMay2019MiamiUSA

Platforms to show: All Mac Windows Linux Cross-Platform

/MacCocoa/NSStatusItem/StatusItem
Function:
Required plugins for this example: MBS MacBase Plugin, MBS MacCocoa Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCocoa/NSStatusItem/StatusItem
This example is the version from Wed, 21th Nov 2017.
Project "StatusItem.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 if not e.CreateMenu(-1) then break end if e.Title=EditField1.text e.HighlightMode=true // clickable p = new picture(70,10,32) p.Graphics.ForeColor=rgb(255,0,0) p.Graphics.fillrect 0,0,10,10 p.Graphics.ForeColor=rgb(0,255,0) p.Graphics.fillrect 10,0,10,10 p.Graphics.ForeColor=rgb(0,0,255) p.Graphics.fillrect 20,0,10,10 p.Graphics.ForeColor=rgb(0,0,0) p.Graphics.fillrect 30,0,10,10 p.Graphics.ForeColor=rgb(127,0,0) p.Graphics.fillrect 40,0,10,10 p.Graphics.ForeColor=rgb(0,127,0) p.Graphics.fillrect 50,0,10,10 p.Graphics.ForeColor=rgb(0,0,127) p.Graphics.fillrect 60,0,10,10 m = new picture(70,10,32) // Mask m.Graphics.ForeColor=rgb(0,0,0) // full m.Graphics.FillRect 0,0,30,10 m.Graphics.ForeColor=rgb(127,127,127) // half m.Graphics.FillRect 40,0,30,10 m.Graphics.ForeColor=rgb(255,255,255) // nothing m.Graphics.FillRect 30,0,10,10 img=new NSImageMBS(p,m) e.Image=img // New in 10.3, an alternative picture: p = new picture(70,10,32) p.Graphics.ForeColor=rgb(255,255,255) // first white p.Graphics.fillrect 0,0,10,10 p.Graphics.ForeColor=rgb(0,255,0) p.Graphics.fillrect 10,0,10,10 p.Graphics.ForeColor=rgb(0,0,255) p.Graphics.fillrect 20,0,10,10 p.Graphics.ForeColor=rgb(0,0,0) p.Graphics.fillrect 30,0,10,10 p.Graphics.ForeColor=rgb(127,0,0) p.Graphics.fillrect 40,0,10,10 p.Graphics.ForeColor=rgb(0,127,0) p.Graphics.fillrect 50,0,10,10 p.Graphics.ForeColor=rgb(0,0,127) p.Graphics.fillrect 60,0,10,10 m = new picture(70,10,32) // Mask m.Graphics.ForeColor=rgb(0,0,0) // full m.Graphics.FillRect 0,0,30,10 m.Graphics.ForeColor=rgb(127,127,127) // half m.Graphics.FillRect 40,0,30,10 m.Graphics.ForeColor=rgb(255,255,255) // nothing m.Graphics.FillRect 30,0,10,10 img=new NSImageMBS(p,m) e.AlternateImage=img 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 EnableButton Inherits PushButton
ControlInstance EnableButton Inherits PushButton
EventHandler Sub Action() e.Enabled=True End EventHandler
End Control
Control DisableButton Inherits PushButton
ControlInstance DisableButton Inherits PushButton
EventHandler Sub Action() e.Enabled=false End EventHandler
End Control
Control EditField1 Inherits TextField
ControlInstance EditField1 Inherits TextField
End Control
Control SetTitleButton Inherits PushButton
ControlInstance SetTitleButton Inherits PushButton
EventHandler Sub Action() e.title=EditField1.text 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() e.ToolTip=EditField2.text End EventHandler
End Control
Control AddMenuButton Inherits PushButton
ControlInstance AddMenuButton Inherits PushButton
EventHandler Sub Action() dim a,b,c,d as MyCocoaMenuItemMBS dim p as Picture m=new MyCocoaMenuMBS 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 c=new MyCocoaMenuItemMBS c.CreateMenuItem "Counter" c.Enabled=true c.state=0 items.Append c m.AddItem c m.CountMenu=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 End EventHandler
End Control
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Control Timer1 Inherits Timer
ControlInstance Timer1 Inherits Timer
EventHandler Sub Action() if e.Handle<>0 then i=i+1 e.title="Test "+str(i) end if End EventHandler
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 PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action() dim s as string dim a as NSAttributedStringMBS s=s+"{\rtf1\mac\ansicpg10000\cocoartf102"+chr(13) s=s+"{\fonttbl\f0\fnil\fcharset77 Zapfino;}"+chr(13) s=s+"{\colortbl;\red255\green255\blue255;}"+chr(13) s=s+"\paperw11900\paperh16840\margl1440\margr1440\vieww9020\viewh7500\viewkind0"+chr(13) s=s+"\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\ql\qnatural"+chr(13) s=s+""+chr(13) s=s+"\f0\fs24 \cf0 Hello World}" a=new NSAttributedStringMBS if a.initWithRTF(s) then e.AttributedTitle=a Timer1.Mode=0 // don't change it! end if 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 PushButton2 Inherits PushButton
ControlInstance PushButton2 Inherits PushButton
EventHandler Sub Action() ChangeImage End EventHandler
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
Protected Sub ChangeImage() dim p,m as Picture dim img as NSImageMBS p = new picture(70,10,32) p.Graphics.ForeColor=rgb(rnd*255,rnd*255,rnd*255) p.Graphics.fillrect 0,0,10,10 p.Graphics.ForeColor=rgb(rnd*255,rnd*255,rnd*255) p.Graphics.fillrect 10,0,10,10 p.Graphics.ForeColor=rgb(rnd*255,rnd*255,rnd*255) p.Graphics.fillrect 20,0,10,10 p.Graphics.ForeColor=rgb(0,0,0) p.Graphics.fillrect 30,0,10,10 p.Graphics.ForeColor=rgb(rnd*127,rnd*127,rnd*127) p.Graphics.fillrect 40,0,10,10 p.Graphics.ForeColor=rgb(rnd*127,rnd*127,rnd*127) p.Graphics.fillrect 50,0,10,10 p.Graphics.ForeColor=rgb(rnd*127,rnd*127,rnd*127) p.Graphics.fillrect 60,0,10,10 m = new picture(70,10,32) // Mask m.Graphics.ForeColor=rgb(0,0,0) // full m.Graphics.FillRect 0,0,30,10 m.Graphics.ForeColor=rgb(127,127,127) // half m.Graphics.FillRect 40,0,30,10 m.Graphics.ForeColor=rgb(255,255,255) // nothing m.Graphics.FillRect 30,0,10,10 img=new NSImageMBS(p,m) e.Image=img // New in 10.3, an alternative picture: p = new picture(70,10,32) p.Graphics.ForeColor=rgb(rnd*255,rnd*255,rnd*255) p.Graphics.fillrect 0,0,10,10 p.Graphics.ForeColor=rgb(rnd*255,rnd*255,rnd*255) p.Graphics.fillrect 10,0,10,10 p.Graphics.ForeColor=rgb(rnd*255,rnd*255,rnd*255) p.Graphics.fillrect 20,0,10,10 p.Graphics.ForeColor=rgb(0,0,0) p.Graphics.fillrect 30,0,10,10 p.Graphics.ForeColor=rgb(rnd*127,rnd*127,rnd*127) p.Graphics.fillrect 40,0,10,10 p.Graphics.ForeColor=rgb(rnd*127,rnd*127,rnd*127) p.Graphics.fillrect 50,0,10,10 p.Graphics.ForeColor=rgb(rnd*127,rnd*127,rnd*127) p.Graphics.fillrect 60,0,10,10 m = new picture(70,10,32) // Mask m.Graphics.ForeColor=rgb(0,0,0) // full m.Graphics.FillRect 0,0,30,10 m.Graphics.ForeColor=rgb(127,127,127) // half m.Graphics.FillRect 40,0,30,10 m.Graphics.ForeColor=rgb(255,255,255) // nothing m.Graphics.FillRect 30,0,10,10 img=new NSImageMBS(p,m) e.AlternateImage=img End Sub
Property Protected e As MyCocoaStatusItemMBS
Property i As integer
Property Protected items(0) As NSMenuitemMBS
Property m As MyCocoaMenuMBS
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 MyCocoaMenuMBS Inherits NSMenuMBS
EventHandler Sub EnableMenuItems() counter=counter+1 CountMenu.Title="EnableMenuItems Counter: "+Str(counter) End EventHandler
Property CountMenu As NSMenuItemMBS
Property Counter As Integer
End Class
End Project

See also:

Feedback, Comments & Corrections

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





Links
MBS Xojo PDF Plugins