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