source: trunk/SRC/ToBeReviewed/WIDGET/COMPOUND_WIDGET/cw_calendar.pro

Last change on this file was 495, checked in by pinsard, 10 years ago

fix thanks to coding rules; typo; dupe empty lines; trailing blanks

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 15.4 KB
RevLine 
[371]1;   IDL> testwid,julday(1,1,1980)+lindgen(100)*5
[2]2;
[69]3; PRO testwid_event, event
4;     ComboboxId = widget_info(event.top,find_by_uname = 'c''est lui')
5;    widget_control, event.id, get_uvalue=uval
6;    if n_elements(uval) EQ 0 then return
7;    case uval of
8;       'done':widget_control, event.top, /destroy
[226]9;       'set':BEGIN
[69]10;          widget_control, event.id, get_value = value
11;          widget_control, ComboboxId, set_value = value
12;       END
[226]13;       'get':BEGIN
[69]14;          widget_control, ComboboxId, get_value = value
15;          help,  value, /struct
16;       END
17;       ELSE:
18;    endcase
19;    return
20; end
21; PRO testwid, calendar, date0, _extra = ex
22;    base=widget_base(/COLUMN)
23; print, 'base=', base
24;    nothing = widget_label(base, value = 'beginning of the test')
25; ;
26;    nothing = cw_calendar(base,calendar, date0, _extra = ex, uname = 'c''est lui', uvalue = 'c''est lui' )
27; print, 'cw_calendar ID =', nothing
28; ;
29;    nothing = widget_label(base, value = 'end of the test')
30;     nothing = widget_text(base, value = string(calendar[0]), uvalue = 'set', /editable)
31;     nothing = widget_button(base, value = 'get', uvalue = 'get')
32;     nothing = widget_button(base, value = 'done', uvalue = 'done')
33;    widget_control, base, /REALIZE
34;    xmanager,'testwid', base
35;    return
36; END
[157]37;+
[231]38;
[157]39; @file_comments
[2]40;
[157]41; @categories
42; Compound widget
[226]43;
[157]44; @param ID
45;
46; @param VALUE
47; It is the default tick mark value (a floating-point number).
48;
49; @returns
[226]50;
[157]51; @uses
[226]52;
[157]53; @restrictions
[226]54;
[157]55; @examples
[226]56;
[157]57; @history
[226]58;
59; @version
[157]60; $Id$
[325]61;
[157]62;-
[69]63PRO cw_calendar_set_value, id, value
[2]64;
[226]65  compile_opt strictarr, strictarrsubs
[2]66;
[69]67@cm_4cal
68; get back the calendar and its related informations
69  winfo_id = widget_info(id, find_by_uname = 'infocal')
70  widget_control, winfo_id, get_uvalue = infowid
71  key_caltype = infowid.caltype
[495]72; high frequency calendar
[69]73  IF keyword_set(infowid.fakecal) THEN BEGIN
74    value2 = date2jul(long(value)) - infowid.fakecal
[226]75    IF value2 LT n_elements(infowid.calendar) AND value2 GE 0 THEN BEGIN
[69]76      stepid = widget_info(id, find_by_uname = 'step')
77      widget_control, stepid, set_value = {combobox_select:value2}
78      infowid.date = jul2date(value2 + infowid.fakecal)
79      widget_control, winfo_id, set_uvalue = infowid
80    ENDIF
[226]81  ENDIF ELSE BEGIN
[279]82    jdval = date2jul(value)
[69]83; check that the date exists in the calendar
[279]84    if (where(abs(infowid.calendar - jdval) LT 1.d/86400.d))[0] EQ - 1 then return
[69]85; update the value of infocal
86    infowid.date = value
87    widget_control, winfo_id, set_uvalue = infowid
88; update the combobox if needed...
89    possiblecase = ['day', 'month', 'year']
90    for name = 2, 0, -1 do BEGIN
91; call set_cal_combobox with out = 2 to specify that the call is coming
92; from cw_calendar_set_value
93      if widget_info(id, find_by_uname = possiblecase[name]) NE 0 then $
94         set_cal_combobox, {handler:id, out:2}, possiblecase[name], value
95    ENDFOR
96  ENDELSE
97;
98  return
[2]99end
100;----------------------------------------------------------------------
[157]101;+
102; @file_comments
103;
104; @categories
105; Compound widget
[226]106;
[157]107; @param ID
108;
109; @returns
[226]110;
[157]111; @uses
[226]112;
[157]113; @restrictions
[226]114;
[157]115; @examples
[226]116;
[157]117; @history
[226]118;
119; @version
[157]120; $Id$
[325]121;
[157]122;-
[2]123FUNCTION cw_calendar_get_value, id
[69]124;
[226]125  compile_opt strictarr, strictarrsubs
[69]126;
127  winfo_id = widget_info(id, find_by_uname = 'infocal')
128  widget_control, winfo_id, get_uvalue = infowid
129  return, infowid.date
130END
131;----------------------------------------------------------------------
[157]132;+
133; @file_comments
134;
135; @categories
136; Compound widget
[226]137;
[157]138; @param ID
139;
140; @param WINFOID
141;
142; @returns
[226]143;
[157]144; @uses
[226]145;
[157]146; @restrictions
[226]147;
[157]148; @examples
[226]149;
[157]150; @history
[226]151;
152; @version
[157]153; $Id$
[325]154;
[157]155;-
[226]156FUNCTION get_cal_value, id, winfoid
[69]157;
[226]158  compile_opt strictarr, strictarrsubs
[69]159;
160  winfo_id = widget_info(id, find_by_uname = 'infocal')
161  widget_control, winfo_id, get_uvalue = infowid
162  oldate = infowid.date
163;-------------day-----------------
164  wid_id = widget_info(id, find_by_uname = 'day')
165  if wid_id NE 0 then BEGIN
166    widget_control, wid_id, get_value = wid_value
[279]167    widget_control, wid_id, get_uvalue = wid_uvalue
168    date = double(wid_value.combobox_gettext) + wid_uvalue.hms[wid_value.combobox_index]
[69]169  ENDIF ELSE date = oldate MOD 100L
170;-------------month---------------
171  wid_id = widget_info(id, find_by_uname = 'month')
172  if wid_id NE 0 then BEGIN
173    widget_control, wid_id, get_value = wid_value
174    allmonths = string(format = '(C(CMoA))', 31*(indgen(12)))
[226]175    month = (where(allmonths EQ wid_value.combobox_gettext))[0] + 1
[69]176    date = date + 100L * long(month)
[495]177  ENDIF ELSE date = date + (oldate MOD 10000L)/ 100L * 100L
[69]178;-------------year----------------
179  wid_id = widget_info(id, find_by_uname = 'year')
180  widget_control, wid_id, get_value = wid_value
181  date = date + 10000L * long(wid_value.combobox_gettext)
182;
183  IF arg_present(winfoid) NE 0 THEN BEGIN
184    winfoid = winfo_id
185    infowid.date = date
186    return, infowid
187  ENDIF ELSE return, date
[2]188end
189;----------------------------------------------------------------------
[69]190; redefine the value and index position of the combobox
[157]191;+
192; @file_comments
193;
194; @categories
195; Compound widget
[226]196;
[157]197; @param EVENT
198;
199; @param CASENAME
200;
201; @param DATE0
202;
203; @returns
[226]204;
[157]205; @uses
[226]206;
[327]207; @restrictions
[226]208;
[157]209; @examples
[226]210;
[157]211; @history
[226]212;
213; @version
[157]214; $Id$
[325]215;
[157]216;-
[69]217PRO set_cal_combobox, event, casename, date0
218;
[226]219  compile_opt strictarr, strictarrsubs
[69]220;
221; casename: Which widget shall we move: 'day', 'month' or 'year' ?
222  wid_id = widget_info(event.handler, find_by_uname = casename)
223; we get back the calendar...
224  winfo_id = widget_info(event.handler, find_by_uname = 'infocal')
225  widget_control, winfo_id, get_uvalue = infowid
[279]226  caldat, infowid.calendar, monthcal, daycal, yearcal, hourcal, mincal, seccal
[69]227; ... and the current date
228  IF n_elements(date0) EQ 0 then date0 = get_cal_value(event.handler)
[279]229  jdate0 = date2jul(date0, month = month0, day = day0, year = year0)
[69]230; index of days/months/years according to date0
231  case casename of
[226]232    'day':BEGIN
[69]233; list of days corresponding to month0 and year0
234      index = where(monthcal EQ month0 AND yearcal EQ year0)
235      current = daycal[index]
[279]236      hms = hourcal[index] / 24.0d0 + mincal[index]/1440.0d0 + seccal[index] / 86400.0d0
[69]237    END
[226]238    'month':BEGIN
[69]239; list of months corresponding to year0
240      index = where(yearcal EQ year0)
241      current = monthcal[index]
242; keep only the uniq values
243      indexbis = uniq(current)
244      index = index[indexbis]
245      current = current[indexbis]
246    END
[226]247    'year':BEGIN
[69]248; keep only the uniq years
249      index = uniq(yearcal)
250      current = yearcal[index]
251    END
252  ENDCASE
253; we update the uvalue of the widget
[279]254  IF casename EQ 'day' THEN widget_control, wid_id, set_uvalue = {name:casename, hms:hms}
[69]255; for event.out = 0, we store the previous position of the combobox to use
256; it as the default position.
257  IF event.out EQ 0 THEN widget_control, wid_id, get_value = oldselect
258; we redefine the new list
259  if casename EQ 'month' then begin
260    widget_control, wid_id, set_value = string(format = '(C(CMoA))', 31*(current-1))
[226]261  ENDIF ELSE BEGIN
[69]262    widget_control, wid_id, set_value = strtrim(current, 1)
[2]263  ENDELSE
[69]264; specify the index position within the new list of values.
265  widget_control, wid_id, get_value = combobox
266  CASE event.out OF
267; -1: we put to the biggest position
268    -1:selected = combobox.combobox_number - 1
269; 0: same as the previous position is the best choice...
270    0:selected = oldselect.combobox_index < (combobox.combobox_number - 1)
271; 1: we put to the smallest position
272    1:selected = 0
273; 2: a new date has been specified...
[226]274    2:BEGIN
[69]275      case casename of
276        'day':selected = (where(current EQ day0))[0]
277        'month':selected = (where(current EQ month0))[0]
278        'year':selected = (where(current EQ year0))[0]
279      ENDCASE
280    END
281  ENDCASE
282  widget_control, wid_id, set_value = {combobox_select:selected}
283; update the date...
284  infowid.date = get_cal_value(event.handler)
285  widget_control, winfo_id, set_uvalue = infowid
286  return
[2]287end
288;----------------------------------------------------------------------
[226]289; move cyclically the calendar to the
[69]290; value 0 if event.out=1 or combobox_number-1 if event.out=-1
[157]291;+
292; @file_comments
293;
294; @categories
295; Compound widget
[226]296;
[157]297; @param EVENT
298;
299; @param CASENAME
300;
301; @returns
[226]302;
[157]303; @uses
[226]304;
[157]305; @restrictions
[226]306;
[157]307; @examples
[226]308;
[157]309; @history
[226]310;
311; @version
[157]312; $Id$
[325]313;
[157]314;-
[279]315PRO cw_cal_move, event, casename
[69]316;
[226]317  compile_opt strictarr, strictarrsubs
[69]318;
319   possiblecase = ['day', 'month', 'year', 'impossiblecase']
320   id = widget_info(event.handler, find_by_uname = casename)
321   widget_control, id, get_value = wvalue
322; we try to move but we are already at the beginning/end of the combobox
323; wvalue.combobox_index EQ (wvalue.combobox_number-1) and event.out EQ 1
324; wvalue.combobox_index EQ 0 and event.out EQ -1 (move is not called when out eq 0)
[2]325   whichcase = (where(possiblecase EQ casename))[0]
[69]326   if wvalue.combobox_index EQ (wvalue.combobox_number-1)*(event.out EQ 1) THEN BEGIN
327      if widget_info(event.handler, find_by_uname = possiblecase[whichcase+1]) EQ 0 then begin
328; it is impossible to move the "next" combobox...
329         widget_control, id, get_value = widvalue
330; we set to "widvalue.combobox_number-1" when event.out EQ -1
331; and to 0 when event.out EQ 1
332         selected = (widvalue.combobox_number-1)*(event.out EQ -1)
333         widget_control, id, set_value = {combobox_select:selected}
334; we call move for the next combobox
[279]335      ENDIF ELSE cw_cal_move, event, possiblecase[whichcase+1]
[69]336; it is possible to move from +/- 1
337   ENDIF ELSE widget_control, id, set_value = {combobox_select:wvalue.combobox_index+event.out}
338   set_cal_combobox, event, possiblecase[whichcase-1]
[2]339   return
340end
[327]341
[157]342;+
[327]343;
[157]344; @file_comments
345;
346; @categories
347; Compound widget
[226]348;
[157]349; @param EVENT
350;
351; @returns
[226]352;
[157]353; @uses
[226]354;
[157]355; @restrictions
[226]356;
[157]357; @examples
[226]358;
[157]359; @history
[226]360;
361; @version
[157]362; $Id$
[325]363;
[157]364;-
[2]365FUNCTION cw_calendar_event, event
[69]366;
367@cm_4cal
[226]368  compile_opt strictarr, strictarrsubs
[69]369;
[74]370  winfo_id = widget_info(event.handler, find_by_uname = 'infocal')
[69]371  widget_control, winfo_id, get_uvalue = infowid
372  key_caltype = infowid.caltype
373;
374  widget_control, event.id, get_uvalue = uval
375; high frequency calendar
[226]376  IF uval.name EQ 'step' THEN BEGIN
[69]377    infowid.date = jul2date(event.index + infowid.fakecal)
378  ENDIF ELSE BEGIN
379;
380    possiblecase = ['day', 'month', 'year', 'impossiblecase']
381    whichcase = (where(possiblecase EQ uval.name))[0]
382    if event.out NE 0 then BEGIN ; we use the +/- button and we want to go out of the combobox:
383; to index = -1 (event.out=-1) or to index = combobox_number (event.out=1)
384; we try to move the combobox just right, with name: possiblecase[whichcase+1]
385      if widget_info(event.handler, find_by_uname = possiblecase[whichcase+1]) EQ 0 then BEGIN
[226]386; this widget do not exist we set cyclically the current widget to the
[69]387; value 0 if event.out=1 or combobox_number-1 if event.out=-1
388        widget_control, event.id, get_value = widvalue
389        selected = (widvalue.combobox_number-1)*(event.out EQ -1)
390        widget_control, event.id, set_value = {combobox_select:selected}
[279]391      ENDIF ELSE cw_cal_move, event, possiblecase[whichcase+1]
[69]392    ENDIF
393; if we changed month(year), we need to update the day (and month) list
394    if uval.name NE 'day' then begin
[2]395      event.out = 0
[69]396      for name = whichcase-1, 0, -1 do BEGIN
397        if widget_info(event.handler, find_by_uname = possiblecase[name]) NE 0 then $
398           set_cal_combobox, event, possiblecase[name]
[2]399      endfor
[69]400    ENDIF
401; we update the date
402    infowid = get_cal_value(event.handler, winfo_id)
[226]403  ENDELSE
[69]404;
405  widget_control, winfo_id, set_uvalue = infowid
406  return, {CW_CALENDAR, ID:event.handler, TOP:event.top, HANDLER:0L $
407           , VALUE:infowid.date, FAKECAL: infowid.fakecal}
[2]408end
[327]409;
[150]410;+
[327]411;
[150]412; @file_comments
413;
414; @categories
[157]415; Compound widget
[226]416;
[150]417; @param PARENT {in}{required}
418; The widget ID of the parent widget.
419;
420; @param CALENDAR
421;
422; @param JDATE0
423;
424; @keyword CALTYPE
[226]425;
[150]426; @keyword FAKECAL
427;
428; @keyword UVALUE
429;
430; @keyword UNAME
431;
432; @keyword _EXTRA
[231]433; Used to pass keywords
[226]434;
[150]435; @returns
[226]436;
[150]437; @uses
[226]438;
[150]439; @restrictions
[226]440;
[150]441; @examples
[226]442;
[150]443; @history
[226]444;
445; @version
[150]446; $Id$
447;
448; @todo
449; seb
[226]450;
[150]451;-
[327]452FUNCTION cw_calendar, parent, calendar, jdate0 $
453                    , CALTYPE=CALTYPE, FAKECAL=fakecal $
454                    , UVALUE=uvalue, UNAME=uname, _EXTRA=ex
[232]455;
[69]456@cm_4cal
457;
[226]458  compile_opt strictarr, strictarrsubs
[69]459;
460  if keyword_set(caltype) then key_caltype = caltype
461; months days years found in the calendar
[279]462  caldat, calendar, monthcal, daycal, yearcal, hourcal, mincal, seccal
463  hmscal = hourcal / 24.0d0 + mincal/1440.0d0 + seccal / 86400.0d0
[69]464; starting date
465  if n_elements(jdate0) EQ 0 then jdate0 = calendar[0]
466  if (where(calendar EQ jdate0))[0] EQ -1 then jdate0 = calendar[0]
[74]467;
[69]468  caldat, jdate0, month0, day0, year0
469; test the type of calendar
470  if n_elements(calendar) GT 1 then BEGIN
471; each day have the same value
472    if n_elements(uniq(daycal, sort(daycal))) EQ 1 then monthly = 1
473; each month and each day have the same value
474    if keyword_set(monthly) AND n_elements(uniq(monthcal, sort(monthcal))) EQ 1 then yearly = 1
475  endif
[2]476;---------------------------------------------------------------------------------------
[69]477  if NOT keyword_set(uvalue) then uvalue = {dummy:''}
478  if NOT keyword_set(uname) then uname = ''
479  base0 = widget_base(parent, /ROW $
[2]480                      , EVENT_FUNC = 'cw_calendar_event' $
[69]481                      , FUNC_GET_VALUE = 'cw_calendar_get_value' $
482                      , PRO_SET_VALUE = 'cw_calendar_set_value' $
483                      , UVALUE = uvalue, UNAME = uname, space = 0, _extra = ex)
484;
485
486  if n_elements(fakecal) eq 0 then fakecal = 0
487  base = widget_base(base0, space = 0, uname = 'infocal' $
488                     , uvalue = {calendar:calendar, date:jul2date(jdate0), fakecal:fakecal, caltype: key_caltype})
489;
[226]490  IF keyword_set(fakecal) THEN BEGIN
[69]491    cmbbid = cw_combobox_pm(base, UVALUE = {name:'step'}, UNAME = 'step' $
492                            , value = strtrim(indgen(n_elements(calendar)), 1))
493    widget_control, cmbbid, set_value = {combobox_select:(where(calendar EQ jdate0))[0]}
[226]494  ENDIF ELSE BEGIN
[69]495;
496    vallen = widget_info(base, string_size = 'm')
497;-------------day-----------------
498    if NOT keyword_set(monthly)  then begin
499      dayindex = where(monthcal EQ month0 AND yearcal EQ year0)
[2]500      currentday = daycal[dayindex]
[69]501      currentday = strtrim(currentday, 1)
[279]502      hms = hmscal[dayindex]
503      cmbbid = cw_combobox_pm(base, UVALUE = {name:'day', hms:hms}, UNAME = 'day', value = currentday)
[69]504      widget_control, cmbbid, set_value = {combobox_select:(where(long(currentday) EQ day0))[0]}
505    endif
506;-------------month---------------
507    if NOT keyword_set(yearly)  then BEGIN
508      monthindex = where(yearcal EQ year0)
[2]509      currentmonth = long(monthcal[monthindex])
[495]510; we suppress the repeated months
[2]511      monthindexbis = uniq(currentmonth, sort(currentmonth))
512      monthindex = monthindex[monthindexbis]
513      currentmonth = currentmonth[monthindexbis]
[69]514      xoff = (34 + 2*vallen[0])*(1-keyword_set(monthly))
515      cmbbid = cw_combobox_pm(base, UVALUE = {name:'month'}, UNAME = 'month', value = string(format = '(C(CMoA))', 31*(currentmonth-1)), xoffset = xoff)
516      widget_control, cmbbid, set_value = {combobox_select:(where(long(currentmonth) EQ month0))[0]}
517    endif
[2]518;-------------year----------------
[69]519    yearindex = uniq(yearcal, sort(yearcal))
520    currentyear = strtrim(yearcal[yearindex], 1)
521    xoff = (34 + 2*vallen[0])*(1-keyword_set(monthly)) + (33 + 3*vallen[0])*(1-keyword_set(yearly))
522    cmbbid = cw_combobox_pm(base, UVALUE = {name:'year'}, UNAME = 'year', value = currentyear, xoffset = xoff)
523    widget_control, cmbbid, set_value = {combobox_select:(where(long(currentyear) EQ year0))[0]}
[2]524;-----------------------------
[69]525  ENDELSE
[2]526;
[69]527  return, base
[2]528end
Note: See TracBrowser for help on using the repository browser.