SetGadgetAttribute() extended

Share your advanced PureBasic knowledge/code with the community.
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

SetGadgetAttribute() extended

Post by Flype »

This is obviously a very very simple trick, but can be useful...

It's a template you can use and easily extends
for adding more Attributes to the function SetGadgetAttribute().

Code: Select all

;=======================================
;== Macro SetGadgetAttribute() 
;== Replaces and Extends the PureBasic original function.
;=======================================

Procedure __SetGadgetAttribute(gadget, attribute, value)
	
	Select attribute
		
		Case #PB_String_ReadOnly
			
			If GadgetType(gadget) = #PB_GadgetType_String
				SendMessage_(GadgetID(gadget), #EM_SETREADONLY, value, #Null) 
				If value = #True
					SetGadgetColor(gadget, #PB_Gadget_BackColor, #White)
				EndIf
			EndIf
			
		Default
			
			SetGadgetAttribute(gadget, attribute, value)
			
	EndSelect
	
EndProcedure

Macro SetGadgetAttribute(gadget, attribute, value)
	__SetGadgetAttribute(gadget, attribute, value)
EndMacro

;=======================================
;== Test
;=======================================

If OpenWindow(0, 300, 300, 100, 30, "test")
	
	StringGadget(0, 5, 5, 90, 20, "")
	
	SetGadgetText(0, "READONLY")
	SetGadgetAttribute(0, #PB_String_ReadOnly, #True)
	
	Repeat
	Until WaitWindowEvent() = #PB_Event_CloseWindow
	
	SetGadgetText(0, "READWRITE")
	SetGadgetAttribute(0, #PB_String_ReadOnly, #False)
	
	Repeat
	Until WaitWindowEvent() = #PB_Event_CloseWindow
	
EndIf

End
Last edited by Flype on Tue Jul 21, 2009 7:11 pm, edited 1 time in total.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
freak
PureBasic Team
PureBasic Team
Posts: 5944
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

You have to place the macro after the procedure, otherwise your SetGadgetAttribute() call inside the procedure will be replaced by the macro too and you get endless recursion when the default case happens.
quidquid Latine dictum sit altum videtur
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

Oups you're right. [Corrected]

Thank you freak
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Post Reply