Platforms to show: All Mac Windows Linux Cross-Platform
/Cocoa/NSStatusItem/StatusItem with NSView
Feedback.
Function:
You find this example project in your Plugins Download as a Realbasic project file within the examples folder: /Cocoa/NSStatusItem/StatusItem with NSView
This example is the version from Wed, 2nd Mar 2010.
Notes: Last modified: Wed, 2nd Mar 2010
Feedback.
Function:
You find this example project in your Plugins Download as a Realbasic project file within the examples folder: /Cocoa/NSStatusItem/StatusItem with NSView
This example is the version from Wed, 2nd Mar 2010.
Notes: Last modified: Wed, 2nd Mar 2010
Class Window1
Inherits Window
// Controls
ControlInstance
Sub Action() Handles Event
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.setView view
const NSLeftMouseDownMask = &h00002
const NSLeftMouseUpMask = &H00004
const NSLeftMouseDraggedMask = &h00040
const NSPeriodicMask = &h10000
e.SendActionOn NSLeftMouseDownMask+NSLeftMouseUpMask
End Sub
End ControlInstance
ControlInstance
Sub Action() Handles Event
e.Close
End Sub
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
Sub Action() Handles Event
if view=nil then
MsgBox "please create a menu first"
else
view.ToolTip=EditField2.text
end if
End Sub
End ControlInstance
ControlInstance
Sub Action() Handles Event
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=newPicture(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 Sub
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
Sub Action() Handles Event
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 Sub
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
Sub Action() Handles Event
msgbox "left="+str(e.Left)+EndOfLine+"top="+str(e.top)+EndOfLine+"width="+str(e.width)+EndOfLine+"height="+str(e.height)
End Sub
End ControlInstance
// Properties
Protected Dim e As MyCocoaStatusItemMBS
Dim i As integer
Protected Dim items(0) As NSMenuitemMBS
Protected Dim view As MyNSViewMBS
// Event implementations
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 Function
Sub Open()
dim f as FolderItem
e=new MyCocoaStatusItemMBS
if not e.Available then
MsgBox "Cocoa not loaded."
end if
End Sub
End Class
Class MyCocoaMenuItemMBS
Inherits NSMenuItemMBS
// Event implementations
Sub Action()
MsgBox "You chose menu item number "+str(tag)
End Sub
End Class
Class MyCocoaStatusItemMBS
Inherits NSStatusItemMBS
// Event implementations
Sub DoubleAction()
System.DebugLog "Statusitem.DoubleAction"
MsgBox "Double Click!"
End Sub
Sub Action()
System.DebugLog "Statusitem.Action"
run
End Sub
// Methods
Sub run()
dim d as date
d=new date
window1.Lastclick.text=d.LongTime
End Sub
End Class
Class App
Inherits Application
// Event implementations
Sub Open()
if TargetMachO=false then
MsgBox "This example needs a MachO target running on Mac OS X."
quit
end if
End Sub
End Class
Class MyNSViewMBS
Inherits CustomNSViewMBS
// Properties
Protected Dim clicked As boolean
Protected Dim image As nsimagembs
Dim statusitem As mycocoastatusItemMBS
Dim menu As nsmenumbs
// Event implementations
Sub Open()
image=GetSafariIcon
End Sub
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 Function
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 Function
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 Sub
// Methods
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
End Class
Class MyNSMenuMBS
Inherits NSMenuMBS
End Class
See also:
Links
MBS REAL studio tutorial videos - Pfarrgemeinde Ministranten Nickenich