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

Last change on this file since 279 was 279, checked in by smasson, 17 years ago

bugfix when using double precision calendar

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