]> git.lyx.org Git - lyx.git/blob - src/insets/insetinclude.C
Angus insetindex patch + protect patch from Dekel
[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().c_str());
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 string unique_id() {
194         static unsigned int seed=1000;
195
196 #ifdef HAVE_SSTREAM
197         std::ostringstream ost;
198         ost << "file" << ++seed;
199
200         // Needed if we use lyxstring.
201         return ost.str().c_str();
202 #else
203         char ctmp[16];
204         ostrstream ost(ctmp,16);
205         ost << "file" << ++seed << '\0';
206
207         // Needed if we use lyxstring.
208         return ost.str();
209 #endif
210 }
211
212
213 InsetInclude::InsetInclude(string const & fname, Buffer * bf)
214         : InsetCommand("include") 
215 {
216         master = bf;
217         setContents(fname);
218         flag = InsetInclude::INCLUDE;
219         noload = false;
220         include_label = unique_id();
221 }
222
223
224 InsetInclude::~InsetInclude()
225 {
226         if (form && form->include->u_vdata == this) {
227                 // this inset is in the popup so hide the popup 
228                 // and remove the reference to this inset. ARRae
229                 if (form->include) {
230                         if (form->include->visible) {
231                                 fl_hide_form(form->include);
232                         }
233                         fl_free_form(form->include);
234                 }
235                 fl_free(form);
236                 form = 0;
237         }
238 }
239
240
241 Inset * InsetInclude::Clone() const
242
243         InsetInclude * ii = new InsetInclude (getContents(), master); 
244         ii->setNoLoad(isNoLoad());
245         // By default, the newly created inset is of `include' type,
246         // so we do not test this case.
247         if (isInput())
248                 ii->setInput();
249         else if (isVerb()) {
250                 ii->setVerb();
251                 ii->setVisibleSpace(isVerbVisibleSpace());
252         }
253         return ii;
254 }
255
256
257 void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
258 {
259         if(bv->buffer()->isReadonly())
260                 WarnReadonly(bv->buffer()->fileName());
261
262         if (!form) {
263                 form = create_form_include();
264                 fl_set_form_atclose(form->include, IgnoreCloseBoxCB, 0);
265         }
266         form->include->u_vdata = this;
267     
268         fl_set_input(form->input, getContents().c_str());
269         fl_set_button(form->flag1, int(isNoLoad()));
270         fl_set_button(form->flag2, int(isInput()));
271         fl_set_button(form->flag3, int(isInclude()));
272         fl_set_button(form->flag4, int(isVerb()));
273         if (isVerb()) 
274             fl_set_button(form->flag41, int(isVerbVisibleSpace()));
275         else {
276             fl_set_button(form->flag41, 0);
277             fl_deactivate_object(form->flag41);
278             fl_set_object_lcol(form->flag41, FL_INACTIVE);
279         }
280         
281         if (form->include->visible) {
282                 fl_raise_form(form->include);
283         } else {
284                 fl_show_form(form->include, FL_PLACE_MOUSE, FL_FULLBORDER,
285                              _("Include"));
286         }
287 }
288
289
290 void InsetInclude::Write(Buffer const *, ostream & os) const
291 {
292         os << "Include " << getCommand() << "\n";
293 }
294
295
296 void InsetInclude::Read(Buffer const * buf, LyXLex & lex)
297 {
298         InsetCommand::Read(buf, lex);
299     
300         if (getCmdName() == "include")
301                 setInclude();
302         else if (getCmdName() == "input")
303                 setInput();
304         else if (contains(getCmdName(), "verbatim")) {
305                 setVerb();
306                 if (getCmdName() == "verbatiminput*")
307                         setVisibleSpace(true);
308         }
309 }
310
311
312 bool InsetInclude::display() const 
313 {
314         return !isInput();
315 }
316
317
318 string InsetInclude::getScreenLabel() const
319 {
320         string temp;
321         if (isInput())
322                 temp += _("Input");
323         else if (isVerb()) {
324                 temp += _("Verbatim Input");
325                 if (isVerbVisibleSpace()) temp += '*';
326         } else temp += _("Include");
327         temp += ": ";
328         
329         if (getContents().empty()) {
330                 temp+= "???";
331         } else {
332                 temp+= getContents();
333         }
334         return temp;
335 }
336
337
338 string InsetInclude::getFileName() const
339 {
340         return MakeAbsPath(getContents(), 
341                            OnlyPath(getMasterFilename()));
342 }
343
344
345 string InsetInclude::getMasterFilename() const
346 {
347         return master->fileName();
348 }
349
350
351 bool InsetInclude::loadIfNeeded() const
352 {
353         if (isNoLoad() || isVerb()) return false;
354         if (!IsLyXFilename(getFileName())) return false;
355         
356         if (bufferlist.exists(getFileName())) return true;
357         
358         // the readonly flag can/will be wrong, not anymore I think.
359         FileInfo finfo(getFileName());
360         bool ro = !finfo.writable();
361         return ( bufferlist.readFile(getFileName(), ro) != 0 );
362 }
363
364
365 int InsetInclude::Latex(Buffer const *, ostream & os,
366                         bool /*fragile*/, bool /*fs*/) const
367 {
368         // Do nothing if no file name has been specified
369         if (getContents().empty())
370                 return 0;
371     
372         // Use += to force a copy of contents (JMarc)
373         // How does that force anything? (Lgb)
374         string incfile(getContents());
375
376         if (loadIfNeeded()) {
377                 Buffer * tmp = bufferlist.getBuffer(getFileName());
378
379                 if (tmp->params.textclass != master->params.textclass) {
380                         lyxerr << "ERROR: Cannot handle include file `"
381                                << MakeDisplayPath(getFileName())
382                                << "' which has textclass `"
383                                << textclasslist.NameOfClass(tmp->params.textclass)
384                                << "' instead of `"
385                                << textclasslist.NameOfClass(master->params.textclass)
386                                << "'." << endl;
387                         return 0;
388                 }
389                 
390                 // write it to a file (so far the complete file)
391                 string writefile = ChangeExtension(getFileName(), ".tex");
392                 if (!master->tmppath.empty()
393                     && !master->niceFile) {
394                         incfile = subst(incfile, '/','@');
395 #ifdef __EMX__
396                         incfile = subst(incfile, ':', '$');
397 #endif
398                         writefile = AddName(master->tmppath, incfile);
399                 } else
400                         writefile = getFileName();
401                 writefile = ChangeExtension(writefile, ".tex");
402                 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
403                 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
404                 
405                 tmp->markDepClean(master->tmppath);
406                 
407                 tmp->makeLaTeXFile(writefile,
408                                    OnlyPath(getMasterFilename()), 
409                                    master->niceFile, true);
410         } 
411
412         if (isVerb()) {
413                 os << '\\' << getCmdName() << '{' << incfile << '}';
414         } else if (isInput()) {
415                 // \input wants file with extension (default is .tex)
416                 if (!IsLyXFilename(getFileName())) {
417                         os << '\\' << getCmdName() << '{' << incfile << '}';
418                 } else {
419                         os << '\\' << getCmdName() << '{'
420                            << ChangeExtension(incfile, ".tex")
421                            <<  '}';
422                 }
423         } else {
424                 // \include don't want extension and demands that the
425                 // file really have .tex
426                 os << '\\' << getCmdName() << '{'
427                    << ChangeExtension(incfile, string())
428                    << '}';
429         }
430
431         return 0;
432 }
433
434
435 int InsetInclude::Linuxdoc(Buffer const *, ostream & os) const
436 {
437         // Do nothing if no file name has been specified
438         if (getContents().empty())
439                 return 0;
440     
441         string incfile(getContents());
442
443         if (loadIfNeeded()) {
444                 Buffer * tmp = bufferlist.getBuffer(getFileName());
445
446                 // write it to a file (so far the complete file)
447                 string writefile = ChangeExtension(getFileName(), ".sgml");
448                 if (!master->tmppath.empty() && !master->niceFile) {
449                         incfile = subst(incfile, '/','@');
450                         writefile = AddName(master->tmppath, incfile);
451                 } else
452                         writefile = getFileName();
453
454                 if(IsLyXFilename(getFileName()))
455                         writefile = ChangeExtension(writefile, ".sgml");
456
457                 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
458                 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
459                 
460                 tmp->makeLinuxDocFile(writefile, master->niceFile, true);
461         } 
462
463         if (isVerb()) {
464                 os << "<!-- includefile verbatim=\"" << incfile << "\" -->";
465         } else 
466                 os << '&' << include_label << ';';
467         
468         return 0;
469 }
470
471
472 int InsetInclude::DocBook(Buffer const *, ostream & os) const
473 {
474         // Do nothing if no file name has been specified
475         if (getContents().empty())
476                 return 0;
477     
478         string incfile(getContents());
479
480         if (loadIfNeeded()) {
481                 Buffer * tmp = bufferlist.getBuffer(getFileName());
482
483                 // write it to a file (so far the complete file)
484                 string writefile = ChangeExtension(getFileName(), ".sgml");
485                 if (!master->tmppath.empty() && !master->niceFile) {
486                         incfile = subst(incfile, '/','@');
487                         writefile = AddName(master->tmppath, incfile);
488                 } else
489                         writefile = getFileName();
490                 if(IsLyXFilename(getFileName()))
491                         writefile = ChangeExtension(writefile, ".sgml");
492
493                 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
494                 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
495                 
496                 tmp->makeDocBookFile(writefile, master->niceFile, true);
497         } 
498
499         if (isVerb()) {
500                 os << "<!-- includefile verbatim=\"" << incfile << "\" -->";
501         } else 
502                 os << '&' << include_label << ';';
503         
504         return 0;
505 }
506
507
508 void InsetInclude::Validate(LaTeXFeatures & features) const
509 {
510
511         string incfile(getContents());
512         string writefile = ChangeExtension(getFileName(), ".sgml");
513         if (!master->tmppath.empty() && !master->niceFile) {
514                 incfile = subst(incfile, '/','@');
515                 writefile = AddName(master->tmppath, incfile);
516         } else
517                 // writefile = getFileName();
518                 // Use the relative path.
519                 writefile = incfile;
520
521         if(IsLyXFilename(getFileName()))
522                 writefile = ChangeExtension(writefile, ".sgml");
523
524         features.IncludedFiles[include_label] = writefile;
525
526         if (isVerb())
527                 features.verbatim = true;
528
529         // Here we must do the fun stuff...
530         // Load the file in the include if it needs
531         // to be loaded:
532         if (loadIfNeeded()) {
533                 // a file got loaded
534                 Buffer * tmp = bufferlist.getBuffer(getFileName());
535                 tmp->validate(features);
536         }
537 }
538
539
540 vector<string> InsetInclude::getLabelList() const
541 {
542     vector<string> l;
543     string parentname;
544
545     if (loadIfNeeded()) {
546         Buffer * tmp = bufferlist.getBuffer(getFileName());
547         tmp->setParentName(""); 
548         l = tmp->getLabelList();
549         tmp->setParentName(getMasterFilename());
550     }
551
552     return l;
553 }
554
555
556 vector<pair<string,string> > InsetInclude::getKeys() const
557 {
558         vector<pair<string,string> > keys;
559         
560         if (loadIfNeeded()) {
561                 Buffer *tmp = bufferlist.getBuffer(getFileName());
562                 tmp->setParentName(""); 
563                 keys =  tmp->getBibkeyList();
564                 tmp->setParentName(getMasterFilename());
565         }
566         
567         return keys;
568 }