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