]> git.lyx.org Git - features.git/blob - src/insets/InsetInclude.cpp
Implement basic context menu for include insets.
[features.git] / src / insets / InsetInclude.cpp
1 /**
2  * \file InsetInclude.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Richard Heck (conversion to InsetCommand)
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetInclude.h"
15
16 #include "LayoutFile.h"
17 #include "Buffer.h"
18 #include "buffer_funcs.h"
19 #include "BufferList.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
22 #include "Cursor.h"
23 #include "DispatchResult.h"
24 #include "ErrorList.h"
25 #include "Exporter.h"
26 #include "FuncRequest.h"
27 #include "FuncStatus.h"
28 #include "LaTeXFeatures.h"
29 #include "LyX.h"
30 #include "LyXRC.h"
31 #include "Lexer.h"
32 #include "MetricsInfo.h"
33 #include "OutputParams.h"
34 #include "TextClass.h"
35 #include "TocBackend.h"
36
37 #include "frontends/alert.h"
38 #include "frontends/Painter.h"
39
40 #include "graphics/PreviewImage.h"
41 #include "graphics/PreviewLoader.h"
42
43 #include "insets/InsetLabel.h"
44 #include "insets/InsetListingsParams.h"
45 #include "insets/RenderPreview.h"
46
47 #include "support/assert.h"
48 #include "support/debug.h"
49 #include "support/docstream.h"
50 #include "support/FileNameList.h"
51 #include "support/filetools.h"
52 #include "support/gettext.h"
53 #include "support/lstrings.h" // contains
54 #include "support/lyxalgo.h"
55 #include "support/convert.h"
56
57 #include <boost/bind.hpp>
58
59 using namespace std;
60 using namespace lyx::support;
61
62 namespace lyx {
63
64 namespace Alert = frontend::Alert;
65
66
67 namespace {
68
69 docstring const uniqueID()
70 {
71         static unsigned int seed = 1000;
72         return "file" + convert<docstring>(++seed);
73 }
74
75
76 /// the type of inclusion
77 enum Types {
78         INCLUDE, VERB, INPUT, VERBAST, LISTINGS, NONE
79 };
80
81
82 Types type(string const & s)
83 {
84         if (s == "input")
85                 return INPUT;
86         if (s == "verbatiminput")
87                 return VERB;
88         if (s == "verbatiminput*")
89                 return VERBAST;
90         if (s == "lstinputlisting")
91                 return LISTINGS;
92         if (s == "include")
93                 return INCLUDE;
94         return NONE;
95 }
96
97
98 Types type(InsetCommandParams const & params)
99 {
100         return type(params.getCmdName());
101 }
102
103
104 bool isListings(InsetCommandParams const & params)
105 {
106         return type(params) == LISTINGS;
107 }
108
109
110 bool isVerbatim(InsetCommandParams const & params)
111 {
112         Types const t = type(params);
113         return t == VERB || t == VERBAST;
114 }
115
116
117 bool isInputOrInclude(InsetCommandParams const & params)
118 {
119         Types const t = type(params);
120         return t == INPUT || t == INCLUDE;
121 }
122
123
124 FileName const masterFileName(Buffer const & buffer)
125 {
126         return buffer.masterBuffer()->fileName();
127 }
128
129
130 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
131
132
133 string const parentFilename(Buffer const & buffer)
134 {
135         return buffer.absFileName();
136 }
137
138
139 FileName const includedFilename(Buffer const & buffer,
140                               InsetCommandParams const & params)
141 {
142         return makeAbsPath(to_utf8(params["filename"]),
143                         onlyPath(parentFilename(buffer)));
144 }
145
146
147 InsetLabel * createLabel(docstring const & label_str)
148 {
149         if (label_str.empty())
150                 return 0;
151         InsetCommandParams icp(LABEL_CODE);
152         icp["name"] = label_str;
153         return new InsetLabel(icp);
154 }
155
156 } // namespace anon
157
158
159 InsetInclude::InsetInclude(InsetCommandParams const & p)
160         : InsetCommand(p, "include"), include_label(uniqueID()),
161           preview_(new RenderMonitoredPreview(this)), set_label_(false), label_(0)
162 {
163         preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
164
165         if (isListings(params())) {
166                 InsetListingsParams listing_params(to_utf8(p["lstparams"]));
167                 label_ = createLabel(from_utf8(listing_params.getParamValue("label")));
168         }
169 }
170
171
172 InsetInclude::InsetInclude(InsetInclude const & other)
173         : InsetCommand(other), include_label(other.include_label),
174           preview_(new RenderMonitoredPreview(this)), set_label_(false), label_(0)
175 {
176         preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
177
178         if (other.label_)
179                 label_ = new InsetLabel(*other.label_);
180 }
181
182
183 InsetInclude::~InsetInclude()
184 {
185         delete label_;
186         if (isVerbatim(params()) || isListings(params()))
187                 return;
188
189
190         string const parent_filename = buffer().absFileName();
191         FileName const included_file = makeAbsPath(to_utf8(params()["filename"]),
192                            onlyPath(parent_filename));
193
194         if (!isLyXFilename(included_file.absFilename()))
195                 return;
196
197         Buffer * child = theBufferList().getBuffer(included_file.absFilename());
198         // File not opened, nothing to close.
199         if (!child)
200                 return;
201
202         // Child document has a different parent, don't close it.
203         if (child->parent() != &buffer())
204                 return;
205
206         //close the buffer.
207         theBufferList().release(child);
208 }
209
210
211 void InsetInclude::setBuffer(Buffer & buffer)
212 {
213         InsetCommand::setBuffer(buffer);
214         if (label_)
215                 label_->setBuffer(buffer);
216 }
217
218
219 ParamInfo const & InsetInclude::findInfo(string const & /* cmdName */)
220 {
221         // FIXME
222         // This is only correct for the case of listings, but it'll do for now.
223         // In the other cases, this second parameter should just be empty.
224         static ParamInfo param_info_;
225         if (param_info_.empty()) {
226                 param_info_.add("filename", ParamInfo::LATEX_REQUIRED);
227                 param_info_.add("lstparams", ParamInfo::LATEX_OPTIONAL);
228         }
229         return param_info_;
230 }
231
232
233 bool InsetInclude::isCompatibleCommand(string const & s)
234 {
235         return type(s) != NONE;
236 }
237
238
239 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
240 {
241         LASSERT(&cur.buffer() == &buffer(), /**/);
242         switch (cmd.action) {
243
244         case LFUN_INSET_MODIFY: {
245                 InsetCommandParams p(INCLUDE_CODE);
246                 if (cmd.getArg(0) == "changetype") {
247                         InsetCommand::doDispatch(cur, cmd);
248                         p = params();
249                 } else
250                         InsetCommand::string2params("include", to_utf8(cmd.argument()), p);
251                 if (!p.getCmdName().empty()) {
252                         if (isListings(p)){
253                                 InsetListingsParams new_params(to_utf8(p["lstparams"]));
254                                 docstring const label_str = from_utf8(new_params.getParamValue("label"));
255                                 if (label_str.empty())
256                                         delete label_;
257                                 else if (label_)
258                                         label_->updateCommand(label_str);
259                                 else {
260                                         label_ = createLabel(label_str);
261                                         label_->setBuffer(buffer());
262                                         label_->initView();
263                                 }
264                         }
265                         setParams(p);
266                 } else
267                         cur.noUpdate();
268                 break;
269         }
270
271         //pass everything else up the chain
272         default:
273                 InsetCommand::doDispatch(cur, cmd);
274                 break;
275         }
276 }
277
278
279 void InsetInclude::setParams(InsetCommandParams const & p)
280 {
281         InsetCommand::setParams(p);
282         set_label_ = false;
283
284         if (preview_->monitoring())
285                 preview_->stopMonitoring();
286
287         if (type(params()) == INPUT)
288                 add_preview(*preview_, *this, buffer());
289
290         buffer().updateBibfilesCache();
291 }
292
293
294 docstring InsetInclude::screenLabel() const
295 {
296         docstring temp;
297
298         switch (type(params())) {
299                 case INPUT:
300                         temp = buffer().B_("Input");
301                         break;
302                 case VERB:
303                         temp = buffer().B_("Verbatim Input");
304                         break;
305                 case VERBAST:
306                         temp = buffer().B_("Verbatim Input*");
307                         break;
308                 case INCLUDE:
309                         temp = buffer().B_("Include");
310                         break;
311                 case LISTINGS:
312                         temp = listings_label_;
313                         break;
314                 case NONE:
315                         LASSERT(false, /**/);
316         }
317
318         temp += ": ";
319
320         if (params()["filename"].empty())
321                 temp += "???";
322         else
323                 temp += from_utf8(onlyFilename(to_utf8(params()["filename"])));
324
325         return temp;
326 }
327
328
329 /// return the child buffer if the file is a LyX doc and is loaded
330 Buffer * getChildBuffer(Buffer const & buffer, InsetCommandParams const & params)
331 {
332         if (isVerbatim(params) || isListings(params))
333                 return 0;
334
335         string const included_file = includedFilename(buffer, params).absFilename();
336         if (!isLyXFilename(included_file))
337                 return 0;
338
339         Buffer * childBuffer = loadIfNeeded(buffer, params); 
340
341         // FIXME: recursive includes
342         return (childBuffer == &buffer) ? 0 : childBuffer;
343 }
344
345
346 /// return true if the file is or got loaded.
347 Buffer * loadIfNeeded(Buffer const & parent, InsetCommandParams const & params)
348 {
349         if (isVerbatim(params) || isListings(params))
350                 return 0;
351
352         string const parent_filename = parent.absFileName();
353         FileName const included_file = makeAbsPath(to_utf8(params["filename"]),
354                            onlyPath(parent_filename));
355
356         if (!isLyXFilename(included_file.absFilename()))
357                 return 0;
358
359         Buffer * child = theBufferList().getBuffer(included_file.absFilename());
360         if (!child) {
361                 // the readonly flag can/will be wrong, not anymore I think.
362                 if (!included_file.exists())
363                         return 0;
364
365                 child = theBufferList().newBuffer(included_file.absFilename());
366                 if (!child)
367                         // Buffer creation is not possible.
368                         return 0;
369
370                 if (!child->loadLyXFile(included_file)) {
371                         //close the buffer we just opened
372                         theBufferList().release(child);
373                         return 0;
374                 }
375         
376                 if (!child->errorList("Parse").empty()) {
377                         // FIXME: Do something.
378                 }
379         }
380         child->setParent(&parent);
381         return child;
382 }
383
384
385 int InsetInclude::latex(odocstream & os, OutputParams const & runparams) const
386 {
387         string incfile = to_utf8(params()["filename"]);
388
389         // Do nothing if no file name has been specified
390         if (incfile.empty())
391                 return 0;
392
393         FileName const included_file = includedFilename(buffer(), params());
394
395         // Check we're not trying to include ourselves.
396         // FIXME RECURSIVE INCLUDE
397         // This isn't sufficient, as the inclusion could be downstream.
398         // But it'll have to do for now.
399         if (isInputOrInclude(params()) &&
400                 buffer().absFileName() == included_file.absFilename())
401         {
402                 Alert::error(_("Recursive input"),
403                                bformat(_("Attempted to include file %1$s in itself! "
404                                "Ignoring inclusion."), from_utf8(incfile)));
405                 return 0;
406         }
407
408         Buffer const * const masterBuffer = buffer().masterBuffer();
409
410         // if incfile is relative, make it relative to the master
411         // buffer directory.
412         if (!FileName(incfile).isAbsolute()) {
413                 // FIXME UNICODE
414                 incfile = to_utf8(makeRelPath(from_utf8(included_file.absFilename()),
415                                               from_utf8(masterBuffer->filePath())));
416         }
417
418         // write it to a file (so far the complete file)
419         string const exportfile = changeExtension(incfile, ".tex");
420         string const mangled =
421                 DocFileName(changeExtension(included_file.absFilename(),".tex")).
422                         mangledFilename();
423         FileName const writefile(makeAbsPath(mangled, masterBuffer->temppath()));
424
425         if (!runparams.nice)
426                 incfile = mangled;
427         else if (!isValidLaTeXFilename(incfile)) {
428                 frontend::Alert::warning(_("Invalid filename"),
429                                          _("The following filename is likely to cause trouble "
430                                            "when running the exported file through LaTeX: ") +
431                                             from_utf8(incfile));
432         }
433         LYXERR(Debug::LATEX, "incfile:" << incfile);
434         LYXERR(Debug::LATEX, "exportfile:" << exportfile);
435         LYXERR(Debug::LATEX, "writefile:" << writefile);
436
437         if (runparams.inComment || runparams.dryrun) {
438                 //Don't try to load or copy the file if we're
439                 //in a comment or doing a dryrun
440         } else if (isInputOrInclude(params()) &&
441                  isLyXFilename(included_file.absFilename())) {
442                 //if it's a LyX file and we're inputting or including,
443                 //try to load it so we can write the associated latex
444                 if (!loadIfNeeded(buffer(), params()))
445                         return false;
446
447                 Buffer * tmp = theBufferList().getBuffer(included_file.absFilename());
448
449                 if (tmp->params().baseClass() != masterBuffer->params().baseClass()) {
450                         // FIXME UNICODE
451                         docstring text = bformat(_("Included file `%1$s'\n"
452                                 "has textclass `%2$s'\n"
453                                 "while parent file has textclass `%3$s'."),
454                                 included_file.displayName(),
455                                 from_utf8(tmp->params().documentClass().name()),
456                                 from_utf8(masterBuffer->params().documentClass().name()));
457                         Alert::warning(_("Different textclasses"), text);
458                 }
459
460                 // Make sure modules used in child are all included in master
461                 //FIXME It might be worth loading the children's modules into the master
462                 //over in BufferParams rather than doing this check.
463                 vector<string> const masterModules = masterBuffer->params().getModules();
464                 vector<string> const childModules = tmp->params().getModules();
465                 vector<string>::const_iterator it = childModules.begin();
466                 vector<string>::const_iterator end = childModules.end();
467                 for (; it != end; ++it) {
468                         string const module = *it;
469                         vector<string>::const_iterator found =
470                                 find(masterModules.begin(), masterModules.end(), module);
471                         if (found == masterModules.end()) {
472                                 docstring text = bformat(_("Included file `%1$s'\n"
473                                         "uses module `%2$s'\n"
474                                         "which is not used in parent file."),
475                                         included_file.displayName(), from_utf8(module));
476                                 Alert::warning(_("Module not found"), text);
477                         }
478                 }
479
480                 tmp->markDepClean(masterBuffer->temppath());
481
482 // FIXME: handle non existing files
483 // FIXME: Second argument is irrelevant!
484 // since only_body is true, makeLaTeXFile will not look at second
485 // argument. Should we set it to string(), or should makeLaTeXFile
486 // make use of it somehow? (JMarc 20031002)
487                 // The included file might be written in a different encoding
488                 Encoding const * const oldEnc = runparams.encoding;
489                 runparams.encoding = &tmp->params().encoding();
490                 tmp->makeLaTeXFile(writefile,
491                                    masterFileName(buffer()).onlyPath().absFilename(),
492                                    runparams, false);
493                 runparams.encoding = oldEnc;
494         } else {
495                 // In this case, it's not a LyX file, so we copy the file
496                 // to the temp dir, so that .aux files etc. are not created
497                 // in the original dir. Files included by this file will be
498                 // found via input@path, see ../Buffer.cpp.
499                 unsigned long const checksum_in  = included_file.checksum();
500                 unsigned long const checksum_out = writefile.checksum();
501
502                 if (checksum_in != checksum_out) {
503                         if (!included_file.copyTo(writefile)) {
504                                 // FIXME UNICODE
505                                 LYXERR(Debug::LATEX,
506                                         to_utf8(bformat(_("Could not copy the file\n%1$s\n"
507                                                                   "into the temporary directory."),
508                                                    from_utf8(included_file.absFilename()))));
509                                 return 0;
510                         }
511                 }
512         }
513
514         string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
515                         "latex" : "pdflatex";
516         if (isVerbatim(params())) {
517                 incfile = latex_path(incfile);
518                 // FIXME UNICODE
519                 os << '\\' << from_ascii(params().getCmdName()) << '{'
520                    << from_utf8(incfile) << '}';
521         } else if (type(params()) == INPUT) {
522                 runparams.exportdata->addExternalFile(tex_format, writefile,
523                                                       exportfile);
524
525                 // \input wants file with extension (default is .tex)
526                 if (!isLyXFilename(included_file.absFilename())) {
527                         incfile = latex_path(incfile);
528                         // FIXME UNICODE
529                         os << '\\' << from_ascii(params().getCmdName())
530                            << '{' << from_utf8(incfile) << '}';
531                 } else {
532                 incfile = changeExtension(incfile, ".tex");
533                 incfile = latex_path(incfile);
534                         // FIXME UNICODE
535                         os << '\\' << from_ascii(params().getCmdName())
536                            << '{' << from_utf8(incfile) <<  '}';
537                 }
538         } else if (type(params()) == LISTINGS) {
539                 os << '\\' << from_ascii(params().getCmdName());
540                 string const opt = to_utf8(params()["lstparams"]);
541                 // opt is set in QInclude dialog and should have passed validation.
542                 InsetListingsParams params(opt);
543                 if (!params.params().empty())
544                         os << "[" << from_utf8(params.params()) << "]";
545                 os << '{'  << from_utf8(incfile) << '}';
546         } else {
547                 runparams.exportdata->addExternalFile(tex_format, writefile,
548                                                       exportfile);
549
550                 // \include don't want extension and demands that the
551                 // file really have .tex
552                 incfile = changeExtension(incfile, string());
553                 incfile = latex_path(incfile);
554                 // FIXME UNICODE
555                 os << '\\' << from_ascii(params().getCmdName()) << '{'
556                    << from_utf8(incfile) << '}';
557         }
558
559         return 0;
560 }
561
562
563 int InsetInclude::plaintext(odocstream & os, OutputParams const &) const
564 {
565         if (isVerbatim(params()) || isListings(params())) {
566                 os << '[' << screenLabel() << '\n';
567                 // FIXME: We don't know the encoding of the file, default to UTF-8.
568                 os << includedFilename(buffer(), params()).fileContents("UTF-8");
569                 os << "\n]";
570                 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
571         } else {
572                 docstring const str = '[' + screenLabel() + ']';
573                 os << str;
574                 return str.size();
575         }
576 }
577
578
579 int InsetInclude::docbook(odocstream & os, OutputParams const & runparams) const
580 {
581         string incfile = to_utf8(params()["filename"]);
582
583         // Do nothing if no file name has been specified
584         if (incfile.empty())
585                 return 0;
586
587         string const included_file = includedFilename(buffer(), params()).absFilename();
588
589         // Check we're not trying to include ourselves.
590         // FIXME RECURSIVE INCLUDE
591         // This isn't sufficient, as the inclusion could be downstream.
592         // But it'll have to do for now.
593         if (buffer().absFileName() == included_file) {
594                 Alert::error(_("Recursive input"),
595                                bformat(_("Attempted to include file %1$s in itself! "
596                                "Ignoring inclusion."), from_utf8(incfile)));
597                 return 0;
598         }
599
600         // write it to a file (so far the complete file)
601         string const exportfile = changeExtension(incfile, ".sgml");
602         DocFileName writefile(changeExtension(included_file, ".sgml"));
603
604         if (loadIfNeeded(buffer(), params())) {
605                 Buffer * tmp = theBufferList().getBuffer(included_file);
606
607                 string const mangled = writefile.mangledFilename();
608                 writefile = makeAbsPath(mangled,
609                                         buffer().masterBuffer()->temppath());
610                 if (!runparams.nice)
611                         incfile = mangled;
612
613                 LYXERR(Debug::LATEX, "incfile:" << incfile);
614                 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
615                 LYXERR(Debug::LATEX, "writefile:" << writefile);
616
617                 tmp->makeDocBookFile(writefile, runparams, true);
618         }
619
620         runparams.exportdata->addExternalFile("docbook", writefile,
621                                               exportfile);
622         runparams.exportdata->addExternalFile("docbook-xml", writefile,
623                                               exportfile);
624
625         if (isVerbatim(params()) || isListings(params())) {
626                 os << "<inlinegraphic fileref=\""
627                    << '&' << include_label << ';'
628                    << "\" format=\"linespecific\">";
629         } else
630                 os << '&' << include_label << ';';
631
632         return 0;
633 }
634
635
636 void InsetInclude::validate(LaTeXFeatures & features) const
637 {
638         string incfile = to_utf8(params()["filename"]);
639         string writefile;
640
641         LASSERT(&buffer() == &features.buffer(), /**/);
642
643         string const included_file =
644                 includedFilename(buffer(), params()).absFilename();
645
646         if (isLyXFilename(included_file))
647                 writefile = changeExtension(included_file, ".sgml");
648         else
649                 writefile = included_file;
650
651         if (!features.runparams().nice && !isVerbatim(params()) && !isListings(params())) {
652                 incfile = DocFileName(writefile).mangledFilename();
653                 writefile = makeAbsPath(incfile,
654                                         buffer().masterBuffer()->temppath()).absFilename();
655         }
656
657         features.includeFile(include_label, writefile);
658
659         if (isVerbatim(params()))
660                 features.require("verbatim");
661         else if (isListings(params()))
662                 features.require("listings");
663
664         // Here we must do the fun stuff...
665         // Load the file in the include if it needs
666         // to be loaded:
667         if (loadIfNeeded(buffer(), params())) {
668                 // a file got loaded
669                 Buffer * const tmp = theBufferList().getBuffer(included_file);
670                 // make sure the buffer isn't us
671                 // FIXME RECURSIVE INCLUDES
672                 // This is not sufficient, as recursive includes could be
673                 // more than a file away. But it will do for now.
674                 if (tmp && tmp != &buffer()) {
675                         // We must temporarily change features.buffer,
676                         // otherwise it would always be the master buffer,
677                         // and nested includes would not work.
678                         features.setBuffer(*tmp);
679                         tmp->validate(features);
680                         features.setBuffer(buffer());
681                 }
682         }
683 }
684
685
686 void InsetInclude::fillWithBibKeys(BiblioInfo & keys,
687         InsetIterator const & /*di*/) const
688 {
689         if (loadIfNeeded(buffer(), params())) {
690                 string const included_file = includedFilename(buffer(), params()).absFilename();
691                 Buffer * tmp = theBufferList().getBuffer(included_file);
692                 BiblioInfo const & newkeys = tmp->localBibInfo();
693                 keys.mergeBiblioInfo(newkeys);
694         }
695 }
696
697
698 void InsetInclude::updateBibfilesCache()
699 {
700         Buffer * const tmp = getChildBuffer(buffer(), params());
701         if (tmp) {
702                 tmp->setParent(0);
703                 tmp->updateBibfilesCache();
704                 tmp->setParent(&buffer());
705         }
706 }
707
708
709 support::FileNameList const &
710         InsetInclude::getBibfilesCache(Buffer const & buffer) const
711 {
712         Buffer * const tmp = getChildBuffer(buffer, params());
713         if (tmp) {
714                 tmp->setParent(0);
715                 support::FileNameList const & cache = tmp->getBibfilesCache();
716                 tmp->setParent(&buffer);
717                 return cache;
718         }
719         static support::FileNameList const empty;
720         return empty;
721 }
722
723
724 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
725 {
726         LASSERT(mi.base.bv, /**/);
727
728         bool use_preview = false;
729         if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
730                 graphics::PreviewImage const * pimage =
731                         preview_->getPreviewImage(mi.base.bv->buffer());
732                 use_preview = pimage && pimage->image();
733         }
734
735         if (use_preview) {
736                 preview_->metrics(mi, dim);
737         } else {
738                 if (!set_label_) {
739                         set_label_ = true;
740                         button_.update(screenLabel(), true);
741                 }
742                 button_.metrics(mi, dim);
743         }
744
745         Box b(0, dim.wid, -dim.asc, dim.des);
746         button_.setBox(b);
747 }
748
749
750 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
751 {
752         LASSERT(pi.base.bv, /**/);
753
754         bool use_preview = false;
755         if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
756                 graphics::PreviewImage const * pimage =
757                         preview_->getPreviewImage(pi.base.bv->buffer());
758                 use_preview = pimage && pimage->image();
759         }
760
761         if (use_preview)
762                 preview_->draw(pi, x, y);
763         else
764                 button_.draw(pi, x, y);
765 }
766
767
768 docstring InsetInclude::contextMenu(BufferView const &, int, int) const
769 {
770         return from_ascii("context-include");
771 }
772
773
774 Inset::DisplayType InsetInclude::display() const
775 {
776         return type(params()) == INPUT ? Inline : AlignCenter;
777 }
778
779
780
781 //
782 // preview stuff
783 //
784
785 void InsetInclude::fileChanged() const
786 {
787         Buffer const * const buffer = updateFrontend();
788         if (!buffer)
789                 return;
790
791         preview_->removePreview(*buffer);
792         add_preview(*preview_.get(), *this, *buffer);
793         preview_->startLoading(*buffer);
794 }
795
796
797 namespace {
798
799 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
800 {
801         FileName const included_file = includedFilename(buffer, params);
802
803         return type(params) == INPUT && params.preview() &&
804                 included_file.isReadableFile();
805 }
806
807
808 docstring latexString(InsetInclude const & inset)
809 {
810         odocstringstream os;
811         // We don't need to set runparams.encoding since this will be done
812         // by latex() anyway.
813         OutputParams runparams(0);
814         runparams.flavor = OutputParams::LATEX;
815         inset.latex(os, runparams);
816
817         return os.str();
818 }
819
820
821 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
822                  Buffer const & buffer)
823 {
824         InsetCommandParams const & params = inset.params();
825         if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
826             preview_wanted(params, buffer)) {
827                 renderer.setAbsFile(includedFilename(buffer, params));
828                 docstring const snippet = latexString(inset);
829                 renderer.addPreview(snippet, buffer);
830         }
831 }
832
833 } // namespace anon
834
835
836 void InsetInclude::addPreview(graphics::PreviewLoader & ploader) const
837 {
838         Buffer const & buffer = ploader.buffer();
839         if (!preview_wanted(params(), buffer))
840                 return;
841         preview_->setAbsFile(includedFilename(buffer, params()));
842         docstring const snippet = latexString(*this);
843         preview_->addPreview(snippet, ploader);
844 }
845
846
847 void InsetInclude::addToToc(ParConstIterator const & cpit) const
848 {
849         TocBackend & backend = buffer().tocBackend();
850
851         if (isListings(params())) {
852                 if (label_)
853                         label_->addToToc(cpit);
854
855                 InsetListingsParams p(to_utf8(params()["lstparams"]));
856                 string caption = p.getParamValue("caption");
857                 if (caption.empty())
858                         return;
859                 Toc & toc = backend.toc("listing");
860                 docstring str = convert<docstring>(toc.size() + 1)
861                         + ". " +  from_utf8(caption);
862                 ParConstIterator pit = cpit;
863                 pit.push_back(*this);
864                 toc.push_back(TocItem(pit, 0, str));
865                 return;
866         }
867         Buffer const * const childbuffer = getChildBuffer(buffer(), params());
868         if (!childbuffer)
869                 return;
870
871         Toc & toc = backend.toc("child");
872         docstring str = childbuffer->fileName().displayName();
873         toc.push_back(TocItem(cpit, 0, str));
874
875         TocList & toclist = backend.tocs();
876         TocList const & childtoclist = childbuffer->tocBackend().tocs();
877         TocList::const_iterator it = childtoclist.begin();
878         TocList::const_iterator const end = childtoclist.end();
879         for(; it != end; ++it)
880                 toclist[it->first].insert(toclist[it->first].end(),
881                         it->second.begin(), it->second.end());
882 }
883
884
885 void InsetInclude::updateLabels(ParIterator const & it)
886 {
887         Buffer const * const childbuffer = getChildBuffer(buffer(), params());
888         if (childbuffer) {
889                 lyx::updateLabels(*childbuffer, true);
890                 return;
891         }
892         if (!isListings(params()))
893                 return;
894
895         if (label_)
896                 label_->updateLabels(it);
897
898         InsetListingsParams const par(to_utf8(params()["lstparams"]));
899         if (par.getParamValue("caption").empty()) {
900                 listings_label_.clear();
901                 return;
902         }
903         Counters & counters = buffer().params().documentClass().counters();
904         docstring const cnt = from_ascii("listing");
905         listings_label_ = buffer().B_("Program Listing");
906         if (counters.hasCounter(cnt)) {
907                 counters.step(cnt);
908                 listings_label_ += " " + convert<docstring>(counters.value(cnt));
909         }
910 }
911
912
913 } // namespace lyx