9 #include FORMS_H_LOCATION
10 #include "insetinclude.h"
13 #include "bufferlist.h"
15 #include "support/filetools.h"
18 #include "LaTeXFeatures.h"
19 #include "lyx_gui_misc.h" // CancelCloseBoxCB
21 #include "include_form.h"
22 #include "support/FileInfo.h"
31 extern BufferView * current_view;
33 extern BufferList bufferlist;
36 FD_include * create_form_include(void)
39 FD_include * fdui = (FD_include *) fl_calloc(1, sizeof(FD_include));
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);
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);
87 //fdui->include->fdui = fdui;
91 /*---------------------------------------*/
94 FD_include * form = 0;
96 extern "C" void include_cb(FL_OBJECT *, long arg)
99 InsetInclude * inset = static_cast<InsetInclude*>(form->include->u_vdata);
103 // Should browsing too be disabled in RO-mode?
105 string mpath = OnlyPath(inset->getMasterFilename());
108 if (fl_get_button(form->flag2)) // Use Input Button
110 else if (fl_get_button(form->flag4)) // Verbatim all files
115 fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
117 // Use by default the master's path
118 string filename = fileDlg.Select(_("Select Child Document"),
120 inset->getContents());
121 XFlush(fl_get_display());
123 // check selected filename
124 if (!filename.empty()) {
125 string filename2 = MakeRelPath(filename,
127 if (prefixIs(filename2, ".."))
128 fl_set_input(form->input,
131 fl_set_input(form->input,
138 if(!current_view->buffer()->isReadonly()) {
139 inset->setContents(fl_get_input(form->input));
141 inset->setNoLoad(fl_get_button(form->flag1));
142 if (fl_get_button(form->flag2))
144 else if (fl_get_button(form->flag3))
146 else if (fl_get_button(form->flag4)) {
148 inset->setVisibleSpace(fl_get_button(form->flag41));
151 fl_hide_form(form->include);
152 current_view->updateInset(inset, true);
157 fl_hide_form(form->include);
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))
165 else if (fl_get_button(form->flag3))
167 else if (fl_get_button(form->flag4)) {
169 inset->setVisibleSpace(fl_get_button(form->flag41));
172 fl_hide_form(form->include);
173 current_view->updateInset(inset, true);
174 current_view->owner()->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, inset->getContents().c_str());
179 fl_activate_object(form->flag41);
180 fl_set_object_lcol(form->flag41, FL_BLACK);
183 fl_deactivate_object(form->flag41);
184 fl_set_object_lcol(form->flag41, FL_INACTIVE);
185 fl_set_button(form->flag41, 0);
191 static string unique_id() {
192 static unsigned int seed=1000;
195 std::ostringstream ost;
196 ost << "file" << ++seed;
199 ostrstream ost(ctmp,16);
200 ost << "file" << ++seed << '\0';
207 InsetInclude::InsetInclude(string const & fname, Buffer * bf)
208 : InsetCommand("include")
212 flag = InsetInclude::INCLUDE;
214 include_label = unique_id();
218 InsetInclude::~InsetInclude()
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
224 if (form->include->visible) {
225 fl_hide_form(form->include);
227 fl_free_form(form->include);
235 Inset * InsetInclude::Clone() const
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.
245 ii->setVisibleSpace(isVerbVisibleSpace());
251 void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
253 if(bv->buffer()->isReadonly())
254 WarnReadonly(bv->buffer()->fileName());
257 form = create_form_include();
258 fl_set_form_atclose(form->include, IgnoreCloseBoxCB, 0);
260 form->include->u_vdata = this;
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()));
268 fl_set_button(form->flag41, int(isVerbVisibleSpace()));
270 fl_set_button(form->flag41, 0);
271 fl_deactivate_object(form->flag41);
272 fl_set_object_lcol(form->flag41, FL_INACTIVE);
275 if (form->include->visible) {
276 fl_raise_form(form->include);
278 fl_show_form(form->include, FL_PLACE_MOUSE, FL_FULLBORDER,
284 void InsetInclude::Write(Buffer const *, ostream & os) const
286 os << "Include " << getCommand() << "\n";
290 void InsetInclude::Read(Buffer const * buf, LyXLex & lex)
292 InsetCommand::Read(buf, lex);
294 if (getCmdName() == "include")
296 else if (getCmdName() == "input")
298 else if (contains(getCmdName(), "verbatim")) {
300 if (getCmdName() == "verbatiminput*")
301 setVisibleSpace(true);
306 bool InsetInclude::display() const
312 string InsetInclude::getScreenLabel() const
318 temp += _("Verbatim Input");
319 if (isVerbVisibleSpace()) temp += '*';
320 } else temp += _("Include");
323 if (getContents().empty()) {
326 temp+= getContents();
332 void InsetInclude::setContents(string const & c)
334 InsetCommand::setContents(c);
335 filename = MakeAbsPath(getContents(),
336 OnlyPath(getMasterFilename()));
340 bool InsetInclude::loadIfNeeded() const
342 if (isNoLoad() || isVerb()) return false;
343 if (!IsLyXFilename(getFileName())) return false;
345 if (bufferlist.exists(getFileName())) return true;
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 );
354 int InsetInclude::Latex(Buffer const *, ostream & os,
355 bool /*fragile*/, bool /*fs*/) const
357 // Do nothing if no file name has been specified
358 if (getContents().empty())
361 // Use += to force a copy of contents (JMarc)
362 // How does that force anything? (Lgb)
363 string incfile(getContents());
365 if (loadIfNeeded()) {
366 Buffer * tmp = bufferlist.getBuffer(getFileName());
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)
374 << textclasslist.NameOfClass(master->params.textclass)
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, '/','@');
385 incfile = subst(incfile, ':', '$');
387 writefile = AddName(master->tmppath, incfile);
389 writefile = getFileName();
390 writefile = ChangeExtension(writefile, ".tex");
391 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
392 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
394 tmp->markDepClean(master->tmppath);
396 tmp->makeLaTeXFile(writefile,
397 OnlyPath(getMasterFilename()),
398 master->niceFile, true);
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 << '}';
408 os << '\\' << getCmdName() << '{'
409 << ChangeExtension(incfile, ".tex")
413 // \include don't want extension and demands that the
414 // file really have .tex
415 os << '\\' << getCmdName() << '{'
416 << ChangeExtension(incfile, string())
424 int InsetInclude::Linuxdoc(Buffer const *, ostream & os) const
426 // Do nothing if no file name has been specified
427 if (getContents().empty())
430 string incfile(getContents());
432 if (loadIfNeeded()) {
433 Buffer * tmp = bufferlist.getBuffer(getFileName());
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);
441 writefile = getFileName();
443 if(IsLyXFilename(getFileName()))
444 writefile = ChangeExtension(writefile, ".sgml");
446 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
447 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
449 tmp->makeLinuxDocFile(writefile, master->niceFile, true);
453 os << "<!-- includefile verbatim=\"" << incfile << "\" -->";
455 os << '&' << include_label << ';';
461 int InsetInclude::DocBook(Buffer const *, ostream & os) const
463 // Do nothing if no file name has been specified
464 if (getContents().empty())
467 string incfile(getContents());
469 if (loadIfNeeded()) {
470 Buffer * tmp = bufferlist.getBuffer(getFileName());
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);
478 writefile = getFileName();
479 if(IsLyXFilename(getFileName()))
480 writefile = ChangeExtension(writefile, ".sgml");
482 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
483 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
485 tmp->makeDocBookFile(writefile, master->niceFile, true);
489 os << "<!-- includefile verbatim=\"" << incfile << "\" -->";
491 os << '&' << include_label << ';';
497 void InsetInclude::Validate(LaTeXFeatures & features) const
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);
506 // writefile = getFileName();
507 // Use the relative path.
510 if(IsLyXFilename(getFileName()))
511 writefile = ChangeExtension(writefile, ".sgml");
513 features.IncludedFiles[include_label] = writefile;
516 features.verbatim = true;
518 // Here we must do the fun stuff...
519 // Load the file in the include if it needs
521 if (loadIfNeeded()) {
523 Buffer * tmp = bufferlist.getBuffer(getFileName());
524 tmp->validate(features);
529 vector<string> InsetInclude::getLabelList() const
534 if (loadIfNeeded()) {
535 Buffer * tmp = bufferlist.getBuffer(getFileName());
536 tmp->setParentName("");
537 l = tmp->getLabelList();
538 tmp->setParentName(getMasterFilename());
545 vector<pair<string,string> > InsetInclude::getKeys() const
547 vector<pair<string,string> > keys;
549 if (loadIfNeeded()) {
550 Buffer *tmp = bufferlist.getBuffer(getFileName());
551 tmp->setParentName("");
552 keys = tmp->getBibkeyList();
553 tmp->setParentName(getMasterFilename());