]> git.lyx.org Git - lyx.git/blob - src/insets/RenderGraphic.cpp
RenderGraphic::metrics(): return early.
[lyx.git] / src / insets / RenderGraphic.cpp
1 /**
2  * \file RenderGraphic.cpp
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 "RenderGraphic.h"
14
15 #include "insets/Inset.h"
16
17 #include "support/FileName.h"
18 #include "support/filetools.h"
19 #include "support/gettext.h"
20 #include "LyX.h"
21 #include "LyXRC.h"
22 #include "MetricsInfo.h"
23
24 #include "frontends/FontMetrics.h"
25 #include "frontends/Painter.h"
26
27 #include "graphics/GraphicsImage.h"
28
29 #include "support/filetools.h"
30
31 #include <boost/bind.hpp>
32
33 using namespace std;
34
35 namespace lyx {
36
37
38 RenderGraphic::RenderGraphic(Inset const * inset)
39 {
40         loader_.connect(boost::bind(&Inset::updateFrontend, inset));
41 }
42
43
44 RenderGraphic::RenderGraphic(RenderGraphic const & other, Inset const * inset)
45         : RenderBase(other), loader_(other.loader_), params_(other.params_)
46 {
47         loader_.connect(boost::bind(&Inset::updateFrontend, inset));
48 }
49
50
51 RenderBase * RenderGraphic::clone(Inset const * inset) const
52 {
53         return new RenderGraphic(*this, inset);
54 }
55
56
57 void RenderGraphic::update(graphics::Params const & params)
58 {
59         params_ = params;
60
61         if (!params_.filename.empty())
62                 loader_.reset(params_.filename, params_);
63 }
64
65
66 namespace {
67
68 bool displayGraphic(graphics::Params const & params)
69 {
70         return params.display != graphics::NoDisplay &&
71                 lyxrc.display_graphics != graphics::NoDisplay;
72 }
73
74
75 docstring const statusMessage(graphics::Params const & params,
76                            graphics::ImageStatus status)
77 {
78         docstring ret;
79
80         if (!displayGraphic(params))
81                 ret = _("Not shown.");
82         else {
83                 switch (status) {
84                 case graphics::WaitingToLoad:
85                         ret = _("Not shown.");
86                         break;
87                 case graphics::Loading:
88                         ret = _("Loading...");
89                         break;
90                 case graphics::Converting:
91                         ret = _("Converting to loadable format...");
92                         break;
93                 case graphics::Loaded:
94                         ret = _("Loaded into memory. Generating pixmap...");
95                         break;
96                 case graphics::ScalingEtc:
97                         ret = _("Scaling etc...");
98                         break;
99                 case graphics::Ready:
100                         ret = _("Ready to display");
101                         break;
102                 case graphics::ErrorNoFile:
103                         ret = _("No file found!");
104                         break;
105                 case graphics::ErrorConverting:
106                         ret = _("Error converting to loadable format");
107                         break;
108                 case graphics::ErrorLoading:
109                         ret = _("Error loading file into memory");
110                         break;
111                 case graphics::ErrorGeneratingPixmap:
112                         ret = _("Error generating the pixmap");
113                         break;
114                 case graphics::ErrorUnknown:
115                         ret = _("No image");
116                         break;
117                 }
118         }
119
120         return ret;
121 }
122
123
124 bool readyToDisplay(graphics::Loader const & loader)
125 {
126         if (!loader.image() || loader.status() != graphics::Ready)
127                 return false;
128         return loader.image()->isDrawable();
129 }
130
131 } // namespace anon
132
133
134 void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
135 {
136         bool image_ready = displayGraphic(params_) && readyToDisplay(loader_);
137
138         if (image_ready) {
139                 dim.wid = loader_.image()->width() + 2 * Inset::TEXT_TO_INSET_OFFSET;
140                 dim.asc = loader_.image()->height();
141                 dim_ = dim;
142                 return;
143         }
144
145         dim.asc = image_ready ? loader_.image()->height() : 50;
146         dim.des = 0;
147
148         int font_width = 0;
149
150         FontInfo msgFont(mi.base.font);
151         msgFont.setFamily(SANS_FAMILY);
152
153         // FIXME UNICODE
154         docstring const justname = from_utf8(params_.filename.onlyFileName());
155         if (!justname.empty()) {
156                 msgFont.setSize(FONT_SIZE_FOOTNOTE);
157                 font_width = theFontMetrics(msgFont).width(justname);
158         }
159
160         docstring const msg = statusMessage(params_, loader_.status());
161         if (!msg.empty()) {
162                 msgFont.setSize(FONT_SIZE_TINY);
163                 font_width = max(font_width,
164                         theFontMetrics(msgFont).width(msg));
165         }
166
167         dim.wid = max(50, font_width + 15);
168
169         dim_ = dim;
170 }
171
172
173 void RenderGraphic::draw(PainterInfo & pi, int x, int y) const
174 {
175         if (displayGraphic(params_)) {
176                 if (loader_.status() == graphics::WaitingToLoad)
177                         loader_.startLoading();
178                 if (!loader_.monitoring())
179                         loader_.startMonitoring();
180         }
181
182         // This will draw the graphics. If the graphics has not been
183         // loaded yet, we draw just a rectangle.
184
185         if (displayGraphic(params_) && readyToDisplay(loader_)) {
186                 pi.pain.image(x + Inset::TEXT_TO_INSET_OFFSET,
187                               y - dim_.asc,
188                               dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
189                               dim_.asc + dim_.des,
190                               *loader_.image());
191
192         } else {
193                 pi.pain.rectangle(x + Inset::TEXT_TO_INSET_OFFSET,
194                                   y - dim_.asc,
195                                   dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
196                                   dim_.asc + dim_.des,
197                                   Color_foreground);
198
199                 // Print the file name.
200                 FontInfo msgFont = pi.base.font;
201                 msgFont.setFamily(SANS_FAMILY);
202                 string const justname = params_.filename.onlyFileName();
203
204                 if (!justname.empty()) {
205                         msgFont.setSize(FONT_SIZE_FOOTNOTE);
206                         pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
207                                    y - theFontMetrics(msgFont).maxAscent() - 4,
208                                    from_utf8(justname), msgFont);
209                 }
210
211                 // Print the message.
212                 docstring const msg = statusMessage(params_, loader_.status());
213                 if (!msg.empty()) {
214                         msgFont.setSize(FONT_SIZE_TINY);
215                         pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
216                                      y - 4, msg, msgFont);
217                 }
218         }
219 }
220
221
222 } // namespace lyx