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