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