Layout

Partagez votre expérience de PureBasic avec les autres utilisateurs.
minirop
Messages : 321
Inscription : mer. 02/août/2006 21:06

Layout

Message par minirop »

bonjour,
comme PB n'intègre pas de Layout, j'ai essayé de les simuler et pour le moment c'est 1 layout par fenêtre et dans une seule direction (vertical ou horizontal) :

Code : Tout sélectionner

Structure GadgetStyle
	gadgetId.l
	height.l
	width.l
EndStructure

NewList gList.GadgetStyle()

Procedure AddGadget(gadgetId.l, width.l, height.l)
	Shared gList()
	
	AddElement(gList())
	gList()\gadgetId = gadgetId
	gList()\height = height
	gList()\width = width
EndProcedure

Enumeration
#layout_h
#layout_v
EndEnumeration

Procedure NewLayout(layoutId.l)
	ProcedureReturn ContainerGadget(layoutId, 0, 0, WindowHeight(windowId), WindowWidth(windowId))
EndProcedure

Procedure CloseLayout()
	CloseGadgetList()
EndProcedure

isDrawing.l = 0

Procedure DrawGadgets(windowId.l, layoutId.l, layoutType.l)
	Shared gList()
	
	;éviter les boucles infines lors du redimentionnement
	If isDrawing <> 0
		ProcedureReturn
	EndIf
	
	isDrawing = 1
	freeSizeH.l = WindowHeight(windowId)
	freeHgadget.l = 0
	totalFixedSizeH.l = 0
	freeSizeW.l = WindowWidth(windowId)
	freeWgadget.l = 0
	totalFixedSizeW.l = 0
	
	ForEach gList()
		If gList()\height <> #PB_Ignore
			freeSizeH - gList()\height
			If layoutType = #layout_v
				totalFixedSizeH + gList()\height
			Else
				If totalFixedSizeH < gList()\height
					totalFixedSizeH = gList()\height
				EndIf
			EndIf
		Else
			freeHgadget + 1
		EndIf
		
		If gList()\width <> #PB_Ignore
			freeSizeW - gList()\width
			If layoutType = #layout_h
				totalFixedSizeW + gList()\width
			Else
				If totalFixedSizeW < gList()\width
					totalFixedSizeW = gList()\width
				EndIf
			EndIf
		Else
			freeWgadget + 1
		EndIf
	Next
	
	If freeSizeH < 0
		freeSizeH = 0
	Else
		If freeHgadget > 0
			freeSizeH / freeHgadget
		Else
			freeSizeH = 0
		EndIf
	EndIf
	
	If freeSizeW < 0
		freeSizeW = 0
	Else
		If freeWgadget > 0
			freeSizeW / freeWgadget
		Else
			freeSizeW = 0
		EndIf
	EndIf
	
	If totalFixedSizeH > WindowHeight(windowId) Or freeSizeH = 0
		ResizeWindow(windowId, #PB_Ignore, #PB_Ignore, #PB_Ignore, totalFixedSizeH)
	EndIf
	If totalFixedSizeW > WindowWidth(windowId) Or freeSizeW = 0
		ResizeWindow(windowId, #PB_Ignore, #PB_Ignore, totalFixedSizeW, #PB_Ignore)
	EndIf
	ResizeGadget(layoutId, 0, 0, WindowWidth(windowId), WindowHeight(windowId))
	
	posX.l = 0
	posY.l = 0
	ForEach gList()
		If gList()\height = #PB_Ignore
			If layoutType = #layout_h
				newSizeH = WindowHeight(windowId)
			Else
				newSizeH = freeSizeH
			EndIf
		Else
			newSizeH = gList()\height
		EndIf
		
		If gList()\width = #PB_Ignore
			If layoutType = #layout_v
				newSizeW = WindowWidth(windowId)
			Else
				newSizeW = freeSizeW
			EndIf
		Else
			newSizeW = gList()\width
		EndIf
		
		If newSizeW > 0 And newSizeH > 0
			ResizeGadget(gList()\gadgetId, posX, posY, newSizeW, newSizeH)
			If layoutType = #layout_h
				posX = posX + newSizeW
			EndIf
			If layoutType = #layout_v
				posY = posY + newSizeH
			EndIf
		EndIf
	Next
	
	isDrawing = 0
EndProcedure

; code d'exemple
OpenWindow(0, #PB_Ignore, #PB_Ignore, 800, 600, "salut", #PB_Window_SystemMenu | #PB_Window_SizeGadget)

NewLayout(1)
	StringGadget(23, 0, 0, 10, 10, "hahaha")
	StringGadget(24, 0, 0, 10, 10, "bfbfbf")
	StringGadget(25, 0, 0, 10, 10, "cgcgcg")
	AddGadget(24, 450, #PB_Ignore)
	AddGadget(23, #PB_Ignore, #PB_Ignore)
	AddGadget(25, #PB_Ignore, 25)
CloseLayout()

DrawGadgets(0, 1, #layout_v)

Repeat
	event = WaitWindowEvent()
	If event
		If event = #PB_Event_SizeWindow
			If WindowHeight(windowId) < 400
				DrawGadgets(0, 1, #layout_h)
			Else
				DrawGadgets(0, 1, #layout_v)
			EndIf
			
			SetWindowTitle(0, Str(WindowWidth(windowId)) + " x " +Str(WindowHeight(windowId)))
		EndIf
	Else  
		Delay(1)
	EndIf
Until event = #PB_Event_CloseWindow
reste à pouvoir mettre un layout dans un autre layout (en partie grâce à une liste chainée dans une structure) et je pourrais améliorer grandement ce code.

ps : il y a quelques bugs d'affichage (du flickering lors du redimensionnement)

voilà :D
Ollivier
Messages : 4197
Inscription : ven. 29/juin/2007 17:50
Localisation : Encore ?
Contact :

Message par Ollivier »

Même si je réponds franchement à la bourre, et même si on a du mal à percevoir la subtilité que tu mets en valeur, je trouve que cette dernière a une valeur assez importante pour relancer le sujet.

Donc merci pour ton code.

Ollivier
Répondre