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