]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/controllers/ControlExternal.C
Add a buffer_path arg to InsetGraphicsMailer's params2string, string2params.
[lyx.git] / src / frontends / controllers / ControlExternal.C
index 0c7afb7fe2f6bd23d0d066bd0a4c5461152d8d09..8874e85487920ac03d6923d454eef6b155feeb01 100644 (file)
-/* 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 "BufferView.h"
-#include "ButtonControllerBase.h"
 #include "ControlExternal.h"
-#include "ControlInset.tmpl"
-#include "Dialogs.h"
-#include "Liason.h"
-#include "LyXView.h"
-#include "ViewBase.h"
 #include "buffer.h"
-#include "frontends/Alert.h"
+#include "funcrequest.h"
 #include "gettext.h"
 #include "helper_funcs.h"
 #include "lyxrc.h"
-#include "support/filetools.h"
-#include "support/lstrings.h"
 
-using std::make_pair;
+#include "insets/ExternalTemplate.h"
+
+#include "support/LAssert.h"
+#include <vector>
+
+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;
@@ -120,15 +95,12 @@ vector<string> const ControlExternal::getTemplates() const
 
 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
@@ -139,32 +111,46 @@ int ControlExternal::getTemplateNumber(string const & name) const
 
 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;
 }
 
 
+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 const title =  _("Select external file");
 
-       string const bufpath = lv_.buffer()->filePath();
+       string const bufpath = kernel().buffer()->filePath();
 
        /// Determine the template file extension
-       ExternalTemplate const & et = params().templ;
-       string pattern = et.fileRegExp;
-       if (pattern.empty())
-               pattern = "*";
+       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
-       pattern += "|";
+       pattern += '|';
 
        std::pair<string, string> dir1(N_("Documents|#o#O"),
                                  string(lyxrc.document_path));
 
-       return browseRelFile(&lv_, input, bufpath, title, pattern, dir1);
+       return browseRelFile(input, bufpath, title, pattern, false, dir1);
 }