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';
208 InsetInclude::InsetInclude(string const & fname, Buffer * bf)
209 : InsetCommand("include")
213 flag = InsetInclude::INCLUDE;
215 include_label = unique_id();
219 InsetInclude::~InsetInclude()
221 if (form && form->include->u_vdata == this) {
222 // this inset is in the popup so hide the popup
223 // and remove the reference to this inset. ARRae
225 if (form->include->visible) {
226 fl_hide_form(form->include);
228 fl_free_form(form->include);
236 Inset * InsetInclude::Clone() const
238 InsetInclude * ii = new InsetInclude (getContents(), master);
239 ii->setNoLoad(isNoLoad());
240 // By default, the newly created inset is of `include' type,
241 // so we do not test this case.
246 ii->setVisibleSpace(isVerbVisibleSpace());
252 void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
254 if(bv->buffer()->isReadonly())
255 WarnReadonly(bv->buffer()->fileName());
258 form = create_form_include();
259 fl_set_form_atclose(form->include, IgnoreCloseBoxCB, 0);
261 form->include->u_vdata = this;
263 fl_set_input(form->input, getContents().c_str());
264 fl_set_button(form->flag1, int(isNoLoad()));
265 fl_set_button(form->flag2, int(isInput()));
266 fl_set_button(form->flag3, int(isInclude()));
267 fl_set_button(form->flag4, int(isVerb()));
269 fl_set_button(form->flag41, int(isVerbVisibleSpace()));
271 fl_set_button(form->flag41, 0);
272 fl_deactivate_object(form->flag41);
273 fl_set_object_lcol(form->flag41, FL_INACTIVE);
276 if (form->include->visible) {
277 fl_raise_form(form->include);
279 fl_show_form(form->include, FL_PLACE_MOUSE, FL_FULLBORDER,
285 void InsetInclude::Write(Buffer const *, ostream & os) const
287 os << "Include " << getCommand() << "\n";
291 void InsetInclude::Read(Buffer const * buf, LyXLex & lex)
293 InsetCommand::Read(buf, lex);
295 if (getCmdName() == "include")
297 else if (getCmdName() == "input")
299 else if (contains(getCmdName(), "verbatim")) {
301 if (getCmdName() == "verbatiminput*")
302 setVisibleSpace(true);
307 bool InsetInclude::display() const
313 string InsetInclude::getScreenLabel() const
319 temp += _("Verbatim Input");
320 if (isVerbVisibleSpace()) temp += '*';
321 } else temp += _("Include");
324 if (getContents().empty()) {
327 temp+= getContents();
333 void InsetInclude::setContents(string const & c)
335 InsetCommand::setContents(c);
336 filename = MakeAbsPath(getContents(),
337 OnlyPath(getMasterFilename()));
341 bool InsetInclude::loadIfNeeded() const
343 if (isNoLoad() || isVerb()) return false;
344 if (!IsLyXFilename(getFileName())) return false;
346 if (bufferlist.exists(getFileName())) return true;
348 // the readonly flag can/will be wrong, not anymore I think.
349 FileInfo finfo(getFileName());
350 bool ro = !finfo.writable();
351 return ( bufferlist.readFile(getFileName(), ro) != 0 );
355 int InsetInclude::Latex(Buffer const *, ostream & os,
356 bool /*fragile*/, bool /*fs*/) const
358 // Do nothing if no file name has been specified
359 if (getContents().empty())
362 // Use += to force a copy of contents (JMarc)
363 // How does that force anything? (Lgb)
364 string incfile(getContents());
366 if (loadIfNeeded()) {
367 Buffer * tmp = bufferlist.getBuffer(getFileName());
369 if (tmp->params.textclass != master->params.textclass) {
370 lyxerr << "ERROR: Cannot handle include file `"
371 << MakeDisplayPath(getFileName())
372 << "' which has textclass `"
373 << textclasslist.NameOfClass(tmp->params.textclass)
375 << textclasslist.NameOfClass(master->params.textclass)
380 // write it to a file (so far the complete file)
381 string writefile = ChangeExtension(getFileName(), ".tex");
382 if (!master->tmppath.empty()
383 && !master->niceFile) {
384 incfile = subst(incfile, '/','@');
386 incfile = subst(incfile, ':', '$');
388 writefile = AddName(master->tmppath, incfile);
390 writefile = getFileName();
391 writefile = ChangeExtension(writefile, ".tex");
392 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
393 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
395 tmp->markDepClean(master->tmppath);
397 tmp->makeLaTeXFile(writefile,
398 OnlyPath(getMasterFilename()),
399 master->niceFile, true);
403 os << '\\' << getCmdName() << '{' << incfile << '}';
404 } else if (isInput()) {
405 // \input wants file with extension (default is .tex)
406 if (!IsLyXFilename(getFileName())) {
407 os << '\\' << getCmdName() << '{' << incfile << '}';
409 os << '\\' << getCmdName() << '{'
410 << ChangeExtension(incfile, ".tex")
414 // \include don't want extension and demands that the
415 // file really have .tex
416 os << '\\' << getCmdName() << '{'
417 << ChangeExtension(incfile, string())
425 int InsetInclude::Linuxdoc(Buffer const *, ostream & os) const
427 // Do nothing if no file name has been specified
428 if (getContents().empty())
431 string incfile(getContents());
433 if (loadIfNeeded()) {
434 Buffer * tmp = bufferlist.getBuffer(getFileName());
436 // write it to a file (so far the complete file)
437 string writefile = ChangeExtension(getFileName(), ".sgml");
438 if (!master->tmppath.empty() && !master->niceFile) {
439 incfile = subst(incfile, '/','@');
440 writefile = AddName(master->tmppath, incfile);
442 writefile = getFileName();
444 if(IsLyXFilename(getFileName()))
445 writefile = ChangeExtension(writefile, ".sgml");
447 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
448 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
450 tmp->makeLinuxDocFile(writefile, master->niceFile, true);
454 os << "<!-- includefile verbatim=\"" << incfile << "\" -->";
456 os << '&' << include_label << ';';
462 int InsetInclude::DocBook(Buffer const *, ostream & os) const
464 // Do nothing if no file name has been specified
465 if (getContents().empty())
468 string incfile(getContents());
470 if (loadIfNeeded()) {
471 Buffer * tmp = bufferlist.getBuffer(getFileName());
473 // write it to a file (so far the complete file)
474 string writefile = ChangeExtension(getFileName(), ".sgml");
475 if (!master->tmppath.empty() && !master->niceFile) {
476 incfile = subst(incfile, '/','@');
477 writefile = AddName(master->tmppath, incfile);
479 writefile = getFileName();
480 if(IsLyXFilename(getFileName()))
481 writefile = ChangeExtension(writefile, ".sgml");
483 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
484 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
486 tmp->makeDocBookFile(writefile, master->niceFile, true);
490 os << "<!-- includefile verbatim=\"" << incfile << "\" -->";
492 os << '&' << include_label << ';';
498 void InsetInclude::Validate(LaTeXFeatures & features) const
501 string incfile(getContents());
502 string writefile = ChangeExtension(getFileName(), ".sgml");
503 if (!master->tmppath.empty() && !master->niceFile) {
504 incfile = subst(incfile, '/','@');
505 writefile = AddName(master->tmppath, incfile);
507 // writefile = getFileName();
508 // Use the relative path.
511 if(IsLyXFilename(getFileName()))
512 writefile = ChangeExtension(writefile, ".sgml");
514 features.IncludedFiles[include_label] = writefile;
517 features.verbatim = true;
519 // Here we must do the fun stuff...
520 // Load the file in the include if it needs
522 if (loadIfNeeded()) {
524 Buffer * tmp = bufferlist.getBuffer(getFileName());
525 tmp->validate(features);
530 vector<string> InsetInclude::getLabelList() const
535 if (loadIfNeeded()) {
536 Buffer * tmp = bufferlist.getBuffer(getFileName());
537 tmp->setParentName("");
538 l = tmp->getLabelList();
539 tmp->setParentName(getMasterFilename());
546 vector<pair<string,string> > InsetInclude::getKeys() const
548 vector<pair<string,string> > keys;
550 if (loadIfNeeded()) {
551 Buffer *tmp = bufferlist.getBuffer(getFileName());
552 tmp->setParentName("");
553 keys = tmp->getBibkeyList();
554 tmp->setParentName(getMasterFilename());