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