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;
96 extern "C" void include_cb(FL_OBJECT *, long arg)
99 InsetInclude * inset = static_cast<InsetInclude*>(form->include->u_vdata);
103 // Should browsing too be disabled in RO-mode?
105 string mpath = OnlyPath(inset->getMasterFilename());
108 if (fl_get_button(form->flag2)) // Use Input Button
110 else if (fl_get_button(form->flag4)) // Verbatim all files
115 fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
117 // Use by default the master's path
118 string filename = fileDlg.Select(_("Select Child Document"),
120 inset->getContents());
121 XFlush(fl_get_display());
123 // check selected filename
124 if (!filename.empty()) {
125 string filename2 = MakeRelPath(filename,
127 if (prefixIs(filename2, ".."))
128 fl_set_input(form->input,
131 fl_set_input(form->input,
138 if(!current_view->buffer()->isReadonly()) {
139 inset->setContents(fl_get_input(form->input));
141 inset->setNoLoad(fl_get_button(form->flag1));
142 if (fl_get_button(form->flag2))
144 else if (fl_get_button(form->flag3))
146 else if (fl_get_button(form->flag4)) {
148 inset->setVisibleSpace(fl_get_button(form->flag41));
151 fl_hide_form(form->include);
152 current_view->updateInset(inset, true);
157 fl_hide_form(form->include);
160 if(!current_view->buffer()->isReadonly()) {
161 inset->setContents(fl_get_input(form->input));
162 inset->setNoLoad(fl_get_button(form->flag1));
163 if (fl_get_button(form->flag2))
165 else if (fl_get_button(form->flag3))
167 else if (fl_get_button(form->flag4)) {
169 inset->setVisibleSpace(fl_get_button(form->flag41));
172 fl_hide_form(form->include);
173 current_view->updateInset(inset, true);
174 current_view->owner()->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, inset->getContents().c_str());
179 fl_activate_object(form->flag41);
180 fl_set_object_lcol(form->flag41, FL_BLACK);
183 fl_deactivate_object(form->flag41);
184 fl_set_object_lcol(form->flag41, FL_INACTIVE);
185 fl_set_button(form->flag41, 0);
191 InsetInclude::InsetInclude(string const & fname, Buffer * bf)
192 : InsetCommand("include")
196 flag = InsetInclude::INCLUDE;
201 InsetInclude::~InsetInclude()
203 if (form && form->include->u_vdata == this) {
204 // this inset is in the popup so hide the popup
205 // and remove the reference to this inset. ARRae
207 if (form->include->visible) {
208 fl_hide_form(form->include);
210 fl_free_form(form->include);
218 Inset * InsetInclude::Clone() const
220 InsetInclude * ii = new InsetInclude (getContents(), master);
221 ii->setNoLoad(isNoLoad());
222 // By default, the newly created inset is of `include' type,
223 // so we do not test this case.
228 ii->setVisibleSpace(isVerbVisibleSpace());
234 void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
236 if(bv->buffer()->isReadonly())
237 WarnReadonly(bv->buffer()->fileName());
240 form = create_form_include();
241 fl_set_form_atclose(form->include, IgnoreCloseBoxCB, 0);
243 form->include->u_vdata = this;
245 fl_set_input(form->input, getContents().c_str());
246 fl_set_button(form->flag1, int(isNoLoad()));
247 fl_set_button(form->flag2, int(isInput()));
248 fl_set_button(form->flag3, int(isInclude()));
249 fl_set_button(form->flag4, int(isVerb()));
251 fl_set_button(form->flag41, int(isVerbVisibleSpace()));
253 fl_set_button(form->flag41, 0);
254 fl_deactivate_object(form->flag41);
255 fl_set_object_lcol(form->flag41, FL_INACTIVE);
258 if (form->include->visible) {
259 fl_raise_form(form->include);
261 fl_show_form(form->include, FL_PLACE_MOUSE, FL_FULLBORDER,
267 void InsetInclude::Write(Buffer const *, ostream & os) const
269 os << "Include " << getCommand() << "\n";
273 void InsetInclude::Read(Buffer const * buf, LyXLex & lex)
275 InsetCommand::Read(buf, lex);
277 if (getCmdName() == "include")
279 else if (getCmdName() == "input")
281 else if (contains(getCmdName(), "verbatim")) {
283 if (getCmdName() == "verbatiminput*")
284 setVisibleSpace(true);
289 bool InsetInclude::display() const
295 string InsetInclude::getScreenLabel() const
301 temp += _("Verbatim Input");
302 if (isVerbVisibleSpace()) temp += '*';
303 } else temp += _("Include");
306 if (getContents().empty()) {
309 temp+= getContents();
315 void InsetInclude::setContents(string const & c)
317 InsetCommand::setContents(c);
318 filename = MakeAbsPath(getContents(),
319 OnlyPath(getMasterFilename()));
323 bool InsetInclude::loadIfNeeded() const
325 if (isNoLoad() || isVerb()) return false;
326 if (!IsLyXFilename(getFileName())) return false;
328 if (bufferlist.exists(getFileName())) return true;
330 // the readonly flag can/will be wrong, not anymore I think.
331 FileInfo finfo(getFileName());
332 bool ro = !finfo.writable();
333 return ( bufferlist.readFile(getFileName(), ro) != 0 );
337 int InsetInclude::Latex(Buffer const *, ostream & os,
338 bool /*fragile*/, bool /*fs*/) const
340 // Do nothing if no file name has been specified
341 if (getContents().empty())
344 // Use += to force a copy of contents (JMarc)
345 // How does that force anything? (Lgb)
346 string incfile(getContents());
348 if (loadIfNeeded()) {
349 Buffer * tmp = bufferlist.getBuffer(getFileName());
351 if (tmp->params.textclass != master->params.textclass) {
352 lyxerr << "ERROR: Cannot handle include file `"
353 << MakeDisplayPath(getFileName())
354 << "' which has textclass `"
355 << textclasslist.NameOfClass(tmp->params.textclass)
357 << textclasslist.NameOfClass(master->params.textclass)
362 // write it to a file (so far the complete file)
363 string writefile = ChangeExtension(getFileName(), ".tex");
364 if (!master->tmppath.empty()
365 && !master->niceFile) {
366 incfile = subst(incfile, '/','@');
368 incfile = subst(incfile, ':', '$');
370 writefile = AddName(master->tmppath, incfile);
372 writefile = getFileName();
373 writefile = ChangeExtension(writefile, ".tex");
374 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
375 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
377 tmp->markDepClean(master->tmppath);
379 tmp->makeLaTeXFile(writefile,
380 OnlyPath(getMasterFilename()),
381 master->niceFile, true);
385 os << '\\' << getCmdName() << '{' << incfile << '}';
386 } else if (isInput()) {
387 // \input wants file with extension (default is .tex)
388 if (!IsLyXFilename(getFileName())) {
389 os << '\\' << getCmdName() << '{' << incfile << '}';
391 os << '\\' << getCmdName() << '{'
392 << ChangeExtension(incfile, ".tex")
396 // \include don't want extension and demands that the
397 // file really have .tex
398 os << '\\' << getCmdName() << '{'
399 << ChangeExtension(incfile, string())
407 void InsetInclude::Validate(LaTeXFeatures & features) const
410 features.verbatim = true;
412 // Here we must do the fun stuff...
413 // Load the file in the include if it needs
415 if (loadIfNeeded()) {
417 Buffer * tmp = bufferlist.getBuffer(getFileName());
418 tmp->validate(features);
423 vector<string> InsetInclude::getLabelList() const
428 if (loadIfNeeded()) {
429 Buffer * tmp = bufferlist.getBuffer(getFileName());
430 tmp->setParentName("");
431 l = tmp->getLabelList();
432 tmp->setParentName(getMasterFilename());
439 vector<pair<string,string> > InsetInclude::getKeys() const
441 vector<pair<string,string> > keys;
443 if (loadIfNeeded()) {
444 Buffer *tmp = bufferlist.getBuffer(getFileName());
445 tmp->setParentName("");
446 keys = tmp->getBibkeyList();
447 tmp->setParentName(getMasterFilename());