Platforms to show: All Mac Windows Linux Cross-Platform
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/SelTextBackColor
This example is the version from Sun, 14th Mar 2020.
Project "SelTextBackColor.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
Control TextArea1 Inherits TextArea
ControlInstance TextArea1 Inherits TextArea
EventHandler Sub SelChange()
// avoid reentry
If insideEvent Then Return
insideEvent = True
// change buttons to match selection
#If TargetWindows Then
If TextArea1.WinSelHasTextBackColorMBS Then
Dim c As Color = TextArea1.WinSelTextBackColorMBS
BackgroundColorButton(0).Value = (c = &cFF0000)
BackgroundColorButton(1).Value = (c = &c00FF00)
BackgroundColorButton(2).Value = (c = &c0000FF)
BackgroundColorButton(3).Value = False
Else
BackgroundColorButton(0).Value = False
BackgroundColorButton(1).Value = False
BackgroundColorButton(2).Value = False
BackgroundColorButton(3).Value = True
End If
If TextArea1.WinSelHasTextColorMBS Then
Dim c As Color = TextArea1.WinSelTextColorMBS
ForegroundColorButton(0).Value = (c = &cFF0000)
ForegroundColorButton(1).Value = (c = &c00FF00)
ForegroundColorButton(2).Value = (c = &c0000FF)
ForegroundColorButton(3).Value = False
Else
ForegroundColorButton(0).Value = False
ForegroundColorButton(1).Value = False
ForegroundColorButton(2).Value = False
ForegroundColorButton(3).Value = True
End If
#ElseIf TargetMacOS Then
Dim textView As NSTextViewMBS = TextArea1.NSTextViewMBS
Dim textStorage As NSTextStorageMBS = textView.textStorage
Dim selectedRange As NSRangeMBS = textView.selectedRange
If selectedRange.Location < textStorage.Length Then
Dim d As Dictionary = textStorage.attributesAtIndex(selectedRange.Location)
If d.HasKey(NSAttributedStringMBS.NSForegroundColorAttributeName) Then
Dim n As NSColorMBS = d.Value(NSAttributedStringMBS.NSForegroundColorAttributeName)
Dim c As Color = n.colorValue
ForegroundColorButton(0).Value = (c = &cFF0000)
ForegroundColorButton(1).Value = (c = &c00FF00)
ForegroundColorButton(2).Value = (c = &c0000FF)
ForegroundColorButton(3).Value = False
Else
ForegroundColorButton(0).Value = False
ForegroundColorButton(1).Value = False
ForegroundColorButton(2).Value = False
ForegroundColorButton(3).Value = True
end if
If d.HasKey(NSAttributedStringMBS.NSBackgroundColorAttributeName) Then
Dim n As NSColorMBS = d.Value(NSAttributedStringMBS.NSBackgroundColorAttributeName)
Dim c As Color = n.colorValue
BackgroundColorButton(0).Value = (c = &cFF0000)
BackgroundColorButton(1).Value = (c = &c00FF00)
BackgroundColorButton(2).Value = (c = &c0000FF)
BackgroundColorButton(3).Value = False
Else
BackgroundColorButton(0).Value = False
BackgroundColorButton(1).Value = False
BackgroundColorButton(2).Value = False
BackgroundColorButton(3).Value = True
End If
Else
// out of range
End If
#Else
Break
#EndIf
insideEvent = False
End EventHandler
End Control
Control ForegroundColorButton Inherits BevelButton
ControlInstance ForegroundColorButton(0) Inherits BevelButton
ControlInstance ForegroundColorButton(1) Inherits BevelButton
ControlInstance ForegroundColorButton(2) Inherits BevelButton
ControlInstance ForegroundColorButton(3) Inherits BevelButton
EventHandler Sub Action(index as Integer)
// avoid reentry
If insideEvent Then Return
insideEvent = True
// clear other buttons
For i As Integer = 0 To 3
If index <> i Then
ForegroundColorButton(i).Value = False
End If
Next
// now react for this button
Select Case index
Case 0
SetForegroundColor &cFF0000
Case 1
SetForegroundColor &c00FF00
Case 2
SetForegroundColor &c0000FF
Case 3
ClearForegroundColor
End Select
insideEvent = False
End EventHandler
End Control
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control Label2 Inherits Label
ControlInstance Label2 Inherits Label
End Control
Control BackgroundColorButton Inherits BevelButton
ControlInstance BackgroundColorButton(0) Inherits BevelButton
ControlInstance BackgroundColorButton(1) Inherits BevelButton
ControlInstance BackgroundColorButton(2) Inherits BevelButton
ControlInstance BackgroundColorButton(3) Inherits BevelButton
EventHandler Sub Action(index as Integer)
// avoid reentry
If insideEvent Then Return
insideEvent = True
// clear other buttons
For i As Integer = 0 To 3
If index <> i Then
BackgroundColorButton(i).Value = False
End If
Next
// now react for this button
Select Case index
Case 0
SetBackgroundColor &cFF0000
Case 1
SetBackgroundColor &c00FF00
Case 2
SetBackgroundColor &c0000FF
Case 3
ClearBackgroundColor
End Select
insideEvent = False
End EventHandler
End Control
Sub ClearBackgroundColor()
#If TargetWindows Then
TextArea1.WinSelHasTextBackColorMBS = False
#ElseIf TargetMacOS Then
Dim t As NSTextViewMBS = TextArea1.NSTextViewMBS
// change selection
dim r as NSRangeMBS = t.selectedRange
t.textStorage.removeAttribute NSAttributedStringMBS.NSBackgroundColorAttributeName, r
// and what you type
dim d as Dictionary = t.typingAttributes
If d.HasKey(NSAttributedStringMBS.NSBackgroundColorAttributeName) Then
d.Remove(NSAttributedStringMBS.NSBackgroundColorAttributeName)
t.typingAttributes = d
End If
#Else
Break
#EndIf
End Sub
Sub ClearForegroundColor()
#If TargetWindows Then
TextArea1.WinSelHasTextColorMBS = False
#ElseIf TargetMacOS Then
Dim t As NSTextViewMBS = TextArea1.NSTextViewMBS
// change selection
Dim r As NSRangeMBS = t.selectedRange
t.textStorage.removeAttribute NSAttributedStringMBS.NSForegroundColorAttributeName, r
// and what you type
Dim d As Dictionary = t.typingAttributes
If d.HasKey(NSAttributedStringMBS.NSForegroundColorAttributeName) Then
d.Remove(NSAttributedStringMBS.NSForegroundColorAttributeName)
t.typingAttributes = d
end if
#Else
Break
#EndIf
End Sub
Sub SetBackgroundColor(c as color)
#If TargetWindows Then
TextArea1.WinSelTextBackColorMBS = c
#ElseIf TargetMacOS Then
Dim n As NSColorMBS = New NSColorMBS(c)
Dim t As NSTextViewMBS = TextArea1.NSTextViewMBS
// change selection
Dim r As NSRangeMBS = t.selectedRange
t.textStorage.addAttribute NSAttributedStringMBS.NSBackgroundColorAttributeName, n, r
// and what you type
dim d as Dictionary = t.typingAttributes
d.Value(NSAttributedStringMBS.NSBackgroundColorAttributeName) = n
t.typingAttributes = d
#Else
Break
#EndIf
End Sub
Sub SetForegroundColor(c as color)
#If TargetWindows Then
TextArea1.WinSelTextColorMBS = c
#ElseIf TargetMacOS Then
Dim n As NSColorMBS = New NSColorMBS(c)
Dim t As NSTextViewMBS = TextArea1.NSTextViewMBS
// change selection
Dim r As NSRangeMBS = t.selectedRange
t.textStorage.addAttribute NSAttributedStringMBS.NSForegroundColorAttributeName, n, r
// and what you type
dim d as Dictionary = t.typingAttributes
d.Value(NSAttributedStringMBS.NSForegroundColorAttributeName) = n
t.typingAttributes = d
#Else
Break
#EndIf
End Sub
Property dummy As NSParagraphStyleMBS
// force Xojo to include this plugin part
Property insideEvent As Boolean
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
End Project
The items on this page are in the following plugins: MBS MacCocoa Plugin.