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