GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
__fltk_uigetfile__.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2010-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #if defined (HAVE_FLTK)
28 
29 #if defined (WIN32)
30 # define WIN32_LEAN_AND_MEAN
31 #endif
32 
33 #include <string>
34 
35 #include <FL/Fl.H>
36 #include <FL/Fl_File_Chooser.H>
37 
38 // FLTK headers may include X11/X.h which defines Complex, and that
39 // conflicts with Octave's Complex typedef. We don't need the X11
40 // Complex definition in this file, so remove it before including Octave
41 // headers which may require Octave's Complex typedef.
42 #undef Complex
43 
44 #endif
45 
46 #include "dMatrix.h"
47 #include "file-ops.h"
48 
49 #include "Cell.h"
50 #include "defun-dld.h"
51 #include "errwarn.h"
52 #include "ov.h"
53 
54 DEFUN_DLD (__fltk_uigetfile__, args, ,
55  doc: /* -*- texinfo -*-
56 @deftypefn {} {} __fltk_uigetfile__ (@dots{})
57 Undocumented internal function.
58 @end deftypefn */)
59 {
60 #if defined (HAVE_FLTK)
61 
62  // Expected argument list:
63  //
64  // args(0) ... FileFilter in fltk format
65  // args(1) ... Title
66  // args(2) ... Default Filename
67  // args(3) ... PositionValue [x,y]
68  // args(4) ... SelectValue "on"/"off"/"dir"/"create"
69 
71 
72  std::string file_filter = args(0).string_value ();
73  std::string title = args(1).string_value ();
74  std::string default_name = args(2).string_value ();
75  Matrix pos = args(3).matrix_value ();
76 
77  int multi_type = Fl_File_Chooser::SINGLE;
78  std::string flabel = "Filename:";
79 
80  std::string multi = args(4).string_value ();
81  if (multi == "on")
82  multi_type = Fl_File_Chooser::MULTI;
83  else if (multi == "dir")
84  {
85  multi_type = Fl_File_Chooser::DIRECTORY;
86  flabel = "Directory:";
87  }
88  else if (multi == "create")
89  multi_type = Fl_File_Chooser::CREATE;
90 
91  Fl_File_Chooser::filename_label = flabel.c_str ();
92 
93  Fl_File_Chooser fc (default_name.c_str (), file_filter.c_str (),
94  multi_type, title.c_str ());
95 
96  fc.preview (0);
97 
98  if (multi_type == Fl_File_Chooser::CREATE)
99  fc.ok_label ("Save");
100 
101  fc.show ();
102 
103  while (fc.shown ())
104  Fl::wait ();
105 
106  if (fc.value ())
107  {
108  int file_count = fc.count ();
110 
111  // FLTK uses forward slash even for Windows
112  std::string sep = "/";
113  size_t idx;
114 
115  if (file_count == 1 && multi_type != Fl_File_Chooser::DIRECTORY)
116  {
117  fname = fc.value ();
118  idx = fname.find_last_of (sep);
119  retval(0) = fname.substr (idx + 1);
120  }
121  else
122  {
123  Cell file_cell = Cell (file_count, 1);
124  for (octave_idx_type n = 1; n <= file_count; n++)
125  {
126  fname = fc.value (n);
127  idx = fname.find_last_of (sep);
128  file_cell(n - 1) = fname.substr (idx + 1);
129  }
130  retval(0) = file_cell;
131  }
132 
133  if (multi_type == Fl_File_Chooser::DIRECTORY)
135  std::string (fc.value ()));
136  else
137  {
139  std::string (fc.directory ()) + sep);
140  retval(2) = fc.filter_value () + 1;
141  }
142  }
143 
144  fc.hide ();
145  Fl::flush ();
146 
147  return retval;
148 
149 #else
150 
151  octave_unused_parameter (args);
152 
153  err_disabled_feature ("__fltk_uigetfile__", "OpenGL and FLTK");
154 
155 #endif
156 }
157 
158 /*
159 ## No test needed for internal helper function.
160 %!assert (1)
161 */
Definition: Cell.h:37
fname
Definition: load-save.cc:767
std::string native_separator_path(const std::string &path)
Definition: file-ops.cc:372
octave_value retval
Definition: data.cc:6246
Definition: dMatrix.h:36
#define DEFUN_DLD(name, args_name, nargout_name, doc)
Macro to define an at run time dynamically loadable builtin function.
Definition: defun-dld.h:58
void err_disabled_feature(const std::string &fcn, const std::string &feature, const std::string &pkg)
Definition: errwarn.cc:50
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888