]> git.lyx.org Git - lyx.git/blob - src/insets/InsetPreview.cpp
DocBook: make empty bibliographies empty.
[lyx.git] / src / insets / InsetPreview.cpp
1 /**
2  * \file InsetPreview.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Vincent van Ravesteijn
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10 #include "config.h"
11
12 #include "InsetPreview.h"
13
14 #include "Buffer.h"
15 #include "BufferParams.h"
16 #include "BufferView.h"
17 #include "Cursor.h"
18 #include "Dimension.h"
19 #include "Lexer.h"
20 #include "MetricsInfo.h"
21 #include "OutputParams.h"
22 #include "RenderPreview.h"
23 #include "texstream.h"
24
25 #include "frontends/Painter.h"
26
27 #include "graphics/PreviewImage.h"
28
29 #include "mathed/InsetMathHull.h"
30 #include "mathed/MacroTable.h"
31
32 #include <sstream>
33
34 using namespace std;
35
36 namespace lyx {
37
38
39 InsetPreview::InsetPreview(Buffer * buf)
40         : InsetText(buf), preview_(new RenderPreview(this))
41 {
42         setDrawFrame(true);
43         setFrameColor(Color_previewframe);
44 }
45
46
47 InsetPreview::~InsetPreview()
48 {}
49
50
51 InsetPreview::InsetPreview(InsetPreview const & other)
52         : InsetText(other)
53 {
54         preview_.reset(new RenderPreview(*other.preview_, this));
55 }
56
57
58 InsetPreview & InsetPreview::operator=(InsetPreview const & other)
59 {
60         if (&other == this)
61                 return *this;
62
63         InsetText::operator=(other);
64         preview_.reset(new RenderPreview(*other.preview_, this));
65
66         return *this;
67 }
68
69
70 void InsetPreview::write(ostream & os) const
71 {
72         os << "Preview" << "\n";
73         text().write(os);
74 }
75
76
77 void InsetPreview::addPreview(DocIterator const & inset_pos,
78         graphics::PreviewLoader &) const
79 {
80         preparePreview(inset_pos);
81 }
82
83
84 void InsetPreview::preparePreview(DocIterator const & pos) const
85 {
86         odocstringstream str;
87         otexstream os(str);
88         OutputParams runparams(&pos.buffer()->params().encoding());
89         latex(os, runparams);
90
91         // collect macros at this position
92         MacroNameSet macros;
93         pos.buffer()->listMacroNames(macros);
94
95         // look for math insets and collect definitions for the used macros
96         MacroNameSet defs;
97         DocIterator dit = doc_iterator_begin(pos.buffer(), this);
98         DocIterator const dend = doc_iterator_end(pos.buffer(), this);
99         if (!dit.nextInset())
100                 dit.forwardInset();
101         for (; dit != dend; dit.forwardInset()) {
102                 InsetMath * im = dit.nextInset()->asInsetMath();
103                 InsetMathHull * hull = im ? im->asHullInset() : nullptr;
104                 if (!hull)
105                         continue;
106                 for (idx_type idx = 0; idx < hull->nargs(); ++idx)
107                         hull->usedMacros(hull->cell(idx), pos, macros, defs);
108         }
109         MacroNameSet::iterator it = defs.begin();
110         MacroNameSet::iterator end = defs.end();
111         docstring macro_preamble;
112         for (; it != end; ++it)
113                 macro_preamble.append(*it);
114
115         docstring const snippet = macro_preamble + str.str();
116         preview_->addPreview(snippet, *pos.buffer());
117 }
118
119
120 bool InsetPreview::previewState(BufferView * bv) const
121 {
122         if (!editing(bv) && RenderPreview::previewText()) {
123                 graphics::PreviewImage const * pimage =
124                         preview_->getPreviewImage(bv->buffer());
125                 return pimage && pimage->image();
126         }
127         return false;
128 }
129
130
131 void InsetPreview::reloadPreview(DocIterator const & pos) const
132 {
133         preparePreview(pos);
134         preview_->startLoading(*pos.buffer());
135 }
136
137
138 void InsetPreview::draw(PainterInfo & pi, int x, int y) const
139 {
140         if (previewState(pi.base.bv)) {
141                 // one pixel gap in front
142                 preview_->draw(pi, x + 1, y);
143         } else
144                 InsetText::draw(pi, x, y);
145 }
146
147
148 void InsetPreview::edit(Cursor & cur, bool front, EntryDirection entry_from)
149 {
150         cur.push(*this);
151         InsetText::edit(cur, front, entry_from);
152 }
153
154
155 Inset * InsetPreview::editXY(Cursor & cur, int x, int y)
156 {
157         if (previewState(&cur.bv())) {
158                 edit(cur, true, ENTRY_DIRECTION_IGNORE);
159                 return this;
160         }
161         cur.push(*this);
162         return InsetText::editXY(cur, x, y);
163 }
164
165
166 void InsetPreview::metrics(MetricsInfo & mi, Dimension & dim) const
167 {
168         if (previewState(mi.base.bv)) {
169                 preview_->metrics(mi, dim);
170
171                 dim.wid = max(dim.wid, 4);
172                 dim.asc = max(dim.asc, 4);
173
174                 dim.asc += topOffset(mi.base.bv);
175                 dim.des += bottomOffset(mi.base.bv);
176                 // insert a one pixel gap
177                 dim.wid += 1;
178                 Dimension dim_dummy;
179                 MetricsInfo mi_dummy = mi;
180                 InsetText::metrics(mi_dummy, dim_dummy);
181                 return;
182         }
183         InsetText::metrics(mi, dim);
184 }
185
186
187 bool InsetPreview::notifyCursorLeaves(Cursor const & old, Cursor & cur)
188 {
189         reloadPreview(old);
190         cur.screenUpdateFlags(Update::Force);
191         return InsetText::notifyCursorLeaves(old, cur);
192 }
193
194
195 } // namespace lyx