[bb] Foldable menus by Wiebo [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:41

Previous topic - Next topic

BlitzBot

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) Select
; foldable menus for B+ by Wiebo

; how to use: create a window, add a menu, add panels to menu, add your gadgets to subpanels

main = CreateWindow ("Fold me", 0,0, 400, 400)

; window, panel amount, [rightside]

menu.collapse = CreateMainMenu ( main, 3, FALSE )

; menu, ysize, index, state, title$, titlestringlength

AddPanelToMenu( menu, 200, 1, OPEN, " Menu 1 ", 50 )
AddPanelToMenu( menu, 100, 2, OPEN, " Menu 2 ", 50 )
AddPanelToMenu( menu, 100, 3, OPEN, " Menu 3 ", 50 )

a = CreateButton( "Button 1", 25,25,50,25, menusubpanel[1],0)
SetGadgetLayout a, 1,0,1,0

a = CreateButton( "Button 2", 25,25,50,25, menusubpanel[2],0)
SetGadgetLayout a, 1,0,1,0

a = CreateButton( "Button 3", 25,25,50,25, menusubpanel[3],0)
SetGadgetLayout a, 1,0,1,0

RedrawMenu( menu )

Repeat

id = WaitEvent()

Select id
Case $201 ; mouse down

Select EventSource()

Case menupanel[1]
menustate[1]  = Not menustate[1]
RedrawMenu( menu )

Case menupanel[2]
menustate[2]  = Not menustate[2]
RedrawMenu( menu )

Case menupanel[3]
menustate[3]  = Not menustate[3]
RedrawMenu( menu )

End Select

Case $101

Select EventData()

Case 1
End

End Select


End Select

Forever

; -------------------------------------------------------

Const OPEN = 0
Const CLOSED = 1
Const MENUWIDTH = 198;214
Const MAXPANELS = 10

Type Collapse
Field MainPanel ; main panel to attach subpanels to goes here
Field MainWidth ; width of main panel
Field MainSlider ; slider attached to main panel
Field Rightside ; true or false. false means panel is drawn at left side of window
Field MainParent ; parent window
Field PanelAmount ; amount of actiual panels in menu
Field Panel[MAXPANELS] ; resizeable panels
Field SubPanel[MAXPANELS] ; add your GADGETS to this panel
Field Label[MAXPANELS] ; name to display
Field State[MAXPANELS] ; open or closed
Field Ysize[MAXPANELS] ; size when opened
End Type

; -------------------------------------------

Function CreateMainMenu.collapse( window, amount, rightside = TRUE )

If amount > 0 And amount <= MAXPANELS

; attach a menu bar to the correct side of a window

collapse.collapse = New collapse

collapseMainParent = window
collapseMainWidth = MENUWIDTH
collapsePanelAmount = amount
collapseRightside = rightside

collapseMainPanel = CreatePanel ( 0, 0, MENUWIDTH+16, 10, window )

; determine position

If rightside = TRUE

SetGadgetLayout collapseMainPanel, 0,1,1,0

collapseMainSlider = CreateSlider ( ClientWidth( window )-16, 0, 16, ClientHeight( window ), window, 2 )
SetGadgetLayout collapseMainSlider, 0,1,1,1
Else
SetGadgetLayout collapseMainPanel, 1,0,1,0

collapseMainSlider = CreateSlider ( 0, 0, 16, ClientHeight( window ), window, 2 )
SetGadgetLayout collapseMainSlider, 1,0,1,1
EndIf

; SetPanelColor collapseMainPanel, 255,0,255  ; indication of main panel

Return collapse
Else
RuntimeError "Use index 1 to " + MAXPANELS + "!"
EndIf

End Function


Function AddPanelToMenu( c.collapse, height, index, state, name$, length )

If index > 0 And index <= cpanelamount

cPanel[index] = CreatePanel ( 0, 0, cMainWidth, 10, cMainPanel, 1 )
SetGadgetLayout cPanel[index], 1,1,1,1
SetPanelColor cpanel[index], 190,190,190 ; click on this panel to open or close menu

cSubPanel[index] = CreatePanel ( 0, 10, cMainWidth, height, cPanel[index],0 )
SetGadgetLayout cSubPanel[index], 1,1,1,1

cYsize[index] = height + 8
cState[index] = state

If state = CLOSED Then HideGadget cSubPanel[index]

cLabel[index] = CreateLabel ( name$, 24, 0, length, 15, cMainPanel, 2 )
SetGadgetLayout cLabel[index], 1,0,1,0
Else
RuntimeError "Use index 1 to " + cpanelamount + "!"
EndIf

End Function


Function RedrawMenu( c.collapse )

; get vertical size of menu and adjust main panel

ysize = 8

For count = 1 To cpanelamount
If cstate[count] = CLOSED
ysize = ysize + 10
Else
ysize = ysize + cYsize[count]
EndIf

ysize = ysize + 16
Next

If cightside = TRUE
SetGadgetShape cMainPanel, ClientWidth( cmainparent )-32-MENUWIDTH, 0, MENUWIDTH+16, ysize
Else
SetGadgetShape cMainPanel, 16, 0, MENUWIDTH+16, ysize
EndIf

; organise panel vertical positions

Ypos = 8

For index = 1 To cpanelamount

If cState[index] = CLOSED
ysize = 10
Else
ysize = cYsize[index]
EndIf

; move panels and labels

SetGadgetShape cPanel[index], 8, Ypos, GadgetWidth( cPanel[index] ), ysize
SetGadgetShape cLabel[index], 16, Ypos - 8, GadgetWidth( cLabel[index] ), GadgetHeight( cLabel[index] )

Ypos = Ypos + ysize + 16
Next

; adjust slider

SetSliderRange cMainSlider, ClientHeight(cMainParent), Ypos

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*