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
152 #warning Candidate for using browseRelFile
155 string const bufpath = lv_.buffer()->filePath();
157 if (!input.empty()) {
158 buf = MakeAbsPath(input, bufpath);
164 FileDialog fileDlg(&lv_,
165 _("Select external file"),
166 LFUN_SELECT_FILE_SYNC,
167 make_pair(string(_("Document|#o#O")), string(buf)));
169 /// Determine the template file extension
170 ExternalTemplate const & et = params().templ;
172 string regexp = et.fileRegExp;
176 // FIXME: a temporary hack until the FileDialog interface is updated
183 string const path = (once) ? current_path : buf;
184 FileDialog::Result result = fileDlg.Select(path, regexp, input);
186 if (result.second.empty())
189 string p = result.second;
191 buf = MakeRelPath(p, bufpath);
192 current_path = OnlyPath(p);
195 if (contains(p, "#") ||
199 Alert::alert(_("Filename can't contain any "
200 "of these characters:"),
201 // xgettext:no-c-format
202 _("'#', '~', '$' or '%'."));