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

Last change on this file since 134 was 134, checked in by navarro, 18 years ago

change *.pro file properties (del eof-style, del executable, set keywords Id

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 14.0 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;
40PRO cw_calendar_set_value, id, value
41;
42  compile_opt strictarr, strictarrsubs
43;
44;
45@cm_4cal
46; get back the calendar and its related informations
47  winfo_id = widget_info(id, find_by_uname = 'infocal')
48  widget_control, winfo_id, get_uvalue = infowid
49  key_caltype = infowid.caltype
50 
51; high freqeuncy calendar
52  IF keyword_set(infowid.fakecal) THEN BEGIN
53    value2 = date2jul(long(value)) - infowid.fakecal
54    IF value2 LT n_elements(infowid.calendar) AND value2 GE 0 THEN BEGIN 
55      stepid = widget_info(id, find_by_uname = 'step')
56      widget_control, stepid, set_value = {combobox_select:value2}
57      infowid.date = jul2date(value2 + infowid.fakecal)
58      widget_control, winfo_id, set_uvalue = infowid
59    ENDIF
60  ENDIF ELSE BEGIN
61;
62    value = long(value[0])
63; define year month day
64    year = value/10000l
65    month = (value MOD 10000L)/100L
66    day = value MOD 100L
67; make sure the values correspond to real dates
68    if year EQ 0 then year = -1
69    if month EQ 0 then month = 6
70    if day EQ 0 then day = 15
71; check that the date exists in the calendar
72  if (where(infowid.calendar EQ julday(month, day, year)))[0] EQ - 1 then return
73; update the value of infocal
74    infowid.date = value
75    widget_control, winfo_id, set_uvalue = infowid
76; update the combobox if needed...
77    possiblecase = ['day', 'month', 'year']
78    for name = 2, 0, -1 do BEGIN
79; call set_cal_combobox with out = 2 to specify that the call is coming
80; from cw_calendar_set_value
81      if widget_info(id, find_by_uname = possiblecase[name]) NE 0 then $
82         set_cal_combobox, {handler:id, out:2}, possiblecase[name], value
83    ENDFOR
84  ENDELSE
85;
86  return
87end
88;----------------------------------------------------------------------
89FUNCTION cw_calendar_get_value, id
90;
91  compile_opt strictarr, strictarrsubs
92;
93  winfo_id = widget_info(id, find_by_uname = 'infocal')
94  widget_control, winfo_id, get_uvalue = infowid
95  return, infowid.date
96END
97;----------------------------------------------------------------------
98FUNCTION get_cal_value, id, winfoid
99;
100  compile_opt strictarr, strictarrsubs
101;
102  winfo_id = widget_info(id, find_by_uname = 'infocal')
103  widget_control, winfo_id, get_uvalue = infowid
104  oldate = infowid.date
105;-------------day-----------------
106  wid_id = widget_info(id, find_by_uname = 'day')
107  if wid_id NE 0 then BEGIN
108    widget_control, wid_id, get_value = wid_value
109    date = long(wid_value.combobox_gettext)
110  ENDIF ELSE date = oldate MOD 100L
111;-------------month---------------
112  wid_id = widget_info(id, find_by_uname = 'month')
113  if wid_id NE 0 then BEGIN
114    widget_control, wid_id, get_value = wid_value
115    allmonths = string(format = '(C(CMoA))', 31*(indgen(12)))
116    month = (where(allmonths EQ wid_value.combobox_gettext))[0] + 1
117    date = date + 100L * long(month)
118  ENDIF ELSE date = date + (oldate MOD 10000L)/100L*100L
119;-------------year----------------
120  wid_id = widget_info(id, find_by_uname = 'year')
121  widget_control, wid_id, get_value = wid_value
122  date = date + 10000L * long(wid_value.combobox_gettext)
123;
124  IF arg_present(winfoid) NE 0 THEN BEGIN
125    winfoid = winfo_id
126    infowid.date = date
127    return, infowid
128  ENDIF ELSE return, date
129end
130;----------------------------------------------------------------------
131; redefine the value and index position of the combobox
132PRO set_cal_combobox, event, casename, date0
133;
134  compile_opt strictarr, strictarrsubs
135;
136; casename: Which widget shall we move: 'day', 'month' or 'year' ?
137  wid_id = widget_info(event.handler, find_by_uname = casename)
138; we get back the calendar...
139  winfo_id = widget_info(event.handler, find_by_uname = 'infocal')
140  widget_control, winfo_id, get_uvalue = infowid
141  caldat, infowid.calendar, monthcal, daycal, yearcal
142; ... and the current date
143  IF n_elements(date0) EQ 0 then date0 = get_cal_value(event.handler)
144  year0 = date0/10000L
145  month0 = (date0 MOD 10000L)/100L
146  day0 = date0 MOD 100L
147; index of days/months/years according to date0
148  case casename of
149    'day':BEGIN
150; list of days corresponding to month0 and year0
151      index = where(monthcal EQ month0 AND yearcal EQ year0)
152      current = daycal[index]
153    END
154    'month':BEGIN
155; list of months corresponding to year0
156      index = where(yearcal EQ year0)
157      current = monthcal[index]
158; keep only the uniq values
159      indexbis = uniq(current)
160      index = index[indexbis]
161      current = current[indexbis]
162    END
163    'year':BEGIN
164; keep only the uniq years
165      index = uniq(yearcal)
166      current = yearcal[index]
167    END
168  ENDCASE
169; we update the uvalue of the widget
170  widget_control, wid_id, set_uvalue = {name:casename}
171; for event.out = 0, we store the previous position of the combobox to use
172; it as the default position.
173  IF event.out EQ 0 THEN widget_control, wid_id, get_value = oldselect
174; we redefine the new list
175  if casename EQ 'month' then begin
176    widget_control, wid_id, set_value = string(format = '(C(CMoA))', 31*(current-1))
177  ENDIF ELSE BEGIN
178    widget_control, wid_id, set_value = strtrim(current, 1)
179  ENDELSE
180; specify the index position within the new list of values.
181  widget_control, wid_id, get_value = combobox
182  CASE event.out OF
183; -1: we put to the biggest position
184    -1:selected = combobox.combobox_number - 1
185; 0: same as the previous position is the best choice...
186    0:selected = oldselect.combobox_index < (combobox.combobox_number - 1)
187; 1: we put to the smallest position
188    1:selected = 0
189; 2: a new date has been specified...
190    2:BEGIN
191      case casename of
192        'day':selected = (where(current EQ day0))[0]
193        'month':selected = (where(current EQ month0))[0]
194        'year':selected = (where(current EQ year0))[0]
195      ENDCASE
196    END
197  ENDCASE
198  widget_control, wid_id, set_value = {combobox_select:selected}
199; update the date...
200  infowid.date = get_cal_value(event.handler)
201  widget_control, winfo_id, set_uvalue = infowid
202  return
203end
204;----------------------------------------------------------------------
205; move cyclicly the calendar to the
206; value 0 if event.out=1 or combobox_number-1 if event.out=-1
207PRO move, event, casename
208;
209  compile_opt strictarr, strictarrsubs
210;
211   possiblecase = ['day', 'month', 'year', 'impossiblecase']
212   id = widget_info(event.handler, find_by_uname = casename)
213   widget_control, id, get_value = wvalue
214; we try to move but we are already at the beginning/end of the combobox
215; wvalue.combobox_index EQ (wvalue.combobox_number-1) and event.out EQ 1
216; wvalue.combobox_index EQ 0 and event.out EQ -1 (move is not called when out eq 0)
217   whichcase = (where(possiblecase EQ casename))[0]
218   if wvalue.combobox_index EQ (wvalue.combobox_number-1)*(event.out EQ 1) THEN BEGIN
219      if widget_info(event.handler, find_by_uname = possiblecase[whichcase+1]) EQ 0 then begin
220; it is impossible to move the "next" combobox...
221         widget_control, id, get_value = widvalue
222; we set to "widvalue.combobox_number-1" when event.out EQ -1
223; and to 0 when event.out EQ 1
224         selected = (widvalue.combobox_number-1)*(event.out EQ -1)
225         widget_control, id, set_value = {combobox_select:selected}
226; we call move for the next combobox
227      ENDIF ELSE move, event, possiblecase[whichcase+1]
228; it is possible to move from +/- 1
229   ENDIF ELSE widget_control, id, set_value = {combobox_select:wvalue.combobox_index+event.out}
230   set_cal_combobox, event, possiblecase[whichcase-1]
231   return
232end
233;----------------------------------------------------------------------
234FUNCTION cw_calendar_event, event
235;
236@cm_4cal
237  compile_opt strictarr, strictarrsubs
238;
239  winfo_id = widget_info(event.handler, find_by_uname = 'infocal')
240  widget_control, winfo_id, get_uvalue = infowid
241  key_caltype = infowid.caltype
242;
243  widget_control, event.id, get_uvalue = uval
244; high frequency calendar
245  IF uval.name EQ 'step' THEN BEGIN
246    infowid.date = jul2date(event.index + infowid.fakecal)
247  ENDIF ELSE BEGIN
248;
249    possiblecase = ['day', 'month', 'year', 'impossiblecase']
250    whichcase = (where(possiblecase EQ uval.name))[0]
251    if event.out NE 0 then BEGIN ; we use the +/- button and we want to go out of the combobox:
252; to index = -1 (event.out=-1) or to index = combobox_number (event.out=1)
253; we try to move the combobox just right, with name: possiblecase[whichcase+1]
254      if widget_info(event.handler, find_by_uname = possiblecase[whichcase+1]) EQ 0 then BEGIN
255; this widget do not exist we set cyclicly the current widget to the
256; value 0 if event.out=1 or combobox_number-1 if event.out=-1
257        widget_control, event.id, get_value = widvalue
258        selected = (widvalue.combobox_number-1)*(event.out EQ -1)
259        widget_control, event.id, set_value = {combobox_select:selected}
260      ENDIF ELSE move, event, possiblecase[whichcase+1]
261    ENDIF
262; if we changed month(year), we need to update the day (and month) list
263    if uval.name NE 'day' then begin
264      event.out = 0
265      for name = whichcase-1, 0, -1 do BEGIN
266        if widget_info(event.handler, find_by_uname = possiblecase[name]) NE 0 then $
267           set_cal_combobox, event, possiblecase[name]
268      endfor
269    ENDIF
270; we update the date
271    infowid = get_cal_value(event.handler, winfo_id)
272  ENDELSE
273;
274  widget_control, winfo_id, set_uvalue = infowid
275  return, {CW_CALENDAR, ID:event.handler, TOP:event.top, HANDLER:0L $
276           , VALUE:infowid.date, FAKECAL: infowid.fakecal}
277end
278;----------------------------------------------------------------------
279;----------------------------------------------------------------------
280FUNCTION cw_calendar, parent, calendar, jdate0, CALTYPE = CALTYPE, FAKECAL = fakecal, UVALUE = uvalue, UNAME = uname, _extra = ex
281;
282@cm_4cal
283;
284  compile_opt strictarr, strictarrsubs
285;
286  if keyword_set(caltype) then key_caltype = caltype
287; months days years found in the calendar
288  caldat, calendar, monthcal, daycal, yearcal, hourcal, mincal, scdcal
289; starting date
290  if n_elements(jdate0) EQ 0 then jdate0 = calendar[0]
291  if (where(calendar EQ jdate0))[0] EQ -1 then jdate0 = calendar[0]
292;
293  caldat, jdate0, month0, day0, year0
294; test the type of calendar
295  if n_elements(calendar) GT 1 then BEGIN
296; each day have the same value
297    if n_elements(uniq(daycal, sort(daycal))) EQ 1 then monthly = 1
298; each month and each day have the same value
299    if keyword_set(monthly) AND n_elements(uniq(monthcal, sort(monthcal))) EQ 1 then yearly = 1
300  endif
301;---------------------------------------------------------------------------------------
302  if NOT keyword_set(uvalue) then uvalue = {dummy:''}
303  if NOT keyword_set(uname) then uname = ''
304  base0 = widget_base(parent, /ROW $
305                      , EVENT_FUNC = 'cw_calendar_event' $
306                      , FUNC_GET_VALUE = 'cw_calendar_get_value' $
307                      , PRO_SET_VALUE = 'cw_calendar_set_value' $
308                      , UVALUE = uvalue, UNAME = uname, space = 0, _extra = ex)
309;
310
311  if n_elements(fakecal) eq 0 then fakecal = 0
312  base = widget_base(base0, space = 0, uname = 'infocal' $
313                     , uvalue = {calendar:calendar, date:jul2date(jdate0), fakecal:fakecal, caltype: key_caltype})
314;
315  IF keyword_set(fakecal) THEN BEGIN
316    cmbbid = cw_combobox_pm(base, UVALUE = {name:'step'}, UNAME = 'step' $
317                            , value = strtrim(indgen(n_elements(calendar)), 1))
318    widget_control, cmbbid, set_value = {combobox_select:(where(calendar EQ jdate0))[0]}
319  ENDIF ELSE BEGIN
320;
321    vallen = widget_info(base, string_size = 'm')
322;-------------day-----------------
323    if NOT keyword_set(monthly)  then begin
324      dayindex = where(monthcal EQ month0 AND yearcal EQ year0)
325      currentday = daycal[dayindex]
326      currentday = strtrim(currentday, 1)
327      cmbbid = cw_combobox_pm(base, UVALUE = {name:'day'}, UNAME = 'day', value = currentday)
328      widget_control, cmbbid, set_value = {combobox_select:(where(long(currentday) EQ day0))[0]}
329    endif
330;-------------month---------------
331    if NOT keyword_set(yearly)  then BEGIN
332      monthindex = where(yearcal EQ year0)
333      currentmonth = long(monthcal[monthindex])
334; we suppress the repeted months
335      monthindexbis = uniq(currentmonth, sort(currentmonth))
336      monthindex = monthindex[monthindexbis]
337      currentmonth = currentmonth[monthindexbis]
338      xoff = (34 + 2*vallen[0])*(1-keyword_set(monthly))
339      cmbbid = cw_combobox_pm(base, UVALUE = {name:'month'}, UNAME = 'month', value = string(format = '(C(CMoA))', 31*(currentmonth-1)), xoffset = xoff)
340      widget_control, cmbbid, set_value = {combobox_select:(where(long(currentmonth) EQ month0))[0]}
341    endif
342;-------------year----------------
343    yearindex = uniq(yearcal, sort(yearcal))
344    currentyear = strtrim(yearcal[yearindex], 1)
345    xoff = (34 + 2*vallen[0])*(1-keyword_set(monthly)) + (33 + 3*vallen[0])*(1-keyword_set(yearly))
346    cmbbid = cw_combobox_pm(base, UVALUE = {name:'year'}, UNAME = 'year', value = currentyear, xoffset = xoff)
347    widget_control, cmbbid, set_value = {combobox_select:(where(long(currentyear) EQ year0))[0]}
348;-----------------------------
349  ENDELSE
350;
351  return, base
352end
Note: See TracBrowser for help on using the repository browser.