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;
199 // Needed if we use lyxstring.
200 return ost.str().c_str();
203 ostrstream ost(ctmp,16);
204 ost << "file" << ++seed << '\0';
206 // Needed if we use lyxstring.
212 InsetInclude::InsetInclude(string const & fname, Buffer * bf)
213 : InsetCommand("include")
217 flag = InsetInclude::INCLUDE;
219 include_label = unique_id();
223 InsetInclude::~InsetInclude()
225 if (form && form->include->u_vdata == this) {
226 // this inset is in the popup so hide the popup
227 // and remove the reference to this inset. ARRae
229 if (form->include->visible) {
230 fl_hide_form(form->include);
232 fl_free_form(form->include);
240 Inset * InsetInclude::Clone() const
242 InsetInclude * ii = new InsetInclude (getContents(), master);
243 ii->setNoLoad(isNoLoad());
244 // By default, the newly created inset is of `include' type,
245 // so we do not test this case.
250 ii->setVisibleSpace(isVerbVisibleSpace());
256 void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
258 if(bv->buffer()->isReadonly())
259 WarnReadonly(bv->buffer()->fileName());
262 form = create_form_include();
263 fl_set_form_atclose(form->include, IgnoreCloseBoxCB, 0);
265 form->include->u_vdata = this;
267 fl_set_input(form->input, getContents().c_str());
268 fl_set_button(form->flag1, int(isNoLoad()));
269 fl_set_button(form->flag2, int(isInput()));
270 fl_set_button(form->flag3, int(isInclude()));
271 fl_set_button(form->flag4, int(isVerb()));
273 fl_set_button(form->flag41, int(isVerbVisibleSpace()));
275 fl_set_button(form->flag41, 0);
276 fl_deactivate_object(form->flag41);
277 fl_set_object_lcol(form->flag41, FL_INACTIVE);
280 if (form->include->visible) {
281 fl_raise_form(form->include);
283 fl_show_form(form->include, FL_PLACE_MOUSE, FL_FULLBORDER,
289 void InsetInclude::Write(Buffer const *, ostream & os) const
291 os << "Include " << getCommand() << "\n";
295 void InsetInclude::Read(Buffer const * buf, LyXLex & lex)
297 InsetCommand::Read(buf, lex);
299 if (getCmdName() == "include")
301 else if (getCmdName() == "input")
303 else if (contains(getCmdName(), "verbatim")) {
305 if (getCmdName() == "verbatiminput*")
306 setVisibleSpace(true);
311 bool InsetInclude::display() const
317 string InsetInclude::getScreenLabel() const
323 temp += _("Verbatim Input");
324 if (isVerbVisibleSpace()) temp += '*';
325 } else temp += _("Include");
328 if (getContents().empty()) {
331 temp+= getContents();
337 void InsetInclude::setContents(string const & c)
339 InsetCommand::setContents(c);
340 filename = MakeAbsPath(getContents(),
341 OnlyPath(getMasterFilename()));
345 bool InsetInclude::loadIfNeeded() const
347 if (isNoLoad() || isVerb()) return false;
348 if (!IsLyXFilename(getFileName())) return false;
350 if (bufferlist.exists(getFileName())) return true;
352 // the readonly flag can/will be wrong, not anymore I think.
353 FileInfo finfo(getFileName());
354 bool ro = !finfo.writable();
355 return ( bufferlist.readFile(getFileName(), ro) != 0 );
359 int InsetInclude::Latex(Buffer const *, ostream & os,
360 bool /*fragile*/, bool /*fs*/) const
362 // Do nothing if no file name has been specified
363 if (getContents().empty())
366 // Use += to force a copy of contents (JMarc)
367 // How does that force anything? (Lgb)
368 string incfile(getContents());
370 if (loadIfNeeded()) {
371 Buffer * tmp = bufferlist.getBuffer(getFileName());
373 if (tmp->params.textclass != master->params.textclass) {
374 lyxerr << "ERROR: Cannot handle include file `"
375 << MakeDisplayPath(getFileName())
376 << "' which has textclass `"
377 << textclasslist.NameOfClass(tmp->params.textclass)
379 << textclasslist.NameOfClass(master->params.textclass)
384 // write it to a file (so far the complete file)
385 string writefile = ChangeExtension(getFileName(), ".tex");
386 if (!master->tmppath.empty()
387 && !master->niceFile) {
388 incfile = subst(incfile, '/','@');
390 incfile = subst(incfile, ':', '$');
392 writefile = AddName(master->tmppath, incfile);
394 writefile = getFileName();
395 writefile = ChangeExtension(writefile, ".tex");
396 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
397 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
399 tmp->markDepClean(master->tmppath);
401 tmp->makeLaTeXFile(writefile,
402 OnlyPath(getMasterFilename()),
403 master->niceFile, true);
407 os << '\\' << getCmdName() << '{' << incfile << '}';
408 } else if (isInput()) {
409 // \input wants file with extension (default is .tex)
410 if (!IsLyXFilename(getFileName())) {
411 os << '\\' << getCmdName() << '{' << incfile << '}';
413 os << '\\' << getCmdName() << '{'
414 << ChangeExtension(incfile, ".tex")
418 // \include don't want extension and demands that the
419 // file really have .tex
420 os << '\\' << getCmdName() << '{'
421 << ChangeExtension(incfile, string())
429 int InsetInclude::Linuxdoc(Buffer const *, ostream & os) const
431 // Do nothing if no file name has been specified
432 if (getContents().empty())
435 string incfile(getContents());
437 if (loadIfNeeded()) {
438 Buffer * tmp = bufferlist.getBuffer(getFileName());
440 // write it to a file (so far the complete file)
441 string writefile = ChangeExtension(getFileName(), ".sgml");
442 if (!master->tmppath.empty() && !master->niceFile) {
443 incfile = subst(incfile, '/','@');
444 writefile = AddName(master->tmppath, incfile);
446 writefile = getFileName();
448 if(IsLyXFilename(getFileName()))
449 writefile = ChangeExtension(writefile, ".sgml");
451 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
452 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
454 tmp->makeLinuxDocFile(writefile, master->niceFile, true);
458 os << "<!-- includefile verbatim=\"" << incfile << "\" -->";
460 os << '&' << include_label << ';';
466 int InsetInclude::DocBook(Buffer const *, ostream & os) const
468 // Do nothing if no file name has been specified
469 if (getContents().empty())
472 string incfile(getContents());
474 if (loadIfNeeded()) {
475 Buffer * tmp = bufferlist.getBuffer(getFileName());
477 // write it to a file (so far the complete file)
478 string writefile = ChangeExtension(getFileName(), ".sgml");
479 if (!master->tmppath.empty() && !master->niceFile) {
480 incfile = subst(incfile, '/','@');
481 writefile = AddName(master->tmppath, incfile);
483 writefile = getFileName();
484 if(IsLyXFilename(getFileName()))
485 writefile = ChangeExtension(writefile, ".sgml");
487 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
488 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
490 tmp->makeDocBookFile(writefile, master->niceFile, true);
494 os << "<!-- includefile verbatim=\"" << incfile << "\" -->";
496 os << '&' << include_label << ';';
502 void InsetInclude::Validate(LaTeXFeatures & features) const
505 string incfile(getContents());
506 string writefile = ChangeExtension(getFileName(), ".sgml");
507 if (!master->tmppath.empty() && !master->niceFile) {
508 incfile = subst(incfile, '/','@');
509 writefile = AddName(master->tmppath, incfile);
511 // writefile = getFileName();
512 // Use the relative path.
515 if(IsLyXFilename(getFileName()))
516 writefile = ChangeExtension(writefile, ".sgml");
518 features.IncludedFiles[include_label] = writefile;
521 features.verbatim = true;
523 // Here we must do the fun stuff...
524 // Load the file in the include if it needs
526 if (loadIfNeeded()) {
528 Buffer * tmp = bufferlist.getBuffer(getFileName());
529 tmp->validate(features);
534 vector<string> 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> > 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());