GNU Octave  4.0.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-2015 John W. Eaton
4 Copyright (C) 2011-2015 Jacob Dawid
5 Copyright (C) 2011-2015 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 ())
66 }
67 
68 // OBJ should be an object of a class that is derived from the base
69 // class octave_link, or 0 to disconnect the link. It is the
70 // responsibility of the caller to delete obj.
71 
72 void
74 {
75  if (obj && instance)
76  ::error ("octave_link is already linked!");
77  else
78  instance = obj;
79 }
80 
81 void
83 {
84 }
85 
86 void
88 {
90 
92 
94 }
95 
96 void
98 {
100 
102 
104 }
105 
106 DEFUN (__octave_link_enabled__, , ,
107  "-*- texinfo -*-\n\
108 @deftypefn {Built-in Function} {} __octave_link_enabled__ ()\n\
109 Undocumented internal function.\n\
110 @end deftypefn")
111 {
113 }
114 
115 DEFUN (__octave_link_edit_file__, args, ,
116  "-*- texinfo -*-\n\
117 @deftypefn {Built-in Function} {} __octave_link_edit_file__ (@var{file})\n\
118 Undocumented internal function.\n\
119 @end deftypefn")
120 {
121  octave_value retval;
122 
123  if (args.length () == 1)
124  {
125  std::string file = args(0).string_value ();
126 
127  if (! error_state)
128  {
130 
131  retval = octave_link::edit_file (file);
132  }
133  else
134  error ("expecting file name as argument");
135  }
136  else if (args.length () == 2)
137  {
138  std::string file = args(0).string_value ();
139 
140  if (! error_state)
141  {
143 
144  retval = octave_link::prompt_new_edit_file (file);
145  }
146  else
147  error ("expecting file name as first argument");
148  }
149 
150  return retval;
151 }
152 
153 DEFUN (__octave_link_message_dialog__, args, ,
154  "-*- texinfo -*-\n\
155 @deftypefn {Built-in Function} {} __octave_link_message_dialog__ (@var{dlg}, @var{msg}, @var{title})\n\
156 Undocumented internal function.\n\
157 @end deftypefn")
158 {
159  octave_value retval;
160 
161  if (args.length () == 3)
162  {
163  std::string dlg = args(0).string_value ();
164  std::string msg = args(1).string_value ();
165  std::string title = args(2).string_value ();
166 
167  if (! error_state)
168  {
170 
171  retval = octave_link::message_dialog (dlg, msg, title);
172  }
173  else
174  error ("invalid arguments");
175  }
176 
177  return retval;
178 }
179 
180 DEFUN (__octave_link_question_dialog__, args, ,
181  "-*- texinfo -*-\n\
182 @deftypefn {Built-in Function} {} __octave_link_question_dialog__ (@var{msg}, @var{title}, @var{btn1}, @var{btn2}, @var{btn3}, @var{default})\n\
183 Undocumented internal function.\n\
184 @end deftypefn")
185 {
186  octave_value retval;
187 
188  if (args.length () == 6)
189  {
190  std::string msg = args(0).string_value ();
191  std::string title = args(1).string_value ();
192  std::string btn1 = args(2).string_value ();
193  std::string btn2 = args(3).string_value ();
194  std::string btn3 = args(4).string_value ();
195  std::string btndef = args(5).string_value ();
196 
197  if (! error_state)
198  {
200 
201  retval = octave_link::question_dialog (msg, title, btn1, btn2, btn3,
202  btndef);
203  }
204  else
205  error ("invalid arguments");
206  }
207 
208  return retval;
209 }
210 
211 DEFUN (__octave_link_file_dialog__, args, ,
212  "-*- texinfo -*-\n\
213 @deftypefn {Built-in Function} {} __octave_link_file_dialog__ (@var{filterlist}, @var{title}, @var{filename}, @var{size} @var{multiselect}, @var{pathname})\n\
214 Undocumented internal function.\n\
215 @end deftypefn")
216 {
217  octave_value_list retval;
218 
219  if (args.length () == 6)
220  {
221 
222  const Array<std::string> flist = args(0).cellstr_value ();
223  std::string title = args(1).string_value ();
224  std::string filename = args(2).string_value ();
225  Matrix pos = args(3).matrix_value ();
226  std::string multi_on = args(4).string_value (); // on, off, create
227  std::string pathname = args(5).string_value ();
228 
229  octave_idx_type nel;
230  octave_link::filter_list filter_lst;
231 
232  for (octave_idx_type i = 0; i < flist.rows (); i++)
233  filter_lst.push_back (std::make_pair (flist(i,0),
234  (flist.columns () > 1
235  ? flist(i,1) : "")));
236 
237  if (! error_state)
238  {
240 
241  std::list<std::string> items_lst
242  = octave_link::file_dialog (filter_lst, title, filename, pathname,
243  multi_on);
244 
245  nel = items_lst.size ();
246 
247  retval.resize (3);
248 
249  // If 3, then retval is filename, directory, and selected index.
250  if (nel <= 3)
251  {
252  int idx = 0;
253  for (std::list<std::string>::iterator it = items_lst.begin ();
254  it != items_lst.end (); it++)
255  {
256  retval(idx++) = *it;
257 
258  if (idx == 1 && retval(0).string_value ().length () == 0)
259  retval(0) = 0;
260 
261  if (idx == 3)
262  retval(2) = atoi (retval(2).string_value ().c_str ());
263  }
264  }
265  else
266  {
267  // Multiple files.
268  nel = items_lst.size () - 2;
269  Cell items (dim_vector (1, nel));
270 
271  std::list<std::string>::iterator it = items_lst.begin ();
272 
273  for (int idx = 0; idx < nel; idx++)
274  {
275  items.xelem (idx) = *it;
276  it++;
277  }
278 
279  retval(0) = items;
280  retval(1) = *it++;
281  retval(2) = atoi (it->c_str ());
282  }
283  }
284  else
285  error ("invalid arguments");
286  }
287 
288  return retval;
289 }
290 
291 DEFUN (__octave_link_list_dialog__, args, ,
292  "-*- texinfo -*-\n\
293 @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\
294 Undocumented internal function.\n\
295 @end deftypefn")
296 {
297  octave_value_list retval;
298 
299  if (args.length () == 8)
300  {
301  Cell list = args(0).cell_value ();
302  const Array<std::string> tlist = list.cellstr_value ();
303  octave_idx_type nel = tlist.numel ();
304  std::list<std::string> list_lst;
305  for (octave_idx_type i = 0; i < nel; i++)
306  list_lst.push_back (tlist(i));
307 
308  std::string mode = args(1).string_value ();
309 
310  Matrix size_matrix = args(2).matrix_value ();
311  int width = size_matrix(0);
312  int height = size_matrix(1);
313 
314  Matrix initial_matrix = args(3).matrix_value ();
315  nel = initial_matrix.numel ();
316  std::list<int> initial_lst;
317  for (octave_idx_type i = 0; i < nel; i++)
318  initial_lst.push_back (initial_matrix(i));
319 
320  std::string name = args(4).string_value ();
321  list = args(5).cell_value ();
322  const Array<std::string> plist = list.cellstr_value ();
323  nel = plist.numel ();
324  std::list<std::string> prompt_lst;
325  for (octave_idx_type i = 0; i < nel; i++)
326  prompt_lst.push_back (plist(i));
327  std::string ok_string = args(6).string_value ();
328  std::string cancel_string = args(7).string_value ();
329 
330  if (! error_state)
331  {
333 
334  std::pair<std::list<int>, int> result
335  = octave_link::list_dialog (list_lst, mode, width, height,
336  initial_lst, name, prompt_lst,
337  ok_string, cancel_string);
338 
339  std::list<int> items_lst = result.first;
340  nel = items_lst.size ();
341  Matrix items (dim_vector (1, nel));
342  octave_idx_type i = 0;
343  for (std::list<int>::iterator it = items_lst.begin ();
344  it != items_lst.end (); it++)
345  {
346  items.xelem(i++) = *it;
347  }
348 
349  retval(1) = result.second;
350  retval(0) = items;
351  }
352  else
353  error ("invalid arguments");
354  }
355 
356  return retval;
357 }
358 
359 DEFUN (__octave_link_input_dialog__, args, ,
360  "-*- texinfo -*-\n\
361 @deftypefn {Built-in Function} {} __octave_link_input_dialog__ (@var{prompt}, @var{title}, @var{rowscols}, @var{defaults})\n\
362 Undocumented internal function.\n\
363 @end deftypefn")
364 {
365  octave_value retval;
366 
367  if (args.length () == 4)
368  {
369  Cell prompt = args(0).cell_value ();
370  Array<std::string> tmp = prompt.cellstr_value ();
371  octave_idx_type nel = tmp.numel ();
372  std::list<std::string> prompt_lst;
373  for (octave_idx_type i = 0; i < nel; i++)
374  prompt_lst.push_back (tmp(i));
375 
376  std::string title = args(1).string_value ();
377 
378  Matrix rc = args(2).matrix_value ();
379  nel = rc.rows ();
380  std::list<float> nr;
381  std::list<float> nc;
382  for (octave_idx_type i = 0; i < nel; i++)
383  {
384  nr.push_back (rc(i,0));
385  nc.push_back (rc(i,1));
386  }
387 
388  Cell defaults = args(3).cell_value ();
389  tmp = defaults.cellstr_value ();
390  nel = tmp.numel ();
391  std::list<std::string> defaults_lst;
392  for (octave_idx_type i = 0; i < nel; i++)
393  defaults_lst.push_back (tmp(i));
394 
395  if (! error_state)
396  {
398 
399  std::list<std::string> items_lst
400  = octave_link::input_dialog (prompt_lst, title, nr, nc,
401  defaults_lst);
402 
403  nel = items_lst.size ();
404  Cell items (dim_vector (nel, 1));
405  octave_idx_type i = 0;
406  for (std::list<std::string>::iterator it = items_lst.begin ();
407  it != items_lst.end (); it++)
408  {
409  items.xelem(i++) = *it;
410  }
411 
412  retval = items;
413  }
414  else
415  error ("invalid arguments");
416  }
417 
418  return retval;
419 }
420 
421 DEFUN (__octave_link_show_preferences__, , ,
422  "-*- texinfo -*-\n\
423 @deftypefn {Built-in Function} {} __octave_link_show_preferences__ ()\n\
424 Undocumented internal function.\n\
425 @end deftypefn")
426 {
427  octave_value retval;
428 
429  retval = octave_link::show_preferences ();
430 
431  return retval;
432 }
433 
434 DEFUN (__octave_link_show_doc__, args, ,
435  "-*- texinfo -*-\n\
436 @deftypefn {Built-in Function} {} __octave_link_show_doc__ (@var{filename})\n\
437 Undocumented internal function.\n\
438 @end deftypefn")
439 {
440  octave_value retval;
441  std::string file;
442 
443  if (args.length () >= 1)
444  file = args(0).string_value();
445 
446  retval = octave_link::show_doc (file);
447 
448  return retval;
449 }
450 
451 
452 
void flush_octave_stdout(void)
Definition: pager.cc:458
void discard(size_t num)
Definition: Cell.h:35
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:275
void run(size_t num)
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:44
void error(const char *fmt,...)
Definition: error.cc:476
static void add_event_hook(event_hook_fcn f)
Definition: cmd-edit.cc:1440
octave_idx_type rows(void) const
Definition: Array.h:313
static scope_id top_scope(void)
Definition: symtab.h:1161
Array< std::string > cellstr_value(void) const
Definition: Cell.cc:146
std::string string_value(bool force=false) const
Definition: ov.h:897
static std::list< workspace_element > workspace_info(void)
Definition: symtab.h:2182
int error_state
Definition: error.cc:101
Definition: dMatrix.h:35
T & xelem(octave_idx_type n)
Definition: Array.h:353
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
Definition: oct-obj.h:93
void lock(void)
Definition: oct-mutex.h:83
octave_idx_type columns(void) const
Definition: Array.h:322
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))
static scope_id current_scope(void)
Definition: symtab.h:1163
void unlock(void)
Definition: oct-mutex.h:88