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