; docformat = 'rst' ;+ ; Modal dialog to present a user with a choice of a few items. ;- ;+ ; Event handler for the dialog. ; ; :Params: ; event : in, required, type=structure ; event structure for the droplist and OK/Cancel buttons ;- pro mg_dialog_list_event, event compile_opt strictarr widget_control, event.top, get_uvalue=pstate uname = widget_info(event.id, /uname) case uname of 'choiceDroplist': (*pstate).choice = event.index 'cancel': widget_control, event.top, /destroy 'ok': begin (*pstate).cancelled = 0B widget_control, event.top, /destroy end endcase end ;+ ; Create a modal dialog to allow the user to select from some choices. ; ; :Returns: ; long, index into choices parameter where -1 indicates the user quit ; ; :Params: ; choices : in, required, type=strarr ; string array of choices ; text : in, required, type=string ; message to user explaining the choice ; ; :Keywords: ; dialog_parent : in, optional, type=long ; widget identifier of the parent widget for the dialog ; title : in, optional, type=string ; title to appear in title bar of the dialog's window ;- function mg_dialog_list, choices, text, dialog_parent=parent, title=title compile_opt strictarr _parent = n_elements(parent) eq 0L ? widget_base(map=0) : parent tlb = widget_base(title=title, /column, /base_align_center, $ group_leader=_parent, /modal) choicesBase = widget_base(tlb, /row) labelWidget = widget_label(choicesBase, value=text) choiceDroplist = widget_droplist(choicesBase, value=choices, $ uname='choiceDroplist') xsize = 75 buttonBase = widget_base(tlb, /row) cancelButton = widget_button(buttonBase, value='Cancel', scr_xsize=xsize, $ uname='cancel') okButton = widget_button(buttonBase, value='OK', scr_xsize=xsize, $ uname='ok') widget_control, tlb, /realize state = { choice: 0L, cancelled: 1B } pstate = ptr_new(state, /no_copy) widget_control, tlb, set_uvalue=pstate xmanager, 'mg_dialog_list', tlb, event_handler='mg_dialog_list_event' ; cleanup if (n_elements(parent) eq 0L) then widget_control, _parent, /destroy choice = (*pstate).choice cancelled = (*pstate).cancelled ptr_free, pstate return, cancelled ? -1L : choice end ; main-level example program choice = mg_dialog_list(['A', 'B', 'C'], $ 'Select the correct answer:', $ title='Multiple choice') help, choice end