/**
* \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 <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/filefilterlist.h"
+#include "support/filetools.h"
+#include "support/globbing.h"
#include "support/lstrings.h"
-#include "gettext.h"
-#include "frontends/Dialogs.h"
-#include "forms_gettext.h"
+#include "support/lyxlib.h"
+#include "support/tostr.h"
+
+#include "lyx_forms.h"
#include <boost/bind.hpp>
+#include <boost/regex.hpp>
+#include <boost/tokenizer.hpp>
+
+#include <algorithm>
+#include <map>
+#include <sstream>
+
+#include <grp.h>
+#include <pwd.h>
//#ifdef HAVE_ERRNO_H
//#include <cerrno>
# endif
#endif
-#ifdef __GNUG__
-#pragma implementation
-#endif
+using lyx::support::AbsolutePath;
+using lyx::support::AddName;
+using lyx::support::ExpandPath;
+using lyx::support::FileFilterList;
+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;
-#include "support/filetools.h"
-#include "FormFiledialog.h"
-#include "forms/form_filedialog.h"
-#include FORMS_H_LOCATION
+using std::max;
+using std::sort;
+using std::ostringstream;
+using std::string;
+using std::map;
+using std::vector;
+
+using namespace lyx::frontend;
namespace {
+/** Given a string "<glob> <glob> <glob>", expand each glob in turn.
+ * Any glob that cannot be expanded is ignored silently.
+ * Invokes \c convert_brace_glob and \c glob internally, so use only
+ * on systems supporting the Posix function 'glob'.
+ * \param mask the string "<glob> <glob> <glob>".
+ * \param directory the current working directory from
+ * which \c glob is invoked.
+ * \returns a vector of all matching file names.
+ */
+vector<string> const expand_globs(string const & mask,
+ string const & directory)
+{
+ // Split into individual globs and then call 'glob' on each one.
+ typedef boost::tokenizer<boost::char_separator<char> > Tokenizer;
+ boost::char_separator<char> const separator(" ");
+
+ vector<string> matches;
+ Tokenizer const tokens(mask, separator);
+ Tokenizer::const_iterator it = tokens.begin();
+ Tokenizer::const_iterator const end = tokens.end();
+ for (; it != end; ++it)
+ lyx::support::glob(matches, *it, directory);
+
+ return matches;
+}
+
+
// six months, in seconds
long const SIX_MONTH_SEC = 6L * 30L * 24L * 60L * 60L;
//static
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));
}
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
GroupCache lyxGroupCache;
// compares two LyXDirEntry objects content (used for sort)
-class comp_direntry {
+class comp_direntry : public std::binary_function<DirEntry, DirEntry, bool> {
public:
bool operator()(DirEntry const & r1, DirEntry const & r2) const
{
}
};
-
} // namespace anon
// 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;
// Reread: updates dialog list to match class directory
// 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();
+#endif
+ directory_ = getcwd();
dir = ::opendir(directory_.c_str());
}
string line, Temp;
string mode;
string File = directory_;
- if (File != "/") {
+ if (File != "/")
File = split(File, Temp, '/');
- }
+
while (!File.empty() || !Temp.empty()) {
string dline = "@b" + line + Temp + '/';
fl_add_browser_line(file_dlg_form_->List, dline.c_str());
++depth_;
}
- // Parses all entries of the given subdirectory
+ vector<string> const glob_matches = expand_globs(mask_, directory_);
+
time_t curTime = time(0);
rewinddir(dir);
while (dirent * entry = readdir(dir)) {
buffer += Link;
// This gives the FileType of the file that
- // is really pointed too after resolving all
+ // is really pointed to after resolving all
// symlinks. This is not necessarily the same
// as the type of Link (which could again be a
// link). Is that intended?
|| fileInfo.isChar()
|| fileInfo.isBlock()
|| fileInfo.isFifo()) {
- if (!regexMatch(fname, mask_))
+ typedef vector<string>::const_iterator viterator;
+ viterator gbegin = glob_matches.begin();
+ viterator const gend = glob_matches.end();
+ if (std::find(gbegin, gend, fname) == gend)
continue;
} else if (!(isDir = fileInfo.isDir()))
continue;
{
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;
}
-// SetMask: sets dialog file mask
-void FileDialog::Private::SetMask(string const & newmask)
+void FileDialog::Private::SetFilters(string const & mask)
+{
+ SetFilters(FileFilterList(mask));
+}
+
+
+void FileDialog::Private::SetFilters(FileFilterList const & filters)
{
- mask_ = newmask;
+ if (filters.empty())
+ return;
+
+ // Just take the first one for now.
+ typedef FileFilterList::Filter::glob_iterator glob_iterator;
+ glob_iterator const begin = filters[0].begin();
+ glob_iterator const end = filters[0].end();
+ if (begin == end)
+ return;
+
+ ostringstream ss;
+ for (glob_iterator it = begin; it != end; ++it) {
+ if (it != begin)
+ ss << ' ';
+ ss << *it;
+ }
+
+ mask_ = ss.str();
fl_set_input(file_dlg_form_->PatBox, mask_.c_str());
}
}
-FileDialog::Private::Private(Dialogs & dia)
+FileDialog::Private::Private()
{
directory_ = MakeAbsPath(string("."));
- mask_ = '*';
// 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);
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));
}
}
if (!name.empty()) {
- fl_set_object_label(ob, idex(name.c_str()));
- fl_set_button_shortcut(ob, scex(name.c_str()), 1);
+ fl_set_object_label(ob, idex(name).c_str());
+ fl_set_button_shortcut(ob, scex(name).c_str(), 1);
fl_show_object(ob);
*tmp = path;
} else {
break;
case 1: // get mask
- current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
+ current_dlg_->SetFilters(fl_get_input(file_dlg_form_->PatBox));
current_dlg_->Reread();
break;
case 10: // rescan
current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
- current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
+ current_dlg_->SetFilters(fl_get_input(file_dlg_form_->PatBox));
current_dlg_->Reread();
break;
case 11: // home
current_dlg_->SetDirectory(GetEnvPath("HOME"));
- current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
+ current_dlg_->SetFilters(fl_get_input(file_dlg_form_->PatBox));
current_dlg_->Reread();
break;
case 12: // user button 1
current_dlg_->SetDirectory(current_dlg_->user_path1_);
- current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
+ current_dlg_->SetFilters(fl_get_input(file_dlg_form_->PatBox));
current_dlg_->Reread();
break;
case 13: // user button 2
current_dlg_->SetDirectory(current_dlg_->user_path2_);
- current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
+ current_dlg_->SetFilters(fl_get_input(file_dlg_form_->PatBox));
current_dlg_->Reread();
break;
// mask was changed
string tmp = fl_get_input(file_dlg_form_->PatBox);
if (tmp != mask_) {
- SetMask(tmp);
+ SetFilters(tmp);
Reread();
return false;
}
// Select: launches dialog and returns selected file
string const FileDialog::Private::Select(string const & title,
string const & path,
- string const & mask,
+ FileFilterList const & filters,
string const & suggested)
{
// handles new mask and path
- bool isOk = true;
- if (!mask.empty()) {
- SetMask(mask);
- isOk = false;
- }
- if (!path.empty()) {
- SetDirectory(path);
- isOk = false;
- }
- if (!isOk)
- Reread();
+ SetFilters(filters);
+ SetDirectory(path);
+ Reread();
// highlight the suggested file in the browser, if it exists.
int sel = 0;
// 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());
- isOk = RunDialog();
+ bool const isOk = RunDialog();
fl_hide_form(file_dlg_form_->form);
fl_activate_all_forms();
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)
+{
+ SetFilters("*/");
+ // 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_;
+}