__fltk_uigetfile__.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2010-2012 Kai Habel
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #if defined (HAVE_FLTK)
00028 
00029 #ifdef WIN32
00030 #define WIN32_LEAN_AND_MEAN
00031 #endif
00032 
00033 #include <FL/Fl.H>
00034 #include <FL/Fl_File_Chooser.H>
00035 
00036 // FLTK headers may include X11/X.h which defines Complex, and that
00037 // conflicts with Octave's Complex typedef.  We don't need the X11
00038 // Complex definition in this file, so remove it before including Octave
00039 // headers which may require Octave's Complex typedef.
00040 #undef Complex
00041 
00042 #include "defun-dld.h"
00043 #include "file-ops.h"
00044 
00045 DEFUN_DLD (__fltk_uigetfile__, args, ,
00046   "-*- texinfo -*-\n\
00047 @deftypefn {Built-in Function} {} __fltk_uigetfile__ (@dots{})\n\
00048 Undocumented internal function.\n\
00049 @end deftypefn")
00050 {
00051   // Expected argument list:
00052   //
00053   //   args(0) ... FileFilter in fltk format
00054   //   args(1) ... Title
00055   //   args(2) ... Default Filename
00056   //   args(3) ... PostionValue [x,y]
00057   //   args(4) ... SelectValue "on"/"off"/"dir"/"create"
00058 
00059   octave_value_list retval (3, octave_value (0));
00060 
00061   std::string file_filter = args(0).string_value();
00062   std::string title = args(1).string_value();
00063   std::string default_name = args(2).string_value();
00064   Matrix pos = args(3).matrix_value();
00065 
00066   int multi_type = Fl_File_Chooser::SINGLE;
00067   std::string flabel = "Filename:";
00068 
00069   std::string multi = args(4).string_value();
00070   if (multi == "on")
00071     multi_type = Fl_File_Chooser::MULTI;
00072   else if (multi == "dir")
00073     {
00074       multi_type = Fl_File_Chooser::DIRECTORY;
00075       flabel = "Directory:";
00076     }
00077   else if (multi == "create")
00078     multi_type = Fl_File_Chooser::CREATE;
00079 
00080   Fl_File_Chooser::filename_label = flabel.c_str ();
00081 
00082   Fl_File_Chooser fc (default_name.c_str (), file_filter.c_str (),
00083                       multi_type, title.c_str ());
00084 
00085   fc.preview (0);
00086 
00087   if (multi_type == Fl_File_Chooser::CREATE)
00088     fc.ok_label ("Save");
00089 
00090   fc.show ();
00091 
00092   while (fc.shown ())
00093     Fl::wait ();
00094 
00095   if (fc.value())
00096     {
00097       int file_count = fc.count ();
00098       std::string fname;
00099 
00100       //fltk uses forward slash even for windows
00101       std::string sep = "/";
00102       std::size_t idx;
00103 
00104       if (file_count == 1 && multi_type != Fl_File_Chooser::DIRECTORY)
00105         {
00106           fname = fc.value ();
00107           idx = fname.find_last_of (sep);
00108           retval(0) = fname.substr (idx + 1);
00109         }
00110       else
00111         {
00112           Cell file_cell = Cell(file_count, 1);
00113           for (octave_idx_type n = 1; n <= file_count; n++)
00114             {
00115               fname = fc.value (n);
00116               idx = fname.find_last_of (sep);
00117               file_cell(n - 1) = fname.substr (idx + 1);
00118             }
00119           retval(0) = file_cell;
00120         }
00121 
00122       if (multi_type == Fl_File_Chooser::DIRECTORY)
00123         retval(0) = std::string (fc.value ());
00124       else
00125         {
00126           retval(1) = std::string (fc.directory ()) + sep;
00127           retval(2) = fc.filter_value () + 1;
00128         }
00129     }
00130 
00131   fc.hide ();
00132   Fl::flush ();
00133 
00134   return retval;
00135 }
00136 
00137 /*
00138 
00139 ## No test needed for internal helper function.
00140 %!assert (1)
00141 
00142 */
00143 
00144 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines