Platforms to show: All Mac Windows Linux Cross-Platform
/MacControls/TextView advanced
Required plugins for this example: MBS MacBase Plugin, MBS Main Plugin, MBS MacCocoa Plugin, MBS MacControls Plugin, MBS MacFrameworks Plugin
Last modified Sun, 14th Apr 2012.
You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /MacControls/TextView advanced
Download this example: TextView advanced.zip
Project "TextView advanced.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control CocoaControlMBS1 Inherits CocoaControlMBS
ControlInstance CocoaControlMBS1 Inherits CocoaControlMBS
EventHandler Function GetView() As NSViewMBS
scrollview=new NSScrollViewMBS(0,0,me.Width,me.Height)
textview=new NSTextViewMBS(0,0,me.Width,me.Height)
textview.ContinuousSpellCheckingEnabled=true
textview.allowsUndo=true
// tell it how the resize is going
textview.autoresizingMask=textview.NSViewHeightSizable+textview.NSViewWidthSizable
scrollview.documentView=textview
scrollview.hasVerticalScroller=true
scrollview.hasHorizontalScroller=True
Return scrollview
End EventHandler
End Control
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action()
dim f as FolderItem = GetOpenFolderItem(FileTypes1.ApplicationRtf)
if f = nil then Return
dim d as Dictionary
dim a as NSAttributedStringMBS = NSAttributedStringMBS.attributedStringWithPath(f, d)
if a<>Nil then
DocumentAttributes = d
textView.textStorage.setAttributedString a
else
MsgBox "Failed to read file."
end if
End EventHandler
End Control
Control PushButton2 Inherits PushButton
ControlInstance PushButton2 Inherits PushButton
EventHandler Sub Action()
dim f as FolderItem = GetOpenFolderItem(FileTypes1.ApplicationMsword)
if f = nil then Return
dim d as Dictionary
dim a as NSAttributedStringMBS = NSAttributedStringMBS.attributedStringWithPath(f, d)
if a<>Nil then
DocumentAttributes = d
textView.textStorage.setAttributedString a
else
MsgBox "Failed to read file."
end if
End EventHandler
End Control
Control PushButton3 Inherits PushButton
ControlInstance PushButton3 Inherits PushButton
EventHandler Sub Action()
dim f as FolderItem = GetSaveFolderItem(FileTypes1.ApplicationRtf, "test.rtf")
if f = nil then Return
dim a as NSAttributedStringMBS = textView.textStorage
dim data as string = a.RTFFromRange(0, a.length, DocumentAttributes)
dim b as BinaryStream = BinaryStream.Create(f, true)
b.Write data
End EventHandler
End Control
Control PushButton4 Inherits PushButton
ControlInstance PushButton4 Inherits PushButton
EventHandler Sub Action()
dim f as FolderItem = GetSaveFolderItem(FileTypes1.ApplicationMsword, "test.doc")
if f = nil then Return
dim a as NSAttributedStringMBS = textView.textStorage
dim data as string = a.docFormatFromRange(0, a.length, DocumentAttributes)
dim b as BinaryStream = BinaryStream.Create(f, true)
b.Write data
End EventHandler
End Control
Control PushButton5 Inherits PushButton
ControlInstance PushButton5 Inherits PushButton
EventHandler Sub Action()
dim f as FolderItem = GetOpenFolderItem(FileTypes1.TextHtml)
if f = nil then Return
dim d as Dictionary
dim a as NSAttributedStringMBS = NSAttributedStringMBS.attributedStringWithPath(f, d)
if a<>Nil then
DocumentAttributes = d
textView.textStorage.setAttributedString a
else
MsgBox "Failed to read file."
end if
End EventHandler
End Control
Control PushButton6 Inherits PushButton
ControlInstance PushButton6 Inherits PushButton
EventHandler Sub Action()
dim f as FolderItem = GetSaveFolderItem(FileTypes1.TextHtml, "test.html")
if f = nil then Return
dim a as NSAttributedStringMBS = textView.textStorage
dim data as string = a.htmlString
dim b as BinaryStream = BinaryStream.Create(f, true)
b.Write data
End EventHandler
End Control
Control PushButton7 Inherits PushButton
ControlInstance PushButton7 Inherits PushButton
EventHandler Sub Action()
dim f as FolderItem = GetOpenFolderItem(FileTypes1.ApplicationRtf)
if f = nil then Return
dim a as NSAttributedStringMBS = NSAttributedStringMBS.attributedStringWithPath(f)
if a<>Nil then
textView.insertText a
else
MsgBox "Failed to read file."
end if
End EventHandler
End Control
Control PushButton8 Inherits PushButton
ControlInstance PushButton8 Inherits PushButton
EventHandler Sub Action()
dim f as FolderItem = GetOpenFolderItem(FileTypes1.ApplicationMsword)
if f = nil then Return
dim a as NSAttributedStringMBS = NSAttributedStringMBS.attributedStringWithPath(f)
if a<>Nil then
textView.insertText a
else
MsgBox "Failed to read file."
end if
End EventHandler
End Control
Control PushButton9 Inherits PushButton
ControlInstance PushButton9 Inherits PushButton
EventHandler Sub Action()
dim f as FolderItem = GetOpenFolderItem(FileTypes1.TextHtml)
if f = nil then Return
dim a as NSAttributedStringMBS = NSAttributedStringMBS.attributedStringWithPath(f)
if a<>Nil then
textView.insertText a
else
MsgBox "Failed to read file."
end if
End EventHandler
End Control
Control PushButton10 Inherits PushButton
ControlInstance PushButton10 Inherits PushButton
EventHandler Sub Action()
dim f as FolderItem = GetOpenFolderItem(FileTypes1.Text)
if f = nil then Return
dim d as Dictionary
dim a as NSAttributedStringMBS = NSAttributedStringMBS.attributedStringWithPath(f, d)
if a<>Nil then
DocumentAttributes = d
textView.textStorage.setAttributedString a
else
MsgBox "Failed to read file."
end if
End EventHandler
End Control
Control PushButton11 Inherits PushButton
ControlInstance PushButton11 Inherits PushButton
EventHandler Sub Action()
dim f as FolderItem = GetSaveFolderItem(FileTypes1.Text, "test.txt")
if f = nil then Return
dim a as NSAttributedStringMBS = textView.textStorage
dim data as string = a.text
dim b as BinaryStream = BinaryStream.Create(f, true)
b.Write data
End EventHandler
End Control
Control PushButton12 Inherits PushButton
ControlInstance PushButton12 Inherits PushButton
EventHandler Sub Action()
dim f as FolderItem = GetOpenFolderItem(FileTypes1.Text)
if f = nil then Return
dim a as NSAttributedStringMBS = NSAttributedStringMBS.attributedStringWithPath(f)
if a<>Nil then
textView.insertText a
else
MsgBox "Failed to read file."
end if
End EventHandler
End Control
EventHandler Sub Open()
if not CocoaControlMBS1.Available then
MsgBox "This example requires Mac OS X 10.5."
quit
end if
End EventHandler
Property DocumentAttributes As Dictionary
Property scrollview As nsscrollViewMBS
Property textView As NSTextViewMBS
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
FileTypes1
Filetype application/msword
Filetype application/rtf
Filetype text/html
Filetype text/plain
End FileTypes1
End Project
See also:
Download this example: TextView advanced.zip
The items on this page are in the following plugins: MBS MacControls Plugin.