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"
30 extern BufferView * current_view;
32 extern BufferList bufferlist;
35 FD_include * create_form_include(void)
38 FD_include * fdui = (FD_include *) fl_calloc(1, sizeof(FD_include));
40 fdui->include = fl_bgn_form(FL_NO_BOX, 340, 210);
41 obj = fl_add_box(FL_UP_BOX, 0, 0, 340, 210, "");
42 obj = fl_add_frame(FL_ENGRAVED_FRAME, 10, 70, 160, 90, "");
43 fdui->browsebt = obj = fl_add_button(FL_NORMAL_BUTTON, 230, 30, 100, 30, idex(_("Browse|#B")));
44 fl_set_button_shortcut(obj, scex(_("Browse|#B")), 1);
45 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
46 fl_set_object_callback(obj, include_cb, 0);
47 fdui->flag1 = obj = fl_add_checkbutton(FL_PUSH_BUTTON, 180, 70, 150, 30, idex(_("Don't typeset|#D")));
48 fl_set_button_shortcut(obj, scex(_("Don't typeset|#D")), 1);
49 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
50 obj = fl_add_button(FL_RETURN_BUTTON, 120, 170, 100, 30, _("OK"));
51 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
52 fl_set_object_callback(obj, include_cb, 1);
53 obj = fl_add_button(FL_NORMAL_BUTTON, 230, 170, 100, 30, idex(_("Cancel|^[")));
54 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
55 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
56 fl_set_object_callback(obj, include_cb, 2);
57 obj = fl_add_button(FL_NORMAL_BUTTON, 230, 130, 100, 30, idex(_("Load|#L")));
58 fl_set_button_shortcut(obj, scex(_("Load|#L")), 1);
59 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
60 fl_set_object_callback(obj, include_cb, 5);
61 fdui->input = obj = fl_add_input(FL_NORMAL_INPUT, 10, 30, 210, 30, idex(_("File name:|#F")));
62 fl_set_input_shortcut(obj, scex(_("File name:|#F")), 1);
63 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
64 fl_set_object_lalign(obj, FL_ALIGN_TOP_LEFT);
65 fdui->flag41 = obj = fl_add_checkbutton(FL_PUSH_BUTTON, 180, 100, 150, 30, idex(_("Visible space|#s")));
66 fl_set_button_shortcut(obj, scex(_("Visible space|#s")), 1);
67 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
69 fdui->include_grp = fl_bgn_group();
70 fdui->flag4 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 130, 160, 30, idex(_("Verbatim|#V")));
71 fl_set_button_shortcut(obj, scex(_("Verbatim|#V")), 1);
72 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
73 fl_set_object_callback(obj, include_cb, 10);
74 fdui->flag2 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 100, 160, 30, idex(_("Use input|#i")));
75 fl_set_button_shortcut(obj, scex(_("Use input|#i")), 1);
76 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
77 fl_set_object_callback(obj, include_cb, 11);
78 fdui->flag3 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 70, 160, 30, idex(_("Use include|#U")));
79 fl_set_button_shortcut(obj, scex(_("Use include|#U")), 1);
80 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
81 fl_set_object_callback(obj, include_cb, 11);
86 //fdui->include->fdui = fdui;
90 /*---------------------------------------*/
93 FD_include * form = 0;
95 extern "C" void include_cb(FL_OBJECT *, long arg)
98 InsetInclude * inset = static_cast<InsetInclude*>(form->include->u_vdata);
102 // Should browsing too be disabled in RO-mode?
104 string mpath = OnlyPath(inset->getMasterFilename());
107 if (fl_get_button(form->flag2)) // Use Input Button
109 else if (fl_get_button(form->flag4)) // Verbatim all files
114 fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
116 // Use by default the master's path
117 string filename = fileDlg.Select(_("Select Child Document"),
119 inset->getContents());
120 XFlush(fl_get_display());
122 // check selected filename
123 if (!filename.empty()) {
124 string filename2 = MakeRelPath(filename,
126 if (prefixIs(filename2, ".."))
127 fl_set_input(form->input,
130 fl_set_input(form->input,
137 if(!current_view->buffer()->isReadonly()) {
138 inset->setContents(fl_get_input(form->input));
140 inset->setNoLoad(fl_get_button(form->flag1));
141 if (fl_get_button(form->flag2))
143 else if (fl_get_button(form->flag3))
145 else if (fl_get_button(form->flag4)) {
147 inset->setVisibleSpace(fl_get_button(form->flag41));
150 fl_hide_form(form->include);
151 current_view->updateInset(inset, true);
156 fl_hide_form(form->include);
159 if(!current_view->buffer()->isReadonly()) {
160 inset->setContents(fl_get_input(form->input));
161 inset->setNoLoad(fl_get_button(form->flag1));
162 if (fl_get_button(form->flag2))
164 else if (fl_get_button(form->flag3))
166 else if (fl_get_button(form->flag4)) {
168 inset->setVisibleSpace(fl_get_button(form->flag41));
171 fl_hide_form(form->include);
172 current_view->updateInset(inset, true);
173 current_view->owner()->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, inset->getContents().c_str());
178 fl_activate_object(form->flag41);
179 fl_set_object_lcol(form->flag41, FL_BLACK);
182 fl_deactivate_object(form->flag41);
183 fl_set_object_lcol(form->flag41, FL_INACTIVE);
184 fl_set_button(form->flag41, 0);
190 InsetInclude::InsetInclude(string const & fname, Buffer * bf)
191 : InsetCommand("include")
195 flag = InsetInclude::INCLUDE;
200 InsetInclude::~InsetInclude()
202 if (form && form->include->u_vdata == this) {
203 // this inset is in the popup so hide the popup
204 // and remove the reference to this inset. ARRae
206 if (form->include->visible) {
207 fl_hide_form(form->include);
209 fl_free_form(form->include);
217 Inset * InsetInclude::Clone() const
219 InsetInclude * ii = new InsetInclude (contents, master);
220 ii->setNoLoad(isNoLoad());
221 // By default, the newly created inset is of `include' type,
222 // so we do not test this case.
227 ii->setVisibleSpace(isVerbVisibleSpace());
233 void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
235 if(bv->buffer()->isReadonly())
236 WarnReadonly(bv->buffer()->fileName());
239 form = create_form_include();
240 fl_set_form_atclose(form->include, IgnoreCloseBoxCB, 0);
242 form->include->u_vdata = this;
244 fl_set_input(form->input, contents.c_str());
245 fl_set_button(form->flag1, int(isNoLoad()));
246 fl_set_button(form->flag2, int(isInput()));
247 fl_set_button(form->flag3, int(isInclude()));
248 fl_set_button(form->flag4, int(isVerb()));
250 fl_set_button(form->flag41, int(isVerbVisibleSpace()));
252 fl_set_button(form->flag41, 0);
253 fl_deactivate_object(form->flag41);
254 fl_set_object_lcol(form->flag41, FL_INACTIVE);
257 if (form->include->visible) {
258 fl_raise_form(form->include);
260 fl_show_form(form->include, FL_PLACE_MOUSE, FL_FULLBORDER,
266 void InsetInclude::Write(ostream & os) const
268 os << "Include " << getCommand() << "\n";
272 void InsetInclude::Read(LyXLex & lex)
274 InsetCommand::Read(lex);
276 if (getCmdName() == "include")
278 else if (getCmdName() == "input")
280 else if (contains(getCmdName(), "verbatim")) {
282 if (getCmdName() == "verbatiminput*")
283 setVisibleSpace(true);
288 bool InsetInclude::display() const
294 string InsetInclude::getScreenLabel() const
300 temp += _("Verbatim Input");
301 if (isVerbVisibleSpace()) temp += '*';
302 } else temp += _("Include");
305 if (contents.empty()) {
314 void InsetInclude::setContents(string const & c)
316 InsetCommand::setContents(c);
317 filename = MakeAbsPath(contents,
318 OnlyPath(getMasterFilename()));
322 bool InsetInclude::loadIfNeeded() const
324 if (isNoLoad() || isVerb()) return false;
325 if (!IsLyXFilename(getFileName())) return false;
327 if (bufferlist.exists(getFileName())) return true;
329 // the readonly flag can/will be wrong, not anymore I think.
330 FileInfo finfo(getFileName());
331 bool ro = !finfo.writable();
332 return ( bufferlist.readFile(getFileName(), ro) != 0 );
336 int InsetInclude::Latex(ostream & os,
337 bool /*fragile*/, bool /*fs*/) const
339 // Do nothing if no file name has been specified
340 if (contents.empty())
343 // Use += to force a copy of contents (JMarc)
344 // How does that force anything? (Lgb)
345 string incfile(contents);
347 if (loadIfNeeded()) {
348 Buffer * tmp = bufferlist.getBuffer(getFileName());
350 if (tmp->params.textclass != master->params.textclass) {
351 lyxerr << "ERROR: Cannot handle include file `"
352 << MakeDisplayPath(getFileName())
353 << "' which has textclass `"
354 << textclasslist.NameOfClass(tmp->params.textclass)
356 << textclasslist.NameOfClass(master->params.textclass)
361 // write it to a file (so far the complete file)
362 string writefile = ChangeExtension(getFileName(), ".tex");
363 if (!master->tmppath.empty()
364 && !master->niceFile) {
365 incfile = subst(incfile, '/','@');
367 incfile = subst(incfile, ':', '$');
369 writefile = AddName(master->tmppath, incfile);
371 writefile = getFileName();
372 writefile = ChangeExtension(writefile, ".tex");
373 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
374 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
376 tmp->markDepClean(master->tmppath);
378 tmp->makeLaTeXFile(writefile,
379 OnlyPath(getMasterFilename()),
380 master->niceFile, true);
384 os << '\\' << command << '{' << incfile << '}';
386 else if (isInput()) {
387 // \input wants file with extension (default is .tex)
388 if (!IsLyXFilename(getFileName())) {
389 os << '\\' << command << '{' << incfile << '}';
391 os << '\\' << command << '{'
392 << ChangeExtension(incfile, ".tex")
396 // \include don't want extension and demands that the
397 // file really have .tex
398 os << '\\' << command << '{'
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 string InsetInclude::getKeys(char delim) const
443 if (loadIfNeeded()) {
444 Buffer *tmp = bufferlist.getBuffer(getFileName());
445 tmp->setParentName("");
446 lst = tmp->getBibkeyList(delim);
447 tmp->setParentName(getMasterFilename());