SHARE
TWEET

Untitled

a guest Jun 1st, 2017 151 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Enumeration ; SWidget\Kind
  3.   #SWidget_Kind_Window
  4.   #SWidget_Kind_HBox
  5.   #SWidget_Kind_VBox
  6.   #SWidget_Kind_PBGadget
  7. EndEnumeration
  8.  
  9. Structure SRectangle
  10.   Left.i
  11.   Top.i
  12.   Width.i
  13.   Height.i
  14. EndStructure
  15.  
  16. Structure SWidget
  17.   Name.s      ; Name (arbitary)
  18.   Kind.i      ; Widget kind (enumerated)
  19.   Number.i    ; The window/gadget number
  20.   Padding.i   ; The amount of blank space allocated around this widget
  21.   ; Container
  22.   Spacing.i   ; The amount of blank space between the children
  23.   ChildCount.i  ; Number of children
  24.   Array *Children(0)
  25. EndStructure
  26.  
  27. Procedure ApplyPadding(*W.SWidget, *R.SRectangle)
  28.   *R\Left + *W\Padding
  29.   *R\Top + *W\Padding
  30.   *R\Height - *W\Padding*2
  31.   *R\Width - *W\Padding*2
  32. EndProcedure
  33.  
  34. Procedure HBox_GetChildBounds(*W.SWidget, *R.SRectangle, Array Bounds.SRectangle(1))
  35.   Protected I = 0, Max = *W\ChildCount-1
  36.   Protected Dim Bounds(Max)
  37.   Protected TotalSpacing = *W\Spacing * (Max)
  38.   Protected AvailWidth = *R\Width-TotalSpacing
  39.   Protected EqualWidth = AvailWidth/*W\ChildCount
  40.  
  41.   For I = 0 To Max
  42.     *Child.SWidget = *W\Children(I)
  43.     Bounds(I)\Top = *R\Top
  44.     Bounds(I)\Left = *R\Left + EqualWidth*I + *W\Spacing*I
  45.     Bounds(I)\Width = EqualWidth
  46.     Bounds(I)\Height = *R\Height
  47.   Next
  48. EndProcedure
  49.  
  50. Procedure VBox_GetChildBounds(*W.SWidget, *R.SRectangle, Array Bounds.SRectangle(1))
  51.   Protected I = 0, Max = *W\ChildCount-1
  52.   Protected Dim Bounds(Max)
  53.   Protected TotalSpacing = *W\Spacing * (Max)
  54.   Protected AvailHeight = *R\Height-TotalSpacing
  55.   Protected EqualHeight = AvailHeight/*W\ChildCount
  56.  
  57.   For I = 0 To Max
  58.     *Child.SWidget = *W\Children(I)
  59.     Bounds(I)\Top = *R\Top + EqualHeight*I + *W\Spacing*I
  60.     Bounds(I)\Left = *R\Left
  61.     Bounds(I)\Width = *R\Width
  62.     Bounds(I)\Height = EqualHeight
  63.   Next
  64. EndProcedure
  65.  
  66. Procedure OnResize(*W.SWidget, *R.SRectangle)
  67.  
  68.   Protected Dim Bounds.SRectangle(0)
  69.   ApplyPadding(*W, *R)
  70.  
  71.   Select *W\Kind
  72.     Case #SWidget_Kind_PBGadget
  73.       ResizeGadget(*W\Number, *R\Left, *R\Top, *R\Width, *R\Height)
  74.       ProcedureReturn 0
  75.     Case #SWidget_Kind_Window, #SWidget_Kind_HBox
  76.       If *W\ChildCount
  77.         HBox_GetChildBounds(*W, *R, Bounds())
  78.       EndIf
  79.     Case #SWidget_Kind_VBox
  80.       If *W\ChildCount
  81.         VBox_GetChildBounds(*W, *R, Bounds())
  82.       EndIf
  83.   EndSelect
  84.  
  85.   ; Container widget size children
  86.   If *W\ChildCount
  87.     For I = 0 To *W\ChildCount-1
  88.       OnResize(*W\Children(I), Bounds(I))
  89.     Next
  90.   EndIf
  91.  
  92. EndProcedure
  93.  
  94. MainWnd.SWidget
  95. MainWnd\Name = "MainWnd"
  96. MainWnd\Kind = #SWidget_Kind_Window
  97. MainWnd\Number = OpenWindow(#PB_Any, 50, 50, 512, 384, "Hello world", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget)
  98. MainWnd\Padding = 10
  99. MainWnd\Spacing = 5
  100. ;SetWindowColor(MainWnd\Number, #Red)
  101.  
  102. Procedure NewButton(Text.s)
  103.   *G.SWidget = AllocateMemory(SizeOf(SWidget))
  104.   InitializeStructure(*G, SWidget)
  105.   *G\Kind = #SWidget_Kind_PBGadget
  106.   *G\Number = ButtonGadget(#PB_Any, 0, 0, 0, 0, Text)
  107.   ProcedureReturn *G
  108. EndProcedure
  109.  
  110.  
  111.  
  112. V.SWidget
  113. V\Kind = #SWidget_Kind_VBox
  114.  
  115. Dim MainWnd\Children(1)
  116. MainWnd\Children(0) = V
  117. MainWnd\Children(1) = NewButton("Hello world.")
  118. MainWnd\ChildCount = 2
  119.  
  120. Dim V\Children(3)
  121. V\ChildCount = 4
  122. V\Spacing = 2
  123. V\Children(0) = NewButton("First!")
  124. *G.SWidget = NewButton("Second!")
  125. *G\Padding = 15
  126. V\Children(1) = *G
  127. V\Children(2) = NewButton("Minute!")
  128. V\Children(3) = NewButton("Luuuuna!")
  129.  
  130. Repeat
  131.   Select WaitWindowEvent()
  132.     Case #PB_Event_SizeWindow
  133.       ClearStructure(@W.SRectangle, SRectangle)
  134.       W\Height = WindowHeight(MainWnd\Number)
  135.       W\Width  = WindowWidth(MainWnd\Number)
  136.       OnResize(MainWnd, W)
  137.     Case #PB_Event_CloseWindow
  138.       Break
  139.   EndSelect
  140. ForEver
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top