]> git.lyx.org Git - lyx.git/blob - src/insets/graphicinset.C
Clean up InsetGraphics::Cache and rename as GraphicsInset.
[lyx.git] / src / insets / graphicinset.C
1 /**
2  * \file graphicinset.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 "insets/graphicinset.h"
14
15 #include "buffer.h"
16 #include "BufferView.h"
17 #include "gettext.h"
18 #include "metricsinfo.h"
19
20 #include "frontends/font_metrics.h"
21 #include "frontends/LyXView.h"
22 #include "frontends/Painter.h"
23
24 #include "graphics/GraphicsImage.h"
25
26 #include "support/filetools.h"
27
28
29 GraphicInset::GraphicInset()
30         : old_ascent_(0), checksum_(0)
31 {}
32
33
34 GraphicInset::GraphicInset(GraphicInset const & other)
35         : loader_(other.loader_),
36           params_(other.params_),
37           nodisplay_message_(other.nodisplay_message_),
38           old_ascent_(0),
39           checksum_(0)
40 {}
41
42
43 void GraphicInset::update(grfx::Params const & params)
44 {
45         if (!params.filename.empty()) {
46                 lyx::Assert(AbsolutePath(params.filename));
47                 loader_.reset(params_.filename, params_);
48         }
49         params_ = params;
50 }
51
52
53 grfx::Params const & GraphicInset::params() const
54 {
55         return params_;
56 }
57
58
59 bool GraphicInset::hasFileChanged() const
60 {
61         unsigned long const new_checksum = loader_.checksum();
62         bool const file_has_changed = checksum_ != new_checksum;
63         if (file_has_changed)
64                 checksum_ = new_checksum;
65         return file_has_changed;
66 }
67
68
69 BufferView * GraphicInset::view() const
70 {
71         return view_.lock().get();
72 }
73
74
75 boost::signals::connection GraphicInset::connect(slot_type const & slot) const
76 {
77         return loader_.connect(slot);
78 }
79
80
81 void GraphicInset::setNoDisplayMessage(string const & str)
82 {
83         nodisplay_message_ = str;
84 }
85
86
87 string const GraphicInset::statusMessage() const
88 {
89         switch (loader_.status()) {
90                 case grfx::WaitingToLoad:
91                         return _("Not shown.");
92                 case grfx::Loading:
93                         return _("Loading...");
94                 case grfx::Converting:
95                         return _("Converting to loadable format...");
96                 case grfx::Loaded:
97                         return _("Loaded into memory. Must now generate pixmap.");
98                 case grfx::ScalingEtc:
99                         return _("Scaling etc...");
100                 case grfx::Ready:
101                         return _("Ready to display");
102                 case grfx::ErrorNoFile:
103                         return _("No file found!");
104                 case grfx::ErrorConverting:
105                         return _("Error converting to loadable format");
106                 case grfx::ErrorLoading:
107                         return _("Error loading file into memory");
108                 case grfx::ErrorGeneratingPixmap:
109                         return _("Error generating the pixmap");
110                 case grfx::ErrorUnknown:
111                         return _("No image");
112         }
113         return string();
114 }
115
116
117 GraphicInset::DisplayType GraphicInset::displayType() const
118 {
119         if (params_.display == grfx::NoDisplay && !nodisplay_message_.empty())
120                 return NODISPLAY_MESSAGE;
121
122         if (!loader_.image() || loader_.status() != grfx::Ready)
123                 return STATUS_MESSAGE;
124
125         return loader_.image()->isDrawable() ? IMAGE : STATUS_MESSAGE;
126 }
127
128
129 void GraphicInset::metrics(MetricsInfo & mi, Dimension & dim) const
130 {
131         DisplayType type = displayType();
132
133         old_ascent_ = (type == IMAGE) ? loader_.image()->getHeight() : 50;
134
135         dim.asc = old_ascent_;
136         dim.des = 0;
137
138         switch (type) {
139         case IMAGE:
140                 dim.wid = loader_.image()->getWidth() +
141                         2 * Inset::TEXT_TO_INSET_OFFSET;
142                 break;
143
144         case STATUS_MESSAGE:
145         {
146                 int font_width = 0;
147
148                 LyXFont msgFont(mi.base.font);
149                 msgFont.setFamily(LyXFont::SANS_FAMILY);
150
151                 string const justname = OnlyFilename(params_.filename);
152                 if (!justname.empty()) {
153                         msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
154                         font_width = font_metrics::width(justname, msgFont);
155                 }
156
157                 string const msg = statusMessage();
158                 if (!msg.empty()) {
159                         msgFont.setSize(LyXFont::SIZE_TINY);
160                         font_width = std::max(font_width,
161                                               font_metrics::width(msg, msgFont));
162                 }
163
164                 dim.wid = std::max(50, font_width + 15);
165                 break;
166         }
167
168         case NODISPLAY_MESSAGE:
169         {
170                 int font_width = 0;
171
172                 LyXFont msgFont(mi.base.font);
173                 msgFont.setFamily(LyXFont::SANS_FAMILY);
174                 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
175                 font_width = font_metrics::width(nodisplay_message_, msgFont);
176
177                 dim.wid = std::max(50, font_width + 15);
178                 break;
179         }
180         }
181
182         dim_ = dim;
183 }
184
185
186 void GraphicInset::draw(PainterInfo & pi, int x, int y) const
187 {
188         BufferView * bv = pi.base.bv;
189         view_ = bv->owner()->view();
190
191 #if 0
192         // MakeAbsPath returns filename_ unchanged if it is absolute
193         // already.
194         string const file_with_path =
195                 MakeAbsPath(params_.filename, bv->buffer()->filePath());
196
197         // A 'paste' operation creates a new inset with the correct filepath,
198         // but then the 'old' inset stored in the 'copy' operation is actually
199         // added to the buffer.
200
201         // Thus, pasting a graphic into a new buffer with different
202         // buffer->filePath() will result in the image being displayed in LyX even
203         // though the relative path now points at nothing at all. Subsequent
204         // loading of the file into LyX will therefore fail.
205
206         // We should ensure that the filepath is correct.
207         if (file_with_path != loader_.filename()) {
208                 params_.filename = file_with_path;
209                 update(params_);
210         }
211 #endif
212
213         // we may have changed while someone other was drawing us so better
214         // to not draw anything as we surely call to redraw ourself soon.
215         // This is not a nice thing to do and should be fixed properly somehow.
216         // But I still don't know the best way to go. So let's do this like this
217         // for now (Jug 20020311)
218         if (dim_.asc != old_ascent_)
219                 return;
220
221         if (params_.display != grfx::NoDisplay &&
222             loader_.status() == grfx::WaitingToLoad)
223                 loader_.startLoading();
224
225         if (!loader_.monitoring())
226                 loader_.startMonitoring();
227
228         // This will draw the graphics. If the graphics has not been loaded yet,
229         // we draw just a rectangle.
230
231         switch (displayType()) {
232         case IMAGE: 
233         {
234                 pi.pain.image(x + Inset::TEXT_TO_INSET_OFFSET,
235                               y - dim_.asc,
236                               dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
237                               dim_.asc + dim_.des,
238                               *loader_.image());
239                 break;
240         }
241
242         case STATUS_MESSAGE:
243         {
244                 pi.pain.rectangle(x + Inset::TEXT_TO_INSET_OFFSET,
245                                   y - dim_.asc,
246                                   dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
247                                   dim_.asc + dim_.des);
248
249                 // Print the file name.
250                 LyXFont msgFont = pi.base.font;
251                 msgFont.setFamily(LyXFont::SANS_FAMILY);
252                 string const justname = OnlyFilename(params_.filename);
253
254                 if (!justname.empty()) {
255                         msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
256                         pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
257                                    y - font_metrics::maxAscent(msgFont) - 4,
258                                    justname, msgFont);
259                 }
260
261                 // Print the message.
262                 string const msg = statusMessage();
263                 if (!msg.empty()) {
264                         msgFont.setSize(LyXFont::SIZE_TINY);
265                         pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
266                                      y - 4, msg, msgFont);
267                 }
268                 break;
269         }
270
271         case NODISPLAY_MESSAGE:
272         {
273                 pi.pain.rectangle(x + Inset::TEXT_TO_INSET_OFFSET,
274                                   y - dim_.asc,
275                                   dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
276                                   dim_.asc + dim_.des);
277
278                 LyXFont msgFont = pi.base.font;
279                 msgFont.setFamily(LyXFont::SANS_FAMILY);
280                 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
281                 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
282                              y - font_metrics::maxAscent(msgFont) - 4,
283                              nodisplay_message_, msgFont);
284                 break;
285         }
286         }
287 }