;------------------------------------------------------------- ;+ ; NAME: ; CW_XYSEL ; ; PURPOSE: ; creates a list and 3 buttons that allow to add a string, ; remove a string or remove all strings from the list ; Only the add event is passed back, all other events are ; handled internally. To remove a certain item manually, ; a procedure named xysel_remove_value has been added. ; Th edefault set_value and get_value procedures are overwritten. ; ; CATEGORY: ; Compound widgets. ; ; CALLING SEQUENCE: ; widget = CW_XYSEL(parent, [keywords]) ; ; INPUTS: ; PARENT --> widget ID of the parent widget ; ; KEYWORD PARAMETERS: ; UVALUE --> user value (not really used) ; ; VALUE --> initial entries of the list (string array) ; ; SELECT --> initial selection of the list ; ; TITLE - Supplies a title of the list (usually 'X' or 'Y') ; ; OUTPUTS: ; cw_xysel returns a widget ID which should be remembered to ; handle future events. ; ; SUBROUTINES: ; FUNCTION is_empty_xysel,entries ; checks if list is empty ; ; PRO xysel_set_value, id, newvalue ; adds a value to the list (this procedure should be ; called when an 'ADD' event is detected in the caller ; program. ; ; FUNCTION xysel_get_value, id ; returns all entries from the list ; ; PRO xysel_remove_value, id, newvalue ; removes the entry VALUE from the list if it exists. ; ; PRO xysel_change_value, id, oldvalue, newvalue ; changes the entry OLDVALUE to NEWVALUE if it exists. ; ; FUNCTION xysel_event, event ; handles internal events (REMOVE, REMOVE_ALL) ; ; REQUIREMENTS: ; ; NOTES: ; used in EXPLORE ; ; EXAMPLE: ; ; MODIFICATION HISTORY: ; mgs, 01 Dec 1997: VERSION 1.00 ; ;- ; Copyright (C) 1997, Martin Schultz, Harvard University ; This software is provided as is without any warranty ; whatsoever. It may be freely used, copied or distributed ; for non-commercial purposes. This copyright notice must be ; kept with any copy of this software. If this software shall ; be used commercially or sold as part of a larger package, ; please contact the author to arrange payment. ; Bugs and comments should be directed to mgs@io.harvard.edu ; with subject "IDL routine cw_xysel" ;------------------------------------------------------------- FUNCTION is_empty_xysel,entries test = 1 ; assume list is empty if(n_elements(entries) gt 1) then test = 0 $ else if(entries(0) ne '') then test = 0 return,test end PRO xysel_set_value, id, newvalue ; ATTENTION : this routine does not SET the value, but simply adds a ; value to the list ! ; Return to caller. ON_ERROR, 2 ; Retrieve the state. stash = WIDGET_INFO(id, /CHILD) WIDGET_CONTROL, stash, GET_UVALUE=state, /NO_COPY ; Set the value here. widget_control,state.listID,get_uvalue=entries if(is_empty_xysel(entries)) then entries=newvalue $ else entries = [entries, newvalue] widget_control,state.listID,set_uvalue=entries,set_value=entries ; Restore the state. WIDGET_CONTROL, stash, SET_UVALUE=state, /NO_COPY END FUNCTION xysel_get_value, id ; return the string array that makes up the list ; Return to caller. ON_ERROR, 2 ; Retrieve the structure from the child that contains the sub ids. stash = WIDGET_INFO(id, /CHILD) WIDGET_CONTROL, stash, GET_UVALUE=state, /NO_COPY ; Get the value here widget_control,state.listID,get_uvalue=value ; Restore the state. WIDGET_CONTROL, stash, SET_UVALUE=state, /NO_COPY ; Return the value here. return,value END PRO xysel_remove_value, id, value ; NOTE: this routine is NOT part of the event mechanism but influences ; the appearance of the list ; Return to caller. ON_ERROR, 2 ; Retrieve the state. stash = WIDGET_INFO(id, /CHILD) WIDGET_CONTROL, stash, GET_UVALUE=state ; Get all current entries widget_control,state.listID,get_uvalue=entries if(is_empty_xysel(entries)) then return ind = where(entries eq value) if (ind(0) lt 0) then return ; nothing to remove ; found entry - remove from list invind = inv_index(ind,n_elements(entries)) if (invind(0) ge 0) then $ entries = entries(invind) $ else $ entries = '' widget_control,state.listID,set_uvalue=entries,set_value=entries ; Restore the state. WIDGET_CONTROL, stash, SET_UVALUE=state END PRO xysel_change_value, id, oldvalue, newvalue ; NOTE: this routine is NOT part of the event mechanism but influences ; the appearance of the list ; Return to caller. ON_ERROR, 2 ; Retrieve the state. stash = WIDGET_INFO(id, /CHILD) WIDGET_CONTROL, stash, GET_UVALUE=state ; Get all current entries widget_control,state.listID,get_uvalue=entries if(is_empty_xysel(entries)) then return ind = where(entries eq oldvalue) if (ind(0) lt 0) then return ; nothing to change ; found entry - change its name (may be multiple !) entries(ind) = newvalue widget_control,state.listID,set_uvalue=entries,set_value=entries ; Restore the state. WIDGET_CONTROL, stash, SET_UVALUE=state END ;----------------------------------------------------------------------------- FUNCTION xysel_event, event parent=event.handler ; unless passevent=1, the event needs not to be passed back passevent = 0 ; Retrieve the structure from the child that contains the sub ids. stash = WIDGET_INFO(parent, /CHILD) WIDGET_CONTROL, stash, GET_UVALUE=state, /NO_COPY if(event.id eq state.bID) then begin ; button pressed if(event.value eq 0) then passevent = 1 ; add one item if(event.value eq 1) then begin ; remove selected item widget_control,state.listID,get_uvalue=entries select = widget_info(state.listID,/list_select) if(not is_empty_xysel(entries)) then begin keepind = indgen(n_elements(entries)) rmind = where(keepind ne select, c) if(c gt 0) then begin keepind = keepind(rmind) entries = entries(keepind) endif else entries = '' select = -1 widget_control,state.listID,set_value=entries, $ set_uvalue=entries,set_list_select=select endif endif if(event.value eq 2) then begin ; clear list entries = '' select = -1 widget_control,state.listID,set_value=entries, $ set_uvalue=entries, set_list_select = select endif endif if(event.id eq state.listID) then begin ; item selected ; select = widget_info(state.listID,/list_select) endif ; Restore the state structure WIDGET_CONTROL, stash, SET_UVALUE=state, /NO_COPY if(passevent) then $ return, { ID:parent, TOP:event.top, HANDLER:0L, VALUE:event.value } $ else return,0 END ;----------------------------------------------------------------------------- FUNCTION cw_xysel, parent, UVALUE=uval, VALUE=entries, SELECT=select, $ TITLE=title IF (N_PARAMS() EQ 0) THEN MESSAGE, 'Must specify a parent for Cw_xysel' ON_ERROR, 2 ;return to caller ; Defaults for keywords IF NOT (KEYWORD_SET(uval)) THEN uval = 0 if (not keyword_set(entries)) then entries = '' if (n_elements(select) eq 0) then select = -1 if (not keyword_set(title)) then title = ' ' ; make sure selection is valid if (select ge 0) then $ if (is_empty_xysel(entries)) then select = -1 base = WIDGET_BASE(parent, UVALUE = uval, $ frame = 3, /row, $ EVENT_FUNC = "xysel_event", $ FUNC_GET_VALUE = "xysel_get_value", $ PRO_SET_VALUE = "xysel_set_value") xybuttons = cw_bgroup(base,/column,[' > ',' < ',' << ']) xylist = widget_list(base, xsize=20,ysize=7,value=entries,uvalue=entries) xylabel = widget_label(base,value=' '+title+' ') widget_control,xylist,set_list_select=select state = { bID:xybuttons, listID:xylist } ; Save out the initial state structure into the first childs UVALUE. WIDGET_CONTROL, WIDGET_INFO(base, /CHILD), SET_UVALUE=state, /NO_COPY RETURN, base END