Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Enumeration ; SWidget\Kind
- #SWidget_Kind_Window
- #SWidget_Kind_HBox
- #SWidget_Kind_VBox
- #SWidget_Kind_PBGadget
- EndEnumeration
- Structure SRectangle
- Left.i
- Top.i
- Width.i
- Height.i
- EndStructure
- Structure SWidget
- Name.s ; Name (arbitary)
- Kind.i ; Widget kind (enumerated)
- Number.i ; The window/gadget number
- Padding.i ; The amount of blank space allocated around this widget
- ; Container
- Spacing.i ; The amount of blank space between the children
- ChildCount.i ; Number of children
- Array *Children(0)
- EndStructure
- Procedure ApplyPadding(*W.SWidget, *R.SRectangle)
- *R\Left + *W\Padding
- *R\Top + *W\Padding
- *R\Height - *W\Padding*2
- *R\Width - *W\Padding*2
- EndProcedure
- Procedure HBox_GetChildBounds(*W.SWidget, *R.SRectangle, Array Bounds.SRectangle(1))
- Protected I = 0, Max = *W\ChildCount-1
- Protected Dim Bounds(Max)
- Protected TotalSpacing = *W\Spacing * (Max)
- Protected AvailWidth = *R\Width-TotalSpacing
- Protected EqualWidth = AvailWidth/*W\ChildCount
- For I = 0 To Max
- *Child.SWidget = *W\Children(I)
- Bounds(I)\Top = *R\Top
- Bounds(I)\Left = *R\Left + EqualWidth*I + *W\Spacing*I
- Bounds(I)\Width = EqualWidth
- Bounds(I)\Height = *R\Height
- Next
- EndProcedure
- Procedure VBox_GetChildBounds(*W.SWidget, *R.SRectangle, Array Bounds.SRectangle(1))
- Protected I = 0, Max = *W\ChildCount-1
- Protected Dim Bounds(Max)
- Protected TotalSpacing = *W\Spacing * (Max)
- Protected AvailHeight = *R\Height-TotalSpacing
- Protected EqualHeight = AvailHeight/*W\ChildCount
- For I = 0 To Max
- *Child.SWidget = *W\Children(I)
- Bounds(I)\Top = *R\Top + EqualHeight*I + *W\Spacing*I
- Bounds(I)\Left = *R\Left
- Bounds(I)\Width = *R\Width
- Bounds(I)\Height = EqualHeight
- Next
- EndProcedure
- Procedure OnResize(*W.SWidget, *R.SRectangle)
- Protected Dim Bounds.SRectangle(0)
- ApplyPadding(*W, *R)
- Select *W\Kind
- Case #SWidget_Kind_PBGadget
- ResizeGadget(*W\Number, *R\Left, *R\Top, *R\Width, *R\Height)
- ProcedureReturn 0
- Case #SWidget_Kind_Window, #SWidget_Kind_HBox
- If *W\ChildCount
- HBox_GetChildBounds(*W, *R, Bounds())
- EndIf
- Case #SWidget_Kind_VBox
- If *W\ChildCount
- VBox_GetChildBounds(*W, *R, Bounds())
- EndIf
- EndSelect
- ; Container widget size children
- If *W\ChildCount
- For I = 0 To *W\ChildCount-1
- OnResize(*W\Children(I), Bounds(I))
- Next
- EndIf
- EndProcedure
- MainWnd.SWidget
- MainWnd\Name = "MainWnd"
- MainWnd\Kind = #SWidget_Kind_Window
- MainWnd\Number = OpenWindow(#PB_Any, 50, 50, 512, 384, "Hello world", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget)
- MainWnd\Padding = 10
- MainWnd\Spacing = 5
- ;SetWindowColor(MainWnd\Number, #Red)
- Procedure NewButton(Text.s)
- *G.SWidget = AllocateMemory(SizeOf(SWidget))
- InitializeStructure(*G, SWidget)
- *G\Kind = #SWidget_Kind_PBGadget
- *G\Number = ButtonGadget(#PB_Any, 0, 0, 0, 0, Text)
- ProcedureReturn *G
- EndProcedure
- V.SWidget
- V\Kind = #SWidget_Kind_VBox
- Dim MainWnd\Children(1)
- MainWnd\Children(0) = V
- MainWnd\Children(1) = NewButton("Hello world.")
- MainWnd\ChildCount = 2
- Dim V\Children(3)
- V\ChildCount = 4
- V\Spacing = 2
- V\Children(0) = NewButton("First!")
- *G.SWidget = NewButton("Second!")
- *G\Padding = 15
- V\Children(1) = *G
- V\Children(2) = NewButton("Minute!")
- V\Children(3) = NewButton("Luuuuna!")
- Repeat
- Select WaitWindowEvent()
- Case #PB_Event_SizeWindow
- ClearStructure(@W.SRectangle, SRectangle)
- W\Height = WindowHeight(MainWnd\Number)
- W\Width = WindowWidth(MainWnd\Number)
- OnResize(MainWnd, W)
- Case #PB_Event_CloseWindow
- Break
- EndSelect
- ForEver
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement