3 * Copyright 2001 the LyX Team
4 * Read the file COPYING
13 #include FORMS_H_LOCATION
16 #pragma implementation
21 #include "support/LAssert.h"
22 #include "lyx_gui_misc.h"
26 #include "FormExternal.h"
27 #include "form_external.h"
28 #include "frontends/FileDialog.h"
30 #include "support/filetools.h"
37 FormExternal::FormExternal(LyXView * lv, Dialogs * d)
38 : FormBaseBD(lv, d, _("Edit external file")),
41 d->showExternal.connect(slot(this, &FormExternal::showInset));
45 extern "C" void ExternalTemplateCB(FL_OBJECT * ob, long data)
47 FormExternal::templateCB(ob, data);
51 extern "C" void ExternalBrowseCB(FL_OBJECT * ob, long data)
53 FormExternal::browseCB(ob, data);
57 extern "C" void ExternalEditCB(FL_OBJECT * ob, long data)
59 FormExternal::editCB(ob, data);
63 extern "C" void ExternalViewCB(FL_OBJECT * ob, long data)
65 FormExternal::viewCB(ob, data);
69 extern "C" void ExternalUpdateCB(FL_OBJECT * ob, long data)
71 FormExternal::updateCB(ob, data);
75 FL_FORM * FormExternal::form() const
83 void FormExternal::connect()
85 u_ = d_->updateBufferDependent.
86 connect(slot(this, &FormExternal::updateSlot));
87 h_ = d_->hideBufferDependent.
88 connect(slot(this, &FormExternal::hide));
89 FormBaseDeprecated::connect();
93 void FormExternal::disconnect()
97 FormBaseBD::disconnect();
101 void FormExternal::updateSlot(bool switched)
110 void FormExternal::showInset(InsetExternal * inset)
114 // If connected to another inset, disconnect from it.
119 params_ = inset_->params();
121 ih_ = inset->hideDialog.connect(slot(this, &FormExternal::hide));
126 void FormExternal::build()
128 dialog_.reset(build_external());
130 fl_addto_choice(dialog_->choice_template,
131 getTemplatesComboString().c_str());
133 bc_.setOK(dialog_->button_ok);
134 bc_.setCancel(dialog_->button_cancel);
137 bc_.addReadOnly(dialog_->input_filename);
138 bc_.addReadOnly(dialog_->button_filenamebrowse);
139 bc_.addReadOnly(dialog_->input_parameters);
143 string const FormExternal::getTemplatesComboString() const
147 ExternalTemplateManager::Templates::const_iterator i1, i2;
148 i1 = ExternalTemplateManager::get().getTemplates().begin();
149 i2 = ExternalTemplateManager::get().getTemplates().end();
150 for (; i1 != i2; ++i1) {
156 result += (*i1).second.lyxName;
162 int FormExternal::getTemplateComboNumber(string const & name) const
165 ExternalTemplateManager::Templates::const_iterator i1, i2;
166 i1 = ExternalTemplateManager::get().getTemplates().begin();
167 i2 = ExternalTemplateManager::get().getTemplates().end();
168 for (; i1 != i2; ++i1) {
169 if (i1->second.lyxName == name)
173 // we can get here if a LyX document has a template not installed
179 ExternalTemplate FormExternal::getTemplate(int i) const
181 ExternalTemplateManager::Templates::const_iterator i1;
182 i1 = ExternalTemplateManager::get().getTemplates().begin();
183 for (int n = 1; n < i; ++n)
190 void FormExternal::update()
192 fl_set_input(dialog_->input_filename, params_.filename.c_str());
193 fl_set_input(dialog_->input_parameters, params_.parameters.c_str());
195 fl_set_choice(dialog_->choice_template, getTemplateComboNumber(params_.templ.lyxName));
203 void FormExternal::updateComboChange()
205 // Update the help text
206 fl_clear_browser(dialog_->browser_helptext);
207 fl_addto_browser(dialog_->browser_helptext, params_.templ.helpText.c_str());
208 fl_set_browser_topline(dialog_->browser_helptext, 0);
210 if (params_.templ.automaticProduction) {
211 fl_deactivate_object(dialog_->button_update);
212 fl_set_object_lcol(dialog_->button_update, FL_INACTIVE);
214 fl_activate_object(dialog_->button_update);
215 fl_set_object_lcol(dialog_->button_update, FL_BLACK);
220 bool FormExternal::input(FL_OBJECT *, long)
222 // FIXME: anything to do here ?
227 void FormExternal::apply()
231 if (lv_->buffer()->isReadonly())
234 params_.filename = fl_get_input(dialog_->input_filename);
235 params_.parameters = fl_get_input(dialog_->input_parameters);
236 params_.templ = getTemplate(fl_get_choice(dialog_->choice_template));
238 inset_->setFromParams(params_);
239 lv_->view()->updateInset(inset_, true);
243 void FormExternal::templateCB(FL_OBJECT * ob, long)
245 FormExternal * form = static_cast<FormExternal*>(ob->form->u_vdata);
247 // set to the chosen template
248 form->params_.templ = form->getTemplate(fl_get_choice(form->dialog_->choice_template));
250 form->updateComboChange();
254 void FormExternal::browseCB(FL_OBJECT * ob, long)
256 FormExternal * form = static_cast<FormExternal*>(ob->form->u_vdata);
258 static string current_path;
261 string p = fl_get_input(form->dialog_->input_filename);
262 string buf = MakeAbsPath(form->lv_->buffer()->fileName());
263 string buf2 = OnlyPath(buf);
266 buf = MakeAbsPath(p, buf2);
269 buf = OnlyPath(form->lv_->buffer()->fileName());
272 FileDialog fileDlg(form->lv_, _("Select external file"),
273 LFUN_SELECT_FILE_SYNC,
274 make_pair(string(_("Document")), string(buf)));
276 /// Determine the template file extension
277 ExternalTemplate const & et = form->params_.templ;
279 string regexp = et.fileRegExp;
283 // FIXME: a temporary hack until the FileDialog interface is updated
287 string const path = (once) ? current_path : buf;
288 FileDialog::Result result = fileDlg.Select(path, regexp, fl_get_input(form->dialog_->input_filename));
290 if (result.second.empty())
293 string p = result.second;
295 buf = MakeRelPath(p, buf2);
296 current_path = OnlyPath(p);
299 if (contains(p, "#") || contains(p, "~") || contains(p, "$")
300 || contains(p, "%")) {
301 WriteAlert(_("Filename can't contain any "
302 "of these characters:"),
303 // xgettext:no-c-format
304 _("'#', '~', '$' or '%'."));
309 fl_set_input(form->dialog_->input_filename, buf.c_str());
313 void FormExternal::editCB(FL_OBJECT * ob, long)
315 FormExternal * form = static_cast<FormExternal*>(ob->form->u_vdata);
318 form->inset_->editExternal();
322 void FormExternal::viewCB(FL_OBJECT * ob, long)
324 FormExternal * form = static_cast<FormExternal*>(ob->form->u_vdata);
327 form->inset_->viewExternal();
331 void FormExternal::updateCB(FL_OBJECT * ob, long)
333 FormExternal * form = static_cast<FormExternal*>(ob->form->u_vdata);
336 form->inset_->updateExternal();