RootsLabs

More than a tool ! GitHub Google+ LinkedIn RSS

StatusBar – Redimensionnement et Largeur de fields

Progi1984 - Commentaires (0)

Un code 2 en 1 pour redimensionner les fields d’une barre de statut et récupérer la largeur d’un field.

  • Systèmes :
    • Windows
    • Linux
    • MacOs
  • PureBasic 4.30

EnableExplicit

Global lInc.l, lEvent.l
Global bEndWindow.b
Global Dim FieldWidths.l(3)

Declare StatusBar_Resize(StatusBarID.l, Num.l, Array StatusBarWidthArr.l(1))
Declare StatusBar_Width(StatusBarID.l, Field.l)
Declare StatusBar_SetFieldWidth(StatusBar.l, Item.l, Width.l)

OpenWindow(0, 0, 0, 600, 400, "Resize StatusBar", #PB_Window_SystemMenu|#PB_Window_TitleBar|#PB_Window_ScreenCentered)
	CreateStatusBar(0, WindowID(0))
	For lInc = 0 To 2
		AddStatusBarField(WindowWidth(0)/3)
		StatusBarText(0, lInc, "Field "+Str(lInc))
	Next
	ButtonGadget(1, (WindowWidth(0) - 200)/2, (WindowHeight(0) - 50)/2, 200, 50, "Resize StatusBar")
	ButtonGadget(2, (WindowWidth(0) - 200)/2, (WindowHeight(0) - 50)/2+50, 200, 50, "Resize a part of StatusBar")

	Repeat
		lEvent = WaitWindowEvent()
		Select lEvent
			Case #PB_Event_Gadget
				Select EventGadget()
					Case 1 ;{ Resize StatusBar
						FieldWidths(0) = Random(WindowWidth(0)/2)
						FieldWidths(1) = Random(WindowWidth(0) - FieldWidths(0))
						FieldWidths(2) = -1
						StatusBar_Resize(0, 3, FieldWidths())
						StatusBarText(0, 0, "Width = "+Str(StatusBar_Width(0,0)))
						StatusBarText(0, 1, "Width = "+Str(StatusBar_Width(0,1)))
						StatusBarText(0, 2, "Width = "+Str(StatusBar_Width(0,2)))
					;}
					Case 2 ;{ Resize a part of StatusBar
						StatusBar_SetFieldWidth(0, 1, 60 + Random(40))
						StatusBarText(0, 1, "Width = "+Str(StatusBar_Width(0,1)))
					;}
				EndSelect
			Case #PB_Event_CloseWindow ;{
				bEndWindow = #True
			;}
		EndSelect
	Until bEndWindow

;@desc Resize fields of a statusbar
;@@windows
;@link http://msdn.microsoft.com/en-us/library/bb760757(VS.85).aspx
ProcedureDLL StatusBar_Resize(StatusBar.l, Num.l, Array StatusBarWidthArr.l(1))
	If IsStatusBar(StatusBar)
		Protected StatusBarID = StatusBarID(StatusBar)
		CompilerSelect #PB_Compiler_OS
			CompilerCase #PB_OS_Linux ;{
				Protected *HBoxGTK.GtkWidget, *FrameGTK.GtkWidget
				Protected *HBoxList.GList, *HBoxItem.GList
				Protected *AllocItem.GtkAllocation
				Protected lIncA.l, lIncB.l, lPadding.l
				*HBoxGTK.GtkWidget = StatusBarID
				*HBoxList = gtk_container_get_children_(*HBoxGTK)
	
				For lIncA = 0 To g_list_length_(*HBoxList) - 1
					*HBoxItem = g_list_nth_(*HBoxList, lIncA)
					*FrameGTK = *HBoxItem\data
	
					; get the padding of gtkobjects in gtkhbox
					gtk_box_query_child_packing_(*HBoxGTK, *FrameGTK, 0, 0, @lPadding, 0)
					*AllocItem = *FrameGTK\allocation
					If lIncA <> g_list_length_(*HBoxList) - 1
						; define the width of statusbarfield
						*AllocItem\width = StatusBarWidthArr(lIncA)
					Else
						; calculate the last statusbarfield width
						If StatusBarWidthArr(lIncA) = - 1 
							*AllocItem\width = *HBoxGTK\allocation\width
							For lIncB = 0 To lIncA
								*AllocItem\width - StatusBarWidthArr(lIncB) 
							Next
							*AllocItem\width - (lIncA + 1) * 2 * lPadding
						Else ; if defined, define the width of statusbarfield
							*AllocItem\width = StatusBarWidthArr(lIncA)
						EndIf
					EndIf
					
					; define the start position of frames
					*AllocItem\x = lPadding
					If lIncA > 0
						For lIncB = 0 To lIncA
							*AllocItem\x + lPadding * 2 + StatusBarWidthArr(lIncB) 
						Next
					EndIf
					gtk_widget_size_allocate_(*FrameGTK, *AllocItem)
					gtk_widget_set_size_request_(*FrameGTK, *AllocItem\width, *AllocItem\height)
				Next
			;}
			CompilerCase #PB_OS_Windows ;{
				SendMessage_(StatusBarID,  #SB_SETPARTS,  Num,  StatusBarWidthArr())
			;}
		CompilerEndSelect
	EndIf
