Page 1 of 1

SetGadgetAttribute() extended

Posted: Mon Jul 20, 2009 7:07 pm
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

Posted: Mon Jul 20, 2009 9:11 pm
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.

Posted: Tue Jul 21, 2009 7:11 pm
by Flype
Oups you're right. [Corrected]

Thank you freak