Fangbeast wrote:As the user types a string, it is grabbed during a change event and:
Any # entered manually are removed.
All first letters in the string are uppercased
All spaces are then removed.
The # symbol is replaced at the start.
Then the string is placed back into the string box.
This "#run program setup"
Should always be this "#RunProgramSetup
Modified this
numeric-filter routine from an earlier post just for you:
Code: Select all
;====================================================================
; Custom StringGadget() filter/handler -> for Fangbeast
;
; cross-platform masking routines by Keya & Shardik - Thank you!
; filter routine modified as per custom requirements, by TI-994A
; original posted code available in the following forum thread:
;
;====================================================================
; http://forums.purebasic.com/english/viewtopic.php?t=67986&p=503517
;====================================================================
;
; tested & working on Win10 running PureBasic v5.62 (x64)
; and Mac OS-X High Sierra running PureBasic v5.60 (x64)
;
; by TI-994A - free to use, improve, share...
;
; 20th May 2018
;====================================================================
;==================
; masking routines
;==================
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
CompilerIf #PB_Compiler_Version < 470 Or (#PB_Compiler_Version >= 500 And Subsystem("Carbon"))
ImportC ""
GetControlData(ControlRef.L, ControlPartCode.L, TagName.L, BufferSize.L, *Buffer, *ActualSize)
SetControlData(ControlRef.L, ControlPartCode.L, TagName.L, BufferSize.L, *Buffer)
EndImport
#kControlEditTextPart = 5
#kControlEditTextSelectionTag = $73656C65 ;'sele'
Structure ControlEditTextSelectionRec
SelStart.W
SelEnd.W
EndStructure
CompilerElse
Global NSRangeZero.NSRange
Procedure.i TextEditor(Gadget.i)
Protected TextField.i = GadgetID(Gadget)
Protected Window.i = CocoaMessage(0, TextField, "window")
ProcedureReturn CocoaMessage(0, Window, "fieldEditor:", #YES, "forObject:", TextField)
EndProcedure
CompilerEndIf
CompilerEndIf
Procedure SetStringGadgetSelection(GadgetID.i, Start.i, Length.i=0)
SetActiveGadget(gadgetid)
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
SendMessage_(GadgetID(GadgetID), #EM_SETSEL, Start-1, Start-1)
CompilerCase #PB_OS_Linux
gtk_editable_select_region_(GadgetID(gadgetid), Start-1, Start-1)
CompilerCase #PB_OS_MacOS
CompilerIf #PB_Compiler_Version < 470 Or (#PB_Compiler_Version >= 500 And Subsystem("Carbon"))
Protected TextSelection.ControlEditTextSelectionRec
TextSelection\selStart = Start - 1
TextSelection\selEnd = Start -1
SetControlData(GadgetID(GadgetID), #kControlEditTextPart,
#kControlEditTextSelectionTag, SizeOf(ControlEditTextSelectionRec), @TextSelection)
CompilerElse
Protected Range.NSRange\location = Start - 1 : Range\length = 0
CocoaMessage(0, TextEditor(GadgetID), "setSelectedRange:@", @Range)
CompilerEndIf
CompilerEndSelect
EndProcedure
Procedure.i GetCaretPosition(GadgetID.i)
Protected Start.i = 0
Protected Stop.i = 0
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
SendMessage_(GadgetID(GadgetID.i), #EM_GETSEL, @Start.i, @Stop.i)
ProcedureReturn Stop + 1
CompilerCase #PB_OS_Linux
Stop = gtk_editable_get_position_(GadgetID(gadgetid))
ProcedureReturn Stop + 1
CompilerCase #PB_OS_MacOS
CompilerIf #PB_Compiler_Version < 470 Or (#PB_Compiler_Version >= 500 And Subsystem("Carbon"))
Protected TextSelection.ControlEditTextSelectionRec
GetControlData(GadgetID(GadgetID), #kControlEditTextPart, #kControlEditTextSelectionTag,
SizeOf(ControlEditTextSelectionRec), @TextSelection.ControlEditTextSelectionRec, 0)
ProcedureReturn TextSelection\End + 1
CompilerElse
Protected Range.NSRange
CocoaMessage(@Range, TextEditor(GadgetID), "selectedRange")
ProcedureReturn Range\location + Range\length + 1
CompilerEndIf
CompilerEndSelect
EndProcedure
Procedure RestrictChars(*pchr.Ascii, slen)
Static newWord = 1
If slen
*pout.Ascii = *pchr
For i = 1 To slen
Select *pchr\a
Case '0' To '9', 'a' To 'z', 'A' To 'Z'
If newWord
changed = i
newWord = 0
EndIf
*pchr+SizeOf(Character): *pout+SizeOf(Character)
Case '#':
hexcnt+1
If hexcnt=1 And i = 1
*pchr+SizeOf(Character): *pout+SizeOf(Character)
Else
Goto BadChar
EndIf
Case ' ':
newWord = 1
Goto BadChar
Default:
BadChar:
*pchr+SizeOf(Character): changed = -1
EndSelect
*pout\a = *pchr\a
Next
Else
newWord = 1
EndIf
ProcedureReturn changed
EndProcedure
Procedure StringFilter()
Static Changing
gadget = EventGadget()
If Not Changing
Changing=1
sTxt$ = GetGadgetText(gadget)
lastcaretpos = GetCaretPosition(gadget)
currentCharPos = RestrictChars(@sTxt$, Len(sTxt$))
If currentCharPos = -1
SetGadgetText(gadget, sTxt$)
SetStringGadgetSelection(gadget, lastcaretpos-1)
ElseIf currentCharPos > 0
If currentCharPos = 1
prefix$ = "#"
EndIf
sTxt$ = prefix$ + Left(sTxt$, Len(sTxt$) - 1) + UCase(Right(sTxt$, 1))
SetGadgetText(gadget, sTxt$)
SetStringGadgetSelection(gadget, lastcaretpos + 1)
EndIf
Changing = 0
EndIf
EndProcedure
;===============
; usage example
;===============
wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
mainWindow = OpenWindow(#PB_Any, 0, 0, 350, 100, "Custom StringGadget()", wFlags)
customStringGadget = StringGadget(#PB_Any, 50, 30, 250, 30, "")
BindGadgetEvent(customStringGadget, @StringFilter(), #PB_EventType_Change)
While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
As indicated in the code header, the original masking routines are courtesy of Keya & Shardik. A little long, but
cross-platform, and easily tucked away into an include toolbox for future use.
The modifications conform to your requirements, of prefixing a hex (#), capitalising every first letter, and stripping all inline spaces. It currently accepts all alpha-numeric characters, and would capitalise characters which follow a tap on the space bar.
Hope it helps.