Platforms to show: All Mac Windows Linux Cross-Platform
/MacControls/ContainerControl List
Required plugins for this example: MBS MacCocoa Plugin, MBS MacBase Plugin, MBS Main Plugin, MBS MacControls Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacControls/ContainerControl List
This example is the version from Thu, 25th Oct 2023.
Project "ContainerControl List.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
Control TestControl Inherits OuterContainerControl
ControlInstance TestControl Inherits OuterContainerControl
EventHandler Sub Open()
For i As Integer = 1 To 100
Dim it As New ItemContainerControl
it.NameLabel.Text = "Item "+Str(i)
me.AddItem it
Next
End EventHandler
End Control
Control CountButton Inherits PushButton
ControlInstance CountButton Inherits PushButton
EventHandler Sub Action()
Dim n As Integer = 0
For Each v As Variant In TestControl.innerContainer.items
Dim c As ItemContainerControl = v
If c.CheckBox1.Value Then
n = n + 1
End If
Next
MessageBox Str(n)+" checkboxes on."
End EventHandler
End Control
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
Class OuterContainerControl Inherits ContainerControl
Control VScrollBar Inherits ScrollBar
ControlInstance VScrollBar Inherits ScrollBar
EventHandler Sub ValueChanged()
innerContainer.top = - me.Value
End EventHandler
End Control
Event Open()
End Event
Event Resized()
End Event
EventHandler Sub Open()
// setup inner container holding the items
innerContainer = New InnerContainerControl
innerContainer.LockLeft = True
innerContainer.LockRight = True
innerContainer.LockTop = True
innerContainer.LockBottom = False // This control will be heigher if needed
innerContainer.EmbedWithin(Self, 0, 0, Me.Width - VScrollBar.width, Me.Height)
#If TargetMacOS Then
// this only works on macOS
Dim outerView As NSViewMBS = Self.NSViewMBS
// now build a scrollview to put between outer and inner containers
Dim frame As NSRectMBS = outerView.frame
Dim ScrollView As New NSScrollViewMBS(0, 0, frame.Width, frame.Height)
ScrollView.hasVerticalScroller = True
ScrollView.hasHorizontalScroller = True
ScrollView.autoresizingMask = NSViewMBS.NSViewWidthSizable + NSViewMBS.NSViewHeightSizable
ScrollView.autoresizesSubviews = True
ScrollView.borderType = NSViewMBS.NSBezelBorder
ScrollView.pageScroll = frame.Height - 20
ScrollView.autohidesScrollers = False
ScrollView.verticalScroller.scrollerStyle = NSScrollerMBS.NSScrollerStyleLegacy
// optionally disable elastic scrolling
'ScrollView.verticalScrollElasticity = ScrollView.NSScrollElasticityNone
Dim innerView As NSViewMBS = innerContainer.NSViewMBS
innerView.removeFromSuperview
ScrollView.documentView = innerView
outerView.addSubview ScrollView
// keep reference
Self.ScrollView = ScrollView
#Else
// Windows, Linux or macOS with Scrollbar
innerContainer.VScrollBar = VScrollBar
VScrollBar.MinimumValue = 0
VScrollBar.MaximumValue = 0
VScrollBar.Value = 0
VScrollBar.Visible = True
VScrollBar.Enabled = True
#EndIf
RaiseEvent Open
End EventHandler
EventHandler Sub Resized()
#If TargetMacOS Then
If ScrollView <> Nil Then // may be called early
Dim v As NSScrollViewMBS = ScrollView
// adjust it
v.pageScroll = Me.Height - 20
End If
#Else
If VScrollBar <> Nil Then // may be called early
// adjust it
VScrollBar.PageStep = Me.Height - 20
if innerContainer <> nil then
VScrollBar.MaximumValue = innerContainer.height - Me.Height
End If
End If
#EndIf
If innerContainer <> Nil Then
innerContainer.Resize Me.Height
End If
RaiseEvent Resized
End EventHandler
EventHandler Sub Resizing()
If innerContainer <> Nil Then
innerContainer.Resize Me.Height
End If
End EventHandler
Sub AddItem(c as ContainerControl)
InnerContainer.AddItem c
End Sub
Sub UpdateScrollbar(NewHeight as integer)
#If TargetMacOS Then
// nothing to do
#else
If VScrollBar <> Nil Then
VScrollBar.MaximumValue = NewHeight - Me.Height
End If
#EndIf
End Sub
Property Private ScrollView As Variant
Property innerContainer As InnerContainerControl
End Class
Class InnerContainerControl Inherits ContainerControl
ComputedProperty ItemCount As Integer
Sub Get()
Return items.Ubound + 1
End Get
End ComputedProperty
EventHandler Function KeyDown(Key As String) As Boolean
#If TargetMacOS
// automatic!
#Else
// this one is only called when no text field has focus!
Select Case Asc(key)
Case 11
// scroll down on page down
VScrollBar.Value = VScrollBar.Value + VScrollBar.PageStep
Case 12
// scroll down on page down
VScrollBar.Value = VScrollBar.Value + VScrollBar.PageStep
Else
'Self.TrueWindow.Title = Str(Asc(key))
End Select
#EndIf
End EventHandler
EventHandler Function MouseWheel(X As Integer, Y As Integer, DeltaX as Integer, DeltaY as Integer) As Boolean
#If TargetMacOS Then
// nsscrollview
#else
Dim VScrollBar As ScrollBar = OuterContainerControl(Self.Window).VScrollBar
VScrollBar.Value = VScrollBar.value + DeltaY * 3 // factor 3 to make it a bit faster
#EndIf
End EventHandler
Sub AddItem(c as ContainerControl)
c.EmbedWithin(Self, 0, InsertOffset, Self.Width, c.Height)
items.Append c
c.TabIndex = items.Ubound
InsertOffset = InsertOffset + c.Height
// resize container to fit new one
Self.Height = Max(Self.Height, InsertOffset)
OuterContainerControl(Self.Window).UpdateScrollbar Self.Height
End Sub
Sub Resize(NewHeight as integer)
If Me.Height <> NewHeight Then
// adjust to changed size of parent
If NewHeight < InsertOffset Then
Me.Height = InsertOffset
Else
Me.Height = NewHeight
End If
End If
End Sub
Property InsertOffset As Integer
Property VScrollBar As ScrollBar
Property items() As ContainerControl
End Class
Class ItemContainerControl Inherits ContainerControl
Control SomeTextField Inherits TextField
ControlInstance SomeTextField Inherits TextField
End Control
Control NameLabel Inherits Label
ControlInstance NameLabel Inherits Label
End Control
Control CheckBox1 Inherits CheckBox
ControlInstance CheckBox1 Inherits CheckBox
End Control
Control SomeColor Inherits Canvas
ControlInstance SomeColor Inherits Canvas
EventHandler Function MouseDown(X As Integer, Y As Integer) As Boolean
Self.TrueWindow.Title = str(x)+"/"+str(y)
End EventHandler
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect)
Dim c As Color = RGB(rnd*255, rnd*255, rnd*255)
g.foreColor = c
g.FillRect 0, 0, g.Width, g.Height
End EventHandler
End Control
End Class
End Project
The items on this page are in the following plugins: MBS MacControls Plugin.