]> git.lyx.org Git - lyx.git/blob - src/insets/InsetPreview.cpp
Added inset-select-all to emacs bindings
[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 <sstream>
28
29 using namespace std;
30
31 namespace lyx {
32
33
34 InsetPreview::InsetPreview(Buffer * buf) 
35         : InsetText(buf),
36           preview_(new RenderPreview(this)), use_preview_(true)
37 {
38         setAutoBreakRows(true);
39         setDrawFrame(true);
40         setFrameColor(Color_previewframe);
41 }
42
43
44 InsetPreview::~InsetPreview() 
45 {}
46
47
48 InsetPreview::InsetPreview(InsetPreview const & other)
49         : InsetText(other)
50 {
51         preview_.reset(new RenderPreview(*other.preview_, this));
52 }
53
54
55 void InsetPreview::write(ostream & os) const
56 {
57         os << "Preview" << "\n";
58         text().write(os);
59 }
60
61
62 void InsetPreview::addPreview(DocIterator const & inset_pos,
63         graphics::PreviewLoader &) const
64 {
65         preparePreview(inset_pos);
66 }
67
68
69 void InsetPreview::preparePreview(DocIterator const & pos) const  
70 {
71         TexRow texrow;
72         odocstringstream str;  
73         otexstream os(str, texrow);
74         OutputParams runparams(&pos.buffer()->params().encoding());
75         latex(os, runparams);
76         docstring const snippet = str.str();
77         preview_->addPreview(snippet, *pos.buffer());  
78 }
79
80
81 bool InsetPreview::previewState(BufferView * bv) const
82 {
83         if (!editing(bv) && RenderPreview::previewText()) {
84                 graphics::PreviewImage const * pimage =
85                         preview_->getPreviewImage(bv->buffer());
86                 return pimage && pimage->image();
87         }
88         return false;
89 }
90
91
92 void InsetPreview::reloadPreview(DocIterator const & pos) const
93 {
94         preparePreview(pos);
95         preview_->startLoading(*pos.buffer());
96 }
97
98
99 void InsetPreview::draw(PainterInfo & pi, int x, int y) const
100 {
101         use_preview_ = previewState(pi.base.bv);
102
103         if (use_preview_) {
104                 // one pixel gap in front
105                 preview_->draw(pi, x + 1 + TEXT_TO_INSET_OFFSET, y);
106                 setPosCache(pi, x, y);
107                 return;
108         }
109         InsetText::draw(pi, x, y);
110 }
111
112
113 void InsetPreview::edit(Cursor & cur, bool front, EntryDirection entry_from)
114 {
115         cur.push(*this);
116         InsetText::edit(cur, front, entry_from);
117 }
118
119
120 Inset * InsetPreview::editXY(Cursor & cur, int x, int y)
121 {
122         if (use_preview_) {
123                 edit(cur, true, ENTRY_DIRECTION_IGNORE);
124                 return this;
125         }
126         cur.push(*this);
127         return InsetText::editXY(cur, x, y);
128 }
129
130
131 void InsetPreview::metrics(MetricsInfo & mi, Dimension & dim) const
132 {
133         if (previewState(mi.base.bv)) {
134                 preview_->metrics(mi, dim);
135                 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
136                 
137                 dim.wid = max(dim.wid, 4);
138                 dim.asc = max(dim.asc, 4);
139                 
140                 dim.asc += TEXT_TO_INSET_OFFSET;
141                 dim.des += TEXT_TO_INSET_OFFSET;
142                 dim.wid += TEXT_TO_INSET_OFFSET;
143                 dim_ = dim;
144                 dim.wid += TEXT_TO_INSET_OFFSET;
145                 // insert a one pixel gap
146                 dim.wid += 1;
147                 // Cache the inset dimension.
148                 setDimCache(mi, dim);
149                 Dimension dim_dummy;
150                 MetricsInfo mi_dummy = mi;
151                 InsetText::metrics(mi_dummy, dim_dummy);
152                 return;
153         }
154         InsetText::metrics(mi, dim);
155 }
156
157
158 bool InsetPreview::notifyCursorLeaves(Cursor const & old, Cursor & cur)
159 {
160         reloadPreview(old);
161         cur.screenUpdateFlags(Update::Force);
162         return InsetText::notifyCursorLeaves(old, cur);
163 }
164
165
166 } // namespace lyx