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;
31 FD_include * create_form_include(void)
34 FD_include * fdui = (FD_include *) fl_calloc(1, sizeof(FD_include));
36 fdui->include = fl_bgn_form(FL_NO_BOX, 340, 210);
37 obj = fl_add_box(FL_UP_BOX, 0, 0, 340, 210, "");
38 obj = fl_add_frame(FL_ENGRAVED_FRAME, 10, 70, 160, 90, "");
39 fdui->browsebt = obj = fl_add_button(FL_NORMAL_BUTTON, 230, 30, 100, 30, idex(_("Browse|#B")));
40 fl_set_button_shortcut(obj, scex(_("Browse|#B")), 1);
41 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
42 fl_set_object_callback(obj, include_cb, 0);
43 fdui->flag1 = obj = fl_add_checkbutton(FL_PUSH_BUTTON, 180, 70, 150, 30, idex(_("Don't typeset|#D")));
44 fl_set_button_shortcut(obj, scex(_("Don't typeset|#D")), 1);
45 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
46 obj = fl_add_button(FL_RETURN_BUTTON, 120, 170, 100, 30, _("OK"));
47 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
48 fl_set_object_callback(obj, include_cb, 1);
49 obj = fl_add_button(FL_NORMAL_BUTTON, 230, 170, 100, 30, idex(_("Cancel|^[")));
50 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
51 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
52 fl_set_object_callback(obj, include_cb, 2);
53 obj = fl_add_button(FL_NORMAL_BUTTON, 230, 130, 100, 30, idex(_("Load|#L")));
54 fl_set_button_shortcut(obj, scex(_("Load|#L")), 1);
55 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
56 fl_set_object_callback(obj, include_cb, 5);
57 fdui->input = obj = fl_add_input(FL_NORMAL_INPUT, 10, 30, 210, 30, idex(_("File name:|#F")));
58 fl_set_input_shortcut(obj, scex(_("File name:|#F")), 1);
59 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
60 fl_set_object_lalign(obj, FL_ALIGN_TOP_LEFT);
61 fdui->flag41 = obj = fl_add_checkbutton(FL_PUSH_BUTTON, 180, 100, 150, 30, idex(_("Visible space|#s")));
62 fl_set_button_shortcut(obj, scex(_("Visible space|#s")), 1);
63 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
65 fdui->include_grp = fl_bgn_group();
66 fdui->flag4 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 130, 160, 30, idex(_("Verbatim|#V")));
67 fl_set_button_shortcut(obj, scex(_("Verbatim|#V")), 1);
68 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
69 fl_set_object_callback(obj, include_cb, 10);
70 fdui->flag2 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 100, 160, 30, idex(_("Use input|#i")));
71 fl_set_button_shortcut(obj, scex(_("Use input|#i")), 1);
72 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
73 fl_set_object_callback(obj, include_cb, 11);
74 fdui->flag3 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 70, 160, 30, idex(_("Use include|#U")));
75 fl_set_button_shortcut(obj, scex(_("Use include|#U")), 1);
76 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
77 fl_set_object_callback(obj, include_cb, 11);
82 //fdui->include->fdui = fdui;
86 /*---------------------------------------*/
89 FD_include * form = 0;
91 extern "C" void include_cb(FL_OBJECT *, long arg)
94 InsetInclude * inset = static_cast<InsetInclude*>(form->include->u_vdata);
98 // Should browsing too be disabled in RO-mode?
100 string mpath = OnlyPath(inset->getMasterFilename());
103 if (fl_get_button(form->flag2)) // Use Input Button
105 else if (fl_get_button(form->flag4)) // Verbatim all files
110 fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
112 // Use by default the master's path
113 string filename = fileDlg.Select(_("Select Child Document"),
115 inset->getContents());
116 XFlush(fl_get_display());
118 // check selected filename
119 if (!filename.empty()) {
120 string filename2 = MakeRelPath(filename,
122 if (prefixIs(filename2, ".."))
123 fl_set_input(form->input,
126 fl_set_input(form->input,
133 if(!current_view->buffer()->isReadonly()) {
134 inset->setContents(fl_get_input(form->input));
136 inset->setNoLoad(fl_get_button(form->flag1));
137 if (fl_get_button(form->flag2))
139 else if (fl_get_button(form->flag3))
141 else if (fl_get_button(form->flag4)) {
143 inset->setVisibleSpace(fl_get_button(form->flag41));
146 fl_hide_form(form->include);
147 current_view->updateInset(inset, true);
152 fl_hide_form(form->include);
155 if(!current_view->buffer()->isReadonly()) {
156 inset->setContents(fl_get_input(form->input));
157 inset->setNoLoad(fl_get_button(form->flag1));
158 if (fl_get_button(form->flag2))
160 else if (fl_get_button(form->flag3))
162 else if (fl_get_button(form->flag4)) {
164 inset->setVisibleSpace(fl_get_button(form->flag41));
167 fl_hide_form(form->include);
168 current_view->updateInset(inset, true);
169 current_view->owner()->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, inset->getContents().c_str());
174 fl_activate_object(form->flag41);
175 fl_set_object_lcol(form->flag41, FL_BLACK);
178 fl_deactivate_object(form->flag41);
179 fl_set_object_lcol(form->flag41, FL_INACTIVE);
180 fl_set_button(form->flag41, 0);
186 InsetInclude::InsetInclude(string const & fname, Buffer * bf)
187 : InsetCommand("include")
191 flag = InsetInclude::INCLUDE;
196 InsetInclude::~InsetInclude()
198 if (form && form->include->u_vdata == this) {
199 // this inset is in the popup so hide the popup
200 // and remove the reference to this inset. ARRae
202 if (form->include->visible) {
203 fl_hide_form(form->include);
205 fl_free_form(form->include);
213 Inset * InsetInclude::Clone() const
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());
229 void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
231 if(bv->buffer()->isReadonly())
232 WarnReadonly(bv->buffer()->fileName());
235 form = create_form_include();
236 fl_set_form_atclose(form->include, IgnoreCloseBoxCB, 0);
238 form->include->u_vdata = this;
240 fl_set_input(form->input, contents.c_str());
241 fl_set_button(form->flag1, int(isNoLoad()));
242 fl_set_button(form->flag2, int(isInput()));
243 fl_set_button(form->flag3, int(isInclude()));
244 fl_set_button(form->flag4, int(isVerb()));
246 fl_set_button(form->flag41, int(isVerbVisibleSpace()));
248 fl_set_button(form->flag41, 0);
249 fl_deactivate_object(form->flag41);
250 fl_set_object_lcol(form->flag41, FL_INACTIVE);
253 if (form->include->visible) {
254 fl_raise_form(form->include);
256 fl_show_form(form->include, FL_PLACE_MOUSE, FL_FULLBORDER,
262 void InsetInclude::Write(ostream & os) const
264 os << "Include " << getCommand() << "\n";
268 void InsetInclude::Read(LyXLex & lex)
270 InsetCommand::Read(lex);
272 if (getCmdName() == "include")
274 else if (getCmdName() == "input")
276 else if (contains(getCmdName(), "verbatim")) {
278 if (getCmdName() == "verbatiminput*")
279 setVisibleSpace(true);
284 string InsetInclude::getScreenLabel() const
290 temp += _("Verbatim Input");
291 if (isVerbVisibleSpace()) temp += '*';
292 } else temp += _("Include");
295 if (contents.empty()) {
304 bool InsetInclude::loadIfNeeded() const
306 if (isNoLoad() || isVerb()) return false;
307 if (!IsLyXFilename(getFileName())) return false;
309 if (bufferlist.exists(getFileName())) return true;
311 // the readonly flag can/will be wrong, not anymore I think.
312 FileInfo finfo(getFileName());
313 bool ro = !finfo.writable();
314 return ( bufferlist.readFile(getFileName(), ro) != 0 );
318 int InsetInclude::Latex(ostream & os, signed char /*fragile*/, bool /*fs*/) const
320 // Do nothing if no file name has been specified
321 if (contents.empty())
324 // Use += to force a copy of contents (JMarc)
325 // How does that force anything? (Lgb)
326 string incfile(contents);
328 if (loadIfNeeded()) {
329 Buffer * tmp = bufferlist.getBuffer(getFileName());
331 if (tmp->params.textclass != master->params.textclass) {
332 lyxerr << "ERROR: Cannot handle include file `"
333 << MakeDisplayPath(getFileName())
334 << "' which has textclass `"
335 << textclasslist.NameOfClass(tmp->params.textclass)
337 << textclasslist.NameOfClass(master->params.textclass)
342 // write it to a file (so far the complete file)
343 string writefile = ChangeExtension(getFileName(), ".tex", false);
344 if (!master->tmppath.empty()
345 && !master->niceFile) {
346 incfile = subst(incfile, '/','@');
348 incfile = subst(incfile, ':', '$');
350 writefile = AddName(master->tmppath, incfile);
352 writefile = getFileName();
353 writefile = ChangeExtension(writefile, ".tex", false);
354 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
355 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
357 tmp->markDepClean(master->tmppath);
359 tmp->makeLaTeXFile(writefile,
360 OnlyPath(getMasterFilename()),
361 master->niceFile, true);
365 os << '\\' << command << '{' << incfile << '}';
367 else if (isInput()) {
368 // \input wants file with extension (default is .tex)
369 if (!IsLyXFilename(getFileName())) {
370 os << '\\' << command << '{' << incfile << '}';
372 os << '\\' << command << '{'
373 << ChangeExtension(incfile, ".tex", false)
377 // \include don't want extension and demands that the
378 // file really have .tex
379 os << '\\' << command << '{'
380 << ChangeExtension(incfile, string(), false)
388 void InsetInclude::Validate(LaTeXFeatures & features) const
391 features.verbatim = true;
393 // Here we must do the fun stuff...
394 // Load the file in the include if it needs
396 if (loadIfNeeded()) {
398 Buffer * tmp = bufferlist.getBuffer(getFileName());
399 tmp->validate(features);
404 string InsetInclude::getLabel(int) const
410 if (loadIfNeeded()) {
411 Buffer * tmp = bufferlist.getBuffer(getFileName());
412 tmp->setParentName("");
413 label = tmp->getReferenceList('\n');
414 tmp->setParentName(getMasterFilename());
421 int InsetInclude::GetNumberOfLabels() const
425 if (loadIfNeeded()) {
426 Buffer * tmp = bufferlist.getBuffer(getFileName());
427 tmp->setParentName("");
428 label = tmp->getReferenceList('\n');
429 tmp->setParentName(getMasterFilename());
431 int nl = (label.empty())? 0: 1;
437 string InsetInclude::getKeys(char delim) const
441 if (loadIfNeeded()) {
442 Buffer *tmp = bufferlist.getBuffer(getFileName());
443 tmp->setParentName("");
444 lst = tmp->getBibkeyList(delim);
445 tmp->setParentName(getMasterFilename());