]> git.lyx.org Git - features.git/blob - src/insets/InsetPreview.cpp
Factor out the list of macro definitions for InsetPreview.
[features.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 MacroNameSet gatherMacroDefinitions(const Buffer* buffer, const Inset * inset)
83 {
84         // Collect macros for this inset.
85         // Not done yet: this function returns a list of macro *definitions*.
86         MacroNameSet macros;
87         buffer->listMacroNames(macros);
88
89         // Look for math insets and collect definitions for the used macros.
90         MacroNameSet defs;
91         DocIterator const dbeg = doc_iterator_begin(buffer, inset);
92         DocIterator dit = dbeg;
93         DocIterator const dend = doc_iterator_end(buffer, inset);
94         if (!dit.nextInset())
95                 dit.forwardInset();
96
97         for (; dit != dend; dit.forwardInset()) {
98                 InsetMath * im = dit.nextInset()->asInsetMath();
99                 InsetMathHull * hull = im ? im->asHullInset() : nullptr;
100                 if (!hull)
101                         continue;
102                 for (idx_type idx = 0; idx < hull->nargs(); ++idx)
103                         hull->usedMacros(hull->cell(idx), dbeg, macros, defs);
104         }
105
106         return defs;
107 }
108
109
110 void InsetPreview::preparePreview(DocIterator const & pos) const
111 {
112         odocstringstream str;
113         otexstream os(str);
114         OutputParams runparams(&pos.buffer()->params().encoding());
115         latex(os, runparams);
116
117         MacroNameSet defs = gatherMacroDefinitions(pos.buffer(), this);
118         docstring macro_preamble;
119         for (const auto& def : defs)
120                 macro_preamble.append(def);
121
122         docstring const snippet = macro_preamble + str.str();
123         preview_->addPreview(snippet, *pos.buffer());
124 }
125
126
127 bool InsetPreview::previewState(BufferView * bv) const
128 {
129         if (!editing(bv) && RenderPreview::previewText()) {
130                 graphics::PreviewImage const * pimage =
131                         preview_->getPreviewImage(bv->buffer());
132                 return pimage && pimage->image();
133         }
134         return false;
135 }
136
137
138 void InsetPreview::reloadPreview(DocIterator const & pos) const
139 {
140         preparePreview(pos);
141         preview_->startLoading(*pos.buffer());
142 }
143
144
145 void InsetPreview::draw(PainterInfo & pi, int x, int y) const
146 {
147         if (previewState(pi.base.bv)) {
148                 // one pixel gap in front
149                 preview_->draw(pi, x + 1, y);
150         } else
151                 InsetText::draw(pi, x, y);
152 }
153
154
155 void InsetPreview::edit(Cursor & cur, bool front, EntryDirection entry_from)
156 {
157         cur.push(*this);
158         InsetText::edit(cur, front, entry_from);
159 }
160
161
162 Inset * InsetPreview::editXY(Cursor & cur, int x, int y)
163 {
164         if (previewState(&cur.bv())) {
165                 edit(cur, true, ENTRY_DIRECTION_IGNORE);
166                 return this;
167         }
168         cur.push(*this);
169         return InsetText::editXY(cur, x, y);
170 }
171
172
173 void InsetPreview::metrics(MetricsInfo & mi, Dimension & dim) const
174 {
175         if (previewState(mi.base.bv)) {
176                 preview_->metrics(mi, dim);
177
178                 dim.wid = max(dim.wid, 4);
179                 dim.asc = max(dim.asc, 4);
180
181                 dim.asc += topOffset(mi.base.bv);
182                 dim.des += bottomOffset(mi.base.bv);
183                 // insert a one pixel gap
184                 dim.wid += 1;
185                 Dimension dim_dummy;
186                 MetricsInfo mi_dummy = mi;
187                 InsetText::metrics(mi_dummy, dim_dummy);
188                 return;
189         }
190         InsetText::metrics(mi, dim);
191 }
192
193
194 bool InsetPreview::notifyCursorLeaves(Cursor const & old, Cursor & cur)
195 {
196         reloadPreview(old);
197         cur.screenUpdateFlags(Update::Force);
198         return InsetText::notifyCursorLeaves(old, cur);
199 }
200
201
202 } // namespace lyx