From fa6f44b0c7f9b2f0555080d18811e5878649eec2 Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Tue, 14 Jan 2003 21:51:34 +0000 Subject: [PATCH] =?utf8?q?(J=EF=BF=BD=EF=BF=BDrgen=20Spitzm=EF=BF=BD?= =?utf8?q?=EF=BF=BDller):=20implement=20a=20BrowseDir=20capablility=20in?= =?utf8?q?=20the=20File=20Dialog.=20Fixes=20bug=20824.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@5955 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/frontends/ChangeLog | 4 ++ src/frontends/FileDialog.h | 7 +++ src/frontends/controllers/ChangeLog | 6 +++ src/frontends/controllers/ControlPrefs.C | 6 +++ src/frontends/controllers/ControlPrefs.h | 3 ++ src/frontends/controllers/helper_funcs.C | 34 +++++++++++++ src/frontends/controllers/helper_funcs.h | 14 ++++++ src/frontends/qt2/ChangeLog | 7 +++ src/frontends/qt2/FileDialog.C | 28 ++++++++++- src/frontends/qt2/QPrefsDialog.C | 8 +-- src/frontends/xforms/FileDialog.C | 20 ++++++++ src/frontends/xforms/FormFiledialog.C | 64 ++++++++++++++++++++++++ src/frontends/xforms/FormFiledialog.h | 4 ++ src/frontends/xforms/FormPreferences.C | 26 ++++++---- 14 files changed, 216 insertions(+), 15 deletions(-) diff --git a/src/frontends/ChangeLog b/src/frontends/ChangeLog index ed7fe8902c..e6c9967f3e 100644 --- a/src/frontends/ChangeLog +++ b/src/frontends/ChangeLog @@ -1,3 +1,7 @@ +2003-01-11 Juergen Spitzmueller + + * FileDialog.h: implement opendir (browse directory) [bug 824] + 2003-01-12 Lars Gullik Bjønnes * Makefile.am (LIBS): delete diff --git a/src/frontends/FileDialog.h b/src/frontends/FileDialog.h index 633eba4401..0ac2899f7f 100644 --- a/src/frontends/FileDialog.h +++ b/src/frontends/FileDialog.h @@ -77,6 +77,13 @@ public: Result const open(string const & path = string(), string const & mask = string(), string const & suggested = string()); + + /** + * Choose a directory, starting in directory \param + * path. + */ + Result const opendir(string const & path = string(), + string const & suggested = string()); /** * Choose a file for saving, starting in directory \param diff --git a/src/frontends/controllers/ChangeLog b/src/frontends/controllers/ChangeLog index 8665b2c666..1c29e38de9 100644 --- a/src/frontends/controllers/ChangeLog +++ b/src/frontends/controllers/ChangeLog @@ -1,3 +1,9 @@ +2003-01-11 Juergen Spitzmueller + + * helper_funcs.[Ch]: implement browseDir (browse directory) [bug 824] + + * ControlPrefs.[Ch]: implement browsedir for the use of use browseDir. + 2002-12-02 Lars Gullik Bjønnes * ControlDocument.C: use BoostFormat.h instead of boost/format.hpp diff --git a/src/frontends/controllers/ControlPrefs.C b/src/frontends/controllers/ControlPrefs.C index fea1d823c1..660149bc9f 100644 --- a/src/frontends/controllers/ControlPrefs.C +++ b/src/frontends/controllers/ControlPrefs.C @@ -110,6 +110,12 @@ string const ControlPrefs::browse(string const & file, string const & title) } +string const ControlPrefs::browsedir(string const & path, string const & title) +{ + return browseDir(&lv_, path, title); +} + + void ControlPrefs::redrawGUI() { // we must be sure to get the new values first diff --git a/src/frontends/controllers/ControlPrefs.h b/src/frontends/controllers/ControlPrefs.h index 0d110aa89e..10d9a74e5c 100644 --- a/src/frontends/controllers/ControlPrefs.h +++ b/src/frontends/controllers/ControlPrefs.h @@ -47,6 +47,9 @@ public: /// general browse string const browse(string const & file, string const & title); + + /// browse directory + string const browsedir(string const & path, string const & title); /// redraw widgets (for xforms color change) void redrawGUI(); diff --git a/src/frontends/controllers/helper_funcs.C b/src/frontends/controllers/helper_funcs.C index e853bb6cdc..8fa6d4d9d4 100644 --- a/src/frontends/controllers/helper_funcs.C +++ b/src/frontends/controllers/helper_funcs.C @@ -89,6 +89,40 @@ string const browseRelFile(LyXView * lv, string const & filename, } +string const browseDir(LyXView * lv, string const & pathname, + string const & title, + pair const & dir1, + pair const & dir2) +{ + string lastPath("."); + if (!pathname.empty()) + lastPath = OnlyPath(pathname); + + FileDialog fileDlg(lv, title, LFUN_SELECT_FILE_SYNC, dir1, dir2); + + FileDialog::Result result; + + while (true) { + result = fileDlg.opendir(lastPath, + OnlyFilename(pathname)); + + if (result.second.empty()) + return result.second; + + lastPath = OnlyPath(result.second); + + if (result.second.find_first_of("#~$% ") == string::npos) + break; + + Alert::alert(_("directory name can't contain any " + "of these characters:"), + _("space, '#', '~', '$' or '%'.")); + } + + return result.second; +} + + // sorry this is just a temporary hack we should include vspace.h! (Jug) extern const char * stringFromUnit(int); diff --git a/src/frontends/controllers/helper_funcs.h b/src/frontends/controllers/helper_funcs.h index fa1e689e05..ea84e07dd4 100644 --- a/src/frontends/controllers/helper_funcs.h +++ b/src/frontends/controllers/helper_funcs.h @@ -58,6 +58,20 @@ browseRelFile(LyXView * lv, string const & filename, std::make_pair(string(), string())); +/** Launch a file dialog and return the chosen directory. + pathname: a suggested pathname. + title: the title of the dialog. + dir1 = (name, dir), dir2 = (name, dir): extra buttons on the dialog. +*/ +string const +browseDir(LyXView * lv, string const & pathname, + string const & title, + std::pair const & dir1 = + std::make_pair(string(), string()), + std::pair const & dir2 = + std::make_pair(string(), string())); + + /// Returns a vector of units that can be used to create a valid LaTeX length. std::vector const getLatexUnits(); diff --git a/src/frontends/qt2/ChangeLog b/src/frontends/qt2/ChangeLog index 618d2fc19b..9afe657fb3 100644 --- a/src/frontends/qt2/ChangeLog +++ b/src/frontends/qt2/ChangeLog @@ -1,3 +1,9 @@ +2003-01-11 Juergen Spitzmueller + + * FileDialog.C: implement opendir [bug 824], fix mask. + + * QPrefsDialog.C: use browsedir for paths [bug 824] + 2003-01-13 Lars Gullik Bjønnes * qgridview.cpp: rename to... @@ -53,6 +59,7 @@ * QtView.h: * QtView.C: + 2003-01-04 Juergen Spitzmueller * QURL.C: isValid(): fix validation. diff --git a/src/frontends/qt2/FileDialog.C b/src/frontends/qt2/FileDialog.C index 28a5342392..de126c81f6 100644 --- a/src/frontends/qt2/FileDialog.C +++ b/src/frontends/qt2/FileDialog.C @@ -88,7 +88,7 @@ FileDialog::Result const FileDialog::open(string const & path, { string filter(mask); if (mask.empty()) - filter = _("*|All files"); + filter = _("All files (*)"); LyXFileDialog dlg(path, filter, title_, private_->b1, private_->b2); lyxerr[Debug::GUI] << "Select with path \"" << path @@ -108,3 +108,29 @@ FileDialog::Result const FileDialog::open(string const & path, dlg.hide(); return result; } + + +FileDialog::Result const FileDialog::opendir(string const & path, + string const & suggested) +{ + string filter = _("Directories"); + + LyXFileDialog dlg(path, filter, title_, private_->b1, private_->b2); + lyxerr[Debug::GUI] << "Select with path \"" << path + << "\", suggested \"" << suggested << endl; + + dlg.setMode(QFileDialog::DirectoryOnly); + + if (!suggested.empty()) + dlg.setSelection(toqstr(suggested)); + + FileDialog::Result result; + lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl; + result.first = FileDialog::Chosen; + int res = dlg.exec(); + lyxerr[Debug::GUI] << "result " << res << endl; + if (res == QDialog::Accepted) + result.second = string(dlg.selectedFile().data()); + dlg.hide(); + return result; +} \ No newline at end of file diff --git a/src/frontends/qt2/QPrefsDialog.C b/src/frontends/qt2/QPrefsDialog.C index 25f1058c75..3fef978421 100644 --- a/src/frontends/qt2/QPrefsDialog.C +++ b/src/frontends/qt2/QPrefsDialog.C @@ -545,7 +545,7 @@ void QPrefsDialog::select_dict() void QPrefsDialog::select_templatedir() { - string file(form_->controller().browse(fromqstr(pathsModule->templateDirED->text()), _("Select a document templates directory"))); + string file(form_->controller().browsedir(fromqstr(pathsModule->templateDirED->text()), _("Select a document templates directory"))); if (!file.empty()) pathsModule->templateDirED->setText(toqstr(file)); } @@ -553,7 +553,7 @@ void QPrefsDialog::select_templatedir() void QPrefsDialog::select_tempdir() { - string file(form_->controller().browse(fromqstr(pathsModule->tempDirED->text()), _("Select a temporary directory"))); + string file(form_->controller().browsedir(fromqstr(pathsModule->tempDirED->text()), _("Select a temporary directory"))); if (!file.empty()) pathsModule->tempDirED->setText(toqstr(file)); } @@ -561,7 +561,7 @@ void QPrefsDialog::select_tempdir() void QPrefsDialog::select_backupdir() { - string file(form_->controller().browse(fromqstr(pathsModule->backupDirED->text()), _("Select a backups directory"))); + string file(form_->controller().browsedir(fromqstr(pathsModule->backupDirED->text()), _("Select a backups directory"))); if (!file.empty()) pathsModule->backupDirED->setText(toqstr(file)); } @@ -569,7 +569,7 @@ void QPrefsDialog::select_backupdir() void QPrefsDialog::select_workingdir() { - string file(form_->controller().browse(fromqstr(pathsModule->workingDirED->text()), _("Select a document directory"))); + string file(form_->controller().browsedir(fromqstr(pathsModule->workingDirED->text()), _("Select a document directory"))); if (!file.empty()) pathsModule->workingDirED->setText(toqstr(file)); } diff --git a/src/frontends/xforms/FileDialog.C b/src/frontends/xforms/FileDialog.C index e629237bf3..23306538e2 100644 --- a/src/frontends/xforms/FileDialog.C +++ b/src/frontends/xforms/FileDialog.C @@ -54,6 +54,26 @@ FileDialog::Result const FileDialog::save(string const & path, string const & ma } +FileDialog::Result const FileDialog::opendir(string const & path, string const & suggested) +{ + lyxerr[Debug::GUI] << "filedialog open with path \"" << path << "\", suggested \"" + << suggested << '"' << endl; + + // no support for asynchronous selection yet + + lv_->prohibitInput(); + + FileDialog::Result result; + + result.first = FileDialog::Chosen; + result.second = private_->SelectDir(title_, path, suggested); + + lv_->allowInput(); + + return result; +} + + FileDialog::Result const FileDialog::open(string const & path, string const & mask, string const & suggested) { string filter = mask; diff --git a/src/frontends/xforms/FormFiledialog.C b/src/frontends/xforms/FormFiledialog.C index 029fcff833..027ee217f2 100644 --- a/src/frontends/xforms/FormFiledialog.C +++ b/src/frontends/xforms/FormFiledialog.C @@ -29,6 +29,7 @@ using std::sort; #include "gettext.h" #include "frontends/Dialogs.h" #include "forms_gettext.h" +#include "xforms_helpers.h" #include @@ -736,6 +737,7 @@ 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); @@ -767,3 +769,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_; +} diff --git a/src/frontends/xforms/FormFiledialog.h b/src/frontends/xforms/FormFiledialog.h index 0222844508..8dbc73ab6f 100644 --- a/src/frontends/xforms/FormFiledialog.h +++ b/src/frontends/xforms/FormFiledialog.h @@ -69,6 +69,10 @@ public: string const & pszPath = string(), string const & pszMask = string(), string const & pszSuggested = string()); + /// launches dialog and returns selected directory + string const SelectDir(string const & pszTitle = string(), + string const & pszPath = string(), + string const & pszSuggested = string()); /// XForms objects callback (static) static void FileDlgCB(FL_OBJECT *, long); /// Callback for double click in list diff --git a/src/frontends/xforms/FormPreferences.C b/src/frontends/xforms/FormPreferences.C index 4e50c2d4c4..9321ef1c7e 100644 --- a/src/frontends/xforms/FormPreferences.C +++ b/src/frontends/xforms/FormPreferences.C @@ -2186,29 +2186,35 @@ bool FormPreferences::Paths::input(FL_OBJECT const * const ob) } if (ob == dialog_->button_default_path_browse) { - string f = parent_.controller().browse( + string f = parent_.controller().browsedir( fl_get_input(dialog_->input_default_path), _("Default path")); - fl_set_input(dialog_->input_default_path, f.c_str()); + if (!f.empty()) + fl_set_input(dialog_->input_default_path, f.c_str()); } else if (ob == dialog_->button_template_path_browse) { - string f = parent_.controller().browse( + string f = parent_.controller().browsedir( fl_get_input(dialog_->input_template_path), _("Template path")); - fl_set_input(dialog_->input_template_path, f.c_str()); + if (!f.empty()) + fl_set_input(dialog_->input_template_path, f.c_str()); } else if (ob == dialog_->button_temp_dir_browse) { - string f = parent_.controller().browse( + string f = parent_.controller().browsedir( fl_get_input(dialog_->input_temp_dir), _("Temporary dir")); - fl_set_input(dialog_->input_temp_dir, f.c_str()); + if (!f.empty()) + fl_set_input(dialog_->input_temp_dir, f.c_str()); } else if (ob == dialog_->button_lastfiles_browse) { string f = parent_.controller().browse( fl_get_input(dialog_->input_lastfiles), _("Last files")); - fl_set_input(dialog_->input_lastfiles, f.c_str()); + if (!f.empty()) + fl_set_input(dialog_->input_lastfiles, f.c_str()); } else if (ob == dialog_->button_backup_path_browse) { - string f = parent_.controller().browse( + string f = parent_.controller().browsedir( fl_get_input(dialog_->input_backup_path), _("Backup path")); - fl_set_input(dialog_->input_backup_path, f.c_str()); + if (!f.empty()) + fl_set_input(dialog_->input_backup_path, f.c_str()); } else if (ob == dialog_->button_serverpipe_browse) { string f = parent_.controller().browse( fl_get_input(dialog_->input_serverpipe), _("LyX server pipes")); - fl_set_input(dialog_->input_serverpipe, f.c_str()); + if (!f.empty()) + fl_set_input(dialog_->input_serverpipe, f.c_str()); } return activate; -- 2.39.5