StatusBar – Redimensionnement et Largeur de fields

6 juin 2009 - 862 mots - purebasic

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
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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

Laisser un commentaire

Merci. Votre message a bien été enregistré.