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