]> git.lyx.org Git - lyx.git/blob - src/insets/insetinclude.C
fix call to Dispatch
[lyx.git] / src / insets / insetinclude.C
1
2 #include <config.h>
3
4 #include <cstdlib>
5
6 #ifdef __GNUG__
7 #pragma implementation
8 #endif
9
10 #include FORMS_H_LOCATION 
11 #include "insetinclude.h"
12 #include "filedlg.h" 
13 #include "buffer.h"
14 #include "bufferlist.h"
15 #include "debug.h"
16 #include "support/filetools.h"
17 #include "lyxrc.h"
18 #include "LyXView.h"
19 #include "LaTeXFeatures.h"
20 #include "lyx_gui_misc.h" // CancelCloseBoxCB
21 #include "gettext.h"
22 #include "include_form.h"
23 #include "support/FileInfo.h"
24 #include "layout.h"
25 #include "lyxfunc.h"
26
27 using std::ostream;
28 using std::endl;
29 using std::vector;
30 using std::pair;
31
32 extern BufferView * current_view;
33
34 extern BufferList bufferlist;
35
36
37 FD_include * create_form_include(void)
38 {
39   FL_OBJECT * obj;
40   FD_include * fdui = (FD_include *) fl_calloc(1, sizeof(FD_include));
41
42   fdui->include = fl_bgn_form(FL_NO_BOX, 340, 210);
43   obj = fl_add_box(FL_UP_BOX, 0, 0, 340, 210, "");
44   obj = fl_add_frame(FL_ENGRAVED_FRAME, 10, 70, 160, 90, "");
45   fdui->browsebt = obj = fl_add_button(FL_NORMAL_BUTTON, 230, 30, 100, 30, idex(_("Browse|#B")));
46     fl_set_button_shortcut(obj, scex(_("Browse|#B")), 1);
47     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
48     fl_set_object_callback(obj, include_cb, 0);
49   fdui->flag1 = obj = fl_add_checkbutton(FL_PUSH_BUTTON, 180, 70, 150, 30, idex(_("Don't typeset|#D")));
50     fl_set_button_shortcut(obj, scex(_("Don't typeset|#D")), 1);
51     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
52   obj = fl_add_button(FL_RETURN_BUTTON, 120, 170, 100, 30, _("OK"));
53     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
54     fl_set_object_callback(obj, include_cb, 1);
55   obj = fl_add_button(FL_NORMAL_BUTTON, 230, 170, 100, 30, idex(_("Cancel|^[")));
56     fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
57     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
58     fl_set_object_callback(obj, include_cb, 2);
59   obj = fl_add_button(FL_NORMAL_BUTTON, 230, 130, 100, 30, idex(_("Load|#L")));
60     fl_set_button_shortcut(obj, scex(_("Load|#L")), 1);
61     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
62     fl_set_object_callback(obj, include_cb, 5);
63   fdui->input = obj = fl_add_input(FL_NORMAL_INPUT, 10, 30, 210, 30, idex(_("File name:|#F")));
64     fl_set_input_shortcut(obj, scex(_("File name:|#F")), 1);
65     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
66     fl_set_object_lalign(obj, FL_ALIGN_TOP_LEFT);
67   fdui->flag41 = obj = fl_add_checkbutton(FL_PUSH_BUTTON, 180, 100, 150, 30, idex(_("Visible space|#s")));
68     fl_set_button_shortcut(obj, scex(_("Visible space|#s")), 1);
69     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
70
71   fdui->include_grp = fl_bgn_group();
72   fdui->flag4 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 130, 160, 30, idex(_("Verbatim|#V")));
73     fl_set_button_shortcut(obj, scex(_("Verbatim|#V")), 1);
74     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
75     fl_set_object_callback(obj, include_cb, 10);
76   fdui->flag2 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 100, 160, 30, idex(_("Use input|#i")));
77     fl_set_button_shortcut(obj, scex(_("Use input|#i")), 1);
78     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
79     fl_set_object_callback(obj, include_cb, 11);
80   fdui->flag3 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 70, 160, 30, idex(_("Use include|#U")));
81     fl_set_button_shortcut(obj, scex(_("Use include|#U")), 1);
82     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
83     fl_set_object_callback(obj, include_cb, 11);
84   fl_end_group();
85
86   fl_end_form();
87
88   //fdui->include->fdui = fdui;
89
90   return fdui;
91 }
92 /*---------------------------------------*/
93
94
95 FD_include * form = 0;
96
97 extern "C"
98 void include_cb(FL_OBJECT *, long arg)
99 {
100     
101         InsetInclude * inset = static_cast<InsetInclude*>(form->include->u_vdata);
102         switch (arg) {
103         case 0:
104         {
105                 // Should browsing too be disabled in RO-mode?
106                 LyXFileDlg fileDlg;
107                 string mpath = OnlyPath(inset->getMasterFilename());
108                 string ext;
109     
110                 if (fl_get_button(form->flag2)) // Use Input Button
111                         ext = "*.tex";
112                 else if (fl_get_button(form->flag4)) // Verbatim all files
113                         ext = "*";
114                 else
115                         ext = "*.lyx";
116                 // launches dialog
117                 fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
118     
119                 // Use by default the master's path
120                 string filename = fileDlg.Select(_("Select Child Document"),
121                                                   mpath, ext, 
122                                                   inset->getContents());
123                 XFlush(fl_get_display());
124  
125                 // check selected filename
126                 if (!filename.empty()) {
127                         string filename2 = MakeRelPath(filename,
128                                                         mpath);
129                         if (prefixIs(filename2, ".."))
130                                 fl_set_input(form->input,
131                                              filename.c_str());
132                         else
133                                 fl_set_input(form->input,
134                                              filename2.c_str());
135                 }
136                 break;
137         }
138
139         case 1:
140                 if(!current_view->buffer()->isReadonly()) {
141                         inset->setContents(fl_get_input(form->input));
142                         // don't typeset
143                         inset->setNoLoad(fl_get_button(form->flag1));
144                         if (fl_get_button(form->flag2))
145                                 inset->setInput();
146                         else if (fl_get_button(form->flag3))
147                                 inset->setInclude();
148                         else if (fl_get_button(form->flag4)) {
149                                 inset->setVerb();
150                                 inset->setVisibleSpace(fl_get_button(form->flag41));
151                         }
152                         
153                         fl_hide_form(form->include);
154                         current_view->updateInset(inset, true);
155                         break;
156                 } // fall through
157                 
158         case 2:
159                 fl_hide_form(form->include);
160                 break;
161         case 5:
162                 if(!current_view->buffer()->isReadonly()) {
163                         inset->setContents(fl_get_input(form->input));
164                         inset->setNoLoad(fl_get_button(form->flag1));
165                         if (fl_get_button(form->flag2))
166                                 inset->setInput();
167                         else if (fl_get_button(form->flag3))
168                                 inset->setInclude();
169                         else if (fl_get_button(form->flag4)) {
170                                 inset->setVerb();
171                                 inset->setVisibleSpace(fl_get_button(form->flag41));
172                         }
173                         
174                         fl_hide_form(form->include);
175                         current_view->updateInset(inset, true);
176                         current_view->owner()->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, inset->getContents());
177                 }
178                 break;
179                 
180         case 10:
181                 fl_activate_object(form->flag41);
182                 fl_set_object_lcol(form->flag41, FL_BLACK); 
183                 break;
184         case 11:
185                 fl_deactivate_object(form->flag41);
186                 fl_set_object_lcol(form->flag41, FL_INACTIVE);
187                 fl_set_button(form->flag41, 0);
188                 break;
189         }
190 }
191
192
193 static inline
194 string unique_id() {
195         static unsigned int seed=1000;
196
197         std::ostringstream ost;
198         ost << "file" << ++seed;
199
200         // Needed if we use lyxstring.
201         return ost.str().c_str();
202 }
203
204
205 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer * bf)
206         : InsetCommand(p), master(bf)
207 {
208         flag = InsetInclude::INCLUDE;
209         noload = false;
210         include_label = unique_id();
211 }
212
213
214 InsetInclude::~InsetInclude()
215 {
216         if (form && form->include->u_vdata == this) {
217                 // this inset is in the popup so hide the popup 
218                 // and remove the reference to this inset. ARRae
219                 if (form->include) {
220                         if (form->include->visible) {
221                                 fl_hide_form(form->include);
222                         }
223                         fl_free_form(form->include);
224                 }
225                 fl_free(form);
226                 form = 0;
227         }
228 }
229
230
231 Inset * InsetInclude::Clone() const
232
233         InsetInclude * ii = new InsetInclude (params(), master); 
234         ii->setNoLoad(isNoLoad());
235         // By default, the newly created inset is of `include' type,
236         // so we do not test this case.
237         if (isInput())
238                 ii->setInput();
239         else if (isVerb()) {
240                 ii->setVerb();
241                 ii->setVisibleSpace(isVerbVisibleSpace());
242         }
243         return ii;
244 }
245
246
247 void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
248 {
249         if(bv->buffer()->isReadonly())
250                 WarnReadonly(bv->buffer()->fileName());
251
252         if (!form) {
253                 form = create_form_include();
254                 fl_set_form_atclose(form->include, IgnoreCloseBoxCB, 0);
255         }
256         form->include->u_vdata = this;
257     
258         fl_set_input(form->input, getContents().c_str());
259         fl_set_button(form->flag1, int(isNoLoad()));
260         fl_set_button(form->flag2, int(isInput()));
261         fl_set_button(form->flag3, int(isInclude()));
262         fl_set_button(form->flag4, int(isVerb()));
263         if (isVerb()) 
264             fl_set_button(form->flag41, int(isVerbVisibleSpace()));
265         else {
266             fl_set_button(form->flag41, 0);
267             fl_deactivate_object(form->flag41);
268             fl_set_object_lcol(form->flag41, FL_INACTIVE);
269         }
270         
271         if (form->include->visible) {
272                 fl_raise_form(form->include);
273         } else {
274                 fl_show_form(form->include, FL_PLACE_MOUSE, FL_FULLBORDER,
275                              _("Include"));
276         }
277 }
278
279
280 void InsetInclude::Write(Buffer const *, ostream & os) const
281 {
282         os << "Include " << getCommand() << "\n";
283 }
284
285
286 void InsetInclude::Read(Buffer const * buf, LyXLex & lex)
287 {
288         InsetCommand::Read(buf, lex);
289     
290         if (getCmdName() == "include")
291                 setInclude();
292         else if (getCmdName() == "input")
293                 setInput();
294         else if (contains(getCmdName(), "verbatim")) {
295                 setVerb();
296                 if (getCmdName() == "verbatiminput*")
297                         setVisibleSpace(true);
298         }
299 }
300
301
302 bool InsetInclude::display() const 
303 {
304         return !isInput();
305 }
306
307
308 string const InsetInclude::getScreenLabel() const
309 {
310         string temp;
311         if (isInput())
312                 temp += _("Input");
313         else if (isVerb()) {
314                 temp += _("Verbatim Input");
315                 if (isVerbVisibleSpace()) temp += '*';
316         } else temp += _("Include");
317         temp += ": ";
318         
319         if (getContents().empty()) {
320                 temp+= "???";
321         } else {
322                 temp+= getContents();
323         }
324         return temp;
325 }
326
327
328 string const InsetInclude::getFileName() const
329 {
330         return MakeAbsPath(getContents(), 
331                            OnlyPath(getMasterFilename()));
332 }
333
334
335 string const InsetInclude::getMasterFilename() const
336 {
337         return master->fileName();
338 }
339
340
341 bool InsetInclude::loadIfNeeded() const
342 {
343         if (isNoLoad() || isVerb()) return false;
344         if (!IsLyXFilename(getFileName())) return false;
345         
346         if (bufferlist.exists(getFileName())) return true;
347         
348         // the readonly flag can/will be wrong, not anymore I think.
349         FileInfo finfo(getFileName());
350         bool ro = !finfo.writable();
351         return ( bufferlist.readFile(getFileName(), ro) != 0 );
352 }
353
354
355 int InsetInclude::Latex(Buffer const *, ostream & os,
356                         bool /*fragile*/, bool /*fs*/) const
357 {
358         // Do nothing if no file name has been specified
359         if (getContents().empty())
360                 return 0;
361     
362         // Use += to force a copy of contents (JMarc)
363         // How does that force anything? (Lgb)
364         string incfile(getContents());
365
366         if (loadIfNeeded()) {
367                 Buffer * tmp = bufferlist.getBuffer(getFileName());
368
369                 if (tmp->params.textclass != master->params.textclass) {
370                         lyxerr << "ERROR: Cannot handle include file `"
371                                << MakeDisplayPath(getFileName())
372                                << "' which has textclass `"
373                                << textclasslist.NameOfClass(tmp->params.textclass)
374                                << "' instead of `"
375                                << textclasslist.NameOfClass(master->params.textclass)
376                                << "'." << endl;
377                         return 0;
378                 }
379                 
380                 // write it to a file (so far the complete file)
381                 string writefile = ChangeExtension(getFileName(), ".tex");
382                 if (!master->tmppath.empty()
383                     && !master->niceFile) {
384                         incfile = subst(incfile, '/','@');
385 #ifdef __EMX__
386                         incfile = subst(incfile, ':', '$');
387 #endif
388                         writefile = AddName(master->tmppath, incfile);
389                 } else
390                         writefile = getFileName();
391                 writefile = ChangeExtension(writefile, ".tex");
392                 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
393                 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
394                 
395                 tmp->markDepClean(master->tmppath);
396                 
397                 tmp->makeLaTeXFile(writefile,
398                                    OnlyPath(getMasterFilename()), 
399                                    master->niceFile, true);
400         } 
401
402         if (isVerb()) {
403                 os << '\\' << getCmdName() << '{' << incfile << '}';
404         } else if (isInput()) {
405                 // \input wants file with extension (default is .tex)
406                 if (!IsLyXFilename(getFileName())) {
407                         os << '\\' << getCmdName() << '{' << incfile << '}';
408                 } else {
409                         os << '\\' << getCmdName() << '{'
410                            << ChangeExtension(incfile, ".tex")
411                            <<  '}';
412                 }
413         } else {
414                 // \include don't want extension and demands that the
415                 // file really have .tex
416                 os << '\\' << getCmdName() << '{'
417                    << ChangeExtension(incfile, string())
418                    << '}';
419         }
420
421         return 0;
422 }
423
424
425 int InsetInclude::Linuxdoc(Buffer const *, ostream & os) const
426 {
427         // Do nothing if no file name has been specified
428         if (getContents().empty())
429                 return 0;
430     
431         string incfile(getContents());
432
433         if (loadIfNeeded()) {
434                 Buffer * tmp = bufferlist.getBuffer(getFileName());
435
436                 // write it to a file (so far the complete file)
437                 string writefile = ChangeExtension(getFileName(), ".sgml");
438                 if (!master->tmppath.empty() && !master->niceFile) {
439                         incfile = subst(incfile, '/','@');
440                         writefile = AddName(master->tmppath, incfile);
441                 } else
442                         writefile = getFileName();
443
444                 if(IsLyXFilename(getFileName()))
445                         writefile = ChangeExtension(writefile, ".sgml");
446
447                 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
448                 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
449                 
450                 tmp->makeLinuxDocFile(writefile, master->niceFile, true);
451         } 
452
453         if (isVerb()) {
454                 os << "<!-- includefile verbatim=\"" << incfile << "\" -->";
455         } else 
456                 os << '&' << include_label << ';';
457         
458         return 0;
459 }
460
461
462 int InsetInclude::DocBook(Buffer const *, ostream & os) const
463 {
464         // Do nothing if no file name has been specified
465         if (getContents().empty())
466                 return 0;
467     
468         string incfile(getContents());
469
470         if (loadIfNeeded()) {
471                 Buffer * tmp = bufferlist.getBuffer(getFileName());
472
473                 // write it to a file (so far the complete file)
474                 string writefile = ChangeExtension(getFileName(), ".sgml");
475                 if (!master->tmppath.empty() && !master->niceFile) {
476                         incfile = subst(incfile, '/','@');
477                         writefile = AddName(master->tmppath, incfile);
478                 } else
479                         writefile = getFileName();
480                 if(IsLyXFilename(getFileName()))
481                         writefile = ChangeExtension(writefile, ".sgml");
482
483                 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
484                 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
485                 
486                 tmp->makeDocBookFile(writefile, master->niceFile, true);
487         } 
488
489         if (isVerb()) {
490                 os << "<!-- includefile verbatim=\"" << incfile << "\" -->";
491         } else 
492                 os << '&' << include_label << ';';
493         
494         return 0;
495 }
496
497
498 void InsetInclude::Validate(LaTeXFeatures & features) const
499 {
500
501         string incfile(getContents());
502         string writefile = ChangeExtension(getFileName(), ".sgml");
503         if (!master->tmppath.empty() && !master->niceFile) {
504                 incfile = subst(incfile, '/','@');
505                 writefile = AddName(master->tmppath, incfile);
506         } else
507                 // writefile = getFileName();
508                 // Use the relative path.
509                 writefile = incfile;
510
511         if(IsLyXFilename(getFileName()))
512                 writefile = ChangeExtension(writefile, ".sgml");
513
514         features.IncludedFiles[include_label] = writefile;
515
516         if (isVerb())
517                 features.verbatim = true;
518
519         // Here we must do the fun stuff...
520         // Load the file in the include if it needs
521         // to be loaded:
522         if (loadIfNeeded()) {
523                 // a file got loaded
524                 Buffer * tmp = bufferlist.getBuffer(getFileName());
525                 tmp->validate(features);
526         }
527 }
528
529
530 vector<string> const InsetInclude::getLabelList() const
531 {
532     vector<string> l;
533     string parentname;
534
535     if (loadIfNeeded()) {
536         Buffer * tmp = bufferlist.getBuffer(getFileName());
537         tmp->setParentName(""); 
538         l = tmp->getLabelList();
539         tmp->setParentName(getMasterFilename());
540     }
541
542     return l;
543 }
544
545
546 vector<pair<string,string> > const InsetInclude::getKeys() const
547 {
548         vector<pair<string,string> > keys;
549         
550         if (loadIfNeeded()) {
551                 Buffer *tmp = bufferlist.getBuffer(getFileName());
552                 tmp->setParentName(""); 
553                 keys =  tmp->getBibkeyList();
554                 tmp->setParentName(getMasterFilename());
555         }
556         
557         return keys;
558 }