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