-/* This file is part of
- * ======================================================
- *
- * LyX, The Document Processor
- *
- * Copyright 2001 The LyX Team.
- *
- * ======================================================
- *
+/**
* \file ControlExternal.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
* \author Asger Alstrup
- * \author John Levon, moz@compsoc.man.ac.uk
- * \author Angus Leeming, a.leeming@ic.ac.uk
+ * \author John Levon
+ * \author Angus Leeming
+ *
+ * Full author contact details are available in file CREDITS
*/
#include <config.h>
-#include <utility>
-#include <vector>
-
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
#include "ControlExternal.h"
#include "buffer.h"
-#include "Dialogs.h"
-#include "Liason.h"
-#include "LyXView.h"
-#include "support/filetools.h"
-#include "frontends/FileDialog.h"
-#include "lyx_gui_misc.h" // WriteAlert
+#include "funcrequest.h"
#include "gettext.h"
+#include "helper_funcs.h"
+#include "lyxrc.h"
+
+#include "insets/ExternalTemplate.h"
+
+#include "support/LAssert.h"
+#include <vector>
-using std::make_pair;
+using namespace lyx::support;
using std::vector;
-ControlExternal::ControlExternal(LyXView & lv, Dialogs & d)
- : ControlInset<InsetExternal, InsetExternal::Params>(lv, d)
-{
- d_.showExternal.connect(SigC::slot(this, &ControlExternal::showInset));
-}
+ControlExternal::ControlExternal(Dialog & parent)
+ : Dialog::Controller(parent)
+{}
-InsetExternal::Params const ControlExternal::getParams(string const &)
-{
- return InsetExternal::Params();
-}
-InsetExternal::Params const
-ControlExternal::getParams(InsetExternal const & inset)
+bool ControlExternal::initialiseParams(string const & data)
{
- return inset.params();
+ params_.reset(new InsetExternal::Params);
+ InsetExternalMailer::string2params(data, *params_);
+ return true;
}
-void ControlExternal::applyParamsToInset()
+void ControlExternal::clearParams()
{
- inset()->setFromParams(params());
- lv_.view()->updateInset(inset(), true);
+ params_.reset();
}
-void ControlExternal::editExternal()
-{
- // fill the local, controller's copy of the Params struct with
- // the contents of the dialog's fields.
- view().apply();
-
- // Create a local copy of the inset and initialise it with this
- // params struct.
- boost::scoped_ptr<InsetExternal> ie;
- ie.reset(static_cast<InsetExternal *>(inset()->Clone(*lv_.buffer())));
- ie->setFromParams(params());
- ie->editExternal();
+void ControlExternal::dispatchParams()
+{
+ string const lfun = InsetExternalMailer::params2string(params());
+ kernel().dispatch(FuncRequest(LFUN_INSET_APPLY, lfun));
}
-void ControlExternal::viewExternal()
+
+void ControlExternal::setParams(InsetExternal::Params const & p)
{
- view().apply();
+ Assert(params_.get());
+ *params_ = p;
+}
- boost::scoped_ptr<InsetExternal> ie;
- ie.reset(static_cast<InsetExternal *>(inset()->Clone(*lv_.buffer())));
- ie->setFromParams(params());
- ie->viewExternal();
+InsetExternal::Params const & ControlExternal::params() const
+{
+ Assert(params_.get());
+ return *params_;
}
-void ControlExternal::updateExternal()
-{
- view().apply();
- boost::scoped_ptr<InsetExternal> ie;
- ie.reset(static_cast<InsetExternal *>(inset()->Clone(*lv_.buffer())));
- ie->setFromParams(params());
+void ControlExternal::editExternal()
+{
+ Assert(params_.get());
- ie->updateExternal();
+ dialog().view().apply();
+ string const lfun = InsetExternalMailer::params2string(params());
+ kernel().dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
}
+
vector<string> const ControlExternal::getTemplates() const
{
vector<string> result;
int ControlExternal::getTemplateNumber(string const & name) const
{
- int i = 0;
-
ExternalTemplateManager::Templates::const_iterator i1, i2;
i1 = ExternalTemplateManager::get().getTemplates().begin();
i2 = ExternalTemplateManager::get().getTemplates().end();
- for (; i1 != i2; ++i1) {
+ for (int i = 0; i1 != i2; ++i1, ++i) {
if (i1->second.lyxName == name)
return i;
- ++i;
}
// we can get here if a LyX document has a template not installed
ExternalTemplate ControlExternal::getTemplate(int i) const
{
- ExternalTemplateManager::Templates::const_iterator i1;
- i1 = ExternalTemplateManager::get().getTemplates().begin();
- for (int n = 1; n < i; ++n)
- ++i1;
+ ExternalTemplateManager::Templates::const_iterator i1
+ = ExternalTemplateManager::get().getTemplates().begin();
+
+ std::advance(i1, i);
- return (*i1).second;
+ return i1->second;
}
+namespace {
+
+ExternalTemplate const * getTemplatePtr(InsetExternal::Params const & params)
+{
+ ExternalTemplateManager & etm = ExternalTemplateManager::get();
+ ExternalTemplate const & templ = etm.getTemplateByName(params.templatename);
+ if (templ.lyxName.empty())
+ return 0;
+ return &templ;
+}
+
+} // namespace anon
+
+
string const ControlExternal::Browse(string const & input) const
{
- string buf = MakeAbsPath(lv_.buffer()->fileName());
- string buf2 = OnlyPath(buf);
-
- if (!input.empty()) {
- buf = MakeAbsPath(input, buf2);
- buf = OnlyPath(buf);
- } else {
- buf = OnlyPath(lv_.buffer()->fileName());
- }
-
- FileDialog fileDlg(&lv_,
- _("Select external file"),
- LFUN_SELECT_FILE_SYNC,
- make_pair(string(_("Document")), string(buf)));
-
- /// Determine the template file extension
- ExternalTemplate const & et = params().templ;
+ string const title = _("Select external file");
+
+ string const bufpath = kernel().buffer()->filePath();
- string regexp = et.fileRegExp;
- if (regexp.empty())
- regexp = "*";
+ /// Determine the template file extension
+ string pattern = "*";
+ ExternalTemplate const * const et_ptr = getTemplatePtr(params());
+ if (et_ptr)
+ pattern = et_ptr->fileRegExp;
// FIXME: a temporary hack until the FileDialog interface is updated
- regexp += "|";
-
- static int once = 0;
- string current_path;
-
- while (1) {
- string const path = (once) ? current_path : buf;
- FileDialog::Result result = fileDlg.Select(path, regexp, input);
-
- if (result.second.empty())
- return string();
-
- string p = result.second;
-
- buf = MakeRelPath(p, buf2);
- current_path = OnlyPath(p);
- once = 1;
-
- if (contains(p, "#") ||
- contains(p, "~") ||
- contains(p, "$") ||
- contains(p, "%")) {
- WriteAlert(_("Filename can't contain any "
- "of these characters:"),
- // xgettext:no-c-format
- _("'#', '~', '$' or '%'."));
- } else
- break;
- }
+ pattern += '|';
+
+ std::pair<string, string> dir1(N_("Documents|#o#O"),
+ string(lyxrc.document_path));
- return buf;
+ return browseRelFile(input, bufpath, title, pattern, false, dir1);
}