Ooops
November 25, 2020, 08:11:51 AM

Author Topic: [bb] Foldable menus by Wiebo [ 1+ years ago ]  (Read 533 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Foldable menus by Wiebo [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : Foldable menus
Author : Wiebo
Posted : 1+ years ago

Description : You know the ones I am talking about, the menus in a sidebar you can fold and unfold. Click on the grey area to fold and unfold. You can even add more menu bars to one window. NOTE: This code when run in debug mode can trash some gadgets. This does not happen with debug off.

Code :
Code: BlitzBasic
  1. ; foldable menus for B+ by Wiebo
  2.  
  3. ; how to use: create a window, add a menu, add panels to menu, add your gadgets to subpanels
  4.  
  5. main = CreateWindow ("Fold me", 0,0, 400, 400)
  6.  
  7. ; window, panel amount, [rightside]
  8.  
  9. menu.collapse = CreateMainMenu ( main, 3, FALSE )
  10.  
  11. ; menu, ysize, index, state, title$, titlestringlength
  12.  
  13. AddPanelToMenu( menu, 200, 1, OPEN, " Menu 1 ", 50 )
  14. AddPanelToMenu( menu, 100, 2, OPEN, " Menu 2 ", 50 )
  15. AddPanelToMenu( menu, 100, 3, OPEN, " Menu 3 ", 50 )
  16.  
  17. a = CreateButton( "Button 1", 25,25,50,25, menusubpanel[1],0)
  18. SetGadgetLayout a, 1,0,1,0
  19.  
  20. a = CreateButton( "Button 2", 25,25,50,25, menusubpanel[2],0)
  21. SetGadgetLayout a, 1,0,1,0
  22.  
  23. a = CreateButton( "Button 3", 25,25,50,25, menusubpanel[3],0)
  24. SetGadgetLayout a, 1,0,1,0
  25.  
  26. RedrawMenu( menu )
  27.  
  28. Repeat
  29.  
  30.         id = WaitEvent()
  31.  
  32.         Select id
  33.                 Case $201       ; mouse down
  34.  
  35.                         Select EventSource()
  36.  
  37.                                 Case menupanel[1]
  38.                                         menustate[1]  = Not menustate[1]
  39.                                         RedrawMenu( menu )
  40.  
  41.                                 Case menupanel[2]
  42.                                         menustate[2]  = Not menustate[2]
  43.                                         RedrawMenu( menu )
  44.  
  45.                                 Case menupanel[3]
  46.                                         menustate[3]  = Not menustate[3]
  47.                                         RedrawMenu( menu )
  48.  
  49.                         End Select
  50.  
  51.                 Case $101
  52.  
  53.                         Select EventData()
  54.  
  55.                                 Case 1
  56.                                         End
  57.  
  58.                         End Select
  59.  
  60.  
  61.         End Select
  62.  
  63. Forever
  64.  
  65. ; -------------------------------------------------------
  66.  
  67. Const OPEN = 0
  68. Const CLOSED = 1
  69. Const MENUWIDTH = 198;214
  70. Const MAXPANELS = 10
  71.  
  72. Type Collapse
  73.         Field MainPanel                         ; main panel to attach subpanels to goes here
  74.         Field MainWidth                         ; width of main panel
  75.         Field MainSlider                        ; slider attached to main panel
  76.         Field Rightside ; true or false. false means panel is drawn at left side of window
  77.         Field MainParent                        ; parent window
  78.         Field PanelAmount                       ; amount of actiual panels in menu
  79.         Field Panel[MAXPANELS]                  ; resizeable panels
  80.         Field SubPanel[MAXPANELS]               ; add your GADGETS to this panel
  81.         Field Label[MAXPANELS]                  ; name to display
  82.         Field State[MAXPANELS]                  ; open or closed
  83.         Field Ysize[MAXPANELS]                  ; size when opened
  84. End Type
  85.  
  86. ; -------------------------------------------
  87.  
  88. Function CreateMainMenu.collapse( window, amount, rightside = TRUE )
  89.  
  90.         If amount > 0 And amount <= MAXPANELS
  91.  
  92.                 ; attach a menu bar to the correct side of a window
  93.  
  94.                 collapse.collapse = New collapse
  95.  
  96.                 collapseMainParent = window
  97.                 collapseMainWidth = MENUWIDTH
  98.                 collapsePanelAmount = amount
  99.                 collapseRightside = rightside
  100.  
  101.                 collapseMainPanel = CreatePanel ( 0, 0, MENUWIDTH+16, 10, window )
  102.  
  103.                 ; determine position
  104.  
  105.                 If rightside = TRUE
  106.  
  107.                         SetGadgetLayout collapseMainPanel, 0,1,1,0
  108.  
  109.                         collapseMainSlider = CreateSlider ( ClientWidth( window )-16, 0, 16, ClientHeight( window ), window, 2 )
  110.                         SetGadgetLayout collapseMainSlider, 0,1,1,1
  111.                 Else
  112.                         SetGadgetLayout collapseMainPanel, 1,0,1,0
  113.  
  114.                         collapseMainSlider = CreateSlider ( 0, 0, 16, ClientHeight( window ), window, 2 )
  115.                         SetGadgetLayout collapseMainSlider, 1,0,1,1
  116.                 EndIf
  117.  
  118. ;               SetPanelColor collapseMainPanel, 255,0,255  ; indication of main panel
  119.  
  120.                 Return collapse
  121.         Else
  122.                 RuntimeError "Use index 1 to " + MAXPANELS + "!"
  123.         EndIf
  124.  
  125. End Function
  126.  
  127.  
  128. Function AddPanelToMenu( c.collapse, height, index, state, name$, length )
  129.  
  130.         If index > 0 And index <= cpanelamount
  131.  
  132.                 cPanel[index] = CreatePanel ( 0, 0, cMainWidth, 10, cMainPanel, 1 )
  133.                 SetGadgetLayout cPanel[index], 1,1,1,1
  134.                 SetPanelColor cpanel[index], 190,190,190        ; click on this panel to open or close menu
  135.  
  136.                 cSubPanel[index] = CreatePanel ( 0, 10, cMainWidth, height, cPanel[index],0 )
  137.                 SetGadgetLayout cSubPanel[index], 1,1,1,1
  138.  
  139.                 cYsize[index] = height + 8
  140.                 cState[index] = state
  141.  
  142.                 If state = CLOSED Then HideGadget cSubPanel[index]
  143.  
  144.                 cLabel[index] = CreateLabel ( name$, 24, 0, length, 15, cMainPanel, 2 )
  145.                 SetGadgetLayout cLabel[index], 1,0,1,0
  146.         Else
  147.                 RuntimeError "Use index 1 to " + cpanelamount + "!"
  148.         EndIf
  149.  
  150. End Function
  151.  
  152.  
  153. Function RedrawMenu( c.collapse )
  154.  
  155.         ; get vertical size of menu and adjust main panel
  156.  
  157.         ysize = 8
  158.  
  159.         For count = 1 To cpanelamount
  160.                 If cstate[count] = CLOSED
  161.                         ysize = ysize + 10
  162.                 Else
  163.                         ysize = ysize + cYsize[count]
  164.                 EndIf
  165.  
  166.                 ysize = ysize + 16
  167.         Next
  168.  
  169.         If c
  170. ightside = TRUE
  171.                 SetGadgetShape cMainPanel, ClientWidth( cmainparent )-32-MENUWIDTH, 0, MENUWIDTH+16, ysize
  172.         Else
  173.                 SetGadgetShape cMainPanel, 16, 0, MENUWIDTH+16, ysize
  174.         EndIf
  175.  
  176.         ; organise panel vertical positions
  177.  
  178.         Ypos = 8
  179.  
  180.         For index = 1 To cpanelamount
  181.  
  182.                 If cState[index] = CLOSED
  183.                         ysize = 10
  184.                 Else
  185.                         ysize = cYsize[index]
  186.                 EndIf
  187.  
  188.                 ; move panels and labels
  189.  
  190.                 SetGadgetShape cPanel[index], 8, Ypos, GadgetWidth( cPanel[index] ), ysize
  191.                 SetGadgetShape cLabel[index], 16, Ypos - 8, GadgetWidth( cLabel[index] ), GadgetHeight( cLabel[index] )
  192.  
  193.                 Ypos = Ypos + ysize + 16
  194.         Next
  195.  
  196.         ; adjust slider
  197.  
  198.         SetSliderRange cMainSlider, ClientHeight(cMainParent), Ypos
  199.  
  200. End Function


Comments :


PCBGuy(Posted 1+ years ago)

 How do you work this with Blitz3D. Some of these commands are not seen like "Main"


N(Posted 1+ years ago)

 Uh... PCBGuy.. this is for BlitzPlus...*puts a bunch of electrodes on PCBGuy's head to make sure he's using his brain*


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal