9 #include FORMS_H_LOCATION
11 #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);
213 Inset * InsetInclude::Clone()
215 InsetInclude * ii = new InsetInclude (contents, master);
216 ii->setNoLoad(isNoLoad());
217 // By default, the newly created inset is of `include' type,
218 // so we do not test this case.
223 ii->setVisibleSpace(isVerbVisibleSpace());
228 void InsetInclude::Edit(int, int)
230 if(current_view->buffer()->isReadonly())
234 form = create_form_include();
235 fl_set_form_atclose(form->include, IgnoreCloseBoxCB, 0);
237 form->include->u_vdata = this;
239 fl_set_input(form->input, contents.c_str());
240 fl_set_button(form->flag1, int(isNoLoad()));
241 fl_set_button(form->flag2, int(isInput()));
242 fl_set_button(form->flag3, int(isInclude()));
243 fl_set_button(form->flag4, int(isVerb()));
245 fl_set_button(form->flag41, int(isVerbVisibleSpace()));
247 fl_set_button(form->flag41, 0);
248 fl_deactivate_object(form->flag41);
249 fl_set_object_lcol(form->flag41, FL_INACTIVE);
252 if (form->include->visible) {
253 fl_raise_form(form->include);
255 fl_show_form(form->include, FL_PLACE_MOUSE, FL_FULLBORDER,
261 void InsetInclude::Write(FILE * file)
263 fprintf(file, "Include %s\n", getCommand().c_str());
267 void InsetInclude::Read(LyXLex & lex)
269 InsetCommand::Read(lex);
271 if (getCmdName() == "include")
273 else if (getCmdName() == "input")
275 else if (contains(getCmdName(), "verbatim")) {
277 if (getCmdName() == "verbatiminput*")
278 setVisibleSpace(true);
283 string InsetInclude::getScreenLabel() const
289 temp += _("Verbatim Input");
290 if (isVerbVisibleSpace()) temp += '*';
291 } else temp += _("Include");
294 if (contents.empty()) {
303 bool InsetInclude::loadIfNeeded() const
305 if (isNoLoad() || isVerb()) return false;
306 if (!IsLyXFilename(getFileName())) return false;
308 if (bufferlist.exists(getFileName())) return true;
310 // the readonly flag can/will be wrong, not anymore I think.
311 FileInfo finfo(getFileName());
312 bool ro = !finfo.writable();
313 return ( bufferlist.readFile(getFileName(), ro) != 0 );
317 int InsetInclude::Latex(FILE * file, signed char /*fragile*/)
320 signed char dummy = 0;
321 Latex(include_file, dummy);
322 fprintf(file, "%s", include_file.c_str());
327 int InsetInclude::Latex(string & file, signed char /*fragile*/)
329 string writefile, incfile;
331 // Do nothing if no file name has been specified
332 if (contents.empty())
335 // Use += to force a copy of contents (JMarc)
338 if (loadIfNeeded()) {
339 Buffer *tmp = bufferlist.getBuffer(getFileName());
341 if (tmp->params.textclass != master->params.textclass) {
342 lyxerr << "ERROR: Cannot handle include file `"
343 << MakeDisplayPath(getFileName())
344 << "' which has textclass `"
345 << textclasslist.NameOfClass(tmp->params.textclass)
347 << textclasslist.NameOfClass(master->params.textclass)
352 // write it to a file (so far the complete file)
353 writefile = ChangeExtension(getFileName(), ".tex", false);
354 if (!master->tmppath.empty()
355 && !master->niceFile) {
356 incfile = subst(incfile, '/','@');
358 incfile = subst(incfile, ':', '$');
360 writefile = AddName(master->tmppath, incfile);
362 writefile = getFileName();
363 writefile = ChangeExtension(writefile, ".tex", false);
364 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
365 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
367 tmp->markDepClean(master->tmppath);
369 tmp->makeLaTeXFile(writefile,
370 OnlyPath(getMasterFilename()),
371 master->niceFile, true);
376 file += command + '{';
377 file += incfile + '}';
379 else if (isInput()) {
380 // \input wants file with extension (default is .tex)
381 if (!IsLyXFilename(getFileName())) {
383 file += command + '{';
384 file += incfile + '}';
387 file += command + '{';
388 file += ChangeExtension(incfile, ".tex", false)
392 // \include don't want extension and demands that the
393 // file really have .tex
395 file += command + '{';
396 file += ChangeExtension(incfile, string(), false)
404 void InsetInclude::Validate(LaTeXFeatures & features) const
407 features.verbatim = true;
409 // Here we must do the fun stuff...
410 // Load the file in the include if it needs
412 if (loadIfNeeded()) {
414 Buffer * tmp = bufferlist.getBuffer(getFileName());
415 tmp->validate(features);
420 string InsetInclude::getLabel(int) const
426 if (loadIfNeeded()) {
427 Buffer * tmp = bufferlist.getBuffer(getFileName());
428 tmp->setParentName("");
429 label = tmp->getReferenceList('\n');
430 tmp->setParentName(getMasterFilename());
437 int InsetInclude::GetNumberOfLabels() const {
441 if (loadIfNeeded()) {
442 Buffer * tmp = bufferlist.getBuffer(getFileName());
443 tmp->setParentName("");
444 label = tmp->getReferenceList('\n');
445 tmp->setParentName(getMasterFilename());
447 nl = (label.empty())? 0: 1;
453 string InsetInclude::getKeys() const
457 if (loadIfNeeded()) {
458 Buffer *tmp = bufferlist.getBuffer(getFileName());
459 tmp->setParentName("");
460 lst = tmp->getBibkeyList(',');
461 tmp->setParentName(getMasterFilename());