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
20 #include "insetbase.h"
21 #include "support/types.h"
44 class Inset : public InsetBase {
46 /** This is not quite the correct place for this enum. I think
47 the correct would be to let each subclass of Inset declare
48 its own enum code. Actually the notion of an Inset::Code
49 should be avoided, but I am not sure how this could be done
55 TOC_CODE, // do these insets really need a code? (ale)
105 SPECIALCHAR_CODE, // 25
117 MATHMACRO_CODE, // 30
139 TEXT_TO_INSET_OFFSET = 2
153 typedef dispatch_result RESULT;
158 Inset(Inset const & in, bool same_id = false);
160 virtual void dimension(BufferView *, LyXFont const &, Dimension &) const = 0;
162 int ascent(BufferView *, LyXFont const &) const;
164 int descent(BufferView *, LyXFont const &) const;
166 int width(BufferView *, LyXFont const &) const;
168 virtual void draw(BufferView *, LyXFont const &, int baseline, float & x) const = 0;
169 /// update the inset representation
170 virtual void update(BufferView *, bool = false)
172 /// what appears in the minibuffer when opening
173 virtual string const editMessage() const;
175 virtual EDITABLE editable() const;
177 virtual bool isTextInset() const { return false; }
178 /// return true if the inset should be removed automatically
179 virtual bool autoDelete() const;
180 /// returns true the inset can hold an inset of given type
181 virtual bool insetAllowed(Inset::Code) const { return false; }
182 /// wrapper around the above
183 bool insetAllowed(Inset * in) const;
185 virtual void write(Buffer const *, std::ostream &) const = 0;
187 virtual void read(Buffer const *, LyXLex & lex) = 0;
188 /** returns the number of rows (\n's) of generated tex code.
189 fragile == true means, that the inset should take care about
190 fragile commands by adding a \protect before.
191 If the free_spc (freespacing) variable is set, then this inset
192 is in a free-spacing paragraph.
194 virtual int latex(Buffer const *, std::ostream &, bool fragile,
195 bool free_spc) const = 0;
197 virtual int ascii(Buffer const *,
198 std::ostream &, int linelen = 0) const = 0;
200 virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
202 virtual int docbook(Buffer const *, std::ostream &, bool) const = 0;
203 /// Updates needed features for this inset.
204 virtual void validate(LaTeXFeatures & features) const;
206 /// returns LyX code associated with the inset. Used for TOC, ...)
207 virtual Inset::Code lyxCode() const { return NO_CODE; }
209 virtual std::vector<string> const getLabelList() const {
210 return std::vector<string>();
214 virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
216 /// returns true to override begin and end inset in file
217 virtual bool directWrite() const;
219 /// Returns true if the inset should be centered alone
220 virtual bool display() const { return false; }
221 /// Changes the display state of the inset
222 virtual void display(bool) {}
224 /// returns true if this inset needs a row on it's own
226 virtual bool needFullRow() const { return false; }
228 void setInsetName(string const & s) { name_ = s; }
230 string const & getInsetName() const { return name_; }
232 void setOwner(Inset * inset) { owner_ = inset; }
234 Inset * owner() const { return owner_; }
236 void parOwner(Paragraph * par) { par_owner_ = par; }
238 Paragraph * parOwner() const { return par_owner_; }
240 void setBackgroundColor(LColor::color);
242 LColor::color backgroundColor() const;
244 int x() const { return top_x; }
246 int y() const { return top_baseline; }
248 // because we could have fake text insets and have to call this
249 // inside them without cast!!!
251 virtual LyXText * getLyXText(BufferView const *,
252 bool const recursive = false) const;
254 virtual void deleteLyXText(BufferView *, bool = true) const {}
256 virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
257 /// returns the actuall scroll-value
258 virtual int scroll(bool recursive=true) const {
259 if (!recursive || !owner_)
264 /// try to get a inset pointer from it's id if we have
265 /// an inset to give back!
266 virtual Inset * getInsetFromID(int /*id*/) const { return 0; }
267 /// if this insets owns paragraphs (f.ex. InsetText) then it
268 /// should return it's very first one!
269 virtual ParagraphList * getParagraphs(int /*num*/) const { return 0; }
271 virtual bool haveParagraphs() const {
275 /// return the cursor if we own one otherwise giv'em just the
276 /// BufferView cursor to work with.
277 virtual LyXCursor const & cursor(BufferView * bview) const;
283 /// used to toggle insets
284 // is the inset open?
285 virtual bool isOpen() const { return false; }
287 virtual void open(BufferView *) {}
289 virtual void close(BufferView *) const {}
290 /// check if the font of the char we want inserting is correct
291 /// and modify it if it is not.
292 virtual bool checkInsertChar(LyXFont &);
293 /// we need this here because collapsed insets are only EDITABLE
294 virtual void setFont(BufferView *, LyXFont const &,
295 bool toggleall = false, bool selectall = false);
297 // needed for spellchecking text
299 virtual bool allowSpellcheck() const { return false; }
301 // should this inset be handled like a normal charater
302 virtual bool isChar() const { return false; }
303 // is this equivalent to a letter?
304 virtual bool isLetter() const { return false; }
305 // is this equivalent to a space (which is BTW different from
306 // a line separator)?
307 virtual bool isSpace() const { return false; }
308 // should we break lines after this inset?
309 virtual bool isLineSeparator() const { return false; }
310 // if this inset has paragraphs should they be output all as default
311 // paragraphs with "Standard" layout?
312 virtual bool forceDefaultParagraphs(Inset const *) const;
313 /** returns true if, when outputing LaTeX, font changes should
314 be closed before generating this inset. This is needed for
315 insets that may contain several paragraphs */
316 virtual bool noFontChange() const { return false; }
318 virtual void getDrawFont(LyXFont &) const {}
319 /* needed for widths which are % of something
320 returns the value of \textwidth in this inset. Most of the
321 time this is the width of the workarea, but if there is a
322 minipage somewhere, it will be the width of this minipage */
323 virtual int latexTextWidth(BufferView *) const;
325 /// mark the inset contents as erased (for change tracking)
326 virtual void markErased() {}
328 /** Adds a LaTeX snippet to the Preview Loader for transformation
329 * into a bitmap image. Does not start the laoding process.
331 * Most insets have no interest in this capability, so the method
334 virtual void addPreview(grfx::PreviewLoader &) const {}
336 /** Find the PreviewLoader, add a LaTeX snippet to it and
337 * start the loading process.
339 * Most insets have no interest in this capability, so the method
342 virtual void generatePreview() const {}
348 mutable int top_baseline;
354 static unsigned int inset_id;
359 /// the paragraph in which this inset has been inserted
360 Paragraph * par_owner_;
364 LColor::color background_color_;
369 bool Inset::insetAllowed(Inset * in) const
371 return insetAllowed(in->lyxCode());
376 bool Inset::checkInsertChar(LyXFont &)
382 * returns true if pointer argument is valid
383 * and points to an editable inset
385 inline bool isEditableInset(Inset const * i)
387 return i && i->editable();
391 * returns true if pointer argument is valid
392 * and points to a highly editable inset
394 inline bool isHighlyEditableInset(Inset const * i)
396 return i && i->editable() == Inset::HIGHLY_EDITABLE;