]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/FormFiledialog.C
Tiny clean-ups.
[lyx.git] / src / frontends / xforms / FormFiledialog.C
index 4e6063fc971012f783596ea319b6a23a1b1f9d88..3d9d5b05c4c0d608a058e826fb99f0790237c248 100644 (file)
@@ -1,35 +1,39 @@
 /**
  * \file FormFiledialog.C
- * Copyright 2001 the LyX Team
- * Read the file COPYING
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
  * \author unknown
- * \author John Levon, moz@compsoc.man.ac.uk
+ * \author John Levon
+ *
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
 
-#include <unistd.h>
-#include <cstdlib>
-#include <pwd.h>
-#include <grp.h>
-//#include <cstring>
-#include <map>
-#include <algorithm>
+#include "FormFiledialog.h"
+#include "forms/form_filedialog.h"
 
-using std::map;
-using std::max;
-using std::sort;
+#include "forms_gettext.h"
+#include "xforms_helpers.h"
+
+#include "frontends/Dialogs.h"
 
-#include "frontends/Alert.h"
 #include "support/FileInfo.h"
 #include "support/lyxlib.h"
 #include "support/lstrings.h"
-#include "gettext.h"
-#include "frontends/Dialogs.h"
-#include "forms_gettext.h"
+#include "support/tostr.h"
+#include "support/filetools.h"
+
+#include "lyx_forms.h"
 
 #include <boost/bind.hpp>
+#include <boost/regex.hpp>
+
+#include <algorithm>
+#include <map>
+#include <grp.h>
+#include <pwd.h>
 
 //#ifdef HAVE_ERRNO_H
 //#include <cerrno>
@@ -50,14 +54,25 @@ using std::sort;
 # endif
 #endif
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
+using lyx::support::AbsolutePath;
+using lyx::support::AddName;
+using lyx::support::ExpandPath;
+using lyx::support::FileInfo;
+using lyx::support::getcwd;
+using lyx::support::GetEnvPath;
+using lyx::support::LyXReadLink;
+using lyx::support::MakeAbsPath;
+using lyx::support::OnlyFilename;
+using lyx::support::split;
+using lyx::support::subst;
+using lyx::support::suffixIs;
+using lyx::support::trim;
+
+using std::max;
+using std::sort;
+using std::string;
+using std::map;
 
-#include "support/filetools.h"
-#include "FormFiledialog.h"
-#include "forms/form_filedialog.h"
-#include FORMS_H_LOCATION
 
 namespace {
 
@@ -114,7 +129,7 @@ private:
 void UserCache::add(uid_t ID) const
 {
        struct passwd const * entry = getpwuid(ID);
-       users[ID] = entry ? entry->pw_name : tostr(ID);
+       users[ID] = entry ? entry->pw_name : tostr(int(ID));
 }
 
 
@@ -147,7 +162,7 @@ string const & GroupCache::find(gid_t ID) const
 void GroupCache::add(gid_t ID) const
 {
        struct group const * entry = getgrgid(ID);
-       groups[ID] = entry ? entry->gr_name : tostr(ID);
+       groups[ID] = entry ? entry->gr_name : tostr(int(ID));
 }
 
 // local instances
@@ -179,6 +194,33 @@ public:
 // static members
 FD_filedialog * FileDialog::Private::file_dlg_form_ = 0;
 FileDialog::Private * FileDialog::Private::current_dlg_ = 0;
+int FileDialog::Private::minw_ = 0;
+int FileDialog::Private::minh_ = 0;
+
+
+namespace {
+
+boost::regex getRegex(string const & pat)
+{
+       // We massage the pattern a bit so that the usual
+       // shell pattern we all are used to will work.
+       // One nice thing about using a real regex is that
+       // things like "*.*[^~]" will work also.
+       // build the regex string.
+       string pattern = subst(pat, ".", "\\.");
+       pattern = subst(pattern, "*", ".*");
+
+       boost::regex reg(pattern);
+       return reg;
+}
+
+
+bool globMatch(string const & a, boost::regex const & reg)
+{
+       return boost::regex_match(a, reg);
+}
+
+} // namespace anon
 
 
 // Reread: updates dialog list to match class directory
@@ -187,9 +229,12 @@ void FileDialog::Private::Reread()
        // Opens directory
        DIR * dir = ::opendir(directory_.c_str());
        if (!dir) {
+// FIXME: re-add ...
+#if 0
                Alert::err_alert(_("Warning! Couldn't open directory."),
-                            directory_);
-               directory_ = lyx::getcwd();
+                       directory_);
+#endif
+               directory_ = getcwd();
                dir = ::opendir(directory_.c_str());
        }
 
@@ -204,13 +249,13 @@ void FileDialog::Private::Reread()
        // Splits complete directory name into directories and compute depth
        depth_ = 0;
        string line, Temp;
-       char szMode[15];
+       string mode;
        string File = directory_;
        if (File != "/") {
                File = split(File, Temp, '/');
        }
        while (!File.empty() || !Temp.empty()) {
-               string dline = "@b"+line + Temp + '/';
+               string dline = "@b" + line + Temp + '/';
                fl_add_browser_line(file_dlg_form_->List, dline.c_str());
                File = split(File, Temp, '/');
                line += ' ';
@@ -218,18 +263,20 @@ void FileDialog::Private::Reread()
        }
 
        // Parses all entries of the given subdirectory
+       boost::regex reg = getRegex(mask_);
+
        time_t curTime = time(0);
        rewinddir(dir);
-       while (dirent * pDirEntry = readdir(dir)) {
+       while (dirent * entry = readdir(dir)) {
                bool isLink = false, isDir = false;
 
                // If the pattern doesn't start with a dot, skip hidden files
                if (!mask_.empty() && mask_[0] != '.' &&
-                   pDirEntry->d_name[0] == '.')
+                   entry->d_name[0] == '.')
                        continue;
 
                // Gets filename
-               string fname = pDirEntry->d_name;
+               string fname = entry->d_name;
 
                // Under all circumstances, "." and ".." are not wanted
                if (fname == "." || fname == "..")
@@ -244,10 +291,10 @@ void FileDialog::Private::Reread()
                if (!fileInfo.isOK())
                        continue;
 
-               fileInfo.modeString(szMode);
-               unsigned int nlink = fileInfo.getNumberOfLinks();
-               string user =   lyxUserCache.find(fileInfo.getUid());
-               string group = lyxGroupCache.find(fileInfo.getGid());
+               mode = fileInfo.modeString();
+               unsigned int const nlink = fileInfo.getNumberOfLinks();
+               string const user  = lyxUserCache.find(fileInfo.getUid());
+               string const group = lyxGroupCache.find(fileInfo.getGid());
 
                time_t modtime = fileInfo.getModificationTime();
                string Time = ctime(&modtime);
@@ -265,21 +312,22 @@ void FileDialog::Private::Reread()
                        Time.erase(16, string::npos);
                }
 
-               string Buffer = string(szMode) + ' ' +
+               string buffer = mode + ' ' +
                        tostr(nlink) + ' ' +
                        user + ' ' +
                        group + ' ' +
                        Time.substr(4, string::npos) + ' ';
 
-               Buffer += pDirEntry->d_name;
-               Buffer += fileInfo.typeIndicator();
+               buffer += entry->d_name;
+               buffer += fileInfo.typeIndicator();
 
-               if ((isLink = fileInfo.isLink())) {
+               isLink = fileInfo.isLink();
+               if (isLink) {
                        string Link;
 
                        if (LyXReadLink(File, Link)) {
-                               Buffer += " -> ";
-                               Buffer += Link;
+                               buffer += " -> ";
+                               buffer += Link;
 
                                // This gives the FileType of the file that
                                // is really pointed too after resolving all
@@ -289,7 +337,7 @@ void FileDialog::Private::Reread()
                                //                              JV 199902
                                fileInfo.newFile(File);
                                if (fileInfo.isOK())
-                                       Buffer += fileInfo.typeIndicator();
+                                       buffer += fileInfo.typeIndicator();
                                else
                                        continue;
                        }
@@ -300,7 +348,7 @@ void FileDialog::Private::Reread()
                    || fileInfo.isChar()
                    || fileInfo.isBlock()
                    || fileInfo.isFifo()) {
-                       if (!regexMatch(fname, mask_))
+                       if (!globMatch(fname, reg))
                                continue;
                } else if (!(isDir = fileInfo.isDir()))
                        continue;
@@ -308,14 +356,15 @@ void FileDialog::Private::Reread()
                DirEntry tmp;
 
                // Note ls_entry_ is an string!
-               tmp.ls_entry_ = Buffer;
+               tmp.ls_entry_ = buffer;
                // creates used name
                string temp = fname;
-               if (isDir) temp += '/';
+               if (isDir)
+                       temp += '/';
 
                tmp.name_ = temp;
                // creates displayed name
-               temp = pDirEntry->d_name;
+               temp = entry->d_name;
                if (isLink)
                        temp += '@';
                else
@@ -347,14 +396,17 @@ void FileDialog::Private::SetDirectory(string const & path)
 {
        string tmp;
        if (path.empty())
-               tmp = lyx::getcwd();
+               tmp = getcwd();
        else
                tmp = MakeAbsPath(ExpandPath(path), directory_);
 
        // must check the directory exists
        DIR * dir = ::opendir(tmp.c_str());
        if (!dir) {
+// FIXME: re-add ...
+#if 0
                Alert::err_alert(_("Warning! Couldn't open directory."), tmp);
+#endif
        } else {
                ::closedir(dir);
                directory_ = tmp;
@@ -378,7 +430,7 @@ void FileDialog::Private::SetInfoLine(string const & line)
 }
 
 
-FileDialog::Private::Private(Dialogs & dia)
+FileDialog::Private::Private()
 {
        directory_ = MakeAbsPath(string("."));
        mask_ = '*';
@@ -386,6 +438,8 @@ FileDialog::Private::Private(Dialogs & dia)
        // Creates form if necessary.
        if (!file_dlg_form_) {
                file_dlg_form_ = build_filedialog(this);
+               minw_ = file_dlg_form_->form->w;
+               minh_ = file_dlg_form_->form->h;
                // Set callbacks. This means that we don't need a patch file
                fl_set_object_callback(file_dlg_form_->DirBox,
                                       C_LyXFileDlg_FileDlgCB, 0);
@@ -415,7 +469,7 @@ FileDialog::Private::Private(Dialogs & dia)
        fl_hide_object(file_dlg_form_->User1);
        fl_hide_object(file_dlg_form_->User2);
 
-       r_ = dia.redrawGUI.connect(boost::bind(&FileDialog::Private::redraw, this));
+       r_ = Dialogs::redrawGUI().connect(boost::bind(&FileDialog::Private::redraw, this));
 }
 
 
@@ -433,28 +487,30 @@ void FileDialog::Private::redraw()
 
 
 // SetButton: sets file selector user button action
-void FileDialog::Private::SetButton(int iIndex, string const & name_,
-                          string const & pszPath)
+void FileDialog::Private::SetButton(int index, string const & name,
+                          string const & path)
 {
        FL_OBJECT * ob;
-       string * pTemp;
+       string * tmp;
 
-       if (iIndex == 0) {
+       if (index == 0) {
                ob = file_dlg_form_->User1;
-               pTemp = &user_path1_;
-       } else if (iIndex == 1) {
+               tmp = &user_path1_;
+       } else if (index == 1) {
                ob = file_dlg_form_->User2;
-               pTemp = &user_path2_;
-       } else return;
+               tmp = &user_path2_;
+       } else {
+               return;
+       }
 
-       if (!name_.empty()) {
-               fl_set_object_label(ob, idex(name_.c_str()));
-               fl_set_button_shortcut(ob, scex(name_.c_str()), 1);
+       if (!name.empty()) {
+               fl_set_object_label(ob, idex(name).c_str());
+               fl_set_button_shortcut(ob, scex(name).c_str(), 1);
                fl_show_object(ob);
-               *pTemp = pszPath;
+               *tmp = path;
        } else {
                fl_hide_object(ob);
-               pTemp->erase();
+               tmp->erase();
        }
 }
 
@@ -490,8 +546,7 @@ bool FileDialog::Private::RunDialog()
                        if (HandleOK())
                                return x_sync_kludge(true);
 
-               } else if (ob == file_dlg_form_->Cancel
-                          || force_cancel_)
+               } else if (ob == file_dlg_form_->Cancel || force_cancel_)
                        return x_sync_kludge(false);
 
                else if (force_ok_)
@@ -579,7 +634,7 @@ bool FileDialog::Private::HandleDoubleClick()
        // set info line
        bool isDir = true;
        int const select_ = fl_get_browser(file_dlg_form_->List);
-       if (select_ > depth_)  {
+       if (select_ > depth_) {
                tmp = dir_entries_[select_ - depth_ - 1].name_;
                SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
                if (!suffixIs(tmp, '/')) {
@@ -708,7 +763,7 @@ string const FileDialog::Private::Select(string const & title,
        if (!filename.empty()) {
                for (int i = 0; i < fl_get_browser_maxline(file_dlg_form_->List); ++i) {
                        string s = fl_get_browser_line(file_dlg_form_->List, i + 1);
-                       s = strip(frontStrip(s));
+                       s = trim(s);
                        if (s == filename) {
                                sel = i + 1;
                                break;
@@ -716,7 +771,8 @@ string const FileDialog::Private::Select(string const & title,
                }
        }
 
-       if (sel != 0) fl_select_browser_line(file_dlg_form_->List, sel);
+       if (sel != 0)
+               fl_select_browser_line(file_dlg_form_->List, sel);
        int const top = max(sel - 5, 1);
        fl_set_browser_topline(file_dlg_form_->List, top);
 
@@ -727,11 +783,18 @@ string const FileDialog::Private::Select(string const & title,
 
        // runs dialog
        SetInfoLine(string());
+       setEnabled(file_dlg_form_->Filename, true);
        fl_set_input(file_dlg_form_->Filename, suggested.c_str());
        fl_set_button(file_dlg_form_->Cancel, 0);
        fl_set_button(file_dlg_form_->Ready, 0);
        fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->Filename);
        fl_deactivate_all_forms();
+       // Prevent xforms crashing if the dialog gets too small by preventing
+       // it from being shrunk beyond a minimum size.
+       // calls to fl_set_form_minsize/maxsize apply only to the next
+       // fl_show_form(), so this comes first.
+       fl_set_form_minsize(file_dlg_form_->form, minw_, minh_);
+
        fl_show_form(file_dlg_form_->form,
                     FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
                     title.c_str());
@@ -752,3 +815,65 @@ string const FileDialog::Private::Select(string const & title,
                file_name_ = AddName(fl_get_input(file_dlg_form_->DirBox), file_name_);
        return file_name_;
 }
+
+
+// SelectDir: launches dialog and returns selected directory
+string const FileDialog::Private::SelectDir(string const & title,
+                                        string const & path,
+                                        string const & suggested)
+{
+       SetMask("*/");
+       // handles new path
+       bool isOk = true;
+       if (!path.empty()) {
+               // handle case where path does not end with "/"
+               // remerge path+suggested and check if it is a valid path
+               if (!suggested.empty()) {
+                       string tmp = suggested;
+                       if (!suffixIs(tmp, '/'))
+                               tmp += '/';
+                       string full_path = path;
+                       full_path += tmp;
+                       // check if this is really a directory
+                       DIR * dir = ::opendir(full_path.c_str());
+                       if (dir)
+                               SetDirectory(full_path);
+                       else
+                               SetDirectory(path);
+               } else
+                       SetDirectory(path);
+               isOk = false;
+       }
+       if (!isOk)
+               Reread();
+
+       // checks whether dialog can be started
+       if (current_dlg_)
+               return string();
+       current_dlg_ = this;
+
+       // runs dialog
+       SetInfoLine(string());
+       fl_set_input(file_dlg_form_->Filename, "");
+       setEnabled(file_dlg_form_->Filename, false);
+       fl_set_button(file_dlg_form_->Cancel, 0);
+       fl_set_button(file_dlg_form_->Ready, 0);
+       fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->DirBox);
+       fl_deactivate_all_forms();
+       fl_show_form(file_dlg_form_->form,
+                    FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
+                    title.c_str());
+
+       isOk = RunDialog();
+
+       fl_hide_form(file_dlg_form_->form);
+       fl_activate_all_forms();
+       current_dlg_ = 0;
+
+       // Returns directory or string() if no valid selection was made
+       if (!isOk)
+               return string();
+
+       file_name_ = fl_get_input(file_dlg_form_->DirBox);
+       return file_name_;
+}