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