; docformat = 'rst'
;+
; Compound widget representing a digital clock.
;
; :Examples:
; Run the main-level program at the end of this file for an example of
; using MG_CW_CLOCK::
;
; IDL> .run mg_cw_clock
;-
;+
; Display the current time in the clock.
;
; :Params:
; pstate : in, required, type=pointer
; pointer to state structure
;-
pro mg_cw_clock_display, pstate
compile_opt strictarr
stime = string(systime(/julian) + (*pstate).offset, format=(*pstate).format)
widget_control, (*pstate).display, set_value=stime
widget_control, (*pstate).timer, timer=(*pstate).updateInterval
end
;+
; Set the time
;
; :Params:
; clock : in, required, type=long
; widget identifier for the compound widget
; value : in, required, type=double
; time to set the clock to in Julian time
;-
pro mg_cw_clock_set_value, clock, value
compile_opt strictarr
on_error, 2
timer = widget_info(clock, find_by_uname='mg_cw_clock_timer')
widget_control, timer, get_uvalue=pstate
(*pstate).offset = value - systime(/julian)
mg_cw_clock_display, pstate
end
;+
; Get the value of a MG_CW_CLOCK compound widget i.e. a MG_CW_CLOCK object.
;
; :Returns:
; MG_CW_CLOCK object
;
; :Params:
; clock : in, required, type=long
; widget identifier of the root of the compound widget hierarchy
;-
function mg_cw_clock_get_value, clock
compile_opt strictarr
timer = widget_info(clock, find_by_uname='mg_cw_clock_timer')
widget_control, timer, get_uvalue=pstate
return, systime(/julian) + (*pstate).offset
end
;+
; Handle all widget events generated by widgets in the compound widget
; hierarchy.
;
; :Returns:
; 0 if the event has been handled; an event structure if the another event
; handler should handle the event
;
; :Params:
; event : in, required, type=structure
; event structure
;-
function mg_cw_clock_event, event
compile_opt strictarr
timer = widget_info(event.handler, find_by_uname='mg_cw_clock_timer')
widget_control, timer, get_uvalue=pstate
mg_cw_clock_display, pstate
return, 0
end
;+
; Called after the widget hierarchy is realized. The clock cannot be
; displayed until the widget hierarchy is realized.
;
; :Params:
; top : in, required, type=long
; widget identifier of the top of the compound widget hierarchy
;-
pro mg_cw_clock_realize, top
compile_opt strictarr
timer = widget_info(top, find_by_uname='mg_cw_clock_timer')
widget_control, timer, get_uvalue=pstate
mg_cw_clock_display, pstate
end
;+
; Free resources of the compound widget i.e. its control object.
;
; :Params:
; timer : in, required, type=long
; widget identifier of the top of the compound widget hierarchy
;-
pro mg_cw_clock_cleanup, timer
compile_opt strictarr
widget_control, timer, get_uvalue=pstate
ptr_free, pstate
end
;+
; Create an MG_CW_CLOCK compound widget.
;
; :Returns:
; widget identifier of the compound widget
;
; :Params:
; parent : in, required, type=long
; widget identifier of the parent base widget for the compound widget
;
; :Keywords:
; time : in, optional, type=double
; Julian time to use as the curren time
; format : in, optional, type=string
; format string to use when displaying time
; update_interval : in, optional, type=float, default=1.0
; time in seconds between clock updates
; _extra : in, optional, type=keywords
; keywords to the MG_CW_CLOCK::init
;-
function mg_cw_clock, parent, $
time=time, $
format=format, $
update_interval=updateInterval, $
_extra=e
compile_opt strictarr
top = widget_base(parent, $
pro_set_value='mg_cw_clock_set_value', $
func_get_value='mg_cw_clock_get_value', $
event_func='mg_cw_clock_event', $
notify_realize='mg_cw_clock_realize')
timer = widget_base(top, scr_xsize=0, scr_ysize=0, $
uname='mg_cw_clock_timer', $
kill_notify='mg_cw_clock_cleanup')
display = widget_label(top, value=systime(), uname='mg_cw_clock_display')
state = { top: top, $
timer: timer, $
display: display, $
updateInterval: n_elements(updateInterval) eq 0L ? 1.0 : updateInterval, $
format: n_elements(format) eq 0L ? '(C())' : format, $
offset: n_elements(time) eq 0L ? 0.0D : (systime(/julian) - time) $
}
pstate = ptr_new(state, /no_copy)
widget_control, timer, set_uvalue=pstate
return, top
end
; main-level example program
tlb = widget_base(title='MG_CW_CLOCK demo', /column)
clock = mg_cw_clock(tlb)
widget_control, tlb, /realize
xmanager, 'mg_cw_clock_demo', tlb, /no_block
end