]> git.lyx.org Git - lyx.git/blob - src/insets/renderers.C
Re-add the RasterImage template.
[lyx.git] / src / insets / renderers.C
1 /**
2  * \file renderers.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "insets/renderers.h"
14
15 #include "insets/inset.h"
16
17 #include "BufferView.h"
18 #include "gettext.h"
19 #include "LColor.h"
20 #include "metricsinfo.h"
21
22 #include "frontends/font_metrics.h"
23 #include "frontends/LyXView.h"
24 #include "frontends/Painter.h"
25
26 #include "graphics/GraphicsImage.h"
27
28 #include "support/filetools.h"
29
30 using lyx::support::AbsolutePath;
31 using lyx::support::OnlyFilename;
32
33
34 RenderInset::RenderInset()
35 {}
36
37
38 RenderInset::RenderInset(RenderInset const &)
39 {
40         // Cached variables are not copied
41 }
42
43
44 RenderInset::~RenderInset()
45 {}
46
47
48 RenderInset & RenderInset::operator=(RenderInset const &)
49 {
50         // Cached variables are not copied
51         return *this;
52 }
53
54
55 BufferView * RenderInset::view() const
56 {
57         return view_.lock().get();
58 }
59
60
61 ButtonRenderer::ButtonRenderer()
62         : editable_(false)
63 {}
64
65
66 RenderInset * ButtonRenderer::clone() const
67 {
68         return new ButtonRenderer(*this);
69 }
70
71
72 void ButtonRenderer::update(string const & text, bool editable)
73 {
74         text_ = text;
75         editable_ = editable;
76 }
77
78
79 void ButtonRenderer::metrics(MetricsInfo & mi, Dimension & dim) const
80 {
81         BOOST_ASSERT(mi.base.bv);
82
83         LyXFont font(LyXFont::ALL_SANE);
84         font.decSize();
85
86         if (editable_)
87                 font_metrics::buttonText(text_, font, dim.wid, dim.asc, dim.des);
88         else
89                 font_metrics::rectText(text_, font, dim.wid, dim.asc, dim.des);
90
91         dim.wid += 4;
92 }
93
94
95 void ButtonRenderer::draw(PainterInfo & pi, int x, int y) const
96 {
97         BOOST_ASSERT(pi.base.bv);
98         view_ = pi.base.bv->owner()->view();
99
100         // Draw it as a box with the LaTeX text
101         LyXFont font(LyXFont::ALL_SANE);
102         font.setColor(LColor::command);
103         font.decSize();
104
105         if (editable_) {
106                 pi.pain.buttonText(x + 2, y, text_, font);
107         } else {
108                 pi.pain.rectText(x + 2, y, text_, font,
109                               LColor::commandbg, LColor::commandframe);
110         }
111 }
112
113
114 GraphicRenderer::GraphicRenderer()
115         : checksum_(0)
116 {}
117
118
119 GraphicRenderer::GraphicRenderer(GraphicRenderer const & other)
120         : RenderInset(other),
121           loader_(other.loader_),
122           params_(other.params_),
123           checksum_(0)
124 {}
125
126
127 RenderInset * GraphicRenderer::clone() const
128 {
129         return new GraphicRenderer(*this);
130 }
131
132
133 void GraphicRenderer::update(lyx::graphics::Params const & params)
134 {
135         params_ = params;
136
137         if (!params_.filename.empty()) {
138                 BOOST_ASSERT(AbsolutePath(params_.filename));
139                 loader_.reset(params_.filename, params_);
140         }
141 }
142
143
144 bool GraphicRenderer::hasFileChanged() const
145 {
146         unsigned long const new_checksum = loader_.checksum();
147         bool const file_has_changed = checksum_ != new_checksum;
148         if (file_has_changed)
149                 checksum_ = new_checksum;
150         return file_has_changed;
151 }
152
153
154 boost::signals::connection GraphicRenderer::connect(slot_type const & slot) const
155 {
156         return loader_.connect(slot);
157 }
158
159
160 string const GraphicRenderer::statusMessage() const
161 {
162         switch (loader_.status()) {
163                 case lyx::graphics::WaitingToLoad:
164                         return _("Not shown.");
165                 case lyx::graphics::Loading:
166                         return _("Loading...");
167                 case lyx::graphics::Converting:
168                         return _("Converting to loadable format...");
169                 case lyx::graphics::Loaded:
170                         return _("Loaded into memory. Must now generate pixmap.");
171                 case lyx::graphics::ScalingEtc:
172                         return _("Scaling etc...");
173                 case lyx::graphics::Ready:
174                         return _("Ready to display");
175                 case lyx::graphics::ErrorNoFile:
176                         return _("No file found!");
177                 case lyx::graphics::ErrorConverting:
178                         return _("Error converting to loadable format");
179                 case lyx::graphics::ErrorLoading:
180                         return _("Error loading file into memory");
181                 case lyx::graphics::ErrorGeneratingPixmap:
182                         return _("Error generating the pixmap");
183                 case lyx::graphics::ErrorUnknown:
184                         return _("No image");
185         }
186         return string();
187 }
188
189
190 bool GraphicRenderer::readyToDisplay() const
191 {
192         if (!loader_.image() || loader_.status() != lyx::graphics::Ready)
193                 return false;
194         return loader_.image()->isDrawable();
195 }
196
197
198 void GraphicRenderer::metrics(MetricsInfo & mi, Dimension & dim) const
199 {
200         bool image_ready = readyToDisplay();
201
202         dim.asc = image_ready ? loader_.image()->getHeight() : 50;
203         dim.des = 0;
204
205         if (image_ready) {
206                 dim.wid = loader_.image()->getWidth() +
207                         2 * InsetOld::TEXT_TO_INSET_OFFSET;
208         } else {
209                 int font_width = 0;
210
211                 LyXFont msgFont(mi.base.font);
212                 msgFont.setFamily(LyXFont::SANS_FAMILY);
213
214                 string const justname = OnlyFilename(params_.filename);
215                 if (!justname.empty()) {
216                         msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
217                         font_width = font_metrics::width(justname, msgFont);
218                 }
219
220                 string const msg = statusMessage();
221                 if (!msg.empty()) {
222                         msgFont.setSize(LyXFont::SIZE_TINY);
223                         font_width = std::max(font_width,
224                                               font_metrics::width(msg, msgFont));
225                 }
226
227                 dim.wid = std::max(50, font_width + 15);
228         }
229
230         dim_ = dim;
231 }
232
233
234 void GraphicRenderer::draw(PainterInfo & pi, int x, int y) const
235 {
236         BOOST_ASSERT(pi.base.bv);
237         view_ = pi.base.bv->owner()->view();
238
239 #if 0
240         // Comment this out and see if anything goes wrong.
241         // The explanation for why it _was_ needed once upon a time is below.
242
243         // MakeAbsPath returns filename_ unchanged if it is absolute
244         // already.
245         string const file_with_path =
246                 MakeAbsPath(params_.filename, view_->buffer()->filePath());
247
248         // A 'paste' operation creates a new inset with the correct filepath,
249         // but then the 'old' inset stored in the 'copy' operation is actually
250         // added to the buffer.
251
252         // Thus, pasting a graphic into a new buffer with different
253         // buffer->filePath() will result in the image being displayed in LyX even
254         // though the relative path now points at nothing at all. Subsequent
255         // loading of the file into LyX will therefore fail.
256
257         // We should ensure that the filepath is correct.
258         if (file_with_path != loader_.filename()) {
259                 params_.filename = file_with_path;
260                 update(params_);
261         }
262 #endif
263
264         if (params_.display != lyx::graphics::NoDisplay &&
265             loader_.status() == lyx::graphics::WaitingToLoad)
266                 loader_.startLoading();
267
268         if (params_.display != lyx::graphics::NoDisplay &&
269             !loader_.monitoring())
270                 loader_.startMonitoring();
271
272         // This will draw the graphics. If the graphics has not been loaded yet,
273         // we draw just a rectangle.
274
275         if (readyToDisplay()) {
276                 pi.pain.image(x + InsetOld::TEXT_TO_INSET_OFFSET,
277                               y - dim_.asc,
278                               dim_.wid - 2 * InsetOld::TEXT_TO_INSET_OFFSET,
279                               dim_.asc + dim_.des,
280                               *loader_.image());
281
282         } else {
283                 pi.pain.rectangle(x + InsetOld::TEXT_TO_INSET_OFFSET,
284                                   y - dim_.asc,
285                                   dim_.wid - 2 * InsetOld::TEXT_TO_INSET_OFFSET,
286                                   dim_.asc + dim_.des,
287                                   LColor::foreground);
288
289                 // Print the file name.
290                 LyXFont msgFont = pi.base.font;
291                 msgFont.setFamily(LyXFont::SANS_FAMILY);
292                 string const justname = OnlyFilename(params_.filename);
293
294                 if (!justname.empty()) {
295                         msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
296                         pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,
297                                    y - font_metrics::maxAscent(msgFont) - 4,
298                                    justname, msgFont);
299                 }
300
301                 // Print the message.
302                 string const msg = statusMessage();
303                 if (!msg.empty()) {
304                         msgFont.setSize(LyXFont::SIZE_TINY);
305                         pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,
306                                      y - 4, msg, msgFont);
307                 }
308         }
309 }