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