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
25 #include "ControlExternal.h"
30 #include "support/filetools.h"
31 #include "frontends/FileDialog.h"
32 #include "lyx_gui_misc.h" // WriteAlert
38 ControlExternal::ControlExternal(LyXView & lv, Dialogs & d)
39 : ControlInset<InsetExternal, InsetExternal::Params>(lv, d)
41 d_.showExternal.connect(SigC::slot(this, &ControlExternal::showInset));
45 InsetExternal::Params const ControlExternal::getParams(string const &)
47 return InsetExternal::Params();
50 InsetExternal::Params const
51 ControlExternal::getParams(InsetExternal const & inset)
53 return inset.params();
57 void ControlExternal::applyParamsToInset()
59 inset()->setFromParams(params());
60 lv_.view()->updateInset(inset(), true);
63 void ControlExternal::editExternal()
65 // fill the local, controller's copy of the Params struct with
66 // the contents of the dialog's fields.
69 // Create a local copy of the inset and initialise it with this
71 boost::scoped_ptr<InsetExternal> ie;
72 ie.reset(static_cast<InsetExternal *>(inset()->Clone(*lv_.buffer())));
73 ie->setFromParams(params());
78 void ControlExternal::viewExternal()
82 boost::scoped_ptr<InsetExternal> ie;
83 ie.reset(static_cast<InsetExternal *>(inset()->Clone(*lv_.buffer())));
84 ie->setFromParams(params());
89 void ControlExternal::updateExternal()
93 boost::scoped_ptr<InsetExternal> ie;
94 ie.reset(static_cast<InsetExternal *>(inset()->Clone(*lv_.buffer())));
95 ie->setFromParams(params());
100 vector<string> const ControlExternal::getTemplates() const
102 vector<string> result;
104 ExternalTemplateManager::Templates::const_iterator i1, i2;
105 i1 = ExternalTemplateManager::get().getTemplates().begin();
106 i2 = ExternalTemplateManager::get().getTemplates().end();
108 for (; i1 != i2; ++i1) {
109 result.push_back(i1->second.lyxName);
115 int ControlExternal::getTemplateNumber(string const & name) const
119 ExternalTemplateManager::Templates::const_iterator i1, i2;
120 i1 = ExternalTemplateManager::get().getTemplates().begin();
121 i2 = ExternalTemplateManager::get().getTemplates().end();
122 for (; i1 != i2; ++i1) {
123 if (i1->second.lyxName == name)
128 // we can get here if a LyX document has a template not installed
134 ExternalTemplate ControlExternal::getTemplate(int i) const
136 ExternalTemplateManager::Templates::const_iterator i1;
137 i1 = ExternalTemplateManager::get().getTemplates().begin();
138 for (int n = 1; n < i; ++n)
145 string const ControlExternal::Browse(string const & input) const
147 string buf = MakeAbsPath(lv_.buffer()->fileName());
148 string buf2 = OnlyPath(buf);
150 if (!input.empty()) {
151 buf = MakeAbsPath(input, buf2);
154 buf = OnlyPath(lv_.buffer()->fileName());
157 FileDialog fileDlg(&lv_,
158 _("Select external file"),
159 LFUN_SELECT_FILE_SYNC,
160 make_pair(string(_("Document")), string(buf)));
162 /// Determine the template file extension
163 ExternalTemplate const & et = params().templ;
165 string regexp = et.fileRegExp;
169 // FIXME: a temporary hack until the FileDialog interface is updated
176 string const path = (once) ? current_path : buf;
177 FileDialog::Result result = fileDlg.Select(path, regexp, input);
179 if (result.second.empty())
182 string p = result.second;
184 buf = MakeRelPath(p, buf2);
185 current_path = OnlyPath(p);
188 if (contains(p, "#") ||
192 WriteAlert(_("Filename can't contain any "
193 "of these characters:"),
194 // xgettext:no-c-format
195 _("'#', '~', '$' or '%'."));