Page 1 of 1

Cursor on string gadget keeps moving in front

Posted: Sat May 28, 2011 6:11 pm
by liam
i have a string gadget which has a regexed input filtering which limits characters only to numerals and periods, the problem with this one is that the cursor keeps on moving in front of the entered text. is there a way to make it behave just like an ordinary string gadget?

Code: Select all

  If OpenWindow(0, 0, 0, 322, 105, "Stubborn Cursor", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    StringGadget(0, 8,  30, 306, 20, "")
    Repeat
      
      Select WaitWindowEvent()
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case 0
              Select EventType()
                Case #PB_EventType_Change
                  SetGadgetText(0, ReplaceRegularExpression(CreateRegularExpression(#PB_Any, "[^1234567890.]"), GetGadgetText(0), "")) 
              EndSelect
          EndSelect 
      EndSelect
      
    Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf

anyway, i think you guys already know that all im after is to filter entered characters, if you guys have any different suggestion in implementing this in another way im all ears.

tia

Re: Cursor on string gadget keeps moving in front

Posted: Sat May 28, 2011 8:21 pm
by rsts
liam wrote: is there a way to make it behave just like an ordinary string gadget?
Is is.

If you wish to have the text appear at the end, you will need to position the curser at the end of the text, since you are setting the text yourself.

Try http://www.purebasic.fr/english/viewtop ... ringgadget

cheers

Re: Cursor on string gadget keeps moving in front

Posted: Sat May 28, 2011 8:22 pm
by Rook Zimbabwe
It could be the way you are calling and looking at events... try this method from the included VD

Code: Select all

Enumeration
  #Button_0
  #String_0
EndEnumeration

Global Window_0

Structure VisualDesignerGadgets
  Gadget.l
  EventFunction.l
EndStructure

Global NewList EventProcedures.VisualDesignerGadgets()

Procedure String_0_Event(Window, Event, Gadget, Type)
  Debug "#String_0"
EndProcedure

Procedure Button_0_Event(Window, Event, Gadget, Type)
  Debug "#Button_0"
  SetGadgetText(#String_0, "")
EndProcedure

Procedure RegisterGadgetEvent(Gadget, *Function)
  
  If IsGadget(Gadget)
    AddElement(EventProcedures())
    EventProcedures()\Gadget        = Gadget
    EventProcedures()\EventFunction = *Function
  EndIf
  
EndProcedure

Procedure CallEventFunction(Window, Event, Gadget, Type)
  
  ForEach EventProcedures()
    If EventProcedures()\Gadget = Gadget
      CallFunctionFast(EventProcedures()\EventFunction, Window, Event, Gadget, Type)
      LastElement(EventProcedures())
    EndIf
  Next
  
EndProcedure

Procedure Open_Window_0()
  
  Window_0 = OpenWindow(#PB_Any, 5, 5, 400, 157, "Window 0",  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered )
  If Window_0
    If CreateGadgetList(WindowID(Window_0))
      ButtonGadget(#Button_0, 10, 80, 120, 50, "BUTTON")
      RegisterGadgetEvent(#Button_0, @Button_0_Event())
      StringGadget(#String_0, 10, 20, 370, 30, "")
      RegisterGadgetEvent(#String_0, @String_0_Event())
      
    EndIf
  EndIf
EndProcedure

Open_Window_0()

Repeat
  
  Event  = WaitWindowEvent()
  Gadget = EventGadget()
  Type   = EventType()
  Window = EventWindow()
  
  Select Event
    Case #PB_Event_Gadget
      CallEventFunction(Window, Event, Gadget, Type)
      
  EndSelect
  
Until Event = #PB_Event_CloseWindow

End
or this method from PUREform

Code: Select all

;{- Enumerations / DataSections
;{ Windows
Enumeration
  #Window_0
EndEnumeration
;}
;{ Gadgets
Enumeration
  #String_0
  #Button_1
EndEnumeration
;}
Define.l Event, EventWindow, EventGadget, EventType, EventMenu
;}
Procedure OpenWindow_Window_0()
  If OpenWindow(#Window_0, 450, 200, 400, 152, "Window_0", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
    StringGadget(#String_0, 15, 10, 360, 30, "")
    ButtonGadget(#Button_1, 20, 55, 165, 50, "BUTTON")
  EndIf
EndProcedure

OpenWindow_Window_0()

;{- Event loop
Repeat
  Event = WaitWindowEvent()
  Select Event
    ; ///////////////////
    Case #PB_Event_Gadget
      EventGadget = EventGadget()
      EventType = EventType()
      If EventGadget = #String_0
      ; essentially do NOTHING (in fact this can be deleted as an instance)
      ElseIf EventGadget = #Button_1
      SetGadgetText(#String_0, "")
      EndIf
    ; ////////////////////////
    Case #PB_Event_CloseWindow
      EventWindow = EventWindow()
      If EventWindow = #Window_0
        CloseWindow(#Window_0)
        Break
      EndIf
  EndSelect
ForEver
;
;}
:mrgreen:

perhaps I misunderstood... with mine code you can click anywhere and modify the text...

Re: Cursor on string gadget keeps moving in front

Posted: Sat May 28, 2011 10:19 pm
by Derek
If you subclass the gadget then you can limit the input before it gets put into the stringgadget.

(most of this code is stolen from one of srod's examples!) :wink:

Code: Select all

Global oldcallback

Procedure newcallback(hwnd, msg, wparam, lparam)
  Select msg
  Case #WM_CHAR
    If (wParam < #VK_0 Or wparam > #VK_9) And wparam<>8 And wparam<>'.'
      wparam=0
    EndIf
  EndSelect
  result = CallWindowProc_(Oldcallback, hwnd, msg, wparam, lparam)
  ProcedureReturn result
EndProcedure

OpenWindow(0, 0, 0, 220, 40, "",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
StringGadget(0, 10, 10, 200, 20,"")
oldcallback = SetWindowLong_(GadgetID(0), #GWL_WNDPROC, @newcallback())
Repeat
  event = WaitWindowEvent()
Until event = #PB_Event_CloseWindow

Re: Cursor on string gadget keeps moving in front

Posted: Sun May 29, 2011 3:15 am
by liam
thank you very much for the replies guys!

@Rook Zimbabwe
im sorry but the code you posted didn't filter any characters [^0-9.] so it didn't work for me but thanks for trying.

@rsts
the code on the link you posted works, but id still have to implement it with the string filtering i already have which makes the code longer but i'll save that to my snippets collection (could come in handy one day)

@Derek (and srod)
neat! that code works too and i don't need to implement the regex filtering anymore making my code a lot shorter, so i guess i'll go with this one.

---
another follow-up question: is there any other way to move the cursor to the end (or at any part) of the string in a string gadget without using winapis?