]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormExternal.C
95748aa7362d6388631646052d2ba8505e7b7431
[lyx.git] / src / frontends / xforms / FormExternal.C
1 /**
2  * \file FormExternal.C
3  * Copyright 2001 the LyX Team
4  * Read the file COPYING
5  *
6  * \author unknown
7  * \author John Levon
8  */
9
10 #include <config.h>
11 #include <utility>
12
13 #include FORMS_H_LOCATION
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include "debug.h"
20 #include "gettext.h"
21 #include "support/LAssert.h"
22 #include "lyx_gui_misc.h"
23 #include "Dialogs.h"
24 #include "LyXView.h"
25 #include "buffer.h"
26 #include "FormExternal.h"
27 #include "form_external.h"
28 #include "frontends/FileDialog.h"
29 #include "LString.h"
30 #include "support/filetools.h"
31
32 using std::pair;
33 using std::make_pair;
34 using std::endl;
35 using SigC::slot;
36
37 FormExternal::FormExternal(LyXView * lv, Dialogs * d)
38         : FormBaseBD(lv, d, _("Edit external file")),
39         inset_(0), ih_(0)
40 {
41         d->showExternal.connect(slot(this, &FormExternal::showInset));
42 }
43
44
45 extern "C" void ExternalTemplateCB(FL_OBJECT * ob, long data)
46 {
47         FormExternal::templateCB(ob, data);
48 }
49
50
51 extern "C" void ExternalBrowseCB(FL_OBJECT * ob, long data)
52 {
53         FormExternal::browseCB(ob, data);
54 }
55
56
57 extern "C" void ExternalEditCB(FL_OBJECT * ob, long data)
58 {
59         FormExternal::editCB(ob, data);
60 }
61
62
63 extern "C" void ExternalViewCB(FL_OBJECT * ob, long data)
64 {
65         FormExternal::viewCB(ob, data);
66 }
67
68
69 extern "C" void ExternalUpdateCB(FL_OBJECT * ob, long data)
70 {
71         FormExternal::updateCB(ob, data);
72 }
73
74
75 FL_FORM * FormExternal::form() const
76 {
77         if (dialog_.get())
78                 return dialog_->form;
79         return 0;
80 }
81
82
83 void FormExternal::connect()
84 {
85         u_ = d_->updateBufferDependent.
86                  connect(slot(this, &FormExternal::updateSlot));
87         h_ = d_->hideBufferDependent.
88                  connect(slot(this, &FormExternal::hide));
89         FormBaseDeprecated::connect();
90 }
91
92
93 void FormExternal::disconnect()
94 {
95         inset_ = 0;
96         ih_.disconnect();
97         FormBaseBD::disconnect();
98 }
99
100
101 void FormExternal::updateSlot(bool switched)
102 {
103         if (switched)
104                 hide();
105         else
106                 update();
107 }
108
109
110 void FormExternal::showInset(InsetExternal * inset)
111 {
112         Assert(inset);
113
114         // If connected to another inset, disconnect from it.
115         if (inset_)
116                 ih_.disconnect();
117
118         inset_ = inset;
119         params_ = inset_->params();
120
121         ih_ = inset->hideDialog.connect(slot(this, &FormExternal::hide));
122         show();
123 }
124
125
126 void FormExternal::build()
127 {
128         dialog_.reset(build_external());
129
130         fl_addto_choice(dialog_->choice_template,
131                         getTemplatesComboString().c_str());
132
133         bc_.setOK(dialog_->button_ok);
134         bc_.setCancel(dialog_->button_cancel);
135         bc_.refresh();
136
137         bc_.addReadOnly(dialog_->input_filename);
138         bc_.addReadOnly(dialog_->button_filenamebrowse);
139         bc_.addReadOnly(dialog_->input_parameters);
140 }
141
142
143 string const FormExternal::getTemplatesComboString() const
144 {
145         string result;
146         bool first = true;
147         ExternalTemplateManager::Templates::const_iterator i1, i2;
148         i1 = ExternalTemplateManager::get().getTemplates().begin();
149         i2 = ExternalTemplateManager::get().getTemplates().end();
150         for (; i1 != i2; ++i1) {
151                 if (!first)
152                         result += "|";
153                 else
154                         first = false;
155
156                 result += (*i1).second.lyxName;
157         }
158         return result;
159 }
160
161
162 int FormExternal::getTemplateComboNumber(string const & name) const
163 {
164         int i = 1;
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)
170                         return i;
171                 ++i;
172         }
173         // we can get here if a LyX document has a template not installed
174         // on this machine.
175         return 0;
176 }
177
178
179 ExternalTemplate FormExternal::getTemplate(int i) const
180 {
181         ExternalTemplateManager::Templates::const_iterator i1;
182         i1 = ExternalTemplateManager::get().getTemplates().begin();
183         for (int n = 1; n < i; ++n)
184                 ++i1;
185
186         return (*i1).second;
187 }
188
189
190 void FormExternal::update()
191 {
192         fl_set_input(dialog_->input_filename, params_.filename.c_str());
193         fl_set_input(dialog_->input_parameters, params_.parameters.c_str());
194
195         fl_set_choice(dialog_->choice_template, getTemplateComboNumber(params_.templ.lyxName));
196
197         updateComboChange();
198
199         bc_.valid();
200 }
201
202
203 void FormExternal::updateComboChange()
204 {
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);
209
210         if (params_.templ.automaticProduction) {
211                 fl_deactivate_object(dialog_->button_update);
212                 fl_set_object_lcol(dialog_->button_update, FL_INACTIVE);
213         } else {
214                 fl_activate_object(dialog_->button_update);
215                 fl_set_object_lcol(dialog_->button_update, FL_BLACK);
216         }
217 }
218
219
220 bool FormExternal::input(FL_OBJECT *, long)
221 {
222         // FIXME: anything to do here ?
223         return true;
224 }
225
226
227 void FormExternal::apply()
228 {
229         Assert(inset_);
230
231         if (lv_->buffer()->isReadonly())
232                 return;
233
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));
237
238         inset_->setFromParams(params_);
239         lv_->view()->updateInset(inset_, true);
240 }
241
242
243 void FormExternal::templateCB(FL_OBJECT * ob, long)
244 {
245         FormExternal * form = static_cast<FormExternal*>(ob->form->u_vdata);
246
247         // set to the chosen template
248         form->params_.templ = form->getTemplate(fl_get_choice(form->dialog_->choice_template));
249
250         form->updateComboChange();
251 }
252
253
254 void FormExternal::browseCB(FL_OBJECT * ob, long)
255 {
256         FormExternal * form = static_cast<FormExternal*>(ob->form->u_vdata);
257
258         static string current_path;
259         static int once = 0;
260         
261         string p = fl_get_input(form->dialog_->input_filename);
262         string buf = MakeAbsPath(form->lv_->buffer()->fileName());
263         string buf2 = OnlyPath(buf);
264
265         if (!p.empty()) {
266                 buf = MakeAbsPath(p, buf2);
267                 buf = OnlyPath(buf);
268         } else {
269                 buf = OnlyPath(form->lv_->buffer()->fileName());
270         }
271     
272         FileDialog fileDlg(form->lv_, _("Select external file"),
273                 LFUN_SELECT_FILE_SYNC,
274                 make_pair(string(_("Document")), string(buf)));
275         
276         /// Determine the template file extension
277         ExternalTemplate const & et = form->params_.templ;
278
279         string regexp = et.fileRegExp;
280         if (regexp.empty())
281                 regexp = "*";
282
283         // FIXME: a temporary hack until the FileDialog interface is updated
284         regexp += "|";
285
286         while (1) {
287                 string const path = (once) ? current_path : buf;
288                 FileDialog::Result result = fileDlg.Select(path, regexp, fl_get_input(form->dialog_->input_filename));
289
290                 if (result.second.empty())
291                         return;
292
293                 string p = result.second;
294
295                 buf = MakeRelPath(p, buf2);
296                 current_path = OnlyPath(p);
297                 once = 1;
298                 
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 '%'."));
305                 } else
306                         break;
307         }
308
309         fl_set_input(form->dialog_->input_filename, buf.c_str());
310 }
311
312
313 void FormExternal::editCB(FL_OBJECT * ob, long)
314 {
315         FormExternal * form = static_cast<FormExternal*>(ob->form->u_vdata);
316
317         form->apply();
318         form->inset_->editExternal();
319 }
320
321
322 void FormExternal::viewCB(FL_OBJECT * ob, long)
323 {
324         FormExternal * form = static_cast<FormExternal*>(ob->form->u_vdata);
325         
326         form->apply();
327         form->inset_->viewExternal();
328 }
329
330
331 void FormExternal::updateCB(FL_OBJECT * ob, long)
332 {
333         FormExternal * form = static_cast<FormExternal*>(ob->form->u_vdata);
334         
335         form->apply();
336         form->inset_->updateExternal();
337 }