Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS MacControls Plugin, MBS MacBase Plugin, MBS MacCocoa Plugin, MBS MacOSX Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCocoa/NSSavePanel
This example is the version from Wed, 26th Feb 2019.
Project "NSSavePanel.xojo_binary_project"
FileTypes
Filetype text
End FileTypes
Class Window1 Inherits Window
Control GroupBox1 Inherits GroupBox
ControlInstance GroupBox1 Inherits GroupBox
End Control
Control CheckallowsOtherFileTypes Inherits CheckBox
ControlInstance CheckallowsOtherFileTypes Inherits CheckBox
End Control
Control CheckcanCreateDirectories Inherits CheckBox
ControlInstance CheckcanCreateDirectories Inherits CheckBox
End Control
Control CheckcanSelectHiddenExtension Inherits CheckBox
ControlInstance CheckcanSelectHiddenExtension Inherits CheckBox
End Control
Control CheckBox1 Inherits CheckBox
ControlInstance CheckBox1 Inherits CheckBox
End Control
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Control iNameFieldLabel Inherits TextField
ControlInstance iNameFieldLabel Inherits TextField
End Control
Control StaticText2 Inherits Label
ControlInstance StaticText2 Inherits Label
End Control
Control iPrompt Inherits TextField
ControlInstance iPrompt Inherits TextField
End Control
Control StaticText3 Inherits Label
ControlInstance StaticText3 Inherits Label
End Control
Control iTitle Inherits TextField
ControlInstance iTitle Inherits TextField
End Control
Control ChecktreatsFilePackagesAsDirectories Inherits CheckBox
ControlInstance ChecktreatsFilePackagesAsDirectories Inherits CheckBox
End Control
Control CheckallowsMultipleSelection Inherits CheckBox
ControlInstance CheckallowsMultipleSelection Inherits CheckBox
End Control
Control CheckcanChooseDirectories Inherits CheckBox
ControlInstance CheckcanChooseDirectories Inherits CheckBox
End Control
Control CheckcanChooseFiles Inherits CheckBox
ControlInstance CheckcanChooseFiles Inherits CheckBox
End Control
Control StaticText4 Inherits Label
ControlInstance StaticText4 Inherits Label
End Control
Control iMessage Inherits TextField
ControlInstance iMessage Inherits TextField
End Control
Control CheckresolvesAliases Inherits CheckBox
ControlInstance CheckresolvesAliases Inherits CheckBox
End Control
Control CheckCustomControl Inherits CheckBox
ControlInstance CheckCustomControl Inherits CheckBox
End Control
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action()
dim p as MyNSOpenPanelMBS
dim f as FolderItem
dim types(-1) as string
dim i,c as integer
ListBox1.DeleteAllRows
p=new MyNSOpenPanelMBS
panel=p
p.allowsMultipleSelection=CheckallowsMultipleSelection.Value
p.allowsOtherFileTypes=CheckallowsOtherFileTypes.Value
p.canChooseDirectories=CheckcanChooseDirectories.Value
p.canChooseFiles=checkcanChooseFiles.Value
p.canCreateDirectories=checkcanCreateDirectories.Value
p.canSelectHiddenExtension=CheckcanSelectHiddenExtension.Value
p.NameFieldLabel=iNameFieldLabel.text
p.Prompt=iPrompt.text
p.Title=iTitle.text
p.Message=iMessage.text
p.treatsFilePackagesAsDirectories=ChecktreatsFilePackagesAsDirectories.Value
p.resolvesAliases=checkresolvesAliases.value
types.Append "txt"
types.Append "rtf"
types.Append "jpg"
p.setAllowedFileTypes types
if CheckCustomControl.value then
p.accessoryView=customview
end if
dim n as integer = p.runModalForDirectory(SpecialFolder.Desktop, "test.mov", "mov")
if n=p.NSOKButton then
ListBox1.AddRow "Result: OK"
f=p.File
if f<>nil then
ListBox1.AddRow "File: "+f.UnixpathMBS
else
ListBox1.AddRow "File: ?"
end if
f=p.Directory
if f<>nil then
ListBox1.AddRow "Directory: "+f.UnixpathMBS
else
ListBox1.AddRow "Directory: ?"
end if
c=p.FilesCount
window1.ListBox1.AddRow "File count: "+str(c)
c=c-1
for i=0 to c
f=p.Files(i)
if f<>nil then
window1.ListBox1.AddRow "File("+str(i)+"): "+f.UnixpathMBS
else
window1.ListBox1.AddRow "File("+str(i)+"): ?"
end if
next
elseif n=p.NSCancelButton then
ListBox1.AddRow "Result: Cancel"
else
ListBox1.AddRow "Result: "+str(n)
end if
End EventHandler
End Control
Control ListBox1 Inherits ListBox
ControlInstance ListBox1 Inherits ListBox
End Control
Control PushButton2 Inherits PushButton
ControlInstance PushButton2 Inherits PushButton
EventHandler Sub Action()
dim p as MyNSSavePanelMBS
dim f as FolderItem
dim types(-1) as string
ListBox1.DeleteAllRows
p=new MyNSSavePanelMBS
panel=p
p.allowsOtherFileTypes=CheckallowsOtherFileTypes.Value
p.canCreateDirectories=checkcanCreateDirectories.Value
p.canSelectHiddenExtension=CheckcanSelectHiddenExtension.Value
p.NameFieldLabel=iNameFieldLabel.text
p.Prompt=iPrompt.text
p.Title=iTitle.text
p.Message=iMessage.text
p.treatsFilePackagesAsDirectories=ChecktreatsFilePackagesAsDirectories.Value
types.Append "txt"
types.Append "rtf"
types.Append "jpg"
p.setAllowedFileTypes types
if CheckCustomControl.value then
p.accessoryView=customview
end if
dim n as integer = p.runModalForDirectory(SpecialFolder.Desktop, "test.mov")
if n=p.NSOKButton then
ListBox1.AddRow "Result: OK"
f=p.File
if f<>nil then
ListBox1.AddRow "File: "+f.UnixpathMBS
else
ListBox1.AddRow "File: ?"
end if
f=p.Directory
if f<>nil then
ListBox1.AddRow "Directory: "+f.UnixpathMBS
else
ListBox1.AddRow "Directory: ?"
end if
elseif n=p.NSCancelButton then
ListBox1.AddRow "Result: Cancel"
else
ListBox1.AddRow "Result: "+str(n)
end if
End EventHandler
End Control
Control PushButton3 Inherits PushButton
ControlInstance PushButton3 Inherits PushButton
EventHandler Sub Action()
dim p as MyNSSavePanelMBS
dim types(-1) as string
ListBox1.DeleteAllRows
p=new MyNSSavePanelMBS
panel=p
p.allowsOtherFileTypes=CheckallowsOtherFileTypes.Value
p.canCreateDirectories=checkcanCreateDirectories.Value
p.canSelectHiddenExtension=CheckcanSelectHiddenExtension.Value
p.NameFieldLabel=iNameFieldLabel.text
p.Prompt=iPrompt.text
p.Title=iTitle.text
p.Message=iMessage.text
p.treatsFilePackagesAsDirectories=ChecktreatsFilePackagesAsDirectories.Value
types.Append "txt"
types.Append "rtf"
types.Append "jpg"
p.setAllowedFileTypes types
if CheckCustomControl.value then
p.accessoryView=customview
end if
p.beginSheetForDirectory(SpecialFolder.Desktop, "test.mov",self)
End EventHandler
End Control
Control PushButton4 Inherits PushButton
ControlInstance PushButton4 Inherits PushButton
EventHandler Sub Action()
dim types(-1) as string
ListBox1.DeleteAllRows
dim p as new MyNSOpenPanelMBS
panel = p
p.allowsMultipleSelection=CheckallowsMultipleSelection.Value
p.allowsOtherFileTypes=CheckallowsOtherFileTypes.Value
p.canChooseDirectories=CheckcanChooseDirectories.Value
p.canChooseFiles=checkcanChooseFiles.Value
p.canCreateDirectories=checkcanCreateDirectories.Value
p.canSelectHiddenExtension=CheckcanSelectHiddenExtension.Value
p.NameFieldLabel=iNameFieldLabel.text
p.Prompt=iPrompt.text
p.Title=iTitle.text
p.Message=iMessage.text
p.treatsFilePackagesAsDirectories=ChecktreatsFilePackagesAsDirectories.Value
p.resolvesAliases=checkresolvesAliases.value
types.Append "txt"
types.Append "rtf"
types.Append "jpg"
p.setAllowedFileTypes types
if CheckCustomControl.value then
p.accessoryView=customview
end if
p.beginSheetForDirectory(SpecialFolder.Desktop, "test.mov", types, self)
End EventHandler
End Control
EventHandler Sub Open()
mycustomview=new MyNSView(10,10,100,20)
textview=new NSTextViewMBS(10,40,100,20)
textview.isSelectable=true
textview.isEditable=true
textview.text="Hello World"
checkboxview=new MyNSButton(120,10,70,20)
checkboxview.setButtonType nsbuttonmbs.NSSwitchButton
checkboxview.intValue=1
buttonview=new MyNSButton(120,40,70,20)
buttonview.setButtonType nsbuttonmbs.NSToggleButton
buttonview.bezelStyle=nsbuttonmbs.NSRoundedBezelStyle
customview=new NSViewMBS(0,0,200,70)
customview.addSubview mycustomview
customview.addSubview textview
customview.addSubview buttonview
customview.addSubview checkboxview
End EventHandler
Property Protected buttonview As nsbuttonMBS
Property Protected checkboxview As nsbuttonMBS
Property Protected customview As NSViewMBS
Property Protected mycustomview As mynsview
Property panel As nssavePanelMBS
Property Protected textview As nstextviewMBS
End Class
MenuBar MenuBar1
MenuItem UntitledMenu1 = ""
MenuItem FileMenu = "&File"
MenuItem FileQuit = "Quit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu0 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "Clear"
End MenuBar
Class App Inherits Application
End Class
Class MyNSSavePanelMBS Inherits NSSavePanelMBS
EventHandler Sub directoryDidChange(path as string, folder as folderitem)
window1.ListBox1.AddRow "Changed directory: "+folder.UnixpathMBS
End EventHandler
EventHandler Function isValidFilename(path as string, item as folderitem) As boolean
Window1.ListBox1.AddRow "is valid: "+item.Name
Return true
End EventHandler
EventHandler Sub panelSelectionDidChange()
window1.ListBox1.AddRow "Selection changed"
End EventHandler
EventHandler Sub savePanelDidEnd(ReturnCode as integer)
dim f as FolderItem
window1.ListBox1.AddRow "Save panel did end: "+str(ReturnCode)
if ReturnCode=NSOKButton then
window1.ListBox1.AddRow "Result: OK"
f=File
if f<>nil then
window1.ListBox1.AddRow "File: "+f.UnixpathMBS
else
window1.ListBox1.AddRow "File: ?"
end if
f=Directory
if f<>nil then
window1.ListBox1.AddRow "Directory: "+f.UnixpathMBS
else
window1.ListBox1.AddRow "Directory: ?"
end if
elseif ReturnCode=NSCancelButton then
window1.ListBox1.AddRow "Result: Cancel"
else
window1.ListBox1.AddRow "Result: "+str(ReturnCode)
end if
End EventHandler
EventHandler Function shouldShowFilename(path as string, item as folderitem) As boolean
Window1.ListBox1.AddRow "Should Show Filename: "+item.Name
Return true
End EventHandler
EventHandler Sub willExpand(expanding as boolean)
if expanding then
window1.ListBox1.AddRow "Will expand"
else
window1.ListBox1.AddRow "Will collapse"
end if
End EventHandler
End Class
Class MyNSOpenPanelMBS Inherits NSOpenPanelMBS
EventHandler Sub directoryDidChange(path as string, folder as folderitem)
window1.ListBox1.AddRow "Changed directory: "+folder.UnixpathMBS
End EventHandler
EventHandler Function isValidFilename(path as string, item as folderitem) As boolean
Window1.ListBox1.AddRow "is valid: "+item.Name
Return true
End EventHandler
EventHandler Sub panelSelectionDidChange()
window1.ListBox1.AddRow "Selection changed"
End EventHandler
EventHandler Sub savePanelDidEnd(ReturnCode as integer)
dim f as FolderItem
dim i,c as integer
window1.ListBox1.AddRow "Open panel did end: "+str(ReturnCode)
if ReturnCode=NSOKButton then
window1.ListBox1.AddRow "Result: OK"
f=File
if f<>nil then
window1.ListBox1.AddRow "File: "+f.UnixpathMBS
else
window1.ListBox1.AddRow "File: ?"
end if
f=Directory
if f<>nil then
window1.ListBox1.AddRow "Directory: "+f.UnixpathMBS
else
window1.ListBox1.AddRow "Directory: ?"
end if
c=FilesCount
window1.ListBox1.AddRow "File count: "+str(c)
c=c-1
for i=0 to c
f=Files(i)
if f<>nil then
window1.ListBox1.AddRow "File("+str(i)+"): "+f.UnixpathMBS
else
window1.ListBox1.AddRow "File("+str(i)+"): ?"
end if
next
elseif ReturnCode=NSCancelButton then
window1.ListBox1.AddRow "Result: Cancel"
else
window1.ListBox1.AddRow "Result: "+str(ReturnCode)
end if
End EventHandler
EventHandler Function shouldShowFilename(path as string, item as folderitem) As boolean
Window1.ListBox1.AddRow "Should Show Filename: "+item.name
Return true
End EventHandler
EventHandler Sub willExpand(expanding as boolean)
if expanding then
window1.ListBox1.AddRow "Will expand"
else
window1.ListBox1.AddRow "Will collapse"
end if
End EventHandler
End Class
Class MyNSView Inherits CustomNSViewMBS
EventHandler Sub DrawRect(g as NSGraphicsMBS, left as double, top as double, width as double, height as double)
g.SetColorRGB 0.5,0,0
g.fillRect left,top,width,height
End EventHandler
EventHandler Function mouseDown(e as NSEventMBS, x as double, y as double) As boolean
if window1.panel<>Nil then
window1.panel.Cancel
end if
End EventHandler
End Class
Class MyNSButton Inherits NSButtonMBS
EventHandler Sub Action()
MsgBox "Button pressed"
End EventHandler
End Class
End Project
The items on this page are in the following plugins: MBS MacCocoa Plugin.