]> git.lyx.org Git - lyx.git/blob - src/insets/insetinclude.C
patch from dekel
[lyx.git] / src / insets / insetinclude.C
1 #include <config.h>
2
3 #include <cstdlib>
4
5 #ifdef __GNUG__
6 #pragma implementation
7 #endif
8
9 #include FORMS_H_LOCATION 
10 #include "insetinclude.h"
11 #include "filedlg.h" 
12 #include "buffer.h"
13 #include "bufferlist.h"
14 #include "debug.h"
15 #include "support/filetools.h"
16 #include "lyxrc.h"
17 #include "LyXView.h"
18 #include "LaTeXFeatures.h"
19 #include "lyx_gui_misc.h" // CancelCloseBoxCB
20 #include "gettext.h"
21 #include "include_form.h"
22 #include "support/FileInfo.h"
23 #include "layout.h"
24 #include "lyxfunc.h"
25
26 using std::ostream;
27 using std::endl;
28 using std::vector;
29
30 extern BufferView * current_view;
31
32 extern BufferList bufferlist;
33
34
35 FD_include * create_form_include(void)
36 {
37   FL_OBJECT * obj;
38   FD_include * fdui = (FD_include *) fl_calloc(1, sizeof(FD_include));
39
40   fdui->include = fl_bgn_form(FL_NO_BOX, 340, 210);
41   obj = fl_add_box(FL_UP_BOX, 0, 0, 340, 210, "");
42   obj = fl_add_frame(FL_ENGRAVED_FRAME, 10, 70, 160, 90, "");
43   fdui->browsebt = obj = fl_add_button(FL_NORMAL_BUTTON, 230, 30, 100, 30, idex(_("Browse|#B")));
44     fl_set_button_shortcut(obj, scex(_("Browse|#B")), 1);
45     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
46     fl_set_object_callback(obj, include_cb, 0);
47   fdui->flag1 = obj = fl_add_checkbutton(FL_PUSH_BUTTON, 180, 70, 150, 30, idex(_("Don't typeset|#D")));
48     fl_set_button_shortcut(obj, scex(_("Don't typeset|#D")), 1);
49     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
50   obj = fl_add_button(FL_RETURN_BUTTON, 120, 170, 100, 30, _("OK"));
51     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
52     fl_set_object_callback(obj, include_cb, 1);
53   obj = fl_add_button(FL_NORMAL_BUTTON, 230, 170, 100, 30, idex(_("Cancel|^[")));
54     fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
55     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
56     fl_set_object_callback(obj, include_cb, 2);
57   obj = fl_add_button(FL_NORMAL_BUTTON, 230, 130, 100, 30, idex(_("Load|#L")));
58     fl_set_button_shortcut(obj, scex(_("Load|#L")), 1);
59     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
60     fl_set_object_callback(obj, include_cb, 5);
61   fdui->input = obj = fl_add_input(FL_NORMAL_INPUT, 10, 30, 210, 30, idex(_("File name:|#F")));
62     fl_set_input_shortcut(obj, scex(_("File name:|#F")), 1);
63     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
64     fl_set_object_lalign(obj, FL_ALIGN_TOP_LEFT);
65   fdui->flag41 = obj = fl_add_checkbutton(FL_PUSH_BUTTON, 180, 100, 150, 30, idex(_("Visible space|#s")));
66     fl_set_button_shortcut(obj, scex(_("Visible space|#s")), 1);
67     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
68
69   fdui->include_grp = fl_bgn_group();
70   fdui->flag4 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 130, 160, 30, idex(_("Verbatim|#V")));
71     fl_set_button_shortcut(obj, scex(_("Verbatim|#V")), 1);
72     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
73     fl_set_object_callback(obj, include_cb, 10);
74   fdui->flag2 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 100, 160, 30, idex(_("Use input|#i")));
75     fl_set_button_shortcut(obj, scex(_("Use input|#i")), 1);
76     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
77     fl_set_object_callback(obj, include_cb, 11);
78   fdui->flag3 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 70, 160, 30, idex(_("Use include|#U")));
79     fl_set_button_shortcut(obj, scex(_("Use include|#U")), 1);
80     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
81     fl_set_object_callback(obj, include_cb, 11);
82   fl_end_group();
83
84   fl_end_form();
85
86   //fdui->include->fdui = fdui;
87
88   return fdui;
89 }
90 /*---------------------------------------*/
91
92
93 FD_include * form = 0;
94
95 extern "C" void include_cb(FL_OBJECT *, long arg)
96 {
97     
98         InsetInclude * inset = static_cast<InsetInclude*>(form->include->u_vdata);
99         switch (arg) {
100         case 0:
101         {
102                 // Should browsing too be disabled in RO-mode?
103                 LyXFileDlg fileDlg;
104                 string mpath = OnlyPath(inset->getMasterFilename());
105                 string ext;
106     
107                 if (fl_get_button(form->flag2)) // Use Input Button
108                         ext = "*.tex";
109                 else if (fl_get_button(form->flag4)) // Verbatim all files
110                         ext = "*";
111                 else
112                         ext = "*.lyx";
113                 // launches dialog
114                 fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
115     
116                 // Use by default the master's path
117                 string filename = fileDlg.Select(_("Select Child Document"),
118                                                   mpath, ext, 
119                                                   inset->getContents());
120                 XFlush(fl_get_display());
121  
122                 // check selected filename
123                 if (!filename.empty()) {
124                         string filename2 = MakeRelPath(filename,
125                                                         mpath);
126                         if (prefixIs(filename2, ".."))
127                                 fl_set_input(form->input,
128                                              filename.c_str());
129                         else
130                                 fl_set_input(form->input,
131                                              filename2.c_str());
132                 }
133                 break;
134         }
135
136         case 1:
137                 if(!current_view->buffer()->isReadonly()) {
138                         inset->setContents(fl_get_input(form->input));
139                         // don't typeset
140                         inset->setNoLoad(fl_get_button(form->flag1));
141                         if (fl_get_button(form->flag2))
142                                 inset->setInput();
143                         else if (fl_get_button(form->flag3))
144                                 inset->setInclude();
145                         else if (fl_get_button(form->flag4)) {
146                                 inset->setVerb();
147                                 inset->setVisibleSpace(fl_get_button(form->flag41));
148                         }
149                         
150                         fl_hide_form(form->include);
151                         current_view->updateInset(inset, true);
152                         break;
153                 } // fall through
154                 
155         case 2:
156                 fl_hide_form(form->include);
157                 break;
158         case 5:
159                 if(!current_view->buffer()->isReadonly()) {
160                         inset->setContents(fl_get_input(form->input));
161                         inset->setNoLoad(fl_get_button(form->flag1));
162                         if (fl_get_button(form->flag2))
163                                 inset->setInput();
164                         else if (fl_get_button(form->flag3))
165                                 inset->setInclude();
166                         else if (fl_get_button(form->flag4)) {
167                                 inset->setVerb();
168                                 inset->setVisibleSpace(fl_get_button(form->flag41));
169                         }
170                         
171                         fl_hide_form(form->include);
172                         current_view->updateInset(inset, true);
173                         current_view->owner()->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, inset->getContents().c_str());
174                 }
175                 break;
176                 
177         case 10:
178                 fl_activate_object(form->flag41);
179                 fl_set_object_lcol(form->flag41, FL_BLACK); 
180                 break;
181         case 11:
182                 fl_deactivate_object(form->flag41);
183                 fl_set_object_lcol(form->flag41, FL_INACTIVE);
184                 fl_set_button(form->flag41, 0);
185                 break;
186         }
187 }
188
189
190 InsetInclude::InsetInclude(string const & fname, Buffer * bf)
191         : InsetCommand("include") 
192 {
193         master = bf;
194         setContents(fname);
195         flag = InsetInclude::INCLUDE;
196         noload = false;
197 }
198
199
200 InsetInclude::~InsetInclude()
201 {
202         if (form && form->include->u_vdata == this) {
203                 // this inset is in the popup so hide the popup 
204                 // and remove the reference to this inset. ARRae
205                 if (form->include) {
206                         if (form->include->visible) {
207                                 fl_hide_form(form->include);
208                         }
209                         fl_free_form(form->include);
210                 }
211                 fl_free(form);
212                 form = 0;
213         }
214 }
215
216
217 Inset * InsetInclude::Clone() const
218
219         InsetInclude * ii = new InsetInclude (contents, master); 
220         ii->setNoLoad(isNoLoad());
221         // By default, the newly created inset is of `include' type,
222         // so we do not test this case.
223         if (isInput())
224                 ii->setInput();
225         else if (isVerb()) {
226                 ii->setVerb();
227                 ii->setVisibleSpace(isVerbVisibleSpace());
228         }
229         return ii;
230 }
231
232
233 void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
234 {
235         if(bv->buffer()->isReadonly())
236                 WarnReadonly(bv->buffer()->fileName());
237
238         if (!form) {
239                 form = create_form_include();
240                 fl_set_form_atclose(form->include, IgnoreCloseBoxCB, 0);
241         }
242         form->include->u_vdata = this;
243     
244         fl_set_input(form->input, contents.c_str());
245         fl_set_button(form->flag1, int(isNoLoad()));
246         fl_set_button(form->flag2, int(isInput()));
247         fl_set_button(form->flag3, int(isInclude()));
248         fl_set_button(form->flag4, int(isVerb()));
249         if (isVerb()) 
250             fl_set_button(form->flag41, int(isVerbVisibleSpace()));
251         else {
252             fl_set_button(form->flag41, 0);
253             fl_deactivate_object(form->flag41);
254             fl_set_object_lcol(form->flag41, FL_INACTIVE);
255         }
256         
257         if (form->include->visible) {
258                 fl_raise_form(form->include);
259         } else {
260                 fl_show_form(form->include, FL_PLACE_MOUSE, FL_FULLBORDER,
261                              _("Include"));
262         }
263 }
264
265
266 void InsetInclude::Write(ostream & os) const
267 {
268         os << "Include " << getCommand() << "\n";
269 }
270
271
272 void InsetInclude::Read(LyXLex & lex)
273 {
274         InsetCommand::Read(lex);
275     
276         if (getCmdName() == "include")
277                 setInclude();
278         else if (getCmdName() == "input")
279                 setInput();
280         else if (contains(getCmdName(), "verbatim")) {
281                 setVerb();
282                 if (getCmdName() == "verbatiminput*")
283                         setVisibleSpace(true);
284         }
285 }
286
287
288 bool InsetInclude::display() const 
289 {
290         return !isInput();
291 }
292
293
294 string InsetInclude::getScreenLabel() const
295 {
296         string temp;
297         if (isInput())
298                 temp += _("Input");
299         else if (isVerb()) {
300                 temp += _("Verbatim Input");
301                 if (isVerbVisibleSpace()) temp += '*';
302         } else temp += _("Include");
303         temp += ": ";
304         
305         if (contents.empty()) {
306                 temp+= "???";
307         } else {
308                 temp+= contents;
309         }
310         return temp;
311 }
312
313
314 void InsetInclude::setContents(string const & c)
315 {
316         InsetCommand::setContents(c);
317         filename = MakeAbsPath(contents, 
318                                OnlyPath(getMasterFilename())); 
319 }
320
321
322 bool InsetInclude::loadIfNeeded() const
323 {
324         if (isNoLoad() || isVerb()) return false;
325         if (!IsLyXFilename(getFileName())) return false;
326         
327         if (bufferlist.exists(getFileName())) return true;
328         
329         // the readonly flag can/will be wrong, not anymore I think.
330         FileInfo finfo(getFileName());
331         bool ro = !finfo.writable();
332         return ( bufferlist.readFile(getFileName(), ro) != 0 );
333 }
334
335
336 int InsetInclude::Latex(ostream & os,
337                         bool /*fragile*/, bool /*fs*/) const
338 {
339         // Do nothing if no file name has been specified
340         if (contents.empty())
341                 return 0;
342     
343         // Use += to force a copy of contents (JMarc)
344         // How does that force anything? (Lgb)
345         string incfile(contents);
346
347         if (loadIfNeeded()) {
348                 Buffer * tmp = bufferlist.getBuffer(getFileName());
349
350                 if (tmp->params.textclass != master->params.textclass) {
351                         lyxerr << "ERROR: Cannot handle include file `"
352                                << MakeDisplayPath(getFileName())
353                                << "' which has textclass `"
354                                << textclasslist.NameOfClass(tmp->params.textclass)
355                                << "' instead of `"
356                                << textclasslist.NameOfClass(master->params.textclass)
357                                << "'." << endl;
358                         return 0;
359                 }
360                 
361                 // write it to a file (so far the complete file)
362                 string writefile = ChangeExtension(getFileName(), ".tex");
363                 if (!master->tmppath.empty()
364                     && !master->niceFile) {
365                         incfile = subst(incfile, '/','@');
366 #ifdef __EMX__
367                         incfile = subst(incfile, ':', '$');
368 #endif
369                         writefile = AddName(master->tmppath, incfile);
370                 } else
371                         writefile = getFileName();
372                 writefile = ChangeExtension(writefile, ".tex");
373                 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
374                 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
375                 
376                 tmp->markDepClean(master->tmppath);
377                 
378                 tmp->makeLaTeXFile(writefile,
379                                    OnlyPath(getMasterFilename()), 
380                                    master->niceFile, true);
381         } 
382
383         if (isVerb()) {
384                 os << '\\' << command << '{' << incfile << '}';
385         } 
386         else if (isInput()) {
387                 // \input wants file with extension (default is .tex)
388                 if (!IsLyXFilename(getFileName())) {
389                         os << '\\' << command << '{' << incfile << '}';
390                 } else {
391                         os << '\\' << command << '{'
392                            << ChangeExtension(incfile, ".tex")
393                            <<  '}';
394                 }
395         } else {
396                 // \include don't want extension and demands that the
397                 // file really have .tex
398                 os << '\\' << command << '{'
399                    << ChangeExtension(incfile, string())
400                    << '}';
401         }
402
403         return 0;
404 }
405
406
407 void InsetInclude::Validate(LaTeXFeatures & features) const
408 {
409         if (isVerb())
410                 features.verbatim = true;
411
412         // Here we must do the fun stuff...
413         // Load the file in the include if it needs
414         // to be loaded:
415         if (loadIfNeeded()) {
416                 // a file got loaded
417                 Buffer * tmp = bufferlist.getBuffer(getFileName());
418                 tmp->validate(features);
419         }
420 }
421
422
423 vector<string> InsetInclude::getLabelList() const
424 {
425     vector<string> l;
426     string parentname;
427
428     if (loadIfNeeded()) {
429         Buffer * tmp = bufferlist.getBuffer(getFileName());
430         tmp->setParentName(""); 
431         l = tmp->getLabelList();
432         tmp->setParentName(getMasterFilename());
433     }
434
435     return l;
436 }
437
438
439 string InsetInclude::getKeys(char delim) const
440 {
441         string lst;
442         
443         if (loadIfNeeded()) {
444                 Buffer *tmp = bufferlist.getBuffer(getFileName());
445                 tmp->setParentName(""); 
446                 lst =  tmp->getBibkeyList(delim);
447                 tmp->setParentName(getMasterFilename());
448         }
449         
450         return lst;
451 }