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