Auto-Complete

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.

Does anyone have an example of making an auto-complete function that monitors a stringgadget as you type, searches a structure for the wanted information (based on the field you are typing in) and fills the rest of the stringgadget in from the matches,starting from the last character position you typed in?

Been trying to get it right but can't :(

Fangles
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

Here's a starting point for you, and not the best way to do it either:

Code: Select all

If OpenWindow(0,200,200,400,100,#PB_Window_SystemMenu,"Test")
  CreateGadgetList(WindowID())
  TextGadget(1,50,30,250,20,"Type 'abc' for the alphabet...")
  StringGadget(2,50,50,250,20,"")
  Repeat
    Sleep_(1) : ev=WindowEvent()
    If ev=#WM_KEYDOWN
      typing=GetTickCount_()+250 ; User is typing, so reset timer.
    EndIf
    If GetTickCount_()>typing ; User has stopped typing.
      a$=GetGadgetText(2)
      If a$="abc"
        SetGadgetText(2,"abcdefghijklmnopqrstuvwxyz")
      EndIf
    EndIf
  Until ev=#PB_Event_CloseWindow
EndIf

PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.

Thanks PB, not sure how this will fit with my existing code as I read somewhere that WindowEvent() will use up all CPU time? Not sure about that.

If you get a chance, I'll email you with exactly what I am trying to do. Danilo also gave me an API device that can reset the cursor position to the last character typed, which is important for how I want this thing to work.


Fangles
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> I read somewhere that WindowEvent() will use up all CPU time?

Only if you don't put Sleep_(1) in there, but I did, so it's okay.


PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.
Originally posted by PB

> I read somewhere that WindowEvent() will use up all CPU time?

Only if you don't put Sleep_(1) in there, but I did, so it's okay.
Yes, but you shouldn't put it where you've put it now, because it will result in slow gadget updates and such. The correct way would be:

Code: Select all

event = windowevent()
if event
  select event
    ...
  endselect
else
  sleep_(1); if no event that needs emidiate attention
           ; sleep for a while.
endif
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> Yes, but you shouldn't put it where you've put it now, because it
> will result in slow gadget updates and such. The correct way would
> be: [snipped]

Thanks for your tip, Pupil -- I have amended all my apps as a result.


PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

Fangbeast: maybe this is a start for you...

Code: Select all

Global hCombo, hComboEdit, hList
Global oldComboEditProc, ListHidden
;
OpenWindow(1,200,200,400,200,#PB_WINDOW_SystemMenu,"")
   CreateGadgetList(WindowID())
   hCombo = ComboBoxGadget(1,10,10,300,100,#PB_ComboBox_Editable)
   hList  = ListViewGadget(2,20,31,280,100)
            HideGadget(2,1): ListHidden = 1
            For a = 1 To 9: AddGadgetItem(2,-1,"FangBeast "+Str(a)): Next a
AddKeyboardShortcut(1,#PB_Shortcut_Return,100)
AddKeyboardShortcut(1,#PB_Shortcut_Up    ,101)
;
Procedure CheckContents(A$)
   If UCase(Left(A$,4)) = "FANG" ;And Len(A$) = 4
      HideGadget(2,0): ListHidden = 0
   Else
      HideGadget(2,1): ListHidden = 1
   EndIf
EndProcedure
;
Procedure ComboKey(Window,Message,wParam,lParam)
  If Message = #WM_KEYDOWN And wParam = #VK_DOWN And ListHidden = 0
     SetFocus_(hList): SetGadgetstate(2,0)
  EndIf
  Result = CallWindowProc_(oldComboEditProc,Window,Message,wParam,lParam)
ProcedureReturn Result
EndProcedure
;
Procedure WindowCallback(Window,Message,wParam,lParam)
 Result = #PB_ProcessPureBasicEvents 
 Select Window
    Case WindowID()
         Select Message
           Case #WM_COMMAND
             If lParam = hCombo And (wParam >> 16) = #CBN_EDITCHANGE
                A$=Space(1000) : GetWindowText_(hCombo,A$,1000)
                CheckContents(A$)
             EndIf
         EndSelect
 EndSelect
 ProcedureReturn Result 
EndProcedure 
;
SetWindowCallback(@WindowCallback())
hComboEdit = ChildWindowFromPoint_(hCombo,5,5)
oldComboEditProc = SetWindowLong_(hComboEdit,#GWL_WNDPROC,@ComboKey())
;
Repeat
   Select WaitWindowEvent()
      Case #PB_EventCloseWindow: End
      Case #PB_EventMenu
        If EventMenuID() = 100 And GetFocus_() = hList
           SetWindowText_(hCombo,GetGadgetItemText(2,GetGadgetState(2),0))
           HideGadget(2,1): ListHidden = 1
           SetFocus_(hComboEdit): SendMessage_(hComboEdit,#EM_SETSEL,$FFFFFFFF,$FFFFFFFF)
           SetGadgetState(2,0)
        ElseIf EventMenuID() = 101 And GetFocus_() = hList 
          If GetGadgetState(2) = 0
           SetGadgetState(2,-1)
           SetFocus_(hComboEdit): SendMessage_(hComboEdit,#EM_SETSEL,$FFFFFFFF,$FFFFFFFF)
          Else
           SetGadgetState(2,GetGadgetState(2)-1)
          EndIf
        EndIf
      Case #PB_EventGadget
   EndSelect
ForEver
Just Type "Fang" in the ComboBox to see whats happening...
But hey, its only small to get you started...

cya,
...Danilo

(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

After request, same for StringGadget:

Code: Select all

Global hString, hComboEdit, hList
Global oldStringGadgetProc, ListHidden
;
OpenWindow(1,200,200,400,200,#PB_WINDOW_SystemMenu,"")
   CreateGadgetList(WindowID())
   hString = StringGadget(1,10,10,300,20,"")
   hList   = ListViewGadget(2,20,31,280,100)
             HideGadget(2,1): ListHidden = 1
             For a = 1 To 9: AddGadgetItem(2,-1,"FangBeast "+Str(a)): Next a
AddKeyboardShortcut(1,#PB_Shortcut_Return,100)
AddKeyboardShortcut(1,#PB_Shortcut_Up    ,101)
;
Procedure CheckContents(A$)
   If UCase(Left(A$,4)) = "FANG" ;And Len(A$) = 4
      HideGadget(2,0): ListHidden = 0
   Else
      HideGadget(2,1): ListHidden = 1
   EndIf
EndProcedure
;
Procedure StringKey(Window,Message,wParam,lParam)
  If Message = #WM_KEYDOWN And wParam = #VK_DOWN And ListHidden = 0
     SetFocus_(hList): SetGadgetState(2,0)
  EndIf
  Result = CallWindowProc_(oldStringGadgetProc,Window,Message,wParam,lParam)
ProcedureReturn Result
EndProcedure
;
oldStringGadgetProc = SetWindowLong_(hString,#GWL_WNDPROC,@StringKey())
;
Repeat
   Select WaitWindowEvent()
      Case #PB_EventCloseWindow: End
      Case #PB_EventMenu
        If EventMenuID() = 100 And GetFocus_() = hList
           SetGadgetText(1,GetGadgetItemText(2,GetGadgetState(2),0))
           HideGadget(2,1): ListHidden = 1: SetGadgetState(2,-1)
           SetFocus_(hString): SendMessage_(hString,#WM_KEYDOWN,#VK_END,0)
        ElseIf EventMenuID() = 101 And GetFocus_() = hList 
          If GetGadgetState(2) = 0
           SetGadgetState(2,-1)
           SetFocus_(hString): SendMessage_(hString,#EM_SETSEL,$FFFFFFFF,$FFFFFFFF)
          Else
           SetGadgetState(2,GetGadgetState(2)-1)
          EndIf
        EndIf
      Case #PB_EventGadget
        If EventGadgetID() = 1 And EventType() = #PB_EventType_Change
           CheckContents(GetGadgetText(1))
        EndIf
   EndSelect
ForEver
As always, keyword is "Fang"... :)

cya,
...Danilo

(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Saboteur.

Perhaps this is more easy.... this need more work, but :) 'mola'...

Code: Select all

Procedure AutocompleteCombo()
  idx.l=GetGadgetState(0)
  texttyped.s=GetGadgetText(0)
  hComboEdit = ChildWindowFromPoint_(GadgetID(0),5,5)
  If texttyped""
    For f=0 To CountGadgetItems(0)-1
      SetGadgetState(0,f)
      If UCase(texttyped)=UCase(Left(GetGadgetText(0),Len(texttyped)))
        textfound.s=GetGadgetText(0)
        itemfound.l=f
      EndIf
    Next f
    If textfound""
      SetGadgetState(0,itemfound)
      SendMessage_(hComboEdit,#EM_SETSEL,Len(texttyped),Len(textfound))
    Else
      SetGadgetState(0,idx)
      ;SetGadgetText(0,texttyped)
      SendMessage_(hComboEdit,#WM_SETTEXT,0,texttyped)
      SendMessage_(hComboEdit,#EM_SETSEL,Len(texttyped),Len(texttyped))
    EndIf
  EndIf
EndProcedure

If OpenWindow(0,0,0,300,300,#PB_Window_SystemMenu | #PB_Window_TitleBar,"Autocompletion")
  If CreateGadgetList(WindowID())
    ComboBoxGadget(0,10,10,200,100,#PB_ComboBox_Editable)
    AddGadgetItem(0,-1,"Autocomplete")
    AddGadgetItem(0,-1,"Bernard")
    AddGadgetItem(0,-1,"Car")
    AddGadgetItem(0,-1,"Explorer")
    AddGadgetItem(0,-1,"Fantastic")
    AddGadgetItem(0,-1,"General protection error")
    AddGadgetItem(0,-1,"Purebasic")
    AddGadgetItem(0,-1,"Purepower")
    AddGadgetItem(0,-1,"Purevisual")
    AddGadgetItem(0,-1,"Question")
    AddGadgetItem(0,-1,"Zas")
  EndIf
EndIf

Repeat
  event=WaitWindowEvent()
  Select event
    Case #PB_Event_Gadget
      If EventGadgetID()=0
        AutocompleteCombo()
      EndIf
  EndSelect
Until event=#PB_Event_CloseWindow

End
Win98 Athlon 1200 256DDR
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.

Thanks but you may notice the date that Danilo posted that last year? October 2002. With his help pointing the way, I actually managed to find a simpler way to do it a couple of days after :):)

Still, 6 months late is better than nothing (laughing evilly)


We are Dyslexic of Borg, prepare to have your ass laminated!
Post Reply