; 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_cwo_clock ;- ;+ ; Display the current time in the clock. ;- pro mg_cwo_clock::display compile_opt strictarr stime = string(systime(/julian) + self.offset, format=self.format) widget_control, self.display, set_value=stime widget_control, self.timer, timer=self.updateInterval end ;+ ; Set the properties of the compound widget. ; ; :Keywords: ; update_interval : in, optional, type=float ; number of seconds between clock updates ; format : in, optional, type=string ; format string for the time output ; time : in, optional, type=double ; current Julian date//time ;- pro mg_cwo_clock::setProperty, update_interval=updateInterval, format=format, $ time=time compile_opt strictarr if (n_elements(updateInterval) gt 0L) then self.updateInterval = updateInterval if (n_elements(format) gt 0L) then self.format = format if (n_elements(time) gt 0L) then self.offset = time - systime(/julian) end ;+ ; Create a clock compound widget object. ; ; :Returns: ; 1 for success, 0 for failure ; ; :Keywords: ; top : in, required, type=long ; widget identifier of the parent compound widget ; update_interval : in, optional, type=float ; number of seconds between clock updates ; format : in, optional, type=string ; format string for the time output ; time : in, optional, type=double ; current Julian date/time ;- function mg_cwo_clock::init, top=top, update_interval=updateInterval, $ format=format, time=time compile_opt strictarr self.top = top self.timer = widget_info(top, find_by_uname='mg_cwo_clock_timer') self.display = widget_info(top, find_by_uname='mg_cwo_clock_display') self.format = n_elements(format) eq 0L ? '(C())' : format self.updateInterval = n_elements(updateInterval) eq 0L ? 1.0 : updateInterval self.offset = n_elements(time) eq 0L ? 0.0D : (systime(/julian) - time) return, 1 end ;+ ; Define the instance variables for the clock compound widget. ; ; :Fields: ; top ; top of the widget hierarchy for the clock compound widget ; timer ; widget identifier for the widget with the timer set on it ; display ; widget identifier for the text widget to display the time ; updateInterval ; number of seconds between clock updates ; format ; format string for the time output ; offset ; difference between the current time and the time of the clock in ; Julian days ;- pro mg_cwo_clock__define compile_opt strictarr define = { mg_cwo_clock, $ top: 0L, $ timer: 0L, $ display: 0L, $ updateInterval: 0.0, $ format: '', $ offset: 0.0D $ } end ;+ ; This would normally set the value of the compound widget, but here the ; "value" of this compound widget is the control object. This value should not ; be set, but the individual properties of the control object should be set ; with its setProperty method. ; ; :Params: ; clock : in, required, type=long ; widget identifier for the compound widget ; value : in, required, type=object ; control object for the clock ;- pro mg_cwo_clock_set_value, clock, value compile_opt strictarr on_error, 2 message, 'do not change the value of a MG_CW_CLOCK compound widget' 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_cwo_clock_get_value, clock compile_opt strictarr timer = widget_info(clock, find_by_uname='mg_cwo_clock_timer') widget_control, timer, get_uvalue=control return, control 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_cwo_clock_event, event compile_opt strictarr timer = widget_info(event.handler, find_by_uname='mg_cwo_clock_timer') widget_control, timer, get_uvalue=control control->display 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_cwo_clock_realize, top compile_opt strictarr timer = widget_info(top, find_by_uname='mg_cwo_clock_timer') widget_control, timer, get_uvalue=control control->display 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_cwo_clock_cleanup, timer compile_opt strictarr widget_control, timer, get_uvalue=control obj_destroy, control 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: ; value : out, optional, type=object ; set to a named variable to get the value of the MG_CW_CLOCK compound ; widget i.e. an MG_CW_CLOCK object ; _extra : in, optional, type=keywords ; keywords to the MG_CW_CLOCK::init ;- function mg_cwo_clock, parent, value=control, _extra=e compile_opt strictarr top = widget_base(parent, $ pro_set_value='mg_cwo_clock_set_value', $ func_get_value='mg_cwo_clock_get_value', $ event_func='mg_cwo_clock_event', $ notify_realize='mg_cwo_clock_realize') timer = widget_base(top, scr_xsize=0, scr_ysize=0, $ uname='mg_cwo_clock_timer', $ kill_notify='mg_cwo_clock_cleanup') display = widget_label(top, value=systime(), uname='mg_cwo_clock_display') control = obj_new('mg_cwo_clock', top=top, _extra=e) widget_control, timer, set_uvalue=control return, top end ; main-level example program tlb = widget_base(title='MG_CW_CLOCK demo', /column) clock = mg_cwo_clock(tlb) widget_control, clock, get_value=control widget_control, tlb, /realize xmanager, 'mg_cwo_clock_demo', tlb, /no_block end