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