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