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