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"
29 extern BufferView * current_view;
31 extern BufferList bufferlist;
34 FD_include * create_form_include(void)
37 FD_include * fdui = (FD_include *) fl_calloc(1, sizeof(FD_include));
39 fdui->include = fl_bgn_form(FL_NO_BOX, 340, 210);
40 obj = fl_add_box(FL_UP_BOX, 0, 0, 340, 210, "");
41 obj = fl_add_frame(FL_ENGRAVED_FRAME, 10, 70, 160, 90, "");
42 fdui->browsebt = obj = fl_add_button(FL_NORMAL_BUTTON, 230, 30, 100, 30, idex(_("Browse|#B")));
43 fl_set_button_shortcut(obj, scex(_("Browse|#B")), 1);
44 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
45 fl_set_object_callback(obj, include_cb, 0);
46 fdui->flag1 = obj = fl_add_checkbutton(FL_PUSH_BUTTON, 180, 70, 150, 30, idex(_("Don't typeset|#D")));
47 fl_set_button_shortcut(obj, scex(_("Don't typeset|#D")), 1);
48 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
49 obj = fl_add_button(FL_RETURN_BUTTON, 120, 170, 100, 30, _("OK"));
50 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
51 fl_set_object_callback(obj, include_cb, 1);
52 obj = fl_add_button(FL_NORMAL_BUTTON, 230, 170, 100, 30, idex(_("Cancel|^[")));
53 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
54 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
55 fl_set_object_callback(obj, include_cb, 2);
56 obj = fl_add_button(FL_NORMAL_BUTTON, 230, 130, 100, 30, idex(_("Load|#L")));
57 fl_set_button_shortcut(obj, scex(_("Load|#L")), 1);
58 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
59 fl_set_object_callback(obj, include_cb, 5);
60 fdui->input = obj = fl_add_input(FL_NORMAL_INPUT, 10, 30, 210, 30, idex(_("File name:|#F")));
61 fl_set_input_shortcut(obj, scex(_("File name:|#F")), 1);
62 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
63 fl_set_object_lalign(obj, FL_ALIGN_TOP_LEFT);
64 fdui->flag41 = obj = fl_add_checkbutton(FL_PUSH_BUTTON, 180, 100, 150, 30, idex(_("Visible space|#s")));
65 fl_set_button_shortcut(obj, scex(_("Visible space|#s")), 1);
66 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
68 fdui->include_grp = fl_bgn_group();
69 fdui->flag4 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 130, 160, 30, idex(_("Verbatim|#V")));
70 fl_set_button_shortcut(obj, scex(_("Verbatim|#V")), 1);
71 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
72 fl_set_object_callback(obj, include_cb, 10);
73 fdui->flag2 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 100, 160, 30, idex(_("Use input|#i")));
74 fl_set_button_shortcut(obj, scex(_("Use input|#i")), 1);
75 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
76 fl_set_object_callback(obj, include_cb, 11);
77 fdui->flag3 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 70, 160, 30, idex(_("Use include|#U")));
78 fl_set_button_shortcut(obj, scex(_("Use include|#U")), 1);
79 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
80 fl_set_object_callback(obj, include_cb, 11);
85 //fdui->include->fdui = fdui;
89 /*---------------------------------------*/
92 FD_include * form = 0;
94 extern "C" void include_cb(FL_OBJECT *, long arg)
97 InsetInclude * inset = static_cast<InsetInclude*>(form->include->u_vdata);
101 // Should browsing too be disabled in RO-mode?
103 string mpath = OnlyPath(inset->getMasterFilename());
106 if (fl_get_button(form->flag2)) // Use Input Button
108 else if (fl_get_button(form->flag4)) // Verbatim all files
113 fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
115 // Use by default the master's path
116 string filename = fileDlg.Select(_("Select Child Document"),
118 inset->getContents());
119 XFlush(fl_get_display());
121 // check selected filename
122 if (!filename.empty()) {
123 string filename2 = MakeRelPath(filename,
125 if (prefixIs(filename2, ".."))
126 fl_set_input(form->input,
129 fl_set_input(form->input,
136 if(!current_view->buffer()->isReadonly()) {
137 inset->setContents(fl_get_input(form->input));
139 inset->setNoLoad(fl_get_button(form->flag1));
140 if (fl_get_button(form->flag2))
142 else if (fl_get_button(form->flag3))
144 else if (fl_get_button(form->flag4)) {
146 inset->setVisibleSpace(fl_get_button(form->flag41));
149 fl_hide_form(form->include);
150 current_view->updateInset(inset, true);
155 fl_hide_form(form->include);
158 if(!current_view->buffer()->isReadonly()) {
159 inset->setContents(fl_get_input(form->input));
160 inset->setNoLoad(fl_get_button(form->flag1));
161 if (fl_get_button(form->flag2))
163 else if (fl_get_button(form->flag3))
165 else if (fl_get_button(form->flag4)) {
167 inset->setVisibleSpace(fl_get_button(form->flag41));
170 fl_hide_form(form->include);
171 current_view->updateInset(inset, true);
172 current_view->owner()->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, inset->getContents().c_str());
177 fl_activate_object(form->flag41);
178 fl_set_object_lcol(form->flag41, FL_BLACK);
181 fl_deactivate_object(form->flag41);
182 fl_set_object_lcol(form->flag41, FL_INACTIVE);
183 fl_set_button(form->flag41, 0);
189 InsetInclude::InsetInclude(string const & fname, Buffer * bf)
190 : InsetCommand("include")
194 flag = InsetInclude::INCLUDE;
199 InsetInclude::~InsetInclude()
201 if (form && form->include->u_vdata == this) {
202 // this inset is in the popup so hide the popup
203 // and remove the reference to this inset. ARRae
205 if (form->include->visible) {
206 fl_hide_form(form->include);
208 fl_free_form(form->include);
216 Inset * InsetInclude::Clone() const
218 InsetInclude * ii = new InsetInclude (contents, master);
219 ii->setNoLoad(isNoLoad());
220 // By default, the newly created inset is of `include' type,
221 // so we do not test this case.
226 ii->setVisibleSpace(isVerbVisibleSpace());
232 void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
234 if(bv->buffer()->isReadonly())
235 WarnReadonly(bv->buffer()->fileName());
238 form = create_form_include();
239 fl_set_form_atclose(form->include, IgnoreCloseBoxCB, 0);
241 form->include->u_vdata = this;
243 fl_set_input(form->input, contents.c_str());
244 fl_set_button(form->flag1, int(isNoLoad()));
245 fl_set_button(form->flag2, int(isInput()));
246 fl_set_button(form->flag3, int(isInclude()));
247 fl_set_button(form->flag4, int(isVerb()));
249 fl_set_button(form->flag41, int(isVerbVisibleSpace()));
251 fl_set_button(form->flag41, 0);
252 fl_deactivate_object(form->flag41);
253 fl_set_object_lcol(form->flag41, FL_INACTIVE);
256 if (form->include->visible) {
257 fl_raise_form(form->include);
259 fl_show_form(form->include, FL_PLACE_MOUSE, FL_FULLBORDER,
265 void InsetInclude::Write(ostream & os) const
267 os << "Include " << getCommand() << "\n";
271 void InsetInclude::Read(LyXLex & lex)
273 InsetCommand::Read(lex);
275 if (getCmdName() == "include")
277 else if (getCmdName() == "input")
279 else if (contains(getCmdName(), "verbatim")) {
281 if (getCmdName() == "verbatiminput*")
282 setVisibleSpace(true);
287 bool InsetInclude::display() const
293 string InsetInclude::getScreenLabel() const
299 temp += _("Verbatim Input");
300 if (isVerbVisibleSpace()) temp += '*';
301 } else temp += _("Include");
304 if (contents.empty()) {
313 void InsetInclude::setContents(string const & c)
315 InsetCommand::setContents(c);
316 filename = MakeAbsPath(contents,
317 OnlyPath(getMasterFilename()));
321 bool InsetInclude::loadIfNeeded() const
323 if (isNoLoad() || isVerb()) return false;
324 if (!IsLyXFilename(getFileName())) return false;
326 if (bufferlist.exists(getFileName())) return true;
328 // the readonly flag can/will be wrong, not anymore I think.
329 FileInfo finfo(getFileName());
330 bool ro = !finfo.writable();
331 return ( bufferlist.readFile(getFileName(), ro) != 0 );
335 int InsetInclude::Latex(ostream & os,
336 bool /*fragile*/, bool /*fs*/) const
338 // Do nothing if no file name has been specified
339 if (contents.empty())
342 // Use += to force a copy of contents (JMarc)
343 // How does that force anything? (Lgb)
344 string incfile(contents);
346 if (loadIfNeeded()) {
347 Buffer * tmp = bufferlist.getBuffer(getFileName());
349 if (tmp->params.textclass != master->params.textclass) {
350 lyxerr << "ERROR: Cannot handle include file `"
351 << MakeDisplayPath(getFileName())
352 << "' which has textclass `"
353 << textclasslist.NameOfClass(tmp->params.textclass)
355 << textclasslist.NameOfClass(master->params.textclass)
360 // write it to a file (so far the complete file)
361 string writefile = ChangeExtension(getFileName(), ".tex", false);
362 if (!master->tmppath.empty()
363 && !master->niceFile) {
364 incfile = subst(incfile, '/','@');
366 incfile = subst(incfile, ':', '$');
368 writefile = AddName(master->tmppath, incfile);
370 writefile = getFileName();
371 writefile = ChangeExtension(writefile, ".tex", false);
372 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
373 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
375 tmp->markDepClean(master->tmppath);
377 tmp->makeLaTeXFile(writefile,
378 OnlyPath(getMasterFilename()),
379 master->niceFile, true);
383 os << '\\' << command << '{' << incfile << '}';
385 else if (isInput()) {
386 // \input wants file with extension (default is .tex)
387 if (!IsLyXFilename(getFileName())) {
388 os << '\\' << command << '{' << incfile << '}';
390 os << '\\' << command << '{'
391 << ChangeExtension(incfile, ".tex", false)
395 // \include don't want extension and demands that the
396 // file really have .tex
397 os << '\\' << command << '{'
398 << ChangeExtension(incfile, string(), false)
406 void InsetInclude::Validate(LaTeXFeatures & features) const
409 features.verbatim = true;
411 // Here we must do the fun stuff...
412 // Load the file in the include if it needs
414 if (loadIfNeeded()) {
416 Buffer * tmp = bufferlist.getBuffer(getFileName());
417 tmp->validate(features);
422 string InsetInclude::getLabel(int) const
428 if (loadIfNeeded()) {
429 Buffer * tmp = bufferlist.getBuffer(getFileName());
430 tmp->setParentName("");
431 label = tmp->getReferenceList('\n');
432 tmp->setParentName(getMasterFilename());
439 int InsetInclude::GetNumberOfLabels() const
443 if (loadIfNeeded()) {
444 Buffer * tmp = bufferlist.getBuffer(getFileName());
445 tmp->setParentName("");
446 label = tmp->getReferenceList('\n');
447 tmp->setParentName(getMasterFilename());
449 int nl = (label.empty())? 0: 1;
455 string InsetInclude::getKeys(char delim) const
459 if (loadIfNeeded()) {
460 Buffer *tmp = bufferlist.getBuffer(getFileName());
461 tmp->setParentName("");
462 lst = tmp->getBibkeyList(delim);
463 tmp->setParentName(getMasterFilename());