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