Below is an example of an incremental search that works pretty good I think.

Some details on how it works.

Each time the user types a character key, the character is appended to the
search string and the list box is searched for an item that starts with
current search string.

For each keypress a timer is set to expire after a certain number of
seconds.  When the timer expires the search string is reset to the empty
string.  This expiration time can be adjusted:

::constant expiration  7

Character keys are not passed through to the listbox.  If they were, the
listbox would also do its own incremental search.

Other keys like page down, home, ctrl-home, etc., are passed through so
that the list box performs its normal thing for those keys.

If the user types a backspace, it backs the search up 1 character.

If the user types the Del key it resets the search altogether.

As I said, it works pretty well for what I would want.  It could be tweaked
to work however you might want.

The program is below.  It certainly has word wraps in it:

/* Simple Dialog myListBoxSearch.rex */

  symbolMap = .table~new
  symbolMap[IDC_LB_FILEs]     = 200
  symbolMap[IDC_ST_SEARCHSTR] = 210

  .application~setDefaults('O', symbolMap, .false, 'Courier New', 10)

  dlg = .SimpleDialog~new
  if dlg~initCode = 0 then do
    if dlg~execute("SHOWTOP") == dlg~IDOK then do
      say 'User searched for and found:' dlg~searchAndFound
    end
    else do
      say 'User canceled'
    end
  end

return 0
-- End of entry point.

::requires "ooDialog.cls"

::class 'SimpleDialog' subclass UserDialog

::constant expiration  7  -- Time between keystrokes before the search
string is
                          -- reset.  If the user does not type any keys
within
                          -- this amount of time, in seconds, the search
string
                          -- is set back to blank.

::attribute searchAndFound get
  expose selectedText
  return selectedText

::method init
  expose selectedText searchStr curIndex timer

  forward class (super) continue
  self~create(30, 30, 186, 124, "Directory Listing with Search", "CENTER")

  selectedText = ''
  searchStr    = ''
  curIndex     = 1
  timer        = .nil

::method defineDialog

    self~createListBox(IDC_LB_FILES, 10, 10, 166, 90, 'VSCROLL HSCROLL
PARTIAL SORT NOTIFY')
    self~createStaticText(IDC_ST_SEARCHSTR, 10, 102, 64, 11, , '')
    self~createPushButton(IDCANCEL, 74, 105, 50, 14, ,"Cancel")
    self~createPushButton(IDOK, 126, 105, 50, 14, 'DEFAUT', "Ok")

::method initDialog
  expose lb stSearchStr

  lb = self~newListBox(IDC_LB_FILES)
  lb~addDirectory("C:\Windows\System32\*", "READWRITE READONLY HIDDEN
SYSTEM DIRECTORY ARCHIVE")

  lb~connectCharEvent

  stSearchStr = self~newStatic(IDC_ST_SEARCHSTR)


::method onChar unguarded
  expose searchStr curIndex
  use arg char, isShift, isCtrl, isAlt, misc, lBox

  if self~isDel(char, isCtrl, misc) then do
    self~resetSearch
    return .true
  end

  if self~isBkSpc(char, isCtrl, misc) then do
    len = searchStr~length
    if len > 0 then searchStr = searchStr~substr(1, searchStr~length - 1)
    curIndex = 1
  end
  else do
    if isShift | isCtrl | isAlt | (misc~pos('extended') <> 0) then return
.true
    searchStr ||= char~d2c
  end

  self~incrementalSearch(lBox)
  return .false

::method incrementalSearch private unguarded
  expose searchStr curIndex
  use strict arg lBox

  self~setStatusBar
  self~resetTimer

  newIndex = lBox~find(searchStr, curIndex)
  if newIndex <> curIndex then do
    if newIndex == 0 then do
      -- Setting the selected index to 0 has the effect of removing
      -- the selection from all items.  This gives the user the
      -- hint that there is no next item with the current search
      -- string.  We leave the current index unchanged.
      lBox~selectIndex(0)
    end
    else do
      len = searchStr~length
      prefix    = lBox~getText(curIndex)~left(len)
      newPrefix = lBox~getText(newIndex)~left(len)

      -- Only move if the new item is a better match then the
      -- current item.  In other words if the prefixes match,
      -- don't do anything
      if prefix \== newPrefix then do
        lBox~selectIndex(newIndex)
        lBox~makeFirstVisible(newIndex)
        curIndex = newIndex
      end
    end
  end

-- When this method is invoked, the user has not typed a key for whatever
-- the timer period is, we abandon the search string.
::method expired unguarded
  expose searchStr curIndex timer stSearchStr
  searchStr = ''
  curIndex  = 1
  timer     = .nil
  self~setStatusBar

-- Set the timer to the expiration time.  Be sure to cancel the
-- existing timer if it exists or we will get a goofy thing going.
::method resetTimer private unguarded
  expose timer

  reply 0

  if timer \== .nil then timer~cancel

  waitPeriod = .TimeSpan~fromSeconds(self~expiration)
  msgObj     = .Message~new(self, 'expired')
  timer      = .Alarm~new(waitPeriod, msgObj)

-- Reset the incremental search
::method resetSearch private unguarded
  expose searchStr curIndex timer stSearchStr

  if timer \== .nil then timer~cancel

  searchStr = ''
  curIndex  = 1
  timer     = .nil
  self~setStatusBar

-- Test for a Backspace keypress
::method isBkSpc private unguarded
  use strict arg char, isCtrl, misc

  if char == .VK~back then return .true
  return .false

-- Test for a Del keypress
::method isDel private unguarded
  use strict arg char, isCtrl, misc

  if \ isCtrl & misc~pos('extended') <> 0 & char == .VK~delete then return
.true
  return .false

-- Show the search string under the list box.  There is a StatuBar control
-- that is a possible future enhancement to ooDialog.  That is where I would
-- really like to display this.
::method setStatusBar private unguarded
  expose stSearchStr searchStr
  stSearchStr~setText(searchStr)

::method ok unguarded
  expose lb selectedText timer

  -- Be sure to cancel the timer or the dialog won't close until it
  -- does expire.
  if timer \== .nil then timer~cancel
  selectedText = lb~selected
  return self~ok:super

::method cancel unguarded
  expose timer selectedText

  if timer \== .nil then timer~cancel
  selectedText = ''
  return self~cancel:super

I'll probably clean it up and add it to the examples distributed with
ooDialog.

--
Mark Miesfeld
------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Oorexx-users mailing list
Oorexx-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/oorexx-users

Reply via email to