]> git.lyx.org Git - lyx.git/blob - src/insets/insetinclude.C
Cleanup patches for xforms (Angus) and KDE (John)
[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 const 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 const filename =
121                         fileDlg.Select(_("Select Child Document"),
122                                        mpath, ext, 
123                                        inset->getContents());
124                 XFlush(fl_get_display());
125  
126                 // check selected filename
127                 if (!filename.empty()) {
128                         string const filename2 = MakeRelPath(filename,
129                                                              mpath);
130                         if (prefixIs(filename2, ".."))
131                                 fl_set_input(form->input,
132                                              filename.c_str());
133                         else
134                                 fl_set_input(form->input,
135                                              filename2.c_str());
136                 }
137                 break;
138         }
139
140         case 1:
141                 if(!current_view->buffer()->isReadonly()) {
142                         inset->setContents(fl_get_input(form->input));
143                         // don't typeset
144                         inset->setNoLoad(fl_get_button(form->flag1));
145                         if (fl_get_button(form->flag2))
146                                 inset->setInput();
147                         else if (fl_get_button(form->flag3))
148                                 inset->setInclude();
149                         else if (fl_get_button(form->flag4)) {
150                                 inset->setVerb();
151                                 inset->setVisibleSpace(fl_get_button(form->flag41));
152                         }
153                         
154                         fl_hide_form(form->include);
155                         current_view->updateInset(inset, true);
156                         break;
157                 } // fall through
158                 
159         case 2:
160                 fl_hide_form(form->include);
161                 break;
162         case 5:
163                 if(!current_view->buffer()->isReadonly()) {
164                         inset->setContents(fl_get_input(form->input));
165                         inset->setNoLoad(fl_get_button(form->flag1));
166                         if (fl_get_button(form->flag2))
167                                 inset->setInput();
168                         else if (fl_get_button(form->flag3))
169                                 inset->setInclude();
170                         else if (fl_get_button(form->flag4)) {
171                                 inset->setVerb();
172                                 inset->setVisibleSpace(fl_get_button(form->flag41));
173                         }
174                         
175                         fl_hide_form(form->include);
176                         current_view->updateInset(inset, true);
177                         current_view->owner()->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, inset->getContents());
178                 }
179                 break;
180                 
181         case 10:
182                 fl_activate_object(form->flag41);
183                 fl_set_object_lcol(form->flag41, FL_BLACK); 
184                 break;
185         case 11:
186                 fl_deactivate_object(form->flag41);
187                 fl_set_object_lcol(form->flag41, FL_INACTIVE);
188                 fl_set_button(form->flag41, 0);
189                 break;
190         }
191 }
192
193
194 static inline
195 string unique_id() {
196         static unsigned int seed = 1000;
197
198         std::ostringstream ost;
199         ost << "file" << ++seed;
200
201         // Needed if we use lyxstring.
202         return ost.str().c_str();
203 }
204
205
206 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & bf)
207         : InsetCommand(p), master(&bf)
208 {
209         flag = InsetInclude::INCLUDE;
210         noload = false;
211         include_label = unique_id();
212 }
213
214
215 InsetInclude::~InsetInclude()
216 {
217         if (form && form->include->u_vdata == this) {
218                 // this inset is in the popup so hide the popup 
219                 // and remove the reference to this inset. ARRae
220                 if (form->include) {
221                         if (form->include->visible) {
222                                 fl_hide_form(form->include);
223                         }
224                         fl_free_form(form->include);
225                 }
226                 fl_free(form);
227                 form = 0;
228         }
229 }
230
231
232 Inset * InsetInclude::Clone(Buffer const & buffer) const
233
234         InsetInclude * ii = new InsetInclude (params(), buffer); 
235         ii->setNoLoad(isNoLoad());
236         // By default, the newly created inset is of `include' type,
237         // so we do not test this case.
238         if (isInput())
239                 ii->setInput();
240         else if (isVerb()) {
241                 ii->setVerb();
242                 ii->setVisibleSpace(isVerbVisibleSpace());
243         }
244         return ii;
245 }
246
247
248 void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
249 {
250         if(bv->buffer()->isReadonly())
251                 WarnReadonly(bv->buffer()->fileName());
252
253         if (!form) {
254                 form = create_form_include();
255                 fl_set_form_atclose(form->include, IgnoreCloseBoxCB, 0);
256         }
257         form->include->u_vdata = this;
258     
259         fl_set_input(form->input, getContents().c_str());
260         fl_set_button(form->flag1, int(isNoLoad()));
261         fl_set_button(form->flag2, int(isInput()));
262         fl_set_button(form->flag3, int(isInclude()));
263         fl_set_button(form->flag4, int(isVerb()));
264         if (isVerb()) 
265             fl_set_button(form->flag41, int(isVerbVisibleSpace()));
266         else {
267             fl_set_button(form->flag41, 0);
268             fl_deactivate_object(form->flag41);
269             fl_set_object_lcol(form->flag41, FL_INACTIVE);
270         }
271         
272         if (form->include->visible) {
273                 fl_raise_form(form->include);
274         } else {
275                 fl_show_form(form->include, FL_PLACE_MOUSE, FL_FULLBORDER,
276                              _("Include"));
277         }
278 }
279
280
281 void InsetInclude::Write(Buffer const *, ostream & os) const
282 {
283         os << "Include " << getCommand() << "\n";
284 }
285
286
287 void InsetInclude::Read(Buffer const * buf, LyXLex & lex)
288 {
289         InsetCommand::Read(buf, lex);
290     
291         if (getCmdName() == "include")
292                 setInclude();
293         else if (getCmdName() == "input")
294                 setInput();
295         else if (contains(getCmdName(), "verbatim")) {
296                 setVerb();
297                 if (getCmdName() == "verbatiminput*")
298                         setVisibleSpace(true);
299         }
300 }
301
302
303 bool InsetInclude::display() const 
304 {
305         return !isInput();
306 }
307
308
309 string const InsetInclude::getScreenLabel() const
310 {
311         string temp;
312         if (isInput())
313                 temp += _("Input");
314         else if (isVerb()) {
315                 temp += _("Verbatim Input");
316                 if (isVerbVisibleSpace()) temp += '*';
317         } else temp += _("Include");
318         temp += ": ";
319         
320         if (getContents().empty()) {
321                 temp+= "???";
322         } else {
323                 temp+= getContents();
324         }
325         return temp;
326 }
327
328
329 string const InsetInclude::getFileName() const
330 {
331         return MakeAbsPath(getContents(), 
332                            OnlyPath(getMasterFilename()));
333 }
334
335
336 string const InsetInclude::getMasterFilename() const
337 {
338         return master->fileName();
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 const ro = !finfo.writable();
352         return bufferlist.readFile(getFileName(), ro) != 0;
353 }
354
355
356 int InsetInclude::Latex(Buffer const * buffer, ostream & os,
357                         bool /*fragile*/, bool /*fs*/) const
358 {
359         string incfile(getContents());
360         
361         // Do nothing if no file name has been specified
362         if (incfile.empty())
363                 return 0;
364     
365         if (loadIfNeeded()) {
366                 Buffer * tmp = bufferlist.getBuffer(getFileName());
367
368                 if (tmp->params.textclass != buffer->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(buffer->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 (!buffer->tmppath.empty()
382                     && !buffer->niceFile) {
383                         incfile = subst(incfile, '/','@');
384 #ifdef __EMX__
385                         incfile = subst(incfile, ':', '$');
386 #endif
387                         writefile = AddName(buffer->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(buffer->tmppath);
395                 
396                 tmp->makeLaTeXFile(writefile,
397                                    OnlyPath(getMasterFilename()), 
398                                    buffer->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 * buffer, ostream & os) const
425 {
426         string incfile(getContents());
427         
428         // Do nothing if no file name has been specified
429         if (incfile.empty())
430                 return 0;
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 (!buffer->tmppath.empty() && !buffer->niceFile) {
438                         incfile = subst(incfile, '/','@');
439                         writefile = AddName(buffer->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, buffer->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 * buffer, ostream & os) const
462 {
463         string incfile(getContents());
464
465         // Do nothing if no file name has been specified
466         if (incfile.empty())
467                 return 0;
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 (!buffer->tmppath.empty() && !buffer->niceFile) {
475                         incfile = subst(incfile, '/','@');
476                         writefile = AddName(buffer->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, buffer->niceFile, true);
486         } 
487
488         if (isVerb()) {
489                 os << "<inlinegraphic fileref=\"" << incfile << "\" format=\"linespecific\">";
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> const InsetInclude::getLabelList() const
530 {
531         vector<string> l;
532
533         if (loadIfNeeded()) {
534                 Buffer * tmp = bufferlist.getBuffer(getFileName());
535                 tmp->setParentName(""); 
536                 l = tmp->getLabelList();
537                 tmp->setParentName(getMasterFilename());
538         }
539
540         return l;
541 }
542
543
544 vector<pair<string,string> > const InsetInclude::getKeys() const
545 {
546         vector<pair<string,string> > keys;
547         
548         if (loadIfNeeded()) {
549                 Buffer * tmp = bufferlist.getBuffer(getFileName());
550                 tmp->setParentName(""); 
551                 keys = tmp->getBibkeyList();
552                 tmp->setParentName(getMasterFilename());
553         }
554         
555         return keys;
556 }