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