1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2001 The LyX Team.
8 * ======================================================
10 * \file ControlExternal.C
11 * \author Asger Alstrup
12 * \author John Levon, moz@compsoc.man.ac.uk
13 * \author Angus Leeming, a.leeming@ic.ac.uk
22 #pragma implementation
26 #include "ButtonControllerBase.h"
27 #include "ControlExternal.h"
32 #include "support/filetools.h"
33 #include "support/lstrings.h"
34 #include "frontends/FileDialog.h"
35 #include "frontends/Alert.h"
37 #include "BufferView.h"
42 ControlExternal::ControlExternal(LyXView & lv, Dialogs & d)
43 : ControlInset<InsetExternal, InsetExternal::Params>(lv, d)
45 d_.showExternal.connect(SigC::slot(this, &ControlExternal::showInset));
49 InsetExternal::Params const ControlExternal::getParams(string const &)
51 return InsetExternal::Params();
54 InsetExternal::Params const
55 ControlExternal::getParams(InsetExternal const & inset)
57 return inset.params();
61 void ControlExternal::applyParamsToInset()
63 inset()->setFromParams(params());
64 lv_.view()->updateInset(inset(), true);
67 void ControlExternal::editExternal()
69 // fill the local, controller's copy of the Params struct with
70 // the contents of the dialog's fields.
73 // Create a local copy of the inset and initialise it with this
75 boost::scoped_ptr<InsetExternal> ie;
76 ie.reset(static_cast<InsetExternal *>(inset()->clone(*lv_.buffer())));
77 ie->setFromParams(params());
82 void ControlExternal::viewExternal()
86 boost::scoped_ptr<InsetExternal> ie;
87 ie.reset(static_cast<InsetExternal *>(inset()->clone(*lv_.buffer())));
88 ie->setFromParams(params());
93 void ControlExternal::updateExternal()
97 boost::scoped_ptr<InsetExternal> ie;
98 ie.reset(static_cast<InsetExternal *>(inset()->clone(*lv_.buffer())));
99 ie->setFromParams(params());
101 ie->updateExternal();
104 vector<string> const ControlExternal::getTemplates() const
106 vector<string> result;
108 ExternalTemplateManager::Templates::const_iterator i1, i2;
109 i1 = ExternalTemplateManager::get().getTemplates().begin();
110 i2 = ExternalTemplateManager::get().getTemplates().end();
112 for (; i1 != i2; ++i1) {
113 result.push_back(i1->second.lyxName);
119 int ControlExternal::getTemplateNumber(string const & name) const
123 ExternalTemplateManager::Templates::const_iterator i1, i2;
124 i1 = ExternalTemplateManager::get().getTemplates().begin();
125 i2 = ExternalTemplateManager::get().getTemplates().end();
126 for (; i1 != i2; ++i1) {
127 if (i1->second.lyxName == name)
132 // we can get here if a LyX document has a template not installed
138 ExternalTemplate ControlExternal::getTemplate(int i) const
140 ExternalTemplateManager::Templates::const_iterator i1;
141 i1 = ExternalTemplateManager::get().getTemplates().begin();
142 for (int n = 1; n < i; ++n)
149 string const ControlExternal::Browse(string const & input) const
151 string buf = MakeAbsPath(lv_.buffer()->fileName());
152 string buf2 = OnlyPath(buf);
154 if (!input.empty()) {
155 buf = MakeAbsPath(input, buf2);
158 buf = OnlyPath(lv_.buffer()->fileName());
161 FileDialog fileDlg(&lv_,
162 _("Select external file"),
163 LFUN_SELECT_FILE_SYNC,
164 make_pair(string(_("Document")), string(buf)));
166 /// Determine the template file extension
167 ExternalTemplate const & et = params().templ;
169 string regexp = et.fileRegExp;
173 // FIXME: a temporary hack until the FileDialog interface is updated
180 string const path = (once) ? current_path : buf;
181 FileDialog::Result result = fileDlg.Select(path, regexp, input);
183 if (result.second.empty())
186 string p = result.second;
188 buf = MakeRelPath(p, buf2);
189 current_path = OnlyPath(p);
192 if (contains(p, "#") ||
196 Alert::alert(_("Filename can't contain any "
197 "of these characters:"),
198 // xgettext:no-c-format
199 _("'#', '~', '$' or '%'."));