]> git.lyx.org Git - lyx.git/blob - src/insets/render_graphic.C
RenderButton, RenderGraphic and RenderPreview now have a common lineage.
[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 "BufferView.h"
18 #include "gettext.h"
19 #include "LColor.h"
20 #include "metricsinfo.h"
21
22 #include "frontends/font_metrics.h"
23 #include "frontends/LyXView.h"
24 #include "frontends/Painter.h"
25
26 #include "graphics/GraphicsImage.h"
27
28 #include "support/filetools.h"
29
30
31 using lyx::support::AbsolutePath;
32 using lyx::support::OnlyFilename;
33
34 using std::string;
35
36
37 RenderGraphic::RenderGraphic()
38         : checksum_(0)
39 {}
40
41
42 RenderGraphic::RenderGraphic(RenderGraphic const & other)
43         : RenderBase(other),
44           loader_(other.loader_),
45           params_(other.params_),
46           checksum_(0)
47 {}
48
49
50 RenderBase * RenderGraphic::clone() const
51 {
52         return new RenderGraphic(*this);
53 }
54
55
56 void RenderGraphic::update(lyx::graphics::Params const & params)
57 {
58         params_ = params;
59
60         if (!params_.filename.empty()) {
61                 BOOST_ASSERT(AbsolutePath(params_.filename));
62                 loader_.reset(params_.filename, params_);
63         }
64 }
65
66
67 bool RenderGraphic::hasFileChanged() const
68 {
69         unsigned long const new_checksum = loader_.checksum();
70         bool const file_has_changed = checksum_ != new_checksum;
71         if (file_has_changed)
72                 checksum_ = new_checksum;
73         return file_has_changed;
74 }
75
76
77 boost::signals::connection RenderGraphic::connect(slot_type const & slot) const
78 {
79         return loader_.connect(slot);
80 }
81
82
83 string const RenderGraphic::statusMessage() const
84 {
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");
108         }
109         return string();
110 }
111
112
113 bool RenderGraphic::readyToDisplay() const
114 {
115         if (!loader_.image() || loader_.status() != lyx::graphics::Ready)
116                 return false;
117         return loader_.image()->isDrawable();
118 }
119
120
121 void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
122 {
123         bool image_ready = readyToDisplay();
124
125         dim.asc = image_ready ? loader_.image()->getHeight() : 50;
126         dim.des = 0;
127
128         if (image_ready) {
129                 dim.wid = loader_.image()->getWidth() +
130                         2 * InsetOld::TEXT_TO_INSET_OFFSET;
131         } else {
132                 int font_width = 0;
133
134                 LyXFont msgFont(mi.base.font);
135                 msgFont.setFamily(LyXFont::SANS_FAMILY);
136
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);
141                 }
142
143                 string const msg = statusMessage();
144                 if (!msg.empty()) {
145                         msgFont.setSize(LyXFont::SIZE_TINY);
146                         font_width = std::max(font_width,
147                                               font_metrics::width(msg, msgFont));
148                 }
149
150                 dim.wid = std::max(50, font_width + 15);
151         }
152
153         dim_ = dim;
154 }
155
156
157 void RenderGraphic::draw(PainterInfo & pi, int x, int y) const
158 {
159         BOOST_ASSERT(pi.base.bv);
160         view_ = pi.base.bv->owner()->view();
161
162         if (params_.display != lyx::graphics::NoDisplay &&
163             loader_.status() == lyx::graphics::WaitingToLoad)
164                 loader_.startLoading();
165
166         if (params_.display != lyx::graphics::NoDisplay &&
167             !loader_.monitoring())
168                 loader_.startMonitoring();
169
170         // This will draw the graphics. If the graphics has not been loaded yet,
171         // we draw just a rectangle.
172
173         if (readyToDisplay()) {
174                 pi.pain.image(x + InsetOld::TEXT_TO_INSET_OFFSET,
175                               y - dim_.asc,
176                               dim_.wid - 2 * InsetOld::TEXT_TO_INSET_OFFSET,
177                               dim_.asc + dim_.des,
178                               *loader_.image());
179
180         } else {
181                 pi.pain.rectangle(x + InsetOld::TEXT_TO_INSET_OFFSET,
182                                   y - dim_.asc,
183                                   dim_.wid - 2 * InsetOld::TEXT_TO_INSET_OFFSET,
184                                   dim_.asc + dim_.des,
185                                   LColor::foreground);
186
187                 // Print the file name.
188                 LyXFont msgFont = pi.base.font;
189                 msgFont.setFamily(LyXFont::SANS_FAMILY);
190                 string const justname = OnlyFilename(params_.filename);
191
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,
196                                    justname, msgFont);
197                 }
198
199                 // Print the message.
200                 string const msg = statusMessage();
201                 if (!msg.empty()) {
202                         msgFont.setSize(LyXFont::SIZE_TINY);
203                         pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,
204                                      y - 4, msg, msgFont);
205                 }
206         }
207 }