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