Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS MacControls Plugin, 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/Text in titlebar
This example is the version from Sun, 17th Mar 2012.
Project "Text in titlebar.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Open()
if not TargetCocoa then
MsgBox "Requires only for Cocoa."
end if
End EventHandler
End Class
Class Main Inherits ExtendedWindow
Control ChangeTextButton Inherits Pushbutton
ControlInstance ChangeTextButton Inherits Pushbutton
EventHandler Sub Action()
//update the text value in the title bar according to the value in the textfield
Self.TitlebarText = TitlebarTextField.Text
End EventHandler
End Control
Control ChangeTextColorButton Inherits PushButton
ControlInstance ChangeTextColorButton Inherits PushButton
EventHandler Sub Action()
//ask the user to pick a color and set it as the new text color
Dim PickedColor as Color = Self.TitlebarTextColor
Dim DidPickColor as Boolean = SelectColor(PickedColor, "Pick a color")
Self.TitlebarTextColor = PickedColor
End EventHandler
End Control
Control TitlebarTextField Inherits TextField
ControlInstance TitlebarTextField Inherits TextField
End Control
Control TitlebarTextVisibleCheck Inherits CheckBox
ControlInstance TitlebarTextVisibleCheck Inherits CheckBox
EventHandler Sub Action()
//update the visibility of the text
Self.TitlebarTextVisible = Me.Value
End EventHandler
End Control
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
EventHandler Sub Close()
//quit the example application when the main window is closed
Quit()
End EventHandler
EventHandler Sub Open()
//uncomment the line of code below to enable fullscreen-support for this window, the text in the title bar will automatically position correctly
//Self.FullScreenPrimaryMBS = True
//set the initial value of the text, but don't fade it in
Self.TitlebarText(False) = "Click here!"
End EventHandler
EventHandler Sub TitlebarTextClicked()
//display a message letting the user know that he/she clicked the text
MsgBox("Titlebar text clicked" + EndOfLine + EndOfLine + "You just clicked the text in the titlebar.")
End EventHandler
EventHandler Sub TitlebarTextVisibilityChanged(Visible as Boolean)
//set the value of the checkbox, according to the visibility of the text
TitlebarTextVisibleCheck.Value = Visible
End EventHandler
Note "About"
This example was created by Joris Vervuurt ( http://www.joris-vervuurt.com/ )
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class ExtendedWindow Inherits Window
ComputedProperty TitlebarTextColor As Color
Sub Set()
//store the passed Color object
PrivateTitlebarTextColor = value
//create an NSColor object from the TextColor parameter
PrivateTitlebarTextNSColor = NSColorMBS.colorWithDeviceRGB(value.Red / 255.0, value.Green / 255.0, value.Blue / 255.0)
//update the text color, only if there is currently text added to the title bar
If not (TopLabel = Nil) Then
TopLabel.textColor = PrivateTitlebarTextNSColor
End If
End Set
Sub Get()
//if a custom color has not been set, return the default color, otherwise return the custom color
If (PrivateTitlebarTextNSColor = Nil) Then
Return RGB(55, 55, 55)
Else
Return PrivateTitlebarTextColor
End If
End Get
End ComputedProperty
ComputedProperty TitlebarTextVisible As Boolean
Sub Set()
//store the passed value
PrivateTitlebarTextVisible = value
//if there is currently any text set, hide or show the text according to the passed value
If not (TitlebarView = Nil) Then
If (PrivateTitlebarTextVisible) Then
TitlebarView.frameTop = Self.NSWindowMBS.frame.Height - 21.0
Else
TitlebarView.frameTop = Self.NSWindowMBS.frame.Height
End If
End If
//raise the TitlebarTextVisibilityChanged event with the passed value
RaiseEvent TitlebarTextVisibilityChanged(PrivateTitlebarTextVisible)
End Set
Sub Get()
//return the True if the text is visible and return False if the text is hidden
Return PrivateTitlebarTextVisible
End Get
End ComputedProperty
Event Close()
End Event
Event Open()
End Event
Event TitlebarTextClicked()
End Event
Event TitlebarTextVisibilityChanged(Visible as Boolean)
End Event
EventHandler Sub Close()
//remove the text in the titlebar before closing the window
RemoveTitlebarText()
RaiseEvent Close
End EventHandler
EventHandler Sub Open()
//check the system version and set the values of the IsLion, IsSnowLeopard and IsLeopard properties
CheckSystemVersion()
RaiseEvent Open
End EventHandler
Private Sub CheckSystemVersion()
//check the system version and set the values of the IsLion, IsSnowLeopard and IsLeopard properties
Dim SystemVersion as Integer
Call System.Gestalt("sysv", SystemVersion)
If ((SystemVersion >= &h1070) and (SystemVersion < &h1080)) Then
IsLion = True
ElseIf ((SystemVersion >= &h1060) and (SystemVersion < &h1070)) Then
IsSnowLeopard = True
ElseIf ((SystemVersion >= &h1050) and (SystemVersion < &h1060)) Then
IsLeopard = True
End If
End Sub
Private Function CreateNewLabel(IsTopLabel as Boolean, Text as String, LabelWidth as Double, Font as NSFontMBS) As NSTextViewMBS
Dim Y as Double = 1.0
Dim LabelTextColor as NSColorMBS = PrivateTitlebarTextNSColor
If not (IsTopLabel) Then
Y = 0.0
//set the 'shadow' color according to the system version
If (IsLion) Then
LabelTextColor = NSColorMBS.colorWithDeviceRGB(230.0 / 255.0, 230.0 / 255.0, 230.0 / 255.0)
ElseIf (IsSnowLeopard) Then
LabelTextColor = NSColorMBS.colorWithDeviceRGB(210.0 / 255.0, 210.0 / 255.0, 210.0 / 255.0)
ElseIf (IsLeopard) Then
LabelTextColor = NSColorMBS.colorWithDeviceRGB(210.0 / 255.0, 210.0 / 255.0, 210.0 / 255.0)
Else //default color value
LabelTextColor = NSColorMBS.colorWithDeviceRGB(230.0 / 255.0, 230.0 / 255.0, 230.0 / 255.0)
End If
End If
//create a new NSTextView, set its properties and then return it
Dim LabelToReturn as New NSTextViewMBS(0.0, Y, LabelWidth, 15.0)
LabelToReturn.font = Font
LabelToReturn.textColor = LabelTextColor
LabelToReturn.text = Text
LabelToReturn.isSelectable = False
LabelToReturn.drawsBackground = False
LabelToReturn.alignment = NSTextViewMBS.NSRightTextAlignment
Return LabelToReturn
End Function
Private Sub HandleEnterFullScreen(sender as FullScreenEventMirror)
//hide the text when entering fullscreen mode, if on Lion
TitlebarTextVisible = False
End Sub
Private Sub HandleExitFullScreen(sender as FullScreenEventMirror)
//show the text when exiting fullscreen mode, if on Lion
TitlebarTextVisible = True
End Sub
Private Sub HandleFailedToEnterFullScreen(sender as FullScreenEventMirror)
//show the text again when entering fullscreen mode failed, if on Lion
TitlebarTextVisible = True
End Sub
Private Sub HandleFailedToExitFullScreen(sender as FullScreenEventMirror)
//hide the text again when exiting fullscreen mode failed, if on Lion
TitlebarTextVisible = False
End Sub
Private Function HandleMouseUp(sender as CustomNSViewMBS, e as NSEventMBS, x as Double, y as Double) As Boolean
//raise the TitlebarTextClicked event
RaiseEvent TitlebarTextClicked
Return True
End Function
Private Sub RemoveTitlebarText()
//remove the titlebar text if there is currently text added to the title bar
If not (TitlebarView = Nil) Then
//make the text invisible
TitlebarTextVisible = False
//remove the mouse click handler
RemoveHandler TitlebarView.mouseUp, AddressOf HandleMouseUp
//if on Lion and if the Window is fullscreen-enabled, remove the fullscreen events handlers
If (IsLion and Self.FullScreenPrimaryMBS) Then
If not (FullScreenEventMirrorObject = Nil) Then
RemoveHandler FullScreenEventMirrorObject.WillEnterFullScreen, AddressOf HandleEnterFullScreen
RemoveHandler FullScreenEventMirrorObject.DidFailToEnterFullScreen, AddressOf HandleFailedToEnterFullScreen
RemoveHandler FullScreenEventMirrorObject.WillExitFullScreen, AddressOf HandleExitFullScreen
RemoveHandler FullScreenEventMirrorObject.DidFailToExitFullScreen, AddressOf HandleFailedToExitFullScreen
FullScreenEventMirrorObject = Nil
End If
End If
//remove and destroy the top label
TopLabel.removeFromSuperview()
TopLabel = Nil
//remove and destroy the bottom 'shadow' label
BottomLabel.removeFromSuperview()
BottomLabel = Nil
//finally, remove and destroy the NSView that contained the top and bottom labels
TitlebarView.removeFromSuperview()
TitlebarView = Nil
End If
End Sub
Function TitlebarText() As String
//return the currently set text
Return PrivateTitlebarText
End Function
Sub TitlebarText(ShouldFadeIn as Boolean = True, Assigns Text as String)
//remove any existing text
RemoveTitlebarText()
If not (Text = "") Then
//store the passed Text
PrivateTitlebarText = Text
//if no color has been set, use the default text color
If (PrivateTitlebarTextNSColor = Nil) Then
TitlebarTextColor = RGB(55, 55, 55)
End If
//calculate the width of the label
Dim NSGraphics as New NSGraphicsMBS()
Dim NSFont as NSFontMBS = NSFontMBS.fontWithName("Helvetica", 11.0)
Dim NSAttributes as New Dictionary(NSAttributedStringMBS.NSFontAttributeName : NSFont)
Dim MaximumLabelWidth as Double = Self.Width - 62.0
Dim LabelWidth as Double = NSGraphics.sizeWithAttributes(Text, NSAttributes).Width + 10.0
//check if the width of the label exceeds the maximum width, to prevent the window's red, yellow and green buttons from malfunctioning
If (LabelWidth > MaximumLabelWidth) Then
LabelWidth = MaximumLabelWidth
End If
//create the two subviews with the text inside of them
BottomLabel = CreateNewLabel(False, Text, LabelWidth, NSFont)
TopLabel = CreateNewLabel(True, Text, LabelWidth, NSFont)
//adjust the X-coordinate according to the value of Self.FullScreenPrimaryMBS
Dim X as Double = 0.0
If (Self.FullScreenPrimaryMBS) Then
X = 18.0
End If
//create a new NSView and add the subviews
TitlebarView = new CustomNSViewMBS((Self.Width - LabelWidth) - X, Self.NSWindowMBS.frame.Height - 21.0, LabelWidth, 15.0)
//show or hide the text depending on the current situation
If not (IsLion) Then
TitlebarTextVisible = True
Else
If (Self.IsFullScreenMBS) Then
TitlebarTextVisible = False
Else
TitlebarTextVisible = True
End If
End If
TitlebarView.addSubview(BottomLabel)
TitlebarView.addSubview(TopLabel)
//make the text invisible, only if the text should fade in
If (ShouldFadeIn) Then
TitlebarView.alphaValue = 0.0
End If
//set the autoresizingMask to make sure that the text stays in the top-right corner of the titlebar
TitlebarView.autoresizingMask = NSViewMBS.NSViewMinXMargin + NSViewMBS.NSViewMinYMargin
//add the NSView with the two subviews to the titlebar
Self.NSWindowMBS.contentView.superview.addSubview(TitlebarView)
//make the text visible by fading it in, only if the text should fade in of course
If (ShouldFadeIn) Then
Dim TitlebarViewAnimator as NSViewMBS = TitlebarView.animator()
If not (TitlebarViewAnimator = Nil) Then
TitlebarViewAnimator.alphaValue = 1.0
Else
TitlebarView.alphaValue = 1.0
End If
End If
//add a handler to handle mouse clicks
AddHandler TitlebarView.mouseUp, AddressOf HandleMouseUp
//if on Lion and if the Window is fullscreen-enabled, add handlers to handle fullscreen events
If (IsLion and Self.FullScreenPrimaryMBS) Then
FullScreenEventMirrorObject = new FullScreenEventMirror(Self.NSWindowMBS)
If not (FullScreenEventMirrorObject = Nil) Then
AddHandler FullScreenEventMirrorObject.WillEnterFullScreen, AddressOf HandleEnterFullScreen
AddHandler FullScreenEventMirrorObject.DidFailToEnterFullScreen, AddressOf HandleFailedToEnterFullScreen
AddHandler FullScreenEventMirrorObject.WillExitFullScreen, AddressOf HandleExitFullScreen
AddHandler FullScreenEventMirrorObject.DidFailToExitFullScreen, AddressOf HandleFailedToExitFullScreen
End If
End If
End If
End Sub
Property Private BottomLabel As NSTextViewMBS
Property Private FullScreenEventMirrorObject As FullScreenEventMirror
Property Private IsLeopard As Boolean
Property Private IsLion As Boolean
Property Private IsSnowLeopard As Boolean
Property Private PrivateTitlebarText As String
Property Private PrivateTitlebarTextColor As Color
Property Private PrivateTitlebarTextNSColor As NSColorMBS
Property Private PrivateTitlebarTextVisible As Boolean = True
Property Private TitlebarView As CustomNSViewMBS
Property Private TopLabel As NSTextViewMBS
End Class
Class FullScreenEventMirror Inherits NSWindowDelegateMBS
Event DidFailToEnterFullScreen()
End Event
Event DidFailToExitFullScreen()
End Event
Event WillEnterFullScreen()
End Event
Event WillExitFullScreen()
End Event
EventHandler Sub windowDidFailToEnterFullScreen(win as NSWindowMBS)
//raise the DidFailToEnterFullScreen event and handle it inside the ExtendedWindow class
RaiseEvent DidFailToEnterFullScreen
End EventHandler
EventHandler Sub windowDidFailToExitFullScreen(win as NSWindowMBS)
//raise the DidFailToExitFullScreen event and handle it inside the ExtendedWindow class
RaiseEvent DidFailToExitFullScreen
End EventHandler
EventHandler Sub windowWillEnterFullScreen(notification as NSNotificationMBS)
//raise the WillEnterFullScreen event and handle it inside the ExtendedWindow class
RaiseEvent WillEnterFullScreen
End EventHandler
EventHandler Sub windowWillExitFullScreen(notification as NSNotificationMBS)
//raise the WillExitFullScreen event and handle it inside the ExtendedWindow class
RaiseEvent WillExitFullScreen
End EventHandler
End Class
End Project
See also:
The items on this page are in the following plugins: MBS MacCocoa Plugin.