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