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 "frontends/FileDialog.h"
34 #include "lyx_gui_misc.h" // WriteAlert
40 ControlExternal::ControlExternal(LyXView & lv, Dialogs & d)
41 : ControlInset<InsetExternal, InsetExternal::Params>(lv, d)
43 d_.showExternal.connect(SigC::slot(this, &ControlExternal::showInset));
47 InsetExternal::Params const ControlExternal::getParams(string const &)
49 return InsetExternal::Params();
52 InsetExternal::Params const
53 ControlExternal::getParams(InsetExternal const & inset)
55 return inset.params();
59 void ControlExternal::applyParamsToInset()
61 inset()->setFromParams(params());
62 lv_.view()->updateInset(inset(), true);
65 void ControlExternal::editExternal()
67 // fill the local, controller's copy of the Params struct with
68 // the contents of the dialog's fields.
71 // Create a local copy of the inset and initialise it with this
73 boost::scoped_ptr<InsetExternal> ie;
74 ie.reset(static_cast<InsetExternal *>(inset()->Clone(*lv_.buffer())));
75 ie->setFromParams(params());
80 void ControlExternal::viewExternal()
84 boost::scoped_ptr<InsetExternal> ie;
85 ie.reset(static_cast<InsetExternal *>(inset()->Clone(*lv_.buffer())));
86 ie->setFromParams(params());
91 void ControlExternal::updateExternal()
95 boost::scoped_ptr<InsetExternal> ie;
96 ie.reset(static_cast<InsetExternal *>(inset()->Clone(*lv_.buffer())));
97 ie->setFromParams(params());
102 vector<string> const ControlExternal::getTemplates() const
104 vector<string> result;
106 ExternalTemplateManager::Templates::const_iterator i1, i2;
107 i1 = ExternalTemplateManager::get().getTemplates().begin();
108 i2 = ExternalTemplateManager::get().getTemplates().end();
110 for (; i1 != i2; ++i1) {
111 result.push_back(i1->second.lyxName);
117 int ControlExternal::getTemplateNumber(string const & name) const
121 ExternalTemplateManager::Templates::const_iterator i1, i2;
122 i1 = ExternalTemplateManager::get().getTemplates().begin();
123 i2 = ExternalTemplateManager::get().getTemplates().end();
124 for (; i1 != i2; ++i1) {
125 if (i1->second.lyxName == name)
130 // we can get here if a LyX document has a template not installed
136 ExternalTemplate ControlExternal::getTemplate(int i) const
138 ExternalTemplateManager::Templates::const_iterator i1;
139 i1 = ExternalTemplateManager::get().getTemplates().begin();
140 for (int n = 1; n < i; ++n)
147 string const ControlExternal::Browse(string const & input) const
149 string buf = MakeAbsPath(lv_.buffer()->fileName());
150 string buf2 = OnlyPath(buf);
152 if (!input.empty()) {
153 buf = MakeAbsPath(input, buf2);
156 buf = OnlyPath(lv_.buffer()->fileName());
159 FileDialog fileDlg(&lv_,
160 _("Select external file"),
161 LFUN_SELECT_FILE_SYNC,
162 make_pair(string(_("Document")), string(buf)));
164 /// Determine the template file extension
165 ExternalTemplate const & et = params().templ;
167 string regexp = et.fileRegExp;
171 // FIXME: a temporary hack until the FileDialog interface is updated
178 string const path = (once) ? current_path : buf;
179 FileDialog::Result result = fileDlg.Select(path, regexp, input);
181 if (result.second.empty())
184 string p = result.second;
186 buf = MakeRelPath(p, buf2);
187 current_path = OnlyPath(p);
190 if (contains(p, "#") ||
194 WriteAlert(_("Filename can't contain any "
195 "of these characters:"),
196 // xgettext:no-c-format
197 _("'#', '~', '$' or '%'."));