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);
193 static string unique_id() {
194 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();
204 ostrstream ost(ctmp,16);
205 ost << "file" << ++seed << '\0';
207 // Needed if we use lyxstring.
213 InsetInclude::InsetInclude(string const & fname, Buffer * bf)
214 : InsetCommand("include")
218 flag = InsetInclude::INCLUDE;
220 include_label = unique_id();
224 InsetInclude::~InsetInclude()
226 if (form && form->include->u_vdata == this) {
227 // this inset is in the popup so hide the popup
228 // and remove the reference to this inset. ARRae
230 if (form->include->visible) {
231 fl_hide_form(form->include);
233 fl_free_form(form->include);
241 Inset * InsetInclude::Clone() const
243 InsetInclude * ii = new InsetInclude (getContents(), master);
244 ii->setNoLoad(isNoLoad());
245 // By default, the newly created inset is of `include' type,
246 // so we do not test this case.
251 ii->setVisibleSpace(isVerbVisibleSpace());
257 void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
259 if(bv->buffer()->isReadonly())
260 WarnReadonly(bv->buffer()->fileName());
263 form = create_form_include();
264 fl_set_form_atclose(form->include, IgnoreCloseBoxCB, 0);
266 form->include->u_vdata = this;
268 fl_set_input(form->input, getContents().c_str());
269 fl_set_button(form->flag1, int(isNoLoad()));
270 fl_set_button(form->flag2, int(isInput()));
271 fl_set_button(form->flag3, int(isInclude()));
272 fl_set_button(form->flag4, int(isVerb()));
274 fl_set_button(form->flag41, int(isVerbVisibleSpace()));
276 fl_set_button(form->flag41, 0);
277 fl_deactivate_object(form->flag41);
278 fl_set_object_lcol(form->flag41, FL_INACTIVE);
281 if (form->include->visible) {
282 fl_raise_form(form->include);
284 fl_show_form(form->include, FL_PLACE_MOUSE, FL_FULLBORDER,
290 void InsetInclude::Write(Buffer const *, ostream & os) const
292 os << "Include " << getCommand() << "\n";
296 void InsetInclude::Read(Buffer const * buf, LyXLex & lex)
298 InsetCommand::Read(buf, lex);
300 if (getCmdName() == "include")
302 else if (getCmdName() == "input")
304 else if (contains(getCmdName(), "verbatim")) {
306 if (getCmdName() == "verbatiminput*")
307 setVisibleSpace(true);
312 bool InsetInclude::display() const
318 string InsetInclude::getScreenLabel() const
324 temp += _("Verbatim Input");
325 if (isVerbVisibleSpace()) temp += '*';
326 } else temp += _("Include");
329 if (getContents().empty()) {
332 temp+= getContents();
338 string InsetInclude::getFileName() const
340 return MakeAbsPath(getContents(),
341 OnlyPath(getMasterFilename()));
345 string InsetInclude::getMasterFilename() const
347 return master->fileName();
351 bool InsetInclude::loadIfNeeded() const
353 if (isNoLoad() || isVerb()) return false;
354 if (!IsLyXFilename(getFileName())) return false;
356 if (bufferlist.exists(getFileName())) return true;
358 // the readonly flag can/will be wrong, not anymore I think.
359 FileInfo finfo(getFileName());
360 bool ro = !finfo.writable();
361 return ( bufferlist.readFile(getFileName(), ro) != 0 );
365 int InsetInclude::Latex(Buffer const *, ostream & os,
366 bool /*fragile*/, bool /*fs*/) const
368 // Do nothing if no file name has been specified
369 if (getContents().empty())
372 // Use += to force a copy of contents (JMarc)
373 // How does that force anything? (Lgb)
374 string incfile(getContents());
376 if (loadIfNeeded()) {
377 Buffer * tmp = bufferlist.getBuffer(getFileName());
379 if (tmp->params.textclass != master->params.textclass) {
380 lyxerr << "ERROR: Cannot handle include file `"
381 << MakeDisplayPath(getFileName())
382 << "' which has textclass `"
383 << textclasslist.NameOfClass(tmp->params.textclass)
385 << textclasslist.NameOfClass(master->params.textclass)
390 // write it to a file (so far the complete file)
391 string writefile = ChangeExtension(getFileName(), ".tex");
392 if (!master->tmppath.empty()
393 && !master->niceFile) {
394 incfile = subst(incfile, '/','@');
396 incfile = subst(incfile, ':', '$');
398 writefile = AddName(master->tmppath, incfile);
400 writefile = getFileName();
401 writefile = ChangeExtension(writefile, ".tex");
402 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
403 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
405 tmp->markDepClean(master->tmppath);
407 tmp->makeLaTeXFile(writefile,
408 OnlyPath(getMasterFilename()),
409 master->niceFile, true);
413 os << '\\' << getCmdName() << '{' << incfile << '}';
414 } else if (isInput()) {
415 // \input wants file with extension (default is .tex)
416 if (!IsLyXFilename(getFileName())) {
417 os << '\\' << getCmdName() << '{' << incfile << '}';
419 os << '\\' << getCmdName() << '{'
420 << ChangeExtension(incfile, ".tex")
424 // \include don't want extension and demands that the
425 // file really have .tex
426 os << '\\' << getCmdName() << '{'
427 << ChangeExtension(incfile, string())
435 int InsetInclude::Linuxdoc(Buffer const *, ostream & os) const
437 // Do nothing if no file name has been specified
438 if (getContents().empty())
441 string incfile(getContents());
443 if (loadIfNeeded()) {
444 Buffer * tmp = bufferlist.getBuffer(getFileName());
446 // write it to a file (so far the complete file)
447 string writefile = ChangeExtension(getFileName(), ".sgml");
448 if (!master->tmppath.empty() && !master->niceFile) {
449 incfile = subst(incfile, '/','@');
450 writefile = AddName(master->tmppath, incfile);
452 writefile = getFileName();
454 if(IsLyXFilename(getFileName()))
455 writefile = ChangeExtension(writefile, ".sgml");
457 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
458 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
460 tmp->makeLinuxDocFile(writefile, master->niceFile, true);
464 os << "<!-- includefile verbatim=\"" << incfile << "\" -->";
466 os << '&' << include_label << ';';
472 int InsetInclude::DocBook(Buffer const *, ostream & os) const
474 // Do nothing if no file name has been specified
475 if (getContents().empty())
478 string incfile(getContents());
480 if (loadIfNeeded()) {
481 Buffer * tmp = bufferlist.getBuffer(getFileName());
483 // write it to a file (so far the complete file)
484 string writefile = ChangeExtension(getFileName(), ".sgml");
485 if (!master->tmppath.empty() && !master->niceFile) {
486 incfile = subst(incfile, '/','@');
487 writefile = AddName(master->tmppath, incfile);
489 writefile = getFileName();
490 if(IsLyXFilename(getFileName()))
491 writefile = ChangeExtension(writefile, ".sgml");
493 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
494 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
496 tmp->makeDocBookFile(writefile, master->niceFile, true);
500 os << "<!-- includefile verbatim=\"" << incfile << "\" -->";
502 os << '&' << include_label << ';';
508 void InsetInclude::Validate(LaTeXFeatures & features) const
511 string incfile(getContents());
512 string writefile = ChangeExtension(getFileName(), ".sgml");
513 if (!master->tmppath.empty() && !master->niceFile) {
514 incfile = subst(incfile, '/','@');
515 writefile = AddName(master->tmppath, incfile);
517 // writefile = getFileName();
518 // Use the relative path.
521 if(IsLyXFilename(getFileName()))
522 writefile = ChangeExtension(writefile, ".sgml");
524 features.IncludedFiles[include_label] = writefile;
527 features.verbatim = true;
529 // Here we must do the fun stuff...
530 // Load the file in the include if it needs
532 if (loadIfNeeded()) {
534 Buffer * tmp = bufferlist.getBuffer(getFileName());
535 tmp->validate(features);
540 vector<string> InsetInclude::getLabelList() const
545 if (loadIfNeeded()) {
546 Buffer * tmp = bufferlist.getBuffer(getFileName());
547 tmp->setParentName("");
548 l = tmp->getLabelList();
549 tmp->setParentName(getMasterFilename());
556 vector<pair<string,string> > InsetInclude::getKeys() const
558 vector<pair<string,string> > keys;
560 if (loadIfNeeded()) {
561 Buffer *tmp = bufferlist.getBuffer(getFileName());
562 tmp->setParentName("");
563 keys = tmp->getBibkeyList();
564 tmp->setParentName(getMasterFilename());