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;
97 void include_cb(FL_OBJECT *, long arg)
100 InsetInclude * inset = static_cast<InsetInclude*>(form->include->u_vdata);
104 // Should browsing too be disabled in RO-mode?
106 string mpath = OnlyPath(inset->getMasterFilename());
109 if (fl_get_button(form->flag2)) // Use Input Button
111 else if (fl_get_button(form->flag4)) // Verbatim all files
116 fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
118 // Use by default the master's path
119 string filename = fileDlg.Select(_("Select Child Document"),
121 inset->getContents());
122 XFlush(fl_get_display());
124 // check selected filename
125 if (!filename.empty()) {
126 string filename2 = MakeRelPath(filename,
128 if (prefixIs(filename2, ".."))
129 fl_set_input(form->input,
132 fl_set_input(form->input,
139 if(!current_view->buffer()->isReadonly()) {
140 inset->setContents(fl_get_input(form->input));
142 inset->setNoLoad(fl_get_button(form->flag1));
143 if (fl_get_button(form->flag2))
145 else if (fl_get_button(form->flag3))
147 else if (fl_get_button(form->flag4)) {
149 inset->setVisibleSpace(fl_get_button(form->flag41));
152 fl_hide_form(form->include);
153 current_view->updateInset(inset, true);
158 fl_hide_form(form->include);
161 if(!current_view->buffer()->isReadonly()) {
162 inset->setContents(fl_get_input(form->input));
163 inset->setNoLoad(fl_get_button(form->flag1));
164 if (fl_get_button(form->flag2))
166 else if (fl_get_button(form->flag3))
168 else if (fl_get_button(form->flag4)) {
170 inset->setVisibleSpace(fl_get_button(form->flag41));
173 fl_hide_form(form->include);
174 current_view->updateInset(inset, true);
175 current_view->owner()->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, inset->getContents().c_str());
180 fl_activate_object(form->flag41);
181 fl_set_object_lcol(form->flag41, FL_BLACK);
184 fl_deactivate_object(form->flag41);
185 fl_set_object_lcol(form->flag41, FL_INACTIVE);
186 fl_set_button(form->flag41, 0);
192 static string unique_id() {
193 static unsigned int seed=1000;
196 std::ostringstream ost;
197 ost << "file" << ++seed;
200 ostrstream ost(ctmp,16);
201 ost << "file" << ++seed << '\0';
204 // Needed if we use lyxstring.
205 return ost.str().c_str();
209 InsetInclude::InsetInclude(string const & fname, Buffer * bf)
210 : InsetCommand("include")
214 flag = InsetInclude::INCLUDE;
216 include_label = unique_id();
220 InsetInclude::~InsetInclude()
222 if (form && form->include->u_vdata == this) {
223 // this inset is in the popup so hide the popup
224 // and remove the reference to this inset. ARRae
226 if (form->include->visible) {
227 fl_hide_form(form->include);
229 fl_free_form(form->include);
237 Inset * InsetInclude::Clone() const
239 InsetInclude * ii = new InsetInclude (getContents(), master);
240 ii->setNoLoad(isNoLoad());
241 // By default, the newly created inset is of `include' type,
242 // so we do not test this case.
247 ii->setVisibleSpace(isVerbVisibleSpace());
253 void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
255 if(bv->buffer()->isReadonly())
256 WarnReadonly(bv->buffer()->fileName());
259 form = create_form_include();
260 fl_set_form_atclose(form->include, IgnoreCloseBoxCB, 0);
262 form->include->u_vdata = this;
264 fl_set_input(form->input, getContents().c_str());
265 fl_set_button(form->flag1, int(isNoLoad()));
266 fl_set_button(form->flag2, int(isInput()));
267 fl_set_button(form->flag3, int(isInclude()));
268 fl_set_button(form->flag4, int(isVerb()));
270 fl_set_button(form->flag41, int(isVerbVisibleSpace()));
272 fl_set_button(form->flag41, 0);
273 fl_deactivate_object(form->flag41);
274 fl_set_object_lcol(form->flag41, FL_INACTIVE);
277 if (form->include->visible) {
278 fl_raise_form(form->include);
280 fl_show_form(form->include, FL_PLACE_MOUSE, FL_FULLBORDER,
286 void InsetInclude::Write(Buffer const *, ostream & os) const
288 os << "Include " << getCommand() << "\n";
292 void InsetInclude::Read(Buffer const * buf, LyXLex & lex)
294 InsetCommand::Read(buf, lex);
296 if (getCmdName() == "include")
298 else if (getCmdName() == "input")
300 else if (contains(getCmdName(), "verbatim")) {
302 if (getCmdName() == "verbatiminput*")
303 setVisibleSpace(true);
308 bool InsetInclude::display() const
314 string InsetInclude::getScreenLabel() const
320 temp += _("Verbatim Input");
321 if (isVerbVisibleSpace()) temp += '*';
322 } else temp += _("Include");
325 if (getContents().empty()) {
328 temp+= getContents();
334 void InsetInclude::setContents(string const & c)
336 InsetCommand::setContents(c);
337 filename = MakeAbsPath(getContents(),
338 OnlyPath(getMasterFilename()));
342 bool InsetInclude::loadIfNeeded() const
344 if (isNoLoad() || isVerb()) return false;
345 if (!IsLyXFilename(getFileName())) return false;
347 if (bufferlist.exists(getFileName())) return true;
349 // the readonly flag can/will be wrong, not anymore I think.
350 FileInfo finfo(getFileName());
351 bool ro = !finfo.writable();
352 return ( bufferlist.readFile(getFileName(), ro) != 0 );
356 int InsetInclude::Latex(Buffer const *, ostream & os,
357 bool /*fragile*/, bool /*fs*/) const
359 // Do nothing if no file name has been specified
360 if (getContents().empty())
363 // Use += to force a copy of contents (JMarc)
364 // How does that force anything? (Lgb)
365 string incfile(getContents());
367 if (loadIfNeeded()) {
368 Buffer * tmp = bufferlist.getBuffer(getFileName());
370 if (tmp->params.textclass != master->params.textclass) {
371 lyxerr << "ERROR: Cannot handle include file `"
372 << MakeDisplayPath(getFileName())
373 << "' which has textclass `"
374 << textclasslist.NameOfClass(tmp->params.textclass)
376 << textclasslist.NameOfClass(master->params.textclass)
381 // write it to a file (so far the complete file)
382 string writefile = ChangeExtension(getFileName(), ".tex");
383 if (!master->tmppath.empty()
384 && !master->niceFile) {
385 incfile = subst(incfile, '/','@');
387 incfile = subst(incfile, ':', '$');
389 writefile = AddName(master->tmppath, incfile);
391 writefile = getFileName();
392 writefile = ChangeExtension(writefile, ".tex");
393 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
394 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
396 tmp->markDepClean(master->tmppath);
398 tmp->makeLaTeXFile(writefile,
399 OnlyPath(getMasterFilename()),
400 master->niceFile, true);
404 os << '\\' << getCmdName() << '{' << incfile << '}';
405 } else if (isInput()) {
406 // \input wants file with extension (default is .tex)
407 if (!IsLyXFilename(getFileName())) {
408 os << '\\' << getCmdName() << '{' << incfile << '}';
410 os << '\\' << getCmdName() << '{'
411 << ChangeExtension(incfile, ".tex")
415 // \include don't want extension and demands that the
416 // file really have .tex
417 os << '\\' << getCmdName() << '{'
418 << ChangeExtension(incfile, string())
426 int InsetInclude::Linuxdoc(Buffer const *, ostream & os) const
428 // Do nothing if no file name has been specified
429 if (getContents().empty())
432 string incfile(getContents());
434 if (loadIfNeeded()) {
435 Buffer * tmp = bufferlist.getBuffer(getFileName());
437 // write it to a file (so far the complete file)
438 string writefile = ChangeExtension(getFileName(), ".sgml");
439 if (!master->tmppath.empty() && !master->niceFile) {
440 incfile = subst(incfile, '/','@');
441 writefile = AddName(master->tmppath, incfile);
443 writefile = getFileName();
445 if(IsLyXFilename(getFileName()))
446 writefile = ChangeExtension(writefile, ".sgml");
448 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
449 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
451 tmp->makeLinuxDocFile(writefile, master->niceFile, true);
455 os << "<!-- includefile verbatim=\"" << incfile << "\" -->";
457 os << '&' << include_label << ';';
463 int InsetInclude::DocBook(Buffer const *, ostream & os) const
465 // Do nothing if no file name has been specified
466 if (getContents().empty())
469 string incfile(getContents());
471 if (loadIfNeeded()) {
472 Buffer * tmp = bufferlist.getBuffer(getFileName());
474 // write it to a file (so far the complete file)
475 string writefile = ChangeExtension(getFileName(), ".sgml");
476 if (!master->tmppath.empty() && !master->niceFile) {
477 incfile = subst(incfile, '/','@');
478 writefile = AddName(master->tmppath, incfile);
480 writefile = getFileName();
481 if(IsLyXFilename(getFileName()))
482 writefile = ChangeExtension(writefile, ".sgml");
484 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
485 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
487 tmp->makeDocBookFile(writefile, master->niceFile, true);
491 os << "<!-- includefile verbatim=\"" << incfile << "\" -->";
493 os << '&' << include_label << ';';
499 void InsetInclude::Validate(LaTeXFeatures & features) const
502 string incfile(getContents());
503 string writefile = ChangeExtension(getFileName(), ".sgml");
504 if (!master->tmppath.empty() && !master->niceFile) {
505 incfile = subst(incfile, '/','@');
506 writefile = AddName(master->tmppath, incfile);
508 // writefile = getFileName();
509 // Use the relative path.
512 if(IsLyXFilename(getFileName()))
513 writefile = ChangeExtension(writefile, ".sgml");
515 features.IncludedFiles[include_label] = writefile;
518 features.verbatim = true;
520 // Here we must do the fun stuff...
521 // Load the file in the include if it needs
523 if (loadIfNeeded()) {
525 Buffer * tmp = bufferlist.getBuffer(getFileName());
526 tmp->validate(features);
531 vector<string> InsetInclude::getLabelList() const
536 if (loadIfNeeded()) {
537 Buffer * tmp = bufferlist.getBuffer(getFileName());
538 tmp->setParentName("");
539 l = tmp->getLabelList();
540 tmp->setParentName(getMasterFilename());
547 vector<pair<string,string> > InsetInclude::getKeys() const
549 vector<pair<string,string> > keys;
551 if (loadIfNeeded()) {
552 Buffer *tmp = bufferlist.getBuffer(getFileName());
553 tmp->setParentName("");
554 keys = tmp->getBibkeyList();
555 tmp->setParentName(getMasterFilename());