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"
28 extern BufferView * current_view;
30 extern BufferList bufferlist;
33 FD_include * create_form_include(void)
36 FD_include * fdui = (FD_include *) fl_calloc(1, sizeof(FD_include));
38 fdui->include = fl_bgn_form(FL_NO_BOX, 340, 210);
39 obj = fl_add_box(FL_UP_BOX, 0, 0, 340, 210, "");
40 obj = fl_add_frame(FL_ENGRAVED_FRAME, 10, 70, 160, 90, "");
41 fdui->browsebt = obj = fl_add_button(FL_NORMAL_BUTTON, 230, 30, 100, 30, idex(_("Browse|#B")));
42 fl_set_button_shortcut(obj, scex(_("Browse|#B")), 1);
43 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
44 fl_set_object_callback(obj, include_cb, 0);
45 fdui->flag1 = obj = fl_add_checkbutton(FL_PUSH_BUTTON, 180, 70, 150, 30, idex(_("Don't typeset|#D")));
46 fl_set_button_shortcut(obj, scex(_("Don't typeset|#D")), 1);
47 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
48 obj = fl_add_button(FL_RETURN_BUTTON, 120, 170, 100, 30, _("OK"));
49 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
50 fl_set_object_callback(obj, include_cb, 1);
51 obj = fl_add_button(FL_NORMAL_BUTTON, 230, 170, 100, 30, idex(_("Cancel|^[")));
52 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
53 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
54 fl_set_object_callback(obj, include_cb, 2);
55 obj = fl_add_button(FL_NORMAL_BUTTON, 230, 130, 100, 30, idex(_("Load|#L")));
56 fl_set_button_shortcut(obj, scex(_("Load|#L")), 1);
57 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
58 fl_set_object_callback(obj, include_cb, 5);
59 fdui->input = obj = fl_add_input(FL_NORMAL_INPUT, 10, 30, 210, 30, idex(_("File name:|#F")));
60 fl_set_input_shortcut(obj, scex(_("File name:|#F")), 1);
61 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
62 fl_set_object_lalign(obj, FL_ALIGN_TOP_LEFT);
63 fdui->flag41 = obj = fl_add_checkbutton(FL_PUSH_BUTTON, 180, 100, 150, 30, idex(_("Visible space|#s")));
64 fl_set_button_shortcut(obj, scex(_("Visible space|#s")), 1);
65 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
67 fdui->include_grp = fl_bgn_group();
68 fdui->flag4 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 130, 160, 30, idex(_("Verbatim|#V")));
69 fl_set_button_shortcut(obj, scex(_("Verbatim|#V")), 1);
70 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
71 fl_set_object_callback(obj, include_cb, 10);
72 fdui->flag2 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 100, 160, 30, idex(_("Use input|#i")));
73 fl_set_button_shortcut(obj, scex(_("Use input|#i")), 1);
74 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
75 fl_set_object_callback(obj, include_cb, 11);
76 fdui->flag3 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 70, 160, 30, idex(_("Use include|#U")));
77 fl_set_button_shortcut(obj, scex(_("Use include|#U")), 1);
78 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
79 fl_set_object_callback(obj, include_cb, 11);
84 //fdui->include->fdui = fdui;
88 /*---------------------------------------*/
91 FD_include * form = 0;
93 extern "C" void include_cb(FL_OBJECT *, long arg)
96 InsetInclude * inset = static_cast<InsetInclude*>(form->include->u_vdata);
100 // Should browsing too be disabled in RO-mode?
102 string mpath = OnlyPath(inset->getMasterFilename());
105 if (fl_get_button(form->flag2)) // Use Input Button
107 else if (fl_get_button(form->flag4)) // Verbatim all files
112 fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
114 // Use by default the master's path
115 string filename = fileDlg.Select(_("Select Child Document"),
117 inset->getContents());
118 XFlush(fl_get_display());
120 // check selected filename
121 if (!filename.empty()) {
122 string filename2 = MakeRelPath(filename,
124 if (prefixIs(filename2, ".."))
125 fl_set_input(form->input,
128 fl_set_input(form->input,
135 if(!current_view->buffer()->isReadonly()) {
136 inset->setContents(fl_get_input(form->input));
138 inset->setNoLoad(fl_get_button(form->flag1));
139 if (fl_get_button(form->flag2))
141 else if (fl_get_button(form->flag3))
143 else if (fl_get_button(form->flag4)) {
145 inset->setVisibleSpace(fl_get_button(form->flag41));
148 fl_hide_form(form->include);
149 current_view->updateInset(inset, true);
154 fl_hide_form(form->include);
157 if(!current_view->buffer()->isReadonly()) {
158 inset->setContents(fl_get_input(form->input));
159 inset->setNoLoad(fl_get_button(form->flag1));
160 if (fl_get_button(form->flag2))
162 else if (fl_get_button(form->flag3))
164 else if (fl_get_button(form->flag4)) {
166 inset->setVisibleSpace(fl_get_button(form->flag41));
169 fl_hide_form(form->include);
170 current_view->updateInset(inset, true);
171 current_view->owner()->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, inset->getContents().c_str());
176 fl_activate_object(form->flag41);
177 fl_set_object_lcol(form->flag41, FL_BLACK);
180 fl_deactivate_object(form->flag41);
181 fl_set_object_lcol(form->flag41, FL_INACTIVE);
182 fl_set_button(form->flag41, 0);
188 InsetInclude::InsetInclude(string const & fname, Buffer * bf)
189 : InsetCommand("include")
193 flag = InsetInclude::INCLUDE;
198 InsetInclude::~InsetInclude()
200 if (form && form->include->u_vdata == this) {
201 // this inset is in the popup so hide the popup
202 // and remove the reference to this inset. ARRae
204 if (form->include->visible) {
205 fl_hide_form(form->include);
207 fl_free_form(form->include);
215 Inset * InsetInclude::Clone() const
217 InsetInclude * ii = new InsetInclude (contents, master);
218 ii->setNoLoad(isNoLoad());
219 // By default, the newly created inset is of `include' type,
220 // so we do not test this case.
225 ii->setVisibleSpace(isVerbVisibleSpace());
231 void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
233 if(bv->buffer()->isReadonly())
234 WarnReadonly(bv->buffer()->fileName());
237 form = create_form_include();
238 fl_set_form_atclose(form->include, IgnoreCloseBoxCB, 0);
240 form->include->u_vdata = this;
242 fl_set_input(form->input, contents.c_str());
243 fl_set_button(form->flag1, int(isNoLoad()));
244 fl_set_button(form->flag2, int(isInput()));
245 fl_set_button(form->flag3, int(isInclude()));
246 fl_set_button(form->flag4, int(isVerb()));
248 fl_set_button(form->flag41, int(isVerbVisibleSpace()));
250 fl_set_button(form->flag41, 0);
251 fl_deactivate_object(form->flag41);
252 fl_set_object_lcol(form->flag41, FL_INACTIVE);
255 if (form->include->visible) {
256 fl_raise_form(form->include);
258 fl_show_form(form->include, FL_PLACE_MOUSE, FL_FULLBORDER,
264 void InsetInclude::Write(ostream & os) const
266 os << "Include " << getCommand() << "\n";
270 void InsetInclude::Read(LyXLex & lex)
272 InsetCommand::Read(lex);
274 if (getCmdName() == "include")
276 else if (getCmdName() == "input")
278 else if (contains(getCmdName(), "verbatim")) {
280 if (getCmdName() == "verbatiminput*")
281 setVisibleSpace(true);
286 string InsetInclude::getScreenLabel() const
292 temp += _("Verbatim Input");
293 if (isVerbVisibleSpace()) temp += '*';
294 } else temp += _("Include");
297 if (contents.empty()) {
306 bool InsetInclude::loadIfNeeded() const
308 if (isNoLoad() || isVerb()) return false;
309 if (!IsLyXFilename(getFileName())) return false;
311 if (bufferlist.exists(getFileName())) return true;
313 // the readonly flag can/will be wrong, not anymore I think.
314 FileInfo finfo(getFileName());
315 bool ro = !finfo.writable();
316 return ( bufferlist.readFile(getFileName(), ro) != 0 );
320 int InsetInclude::Latex(ostream & os,
321 signed char /*fragile*/, bool /*fs*/) const
323 // Do nothing if no file name has been specified
324 if (contents.empty())
327 // Use += to force a copy of contents (JMarc)
328 // How does that force anything? (Lgb)
329 string incfile(contents);
331 if (loadIfNeeded()) {
332 Buffer * tmp = bufferlist.getBuffer(getFileName());
334 if (tmp->params.textclass != master->params.textclass) {
335 lyxerr << "ERROR: Cannot handle include file `"
336 << MakeDisplayPath(getFileName())
337 << "' which has textclass `"
338 << textclasslist.NameOfClass(tmp->params.textclass)
340 << textclasslist.NameOfClass(master->params.textclass)
345 // write it to a file (so far the complete file)
346 string writefile = ChangeExtension(getFileName(), ".tex", false);
347 if (!master->tmppath.empty()
348 && !master->niceFile) {
349 incfile = subst(incfile, '/','@');
351 incfile = subst(incfile, ':', '$');
353 writefile = AddName(master->tmppath, incfile);
355 writefile = getFileName();
356 writefile = ChangeExtension(writefile, ".tex", false);
357 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
358 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
360 tmp->markDepClean(master->tmppath);
362 tmp->makeLaTeXFile(writefile,
363 OnlyPath(getMasterFilename()),
364 master->niceFile, true);
368 os << '\\' << command << '{' << incfile << '}';
370 else if (isInput()) {
371 // \input wants file with extension (default is .tex)
372 if (!IsLyXFilename(getFileName())) {
373 os << '\\' << command << '{' << incfile << '}';
375 os << '\\' << command << '{'
376 << ChangeExtension(incfile, ".tex", false)
380 // \include don't want extension and demands that the
381 // file really have .tex
382 os << '\\' << command << '{'
383 << ChangeExtension(incfile, string(), false)
391 void InsetInclude::Validate(LaTeXFeatures & features) const
394 features.verbatim = true;
396 // Here we must do the fun stuff...
397 // Load the file in the include if it needs
399 if (loadIfNeeded()) {
401 Buffer * tmp = bufferlist.getBuffer(getFileName());
402 tmp->validate(features);
407 string InsetInclude::getLabel(int) const
413 if (loadIfNeeded()) {
414 Buffer * tmp = bufferlist.getBuffer(getFileName());
415 tmp->setParentName("");
416 label = tmp->getReferenceList('\n');
417 tmp->setParentName(getMasterFilename());
424 int InsetInclude::GetNumberOfLabels() const
428 if (loadIfNeeded()) {
429 Buffer * tmp = bufferlist.getBuffer(getFileName());
430 tmp->setParentName("");
431 label = tmp->getReferenceList('\n');
432 tmp->setParentName(getMasterFilename());
434 int nl = (label.empty())? 0: 1;
440 string InsetInclude::getKeys(char delim) const
444 if (loadIfNeeded()) {
445 Buffer *tmp = bufferlist.getBuffer(getFileName());
446 tmp->setParentName("");
447 lst = tmp->getBibkeyList(delim);
448 tmp->setParentName(getMasterFilename());