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, CancelCloseBoxCB, 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,
276 FL_PLACE_MOUSE | FL_FREE_SIZE, FL_TRANSIENT,
282 void InsetInclude::Write(Buffer const *, ostream & os) const
284 os << "Include " << getCommand() << "\n";
288 void InsetInclude::Read(Buffer const * buf, LyXLex & lex)
290 InsetCommand::Read(buf, lex);
292 if (getCmdName() == "include")
294 else if (getCmdName() == "input")
296 else if (contains(getCmdName(), "verbatim")) {
298 if (getCmdName() == "verbatiminput*")
299 setVisibleSpace(true);
304 bool InsetInclude::display() const
310 string const InsetInclude::getScreenLabel() const
316 temp += _("Verbatim Input");
317 if (isVerbVisibleSpace()) temp += '*';
318 } else temp += _("Include");
321 if (getContents().empty()) {
324 temp+= getContents();
330 string const InsetInclude::getFileName() const
332 return MakeAbsPath(getContents(),
333 OnlyPath(getMasterFilename()));
337 string const InsetInclude::getMasterFilename() const
339 return master->fileName();
343 bool InsetInclude::loadIfNeeded() const
345 if (isNoLoad() || isVerb()) return false;
346 if (!IsLyXFilename(getFileName())) return false;
348 if (bufferlist.exists(getFileName())) return true;
350 // the readonly flag can/will be wrong, not anymore I think.
351 FileInfo finfo(getFileName());
352 bool const ro = !finfo.writable();
353 return bufferlist.readFile(getFileName(), ro) != 0;
357 int InsetInclude::Latex(Buffer const * buffer, ostream & os,
358 bool /*fragile*/, bool /*fs*/) const
360 string incfile(getContents());
362 // Do nothing if no file name has been specified
366 if (loadIfNeeded()) {
367 Buffer * tmp = bufferlist.getBuffer(getFileName());
369 if (tmp->params.textclass != buffer->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(buffer->params.textclass)
380 // write it to a file (so far the complete file)
381 string writefile = ChangeExtension(getFileName(), ".tex");
382 if (!buffer->tmppath.empty()
383 && !buffer->niceFile) {
384 incfile = subst(incfile, '/','@');
386 incfile = subst(incfile, ':', '$');
388 writefile = AddName(buffer->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(buffer->tmppath);
397 tmp->makeLaTeXFile(writefile,
398 OnlyPath(getMasterFilename()),
399 buffer->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 * buffer, ostream & os) const
427 string incfile(getContents());
429 // Do nothing if no file name has been specified
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 (!buffer->tmppath.empty() && !buffer->niceFile) {
439 incfile = subst(incfile, '/','@');
440 writefile = AddName(buffer->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, buffer->niceFile, true);
454 os << "<inlinegraphic fileref=\"" << '&' << include_label << ';'
455 << "\" format=\"linespecific\">"
456 << "</inlinegraphic>";
458 os << '&' << include_label << ';';
464 int InsetInclude::DocBook(Buffer const * buffer, ostream & os) const
466 string incfile(getContents());
468 // Do nothing if no file name has been specified
472 if (loadIfNeeded()) {
473 Buffer * tmp = bufferlist.getBuffer(getFileName());
475 // write it to a file (so far the complete file)
476 string writefile = ChangeExtension(getFileName(), ".sgml");
477 if (!buffer->tmppath.empty() && !buffer->niceFile) {
478 incfile = subst(incfile, '/','@');
479 writefile = AddName(buffer->tmppath, incfile);
481 writefile = getFileName();
482 if (IsLyXFilename(getFileName()))
483 writefile = ChangeExtension(writefile, ".sgml");
485 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
486 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
488 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
492 os << "<inlinegraphic fileref=\"" << '&' << include_label << ';'
493 << "\" format=\"linespecific\">"
494 << "</inlinegraphic>";
496 os << '&' << include_label << ';';
502 void InsetInclude::Validate(LaTeXFeatures & features) const
505 string incfile(getContents());
506 string writefile; // = ChangeExtension(getFileName(), ".sgml");
508 if (!master->tmppath.empty() && !master->niceFile) {
509 incfile = subst(incfile, '/','@');
510 writefile = AddName(master->tmppath, incfile);
512 writefile = getFileName();
513 // Use the relative path.
514 //writefile = incfile;
516 if (IsLyXFilename(getFileName()))
517 writefile = ChangeExtension(writefile, ".sgml");
519 features.IncludedFiles[include_label] = writefile;
522 features.verbatim = true;
524 // Here we must do the fun stuff...
525 // Load the file in the include if it needs
527 if (loadIfNeeded()) {
529 Buffer * tmp = bufferlist.getBuffer(getFileName());
530 tmp->validate(features);
535 vector<string> const InsetInclude::getLabelList() const
539 if (loadIfNeeded()) {
540 Buffer * tmp = bufferlist.getBuffer(getFileName());
541 tmp->setParentName("");
542 l = tmp->getLabelList();
543 tmp->setParentName(getMasterFilename());
550 vector<pair<string,string> > const InsetInclude::getKeys() const
552 vector<pair<string,string> > keys;
554 if (loadIfNeeded()) {
555 Buffer * tmp = bufferlist.getBuffer(getFileName());
556 tmp->setParentName("");
557 keys = tmp->getBibkeyList();
558 tmp->setParentName(getMasterFilename());