EndProcedure

;@desc Returns the width of the nth field of a statusbar
;@@windows
;@link http://msdn.microsoft.com/en-us/library/bb760748(VS.85).aspx
ProcedureDLL StatusBar_Width(StatusBar.l, Field.l)
	If IsStatusBar(StatusBar)
		Protected StatusBarID = StatusBarID(StatusBar)
		CompilerSelect #PB_Compiler_OS
			CompilerCase #PB_OS_Linux ;{
				Protected *HBoxGTK.GtkWidget, *FrameGTK.GtkWidget
				Protected *HBoxList.GList, *HBoxItem.GList
				Protected *AllocItem.GtkAllocation
				*HBoxGTK.GtkWidget = StatusBarID
				*HBoxList = gtk_container_get_children_(*HBoxGTK)
				If Field >= g_list_length_(*HBoxList)
					Field = g_list_length_(*HBoxList) - 1
				EndIf
	
				*HBoxItem = g_list_nth_(*HBoxList, Field)
				*FrameGTK = *HBoxItem\data
				*AllocItem = *FrameGTK\allocation
				ProcedureReturn *AllocItem\width
			;}
			CompilerCase #PB_OS_Windows ;{
				Protected rc.RECT
				SendMessage_(StatusBarID, #SB_GETRECT, Field,@rc)
				ProcedureReturn rc\right - rc\left
			;}
		CompilerEndSelect
	EndIf
EndProcedure

;@@windows
;@author Freak
ProcedureDLL StatusBar_SetFieldWidth(StatusBar.l, Item.l, Width.l)
	If IsStatusBar(StatusBar)
		Protected StatusBarID = StatusBarID(StatusBar)
		CompilerSelect #PB_Compiler_OS
			CompilerCase #PB_OS_Linux ;{
				Protected *HBoxGTK.GtkWidget, *FrameGTK.GtkWidget
				Protected *HBoxList.GList, *HBoxItem.GList
				Protected *AllocItem.GtkAllocation
				Protected lIncA.l, lIncB.l, lPadding.l
				*HBoxGTK.GtkWidget = StatusBarID
				*HBoxList = gtk_container_get_children_(*HBoxGTK)
	
				*HBoxItem = g_list_nth_(*HBoxList, Item)
				*FrameGTK = *HBoxItem\data
	
				; get the padding of gtkobjects in gtkhbox
				gtk_box_query_child_packing_(*HBoxGTK, *FrameGTK, 0, 0, @lPadding, 0)
				*AllocItem = *FrameGTK\allocation
				If lIncA <> g_list_length_(*HBoxList) - 1
					; define the width of statusbarfield
					*AllocItem\width = Width
				Else
					; calculate the last statusbarfield width
					If Width = - 1 
						*AllocItem\width = *HBoxGTK\allocation\width
						For lIncB = 0 To lIncA
							*AllocItem\width - StatusBar_Width(StatusBar, lIncB) 
						Next
						*AllocItem\width - (lIncA + 1) * 2 * lPadding
					Else ; if defined, define the width of statusbarfield
						*AllocItem\width = Width
					EndIf
				EndIf
				
				; define the start position of frames
				*AllocItem\x = lPadding
				If lIncA > 0
					For lIncB = 0 To lIncA
						*AllocItem\x + lPadding * 2 + StatusBar_Width(StatusBar, lIncB) 
					Next
				EndIf
				gtk_widget_size_allocate_(*FrameGTK, *AllocItem)
				gtk_widget_set_size_request_(*FrameGTK, *AllocItem\width, *AllocItem\height)
			;}
			CompilerCase #PB_OS_Windows ;{
			  Protected nParts.l, lLeftField.l, lResult.l
			  Protected *dwFields
				nParts = SendMessage_(StatusBarID, #SB_GETPARTS, 0, 0)
				*dwFields = AllocateMemory(nParts * 4)
				If *dwFields
					SendMessage_(StatusBarID, #SB_GETPARTS, nParts, *dwFields)
					If Item < 1
						lLeftField = 0
					Else
						lLeftField = PeekL(*dwFields + ((Item - 1) * 4))
					EndIf
					PokeL(*dwFields + (Item * 4), lLeftField + Width)
					lResult = SendMessage_(StatusBarID, #SB_SETPARTS, nParts, *dwFields)
					FreeMemory(*dwFields)
					ProcedureReturn lResult
				EndIf
				ProcedureReturn #False
			;}
		CompilerEndSelect
	EndIf
EndProcedure

Ajouter un commentaire

Commentaire :