Extended StringGadget

Share your advanced PureBasic knowledge/code with the community.
Cyllceaux
Enthusiast
Enthusiast
Posts: 469
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Extended StringGadget

Post by Cyllceaux »

Hey...

I have the problem to give a StringGadget a little more Functions. Like a Back-Text or a "Pressed-Return-Event". So I created a "bad" module to solve that problem.

Code: Select all

DeclareModule StringGadget
	Prototype ReturnEvent()
	Declare RegisterStringGadget(gadget,text.s,ReturnEvent.ReturnEvent=0)
	Declare UnRegisterStringGadget(gadget)
EndDeclareModule

Module StringGadget
	EnableExplicit
	
	Structure strGad
		string.i
		short.i
		text.s
		event.ReturnEvent
		idx.i
	EndStructure
	
	Global NewMap gadgets.strGad()
	
	Global pidx=1000
	
	Procedure RC()
		Protected gadget=GetActiveGadget()
		Protected key.s=Str(gadget)
		Protected *gad.strGad=gadgets(key)
		If *gad\Event
			*gad\Event()
		EndIf
	EndProcedure
	
	
	Procedure LF()
		Protected gadget=EventGadget()
		Protected key.s=Str(gadget)
		Protected text.s=GetGadgetText(gadget)
		Protected *gad.strGad=gadgets(key)
		If text=""
			SetGadgetText(gadget,*gad\text)
			SetGadgetColor(gadget,#PB_Gadget_FrontColor,#Gray)
		EndIf
	EndProcedure
	
	Procedure GF()
		Protected gadget=EventGadget()
		Protected key.s=Str(gadget)
		Protected text.s=GetGadgetText(gadget)
		Protected *gad.strGad=gadgets(key)
		If text=*gad\text
			SetGadgetText(gadget,"")
		EndIf
		SetGadgetColor(gadget,#PB_Gadget_FrontColor,#Black)
	EndProcedure
	
	Procedure RegisterStringGadget(gadget,text.s,ret.ReturnEvent=0)
		Protected key.s=Str(gadget)
		Protected *gad.strGad=AddMapElement(gadgets(),key)
		With *gad
			\string=gadget
			\text=text
			\Event=ret
			\short=CreatePopupMenu(#PB_Any)
			\idx=pidx
		EndWith
		pidx+1
		If text<>""
			BindGadgetEvent(gadget,@LF(),#PB_EventType_LostFocus)
			BindGadgetEvent(gadget,@GF(),#PB_EventType_Focus)
			SetGadgetText(gadget,text)
			SetGadgetColor(gadget,#PB_Gadget_FrontColor,#Gray)
		EndIf
		If ret
			AddKeyboardShortcut(GetActiveWindow(),#PB_Shortcut_Return,*gad\idx)
			BindMenuEvent(*gad\short,*gad\idx,@RC())
		EndIf
	EndProcedure
	
	Procedure UnRegisterStringGadget(gadget)
		Protected key.s=Str(gadget)
		Protected *gad.strGad=FindMapElement(gadgets(),key)
		UnbindMenuEvent(*gad\short,*gad\idx,@RC())
		FreeMenu(*gad\short)
		DeleteMapElement(gadgets(),key)
		UnbindGadgetEvent(gadget,@LF(),#PB_EventType_LostFocus)
		UnbindGadgetEvent(gadget,@GF(),#PB_EventType_Focus)
		SetGadgetColor(gadget,#PB_Gadget_FrontColor,#Black)
		
	EndProcedure
	
EndModule

CompilerIf #PB_Compiler_IsMainFile
	Procedure pressedReturn()
		Debug GetGadgetText(0)
	EndProcedure
	OpenWindow(0,0,0,300,200,"StringGadget",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
	StringGadget(0,5,5,150,22,"")
	
	StringGadget::RegisterStringGadget(0,"Search...",@pressedReturn())
	Repeat
	Until WaitWindowEvent()=#PB_Event_CloseWindow
CompilerEndIf