]> git.lyx.org Git - lyx.git/blob - src/insets/InsetPreview.cpp
Squash some warnings.
[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
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         TexRow texrow;
85         odocstringstream str;
86         otexstream os(str, texrow);
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 + TEXT_TO_INSET_OFFSET, y);
142                 setPosCache(pi, x, 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                 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
171
172                 dim.wid = max(dim.wid, 4);
173                 dim.asc = max(dim.asc, 4);
174
175                 dim.asc += TEXT_TO_INSET_OFFSET;
176                 dim.des += TEXT_TO_INSET_OFFSET;
177                 dim.wid += TEXT_TO_INSET_OFFSET;
178                 dim.wid += TEXT_TO_INSET_OFFSET;
179                 // insert a one pixel gap
180                 dim.wid += 1;
181                 // Cache the inset dimension.
182                 setDimCache(mi, dim);
183                 Dimension dim_dummy;
184                 MetricsInfo mi_dummy = mi;
185                 InsetText::metrics(mi_dummy, dim_dummy);
186                 return;
187         }
188         InsetText::metrics(mi, dim);
189 }
190
191
192 bool InsetPreview::notifyCursorLeaves(Cursor const & old, Cursor & cur)
193 {
194         reloadPreview(old);
195         cur.screenUpdateFlags(Update::Force);
196         return InsetText::notifyCursorLeaves(old, cur);
197 }
198
199
200 } // namespace lyx