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