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