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 //#ifdef HAVE_SSTREAM
198 std::ostringstream ost;
199 ost << "file" << ++seed;
201 // Needed if we use lyxstring.
202 return ost.str().c_str();
205 // ostrstream ost(ctmp,16);
206 // ost << "file" << ++seed << '\0';
208 // // Needed if we use lyxstring.
214 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer * bf)
215 : InsetCommand(p), master(bf)
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 (params(), 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 const InsetInclude::getScreenLabel() const
323 temp += _("Verbatim Input");
324 if (isVerbVisibleSpace()) temp += '*';
325 } else temp += _("Include");
328 if (getContents().empty()) {
331 temp+= getContents();
337 string const InsetInclude::getFileName() const
339 return MakeAbsPath(getContents(),
340 OnlyPath(getMasterFilename()));
344 string const InsetInclude::getMasterFilename() const
346 return master->fileName();
350 bool InsetInclude::loadIfNeeded() const
352 if (isNoLoad() || isVerb()) return false;
353 if (!IsLyXFilename(getFileName())) return false;
355 if (bufferlist.exists(getFileName())) return true;
357 // the readonly flag can/will be wrong, not anymore I think.
358 FileInfo finfo(getFileName());
359 bool ro = !finfo.writable();
360 return ( bufferlist.readFile(getFileName(), ro) != 0 );
364 int InsetInclude::Latex(Buffer const *, ostream & os,
365 bool /*fragile*/, bool /*fs*/) const
367 // Do nothing if no file name has been specified
368 if (getContents().empty())
371 // Use += to force a copy of contents (JMarc)
372 // How does that force anything? (Lgb)
373 string incfile(getContents());
375 if (loadIfNeeded()) {
376 Buffer * tmp = bufferlist.getBuffer(getFileName());
378 if (tmp->params.textclass != master->params.textclass) {
379 lyxerr << "ERROR: Cannot handle include file `"
380 << MakeDisplayPath(getFileName())
381 << "' which has textclass `"
382 << textclasslist.NameOfClass(tmp->params.textclass)
384 << textclasslist.NameOfClass(master->params.textclass)
389 // write it to a file (so far the complete file)
390 string writefile = ChangeExtension(getFileName(), ".tex");
391 if (!master->tmppath.empty()
392 && !master->niceFile) {
393 incfile = subst(incfile, '/','@');
395 incfile = subst(incfile, ':', '$');
397 writefile = AddName(master->tmppath, incfile);
399 writefile = getFileName();
400 writefile = ChangeExtension(writefile, ".tex");
401 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
402 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
404 tmp->markDepClean(master->tmppath);
406 tmp->makeLaTeXFile(writefile,
407 OnlyPath(getMasterFilename()),
408 master->niceFile, true);
412 os << '\\' << getCmdName() << '{' << incfile << '}';
413 } else if (isInput()) {
414 // \input wants file with extension (default is .tex)
415 if (!IsLyXFilename(getFileName())) {
416 os << '\\' << getCmdName() << '{' << incfile << '}';
418 os << '\\' << getCmdName() << '{'
419 << ChangeExtension(incfile, ".tex")
423 // \include don't want extension and demands that the
424 // file really have .tex
425 os << '\\' << getCmdName() << '{'
426 << ChangeExtension(incfile, string())
434 int InsetInclude::Linuxdoc(Buffer const *, ostream & os) const
436 // Do nothing if no file name has been specified
437 if (getContents().empty())
440 string incfile(getContents());
442 if (loadIfNeeded()) {
443 Buffer * tmp = bufferlist.getBuffer(getFileName());
445 // write it to a file (so far the complete file)
446 string writefile = ChangeExtension(getFileName(), ".sgml");
447 if (!master->tmppath.empty() && !master->niceFile) {
448 incfile = subst(incfile, '/','@');
449 writefile = AddName(master->tmppath, incfile);
451 writefile = getFileName();
453 if(IsLyXFilename(getFileName()))
454 writefile = ChangeExtension(writefile, ".sgml");
456 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
457 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
459 tmp->makeLinuxDocFile(writefile, master->niceFile, true);
463 os << "<!-- includefile verbatim=\"" << incfile << "\" -->";
465 os << '&' << include_label << ';';
471 int InsetInclude::DocBook(Buffer const *, ostream & os) const
473 // Do nothing if no file name has been specified
474 if (getContents().empty())
477 string incfile(getContents());
479 if (loadIfNeeded()) {
480 Buffer * tmp = bufferlist.getBuffer(getFileName());
482 // write it to a file (so far the complete file)
483 string writefile = ChangeExtension(getFileName(), ".sgml");
484 if (!master->tmppath.empty() && !master->niceFile) {
485 incfile = subst(incfile, '/','@');
486 writefile = AddName(master->tmppath, incfile);
488 writefile = getFileName();
489 if(IsLyXFilename(getFileName()))
490 writefile = ChangeExtension(writefile, ".sgml");
492 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
493 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
495 tmp->makeDocBookFile(writefile, master->niceFile, true);
499 os << "<!-- includefile verbatim=\"" << incfile << "\" -->";
501 os << '&' << include_label << ';';
507 void InsetInclude::Validate(LaTeXFeatures & features) const
510 string incfile(getContents());
511 string writefile = ChangeExtension(getFileName(), ".sgml");
512 if (!master->tmppath.empty() && !master->niceFile) {
513 incfile = subst(incfile, '/','@');
514 writefile = AddName(master->tmppath, incfile);
516 // writefile = getFileName();
517 // Use the relative path.
520 if(IsLyXFilename(getFileName()))
521 writefile = ChangeExtension(writefile, ".sgml");
523 features.IncludedFiles[include_label] = writefile;
526 features.verbatim = true;
528 // Here we must do the fun stuff...
529 // Load the file in the include if it needs
531 if (loadIfNeeded()) {
533 Buffer * tmp = bufferlist.getBuffer(getFileName());
534 tmp->validate(features);
539 vector<string> const InsetInclude::getLabelList() const
544 if (loadIfNeeded()) {
545 Buffer * tmp = bufferlist.getBuffer(getFileName());
546 tmp->setParentName("");
547 l = tmp->getLabelList();
548 tmp->setParentName(getMasterFilename());
555 vector<pair<string,string> > const InsetInclude::getKeys() const
557 vector<pair<string,string> > keys;
559 if (loadIfNeeded()) {
560 Buffer *tmp = bufferlist.getBuffer(getFileName());
561 tmp->setParentName("");
562 keys = tmp->getBibkeyList();
563 tmp->setParentName(getMasterFilename());