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