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