]> git.lyx.org Git - lyx.git/blob - src/insets/insetinclude.C
Fix selection in insettext.
[lyx.git] / src / insets / insetinclude.C
1
2 #include <config.h>
3
4 #include <cstdlib>
5
6 #ifdef __GNUG__
7 #pragma implementation
8 #endif
9
10 #include FORMS_H_LOCATION 
11 #include "insetinclude.h"
12 #include "filedlg.h" 
13 #include "buffer.h"
14 #include "bufferlist.h"
15 #include "debug.h"
16 #include "support/filetools.h"
17 #include "lyxrc.h"
18 #include "LyXView.h"
19 #include "LaTeXFeatures.h"
20 #include "lyx_gui_misc.h" // CancelCloseBoxCB
21 #include "gettext.h"
22 #include "include_form.h"
23 #include "support/FileInfo.h"
24 #include "layout.h"
25 #include "lyxfunc.h"
26
27 using std::ostream;
28 using std::endl;
29 using std::vector;
30 using std::pair;
31
32 extern BufferView * current_view;
33
34 extern BufferList bufferlist;
35
36
37 FD_include * create_form_include(void)
38 {
39   FL_OBJECT * obj;
40   FD_include * fdui = (FD_include *) fl_calloc(1, sizeof(FD_include));
41
42   fdui->include = fl_bgn_form(FL_NO_BOX, 340, 210);
43   obj = fl_add_box(FL_UP_BOX, 0, 0, 340, 210, "");
44   obj = fl_add_frame(FL_ENGRAVED_FRAME, 10, 70, 160, 90, "");
45   fdui->browsebt = obj = fl_add_button(FL_NORMAL_BUTTON, 230, 30, 100, 30, idex(_("Browse|#B")));
46     fl_set_button_shortcut(obj, scex(_("Browse|#B")), 1);
47     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
48     fl_set_object_callback(obj, include_cb, 0);
49   fdui->flag1 = obj = fl_add_checkbutton(FL_PUSH_BUTTON, 180, 70, 150, 30, idex(_("Don't typeset|#D")));
50     fl_set_button_shortcut(obj, scex(_("Don't typeset|#D")), 1);
51     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
52   obj = fl_add_button(FL_RETURN_BUTTON, 120, 170, 100, 30, _("OK"));
53     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
54     fl_set_object_callback(obj, include_cb, 1);
55   obj = fl_add_button(FL_NORMAL_BUTTON, 230, 170, 100, 30, idex(_("Cancel|^[")));
56     fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
57     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
58     fl_set_object_callback(obj, include_cb, 2);
59   obj = fl_add_button(FL_NORMAL_BUTTON, 230, 130, 100, 30, idex(_("Load|#L")));
60     fl_set_button_shortcut(obj, scex(_("Load|#L")), 1);
61     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
62     fl_set_object_callback(obj, include_cb, 5);
63   fdui->input = obj = fl_add_input(FL_NORMAL_INPUT, 10, 30, 210, 30, idex(_("File name:|#F")));
64     fl_set_input_shortcut(obj, scex(_("File name:|#F")), 1);
65     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
66     fl_set_object_lalign(obj, FL_ALIGN_TOP_LEFT);
67   fdui->flag41 = obj = fl_add_checkbutton(FL_PUSH_BUTTON, 180, 100, 150, 30, idex(_("Visible space|#s")));
68     fl_set_button_shortcut(obj, scex(_("Visible space|#s")), 1);
69     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
70
71   fdui->include_grp = fl_bgn_group();
72   fdui->flag4 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 130, 160, 30, idex(_("Verbatim|#V")));
73     fl_set_button_shortcut(obj, scex(_("Verbatim|#V")), 1);
74     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
75     fl_set_object_callback(obj, include_cb, 10);
76   fdui->flag2 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 100, 160, 30, idex(_("Use input|#i")));
77     fl_set_button_shortcut(obj, scex(_("Use input|#i")), 1);
78     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
79     fl_set_object_callback(obj, include_cb, 11);
80   fdui->flag3 = obj = fl_add_checkbutton(FL_RADIO_BUTTON, 10, 70, 160, 30, idex(_("Use include|#U")));
81     fl_set_button_shortcut(obj, scex(_("Use include|#U")), 1);
82     fl_set_object_lsize(obj, FL_NORMAL_SIZE);
83     fl_set_object_callback(obj, include_cb, 11);
84   fl_end_group();
85
86   fl_end_form();
87
88   //fdui->include->fdui = fdui;
89
90   return fdui;
91 }
92 /*---------------------------------------*/
93
94
95 FD_include * form = 0;
96
97 extern "C"
98 void include_cb(FL_OBJECT *, long arg)
99 {
100     
101         InsetInclude * inset = static_cast<InsetInclude*>(form->include->u_vdata);
102         switch (arg) {
103         case 0:
104         {
105                 // Should browsing too be disabled in RO-mode?
106                 LyXFileDlg fileDlg;
107                 string const mpath = OnlyPath(inset->getMasterFilename());
108                 string ext;
109     
110                 if (fl_get_button(form->flag2)) // Use Input Button
111                         ext = "*.tex";
112                 else if (fl_get_button(form->flag4)) // Verbatim all files
113                         ext = "*";
114                 else
115                         ext = "*.lyx";
116                 // launches dialog
117                 fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
118     
119                 // Use by default the master's path
120                 string const filename =
121                         fileDlg.Select(_("Select Child Document"),
122                                        mpath, ext, 
123                                        inset->getContents());
124                 XFlush(fl_get_display());
125  
126                 // check selected filename
127                 if (!filename.empty()) {
128                         string const filename2 = MakeRelPath(filename,
129                                                              mpath);
130                         if (prefixIs(filename2, ".."))
131                                 fl_set_input(form->input,
132                                              filename.c_str());
133                         else
134                                 fl_set_input(form->input,
135                                              filename2.c_str());
136                 }
137                 break;
138         }
139
140         case 1:
141                 if (!current_view->buffer()->isReadonly()) {
142                         inset->setContents(fl_get_input(form->input));
143                         // don't typeset
144                         inset->setNoLoad(fl_get_button(form->flag1));
145                         if (fl_get_button(form->flag2))
146                                 inset->setInput();
147                         else if (fl_get_button(form->flag3))
148                                 inset->setInclude();
149                         else if (fl_get_button(form->flag4)) {
150                                 inset->setVerb();
151                                 inset->setVisibleSpace(fl_get_button(form->flag41));
152                         }
153                         
154                         fl_hide_form(form->include);
155                         current_view->updateInset(inset, true);
156                         break;
157                 } // fall through
158                 
159         case 2:
160                 fl_hide_form(form->include);
161                 break;
162         case 5:
163                 if (!current_view->buffer()->isReadonly()) {
164                         inset->setContents(fl_get_input(form->input));
165                         inset->setNoLoad(fl_get_button(form->flag1));
166                         if (fl_get_button(form->flag2))
167                                 inset->setInput();
168                         else if (fl_get_button(form->flag3))
169                                 inset->setInclude();
170                         else if (fl_get_button(form->flag4)) {
171                                 inset->setVerb();
172                                 inset->setVisibleSpace(fl_get_button(form->flag41));
173                         }
174                         
175                         fl_hide_form(form->include);
176                         current_view->updateInset(inset, true);
177                         current_view->owner()->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, inset->getContents());
178                 }
179                 break;
180                 
181         case 10:
182                 fl_activate_object(form->flag41);
183                 fl_set_object_lcol(form->flag41, FL_BLACK); 
184                 break;
185         case 11:
186                 fl_deactivate_object(form->flag41);
187                 fl_set_object_lcol(form->flag41, FL_INACTIVE);
188                 fl_set_button(form->flag41, 0);
189                 break;
190         }
191 }
192
193
194 static inline
195 string unique_id() {
196         static unsigned int seed = 1000;
197
198         std::ostringstream ost;
199         ost << "file" << ++seed;
200
201         // Needed if we use lyxstring.
202         return ost.str().c_str();
203 }
204
205
206 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & bf)
207         : InsetCommand(p), master(&bf)
208 {
209         flag = InsetInclude::INCLUDE;
210         noload = false;
211         include_label = unique_id();
212 }
213
214
215 InsetInclude::~InsetInclude()
216 {
217         if (form && form->include->u_vdata == this) {
218                 // this inset is in the popup so hide the popup 
219                 // and remove the reference to this inset. ARRae
220                 if (form->include) {
221                         if (form->include->visible) {
222                                 fl_hide_form(form->include);
223                         }
224                         fl_free_form(form->include);
225                 }
226                 fl_free(form);
227                 form = 0;
228         }
229 }
230
231
232 Inset * InsetInclude::Clone(Buffer const & buffer) const
233
234         InsetInclude * ii = new InsetInclude (params(), buffer); 
235         ii->setNoLoad(isNoLoad());
236         // By default, the newly created inset is of `include' type,
237         // so we do not test this case.
238         if (isInput())
239                 ii->setInput();
240         else if (isVerb()) {
241                 ii->setVerb();
242                 ii->setVisibleSpace(isVerbVisibleSpace());
243         }
244         return ii;
245 }
246
247
248 void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
249 {
250         if (bv->buffer()->isReadonly())
251                 WarnReadonly(bv->buffer()->fileName());
252
253         if (!form) {
254                 form = create_form_include();
255                 fl_set_form_atclose(form->include, CancelCloseBoxCB, 0);
256         }
257         form->include->u_vdata = this;
258     
259         fl_set_input(form->input, getContents().c_str());
260         fl_set_button(form->flag1, int(isNoLoad()));
261         fl_set_button(form->flag2, int(isInput()));
262         fl_set_button(form->flag3, int(isInclude()));
263         fl_set_button(form->flag4, int(isVerb()));
264         if (isVerb()) 
265             fl_set_button(form->flag41, int(isVerbVisibleSpace()));
266         else {
267             fl_set_button(form->flag41, 0);
268             fl_deactivate_object(form->flag41);
269             fl_set_object_lcol(form->flag41, FL_INACTIVE);
270         }
271         
272         if (form->include->visible) {
273                 fl_raise_form(form->include);
274         } else {
275                 fl_show_form(form->include,
276                              FL_PLACE_MOUSE | FL_FREE_SIZE, FL_TRANSIENT,
277                              _("Include"));
278         }
279 }
280
281
282 void InsetInclude::Write(Buffer const *, ostream & os) const
283 {
284         os << "Include " << getCommand() << "\n";
285 }
286
287
288 void InsetInclude::Read(Buffer const * buf, LyXLex & lex)
289 {
290         InsetCommand::Read(buf, lex);
291     
292         if (getCmdName() == "include")
293                 setInclude();
294         else if (getCmdName() == "input")
295                 setInput();
296         else if (contains(getCmdName(), "verbatim")) {
297                 setVerb();
298                 if (getCmdName() == "verbatiminput*")
299                         setVisibleSpace(true);
300         }
301 }
302
303
304 bool InsetInclude::display() const 
305 {
306         return !isInput();
307 }
308
309
310 string const InsetInclude::getScreenLabel() const
311 {
312         string temp;
313         if (isInput())
314                 temp += _("Input");
315         else if (isVerb()) {
316                 temp += _("Verbatim Input");
317                 if (isVerbVisibleSpace()) temp += '*';
318         } else temp += _("Include");
319         temp += ": ";
320         
321         if (getContents().empty()) {
322                 temp+= "???";
323         } else {
324                 temp+= getContents();
325         }
326         return temp;
327 }
328
329
330 string const InsetInclude::getFileName() const
331 {
332         return MakeAbsPath(getContents(), 
333                            OnlyPath(getMasterFilename()));
334 }
335
336
337 string const InsetInclude::getMasterFilename() const
338 {
339         return master->fileName();
340 }
341
342
343 bool InsetInclude::loadIfNeeded() const
344 {
345         if (isNoLoad() || isVerb()) return false;
346         if (!IsLyXFilename(getFileName())) return false;
347         
348         if (bufferlist.exists(getFileName())) return true;
349         
350         // the readonly flag can/will be wrong, not anymore I think.
351         FileInfo finfo(getFileName());
352         bool const ro = !finfo.writable();
353         return bufferlist.readFile(getFileName(), ro) != 0;
354 }
355
356
357 int InsetInclude::Latex(Buffer const * buffer, ostream & os,
358                         bool /*fragile*/, bool /*fs*/) const
359 {
360         string incfile(getContents());
361         
362         // Do nothing if no file name has been specified
363         if (incfile.empty())
364                 return 0;
365     
366         if (loadIfNeeded()) {
367                 Buffer * tmp = bufferlist.getBuffer(getFileName());
368
369                 if (tmp->params.textclass != buffer->params.textclass) {
370                         lyxerr << "ERROR: Cannot handle include file `"
371                                << MakeDisplayPath(getFileName())
372                                << "' which has textclass `"
373                                << textclasslist.NameOfClass(tmp->params.textclass)
374                                << "' instead of `"
375                                << textclasslist.NameOfClass(buffer->params.textclass)
376                                << "'." << endl;
377                         return 0;
378                 }
379                 
380                 // write it to a file (so far the complete file)
381                 string writefile = ChangeExtension(getFileName(), ".tex");
382                 if (!buffer->tmppath.empty()
383                     && !buffer->niceFile) {
384                         incfile = subst(incfile, '/','@');
385 #ifdef __EMX__
386                         incfile = subst(incfile, ':', '$');
387 #endif
388                         writefile = AddName(buffer->tmppath, incfile);
389                 } else
390                         writefile = getFileName();
391                 writefile = ChangeExtension(writefile, ".tex");
392                 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
393                 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
394                 
395                 tmp->markDepClean(buffer->tmppath);
396                 
397                 tmp->makeLaTeXFile(writefile,
398                                    OnlyPath(getMasterFilename()), 
399                                    buffer->niceFile, true);
400         } 
401
402         if (isVerb()) {
403                 os << '\\' << getCmdName() << '{' << incfile << '}';
404         } else if (isInput()) {
405                 // \input wants file with extension (default is .tex)
406                 if (!IsLyXFilename(getFileName())) {
407                         os << '\\' << getCmdName() << '{' << incfile << '}';
408                 } else {
409                         os << '\\' << getCmdName() << '{'
410                            << ChangeExtension(incfile, ".tex")
411                            <<  '}';
412                 }
413         } else {
414                 // \include don't want extension and demands that the
415                 // file really have .tex
416                 os << '\\' << getCmdName() << '{'
417                    << ChangeExtension(incfile, string())
418                    << '}';
419         }
420
421         return 0;
422 }
423
424
425 int InsetInclude::Ascii(Buffer const *, std::ostream & os, int) const
426 {
427         if (isVerb())
428                 os << GetFileContents(getFileName());
429         return 0;
430 }
431
432
433 int InsetInclude::Linuxdoc(Buffer const * buffer, ostream & os) const
434 {
435         string incfile(getContents());
436         
437         // Do nothing if no file name has been specified
438         if (incfile.empty())
439                 return 0;
440     
441         if (loadIfNeeded()) {
442                 Buffer * tmp = bufferlist.getBuffer(getFileName());
443
444                 // write it to a file (so far the complete file)
445                 string writefile = ChangeExtension(getFileName(), ".sgml");
446                 if (!buffer->tmppath.empty() && !buffer->niceFile) {
447                         incfile = subst(incfile, '/','@');
448                         writefile = AddName(buffer->tmppath, incfile);
449                 } else
450                         writefile = getFileName();
451
452                 if (IsLyXFilename(getFileName()))
453                         writefile = ChangeExtension(writefile, ".sgml");
454
455                 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
456                 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
457                 
458                 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
459         } 
460
461         if (isVerb()) {
462                 os << "<inlinegraphic fileref=\"" << '&' << include_label << ';'
463                    << "\" format=\"linespecific\">"
464                    << "</inlinegraphic>";
465         } else 
466                 os << '&' << include_label << ';';
467         
468         return 0;
469 }
470
471
472 int InsetInclude::DocBook(Buffer const * buffer, ostream & os) const
473 {
474         string incfile(getContents());
475
476         // Do nothing if no file name has been specified
477         if (incfile.empty())
478                 return 0;
479     
480         if (loadIfNeeded()) {
481                 Buffer * tmp = bufferlist.getBuffer(getFileName());
482
483                 // write it to a file (so far the complete file)
484                 string writefile = ChangeExtension(getFileName(), ".sgml");
485                 if (!buffer->tmppath.empty() && !buffer->niceFile) {
486                         incfile = subst(incfile, '/','@');
487                         writefile = AddName(buffer->tmppath, incfile);
488                 } else
489                         writefile = getFileName();
490                 if (IsLyXFilename(getFileName()))
491                         writefile = ChangeExtension(writefile, ".sgml");
492
493                 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
494                 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
495                 
496                 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
497         } 
498
499         if (isVerb()) {
500                 os << "<inlinegraphic fileref=\"" << '&' << include_label << ';'
501                    << "\" format=\"linespecific\">"
502                    << "</inlinegraphic>";
503         } else 
504                 os << '&' << include_label << ';';
505         
506         return 0;
507 }
508
509
510 void InsetInclude::Validate(LaTeXFeatures & features) const
511 {
512
513         string incfile(getContents());
514         string writefile; // = ChangeExtension(getFileName(), ".sgml");
515
516         if (!master->tmppath.empty() && !master->niceFile) {
517                 incfile = subst(incfile, '/','@');
518                 writefile = AddName(master->tmppath, incfile);
519         } else
520                 writefile = getFileName();
521                 // Use the relative path.
522                 //writefile = incfile;
523
524         if (IsLyXFilename(getFileName()))
525                 writefile = ChangeExtension(writefile, ".sgml");
526
527         features.IncludedFiles[include_label] = writefile;
528
529         if (isVerb())
530                 features.verbatim = true;
531
532         // Here we must do the fun stuff...
533         // Load the file in the include if it needs
534         // to be loaded:
535         if (loadIfNeeded()) {
536                 // a file got loaded
537                 Buffer * tmp = bufferlist.getBuffer(getFileName());
538                 tmp->validate(features);
539         }
540 }
541
542
543 vector<string> const InsetInclude::getLabelList() const
544 {
545         vector<string> l;
546
547         if (loadIfNeeded()) {
548                 Buffer * tmp = bufferlist.getBuffer(getFileName());
549                 tmp->setParentName(""); 
550                 l = tmp->getLabelList();
551                 tmp->setParentName(getMasterFilename());
552         }
553
554         return l;
555 }
556
557
558 vector<pair<string,string> > const InsetInclude::getKeys() const
559 {
560         vector<pair<string,string> > keys;
561         
562         if (loadIfNeeded()) {
563                 Buffer * tmp = bufferlist.getBuffer(getFileName());
564                 tmp->setParentName(""); 
565                 keys = tmp->getBibkeyList();
566                 tmp->setParentName(getMasterFilename());
567         }
568         
569         return keys;
570 }