The following procedures build a simple application that allows the user to select data from a table, plotting the data in a draw window and optionally displaying the data values in a text widget. The user can switch the table between standard and disjoint selection modes.
This example is included in the file table_widget_example1.pro in the examples/doc/widgets subdirectory of the IDL distribution. Run this example procedure by entering table_widget_example1 at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT table_widget_example1.pro. See Running the Example Code if IDL does not run the program as expected.
PRO table_widget_example1_event, ev
WIDGET_CONTROL, ev.top, GET_UVALUE=stash
disjoint = WIDGET_INFO(stash.table, /TABLE_DISJOINT_SELECTION)
selection = WIDGET_INFO(stash.table, /TABLE_SELECT)
IF (selection[0] ne -1) THEN hasSelection = 1 $
ELSE hasSelection = 0
IF (hasSelection) THEN WIDGET_CONTROL, stash.table, $ GET_VALUE=value, /USE_TABLE_SELECT
IF ((ev.ID eq stash.table) AND hasSelection) THEN BEGIN
WSET, stash.draw
PLOT, value
ENDIF
IF ((ev.ID eq stash.b_value) AND hasSelection) THEN BEGIN
IF (disjoint eq 0) THEN BEGIN
WIDGET_CONTROL, stash.text, SET_VALUE=STRING(value, /PRINT)
ENDIF ELSE BEGIN
WIDGET_CONTROL, stash.text, SET_VALUE=STRING(value)
ENDELSE
ENDIF
IF ((ev.ID eq stash.b_select) AND hasSelection) THEN BEGIN
IF (disjoint eq 0) THEN BEGIN
list0 = 'Standard Selection'
list1 = 'Left: ' + STRING(selection[0])
list2 = 'Top: ' + STRING(selection[1])
list3 = 'Right: ' + STRING(selection[2])
list4 = 'Bottom: ' + STRING(selection[3])
list = [list0, list1, list2, list3, list4]
ENDIF ELSE BEGIN
n = N_ELEMENTS(selection)
list = STRARR(n/2+1)
list[0] = 'Disjoint Selection'
FOR j=0,n-1,2 DO BEGIN
list[j/2+1] = 'Column: ' + STRING(selection[j]) + $
', Row: ' + STRING(selection[j+1])
ENDFOR
ENDELSE
WIDGET_CONTROL, stash.text, SET_VALUE=list
ENDIF
IF (ev.ID eq stash.b_change) THEN BEGIN
IF (disjoint eq 0) THEN BEGIN
WIDGET_CONTROL, stash.table, TABLE_DISJOINT_SELECTION=1
WIDGET_CONTROL, stash.b_change, $
SET_VALUE='Change to Standard Selection Mode'
ENDIF ELSE BEGIN
WIDGET_CONTROL, stash.table, TABLE_DISJOINT_SELECTION=0
WIDGET_CONTROL, stash.b_change, $
SET_VALUE='Change to Disjoint Selection Mode'
ENDELSE
ENDIF
IF (ev.ID eq stash.b_quit) THEN WIDGET_CONTROL, ev.TOP, /DESTROY
END
PRO table_widget_example1
data = DIST(7)
help = ['Select data from the table below using the mouse.']
base = WIDGET_BASE(/COLUMN)
subbase1 = WIDGET_BASE(base, /ROW)
draw = WIDGET_DRAW(subbase1, XSIZE=250, YSIZE=250)
subbase2 = WIDGET_BASE(subbase1, /COLUMN)
text = WIDGET_text(subbase2, XS=50, YS=8, VALUE=help, /SCROLL)
b_value = WIDGET_BUTTON(subbase2, VALUE='Show Selected Data')
b_select = WIDGET_BUTTON(subbase2, VALUE='Show Selected Cells')
b_change = WIDGET_BUTTON(subbase2, $
VALUE='Change to Disjoint Selection Mode')
b_quit = WIDGET_BUTTON(subbase2, VALUE='Quit')
table = WIDGET_TABLE(base, VALUE=data, /ALL_EVENTS)
WIDGET_CONTROL, base, /REALIZE
WIDGET_CONTROL, draw, GET_VALUE=drawID
stash = {draw:drawID, table:table, text:text, b_value:b_value, $
b_select:b_select, b_change:b_change, b_quit:b_quit}
WIDGET_CONTROL, base, SET_UVALUE=stash
XMANAGER, 'table_widget_example1', base
END
The following things about this example are worth noting:
- It is important to check whether a selection exists before using WIDGET_CONTROL to retrieve the selection.
- Data from disjoint selections is handled differently than data from standard selections.
- For a relatively simple application, passing a group of widget IDs via the top-level base widget’s user value allows using a single event routine rather than separate event routines for each widget.