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 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 filename = fileDlg.Select(_("Select Child Document"),
122 inset->getContents());
123 XFlush(fl_get_display());
125 // check selected filename
126 if (!filename.empty()) {
127 string filename2 = MakeRelPath(filename,
129 if (prefixIs(filename2, ".."))
130 fl_set_input(form->input,
133 fl_set_input(form->input,
140 if(!current_view->buffer()->isReadonly()) {
141 inset->setContents(fl_get_input(form->input));
143 inset->setNoLoad(fl_get_button(form->flag1));
144 if (fl_get_button(form->flag2))
146 else if (fl_get_button(form->flag3))
148 else if (fl_get_button(form->flag4)) {
150 inset->setVisibleSpace(fl_get_button(form->flag41));
153 fl_hide_form(form->include);
154 current_view->updateInset(inset, true);
159 fl_hide_form(form->include);
162 if(!current_view->buffer()->isReadonly()) {
163 inset->setContents(fl_get_input(form->input));
164 inset->setNoLoad(fl_get_button(form->flag1));
165 if (fl_get_button(form->flag2))
167 else if (fl_get_button(form->flag3))
169 else if (fl_get_button(form->flag4)) {
171 inset->setVisibleSpace(fl_get_button(form->flag41));
174 fl_hide_form(form->include);
175 current_view->updateInset(inset, true);
176 current_view->owner()->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, inset->getContents().c_str());
181 fl_activate_object(form->flag41);
182 fl_set_object_lcol(form->flag41, FL_BLACK);
185 fl_deactivate_object(form->flag41);
186 fl_set_object_lcol(form->flag41, FL_INACTIVE);
187 fl_set_button(form->flag41, 0);
195 static unsigned int seed=1000;
197 std::ostringstream ost;
198 ost << "file" << ++seed;
200 // Needed if we use lyxstring.
201 return ost.str().c_str();
205 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer * bf)
206 : InsetCommand(p), master(bf)
208 flag = InsetInclude::INCLUDE;
210 include_label = unique_id();
214 InsetInclude::~InsetInclude()
216 if (form && form->include->u_vdata == this) {
217 // this inset is in the popup so hide the popup
218 // and remove the reference to this inset. ARRae
220 if (form->include->visible) {
221 fl_hide_form(form->include);
223 fl_free_form(form->include);
231 Inset * InsetInclude::Clone() const
233 InsetInclude * ii = new InsetInclude (params(), master);
234 ii->setNoLoad(isNoLoad());
235 // By default, the newly created inset is of `include' type,
236 // so we do not test this case.
241 ii->setVisibleSpace(isVerbVisibleSpace());
247 void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
249 if(bv->buffer()->isReadonly())
250 WarnReadonly(bv->buffer()->fileName());
253 form = create_form_include();
254 fl_set_form_atclose(form->include, IgnoreCloseBoxCB, 0);
256 form->include->u_vdata = this;
258 fl_set_input(form->input, getContents().c_str());
259 fl_set_button(form->flag1, int(isNoLoad()));
260 fl_set_button(form->flag2, int(isInput()));
261 fl_set_button(form->flag3, int(isInclude()));
262 fl_set_button(form->flag4, int(isVerb()));
264 fl_set_button(form->flag41, int(isVerbVisibleSpace()));
266 fl_set_button(form->flag41, 0);
267 fl_deactivate_object(form->flag41);
268 fl_set_object_lcol(form->flag41, FL_INACTIVE);
271 if (form->include->visible) {
272 fl_raise_form(form->include);
274 fl_show_form(form->include, FL_PLACE_MOUSE, FL_FULLBORDER,
280 void InsetInclude::Write(Buffer const *, ostream & os) const
282 os << "Include " << getCommand() << "\n";
286 void InsetInclude::Read(Buffer const * buf, LyXLex & lex)
288 InsetCommand::Read(buf, lex);
290 if (getCmdName() == "include")
292 else if (getCmdName() == "input")
294 else if (contains(getCmdName(), "verbatim")) {
296 if (getCmdName() == "verbatiminput*")
297 setVisibleSpace(true);
302 bool InsetInclude::display() const
308 string const InsetInclude::getScreenLabel() const
314 temp += _("Verbatim Input");
315 if (isVerbVisibleSpace()) temp += '*';
316 } else temp += _("Include");
319 if (getContents().empty()) {
322 temp+= getContents();
328 string const InsetInclude::getFileName() const
330 return MakeAbsPath(getContents(),
331 OnlyPath(getMasterFilename()));
335 string const InsetInclude::getMasterFilename() const
337 return master->fileName();
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> const 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> > const 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());