X

Help Articles are product support tips and information straight from the NV5 Geospatial Technical Support team developed to help you use our products to their fullest potential.



3781 Rate this article:
No rating

A GUI-based User Prompt Function

Topic
The IDL language has two built-in routines designed to facilitate the task of inviting one-line text entries: 

a) READ, which turns the IDL> command line into a prompt for user entries, and 

b) CW_FIELD, which is a user prompt component that can be inserted into other widget programs. 

The READ approach has a bit of a primitive look to it, and limited user-friendliness, while using CW_FIELD requires a knowledge of widget programming, which many IDL programmers may not have. This Help Article presents a simple utility for inviting one-line user text entries, which can be called with just one line of code from either non-GUI or GUI-based programs. The code for this utility can either be copied and pasted from the text below, or downloaded from the link right above.

Discussion
'gui_prompt.pro' is essentially a top-level WIDGET_BASE wrapper and event handler for IDL's built-in CW_FIELD function. It allows CW_FIELD to be a stand-alone prompting tool. With syntax like:

    userEntry = GUI_PROMPT('Enter value here: ', $
        TITLE='Input any real number', $
        XSIZE=30, XOFFSET=100, YOFFSET=100, $
        VALIDATE_TYPE=size(0.0, /TYPE))
    ; The VALIDATE_TYPE syntax above is for real numbers

it will bring up on the user's screen a small GUI like the one below.

gui_prompt.png

This dialog box will block further IDL processing until the user presses the 'Enter' key or hits the dialog window close button. Upon closing, the function returns the user's entry as a string datatype.

The source code for this function is displayed below, or you can download the code via this link: 'gui_prompt.pro'.

Solution

; The event handler
PRO gui_prompt_event, event
widget_control, event.id, GET_VALUE=userEntry, GET_UVALUE=pReturnValue
*pReturnValue = userEntry
widget_control, event.top, /DESTROY
END

; The widget creation code
FUNCTION gui_prompt, promptText, $
    TITLE=windowTitle, VALIDATE_TYPE=datatype, XSIZE=nCharacters, $
    XOFFSET=xoffset, YOFFSET=yoffset, _EXTRA=_extra
if n_elements(promptText) eq 0 then return, ''
; Optional keyword for validating user entries. The values are based
; on IDL type ID's as seen in table in Online Help for SIZE function.
if keyword_set(datatype) then begin
    switch datatype of
    1:    ; Integer types
    2:
    3:
    12:
    13:
    14:
    15: begin
        validateLong = 1
        break
    end
    4:    ; floating-point types
    5: begin
        validateReal = 1
        break
    end
    else: returnValue = ''    ; default type is /STRING
    endswitch
endif
if n_elements(windowTitle) eq 0 then winTitle = 'Entry Form'
device, GET_SCREEN_SIZE=displayDims
; By default, center the prompt (approximately) on the display
if n_elements(xoffset) eq 0 then xoffset = displayDims[0] / 2
if n_elements(yoffset) eq 0 then yoffset = displayDims[1] / 2
; By default, set the entry textbox width to 20
if n_elements(nCharacters) eq 0 then nCharacters = 20
tlb = widget_base(TITLE=windowTitle, XOFFSET=xoffset, YOFFSET=yoffset)
pReturnValue = ptr_new(/ALLOCATE_HEAP)
wPromptBox = cw_field(tlb, TITLE=promptText, FLOATING=validateReal, $
    LONG=validateLong, UVALUE=pReturnValue, $
    /RETURN_EVENTS, XSIZE=nCharacters, _EXTRA=_extra)
widget_control, tlb, /REALIZE
xmanager, 'gui_prompt', tlb
if n_elements(*pReturnValue) eq 0 $
    then returnValue='' $
else $
    returnValue = strtrim(*pReturnValue, 2)
ptr_free, pReturnValue
return, returnValue
END
Please login or register to post comments.
Featured

End-of-Life Policy Enforcement for ENVI 5.3 / IDL 8.5 and Earlier Versions

5/6/2024

April 1, 2024 Dear ENVI/IDL Customer,  We are reaching out to notify you of our supported... more »

How to Upgrade licenses to ENVI 6.x / IDL 9.x

12/5/2023

What is the new Upgrade function? Starting with ENVI 6.0 and IDL 9.0, we have implemented an... more »

What to do if the 'License Administrator - License Server' for the Next-Generation License Server does not start?

6/13/2023

Background: With the release of ENVI 5.7 & IDL 8.9 and the corresponding Next-Generation licensing... more »

Next-Generation Licensing FAQ

4/28/2023

  NV5 Geospatial has adopted a new licensing technology for all future releases of our ENVI, IDL... more »

The IDL Virtual Machine

6/6/2013

What is the IDL Virtual Machine? An IDL Virtual Machine is a runtime version of IDL that can... more »