]> git.lyx.org Git - lyx.git/blob - src/insets/insetinclude.C
eed1888a8f6edab0e299fd209a73c1da7ee40c42
[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::endl;
27
28 extern BufferView * current_view;
29
30 extern BufferList bufferlist;
31
32
33 FD_include * create_form_include(void)
34 {
35   FL_OBJECT * obj;
36   FD_include * fdui = (FD_include *) fl_calloc(1, sizeof(FD_include));
37
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);
66
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);
80   fl_end_group();
81
82   fl_end_form();
83
84   //fdui->include->fdui = fdui;
85
86   return fdui;
87 }
88 /*---------------------------------------*/
89
90
91 FD_include * form = 0;
92
93 extern "C" void include_cb(FL_OBJECT *, long arg)
94 {
95     
96         InsetInclude * inset = static_cast<InsetInclude*>(form->include->u_vdata);
97         switch (arg) {
98         case 0:
99         {
100                 // Should browsing too be disabled in RO-mode?
101                 LyXFileDlg fileDlg;
102                 string mpath = OnlyPath(inset->getMasterFilename());
103                 string ext;
104     
105                 if (fl_get_button(form->flag2)) // Use Input Button
106                         ext = "*.tex";
107                 else if (fl_get_button(form->flag4)) // Verbatim all files
108                         ext = "*";
109                 else
110                         ext = "*.lyx";
111                 // launches dialog
112                 fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
113     
114                 // Use by default the master's path
115                 string filename = fileDlg.Select(_("Select Child Document"),
116                                                   mpath, ext, 
117                                                   inset->getContents());
118                 XFlush(fl_get_display());
119  
120                 // check selected filename
121                 if (!filename.empty()) {
122                         string filename2 = MakeRelPath(filename,
123                                                         mpath);
124                         if (prefixIs(filename2, ".."))
125                                 fl_set_input(form->input,
126                                              filename.c_str());
127                         else
128                                 fl_set_input(form->input,
129                                              filename2.c_str());
130                 }
131                 break;
132         }
133
134         case 1:
135                 if(!current_view->buffer()->isReadonly()) {
136                         inset->setContents(fl_get_input(form->input));
137                         // don't typeset
138                         inset->setNoLoad(fl_get_button(form->flag1));
139                         if (fl_get_button(form->flag2))
140                                 inset->setInput();
141                         else if (fl_get_button(form->flag3))
142                                 inset->setInclude();
143                         else if (fl_get_button(form->flag4)) {
144                                 inset->setVerb();
145                                 inset->setVisibleSpace(fl_get_button(form->flag41));
146                         }
147                         
148                         fl_hide_form(form->include);
149                         current_view->updateInset(inset, true);
150                         break;
151                 } // fall through
152                 
153         case 2:
154                 fl_hide_form(form->include);
155                 break;
156         case 5:
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))
161                                 inset->setInput();
162                         else if (fl_get_button(form->flag3))
163                                 inset->setInclude();
164                         else if (fl_get_button(form->flag4)) {
165                                 inset->setVerb();
166                                 inset->setVisibleSpace(fl_get_button(form->flag41));
167                         }
168                         
169                         fl_hide_form(form->include);
170                         current_view->updateInset(inset, true);
171                         current_view->owner()->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, inset->getContents().c_str());
172                 }
173                 break;
174                 
175         case 10:
176                 fl_activate_object(form->flag41);
177                 fl_set_object_lcol(form->flag41, FL_BLACK); 
178                 break;
179         case 11:
180                 fl_deactivate_object(form->flag41);
181                 fl_set_object_lcol(form->flag41, FL_INACTIVE);
182                 fl_set_button(form->flag41, 0);
183                 break;
184         }
185 }
186
187
188 InsetInclude::InsetInclude(string const & fname, Buffer * bf)
189         : InsetCommand("include") 
190 {
191         master = bf;
192         setContents(fname);
193         flag = InsetInclude::INCLUDE;
194         noload = false;
195 }
196
197
198 InsetInclude::~InsetInclude()
199 {
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
203                 if (form->include) {
204                         if (form->include->visible) {
205                                 fl_hide_form(form->include);
206                         }
207                         fl_free_form(form->include);
208                 }
209                 fl_free(form);
210                 form = 0;
211         }
212 }
213
214
215 Inset * InsetInclude::Clone() const
216
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.
221         if (isInput())
222                 ii->setInput();
223         else if (isVerb()) {
224                 ii->setVerb();
225                 ii->setVisibleSpace(isVerbVisibleSpace());
226         }
227         return ii;
228 }
229
230
231 void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
232 {
233         if(bv->buffer()->isReadonly())
234                 WarnReadonly(bv->buffer()->fileName());
235
236         if (!form) {
237                 form = create_form_include();
238                 fl_set_form_atclose(form->include, IgnoreCloseBoxCB, 0);
239         }
240         form->include->u_vdata = this;
241     
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()));
247         if (isVerb()) 
248             fl_set_button(form->flag41, int(isVerbVisibleSpace()));
249         else {
250             fl_set_button(form->flag41, 0);
251             fl_deactivate_object(form->flag41);
252             fl_set_object_lcol(form->flag41, FL_INACTIVE);
253         }
254         
255         if (form->include->visible) {
256                 fl_raise_form(form->include);
257         } else {
258                 fl_show_form(form->include, FL_PLACE_MOUSE, FL_FULLBORDER,
259                              _("Include"));
260         }
261 }
262
263
264 void InsetInclude::Write(ostream & os) const
265 {
266         os << "Include " << getCommand() << "\n";
267 }
268
269
270 void InsetInclude::Read(LyXLex & lex)
271 {
272         InsetCommand::Read(lex);
273     
274         if (getCmdName() == "include")
275                 setInclude();
276         else if (getCmdName() == "input")
277                 setInput();
278         else if (contains(getCmdName(), "verbatim")) {
279                 setVerb();
280                 if (getCmdName() == "verbatiminput*")
281                         setVisibleSpace(true);
282         }
283 }
284
285
286 string InsetInclude::getScreenLabel() const
287 {
288         string temp;
289         if (isInput())
290                 temp += _("Input");
291         else if (isVerb()) {
292                 temp += _("Verbatim Input");
293                 if (isVerbVisibleSpace()) temp += '*';
294         } else temp += _("Include");
295         temp += ": ";
296         
297         if (contents.empty()) {
298                 temp+= "???";
299         } else {
300                 temp+= contents;
301         }
302         return temp;
303 }
304
305
306 bool InsetInclude::loadIfNeeded() const
307 {
308         if (isNoLoad() || isVerb()) return false;
309         if (!IsLyXFilename(getFileName())) return false;
310         
311         if (bufferlist.exists(getFileName())) return true;
312         
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 );
317 }
318
319
320 int InsetInclude::Latex(ostream & os,
321                         signed char /*fragile*/, bool /*fs*/) const
322 {
323         // Do nothing if no file name has been specified
324         if (contents.empty())
325                 return 0;
326     
327         // Use += to force a copy of contents (JMarc)
328         // How does that force anything? (Lgb)
329         string incfile(contents);
330
331         if (loadIfNeeded()) {
332                 Buffer * tmp = bufferlist.getBuffer(getFileName());
333
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)
339                                << "' instead of `"
340                                << textclasslist.NameOfClass(master->params.textclass)
341                                << "'." << endl;
342                         return 0;
343                 }
344                 
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, '/','@');
350 #ifdef __EMX__
351                         incfile = subst(incfile, ':', '$');
352 #endif
353                         writefile = AddName(master->tmppath, incfile);
354                 } else
355                         writefile = getFileName();
356                 writefile = ChangeExtension(writefile, ".tex", false);
357                 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
358                 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
359                 
360                 tmp->markDepClean(master->tmppath);
361                 
362                 tmp->makeLaTeXFile(writefile,
363                                    OnlyPath(getMasterFilename()), 
364                                    master->niceFile, true);
365         } 
366
367         if (isVerb()) {
368                 os << '\\' << command << '{' << incfile << '}';
369         } 
370         else if (isInput()) {
371                 // \input wants file with extension (default is .tex)
372                 if (!IsLyXFilename(getFileName())) {
373                         os << '\\' << command << '{' << incfile << '}';
374                 } else {
375                         os << '\\' << command << '{'
376                            << ChangeExtension(incfile, ".tex", false)
377                            <<  '}';
378                 }
379         } else {
380                 // \include don't want extension and demands that the
381                 // file really have .tex
382                 os << '\\' << command << '{'
383                    << ChangeExtension(incfile, string(), false)
384                    << '}';
385         }
386
387         return 0;
388 }
389
390
391 void InsetInclude::Validate(LaTeXFeatures & features) const
392 {
393         if (isVerb())
394                 features.verbatim = true;
395
396         // Here we must do the fun stuff...
397         // Load the file in the include if it needs
398         // to be loaded:
399         if (loadIfNeeded()) {
400                 // a file got loaded
401                 Buffer * tmp = bufferlist.getBuffer(getFileName());
402                 tmp->validate(features);
403         }
404 }
405
406
407 string InsetInclude::getLabel(int) const
408 {
409     string label;
410     string parentname;
411         
412         
413     if (loadIfNeeded()) {
414         Buffer * tmp = bufferlist.getBuffer(getFileName());
415         tmp->setParentName(""); 
416         label =  tmp->getReferenceList('\n');
417         tmp->setParentName(getMasterFilename());
418     }
419
420     return label;
421 }
422
423
424 int InsetInclude::GetNumberOfLabels() const
425 {
426     string label;
427
428     if (loadIfNeeded()) {
429         Buffer * tmp = bufferlist.getBuffer(getFileName());
430         tmp->setParentName("");    
431         label = tmp->getReferenceList('\n');
432         tmp->setParentName(getMasterFilename());
433     }
434     int nl = (label.empty())? 0: 1;
435         
436     return nl;
437 }
438
439
440 string InsetInclude::getKeys(char delim) const
441 {
442         string lst;
443         
444         if (loadIfNeeded()) {
445                 Buffer *tmp = bufferlist.getBuffer(getFileName());
446                 tmp->setParentName(""); 
447                 lst =  tmp->getBibkeyList(delim);
448                 tmp->setParentName(getMasterFilename());
449         }
450         
451         return lst;
452 }