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"
25 extern BufferView * current_view;
28 extern BufferList bufferlist;
29 extern void UpdateInset(Inset * inset, bool mark_dirty = true);
32 FD_include * create_form_include(void)
35 FD_include * fdui = (FD_include *) fl_calloc(1, sizeof(FD_include));
37 fdui->include = fl_bgn_form(FL_NO_BOX, 340, 210);
38 obj = fl_add_box(FL_UP_BOX, 0, 0, 340, 210, "");
39 obj = fl_add_frame(FL_ENGRAVED_FRAME, 10, 70, 160, 90, "");
40 fdui->browsebt = obj = fl_add_button(FL_NORMAL_BUTTON, 230, 30, 100, 30, idex(_("Browse|#B")));
41 fl_set_button_shortcut(obj, scex(_("Browse|#B")), 1);
42 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
43 fl_set_object_callback(obj, include_cb, 0);
44 fdui->flag1 = obj = fl_add_checkbutton(FL_PUSH_BUTTON, 180, 70, 150, 30, idex(_("Don't typeset|#D")));
45 fl_set_button_shortcut(obj, scex(_("Don't typeset|#D")), 1);
46 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
47 obj = fl_add_button(FL_RETURN_BUTTON, 120, 170, 100, 30, _("OK"));
48 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
49 fl_set_object_callback(obj, include_cb, 1);
50 obj = fl_add_button(FL_NORMAL_BUTTON, 230, 170, 100, 30, idex(_("Cancel|^[")));
51 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
52 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
53 fl_set_object_callback(obj, include_cb, 2);
54 obj = fl_add_button(FL_NORMAL_BUTTON, 230, 130, 100, 30, idex(_("Load|#L")));
55 fl_set_button_shortcut(obj, scex(_("Load|#L")), 1);
56 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
57 fl_set_object_callback(obj, include_cb, 5);
58 fdui->input = obj = fl_add_input(FL_NORMAL_INPUT, 10, 30, 210, 30, idex(_("File name:|#F")));
59 fl_set_input_shortcut(obj, scex(_("File name:|#F")), 1);
60 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
61 fl_set_object_lalign(obj, FL_ALIGN_TOP_LEFT);
62 fdui->flag41 = obj = fl_add_checkbutton(FL_PUSH_BUTTON, 180, 100, 150, 30, idex(_("Visible space|#s")));
63 fl_set_button_shortcut(obj, scex(_("Visible space|#s")), 1);
64 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
66 fdui->include_grp = fl_bgn_group();
67 fdui->flag4 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 130, 160, 30, idex(_("Verbatim|#V")));
68 fl_set_button_shortcut(obj, scex(_("Verbatim|#V")), 1);
69 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
70 fl_set_object_callback(obj, include_cb, 10);
71 fdui->flag2 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 100, 160, 30, idex(_("Use input|#i")));
72 fl_set_button_shortcut(obj, scex(_("Use input|#i")), 1);
73 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
74 fl_set_object_callback(obj, include_cb, 11);
75 fdui->flag3 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 70, 160, 30, idex(_("Use include|#U")));
76 fl_set_button_shortcut(obj, scex(_("Use include|#U")), 1);
77 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
78 fl_set_object_callback(obj, include_cb, 11);
83 //fdui->include->fdui = fdui;
87 /*---------------------------------------*/
90 FD_include * form = 0;
92 extern "C" void include_cb(FL_OBJECT *, long arg)
95 InsetInclude * inset = static_cast<InsetInclude*>(form->include->u_vdata);
99 // Should browsing too be disabled in RO-mode?
101 string mpath = OnlyPath(inset->getMasterFilename());
104 if (fl_get_button(form->flag2)) // Use Input Button
106 else if (fl_get_button(form->flag4)) // Verbatim all files
111 fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
113 // Use by default the master's path
114 string filename = fileDlg.Select(_("Select Child Document"),
116 inset->getContents());
117 XFlush(fl_get_display());
119 // check selected filename
120 if (!filename.empty()) {
121 string filename2 = MakeRelPath(filename,
123 if (prefixIs(filename2, ".."))
124 fl_set_input(form->input,
127 fl_set_input(form->input,
134 if(!current_view->buffer()->isReadonly()) {
135 inset->setContents(fl_get_input(form->input));
137 inset->setNoLoad(fl_get_button(form->flag1));
138 if (fl_get_button(form->flag2))
140 else if (fl_get_button(form->flag3))
142 else if (fl_get_button(form->flag4)) {
144 inset->setVisibleSpace(fl_get_button(form->flag41));
147 fl_hide_form(form->include);
153 fl_hide_form(form->include);
156 if(!current_view->buffer()->isReadonly()) {
157 inset->setContents(fl_get_input(form->input));
158 inset->setNoLoad(fl_get_button(form->flag1));
159 if (fl_get_button(form->flag2))
161 else if (fl_get_button(form->flag3))
163 else if (fl_get_button(form->flag4)) {
165 inset->setVisibleSpace(fl_get_button(form->flag41));
168 fl_hide_form(form->include);
170 current_view->owner()->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, inset->getContents().c_str());
175 fl_activate_object(form->flag41);
176 fl_set_object_lcol(form->flag41, FL_BLACK);
179 fl_deactivate_object(form->flag41);
180 fl_set_object_lcol(form->flag41, FL_INACTIVE);
181 fl_set_button(form->flag41, 0);
187 InsetInclude::InsetInclude(string const & fname, Buffer * bf)
188 : InsetCommand("include")
192 flag = InsetInclude::INCLUDE;
197 InsetInclude::~InsetInclude()
199 if (form && form->include->u_vdata == this) {
200 // this inset is in the popup so hide the popup
201 // and remove the reference to this inset. ARRae
203 if (form->include->visible) {
204 fl_hide_form(form->include);
206 fl_free_form(form->include);
214 Inset * InsetInclude::Clone() const
216 InsetInclude * ii = new InsetInclude (contents, master);
217 ii->setNoLoad(isNoLoad());
218 // By default, the newly created inset is of `include' type,
219 // so we do not test this case.
224 ii->setVisibleSpace(isVerbVisibleSpace());
230 void InsetInclude::Edit(int, int)
232 if(current_view->buffer()->isReadonly())
233 WarnReadonly(current_view->buffer()->fileName());
236 form = create_form_include();
237 fl_set_form_atclose(form->include, IgnoreCloseBoxCB, 0);
239 form->include->u_vdata = this;
241 fl_set_input(form->input, contents.c_str());
242 fl_set_button(form->flag1, int(isNoLoad()));
243 fl_set_button(form->flag2, int(isInput()));
244 fl_set_button(form->flag3, int(isInclude()));
245 fl_set_button(form->flag4, int(isVerb()));
247 fl_set_button(form->flag41, int(isVerbVisibleSpace()));
249 fl_set_button(form->flag41, 0);
250 fl_deactivate_object(form->flag41);
251 fl_set_object_lcol(form->flag41, FL_INACTIVE);
254 if (form->include->visible) {
255 fl_raise_form(form->include);
257 fl_show_form(form->include, FL_PLACE_MOUSE, FL_FULLBORDER,
263 void InsetInclude::Write(ostream & os)
265 os << "Include " << getCommand() << "\n";
269 void InsetInclude::Read(LyXLex & lex)
271 InsetCommand::Read(lex);
273 if (getCmdName() == "include")
275 else if (getCmdName() == "input")
277 else if (contains(getCmdName(), "verbatim")) {
279 if (getCmdName() == "verbatiminput*")
280 setVisibleSpace(true);
285 string InsetInclude::getScreenLabel() const
291 temp += _("Verbatim Input");
292 if (isVerbVisibleSpace()) temp += '*';
293 } else temp += _("Include");
296 if (contents.empty()) {
305 bool InsetInclude::loadIfNeeded() const
307 if (isNoLoad() || isVerb()) return false;
308 if (!IsLyXFilename(getFileName())) return false;
310 if (bufferlist.exists(getFileName())) return true;
312 // the readonly flag can/will be wrong, not anymore I think.
313 FileInfo finfo(getFileName());
314 bool ro = !finfo.writable();
315 return ( bufferlist.readFile(getFileName(), ro) != 0 );
319 int InsetInclude::Latex(ostream & os, signed char /*fragile*/)
322 signed char dummy = 0;
323 Latex(include_file, dummy);
329 int InsetInclude::Latex(string & file, signed char /*fragile*/)
331 string writefile, incfile;
333 // Do nothing if no file name has been specified
334 if (contents.empty())
337 // Use += to force a copy of contents (JMarc)
340 if (loadIfNeeded()) {
341 Buffer * tmp = bufferlist.getBuffer(getFileName());
343 if (tmp->params.textclass != master->params.textclass) {
344 lyxerr << "ERROR: Cannot handle include file `"
345 << MakeDisplayPath(getFileName())
346 << "' which has textclass `"
347 << textclasslist.NameOfClass(tmp->params.textclass)
349 << textclasslist.NameOfClass(master->params.textclass)
354 // write it to a file (so far the complete file)
355 writefile = ChangeExtension(getFileName(), ".tex", false);
356 if (!master->tmppath.empty()
357 && !master->niceFile) {
358 incfile = subst(incfile, '/','@');
360 incfile = subst(incfile, ':', '$');
362 writefile = AddName(master->tmppath, incfile);
364 writefile = getFileName();
365 writefile = ChangeExtension(writefile, ".tex", false);
366 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
367 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
369 tmp->markDepClean(master->tmppath);
371 tmp->makeLaTeXFile(writefile,
372 OnlyPath(getMasterFilename()),
373 master->niceFile, true);
378 file += command + '{';
379 file += incfile + '}';
381 else if (isInput()) {
382 // \input wants file with extension (default is .tex)
383 if (!IsLyXFilename(getFileName())) {
385 file += command + '{';
386 file += incfile + '}';
389 file += command + '{';
390 file += ChangeExtension(incfile, ".tex", false)
394 // \include don't want extension and demands that the
395 // file really have .tex
397 file += command + '{';
398 file += 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());