]> git.lyx.org Git - lyx.git/blob - src/insets/insetexternal.C
caaad36f993af5db92b3e2f6b129cc73d6a104ba
[lyx.git] / src / insets / insetexternal.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright 1995 Matthias Ettrich
7  *          Copyright 1995-2000 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include FORMS_H_LOCATION
18 #include <cstdio>
19
20 #include "insetexternal.h"
21 #include "ExternalTemplate.h"
22 #include "lyx_gui_misc.h" // CancelCloseBoxCB
23 #include "BufferView.h"
24 #include "buffer.h"
25 #include "filedlg.h"
26 #include "lyx_main.h"
27 #include "LaTeXFeatures.h"
28 #include "support/filetools.h"
29 #include "support/lstrings.h"
30 #include "support/path.h"
31 #include "support/syscall.h"
32
33 using std::endl;
34
35
36 InsetExternal::InsetExternal() 
37         : form_external(0)
38 {
39         tempname = TmpFileName();
40 }
41
42
43 InsetExternal::~InsetExternal()
44 {
45         remove(tempname.c_str());
46 }
47
48
49 extern "C"
50 void ExternalTemplateCB(FL_OBJECT * ob, long data)
51 {
52         InsetExternal::templateCB(ob, data);
53 }
54
55
56 extern "C"
57 void ExternalBrowseCB(FL_OBJECT * ob, long data)
58 {
59         InsetExternal::browseCB(ob, data);
60 }
61
62
63 extern "C"
64 void ExternalEditCB(FL_OBJECT * ob, long data)
65 {
66         InsetExternal::editCB(ob, data);
67 }
68
69
70 extern "C"
71 void ExternalViewCB(FL_OBJECT * ob, long data)
72 {
73         InsetExternal::viewCB(ob, data);
74 }
75
76
77 extern "C"
78 void ExternalUpdateCB(FL_OBJECT * ob, long data)
79 {
80         InsetExternal::updateCB(ob, data);
81 }
82
83
84 extern "C"
85 void ExternalOKCB(FL_OBJECT * ob, long data)
86 {
87         InsetExternal::okCB(ob, data);
88 }
89
90
91 extern "C"
92 void ExternalCancelCB(FL_OBJECT * ob, long data)
93 {
94         InsetExternal::cancelCB(ob, data);
95 }
96
97
98 void InsetExternal::templateCB(FL_OBJECT * ob, long)
99 {
100         Holder * holder = static_cast<Holder*>(ob->form->u_vdata);
101         InsetExternal * inset = holder->inset;
102         ExternalTemplate et = inset->getTemplate(inset->getCurrentTemplate());
103         // Update the help text
104         fl_clear_browser(inset->form_external->helptext);
105         fl_addto_browser(inset->form_external->helptext, et.helpText.c_str());
106         fl_set_browser_topline(inset->form_external->helptext, 0);
107 }
108
109
110 void InsetExternal::browseCB(FL_OBJECT * ob, long)
111 {
112         Holder * holder = static_cast<Holder*>(ob->form->u_vdata);
113         InsetExternal * inset = holder->inset;
114
115         static string current_path;
116         static int once = 0;
117         LyXFileDlg fileDlg;
118         
119         string p = inset->filename;
120         string buf = MakeAbsPath(holder->view->buffer()->fileName());
121         string buf2 = OnlyPath(buf);
122         if (!p.empty()) {
123                 buf = MakeAbsPath(p, buf2);
124                 buf = OnlyPath(buf);
125         } else {
126                 buf = OnlyPath(holder->view->buffer()->fileName().c_str());
127         }
128        
129         fileDlg.SetButton(0, _("Document"), buf); 
130
131         /// Determine the template file extension
132         ExternalTemplate et = inset->getTemplate(inset->getCurrentTemplate());
133         string regexp = et.fileRegExp;
134         if (regexp.empty()) {
135                 regexp = "*";
136         }
137
138         bool error = false;
139         do {
140                 //      ProhibitInput(current_view);
141                 if (once) {
142                         p = fileDlg.Select(_("External inset file"),
143                                            current_path,
144                                            regexp, string());
145                 } else {
146                         p = fileDlg.Select(_("External inset file"), buf,
147                                            regexp, string());
148                 }
149                 //      AllowInput(current_view);
150
151                 if (p.empty()) return;
152
153                 buf = MakeRelPath(p, buf2);
154                 current_path = OnlyPath(p);
155                 once = 1;
156                 
157                 if (contains(p, "#") || contains(p, "~") || contains(p, "$")
158                     || contains(p, "%")) {
159                         WriteAlert(_("Filename can't contain any "
160                                      "of these characters:"),
161                                    // xgettext:no-c-format
162                                    _("'#', '~', '$' or '%'.")); 
163                         error = true;
164                 }
165         } while (error);
166
167         if (inset->form_external) 
168                 fl_set_input(inset->form_external->filename, buf.c_str());
169         
170 }
171
172
173 void InsetExternal::editCB(FL_OBJECT * ob, long)
174 {
175         Holder * holder = static_cast<Holder*>(ob->form->u_vdata);
176         InsetExternal * inset = holder->inset;
177         inset->doApply(holder->view);
178         inset->doEdit(holder->view);
179 }
180
181
182 void InsetExternal::viewCB(FL_OBJECT * ob, long)
183 {
184         Holder * holder = static_cast<Holder*>(ob->form->u_vdata);
185         InsetExternal * inset = holder->inset;
186         inset->doApply(holder->view);
187         inset->doView(holder->view);
188 }
189
190
191 void InsetExternal::updateCB(FL_OBJECT * ob, long)
192 {
193         Holder * holder = static_cast<Holder*>(ob->form->u_vdata);
194         InsetExternal * inset = holder->inset;
195         inset->doApply(holder->view);
196         inset->doUpdate(holder->view);
197 }
198
199
200 void InsetExternal::okCB(FL_OBJECT * ob, long data)
201 {
202         Holder * holder = static_cast<Holder*>(ob->form->u_vdata);
203         InsetExternal * inset = holder->inset;
204         inset->doApply(holder->view);
205         cancelCB(ob,data);
206 }
207
208
209 void InsetExternal::doApply(BufferView * bufview)
210 {
211         bool update = false;
212         if (templatename != getCurrentTemplate()) {
213                 templatename = getCurrentTemplate();
214                 update = true;
215         }
216         if (filename != fl_get_input(form_external->filename)) {
217                 filename = fl_get_input(form_external->filename);
218                 update = true;
219         }
220         if (parameters != fl_get_input(form_external->parameters)) {
221                 parameters = fl_get_input(form_external->parameters);
222                 update = true;
223         }
224
225         if (update) {
226                 // The text might have change,
227                 // so we should update the button look
228                 bufview->updateInset(this, true);
229         }
230 }
231
232
233 void InsetExternal::cancelCB(FL_OBJECT * ob, long)
234 {
235         Holder * holder = static_cast<Holder*>(ob->form->u_vdata);
236
237         InsetExternal * inset = holder->inset;
238         // BufferView * bv = holder->view;
239
240         if (inset->form_external) {
241                 fl_hide_form(inset->form_external->form_external);
242                 fl_free_form(inset->form_external->form_external);
243                 inset->form_external = 0;
244         }
245 }
246
247
248 char const * InsetExternal::EditMessage() const
249 {
250         ExternalTemplate const & et = getTemplate(templatename);
251         return doSubstitution(0, et.guiName).c_str();
252 }
253
254
255 void InsetExternal::Edit(BufferView * bv,
256                          int /*x*/, int /*y*/, unsigned int /*button*/)
257 {
258         static int ow = -1, oh;
259
260         if (bv->buffer()->isReadonly())
261                 WarnReadonly(bv->buffer()->fileName());
262
263         if (!form_external) {
264                 form_external = create_form_form_external();
265                 holder.inset = this;
266                 //              form_external->ok->u_vdata = &holder;
267                 form_external->form_external->u_vdata = &holder;
268                 fl_set_form_atclose(form_external->form_external,
269                                     CancelCloseBoxCB, 0);
270         }
271         holder.view = bv;
272         fl_addto_choice(form_external->templatechoice,
273                         getTemplateString().c_str());
274         fl_set_input(form_external->filename, filename.c_str());
275         fl_set_input(form_external->parameters, parameters.c_str());
276         if (!templatename.empty()) {
277                 fl_set_choice(form_external->templatechoice,
278                               getTemplateNumber(templatename));
279         }
280         // Update the help text
281         templateCB(form_external->templatechoice, 0);
282
283         ExternalTemplate const & et = getTemplate(templatename);
284         if (et.automaticProduction) {
285                 fl_deactivate_object(form_external->update);
286                 fl_set_object_lcol(form_external->update, FL_INACTIVE);
287         } else {
288                 fl_activate_object(form_external->update);
289                 fl_set_object_lcol(form_external->update, FL_BLACK);
290         }
291
292         if (form_external->form_external->visible) {
293                 fl_raise_form(form_external->form_external);
294         } else {
295                 fl_show_form(form_external->form_external,
296                              FL_PLACE_MOUSE | FL_FREE_SIZE,
297                              FL_FULLBORDER, _("Insert external inset"));
298                 if (ow < 0) {
299                         ow = form_external->form_external->w;
300                         oh = form_external->form_external->h;
301                 }
302                 fl_set_form_minsize(form_external->form_external, ow, oh);
303         }
304 }
305
306
307 void InsetExternal::Write(Buffer const *, std::ostream & os) const
308 {
309         os << "External " << templatename << ",\"" << filename 
310            << "\",\"" << parameters << "\"\n";
311 }
312
313
314 void InsetExternal::Read(Buffer const *, LyXLex & lex)
315 {
316         lex.EatLine();
317         string format = lex.GetString();
318         int pos1 = format.find(",");
319         templatename = format.substr(0, pos1);
320         int pos2 = format.find("\",\"", pos1);
321         filename = format.substr(pos1 + 2, pos2 - (pos1 + 2));
322         parameters = format.substr(pos2 + 3, format.length() - (pos2 + 4));
323
324         lyxerr << templatename << " " << filename << " " << parameters << endl;
325 }
326
327
328 int InsetExternal::write(string const & format,
329                          Buffer const * buf, std::ostream & os) const
330 {
331         ExternalTemplate const & et = getTemplate(templatename);
332         ExternalTemplate::Formats::const_iterator cit =
333                 et.formats.find(format);
334         if (cit == et.formats.end()) {
335                 lyxerr << "External template format '" << format
336                        << "' not specified in template " << templatename
337                        << endl;
338                 return 0;
339         }
340         
341         if (et.automaticProduction) {
342                 executeCommand(doSubstitution(buf,
343                                               (*cit).second.updateCommand),
344                                buf);
345         }
346         
347         os << doSubstitution(buf, (*cit).second.product);
348         return 0; // CHECK
349 }
350
351
352 int InsetExternal::Latex(Buffer const * buf,
353                          std::ostream & os, bool, bool) const
354 {
355         return write("LaTeX", buf, os);
356 }
357
358
359 int InsetExternal::Ascii(Buffer const * buf, std::ostream & os) const
360 {
361         return write("Ascii", buf, os);
362 }
363
364
365 int InsetExternal::Linuxdoc(Buffer const * buf, std::ostream & os) const
366 {
367         return write("LinuxDoc", buf, os);
368 }
369
370
371 int InsetExternal::DocBook(Buffer const * buf, std::ostream & os) const
372 {
373         return write("DocBook", buf, os);
374 }
375
376
377 void InsetExternal::Validate(LaTeXFeatures & features) const
378 {
379         ExternalTemplate const & et = getTemplate(templatename);
380         ExternalTemplate::Formats::const_iterator cit =
381                 et.formats.find("LaTeX");
382         if (cit == et.formats.end()) {
383                 return;
384         }
385         
386         if (!(*cit).second.requirement.empty()) {
387                 features.require((*cit).second.requirement);
388         }
389         if (!(*cit).second.preamble.empty()) {
390                 features.externalPreambles += (*cit).second.preamble + "\n";
391         }
392 }
393
394
395 Inset * InsetExternal::Clone() const
396 {
397         InsetExternal * inset = new InsetExternal();
398         inset->templatename = templatename;
399         inset->filename = filename;
400         inset->parameters = parameters;
401         return inset;
402 }
403
404
405 string InsetExternal::getScreenLabel() const
406 {
407         if (templatename.empty()) {
408                 return _("External");
409         } else {
410                 ExternalTemplate const & et = getTemplate(templatename);
411                 return doSubstitution(0, et.guiName);
412         }
413 }
414
415
416 void InsetExternal::doUpdate(BufferView const * bv) const
417 {
418         ExternalTemplate const & et = getTemplate(getCurrentTemplate());
419         ExternalTemplate::Formats::const_iterator cit =
420                 et.formats.find("LaTeX");
421         if (cit == et.formats.end())
422                 return;
423         
424         executeCommand(doSubstitution(bv->buffer(),
425                                       (*cit).second.updateCommand),
426                        bv->buffer());
427 }
428
429
430 void InsetExternal::doView(BufferView const * bv) const
431 {
432         automaticUpdate(bv);
433         ExternalTemplate const & et = getTemplate(getCurrentTemplate());
434         executeCommand(doSubstitution(bv->buffer(), et.viewCommand),
435                        bv->buffer());
436 }
437
438
439 void InsetExternal::doEdit(BufferView const * bv) const
440 {
441         automaticUpdate(bv);
442         ExternalTemplate const & et = getTemplate(getCurrentTemplate());
443         executeCommand(doSubstitution(bv->buffer(), et.editCommand),
444                        bv->buffer());
445 }
446
447
448 void InsetExternal::executeCommand(string const & s,
449                                    Buffer const * buffer) const
450 {
451         string buf = MakeAbsPath(buffer->fileName());
452         string path = OnlyPath(buf);
453         Path p(path);
454         Systemcalls one;
455         if (lyxerr.debugging()) {
456                 lyxerr << "Executing '" << s << "' in '"
457                        << path << "'" << endl;
458         }
459         one.startscript(Systemcalls::Wait, s);
460 }
461
462
463 void InsetExternal::automaticUpdate(BufferView const * bv) const
464 {
465         ExternalTemplate const & et = getTemplate(templatename);
466         if (et.automaticProduction) {
467                 doUpdate(bv);
468         }
469 }
470
471
472 string InsetExternal::doSubstitution(Buffer const * buffer,
473                                      string const & s) const
474 {
475         string result;
476         string basename = ChangeExtension(filename, string());
477         result = subst(s, "$$FName", filename);
478         result = subst(result, "$$Basename", basename);
479         result = subst(result, "$$Parameters", parameters);
480         result = ReplaceEnvironmentPath(result);
481         result = subst(result, "$$Tempname", tempname);
482         result = subst(result, "$$Sysdir", system_lyxdir);
483         
484         // Handle the $$Contents(filename) syntax
485         if (contains(result, "$$Contents(\"")) {
486
487                 int pos = result.find("$$Contents(\"");
488                 int end = result.find("\")", pos);
489                 string file = result.substr(pos + 12, end - (pos + 12));
490                 string contents;
491                 if (buffer) {
492                         // Make sure we are in the directory of the buffer
493                         string buf = MakeAbsPath(buffer->fileName());
494                         string path = OnlyPath(buf);
495                         Path p(path);
496                         contents = GetFileContents(file);
497                 } else {
498                         contents = GetFileContents(file);
499                 }
500                 result = subst(result,
501                                ("$$Contents(\"" + file + "\")").c_str(),
502                                contents);
503         }
504
505         return result;
506 }
507
508
509 string InsetExternal::getCurrentTemplate() const
510 {
511         return getTemplateName(fl_get_choice(form_external->templatechoice));
512 }
513
514
515 ExternalTemplate InsetExternal::getTemplate(string const & name) const
516 {
517         ExternalTemplateManager::Templates::const_iterator i = 
518                 ExternalTemplateManager::get().getTemplates().find(name);
519         // Make sure that the template exists in the map
520         if (i == ExternalTemplateManager::get().getTemplates().end()) {
521                 lyxerr << "Unknown external material template: "
522                        << name << endl;
523                 return ExternalTemplate();
524         }
525         return (*i).second;
526 }
527
528
529 int InsetExternal::getTemplateNumber(string const & name) const
530 {
531         int i = 1;
532         ExternalTemplateManager::Templates::const_iterator i1, i2;
533         i1 = ExternalTemplateManager::get().getTemplates().begin();
534         i2 = ExternalTemplateManager::get().getTemplates().end();
535         for (; i1 != i2; ++i1) {
536                 if ((*i1).second.lyxName == name)
537                         return i;
538                 ++i;
539         }
540         // This should never happen
541         Assert(false);
542         return 0;
543 }
544
545
546 string InsetExternal::getTemplateName(int i) const
547 {
548         ExternalTemplateManager::Templates::const_iterator i1;
549         i1 = ExternalTemplateManager::get().getTemplates().begin();
550         for (int n = 1; n < i; ++n) {
551                 ++i1;
552         }
553         return (*i1).second.lyxName;
554 }
555
556
557 string InsetExternal::getTemplateString() const
558 {
559         string result;
560         bool first = true;
561         ExternalTemplateManager::Templates::const_iterator i1, i2;
562         i1 = ExternalTemplateManager::get().getTemplates().begin();
563         i2 = ExternalTemplateManager::get().getTemplates().end();
564         for (; i1 != i2; ++i1) {
565                 if (!first) {
566                         result += "|";
567                 } else {
568                         first = false;
569                 }
570                 result += (*i1).second.lyxName;
571         }
572         return result;
573 }