]> git.lyx.org Git - lyx.git/blob - src/insets/RenderGraphic.cpp
ca8008c65eede0baa308408834167d25e6ebf6bc
[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         if (displayGraphic(params_)) {
137                 if (loader_.status() == graphics::WaitingToLoad)
138                         loader_.startLoading();
139                 if (!loader_.monitoring())
140                         loader_.startMonitoring();
141         }
142
143         bool image_ready = displayGraphic(params_) && readyToDisplay(loader_);
144         if (image_ready) {
145                 dim.wid = loader_.image()->width() + 2 * Inset::TEXT_TO_INSET_OFFSET;
146                 dim.asc = loader_.image()->height();
147                 dim_ = dim;
148                 return;
149         }
150
151         dim.asc = image_ready ? loader_.image()->height() : 50;
152         dim.des = 0;
153
154         int font_width = 0;
155
156         FontInfo msgFont(mi.base.font);
157         msgFont.setFamily(SANS_FAMILY);
158
159         // FIXME UNICODE
160         docstring const justname = from_utf8(params_.filename.onlyFileName());
161         if (!justname.empty()) {
162                 msgFont.setSize(FONT_SIZE_FOOTNOTE);
163                 font_width = theFontMetrics(msgFont).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 = max(font_width,
170                         theFontMetrics(msgFont).width(msg));
171         }
172
173         dim.wid = max(50, font_width + 15);
174
175         dim_ = dim;
176 }
177
178
179 void RenderGraphic::draw(PainterInfo & pi, int x, int y) const
180 {
181         // This will draw the graphics. If the graphics has not been
182         // loaded yet, we draw just a rectangle.
183
184         if (displayGraphic(params_) && readyToDisplay(loader_)) {
185                 pi.pain.image(x + Inset::TEXT_TO_INSET_OFFSET,
186                               y - dim_.asc,
187                               dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
188                               dim_.asc + dim_.des,
189                               *loader_.image());
190
191         } else {
192                 pi.pain.rectangle(x + Inset::TEXT_TO_INSET_OFFSET,
193                                   y - dim_.asc,
194                                   dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
195                                   dim_.asc + dim_.des,
196                                   Color_foreground);
197
198                 // Print the file name.
199                 FontInfo msgFont = pi.base.font;
200                 msgFont.setFamily(SANS_FAMILY);
201                 string const justname = params_.filename.onlyFileName();
202
203                 if (!justname.empty()) {
204                         msgFont.setSize(FONT_SIZE_FOOTNOTE);
205                         pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
206                                    y - theFontMetrics(msgFont).maxAscent() - 4,
207                                    from_utf8(justname), msgFont);
208                 }
209
210                 // Print the message.
211                 docstring const msg = statusMessage(params_, loader_.status());
212                 if (!msg.empty()) {
213                         msgFont.setSize(FONT_SIZE_TINY);
214                         pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
215                                      y - 4, msg, msgFont);
216                 }
217         }
218 }
219
220
221 } // namespace lyx