]> git.lyx.org Git - lyx.git/blob - src/insets/render_graphic.C
Move a couple of private member functions into namespace anon.
[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 "metricsinfo.h"
20
21 #include "frontends/font_metrics.h"
22 #include "frontends/Painter.h"
23
24 #include "graphics/GraphicsImage.h"
25
26 #include "support/filetools.h"
27
28 namespace graphics = lyx::graphics;
29
30 using lyx::support::AbsolutePath;
31 using lyx::support::OnlyFilename;
32
33 using std::string;
34
35
36 RenderGraphic::RenderGraphic()
37         : checksum_(0)
38 {}
39
40
41 RenderGraphic::RenderGraphic(RenderGraphic const & other)
42         : RenderBase(other),
43           loader_(other.loader_),
44           params_(other.params_),
45           checksum_(0)
46 {}
47
48
49 RenderBase * RenderGraphic::clone() const
50 {
51         return new RenderGraphic(*this);
52 }
53
54
55 void RenderGraphic::update(graphics::Params const & params)
56 {
57         params_ = params;
58
59         if (!params_.filename.empty()) {
60                 BOOST_ASSERT(AbsolutePath(params_.filename));
61                 loader_.reset(params_.filename, params_);
62         }
63 }
64
65
66 bool RenderGraphic::hasFileChanged() const
67 {
68         unsigned long const new_checksum = loader_.checksum();
69         bool const file_has_changed = checksum_ != new_checksum;
70         if (file_has_changed)
71                 checksum_ = new_checksum;
72         return file_has_changed;
73 }
74
75
76 boost::signals::connection RenderGraphic::connect(slot_type const & slot) const
77 {
78         return loader_.connect(slot);
79 }
80
81
82 namespace {
83  
84 string const statusMessage(graphics::ImageStatus status)
85 {
86         switch (status) {
87         case graphics::WaitingToLoad:
88                 return _("Not shown.");
89         case graphics::Loading:
90                 return _("Loading...");
91         case graphics::Converting:
92                 return _("Converting to loadable format...");
93         case graphics::Loaded:
94                 return _("Loaded into memory. Must now generate pixmap.");
95         case graphics::ScalingEtc:
96                 return _("Scaling etc...");
97         case graphics::Ready:
98                 return _("Ready to display");
99         case graphics::ErrorNoFile:
100                 return _("No file found!");
101         case graphics::ErrorConverting:
102                 return _("Error converting to loadable format");
103         case graphics::ErrorLoading:
104                 return _("Error loading file into memory");
105         case graphics::ErrorGeneratingPixmap:
106                 return _("Error generating the pixmap");
107         case graphics::ErrorUnknown:
108                 return _("No image");
109         }
110         return string();
111 }
112
113
114 bool readyToDisplay(graphics::Loader const & loader)
115 {
116         if (!loader.image() || loader.status() != graphics::Ready)
117                 return false;
118         return loader.image()->isDrawable();
119 }
120
121 } // namespace anon
122
123
124 void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
125 {
126         bool image_ready = readyToDisplay(loader_);
127
128         dim.asc = image_ready ? loader_.image()->getHeight() : 50;
129         dim.des = 0;
130
131         if (image_ready) {
132                 dim.wid = loader_.image()->getWidth() +
133                         2 * InsetOld::TEXT_TO_INSET_OFFSET;
134         } else {
135                 int font_width = 0;
136
137                 LyXFont msgFont(mi.base.font);
138                 msgFont.setFamily(LyXFont::SANS_FAMILY);
139
140                 string const justname = OnlyFilename(params_.filename);
141                 if (!justname.empty()) {
142                         msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
143                         font_width = font_metrics::width(justname, msgFont);
144                 }
145
146                 string const msg = statusMessage(loader_.status());
147                 if (!msg.empty()) {
148                         msgFont.setSize(LyXFont::SIZE_TINY);
149                         font_width = std::max(font_width,
150                                               font_metrics::width(msg, msgFont));
151                 }
152
153                 dim.wid = std::max(50, font_width + 15);
154         }
155
156         dim_ = dim;
157 }
158
159
160 void RenderGraphic::draw(PainterInfo & pi, int x, int y) const
161 {
162         if (params_.display != graphics::NoDisplay &&
163             loader_.status() == graphics::WaitingToLoad)
164                 loader_.startLoading();
165
166         if (params_.display != 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(loader_)) {
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(loader_.status());
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 }