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.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
13 #include "render_graphic.h"
15 #include "insets/inset.h"
17 #include "BufferView.h"
20 #include "metricsinfo.h"
22 #include "frontends/font_metrics.h"
23 #include "frontends/LyXView.h"
24 #include "frontends/Painter.h"
26 #include "graphics/GraphicsImage.h"
28 #include "support/filetools.h"
31 using lyx::support::AbsolutePath;
32 using lyx::support::OnlyFilename;
37 RenderGraphic::RenderGraphic()
42 RenderGraphic::RenderGraphic(RenderGraphic const & other)
44 loader_(other.loader_),
45 params_(other.params_),
50 RenderBase * RenderGraphic::clone() const
52 return new RenderGraphic(*this);
56 void RenderGraphic::update(lyx::graphics::Params const & params)
60 if (!params_.filename.empty()) {
61 BOOST_ASSERT(AbsolutePath(params_.filename));
62 loader_.reset(params_.filename, params_);
67 bool RenderGraphic::hasFileChanged() const
69 unsigned long const new_checksum = loader_.checksum();
70 bool const file_has_changed = checksum_ != new_checksum;
72 checksum_ = new_checksum;
73 return file_has_changed;
77 boost::signals::connection RenderGraphic::connect(slot_type const & slot) const
79 return loader_.connect(slot);
83 string const RenderGraphic::statusMessage() const
85 switch (loader_.status()) {
86 case lyx::graphics::WaitingToLoad:
87 return _("Not shown.");
88 case lyx::graphics::Loading:
89 return _("Loading...");
90 case lyx::graphics::Converting:
91 return _("Converting to loadable format...");
92 case lyx::graphics::Loaded:
93 return _("Loaded into memory. Must now generate pixmap.");
94 case lyx::graphics::ScalingEtc:
95 return _("Scaling etc...");
96 case lyx::graphics::Ready:
97 return _("Ready to display");
98 case lyx::graphics::ErrorNoFile:
99 return _("No file found!");
100 case lyx::graphics::ErrorConverting:
101 return _("Error converting to loadable format");
102 case lyx::graphics::ErrorLoading:
103 return _("Error loading file into memory");
104 case lyx::graphics::ErrorGeneratingPixmap:
105 return _("Error generating the pixmap");
106 case lyx::graphics::ErrorUnknown:
107 return _("No image");
113 bool RenderGraphic::readyToDisplay() const
115 if (!loader_.image() || loader_.status() != lyx::graphics::Ready)
117 return loader_.image()->isDrawable();
121 void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
123 bool image_ready = readyToDisplay();
125 dim.asc = image_ready ? loader_.image()->getHeight() : 50;
129 dim.wid = loader_.image()->getWidth() +
130 2 * InsetOld::TEXT_TO_INSET_OFFSET;
134 LyXFont msgFont(mi.base.font);
135 msgFont.setFamily(LyXFont::SANS_FAMILY);
137 string const justname = OnlyFilename(params_.filename);
138 if (!justname.empty()) {
139 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
140 font_width = font_metrics::width(justname, msgFont);
143 string const msg = statusMessage();
145 msgFont.setSize(LyXFont::SIZE_TINY);
146 font_width = std::max(font_width,
147 font_metrics::width(msg, msgFont));
150 dim.wid = std::max(50, font_width + 15);
157 void RenderGraphic::draw(PainterInfo & pi, int x, int y) const
159 BOOST_ASSERT(pi.base.bv);
160 view_ = pi.base.bv->owner()->view();
162 if (params_.display != lyx::graphics::NoDisplay &&
163 loader_.status() == lyx::graphics::WaitingToLoad)
164 loader_.startLoading();
166 if (params_.display != lyx::graphics::NoDisplay &&
167 !loader_.monitoring())
168 loader_.startMonitoring();
170 // This will draw the graphics. If the graphics has not been loaded yet,
171 // we draw just a rectangle.
173 if (readyToDisplay()) {
174 pi.pain.image(x + InsetOld::TEXT_TO_INSET_OFFSET,
176 dim_.wid - 2 * InsetOld::TEXT_TO_INSET_OFFSET,
181 pi.pain.rectangle(x + InsetOld::TEXT_TO_INSET_OFFSET,
183 dim_.wid - 2 * InsetOld::TEXT_TO_INSET_OFFSET,
187 // Print the file name.
188 LyXFont msgFont = pi.base.font;
189 msgFont.setFamily(LyXFont::SANS_FAMILY);
190 string const justname = OnlyFilename(params_.filename);
192 if (!justname.empty()) {
193 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
194 pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,
195 y - font_metrics::maxAscent(msgFont) - 4,
199 // Print the message.
200 string const msg = statusMessage();
202 msgFont.setSize(LyXFont::SIZE_TINY);
203 pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,
204 y - 4, msg, msgFont);