; docformat = 'rst' ;+ ; Demo of incorporating a function graphic in a widget program. ;- ;+ ; Handle events generated by the save button and the function graphics ; window. ; ; :Params: ; event : in, required, type=structure ; `WIDGET_BUTTON` and `WIDGET_WINDOW` events ;- pro mg_functiongraphics_widget_event, event compile_opt strictarr widget_control, event.top, get_uvalue=pstate uname = widget_info(event.id, /uname) case uname of 'draw': begin status = widget_info(event.top, find_by_uname='status') coords = (*pstate).plot->convertCoord(event.x, event.y, $ /device, /to_data) label = string(coords[0], coords[1], format='(%"(%f, %f)")') widget_control, status, set_value=label end 'save': begin filename = dialog_pickfile() if (filename ne '') then (*pstate).plot->save, filename end endcase end ;+ ; Free resources of the widget program, including the function graphics ; window. ; ; :Params: ; tlb : in, required, type=long ; widget identifier for the top-level base ;- pro mg_functiongraphics_widget_cleanup, tlb compile_opt strictarr widget_control, tlb, get_uvalue=pstate obj_destroy, (*pstate).plot ptr_free, pstate end ;+ ; Launch routine for demo of a widget program using a function graphic. ; ; :Params: ; data : in, optional, type=fltarr ; data to plot, default is a sine curve ;- pro mg_functiongraphics_widget, data compile_opt strictarr ; create a simple widget hierarchy tlb = widget_base(/column) save = widget_button(tlb, value='Save as PNG', uname='save') draw = widget_window(tlb, xsize=500, ysize=300, $ /button_events, uname='draw') status = widget_label(tlb, value='Ready.', $ scr_xsize=500, /sunken_frame, /align_left, $ uname='status') widget_control, tlb, /realize ; the value of the WIDGET_WINDOW is a function graphics window object widget_control, draw, get_value=win ; plot the data into the current function graphics window, which is the one ; we just created p = plot(n_elements(data) eq 0L ? sin(findgen(360) * !dtor) : data, $ /current) ; set up data for event handlers state = { plot: p } pstate = ptr_new(state, /no_copy) widget_control, tlb, set_uvalue=pstate xmanager, 'mg_functiongraphics_widget', tlb, /no_block, $ event_handler='mg_functiongraphics_widget_event', $ cleanup='mg_functiongraphics_widget_cleanup' end