]> git.lyx.org Git - lyx.git/blob - src/insets/insetinclude.C
the fstream/iostream changes and some small other things
[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
214 Inset * InsetInclude::Clone() const
215
216         InsetInclude * ii = new InsetInclude (contents, master); 
217         ii->setNoLoad(isNoLoad());
218         // By default, the newly created inset is of `include' type,
219         // so we do not test this case.
220         if (isInput())
221                 ii->setInput();
222         else if (isVerb()) {
223                 ii->setVerb();
224                 ii->setVisibleSpace(isVerbVisibleSpace());
225         }
226         return ii;
227 }
228
229
230 void InsetInclude::Edit(int, int)
231 {
232         if(current_view->buffer()->isReadonly())
233                 WarnReadonly();
234
235         if (!form) {
236                 form = create_form_include();
237                 fl_set_form_atclose(form->include, IgnoreCloseBoxCB, 0);
238         }
239         form->include->u_vdata = this;
240     
241         fl_set_input(form->input, contents.c_str());
242         fl_set_button(form->flag1, int(isNoLoad()));
243         fl_set_button(form->flag2, int(isInput()));
244         fl_set_button(form->flag3, int(isInclude()));
245         fl_set_button(form->flag4, int(isVerb()));
246         if (isVerb()) 
247             fl_set_button(form->flag41, int(isVerbVisibleSpace()));
248         else {
249             fl_set_button(form->flag41, 0);
250             fl_deactivate_object(form->flag41);
251             fl_set_object_lcol(form->flag41, FL_INACTIVE);
252         }
253         
254         if (form->include->visible) {
255                 fl_raise_form(form->include);
256         } else {
257                 fl_show_form(form->include, FL_PLACE_MOUSE, FL_FULLBORDER,
258                              _("Include"));
259         }
260 }
261
262
263 void InsetInclude::Write(ostream & os)
264 {
265         os << "Include " << getCommand() << "\n";
266 }
267
268
269 void InsetInclude::Read(LyXLex & lex)
270 {
271         InsetCommand::Read(lex);
272     
273         if (getCmdName() == "include")
274                 setInclude();
275         else if (getCmdName() == "input")
276                 setInput();
277         else if (contains(getCmdName(), "verbatim")) {
278                 setVerb();
279                 if (getCmdName() == "verbatiminput*")
280                         setVisibleSpace(true);
281         }
282 }
283
284
285 string InsetInclude::getScreenLabel() const
286 {
287         string temp;
288         if (isInput())
289                 temp += _("Input");
290         else if (isVerb()) {
291                 temp += _("Verbatim Input");
292                 if (isVerbVisibleSpace()) temp += '*';
293         } else temp += _("Include");
294         temp += ": ";
295         
296         if (contents.empty()) {
297                 temp+= "???";
298         } else {
299                 temp+= contents;
300         }
301         return temp;
302 }
303
304
305 bool InsetInclude::loadIfNeeded() const
306 {
307         if (isNoLoad() || isVerb()) return false;
308         if (!IsLyXFilename(getFileName())) return false;
309         
310         if (bufferlist.exists(getFileName())) return true;
311         
312         // the readonly flag can/will be wrong, not anymore I think.
313         FileInfo finfo(getFileName());
314         bool ro = !finfo.writable();
315         return ( bufferlist.readFile(getFileName(), ro) != 0 );
316 }
317
318
319 int InsetInclude::Latex(ostream & os, signed char /*fragile*/)
320 {
321         string include_file;
322         signed char dummy = 0;
323         Latex(include_file, dummy);
324         os << include_file;
325         return 0;
326 }
327
328
329 int InsetInclude::Latex(string & file, signed char /*fragile*/)
330 {
331         string writefile, incfile;
332
333         // Do nothing if no file name has been specified
334         if (contents.empty())
335                 return 0;
336     
337         // Use += to force a copy of contents (JMarc)
338         incfile += contents;
339
340         if (loadIfNeeded()) {
341                 Buffer * tmp = bufferlist.getBuffer(getFileName());
342
343                 if (tmp->params.textclass != master->params.textclass) {
344                         lyxerr << "ERROR: Cannot handle include file `"
345                                << MakeDisplayPath(getFileName())
346                                << "' which has textclass `"
347                                << textclasslist.NameOfClass(tmp->params.textclass)
348                                << "' instead of `"
349                                << textclasslist.NameOfClass(master->params.textclass)
350                                << "'." << endl;
351                         return 0;
352                 }
353                 
354                 // write it to a file (so far the complete file)
355                 writefile = ChangeExtension(getFileName(), ".tex", false);
356                 if (!master->tmppath.empty()
357                     && !master->niceFile) {
358                         incfile = subst(incfile, '/','@');
359 #ifdef __EMX__
360                         incfile = subst(incfile, ':', '$');
361 #endif
362                         writefile = AddName(master->tmppath, incfile);
363                 } else
364                         writefile = getFileName();
365                 writefile = ChangeExtension(writefile, ".tex", false);
366                 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
367                 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
368                 
369                 tmp->markDepClean(master->tmppath);
370                 
371                 tmp->makeLaTeXFile(writefile,
372                                    OnlyPath(getMasterFilename()), 
373                                    master->niceFile, true);
374         } 
375
376         if (isVerb()) {
377                 file += '\\';
378                 file += command + '{';
379                 file += incfile + '}';
380         } 
381         else if (isInput()) {
382                 // \input wants file with extension (default is .tex)
383                 if (!IsLyXFilename(getFileName())) {
384                         file += '\\';
385                         file += command + '{';
386                         file += incfile + '}';
387                 } else {
388                         file += '\\';
389                         file += command + '{';
390                         file += ChangeExtension(incfile, ".tex", false)
391                                 + '}';
392                 }
393         } else {
394                 // \include don't want extension and demands that the
395                 // file really have .tex
396                 file += '\\';
397                 file += command + '{';
398                 file +=         ChangeExtension(incfile, string(), false)
399                         + '}';
400         }
401
402         return 0;
403 }
404
405
406 void InsetInclude::Validate(LaTeXFeatures & features) const
407 {
408         if (isVerb())
409                 features.verbatim = true;
410
411         // Here we must do the fun stuff...
412         // Load the file in the include if it needs
413         // to be loaded:
414         if (loadIfNeeded()) {
415                 // a file got loaded
416                 Buffer * tmp = bufferlist.getBuffer(getFileName());
417                 tmp->validate(features);
418         }
419 }
420
421
422 string InsetInclude::getLabel(int) const
423 {
424     string label;
425     string parentname;
426         
427         
428     if (loadIfNeeded()) {
429         Buffer * tmp = bufferlist.getBuffer(getFileName());
430         tmp->setParentName(""); 
431         label =  tmp->getReferenceList('\n');
432         tmp->setParentName(getMasterFilename());
433     }
434
435     return label;
436 }
437
438
439 int InsetInclude::GetNumberOfLabels() const
440 {
441     string label;
442
443     if (loadIfNeeded()) {
444         Buffer * tmp = bufferlist.getBuffer(getFileName());
445         tmp->setParentName("");    
446         label = tmp->getReferenceList('\n');
447         tmp->setParentName(getMasterFilename());
448     }
449     int nl = (label.empty())? 0: 1;
450         
451     return nl;
452 }
453
454
455 string InsetInclude::getKeys() const
456 {
457         string lst;
458         
459         if (loadIfNeeded()) {
460                 Buffer *tmp = bufferlist.getBuffer(getFileName());
461                 tmp->setParentName(""); 
462                 lst =  tmp->getBibkeyList(',');
463                 tmp->setParentName(getMasterFilename());
464         }
465         
466         return lst;
467 }