From help-octave-request at bevo dot che dot wisc dot edu Sun Jan 30 21:14:04 2000 Subject: Re: perlTK From: "John W. Eaton" To: Joao Cardoso Cc: "help-octave at bevo dot che dot wisc dot edu" Date: Sun, 30 Jan 2000 21:14:36 -0600 (CST) On 31-Jan-2000, Joao Cardoso wrote: | Couldn't Octave have a function to do it? Something like | | on_idle("user function","user data") Sure, I've appended a solution. If your user function waits for and processes one GUI event at a time, then you will also need something like void do_nothing (ClientData data, int i) { } and Tk_CreateFileHandler (0, TK_READABLE, do_nothing, 0); which sets up a function to be called whenever there is input waiting on stdin. By doing nothing, you force an event to happen and be processed, then user function can return so that Octave (readline) can process the next character(s) on the input stream. I'm not really sure that the following function buys you much unless you also write a DEFUN for your GUI's equivalent of Tcl_DoOneEvent (TCL_ALL_EVENTS); so that it can be called from your user function. In any case, it would be more efficient to do it all in C/C++. Also, my use of Tcl/Tk here should not be taken as an endorsement from me for Tcl/Tk, or any indication that I plan to use Tcl/Tk with Octave. I just happened to have the Tcl/Tk and readline example available. You should be able to do similar things with other GUI toolkits. jwe Index: input.cc =================================================================== RCS file: /usr/local/cvsroot/octave/src/input.cc,v retrieving revision 1.125 diff -c -r1.125 input.cc *** input.cc 2000/01/27 23:30:46 1.125 --- input.cc 2000/01/31 03:06:06 *************** *** 880,885 **** --- 880,956 ---- return retval; } + static string hook_fcn; + static octave_value user_data; + + static int + input_event_hook (...) + { + if (user_data.is_defined ()) + feval (hook_fcn, user_data, 0); + else + feval (hook_fcn, octave_value_list (), 0); + + return 0; + } + + DEFUN (input_event_hook, args, , + "-*- texinfo -*-\n\ + at deftypefn {Built-in Function} {[@var{ofcn}, @var{odata}] =} input_event_hook (@var{fcn}, @var{data})\n\ + Given the name of a function as a string and any Octave value object,\n\ + install at var{fcn} as a function to call periodically, when Octave is\n\ + waiting for input. The function should have the form\n\ + at example\n\ + at var{fcn} (@var{data})\n\ + at end example\n\ + \n\ + If at var{data} is omitted, Octave calls the function without any\n\ + arguments. If both at var{fcn} and @var{data} are omitted, Octave\n\ + clears the hook. In all cases, the name of the previous hook function\n\ + and the user data are returned.\n\ + at end deftypefn") + { + octave_value_list retval; + + int nargin = args.length (); + + if (nargin > 2) + print_usage ("input_event_hook"); + else + { + octave_value tmp_user_data; + + string tmp_hook_fcn; + + if (nargin > 1) + tmp_user_data = args(1); + + if (nargin > 0) + { + tmp_hook_fcn = args(0).string_value (); + + if (error_state) + { + error ("input_event_hook: expecting string as first arg"); + return retval; + } + + command_editor::set_event_hook (input_event_hook); + } + + if (nargin == 0) + command_editor::set_event_hook (0); + + retval(1) = user_data; + retval(0) = hook_fcn; + + hook_fcn = tmp_hook_fcn; + user_data = tmp_user_data; + } + + return retval; + } + static int ps1 (void) { ----------------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.che.wisc.edu/octave/octave.html How to fund new projects: http://www.che.wisc.edu/octave/funding.html Subscription information: http://www.che.wisc.edu/octave/archive.html -----------------------------------------------------------------------