]> git.lyx.org Git - lyx.git/blob - src/insets/insetinclude.C
9bc670cf7b343667920d05e403d89337df2ae623
[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" void include_cb(FL_OBJECT *, long arg)
97 {
98     
99         InsetInclude * inset = static_cast<InsetInclude*>(form->include->u_vdata);
100         switch (arg) {
101         case 0:
102         {
103                 // Should browsing too be disabled in RO-mode?
104                 LyXFileDlg fileDlg;
105                 string mpath = OnlyPath(inset->getMasterFilename());
106                 string ext;
107     
108                 if (fl_get_button(form->flag2)) // Use Input Button
109                         ext = "*.tex";
110                 else if (fl_get_button(form->flag4)) // Verbatim all files
111                         ext = "*";
112                 else
113                         ext = "*.lyx";
114                 // launches dialog
115                 fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
116     
117                 // Use by default the master's path
118                 string filename = fileDlg.Select(_("Select Child Document"),
119                                                   mpath, ext, 
120                                                   inset->getContents());
121                 XFlush(fl_get_display());
122  
123                 // check selected filename
124                 if (!filename.empty()) {
125                         string filename2 = MakeRelPath(filename,
126                                                         mpath);
127                         if (prefixIs(filename2, ".."))
128                                 fl_set_input(form->input,
129                                              filename.c_str());
130                         else
131                                 fl_set_input(form->input,
132                                              filename2.c_str());
133                 }
134                 break;
135         }
136
137         case 1:
138                 if(!current_view->buffer()->isReadonly()) {
139                         inset->setContents(fl_get_input(form->input));
140                         // don't typeset
141                         inset->setNoLoad(fl_get_button(form->flag1));
142                         if (fl_get_button(form->flag2))
143                                 inset->setInput();
144                         else if (fl_get_button(form->flag3))
145                                 inset->setInclude();
146                         else if (fl_get_button(form->flag4)) {
147                                 inset->setVerb();
148                                 inset->setVisibleSpace(fl_get_button(form->flag41));
149                         }
150                         
151                         fl_hide_form(form->include);
152                         current_view->updateInset(inset, true);
153                         break;
154                 } // fall through
155                 
156         case 2:
157                 fl_hide_form(form->include);
158                 break;
159         case 5:
160                 if(!current_view->buffer()->isReadonly()) {
161                         inset->setContents(fl_get_input(form->input));
162                         inset->setNoLoad(fl_get_button(form->flag1));
163                         if (fl_get_button(form->flag2))
164                                 inset->setInput();
165                         else if (fl_get_button(form->flag3))
166                                 inset->setInclude();
167                         else if (fl_get_button(form->flag4)) {
168                                 inset->setVerb();
169                                 inset->setVisibleSpace(fl_get_button(form->flag41));
170                         }
171                         
172                         fl_hide_form(form->include);
173                         current_view->updateInset(inset, true);
174                         current_view->owner()->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, inset->getContents().c_str());
175                 }
176                 break;
177                 
178         case 10:
179                 fl_activate_object(form->flag41);
180                 fl_set_object_lcol(form->flag41, FL_BLACK); 
181                 break;
182         case 11:
183                 fl_deactivate_object(form->flag41);
184                 fl_set_object_lcol(form->flag41, FL_INACTIVE);
185                 fl_set_button(form->flag41, 0);
186                 break;
187         }
188 }
189
190
191 InsetInclude::InsetInclude(string const & fname, Buffer * bf)
192         : InsetCommand("include") 
193 {
194         master = bf;
195         setContents(fname);
196         flag = InsetInclude::INCLUDE;
197         noload = false;
198 }
199
200
201 InsetInclude::~InsetInclude()
202 {
203         if (form && form->include->u_vdata == this) {
204                 // this inset is in the popup so hide the popup 
205                 // and remove the reference to this inset. ARRae
206                 if (form->include) {
207                         if (form->include->visible) {
208                                 fl_hide_form(form->include);
209                         }
210                         fl_free_form(form->include);
211                 }
212                 fl_free(form);
213                 form = 0;
214         }
215 }
216
217
218 Inset * InsetInclude::Clone() const
219
220         InsetInclude * ii = new InsetInclude (getContents(), master); 
221         ii->setNoLoad(isNoLoad());
222         // By default, the newly created inset is of `include' type,
223         // so we do not test this case.
224         if (isInput())
225                 ii->setInput();
226         else if (isVerb()) {
227                 ii->setVerb();
228                 ii->setVisibleSpace(isVerbVisibleSpace());
229         }
230         return ii;
231 }
232
233
234 void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
235 {
236         if(bv->buffer()->isReadonly())
237                 WarnReadonly(bv->buffer()->fileName());
238
239         if (!form) {
240                 form = create_form_include();
241                 fl_set_form_atclose(form->include, IgnoreCloseBoxCB, 0);
242         }
243         form->include->u_vdata = this;
244     
245         fl_set_input(form->input, getContents().c_str());
246         fl_set_button(form->flag1, int(isNoLoad()));
247         fl_set_button(form->flag2, int(isInput()));
248         fl_set_button(form->flag3, int(isInclude()));
249         fl_set_button(form->flag4, int(isVerb()));
250         if (isVerb()) 
251             fl_set_button(form->flag41, int(isVerbVisibleSpace()));
252         else {
253             fl_set_button(form->flag41, 0);
254             fl_deactivate_object(form->flag41);
255             fl_set_object_lcol(form->flag41, FL_INACTIVE);
256         }
257         
258         if (form->include->visible) {
259                 fl_raise_form(form->include);
260         } else {
261                 fl_show_form(form->include, FL_PLACE_MOUSE, FL_FULLBORDER,
262                              _("Include"));
263         }
264 }
265
266
267 void InsetInclude::Write(Buffer const *, ostream & os) const
268 {
269         os << "Include " << getCommand() << "\n";
270 }
271
272
273 void InsetInclude::Read(Buffer const * buf, LyXLex & lex)
274 {
275         InsetCommand::Read(buf, lex);
276     
277         if (getCmdName() == "include")
278                 setInclude();
279         else if (getCmdName() == "input")
280                 setInput();
281         else if (contains(getCmdName(), "verbatim")) {
282                 setVerb();
283                 if (getCmdName() == "verbatiminput*")
284                         setVisibleSpace(true);
285         }
286 }
287
288
289 bool InsetInclude::display() const 
290 {
291         return !isInput();
292 }
293
294
295 string InsetInclude::getScreenLabel() const
296 {
297         string temp;
298         if (isInput())
299                 temp += _("Input");
300         else if (isVerb()) {
301                 temp += _("Verbatim Input");
302                 if (isVerbVisibleSpace()) temp += '*';
303         } else temp += _("Include");
304         temp += ": ";
305         
306         if (getContents().empty()) {
307                 temp+= "???";
308         } else {
309                 temp+= getContents();
310         }
311         return temp;
312 }
313
314
315 void InsetInclude::setContents(string const & c)
316 {
317         InsetCommand::setContents(c);
318         filename = MakeAbsPath(getContents(), 
319                                OnlyPath(getMasterFilename())); 
320 }
321
322
323 bool InsetInclude::loadIfNeeded() const
324 {
325         if (isNoLoad() || isVerb()) return false;
326         if (!IsLyXFilename(getFileName())) return false;
327         
328         if (bufferlist.exists(getFileName())) return true;
329         
330         // the readonly flag can/will be wrong, not anymore I think.
331         FileInfo finfo(getFileName());
332         bool ro = !finfo.writable();
333         return ( bufferlist.readFile(getFileName(), ro) != 0 );
334 }
335
336
337 int InsetInclude::Latex(Buffer const *, ostream & os,
338                         bool /*fragile*/, bool /*fs*/) const
339 {
340         // Do nothing if no file name has been specified
341         if (getContents().empty())
342                 return 0;
343     
344         // Use += to force a copy of contents (JMarc)
345         // How does that force anything? (Lgb)
346         string incfile(getContents());
347
348         if (loadIfNeeded()) {
349                 Buffer * tmp = bufferlist.getBuffer(getFileName());
350
351                 if (tmp->params.textclass != master->params.textclass) {
352                         lyxerr << "ERROR: Cannot handle include file `"
353                                << MakeDisplayPath(getFileName())
354                                << "' which has textclass `"
355                                << textclasslist.NameOfClass(tmp->params.textclass)
356                                << "' instead of `"
357                                << textclasslist.NameOfClass(master->params.textclass)
358                                << "'." << endl;
359                         return 0;
360                 }
361                 
362                 // write it to a file (so far the complete file)
363                 string writefile = ChangeExtension(getFileName(), ".tex");
364                 if (!master->tmppath.empty()
365                     && !master->niceFile) {
366                         incfile = subst(incfile, '/','@');
367 #ifdef __EMX__
368                         incfile = subst(incfile, ':', '$');
369 #endif
370                         writefile = AddName(master->tmppath, incfile);
371                 } else
372                         writefile = getFileName();
373                 writefile = ChangeExtension(writefile, ".tex");
374                 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
375                 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
376                 
377                 tmp->markDepClean(master->tmppath);
378                 
379                 tmp->makeLaTeXFile(writefile,
380                                    OnlyPath(getMasterFilename()), 
381                                    master->niceFile, true);
382         } 
383
384         if (isVerb()) {
385                 os << '\\' << getCmdName() << '{' << incfile << '}';
386         } else if (isInput()) {
387                 // \input wants file with extension (default is .tex)
388                 if (!IsLyXFilename(getFileName())) {
389                         os << '\\' << getCmdName() << '{' << incfile << '}';
390                 } else {
391                         os << '\\' << getCmdName() << '{'
392                            << ChangeExtension(incfile, ".tex")
393                            <<  '}';
394                 }
395         } else {
396                 // \include don't want extension and demands that the
397                 // file really have .tex
398                 os << '\\' << getCmdName() << '{'
399                    << ChangeExtension(incfile, string())
400                    << '}';
401         }
402
403         return 0;
404 }
405
406
407 int InsetInclude::Linuxdoc(Buffer const *, ostream & os) const
408 {
409         // Do nothing if no file name has been specified
410         if (getContents().empty())
411                 return 0;
412     
413         string incfile(getContents());
414
415         if (loadIfNeeded()) {
416                 Buffer * tmp = bufferlist.getBuffer(getFileName());
417
418                 // write it to a file (so far the complete file)
419                 string writefile = ChangeExtension(getFileName(), ".sgml");
420                 if (!master->tmppath.empty() && !master->niceFile) {
421                         incfile = subst(incfile, '/','@');
422                         writefile = AddName(master->tmppath, incfile);
423                 } else
424                         writefile = getFileName();
425
426                 if(IsLyXFilename(getFileName()))
427                         writefile = ChangeExtension(writefile, ".sgml");
428
429                 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
430                 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
431                 
432                 tmp->makeLinuxDocFile(writefile, master->niceFile, true);
433         } 
434
435         if (isVerb()) {
436                 os << "<!-- includefile verbatim=\"" << incfile << "\" -->";
437         } else 
438                 os << '&' << include_label << ';';
439         
440         return 0;
441 }
442
443
444 int InsetInclude::DocBook(Buffer const *, ostream & os) const
445 {
446         // Do nothing if no file name has been specified
447         if (getContents().empty())
448                 return 0;
449     
450         string incfile(getContents());
451
452         if (loadIfNeeded()) {
453                 Buffer * tmp = bufferlist.getBuffer(getFileName());
454
455                 // write it to a file (so far the complete file)
456                 string writefile = ChangeExtension(getFileName(), ".sgml");
457                 if (!master->tmppath.empty() && !master->niceFile) {
458                         incfile = subst(incfile, '/','@');
459                         writefile = AddName(master->tmppath, incfile);
460                 } else
461                         writefile = getFileName();
462                 if(IsLyXFilename(getFileName()))
463                         writefile = ChangeExtension(writefile, ".sgml");
464
465                 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
466                 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
467                 
468                 tmp->makeDocBookFile(writefile, master->niceFile, true);
469         } 
470
471         if (isVerb()) {
472                 os << "<!-- includefile verbatim=\"" << incfile << "\" -->";
473         } else 
474                 os << '&' << include_label << ';';
475         
476         return 0;
477 }
478
479
480 static unsigned int unique_id() {
481         static unsigned int seed=1000;
482
483         return ++seed;
484 }
485
486
487 void InsetInclude::Validate(LaTeXFeatures & features) const
488 {
489
490 #ifdef HAVE_SSTREAM
491         std::ostringstream ost;
492         ost << "file" << unique_id();
493         include_label = ost.str();
494 #else
495         char ctmp[16];
496         ostrstream ost(ctmp,16);
497         ost << "file" << unique_id() << '\0';
498         include_label = ost.str();
499 #endif
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> 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> > 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 }