]> git.lyx.org Git - lyx.git/blob - src/insets/insetexternal.C
fix the deadkey bug and a patch from Dekel
[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 << templatename << " " << filename << " " << parameters << endl;
337 }
338
339
340 int InsetExternal::write(string const & format,
341                          Buffer const * buf, std::ostream & os) const
342 {
343         ExternalTemplate const & et = getTemplate(templatename);
344         ExternalTemplate::Formats::const_iterator cit =
345                 et.formats.find(format);
346         if (cit == et.formats.end()) {
347                 lyxerr << "External template format '" << format
348                        << "' not specified in template " << templatename
349                        << endl;
350                 return 0;
351         }
352         
353         if (et.automaticProduction) {
354                 executeCommand(doSubstitution(buf,
355                                               (*cit).second.updateCommand),
356                                buf);
357         }
358         
359         os << doSubstitution(buf, (*cit).second.product);
360         return 0; // CHECK
361 }
362
363
364 int InsetExternal::Latex(Buffer const * buf,
365                          std::ostream & os, bool, bool) const
366 {
367         return write("LaTeX", buf, os);
368 }
369
370
371 int InsetExternal::Ascii(Buffer const * buf, std::ostream & os, int) const
372 {
373         return write("Ascii", buf, os);
374 }
375
376
377 int InsetExternal::Linuxdoc(Buffer const * buf, std::ostream & os) const
378 {
379         return write("LinuxDoc", buf, os);
380 }
381
382
383 int InsetExternal::DocBook(Buffer const * buf, std::ostream & os) const
384 {
385         return write("DocBook", buf, os);
386 }
387
388
389 void InsetExternal::Validate(LaTeXFeatures & features) const
390 {
391         ExternalTemplate const & et = getTemplate(templatename);
392         ExternalTemplate::Formats::const_iterator cit =
393                 et.formats.find("LaTeX");
394         if (cit == et.formats.end()) {
395                 return;
396         }
397         
398         if (!(*cit).second.requirement.empty()) {
399                 features.require((*cit).second.requirement);
400         }
401         if (!(*cit).second.preamble.empty()) {
402                 features.externalPreambles += (*cit).second.preamble + "\n";
403         }
404 }
405
406
407 Inset * InsetExternal::Clone(Buffer const &) const
408 {
409         InsetExternal * inset = new InsetExternal();
410         inset->templatename = templatename;
411         inset->filename = filename;
412         inset->parameters = parameters;
413         return inset;
414 }
415
416
417 string const InsetExternal::getScreenLabel() const
418 {
419         if (templatename.empty()) {
420                 return _("External");
421         } else {
422                 ExternalTemplate const & et = getTemplate(templatename);
423                 if (et.guiName.empty())
424                         return "ext: ???";
425                 else
426                         return doSubstitution(0, et.guiName);
427         }
428 }
429
430
431 void InsetExternal::doUpdate(BufferView const * bv) const
432 {
433         ExternalTemplate const & et = getTemplate(getCurrentTemplate());
434         ExternalTemplate::Formats::const_iterator cit =
435                 et.formats.find("LaTeX");
436         if (cit == et.formats.end())
437                 return;
438         
439         executeCommand(doSubstitution(bv->buffer(),
440                                       (*cit).second.updateCommand),
441                        bv->buffer());
442 }
443
444
445 void InsetExternal::doView(BufferView const * bv) const
446 {
447         automaticUpdate(bv);
448         ExternalTemplate const & et = getTemplate(getCurrentTemplate());
449         executeCommand(doSubstitution(bv->buffer(), et.viewCommand),
450                        bv->buffer());
451 }
452
453
454 void InsetExternal::doEdit(BufferView const * bv) const
455 {
456         automaticUpdate(bv);
457         ExternalTemplate const & et = getTemplate(getCurrentTemplate());
458         executeCommand(doSubstitution(bv->buffer(), et.editCommand),
459                        bv->buffer());
460 }
461
462
463 void InsetExternal::executeCommand(string const & s,
464                                    Buffer const * buffer) const
465 {
466         string buf = MakeAbsPath(buffer->fileName());
467         string path = OnlyPath(buf);
468         Path p(path);
469         Systemcalls one;
470         if (lyxerr.debugging()) {
471                 lyxerr << "Executing '" << s << "' in '"
472                        << path << "'" << endl;
473         }
474         one.startscript(Systemcalls::Wait, s);
475 }
476
477
478 void InsetExternal::automaticUpdate(BufferView const * bv) const
479 {
480         ExternalTemplate const & et = getTemplate(templatename);
481         if (et.automaticProduction) {
482                 doUpdate(bv);
483         }
484 }
485
486
487 string const InsetExternal::doSubstitution(Buffer const * buffer,
488                                      string const & s) const
489 {
490         string result;
491         string const basename = ChangeExtension(filename, string());
492         result = subst(s, "$$FName", filename);
493         result = subst(result, "$$Basename", basename);
494         result = subst(result, "$$Parameters", parameters);
495         result = ReplaceEnvironmentPath(result);
496         result = subst(result, "$$Tempname", tempname);
497         result = subst(result, "$$Sysdir", system_lyxdir);
498         
499         // Handle the $$Contents(filename) syntax
500         if (contains(result, "$$Contents(\"")) {
501
502                 string::size_type const pos = result.find("$$Contents(\"");
503                 string::size_type const end = result.find("\")", pos);
504                 string const file = result.substr(pos + 12, end - (pos + 12));
505                 string contents;
506                 if (buffer) {
507                         // Make sure we are in the directory of the buffer
508                         string const buf = MakeAbsPath(buffer->fileName());
509                         string const path = OnlyPath(buf);
510                         Path p(path);
511                         contents = GetFileContents(file);
512                 } else {
513                         contents = GetFileContents(file);
514                 }
515                 result = subst(result,
516                                ("$$Contents(\"" + file + "\")").c_str(),
517                                contents);
518         }
519
520         return result;
521 }
522
523
524 string const InsetExternal::getCurrentTemplate() const
525 {
526         return getTemplateName(fl_get_choice(form_external->templatechoice));
527 }
528
529
530 ExternalTemplate const InsetExternal::getTemplate(string const & name) const
531 {
532         ExternalTemplateManager::Templates::iterator i = 
533                 ExternalTemplateManager::get().getTemplates().find(name);
534         // Make sure that the template exists in the map
535         if (i == ExternalTemplateManager::get().getTemplates().end()) {
536                 lyxerr << "Unknown external material template: "
537                        << name << endl;
538                 return ExternalTemplate();
539         }
540         return (*i).second;
541 }
542
543
544 int InsetExternal::getTemplateNumber(string const & name) const
545 {
546         int i = 1;
547         ExternalTemplateManager::Templates::const_iterator i1, i2;
548         i1 = ExternalTemplateManager::get().getTemplates().begin();
549         i2 = ExternalTemplateManager::get().getTemplates().end();
550         for (; i1 != i2; ++i1) {
551                 if ((*i1).second.lyxName == name)
552                         return i;
553                 ++i;
554         }
555         // This should never happen
556         ///  This can happen if someone sends you a lyx file that uses
557         ///  external templates that are defined only on his machine
558         //Assert(false);
559         return 0;
560 }
561
562
563 string const InsetExternal::getTemplateName(int i) const
564 {
565         ExternalTemplateManager::Templates::const_iterator i1;
566         i1 = ExternalTemplateManager::get().getTemplates().begin();
567         for (int n = 1; n < i; ++n) {
568                 ++i1;
569         }
570         return (*i1).second.lyxName;
571 }
572
573
574 string const InsetExternal::getTemplateString() const
575 {
576         string result;
577         bool first = true;
578         ExternalTemplateManager::Templates::const_iterator i1, i2;
579         i1 = ExternalTemplateManager::get().getTemplates().begin();
580         i2 = ExternalTemplateManager::get().getTemplates().end();
581         for (; i1 != i2; ++i1) {
582                 if (!first) {
583                         result += "|";
584                 } else {
585                         first = false;
586                 }
587                 result += (*i1).second.lyxName;
588         }
589         return result;
590 }