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
__fltk_uigetfile__.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2010-2015 Kai Habel
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #ifdef HAVE_FLTK
28 
29 #ifdef WIN32
30 #define WIN32_LEAN_AND_MEAN
31 #endif
32 
33 #include <FL/Fl.H>
34 #include <FL/Fl_File_Chooser.H>
35 
36 // FLTK headers may include X11/X.h which defines Complex, and that
37 // conflicts with Octave's Complex typedef. We don't need the X11
38 // Complex definition in this file, so remove it before including Octave
39 // headers which may require Octave's Complex typedef.
40 #undef Complex
41 
42 #endif
43 
44 #include "defun-dld.h"
45 #include "file-ops.h"
46 
47 DEFUN_DLD (__fltk_uigetfile__, args, ,
48  "-*- texinfo -*-\n\
49 @deftypefn {Built-in Function} {} __fltk_uigetfile__ (@dots{})\n\
50 Undocumented internal function.\n\
51 @end deftypefn")
52 {
53 #ifdef HAVE_FLTK
54  // Expected argument list:
55  //
56  // args(0) ... FileFilter in fltk format
57  // args(1) ... Title
58  // args(2) ... Default Filename
59  // args(3) ... PostionValue [x,y]
60  // args(4) ... SelectValue "on"/"off"/"dir"/"create"
61 
62  octave_value_list retval (3, octave_value (0));
63 
64  std::string file_filter = args(0).string_value ();
65  std::string title = args(1).string_value ();
66  std::string default_name = args(2).string_value ();
67  Matrix pos = args(3).matrix_value ();
68 
69  int multi_type = Fl_File_Chooser::SINGLE;
70  std::string flabel = "Filename:";
71 
72  std::string multi = args(4).string_value ();
73  if (multi == "on")
74  multi_type = Fl_File_Chooser::MULTI;
75  else if (multi == "dir")
76  {
77  multi_type = Fl_File_Chooser::DIRECTORY;
78  flabel = "Directory:";
79  }
80  else if (multi == "create")
81  multi_type = Fl_File_Chooser::CREATE;
82 
83  Fl_File_Chooser::filename_label = flabel.c_str ();
84 
85  Fl_File_Chooser fc (default_name.c_str (), file_filter.c_str (),
86  multi_type, title.c_str ());
87 
88  fc.preview (0);
89 
90  if (multi_type == Fl_File_Chooser::CREATE)
91  fc.ok_label ("Save");
92 
93  fc.show ();
94 
95  while (fc.shown ())
96  Fl::wait ();
97 
98  if (fc.value ())
99  {
100  int file_count = fc.count ();
101  std::string fname;
102 
103  //fltk uses forward slash even for windows
104  std::string sep = "/";
105  size_t idx;
106 
107  if (file_count == 1 && multi_type != Fl_File_Chooser::DIRECTORY)
108  {
109  fname = fc.value ();
110  idx = fname.find_last_of (sep);
111  retval(0) = fname.substr (idx + 1);
112  }
113  else
114  {
115  Cell file_cell = Cell (file_count, 1);
116  for (octave_idx_type n = 1; n <= file_count; n++)
117  {
118  fname = fc.value (n);
119  idx = fname.find_last_of (sep);
120  file_cell(n - 1) = fname.substr (idx + 1);
121  }
122  retval(0) = file_cell;
123  }
124 
125  if (multi_type == Fl_File_Chooser::DIRECTORY)
126  retval(0) = file_ops::native_separator_path (std::string (fc.value ()));
127  else
128  {
129  retval(1) = file_ops::native_separator_path (
130  std::string (fc.directory ()) + sep);
131  retval(2) = fc.filter_value () + 1;
132  }
133  }
134 
135  fc.hide ();
136  Fl::flush ();
137 
138  return retval;
139 #else
140  error ("__fltk_uigetfile__: not available without OpenGL and FLTK libraries");
141  return octave_value ();
142 #endif
143 }
144 
145 /*
146 ## No test needed for internal helper function.
147 %!assert (1)
148 */
149 
Definition: Cell.h:35
static std::string native_separator_path(const std::string &path)
Definition: file-ops.cc:370
void error(const char *fmt,...)
Definition: error.cc:476
Definition: dMatrix.h:35
#define DEFUN_DLD(name, args_name, nargout_name, doc)
Definition: defun-dld.h:59
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))