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