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 const 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 const filename =
121 fileDlg.Select(_("Select Child Document"),
123 inset->getContents());
124 XFlush(fl_get_display());
126 // check selected filename
127 if (!filename.empty()) {
128 string const filename2 = MakeRelPath(filename,
130 if (prefixIs(filename2, ".."))
131 fl_set_input(form->input,
134 fl_set_input(form->input,
141 if(!current_view->buffer()->isReadonly()) {
142 inset->setContents(fl_get_input(form->input));
144 inset->setNoLoad(fl_get_button(form->flag1));
145 if (fl_get_button(form->flag2))
147 else if (fl_get_button(form->flag3))
149 else if (fl_get_button(form->flag4)) {
151 inset->setVisibleSpace(fl_get_button(form->flag41));
154 fl_hide_form(form->include);
155 current_view->updateInset(inset, true);
160 fl_hide_form(form->include);
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))
168 else if (fl_get_button(form->flag3))
170 else if (fl_get_button(form->flag4)) {
172 inset->setVisibleSpace(fl_get_button(form->flag41));
175 fl_hide_form(form->include);
176 current_view->updateInset(inset, true);
177 current_view->owner()->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, inset->getContents());
182 fl_activate_object(form->flag41);
183 fl_set_object_lcol(form->flag41, FL_BLACK);
186 fl_deactivate_object(form->flag41);
187 fl_set_object_lcol(form->flag41, FL_INACTIVE);
188 fl_set_button(form->flag41, 0);
196 static unsigned int seed = 1000;
198 std::ostringstream ost;
199 ost << "file" << ++seed;
201 // Needed if we use lyxstring.
202 return ost.str().c_str();
206 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & bf)
207 : InsetCommand(p), master(&bf)
209 flag = InsetInclude::INCLUDE;
211 include_label = unique_id();
215 InsetInclude::~InsetInclude()
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
221 if (form->include->visible) {
222 fl_hide_form(form->include);
224 fl_free_form(form->include);
232 Inset * InsetInclude::Clone(Buffer const & buffer) const
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.
242 ii->setVisibleSpace(isVerbVisibleSpace());
248 void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
250 if(bv->buffer()->isReadonly())
251 WarnReadonly(bv->buffer()->fileName());
254 form = create_form_include();
255 fl_set_form_atclose(form->include, IgnoreCloseBoxCB, 0);
257 form->include->u_vdata = this;
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()));
265 fl_set_button(form->flag41, int(isVerbVisibleSpace()));
267 fl_set_button(form->flag41, 0);
268 fl_deactivate_object(form->flag41);
269 fl_set_object_lcol(form->flag41, FL_INACTIVE);
272 if (form->include->visible) {
273 fl_raise_form(form->include);
275 fl_show_form(form->include, FL_PLACE_MOUSE, FL_FULLBORDER,
281 void InsetInclude::Write(Buffer const *, ostream & os) const
283 os << "Include " << getCommand() << "\n";
287 void InsetInclude::Read(Buffer const * buf, LyXLex & lex)
289 InsetCommand::Read(buf, lex);
291 if (getCmdName() == "include")
293 else if (getCmdName() == "input")
295 else if (contains(getCmdName(), "verbatim")) {
297 if (getCmdName() == "verbatiminput*")
298 setVisibleSpace(true);
303 bool InsetInclude::display() const
309 string const InsetInclude::getScreenLabel() const
315 temp += _("Verbatim Input");
316 if (isVerbVisibleSpace()) temp += '*';
317 } else temp += _("Include");
320 if (getContents().empty()) {
323 temp+= getContents();
329 string const InsetInclude::getFileName() const
331 return MakeAbsPath(getContents(),
332 OnlyPath(getMasterFilename()));
336 string const InsetInclude::getMasterFilename() const
338 return master->fileName();
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 const ro = !finfo.writable();
352 return bufferlist.readFile(getFileName(), ro) != 0;
356 int InsetInclude::Latex(Buffer const * buffer, ostream & os,
357 bool /*fragile*/, bool /*fs*/) const
359 string incfile(getContents());
361 // Do nothing if no file name has been specified
365 if (loadIfNeeded()) {
366 Buffer * tmp = bufferlist.getBuffer(getFileName());
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)
374 << textclasslist.NameOfClass(buffer->params.textclass)
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, '/','@');
385 incfile = subst(incfile, ':', '$');
387 writefile = AddName(buffer->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(buffer->tmppath);
396 tmp->makeLaTeXFile(writefile,
397 OnlyPath(getMasterFilename()),
398 buffer->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 * buffer, ostream & os) const
426 string incfile(getContents());
428 // Do nothing if no file name has been specified
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 (!buffer->tmppath.empty() && !buffer->niceFile) {
438 incfile = subst(incfile, '/','@');
439 writefile = AddName(buffer->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, buffer->niceFile, true);
453 os << "<!-- includefile verbatim=\"" << incfile << "\" -->";
455 os << '&' << include_label << ';';
461 int InsetInclude::DocBook(Buffer const * buffer, ostream & os) const
463 string incfile(getContents());
465 // Do nothing if no file name has been specified
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 (!buffer->tmppath.empty() && !buffer->niceFile) {
475 incfile = subst(incfile, '/','@');
476 writefile = AddName(buffer->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, buffer->niceFile, true);
489 os << "<inlinegraphic fileref=\"" << incfile << "\" format=\"linespecific\">";
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> const InsetInclude::getLabelList() const
533 if (loadIfNeeded()) {
534 Buffer * tmp = bufferlist.getBuffer(getFileName());
535 tmp->setParentName("");
536 l = tmp->getLabelList();
537 tmp->setParentName(getMasterFilename());
544 vector<pair<string,string> > const InsetInclude::getKeys() const
546 vector<pair<string,string> > keys;
548 if (loadIfNeeded()) {
549 Buffer * tmp = bufferlist.getBuffer(getFileName());
550 tmp->setParentName("");
551 keys = tmp->getBibkeyList();
552 tmp->setParentName(getMasterFilename());