GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
octave-link.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2013 John W. Eaton
4 Copyright (C) 2011-2013 Jacob Dawid
5 Copyright (C) 2011-2013 John P. Swensen
6 
7 This file is part of Octave.
8 
9 Octave is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
13 
14 Octave is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Octave; see the file COPYING. If not, see
21 <http://www.gnu.org/licenses/>.
22 
23 */
24 
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 
29 #include "cmd-edit.h"
30 #include "defun.h"
31 #include "oct-env.h"
32 #include "oct-mutex.h"
33 #include "singleton-cleanup.h"
34 #include "toplev.h"
35 
36 #include "octave-link.h"
37 
38 static int
40 {
45 
46  return 0;
47 }
48 
50 
52  : event_queue_mutex (new octave_mutex ()), gui_event_queue (),
53  debugging (false), link_enabled (true)
54 {
56 }
57 
58 void
60 {
61  if (enabled ())
65 }
66 
67 // OBJ should be an object of a class that is derived from the base
68 // class octave_link, or 0 to disconnect the link. It is the
69 // responsibility of the caller to delete obj.
70 
71 void
73 {
74  if (obj && instance)
75  ::error ("octave_link is already linked!");
76  else
77  instance = obj;
78 }
79 
80 void
82 {
83 }
84 
85 void
87 {
89 
91 
93 }
94 
95 void
97 {
99 
101 
103 }
104 
105 DEFUN (__octave_link_enabled__, , ,
106  "-*- texinfo -*-\n\
107 @deftypefn {Built-in Function} {} __octave_link_enabled__ ()\n\
108 Undocumented internal function.\n\
109 @end deftypefn")
110 {
112 }
113 
114 DEFUN (__octave_link_edit_file__, args, ,
115  "-*- texinfo -*-\n\
116 @deftypefn {Built-in Function} {} __octave_link_edit_file__ (@var{file})\n\
117 Undocumented internal function.\n\
118 @end deftypefn")
119 {
120  octave_value retval;
121 
122  if (args.length () == 1)
123  {
124  std::string file = args(0).string_value ();
125 
126  if (! error_state)
127  {
129 
130  retval = octave_link::edit_file (file);
131  }
132  else
133  error ("expecting file name as argument");
134  }
135  else if (args.length () == 2)
136  {
137  std::string file = args(0).string_value ();
138 
139  if (! error_state)
140  {
142 
143  retval = octave_link::prompt_new_edit_file (file);
144  }
145  else
146  error ("expecting file name as first argument");
147  }
148 
149  return retval;
150 }
151 
152 DEFUN (__octave_link_message_dialog__, args, ,
153  "-*- texinfo -*-\n\
154 @deftypefn {Built-in Function} {} __octave_link_message_dialog__ (@var{dlg}, @var{msg}, @var{title})\n\
155 Undocumented internal function.\n\
156 @end deftypefn")
157 {
158  octave_value retval;
159 
160  if (args.length () == 3)
161  {
162  std::string dlg = args(0).string_value ();
163  std::string msg = args(1).string_value ();
164  std::string title = args(2).string_value ();
165 
166  if (! error_state)
167  {
169 
170  retval = octave_link::message_dialog (dlg, msg, title);
171  }
172  else
173  error ("invalid arguments");
174  }
175 
176  return retval;
177 }
178 
179 DEFUN (__octave_link_question_dialog__, args, ,
180  "-*- texinfo -*-\n\
181 @deftypefn {Built-in Function} {} __octave_link_question_dialog__ (@var{msg}, @var{title}, @var{btn1}, @var{btn2}, @var{btn3}, @var{default})\n\
182 Undocumented internal function.\n\
183 @end deftypefn")
184 {
185  octave_value retval;
186 
187  if (args.length () == 6)
188  {
189  std::string msg = args(0).string_value ();
190  std::string title = args(1).string_value ();
191  std::string btn1 = args(2).string_value ();
192  std::string btn2 = args(3).string_value ();
193  std::string btn3 = args(4).string_value ();
194  std::string btndef = args(5).string_value ();
195 
196  if (! error_state)
197  {
199 
200  retval = octave_link::question_dialog (msg, title, btn1, btn2, btn3,
201  btndef);
202  }
203  else
204  error ("invalid arguments");
205  }
206 
207  return retval;
208 }
209 
210 DEFUN (__octave_link_file_dialog__, args, ,
211  "-*- texinfo -*-\n\
212 @deftypefn {Built-in Function} {} __octave_link_file_dialog__ (@var{filterlist}, @var{title}, @var{filename}, @var{size} @var{multiselect}, @var{pathname})\n\
213 Undocumented internal function.\n\
214 @end deftypefn")
215 {
216  octave_value_list retval;
217 
218  if (args.length () == 6)
219  {
220 
221  const Array<std::string> flist = args(0).cellstr_value ();
222  std::string title = args(1).string_value ();
223  std::string filename = args(2).string_value ();
224  Matrix pos = args(3).matrix_value ();
225  std::string multi_on = args(4).string_value (); // on, off, create
226  std::string pathname = args(5).string_value ();
227 
228  octave_idx_type nel = flist.numel ();
229  octave_link::filter_list filter_lst;
230 
231  for (octave_idx_type i = 0; i < flist.rows (); i++)
232  filter_lst.push_back (std::make_pair (flist(i,0),
233  (flist.columns () > 1
234  ? flist(i,1) : "")));
235 
236  if (! error_state)
237  {
239 
240  std::list<std::string> items_lst
241  = octave_link::file_dialog (filter_lst, title, filename, pathname,
242  multi_on);
243 
244  nel = items_lst.size ();
245 
246  retval.resize (3);
247 
248  // If 3, then retval is filename, directory, and selected index.
249  if (nel <= 3)
250  {
251  int idx = 0;
252  for (std::list<std::string>::iterator it = items_lst.begin ();
253  it != items_lst.end (); it++)
254  {
255  retval(idx++) = *it;
256 
257  if (idx == 1 && retval(0).string_value ().length () == 0)
258  retval(0) = 0;
259 
260  if (idx == 3)
261  retval(2) = atoi (retval(2).string_value ().c_str ());
262  }
263  }
264  else
265  {
266  // Multiple files.
267  nel = items_lst.size () - 2;
268  Cell items (dim_vector (1, nel));
269 
270  std::list<std::string>::iterator it = items_lst.begin ();
271 
272  for (int idx = 0; idx < nel; idx++)
273  {
274  items.xelem (idx) = *it;
275  it++;
276  }
277 
278  retval(0) = items;
279  retval(1) = *it++;
280  retval(2) = atoi (it->c_str ());
281  }
282  }
283  else
284  error ("invalid arguments");
285  }
286 
287  return retval;
288 }
289 
290 DEFUN (__octave_link_list_dialog__, args, ,
291  "-*- texinfo -*-\n\
292 @deftypefn {Built-in Function} {} __octave_link_list_dialog__ (@var{list}, @var{mode}, @var{size}, @var{intial}, @var{name}, @var{prompt}, @var{ok_string}, @var{cancel_string})\n\
293 Undocumented internal function.\n\
294 @end deftypefn")
295 {
296  octave_value_list retval;
297 
298  if (args.length () == 8)
299  {
300  Cell list = args(0).cell_value ();
301  const Array<std::string> tlist = list.cellstr_value ();
302  octave_idx_type nel = tlist.numel ();
303  std::list<std::string> list_lst;
304  for (octave_idx_type i = 0; i < nel; i++)
305  list_lst.push_back (tlist(i));
306 
307  std::string mode = args(1).string_value ();
308 
309  Matrix size_matrix = args(2).matrix_value ();
310  int width = size_matrix(0);
311  int height = size_matrix(1);
312 
313  Matrix initial_matrix = args(3).matrix_value ();
314  nel = initial_matrix.numel ();
315  std::list<int> initial_lst;
316  for (octave_idx_type i = 0; i < nel; i++)
317  initial_lst.push_back (initial_matrix(i));
318 
319  std::string name = args(4).string_value ();
320  list = args(5).cell_value ();
321  const Array<std::string> plist = list.cellstr_value ();
322  nel = plist.numel ();
323  std::list<std::string> prompt_lst;
324  for (octave_idx_type i = 0; i < nel; i++)
325  prompt_lst.push_back (plist(i));
326  std::string ok_string = args(6).string_value ();
327  std::string cancel_string = args(7).string_value ();
328 
329  if (! error_state)
330  {
332 
333  std::pair<std::list<int>, int> result
334  = octave_link::list_dialog (list_lst, mode, width, height,
335  initial_lst, name, prompt_lst,
336  ok_string, cancel_string);
337 
338  std::list<int> items_lst = result.first;
339  nel = items_lst.size ();
340  Matrix items (dim_vector (1, nel));
341  octave_idx_type i = 0;
342  for (std::list<int>::iterator it = items_lst.begin ();
343  it != items_lst.end (); it++)
344  {
345  items.xelem(i++) = *it;
346  }
347 
348  retval(1) = result.second;
349  retval(0) = items;
350  }
351  else
352  error ("invalid arguments");
353  }
354 
355  return retval;
356 }
357 
358 DEFUN (__octave_link_input_dialog__, args, ,
359  "-*- texinfo -*-\n\
360 @deftypefn {Built-in Function} {} __octave_link_input_dialog__ (@var{prompt}, @var{title}, @var{rowscols}, @var{defaults})\n\
361 Undocumented internal function.\n\
362 @end deftypefn")
363 {
364  octave_value retval;
365 
366  if (args.length () == 4)
367  {
368  Cell prompt = args(0).cell_value ();
369  Array<std::string> tmp = prompt.cellstr_value ();
370  octave_idx_type nel = tmp.numel ();
371  std::list<std::string> prompt_lst;
372  for (octave_idx_type i = 0; i < nel; i++)
373  prompt_lst.push_back (tmp(i));
374 
375  std::string title = args(1).string_value ();
376 
377  Matrix rc = args(2).matrix_value ();
378  nel = rc.rows ();
379  std::list<float> nr;
380  std::list<float> nc;
381  for (octave_idx_type i = 0; i < nel; i++)
382  {
383  nr.push_back (rc(i,0));
384  nc.push_back (rc(i,1));
385  }
386 
387  Cell defaults = args(3).cell_value ();
388  tmp = defaults.cellstr_value ();
389  nel = tmp.numel ();
390  std::list<std::string> defaults_lst;
391  for (octave_idx_type i = 0; i < nel; i++)
392  defaults_lst.push_back (tmp(i));
393 
394  if (! error_state)
395  {
397 
398  std::list<std::string> items_lst
399  = octave_link::input_dialog (prompt_lst, title, nr, nc,
400  defaults_lst);
401 
402  nel = items_lst.size ();
403  Cell items (dim_vector (1, nel));
404  octave_idx_type i = 0;
405  for (std::list<std::string>::iterator it = items_lst.begin ();
406  it != items_lst.end (); it++)
407  {
408  items.xelem(i++) = *it;
409  }
410 
411  retval = items;
412  }
413  else
414  error ("invalid arguments");
415  }
416 
417  return retval;
418 }
419 
420 DEFUN (__octave_link_show_preferences__, , ,
421  "-*- texinfo -*-\n\
422 @deftypefn {Built-in Function} {} __octave_link_show_preferences__ ()\n\
423 Undocumented internal function.\n\
424 @end deftypefn")
425 {
426  octave_value retval;
427 
428  retval = octave_link::show_preferences ();
429 
430  return retval;
431 }
432 
433 DEFUN (__octave_link_show_doc__, args, ,
434  "-*- texinfo -*-\n\
435 @deftypefn {Built-in Function} {} __octave_link_show_doc__ ( @var{filename} )\n\
436 Undocumented internal function.\n\
437 @end deftypefn")
438 {
439  octave_value retval;
440  std::string file;
441 
442  if (args.length () >= 1)
443  file = args(0).string_value();
444 
445  retval = octave_link::show_doc (file);
446 
447  return retval;
448 }
449 
450 
451