10 #include FORMS_H_LOCATION
11 #include "insetinclude.h"
14 #include "bufferlist.h"
16 #include "support/filetools.h"
19 #include "LaTeXFeatures.h"
20 #include "lyx_gui_misc.h" // CancelCloseBoxCB
22 #include "include_form.h"
23 #include "support/FileInfo.h"
32 extern BufferView * current_view;
34 extern BufferList bufferlist;
37 FD_include * create_form_include(void)
40 FD_include * fdui = (FD_include *) fl_calloc(1, sizeof(FD_include));
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);
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);
88 //fdui->include->fdui = fdui;
92 /*---------------------------------------*/
95 FD_include * form = 0;
98 void include_cb(FL_OBJECT *, long arg)
101 InsetInclude * inset = static_cast<InsetInclude*>(form->include->u_vdata);
105 // Should browsing too be disabled in RO-mode?
107 string mpath = OnlyPath(inset->getMasterFilename());
110 if (fl_get_button(form->flag2)) // Use Input Button
112 else if (fl_get_button(form->flag4)) // Verbatim all files
117 fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
119 // Use by default the master's path
120 string filename = fileDlg.Select(_("Select Child Document"),
122 inset->getContents());
123 XFlush(fl_get_display());
125 // check selected filename
126 if (!filename.empty()) {
127 string filename2 = MakeRelPath(filename,
129 if (prefixIs(filename2, ".."))
130 fl_set_input(form->input,
133 fl_set_input(form->input,
140 if(!current_view->buffer()->isReadonly()) {
141 inset->setContents(fl_get_input(form->input));
143 inset->setNoLoad(fl_get_button(form->flag1));
144 if (fl_get_button(form->flag2))
146 else if (fl_get_button(form->flag3))
148 else if (fl_get_button(form->flag4)) {
150 inset->setVisibleSpace(fl_get_button(form->flag41));
153 fl_hide_form(form->include);
154 current_view->updateInset(inset, true);
159 fl_hide_form(form->include);
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))
167 else if (fl_get_button(form->flag3))
169 else if (fl_get_button(form->flag4)) {
171 inset->setVisibleSpace(fl_get_button(form->flag41));
174 fl_hide_form(form->include);
175 current_view->updateInset(inset, true);
176 current_view->owner()->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, inset->getContents().c_str());
181 fl_activate_object(form->flag41);
182 fl_set_object_lcol(form->flag41, FL_BLACK);
185 fl_deactivate_object(form->flag41);
186 fl_set_object_lcol(form->flag41, FL_INACTIVE);
187 fl_set_button(form->flag41, 0);
193 static string unique_id() {
194 static unsigned int seed=1000;
197 std::ostringstream ost;
198 ost << "file" << ++seed;
200 // Needed if we use lyxstring.
201 return ost.str().c_str();
204 ostrstream ost(ctmp,16);
205 ost << "file" << ++seed << '\0';
207 // Needed if we use lyxstring.
213 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer * bf)
214 : InsetCommand(p), master(bf)
216 flag = InsetInclude::INCLUDE;
218 include_label = unique_id();
222 InsetInclude::~InsetInclude()
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
228 if (form->include->visible) {
229 fl_hide_form(form->include);
231 fl_free_form(form->include);
239 Inset * InsetInclude::Clone() const
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.
249 ii->setVisibleSpace(isVerbVisibleSpace());
255 void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
257 if(bv->buffer()->isReadonly())
258 WarnReadonly(bv->buffer()->fileName());
261 form = create_form_include();
262 fl_set_form_atclose(form->include, IgnoreCloseBoxCB, 0);
264 form->include->u_vdata = this;
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()));
272 fl_set_button(form->flag41, int(isVerbVisibleSpace()));
274 fl_set_button(form->flag41, 0);
275 fl_deactivate_object(form->flag41);
276 fl_set_object_lcol(form->flag41, FL_INACTIVE);
279 if (form->include->visible) {
280 fl_raise_form(form->include);
282 fl_show_form(form->include, FL_PLACE_MOUSE, FL_FULLBORDER,
288 void InsetInclude::Write(Buffer const *, ostream & os) const
290 os << "Include " << getCommand() << "\n";
294 void InsetInclude::Read(Buffer const * buf, LyXLex & lex)
296 InsetCommand::Read(buf, lex);
298 if (getCmdName() == "include")
300 else if (getCmdName() == "input")
302 else if (contains(getCmdName(), "verbatim")) {
304 if (getCmdName() == "verbatiminput*")
305 setVisibleSpace(true);
310 bool InsetInclude::display() const
316 string InsetInclude::getScreenLabel() const
322 temp += _("Verbatim Input");
323 if (isVerbVisibleSpace()) temp += '*';
324 } else temp += _("Include");
327 if (getContents().empty()) {
330 temp+= getContents();
336 string InsetInclude::getFileName() const
338 return MakeAbsPath(getContents(),
339 OnlyPath(getMasterFilename()));
343 string InsetInclude::getMasterFilename() const
345 return master->fileName();
349 bool InsetInclude::loadIfNeeded() const
351 if (isNoLoad() || isVerb()) return false;
352 if (!IsLyXFilename(getFileName())) return false;
354 if (bufferlist.exists(getFileName())) return true;
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 );
363 int InsetInclude::Latex(Buffer const *, ostream & os,
364 bool /*fragile*/, bool /*fs*/) const
366 // Do nothing if no file name has been specified
367 if (getContents().empty())
370 // Use += to force a copy of contents (JMarc)
371 // How does that force anything? (Lgb)
372 string incfile(getContents());
374 if (loadIfNeeded()) {
375 Buffer * tmp = bufferlist.getBuffer(getFileName());
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)
383 << textclasslist.NameOfClass(master->params.textclass)
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, '/','@');
394 incfile = subst(incfile, ':', '$');
396 writefile = AddName(master->tmppath, incfile);
398 writefile = getFileName();
399 writefile = ChangeExtension(writefile, ".tex");
400 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
401 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
403 tmp->markDepClean(master->tmppath);
405 tmp->makeLaTeXFile(writefile,
406 OnlyPath(getMasterFilename()),
407 master->niceFile, true);
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 << '}';
417 os << '\\' << getCmdName() << '{'
418 << ChangeExtension(incfile, ".tex")
422 // \include don't want extension and demands that the
423 // file really have .tex
424 os << '\\' << getCmdName() << '{'
425 << ChangeExtension(incfile, string())
433 int InsetInclude::Linuxdoc(Buffer const *, ostream & os) const
435 // Do nothing if no file name has been specified
436 if (getContents().empty())
439 string incfile(getContents());
441 if (loadIfNeeded()) {
442 Buffer * tmp = bufferlist.getBuffer(getFileName());
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);
450 writefile = getFileName();
452 if(IsLyXFilename(getFileName()))
453 writefile = ChangeExtension(writefile, ".sgml");
455 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
456 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
458 tmp->makeLinuxDocFile(writefile, master->niceFile, true);
462 os << "<!-- includefile verbatim=\"" << incfile << "\" -->";
464 os << '&' << include_label << ';';
470 int InsetInclude::DocBook(Buffer const *, ostream & os) const
472 // Do nothing if no file name has been specified
473 if (getContents().empty())
476 string incfile(getContents());
478 if (loadIfNeeded()) {
479 Buffer * tmp = bufferlist.getBuffer(getFileName());
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);
487 writefile = getFileName();
488 if(IsLyXFilename(getFileName()))
489 writefile = ChangeExtension(writefile, ".sgml");
491 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
492 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
494 tmp->makeDocBookFile(writefile, master->niceFile, true);
498 os << "<!-- includefile verbatim=\"" << incfile << "\" -->";
500 os << '&' << include_label << ';';
506 void InsetInclude::Validate(LaTeXFeatures & features) const
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);
515 // writefile = getFileName();
516 // Use the relative path.
519 if(IsLyXFilename(getFileName()))
520 writefile = ChangeExtension(writefile, ".sgml");
522 features.IncludedFiles[include_label] = writefile;
525 features.verbatim = true;
527 // Here we must do the fun stuff...
528 // Load the file in the include if it needs
530 if (loadIfNeeded()) {
532 Buffer * tmp = bufferlist.getBuffer(getFileName());
533 tmp->validate(features);
538 vector<string> InsetInclude::getLabelList() const
543 if (loadIfNeeded()) {
544 Buffer * tmp = bufferlist.getBuffer(getFileName());
545 tmp->setParentName("");
546 l = tmp->getLabelList();
547 tmp->setParentName(getMasterFilename());
554 vector<pair<string,string> > InsetInclude::getKeys() const
556 vector<pair<string,string> > keys;
558 if (loadIfNeeded()) {
559 Buffer *tmp = bufferlist.getBuffer(getFileName());
560 tmp->setParentName("");
561 keys = tmp->getBibkeyList();
562 tmp->setParentName(getMasterFilename());