xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

AChoice()

Displays a list of items to select one from.

Syntax

AChoice( [<nTop>]       , ;
         [<nLeft>]      , ;
         [<nBottom>]    , ;
         [<nRight>]     , ;
         <acItems>      , ;
         [<aSelectable>], ;
         [<cUserFunc>]  , ;
         [<nFirstItem>] , ;
         [<nFirstRow>]    ) --> nSelectedItem

Arguments

<nTop> and <nLeft>
Numeric values indicating the screen coordinates for the upper left corner of the AChoice() window. The default value for both parameters is zero.
<nBottom>
Numeric value indicating the bottom row screen coordinate. It defaults to MaxRow().
<nRight>
Numeric value indicating the right column screen coordinate. It defaults to MaxCol().
<acItems>
A one dimensional array containing characters strings must be passed for <acItems>. It represents a list of items to choose one from. The character strings are displayed in the AChoice() window.

Note:  No element in <acItems> may contain a null-string (""). A null-string is treated as End-of-list marker.

<aSelectable>
This parameter is optional. Either a logical value or an array containing logical values or character strings can be specified. It defaults to .T. (true) which means that all items in <acItems> can be selected. If .F. (false) is specified for <aSelectable>, the Ahoice() window is displayed but the function returns immediately, allowing no selection.

When an array is passed, it must have at least the same number of elements as <acItems> and is treated as parallel array indicating individual elements that can be selected. If an element of <aSelectable> contains .T. (true), the corresponding item in <acItems> is selectable, otherwise it is displayed but not selectable.

As an alternative, elements in <aSelectable> may contain character strings. They are treated as macro-expressions and are evaluated with the macro-operator. The result of the macro-expression must be a logical value.

<cUserFunc>
This is an optional character string holding the symbolic name of a user-defined function. The user function is called each time a key is pressed while AChoice() is active. The return value of the user function influences the AChoice() behavior. Note that the user function must be specified without parentheses, only the symbolic name may be passed.
<nFirstItem>
Optionally, the first item to start the selection with can be specified as a numeric value. It indicates the ordinal position of the element in <acItems> and defaults to 1, the first element.
<nFirstRow>
<nFirstRow> specifies the offset from the first window row to position the hilight bar in at initial display. It defaults to zero, i.e. the highlight bar is displayed in the <nTop> row when AChoice() begins.

Return

AChoice() returns a numeric value. It indicates the position of the selcted item in <acItems>. When the user makes no selection, the return value is zero.

Description

AChoice() is a console window function that presents to the user a list of items to select one from. The function maintains a window region and displays in it the items specified as the <acItems> array containing character strings. The colors for display are taken from the current SetColor() string. That is, normal items are displayed in Standard color, the highlight in Enhanced color and unselectable items with the Unselected color

The user can navigate through the list using cursor keys. AChoice() automatically scrolls the list within the defined window region if the user attempts to navigate the highlight bar outside the window. The user selects an item by pressing the Return key or by double clicking an item, and aborts selection with the Esc key.

Direct navigation to an item as accomplished by pressing letter keys. In this case, AChoice() moves the highlight to the next item whose first letter matches the pressed key.

Navigation without user function

AChoice() recognizes in its default navigation mode the keys listed in following table. The default mode is present when no user function <cUserFunc> is specified.

AChoice() default navigation
KeyNavigation
Up arrowGo to previous item
Down arrowGo to next item
HomeGo to first item in menu
EndGo to last item in menu
Ctrl+HomeGo to first item in window
Ctrl+EndGo to last item in window
PgUpGo to previous page
PgDnGo to next page
Ctrl+PgUpGo to the first item in menu
Ctrl+PgDnGo to last item in menu
ReturnSelect current item
EscAbort selection
Left arrowAbort selection
Right arrowAbort selection
 
First letterGo to next item beginning with first letter
 
Left click *)Go to clicked item
Left double click *)Select clicked item

Navigation with user function

AChoice() processes some very basic navigation keys automatically when a user function is specified. This nvigation occurs before the user function is called. All keys not listed in the following table are passed on to the user function which may instruct AChoice() to take appropriate action.

