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
132 TEXT_TO_INSET_OFFSET = 2
146 typedef dispatch_result RESULT;
151 Inset(Inset const & in, bool same_id = false);
153 virtual int ascent(BufferView *, LyXFont const &) const = 0;
155 virtual int descent(BufferView *, LyXFont const &) const = 0;
157 virtual int width(BufferView *, LyXFont const &) const = 0;
159 virtual void draw(BufferView *, LyXFont const &,
160 int baseline, float & x, bool cleared) const = 0;
161 /// update the inset representation
162 virtual void update(BufferView *, LyXFont const &, bool = false)
164 /// what appears in the minibuffer when opening
165 virtual string const editMessage() const;
167 virtual void edit(BufferView *, int x, int y, mouse_button::state button);
169 virtual void edit(BufferView *, bool front = true);
171 virtual EDITABLE editable() const;
173 virtual bool isTextInset() const { return false; }
175 virtual bool doClearArea() const { return true; }
176 /// return true if the inset should be removed automatically
177 virtual bool autoDelete() const;
178 /// returns true the inset can hold an inset of given type
179 virtual bool insetAllowed(Inset::Code) const { return false; }
180 /// wrapper around the above
181 bool insetAllowed(Inset * in) const;
183 virtual void write(Buffer const *, std::ostream &) const = 0;
185 virtual void read(Buffer const *, LyXLex & lex) = 0;
186 /** returns the number of rows (\n's) of generated tex code.
187 fragile == true means, that the inset should take care about
188 fragile commands by adding a \protect before.
189 If the free_spc (freespacing) variable is set, then this inset
190 is in a free-spacing paragraph.
192 virtual int latex(Buffer const *, std::ostream &, bool fragile,
193 bool free_spc) const = 0;
195 virtual int ascii(Buffer const *,
196 std::ostream &, int linelen = 0) const = 0;
198 virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
200 virtual int docbook(Buffer const *, std::ostream &, bool) const = 0;
201 /// Updates needed features for this inset.
202 virtual void validate(LaTeXFeatures & features) const;
204 /// returns LyX code associated with the inset. Used for TOC, ...)
205 virtual Inset::Code lyxCode() const { return NO_CODE; }
207 virtual std::vector<string> const getLabelList() const {
208 return std::vector<string>();
212 virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
214 /// returns true to override begin and end inset in file
215 virtual bool directWrite() const;
217 /// Returns true if the inset should be centered alone
218 virtual bool display() const { return false; }
219 /// Changes the display state of the inset
220 virtual void display(bool) {}
222 /// returns true if this inset needs a row on it's own
224 virtual bool needFullRow() const { return false; }
226 void setInsetName(string const & s) { name_ = s; }
228 string const & getInsetName() const { return name_; }
230 void setOwner(Inset * inset) { owner_ = inset; }
232 Inset * owner() const { return owner_; }
234 void parOwner(Paragraph * par) { par_owner_ = par; }
236 Paragraph * parOwner() const { return par_owner_; }
238 void setBackgroundColor(LColor::color);
240 LColor::color backgroundColor() const;
242 int x() const { return top_x; }
244 int y() const { return top_baseline; }
246 // because we could have fake text insets and have to call this
247 // inside them without cast!!!
249 virtual LyXText * getLyXText(BufferView const *,
250 bool const recursive = false) const;
252 virtual void deleteLyXText(BufferView *, bool = true) const {}
254 virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
255 /// returns the actuall scroll-value
256 virtual int scroll(bool recursive=true) const {
257 if (!recursive || !owner_)
262 /// try to get a inset pointer from it's id if we have
263 /// an inset to give back!
264 virtual Inset * getInsetFromID(int /*id*/) const { return 0; }
265 /// if this insets owns paragraphs (f.ex. InsetText) then it
266 /// should return it's very first one!
267 virtual Paragraph * firstParagraph() const { return 0; }
269 virtual Paragraph * getFirstParagraph(int /*num*/) const { return 0; }
271 /// return the cursor if we own one otherwise giv'em just the
272 /// BufferView cursor to work with.
273 virtual LyXCursor const & cursor(BufferView * bview) const;
279 /// used to toggle insets
280 // is the inset open?
281 virtual bool isOpen() const { return false; }
283 virtual void open(BufferView *) {}
285 virtual void close(BufferView *) const {}
286 /// check if the font of the char we want inserting is correct
287 /// and modify it if it is not.
288 virtual bool checkInsertChar(LyXFont &);
289 /// we need this here because collapsed insets are only EDITABLE
290 virtual void setFont(BufferView *, LyXFont const &,
291 bool toggleall = false, bool selectall = false);
293 // needed for spellchecking text
295 virtual bool allowSpellcheck() const { return false; }
297 // should this inset be handled like a normal charater
298 virtual bool isChar() const { return false; }
299 // is this equivalent to a letter?
300 virtual bool isLetter() const { return false; }
301 // is this equivalent to a space (which is BTW different from
302 // a line separator)?
303 virtual bool isSpace() const { return false; }
304 // should we break lines after this inset?
305 virtual bool isLineSeparator() const { return false; }
306 // if this inset has paragraphs should they be output all as default
307 // paragraphs with "Standard" layout?
308 virtual bool forceDefaultParagraphs(Inset const *) const;
309 /** returns true if, when outputing LaTeX, font changes should
310 be closed before generating this inset. This is needed for
311 insets that may contain several paragraphs */
312 virtual bool noFontChange() const { return false; }
314 virtual void getDrawFont(LyXFont &) const {}
315 /* needed for widths which are % of something
316 returns the value of \textwidth in this inset. Most of the
317 time this is the width of the workarea, but if there is a
318 minipage somewhere, it will be the width of this minipage */
319 virtual int latexTextWidth(BufferView *) const;
321 /// mark the inset contents as erased (for change tracking)
322 virtual void markErased() {}
324 /** Adds a LaTeX snippet to the Preview Loader for transformation
325 * into a bitmap image. Does not start the laoding process.
327 * Most insets have no interest in this capability, so the method
330 virtual void addPreview(grfx::PreviewLoader &) const {}
332 /** Find the PreviewLoader, add a LaTeX snippet to it and
333 * start the loading process.
335 * Most insets have no interest in this capability, so the method
338 virtual void generatePreview() const {}
344 mutable bool topx_set; /* have we already drawn ourself! */
346 mutable int top_baseline;
352 static unsigned int inset_id;
357 /// the paragraph in which this inset has been inserted
358 Paragraph * par_owner_;
362 LColor::color background_color_;
367 bool Inset::insetAllowed(Inset * in) const
369 return insetAllowed(in->lyxCode());
374 bool Inset::checkInsertChar(LyXFont &)
380 * returns true if pointer argument is valid
381 * and points to an editable inset
383 inline bool isEditableInset(Inset const * i)
385 return i && i->editable();
389 * returns true if pointer argument is valid
390 * and points to a highly editable inset
392 inline bool isHighlyEditableInset(Inset const * i)
394 return i && i->editable() == Inset::HIGHLY_EDITABLE;