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
36 #include "BufferView.h"
41 ControlExternal::ControlExternal(LyXView & lv, Dialogs & d)
42 : ControlInset<InsetExternal, InsetExternal::Params>(lv, d)
44 d_.showExternal.connect(SigC::slot(this, &ControlExternal::showInset));
48 InsetExternal::Params const ControlExternal::getParams(string const &)
50 return InsetExternal::Params();
53 InsetExternal::Params const
54 ControlExternal::getParams(InsetExternal const & inset)
56 return inset.params();
60 void ControlExternal::applyParamsToInset()
62 inset()->setFromParams(params());
63 lv_.view()->updateInset(inset(), true);
66 void ControlExternal::editExternal()
68 // fill the local, controller's copy of the Params struct with
69 // the contents of the dialog's fields.
72 // Create a local copy of the inset and initialise it with this
74 boost::scoped_ptr<InsetExternal> ie;
75 ie.reset(static_cast<InsetExternal *>(inset()->clone(*lv_.buffer())));
76 ie->setFromParams(params());
81 void ControlExternal::viewExternal()
85 boost::scoped_ptr<InsetExternal> ie;
86 ie.reset(static_cast<InsetExternal *>(inset()->clone(*lv_.buffer())));
87 ie->setFromParams(params());
92 void ControlExternal::updateExternal()
96 boost::scoped_ptr<InsetExternal> ie;
97 ie.reset(static_cast<InsetExternal *>(inset()->clone(*lv_.buffer())));
98 ie->setFromParams(params());
100 ie->updateExternal();
103 vector<string> const ControlExternal::getTemplates() const
105 vector<string> result;
107 ExternalTemplateManager::Templates::const_iterator i1, i2;
108 i1 = ExternalTemplateManager::get().getTemplates().begin();
109 i2 = ExternalTemplateManager::get().getTemplates().end();
111 for (; i1 != i2; ++i1) {
112 result.push_back(i1->second.lyxName);
118 int ControlExternal::getTemplateNumber(string const & name) const
122 ExternalTemplateManager::Templates::const_iterator i1, i2;
123 i1 = ExternalTemplateManager::get().getTemplates().begin();
124 i2 = ExternalTemplateManager::get().getTemplates().end();
125 for (; i1 != i2; ++i1) {
126 if (i1->second.lyxName == name)
131 // we can get here if a LyX document has a template not installed
137 ExternalTemplate ControlExternal::getTemplate(int i) const
139 ExternalTemplateManager::Templates::const_iterator i1;
140 i1 = ExternalTemplateManager::get().getTemplates().begin();
141 for (int n = 1; n < i; ++n)
148 string const ControlExternal::Browse(string const & input) const
150 string buf = MakeAbsPath(lv_.buffer()->fileName());
151 string buf2 = OnlyPath(buf);
153 if (!input.empty()) {
154 buf = MakeAbsPath(input, buf2);
157 buf = OnlyPath(lv_.buffer()->fileName());
160 FileDialog fileDlg(&lv_,
161 _("Select external file"),
162 LFUN_SELECT_FILE_SYNC,
163 make_pair(string(_("Document")), string(buf)));
165 /// Determine the template file extension
166 ExternalTemplate const & et = params().templ;
168 string regexp = et.fileRegExp;
172 // FIXME: a temporary hack until the FileDialog interface is updated
179 string const path = (once) ? current_path : buf;
180 FileDialog::Result result = fileDlg.Select(path, regexp, input);
182 if (result.second.empty())
185 string p = result.second;
187 buf = MakeRelPath(p, buf2);
188 current_path = OnlyPath(p);
191 if (contains(p, "#") ||
195 WriteAlert(_("Filename can't contain any "
196 "of these characters:"),
197 // xgettext:no-c-format
198 _("'#', '~', '$' or '%'."));