4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Alejandro Aguilar Sierra
9 * \author Lars Gullik Bjønnes
10 * \author Matthias Ettrich
12 * Full author contact details are available in file CREDITS
22 #include "insetbase.h"
23 #include "frontends/mouse_state.h"
24 #include "support/types.h"
43 class Inset : public InsetBase {
45 /** This is not quite the correct place for this enum. I think
46 the correct would be to let each subclass of Inset declare
47 its own enum code. Actually the notion of an Inset::Code
48 should be avoided, but I am not sure how this could be done
54 TOC_CODE, // do these insets really need a code? (ale)
104 SPECIALCHAR_CODE, // 25
116 MATHMACRO_CODE, // 30
138 TEXT_TO_INSET_OFFSET = 2
152 typedef dispatch_result RESULT;
157 Inset(Inset const & in, bool same_id = false);
159 virtual int ascent(BufferView *, LyXFont const &) const = 0;
161 virtual int descent(BufferView *, LyXFont const &) const = 0;
163 virtual int width(BufferView *, LyXFont const &) const = 0;
165 virtual void draw(BufferView *, LyXFont const &, int baseline, float & x) const = 0;
166 /// update the inset representation
167 virtual void update(BufferView *, bool = false)
169 /// what appears in the minibuffer when opening
170 virtual string const editMessage() const;
172 virtual void edit(BufferView *, int x, int y, mouse_button::state button);
174 virtual void edit(BufferView *, bool front = true);
176 virtual EDITABLE editable() const;
178 virtual bool isTextInset() const { return false; }
179 /// return true if the inset should be removed automatically
180 virtual bool autoDelete() const;
181 /// returns true the inset can hold an inset of given type
182 virtual bool insetAllowed(Inset::Code) const { return false; }
183 /// wrapper around the above
184 bool insetAllowed(Inset * in) const;
186 virtual void write(Buffer const *, std::ostream &) const = 0;
188 virtual void read(Buffer const *, LyXLex & lex) = 0;
189 /** returns the number of rows (\n's) of generated tex code.
190 fragile == true means, that the inset should take care about
191 fragile commands by adding a \protect before.
192 If the free_spc (freespacing) variable is set, then this inset
193 is in a free-spacing paragraph.
195 virtual int latex(Buffer const *, std::ostream &, bool fragile,
196 bool free_spc) const = 0;
198 virtual int ascii(Buffer const *,
199 std::ostream &, int linelen = 0) const = 0;
201 virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
203 virtual int docbook(Buffer const *, std::ostream &, bool) const = 0;
204 /// Updates needed features for this inset.
205 virtual void validate(LaTeXFeatures & features) const;
207 /// returns LyX code associated with the inset. Used for TOC, ...)
208 virtual Inset::Code lyxCode() const { return NO_CODE; }
210 virtual std::vector<string> const getLabelList() const {
211 return std::vector<string>();
215 virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
217 /// returns true to override begin and end inset in file
218 virtual bool directWrite() const;
220 /// Returns true if the inset should be centered alone
221 virtual bool display() const { return false; }
222 /// Changes the display state of the inset
223 virtual void display(bool) {}
225 /// returns true if this inset needs a row on it's own
227 virtual bool needFullRow() const { return false; }
229 void setInsetName(string const & s) { name_ = s; }
231 string const & getInsetName() const { return name_; }
233 void setOwner(Inset * inset) { owner_ = inset; }
235 Inset * owner() const { return owner_; }
237 void parOwner(Paragraph * par) { par_owner_ = par; }
239 Paragraph * parOwner() const { return par_owner_; }
241 void setBackgroundColor(LColor::color);
243 LColor::color backgroundColor() const;
245 int x() const { return top_x; }
247 int y() const { return top_baseline; }
249 // because we could have fake text insets and have to call this
250 // inside them without cast!!!
252 virtual LyXText * getLyXText(BufferView const *,
253 bool const recursive = false) const;
255 virtual void deleteLyXText(BufferView *, bool = true) const {}
257 virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
258 /// returns the actuall scroll-value
259 virtual int scroll(bool recursive=true) const {
260 if (!recursive || !owner_)
265 /// try to get a inset pointer from it's id if we have
266 /// an inset to give back!
267 virtual Inset * getInsetFromID(int /*id*/) const { return 0; }
268 /// if this insets owns paragraphs (f.ex. InsetText) then it
269 /// should return it's very first one!
270 virtual Paragraph * firstParagraph() const { return 0; }
272 virtual Paragraph * getFirstParagraph(int /*num*/) const { return 0; }
274 /// return the cursor if we own one otherwise giv'em just the
275 /// BufferView cursor to work with.
276 virtual LyXCursor const & cursor(BufferView * bview) const;
282 /// used to toggle insets
283 // is the inset open?
284 virtual bool isOpen() const { return false; }
286 virtual void open(BufferView *) {}
288 virtual void close(BufferView *) const {}
289 /// check if the font of the char we want inserting is correct
290 /// and modify it if it is not.
291 virtual bool checkInsertChar(LyXFont &);
292 /// we need this here because collapsed insets are only EDITABLE
293 virtual void setFont(BufferView *, LyXFont const &,
294 bool toggleall = false, bool selectall = false);
296 // needed for spellchecking text
298 virtual bool allowSpellcheck() const { return false; }
300 // should this inset be handled like a normal charater
301 virtual bool isChar() const { return false; }
302 // is this equivalent to a letter?
303 virtual bool isLetter() const { return false; }
304 // is this equivalent to a space (which is BTW different from
305 // a line separator)?
306 virtual bool isSpace() const { return false; }
307 // should we break lines after this inset?
308 virtual bool isLineSeparator() const { return false; }
309 // if this inset has paragraphs should they be output all as default
310 // paragraphs with "Standard" layout?
311 virtual bool forceDefaultParagraphs(Inset const *) const;
312 /** returns true if, when outputing LaTeX, font changes should
313 be closed before generating this inset. This is needed for
314 insets that may contain several paragraphs */
315 virtual bool noFontChange() const { return false; }
317 virtual void getDrawFont(LyXFont &) const {}
318 /* needed for widths which are % of something
319 returns the value of \textwidth in this inset. Most of the
320 time this is the width of the workarea, but if there is a
321 minipage somewhere, it will be the width of this minipage */
322 virtual int latexTextWidth(BufferView *) const;
324 /// mark the inset contents as erased (for change tracking)
325 virtual void markErased() {}
327 /** Adds a LaTeX snippet to the Preview Loader for transformation
328 * into a bitmap image. Does not start the laoding process.
330 * Most insets have no interest in this capability, so the method
333 virtual void addPreview(grfx::PreviewLoader &) const {}
335 /** Find the PreviewLoader, add a LaTeX snippet to it and
336 * start the loading process.
338 * Most insets have no interest in this capability, so the method
341 virtual void generatePreview() const {}
347 mutable int top_baseline;
353 static unsigned int inset_id;
358 /// the paragraph in which this inset has been inserted
359 Paragraph * par_owner_;
363 LColor::color background_color_;
368 bool Inset::insetAllowed(Inset * in) const
370 return insetAllowed(in->lyxCode());
375 bool Inset::checkInsertChar(LyXFont &)
381 * returns true if pointer argument is valid
382 * and points to an editable inset
384 inline bool isEditableInset(Inset const * i)
386 return i && i->editable();
390 * returns true if pointer argument is valid
391 * and points to a highly editable inset
393 inline bool isHighlyEditableInset(Inset const * i)
395 return i && i->editable() == Inset::HIGHLY_EDITABLE;