]> git.lyx.org Git - lyx.git/blob - src/insets/RenderGraphic.cpp
Fix text frame drawing.
[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 bool 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         bool const changed = dim_ != dim;
178         dim_ = dim;
179         return changed;
180 }
181
182
183 void RenderGraphic::draw(PainterInfo & pi, int x, int y) const
184 {
185         if (displayGraphic(params_)) {
186                 if (loader_.status() == graphics::WaitingToLoad)
187                         loader_.startLoading();
188                 if (!loader_.monitoring())
189                         loader_.startMonitoring();
190         }
191
192         // This will draw the graphics. If the graphics has not been
193         // loaded yet, we draw just a rectangle.
194
195         if (displayGraphic(params_) && readyToDisplay(loader_)) {
196                 pi.pain.image(x + Inset::TEXT_TO_INSET_OFFSET,
197                               y - dim_.asc,
198                               dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
199                               dim_.asc + dim_.des,
200                               *loader_.image());
201
202         } else {
203                 pi.pain.rectangle(x + Inset::TEXT_TO_INSET_OFFSET,
204                                   y - dim_.asc,
205                                   dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
206                                   dim_.asc + dim_.des,
207                                   Color::foreground);
208
209                 // Print the file name.
210                 Font msgFont = pi.base.font;
211                 msgFont.setFamily(Font::SANS_FAMILY);
212                 string const justname = onlyFilename(params_.filename.absFilename());
213
214                 if (!justname.empty()) {
215                         msgFont.setSize(Font::SIZE_FOOTNOTE);
216                         pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
217                                    y - theFontMetrics(msgFont).maxAscent() - 4,
218                                    from_utf8(justname), msgFont);
219                 }
220
221                 // Print the message.
222                 docstring const msg = statusMessage(params_, loader_.status());
223                 if (!msg.empty()) {
224                         msgFont.setSize(Font::SIZE_TINY);
225                         pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
226                                      y - 4, msg, msgFont);
227                 }
228         }
229 }
230
231
232 } // namespace lyx