source: trunk/SRC/ToBeReviewed/WIDGET/COMPOUND_WIDGET/cw_bgroup.pro @ 242

Last change on this file since 242 was 242, checked in by pinsard, 17 years ago

improvements/corrections of some *.pro headers + replace some message by some report

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 12.9 KB
Line 
1;+
2;
3; @file_comments
4;
5; @categories
6; Compound widget
7;
8; @param ID
9;
10; @param VALUE
11; It is the default tick mark value (a floating-point number).
12;
13; @returns
14;
15; @uses
16;
17; @restrictions
18;
19; @examples
20;
21; @history
22;
23; @version
24; $Id$
25;-
26;
27PRO cw_bgroup_setv, id, value
28;
29  compile_opt hidden, idl2, strictarrsubs
30;
31  ON_ERROR, 2                       ;return to caller
32
33  stash = WIDGET_INFO(id, /CHILD)
34  WIDGET_CONTROL, stash, GET_UVALUE=state, /NO_COPY
35
36  case state.type of
37    0: message,'unable to set plain button group value'
38    1: begin
39      WIDGET_CONTROL, SET_BUTTON=0, state.ids[state.excl_pos]
40      state.excl_pos = value
41      WIDGET_CONTROL, /SET_BUTTON, state.ids[value]
42    end
43    2: begin
44      n = n_elements(value)-1
45      for i = 0, n do begin
46        state.nonexcl_curpos[i] = value[i]
47        WIDGET_CONTROL, state.ids[i], SET_BUTTON=value[i]
48      endfor
49    end
50  endcase
51
52  WIDGET_CONTROL, stash, SET_UVALUE=state, /NO_COPY
53end
54
55
56
57;+
58; @file_comments
59; Compound widget
60;
61; @categories
62;
63; @param ID
64;
65;
66; @param VALUE
67; It is the default tick mark value (a floating-point number).
68;
69; @returns
70;
71; @uses
72;
73; @restrictions
74;
75; @examples
76;
77; @history
78;
79; @version
80; $Id$
81;-
82function cw_bgroup_getv, id, value
83
84  compile_opt hidden, idl2, strictarrsubs
85  ON_ERROR, 2                       ;return to caller
86
87  stash = WIDGET_INFO(id, /CHILD)
88  WIDGET_CONTROL, stash, GET_UVALUE=state, /NO_COPY
89
90  case state.type of
91    0: message, 'unable to get plain button group value'
92;    1: ret = state.excl_pos
93    1: ret = state.ret_arr[state.excl_pos]
94;    2: ret = state.nonexcl_curpos
95    2: BEGIN
96      index = where(state.nonexcl_curpos NE 0)
97      if index[0] EQ -1 then begin
98        if size(state.ret_arr, /type) EQ 7 then ret = '' ELSE ret = -1
99      ENDIF ELSE ret = state.ret_arr[index]
100    END
101  endcase
102
103  WIDGET_CONTROL, stash, SET_UVALUE=state, /NO_COPY
104
105  return, ret
106
107end
108;
109;+
110;
111; @file_comments
112; Compound widget
113;
114; @categories
115;
116; @param EV
117;
118; @returns
119;
120; @uses
121;
122; @restrictions
123;
124; @examples
125;
126; @history
127;
128; @version
129; $Id$
130;-
131function cw_bgroup_event, ev
132  compile_opt hidden, idl2, strictarrsubs
133  WIDGET_CONTROL, ev.handler, GET_UVALUE=stash
134  WIDGET_CONTROL, stash, GET_UVALUE=state, /NO_COPY
135  WIDGET_CONTROL, ev.id, get_uvalue=uvalue
136
137  ret = 1           ;Assume we return a struct
138  case state.type of
139    0:
140    1: if (ev.select eq 1) then begin
141      state.excl_pos = uvalue
142    ENDIF else begin
143      if (state.no_release ne 0) then ret = 0
144    ENDELSE
145    2: begin
146      ; Keep track of the current state
147      state.nonexcl_curpos[uvalue] = ev.select
148          if (state.no_release ne 0) and (ev.select eq 0) then ret = 0
149    end
150  endcase
151
152  if ret then begin     ;Return a struct?
153      ret = { ID:state.base, TOP:ev.top, HANDLER:0L, SELECT:ev.select, $
154           VALUE:state.ret_arr[uvalue] }
155      efun = state.efun
156      WIDGET_CONTROL, stash, SET_UVALUE=state, /NO_COPY
157      if efun ne '' then return, CALL_FUNCTION(efun, ret) $
158      else return, ret
159  endif else begin      ;Trash the event
160      WIDGET_CONTROL, stash, SET_UVALUE=state, /NO_COPY
161      return, 0
162  endelse
163end
164
165
166
167
168;
169;+
170; @file_comments
171; CW_BGROUP is a compound widget that simplifies creating
172; a base of buttons. It handles the details of creating the
173; proper base (standard, exclusive, or non-exclusive) and filling
174; in the desired buttons. Events for the individual buttons are
175; handled transparently, and a CW_BGROUP event returned. This
176; event can return any one of the following:
177;      - The Index of the button within the base.
178;      - The widget ID of the button.
179;      - The name of the button.
180;      - An arbitrary value taken from an array of User values.
181;
182; @categories
183; Compound widget
184;
185; @param PARENT {in}{required}
186; The ID of the parent widget.
187;
188; @param NAMES {type=string array}
189; A string array, containing one string per button,
190; giving the name of each button.
191;
192; @keyword BUTTON_UVALUE {type=array}
193; An array of user values to be associated with
194; each button and returned in the event structure.
195;
196; @keyword COLUMN
197; Buttons will be arranged in the number of columns
198; specified by this keyword.
199;
200; @keyword EVENT_FUNCT
201; The name of an optional user-supplied event function
202; for buttons. This function is called with the return
203; value structure whenever a button is pressed, and
204; follows the conventions for user-written event functions.
205;
206; @keyword EXCLUSIVE
207; Buttons will be placed in an exclusive base, with
208; only one button allowed to be selected at a time.
209;
210; @keyword FONT
211; The name of the font to be used for the button
212; titles. If this keyword is not specified, the default
213; font is used.
214;
215; @keyword FRAME
216; Specifies the width of the frame to be drawn around the base.
217;
218; @keyword IDS
219; A named variable into which the button IDs will be
220; stored, as a longword vector.
221;
222; @keyword LABEL_LEFT
223; Creates a text label to the left of the buttons.
224;
225; @keyword LABEL_TOP
226; Creates a text label above the buttons.
227;
228; @keyword MAP
229; If set, the base will be mapped when the widget
230; is realized (the default).
231;
232; @keyword NONEXCLUSIVE
233; Buttons will be placed in an non-exclusive base.
234; The buttons will be independent.
235;
236; @keyword NO_RELEASE
237; If set, button release events will not be returned.
238;
239; @keyword RETURN_ID
240; If set, the VALUE field of returned events will be
241; the widget ID of the button.
242;
243; @keyword RETURN_INDEX
244; If set, the VALUE field of returned events will be
245; the zero-based index of the button within the base.
246; THIS IS THE DEFAULT.
247;
248; @keyword RETURN_NAME
249; If set, the VALUE field of returned events will be
250; the name of the button within the base.
251;
252; @keyword ROW
253; Buttons will be arranged in the number of rows
254; specified by this keyword.
255;
256; @keyword SCROLL
257; If set, the base will include scroll bars to allow
258; viewing a large base through a smaller viewport.
259;
260; @keyword SET_VALUE
261; The initial value of the buttons. This is equivalent
262; to the later statement:
263;
264; WIDGET_CONTROL, widget, set_value=value
265;
266; @keyword SPACE
267; The space, in pixels, to be left around the edges
268; of a row or column major base. This keyword is
269; ignored if EXCLUSIVE or NONEXCLUSIVE are specified.
270;
271; @keyword UVALUE
272; The user value to be associated with the widget.
273;
274; @keyword UNAME
275; The user name to be associated with the widget.
276;
277; @keyword XOFFSET
278; The X offset of the widget relative to its parent.
279;
280; @keyword XPAD
281; The horizontal space, in pixels, between children
282; of a row or column major base. Ignored if EXCLUSIVE
283; or NONEXCLUSIVE are specified.
284;
285; @keyword XSIZE
286; The width of the base.
287; @keyword X_SCROLL_SIZE
288; The width of the viewport if SCROLL is specified.
289;
290; @keyword YOFFSET
291; The Y offset of the widget relative to its parent.
292;
293; @keyword YPAD
294; The vertical space, in pixels, between children of
295; a row or column major base. Ignored if EXCLUSIVE
296; or NONEXCLUSIVE are specified.
297;
298; @keyword YSIZE
299; The height of the base.
300;
301; @keyword Y_SCROLL_SIZE
302; The height of the viewport if SCROLL is specified.
303;
304; @returns
305; The ID of the created widget is returned.
306;
307; @restrictions
308; This widget generates event structures with the following definition:
309;
310;       event = { ID:0L, TOP:0L, HANDLER:0L, SELECT:0, VALUE:0 }
311;
312; The SELECT field is passed through from the button event. VALUE is
313; either the INDEX, ID, NAME, or BUTTON_UVALUE of the button,
314; depending on how the widget was created.
315;
316; @restrictions
317; Only buttons with textual names are handled by this widget.
318; Bitmaps are not understood.
319;
320; @history
321; 15 June 1992, AB
322; 7 April 1993, AB, Removed state caching.
323; 6 Oct. 1994, KDB, Font keyword is not applied to the label.
324; 10 FEB 1995, DJC  fixed bad bug in event procedure, getting
325;                         id of stash widget.
326; 11 April 1995, AB Removed Motif special cases.
327;
328; Copyright (c) 1992-2005, Research Systems, Inc.  All rights reserved.
329;   Unauthorized reproduction prohibited.
330;
331; @version
332; $Id$
333;
334;-
335
336
337
338
339
340function cw_bgroup, parent, names, $
341    BUTTON_UVALUE = button_uvalue, COLUMN=column, EVENT_FUNCT = efun, $
342    EXCLUSIVE=excl, FONT=font, FRAME=frame, IDS=ids, LABEL_TOP=label_top, $
343    LABEL_LEFT=label_left, MAP=map, $
344    NONEXCLUSIVE=nonexcl, NO_RELEASE=no_release, RETURN_ID=return_id, $
345    RETURN_INDEX=return_index, RETURN_NAME=return_name, $
346    ROW=row, SCROLL=scroll, SET_VALUE=sval, SPACE=space, $
347    TAB_MODE=tab_mode, UVALUE=uvalue, $
348    XOFFSET=xoffset, XPAD=xpad, XSIZE=xsize, X_SCROLL_SIZE=x_scroll_size,$
349    YOFFSET=yoffset, YPAD=ypad, YSIZE=ysize, Y_SCROLL_SIZE=y_scroll_size, $
350    UNAME=uname
351
352
353  IF (N_PARAMS() ne 2) THEN ras = report('Incorrect number of arguments')
354
355  ON_ERROR, 2                       ;return to caller
356
357  ; Set default values for the keywords
358  version = WIDGET_INFO(/version)
359  if (version.toolkit eq 'OLIT') then def_space_pad = 4 else def_space_pad = 3
360  IF (N_ELEMENTS(column) eq 0)      then column = 0
361  IF (N_ELEMENTS(excl) eq 0)        then excl = 0
362  IF (N_ELEMENTS(frame) eq 0)       then frame = 0
363  IF (N_ELEMENTS(map) eq 0)     then map=1
364  IF (N_ELEMENTS(nonexcl) eq 0)     then nonexcl = 0
365  IF (N_ELEMENTS(no_release) eq 0)  then no_release = 0
366  IF (N_ELEMENTS(row) eq 0)     then row = 0
367  IF (N_ELEMENTS(scroll) eq 0)      then scroll = 0
368  IF (N_ELEMENTS(space) eq 0)       then space = def_space_pad
369  IF (N_ELEMENTS(uname) eq 0)      then uname = 'CW_BGROUP_UNAME'
370  IF (N_ELEMENTS(uvalue) eq 0)      then uvalue = 0
371  IF (N_ELEMENTS(xoffset) eq 0)     then xoffset=0
372  IF (N_ELEMENTS(xpad) eq 0)        then xpad = def_space_pad
373  IF (N_ELEMENTS(xsize) eq 0)       then xsize = 0
374  IF (N_ELEMENTS(x_scroll_size) eq 0)   then x_scroll_size = 0
375  IF (N_ELEMENTS(yoffset) eq 0)     then yoffset=0
376  IF (N_ELEMENTS(ypad) eq 0)        then ypad = def_space_pad
377  IF (N_ELEMENTS(ysize) eq 0)       then ysize = 0
378  IF (N_ELEMENTS(y_scroll_size) eq 0)   then y_scroll_size = 0
379
380
381
382
383  top_base = 0L
384  if (n_elements(label_top) ne 0) then begin
385    next_base = WIDGET_BASE(parent, XOFFSET=xoffset, YOFFSET=yoffset, /COLUMN)
386    if(keyword_set(font))then $
387       junk = WIDGET_LABEL(next_base, value=label_top,font=font) $
388    else    junk = WIDGET_LABEL(next_base, value=label_top)
389    top_base = next_base
390  endif else next_base = parent
391
392  if (n_elements(label_left) ne 0) then begin
393    next_base = WIDGET_BASE(next_base, XOFFSET=xoffset, YOFFSET=yoffset, /ROW)
394    if(keyword_set(font))then $
395       junk = WIDGET_LABEL(next_base, value=label_left, font=font) $
396    else junk = WIDGET_LABEL(next_base, value=label_left)
397    if (top_base eq 0L) then top_base = next_base
398  endif
399  ; We need some kind of outer base to hold the users UVALUE
400  if (top_base eq 0L) then begin
401    top_base = WIDGET_BASE(parent, XOFFSET=xoffset, YOFFSET=yoffset)
402    next_base = top_base
403  endif
404  If (top_base EQ next_base) THEN $
405     next_base = WIDGET_BASE(top_base, Xpad=1, Ypad=1, Space=1)
406
407  ; Set top level base attributes
408  WIDGET_CONTROL, top_base, MAP=map, $
409    FUNC_GET_VALUE='CW_BGROUP_GETV', PRO_SET_VALUE='CW_BGROUP_SETV', $
410    SET_UVALUE=uvalue, SET_UNAME=uname
411
412  ; Tabbing
413  if (n_elements(tab_mode) ne 0) then begin
414    WIDGET_CONTROL, top_base, TAB_MODE=tab_mode
415    WIDGET_CONTROL, next_base, TAB_MODE=tab_mode
416  end
417
418  ; The actual button holding base
419  base = WIDGET_BASE(next_base, COLUMN=column, EXCLUSIVE=excl, FRAME=frame, $
420    NONEXCLUSIVE=nonexcl, ROW=row, SCROLL=scroll, SPACE=space, $
421    XPAD=xpad, XSIZE=xsize, X_SCROLL_SIZE=x_scroll_size, $
422    YPAD=ypad, YSIZE=ysize, Y_SCROLL_SIZE=y_scroll_size, $
423    EVENT_FUNC='CW_BGROUP_EVENT', $
424    UVALUE=WIDGET_INFO(top_base, /child))
425
426
427  n = n_elements(names)
428  ids = lonarr(n)
429  for i = 0, n-1 do begin
430    if (n_elements(font) eq 0) then begin
431      ids[i] = WIDGET_BUTTON(base, value=names[i], UVALUE=i, $
432      UNAME=uname+'_BUTTON'+STRTRIM(i,2))
433    endif else begin
434      ids[i] = WIDGET_BUTTON(base, value=names[i], FONT=font, $
435      UVALUE=i, UNAME=uname+'_BUTTON'+STRTRIM(i,2))
436    endelse
437  endfor
438
439  ; Keep the state info in the real (inner) base UVALUE.
440  ; Pick an event value type:
441  ; 0 - Return ID
442  ; 1 - Return INDEX
443  ; 2 - Return NAME
444  ret_type = 1
445  if KEYWORD_SET(RETURN_ID) then ret_type = 0
446  if KEYWORD_SET(RETURN_NAME) then ret_type = 2
447  if KEYWORD_SET(BUTTON_UVALUE) then ret_type = 3
448    case ret_type of
449      0: ret_arr = ids
450      1: ret_arr = indgen(n)
451      2: ret_arr = names
452      3: ret_arr = button_uvalue
453    endcase
454  type = 0
455  if (excl ne 0) then type = 1
456
457  if (nonexcl ne 0) then type = 2
458  if n_elements(efun) le 0 then efun = ''
459  state = { type:type, $    ; 0-Standard, 1-Exclusive, 2-Non-exclusive
460        base: top_base, $   ; cw_bgroup base...
461        ret_arr:ret_arr, $  ; Vector of event values
462        efun : efun, $  ; Name of event fcn
463        nonexcl_curpos:intarr(n), $ ; If non-exclus, tracks state
464        excl_pos:0, $           ; If exclusive, current button
465        ids:ids, $          ; Ids of buttons
466        no_release:no_release }
467  WIDGET_CONTROL, WIDGET_INFO(top_base, /CHILD), SET_UVALUE=state, /NO_COPY
468
469  if (n_elements(sval) ne 0) then CW_BGROUP_SETV, top_base, sval
470
471  return, top_base
472END
Note: See TracBrowser for help on using the repository browser.