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

Last change on this file since 378 was 371, checked in by pinsard, 16 years ago

improvements of headers (alignments of IDL prompt in examples)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 15.4 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; @param CASENAME
208;
209; @param DATE0
210;
211; @returns
212;
213; @uses
214;
215; @restrictions
216;
217; @examples
218;
219; @history
220;
221; @version
222; $Id$
223;
224;-
225PRO set_cal_combobox, event, casename, date0
226;
227  compile_opt strictarr, strictarrsubs
228;
229; casename: Which widget shall we move: 'day', 'month' or 'year' ?
230  wid_id = widget_info(event.handler, find_by_uname = casename)
231; we get back the calendar...
232  winfo_id = widget_info(event.handler, find_by_uname = 'infocal')
233  widget_control, winfo_id, get_uvalue = infowid
234  caldat, infowid.calendar, monthcal, daycal, yearcal, hourcal, mincal, seccal
235; ... and the current date
236  IF n_elements(date0) EQ 0 then date0 = get_cal_value(event.handler)
237  jdate0 = date2jul(date0, month = month0, day = day0, year = year0)
238; index of days/months/years according to date0
239  case casename of
240    'day':BEGIN
241; list of days corresponding to month0 and year0
242      index = where(monthcal EQ month0 AND yearcal EQ year0)
243      current = daycal[index]
244      hms = hourcal[index] / 24.0d0 + mincal[index]/1440.0d0 + seccal[index] / 86400.0d0
245    END
246    'month':BEGIN
247; list of months corresponding to year0
248      index = where(yearcal EQ year0)
249      current = monthcal[index]
250; keep only the uniq values
251      indexbis = uniq(current)
252      index = index[indexbis]
253      current = current[indexbis]
254    END
255    'year':BEGIN
256; keep only the uniq years
257      index = uniq(yearcal)
258      current = yearcal[index]
259    END
260  ENDCASE
261; we update the uvalue of the widget
262  IF casename EQ 'day' THEN widget_control, wid_id, set_uvalue = {name:casename, hms:hms}
263; for event.out = 0, we store the previous position of the combobox to use
264; it as the default position.
265  IF event.out EQ 0 THEN widget_control, wid_id, get_value = oldselect
266; we redefine the new list
267  if casename EQ 'month' then begin
268    widget_control, wid_id, set_value = string(format = '(C(CMoA))', 31*(current-1))
269  ENDIF ELSE BEGIN
270    widget_control, wid_id, set_value = strtrim(current, 1)
271  ENDELSE
272; specify the index position within the new list of values.
273  widget_control, wid_id, get_value = combobox
274  CASE event.out OF
275; -1: we put to the biggest position
276    -1:selected = combobox.combobox_number - 1
277; 0: same as the previous position is the best choice...
278    0:selected = oldselect.combobox_index < (combobox.combobox_number - 1)
279; 1: we put to the smallest position
280    1:selected = 0
281; 2: a new date has been specified...
282    2:BEGIN
283      case casename of
284        'day':selected = (where(current EQ day0))[0]
285        'month':selected = (where(current EQ month0))[0]
286        'year':selected = (where(current EQ year0))[0]
287      ENDCASE
288    END
289  ENDCASE
290  widget_control, wid_id, set_value = {combobox_select:selected}
291; update the date...
292  infowid.date = get_cal_value(event.handler)
293  widget_control, winfo_id, set_uvalue = infowid
294  return
295end
296;----------------------------------------------------------------------
297; move cyclically the calendar to the
298; value 0 if event.out=1 or combobox_number-1 if event.out=-1
299;+
300; @file_comments
301;
302;
303; @categories
304; Compound widget
305;
306; @param EVENT
307;
308;
309; @param CASENAME
310;
311;
312; @returns
313;
314; @uses
315;
316; @restrictions
317;
318; @examples
319;
320; @history
321;
322; @version
323; $Id$
324;
325;-
326PRO cw_cal_move, event, casename
327;
328  compile_opt strictarr, strictarrsubs
329;
330   possiblecase = ['day', 'month', 'year', 'impossiblecase']
331   id = widget_info(event.handler, find_by_uname = casename)
332   widget_control, id, get_value = wvalue
333; we try to move but we are already at the beginning/end of the combobox
334; wvalue.combobox_index EQ (wvalue.combobox_number-1) and event.out EQ 1
335; wvalue.combobox_index EQ 0 and event.out EQ -1 (move is not called when out eq 0)
336   whichcase = (where(possiblecase EQ casename))[0]
337   if wvalue.combobox_index EQ (wvalue.combobox_number-1)*(event.out EQ 1) THEN BEGIN
338      if widget_info(event.handler, find_by_uname = possiblecase[whichcase+1]) EQ 0 then begin
339; it is impossible to move the "next" combobox...
340         widget_control, id, get_value = widvalue
341; we set to "widvalue.combobox_number-1" when event.out EQ -1
342; and to 0 when event.out EQ 1
343         selected = (widvalue.combobox_number-1)*(event.out EQ -1)
344         widget_control, id, set_value = {combobox_select:selected}
345; we call move for the next combobox
346      ENDIF ELSE cw_cal_move, event, possiblecase[whichcase+1]
347; it is possible to move from +/- 1
348   ENDIF ELSE widget_control, id, set_value = {combobox_select:wvalue.combobox_index+event.out}
349   set_cal_combobox, event, possiblecase[whichcase-1]
350   return
351end
352
353;+
354;
355; @file_comments
356;
357; @categories
358; Compound widget
359;
360; @param EVENT
361;
362;
363; @returns
364;
365; @uses
366;
367; @restrictions
368;
369; @examples
370;
371; @history
372;
373; @version
374; $Id$
375;
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;-
468FUNCTION cw_calendar, parent, calendar, jdate0 $
469                    , CALTYPE=CALTYPE, FAKECAL=fakecal $
470                    , UVALUE=uvalue, UNAME=uname, _EXTRA=ex
471;
472@cm_4cal
473;
474  compile_opt strictarr, strictarrsubs
475;
476  if keyword_set(caltype) then key_caltype = caltype
477; months days years found in the calendar
478  caldat, calendar, monthcal, daycal, yearcal, hourcal, mincal, seccal
479  hmscal = hourcal / 24.0d0 + mincal/1440.0d0 + seccal / 86400.0d0
480; starting date
481  if n_elements(jdate0) EQ 0 then jdate0 = calendar[0]
482  if (where(calendar EQ jdate0))[0] EQ -1 then jdate0 = calendar[0]
483;
484  caldat, jdate0, month0, day0, year0
485; test the type of calendar
486  if n_elements(calendar) GT 1 then BEGIN
487; each day have the same value
488    if n_elements(uniq(daycal, sort(daycal))) EQ 1 then monthly = 1
489; each month and each day have the same value
490    if keyword_set(monthly) AND n_elements(uniq(monthcal, sort(monthcal))) EQ 1 then yearly = 1
491  endif
492;---------------------------------------------------------------------------------------
493  if NOT keyword_set(uvalue) then uvalue = {dummy:''}
494  if NOT keyword_set(uname) then uname = ''
495  base0 = widget_base(parent, /ROW $
496                      , EVENT_FUNC = 'cw_calendar_event' $
497                      , FUNC_GET_VALUE = 'cw_calendar_get_value' $
498                      , PRO_SET_VALUE = 'cw_calendar_set_value' $
499                      , UVALUE = uvalue, UNAME = uname, space = 0, _extra = ex)
500;
501
502  if n_elements(fakecal) eq 0 then fakecal = 0
503  base = widget_base(base0, space = 0, uname = 'infocal' $
504                     , uvalue = {calendar:calendar, date:jul2date(jdate0), fakecal:fakecal, caltype: key_caltype})
505;
506  IF keyword_set(fakecal) THEN BEGIN
507    cmbbid = cw_combobox_pm(base, UVALUE = {name:'step'}, UNAME = 'step' $
508                            , value = strtrim(indgen(n_elements(calendar)), 1))
509    widget_control, cmbbid, set_value = {combobox_select:(where(calendar EQ jdate0))[0]}
510  ENDIF ELSE BEGIN
511;
512    vallen = widget_info(base, string_size = 'm')
513;-------------day-----------------
514    if NOT keyword_set(monthly)  then begin
515      dayindex = where(monthcal EQ month0 AND yearcal EQ year0)
516      currentday = daycal[dayindex]
517      currentday = strtrim(currentday, 1)
518      hms = hmscal[dayindex]
519      cmbbid = cw_combobox_pm(base, UVALUE = {name:'day', hms:hms}, UNAME = 'day', value = currentday)
520      widget_control, cmbbid, set_value = {combobox_select:(where(long(currentday) EQ day0))[0]}
521    endif
522;-------------month---------------
523    if NOT keyword_set(yearly)  then BEGIN
524      monthindex = where(yearcal EQ year0)
525      currentmonth = long(monthcal[monthindex])
526; we suppress the repeted months
527      monthindexbis = uniq(currentmonth, sort(currentmonth))
528      monthindex = monthindex[monthindexbis]
529      currentmonth = currentmonth[monthindexbis]
530      xoff = (34 + 2*vallen[0])*(1-keyword_set(monthly))
531      cmbbid = cw_combobox_pm(base, UVALUE = {name:'month'}, UNAME = 'month', value = string(format = '(C(CMoA))', 31*(currentmonth-1)), xoffset = xoff)
532      widget_control, cmbbid, set_value = {combobox_select:(where(long(currentmonth) EQ month0))[0]}
533    endif
534;-------------year----------------
535    yearindex = uniq(yearcal, sort(yearcal))
536    currentyear = strtrim(yearcal[yearindex], 1)
537    xoff = (34 + 2*vallen[0])*(1-keyword_set(monthly)) + (33 + 3*vallen[0])*(1-keyword_set(yearly))
538    cmbbid = cw_combobox_pm(base, UVALUE = {name:'year'}, UNAME = 'year', value = currentyear, xoffset = xoff)
539    widget_control, cmbbid, set_value = {combobox_select:(where(long(currentyear) EQ year0))[0]}
540;-----------------------------
541  ENDELSE
542;
543  return, base
544end
Note: See TracBrowser for help on using the repository browser.