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