AChoice() restricted navigation
KeyNavigation
Down arrowGo to next item
Ctrl+HomeGo to first item in window
Ctrl+EndGo to last item in window
PgUpGo to previous page
PgDnGo to next page
Ctrl+PgUpGo to the first item in menu
Ctrl+PgDnGo to last item in menu
 
Left click *)Go to clicked item

After default action is taken, AChoice() invokes the user function, passing three parameters for further processing:

1) The current AChoice() mode (see table below).

2) The current element in the array of items.

3) The relative row position of the highlight bar within the window region.

AChoice() modes passed to the user function indicate the internal state. They are encoded as numeric values for which #define constants exist in the ACHOICE.CH file.

AChoice() modes
ConstantModeDescription
AC_IDLE0Idle
AC_HITTOP1Attempt to move cursor past top of list
AC_HITBOTTOM2Attempt to move cursor past bottom of list
AC_EXCEPT3Unknown key pressed
AC_NOITEM4No selectable items

The user function can process the passed parameters. It then instructs AChoice() how to proceed by returning one of the following numeric values. All user-function return values are also available as #define constants.

User-function return values for AChoice()
ConstantValueAction
AC_ABORT0Abort selection
AC_SELECT1Make selection
AC_CONT2Continue AChoice()
AC_GOTO3Go to the next item whose first character matches the key pressed

Info

See also:@...PROMPT, DbEdit(), MENU TO, TBrowseNew()
Category: Array functions , UI functions
Header:achoice.ch
Source:rtl/achoice.prg
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example uses AChoice() to display names of files in the current
// directory. Only files with the TXT or PRG extension can be selected.
// A selected file is displayed using MemoEdit(). Note how the return
// value of the user function triggers the AChoice() action.

   #include "Achoice.ch"
   #include "Inkey.ch"

   MEMVAR  aFiles

   PROCEDURE Main
      LOCAL nSelect
      LOCAL aItems  := Directory()
      LOCAL i, imax := Len( aItems )
      LOCAL aSelect := Array( imax )
      LOCAL cScreen

      SetColor( "W/N" )
      CLS
      SetColor( "N/BG,W+/B,,,W/BG" )

      PRIVATE aFiles := AClone( aItems )

      FOR i:=1 TO imax
         aItems[i]  := Upper( aItems[i,1] )
         aSelect[i] := ( ".TXT" $ aItems[i] .OR. ;
                         ".PRG" $ aItems[i] )
      NEXT

      DO WHILE LastKey() <> K_ESC
         nSelect := Achoice( 2, 2, MaxRow()-5, 30, ;
                             aItems, aSelect, "USERFUNC" )

         IF nSelect <> 0
            // Display selected file
            SAVE SCREEN TO cScreen
            MemoEdit( MemoRead( aItems[nSelect] ) )
            RESTORE SCREEN FROM cScreen
            KEYBOARD Chr(255)      // sets Lastkey() to 255
            Inkey()
         ENDIF

      ENDDO
   RETURN


   FUNCTION UserFunc( nMode, nElement, nRow )
      LOCAL nKey := LastKey()
      LOCAL nRet := AC_CONT
      LOCAL cMsg

      DO CASE
      CASE nMode == AC_IDLE
         // do some idle processing
         cMsg := " File: " +       aFiles[nElement,1]   + ;
                 " Size: " +  Str( aFiles[nElement,2] ) + ;
                 " Date: " + DtoC( aFiles[nElement,3] ) + ;
                 " Time: " +       aFiles[nElement,4]

         DispOutAt( MaxRow(), 0, Padr(cMsg, MaxCol()+1), "W+/R" )

      CASE nMode == AC_EXCEPT
         // key handling for unknown keys
         IF nKey == K_ESC
            nRet := AC_ABORT

         ELSEIF nKey == K_RETURN .OR. nKey == K_LDBLCLK
            nRet := AC_SELECT

         ELSEIF nKey > 31 .AND. nKey < 255
            nRet := AC_GOTO

         ENDIF
      ENDCASE

   RETURN nRet

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe