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