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
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; @file_comments
40;
41; @categories
42; Compound widget
43;
44; @param ID
45;
46; @param VALUE
47; It is the default tick mark value (a floating-point number).
48;
49; @returns
50;
51; @uses
52;
53; @restrictions
54;
55; @examples
56;
57; @history
58;
59; @version
60; $Id$
61;
62;-
63PRO cw_calendar_set_value, id, value
64;
65  compile_opt strictarr, strictarrsubs
66;
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
72; high frequency calendar
73  IF keyword_set(infowid.fakecal) THEN BEGIN
74    value2 = date2jul(long(value)) - infowid.fakecal
75    IF value2 LT n_elements(infowid.calendar) AND value2 GE 0 THEN BEGIN
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
81  ENDIF ELSE BEGIN
82    jdval = date2jul(value)
83; check that the date exists in the calendar
84    if (where(abs(infowid.calendar - jdval) LT 1.d/86400.d))[0] EQ - 1 then return
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
99end
100;----------------------------------------------------------------------
101;+
102; @file_comments
103;
104; @categories
105; Compound widget
106;
107; @param ID
108;
109; @returns
110;
111; @uses
112;
113; @restrictions
114;
115; @examples
116;
117; @history
118;
119; @version
120; $Id$
121;
122;-
123FUNCTION cw_calendar_get_value, id
124;
125  compile_opt strictarr, strictarrsubs
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;----------------------------------------------------------------------
132;+
133; @file_comments
134;
135; @categories
136; Compound widget
137;
138; @param ID
139;
140; @param WINFOID
141;
142; @returns
143;
144; @uses
145;
146; @restrictions
147;
148; @examples
149;
150; @history
151;
152; @version
153; $Id$
154;
155;-
156FUNCTION get_cal_value, id, winfoid
157;
158  compile_opt strictarr, strictarrsubs
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
167    widget_control, wid_id, get_uvalue = wid_uvalue
168    date = double(wid_value.combobox_gettext) + wid_uvalue.hms[wid_value.combobox_index]
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)))
175    month = (where(allmonths EQ wid_value.combobox_gettext))[0] + 1
176    date = date + 100L * long(month)
177  ENDIF ELSE date = date + (oldate MOD 10000L)/ 100L * 100L
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
188end
189;----------------------------------------------------------------------
190; redefine the value and index position of the combobox
191;+
192; @file_comments
193;
194; @categories
195; Compound widget
196;
197; @param EVENT
198;
199; @param CASENAME
200;
201; @param DATE0
202;
203; @returns
204;
205; @uses
206;
207; @restrictions
208;
209; @examples
210;
211; @history
212;
213; @version
214; $Id$
215;
216;-
217PRO set_cal_combobox, event, casename, date0
218;
219  compile_opt strictarr, strictarrsubs
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
226  caldat, infowid.calendar, monthcal, daycal, yearcal, hourcal, mincal, seccal
227; ... and the current date
228  IF n_elements(date0) EQ 0 then date0 = get_cal_value(event.handler)
229  jdate0 = date2jul(date0, month = month0, day = day0, year = year0)
230; index of days/months/years according to date0
231  case casename of
232    'day':BEGIN
233; list of days corresponding to month0 and year0
234      index = where(monthcal EQ month0 AND yearcal EQ year0)
235      current = daycal[index]
236      hms = hourcal[index] / 24.0d0 + mincal[index]/1440.0d0 + seccal[index] / 86400.0d0
237    END
238    'month':BEGIN
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
247    'year':BEGIN
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
254  IF casename EQ 'day' THEN widget_control, wid_id, set_uvalue = {name:casename, hms:hms}
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))
261  ENDIF ELSE BEGIN
262    widget_control, wid_id, set_value = strtrim(current, 1)
263  ENDELSE
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...
274    2:BEGIN
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
287end
288;----------------------------------------------------------------------
289; move cyclically the calendar to the
290; value 0 if event.out=1 or combobox_number-1 if event.out=-1
291;+
292; @file_comments
293;
294; @categories
295; Compound widget
296;
297; @param EVENT
298;
299; @param CASENAME
300;
301; @returns
302;
303; @uses
304;
305; @restrictions
306;
307; @examples
308;
309; @history
310;
311; @version
312; $Id$
313;
314;-
315PRO cw_cal_move, event, casename
316;
317  compile_opt strictarr, strictarrsubs
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)
325   whichcase = (where(possiblecase EQ casename))[0]
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
335      ENDIF ELSE cw_cal_move, event, possiblecase[whichcase+1]
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]
339   return
340end
341
342;+
343;
344; @file_comments
345;
346; @categories
347; Compound widget
348;
349; @param EVENT
350;
351; @returns
352;
353; @uses
354;
355; @restrictions
356;
357; @examples
358;
359; @history
360;
361; @version
362; $Id$
363;
364;-
365FUNCTION cw_calendar_event, event
366;
367@cm_4cal
368  compile_opt strictarr, strictarrsubs
369;
370  winfo_id = widget_info(event.handler, find_by_uname = 'infocal')
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
376  IF uval.name EQ 'step' THEN BEGIN
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
386; this widget do not exist we set cyclically the current widget to the
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}
391      ENDIF ELSE cw_cal_move, event, possiblecase[whichcase+1]
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
395      event.out = 0
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]
399      endfor
400    ENDIF
401; we update the date
402    infowid = get_cal_value(event.handler, winfo_id)
403  ENDELSE
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}
408end
409;
410;+
411;
412; @file_comments
413;
414; @categories
415; Compound widget
416;
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
425;
426; @keyword FAKECAL
427;
428; @keyword UVALUE
429;
430; @keyword UNAME
431;
432; @keyword _EXTRA
433; Used to pass keywords
434;
435; @returns
436;
437; @uses
438;
439; @restrictions
440;
441; @examples
442;
443; @history
444;
445; @version
446; $Id$
447;
448; @todo
449; seb
450;
451;-
452FUNCTION cw_calendar, parent, calendar, jdate0 $
453                    , CALTYPE=CALTYPE, FAKECAL=fakecal $
454                    , UVALUE=uvalue, UNAME=uname, _EXTRA=ex
455;
456@cm_4cal
457;
458  compile_opt strictarr, strictarrsubs
459;
460  if keyword_set(caltype) then key_caltype = caltype
461; months days years found in the calendar
462  caldat, calendar, monthcal, daycal, yearcal, hourcal, mincal, seccal
463  hmscal = hourcal / 24.0d0 + mincal/1440.0d0 + seccal / 86400.0d0
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]
467;
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
476;---------------------------------------------------------------------------------------
477  if NOT keyword_set(uvalue) then uvalue = {dummy:''}
478  if NOT keyword_set(uname) then uname = ''
479  base0 = widget_base(parent, /ROW $
480                      , EVENT_FUNC = 'cw_calendar_event' $
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;
490  IF keyword_set(fakecal) THEN BEGIN
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]}
494  ENDIF ELSE BEGIN
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)
500      currentday = daycal[dayindex]
501      currentday = strtrim(currentday, 1)
502      hms = hmscal[dayindex]
503      cmbbid = cw_combobox_pm(base, UVALUE = {name:'day', hms:hms}, UNAME = 'day', value = currentday)
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)
509      currentmonth = long(monthcal[monthindex])
510; we suppress the repeated months
511      monthindexbis = uniq(currentmonth, sort(currentmonth))
512      monthindex = monthindex[monthindexbis]
513      currentmonth = currentmonth[monthindexbis]
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
518;-------------year----------------
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]}
524;-----------------------------
525  ENDELSE
526;
527  return, base
528end
Note: See TracBrowser for help on using the repository browser.