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"
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 int ascent(BufferView *, LyXFont const &) const = 0;
162 virtual int descent(BufferView *, LyXFont const &) const = 0;
164 virtual int width(BufferView *, LyXFont const &) const = 0;
166 virtual void draw(BufferView *, LyXFont const &, int baseline, float & x) const = 0;
167 /// update the inset representation
168 virtual void update(BufferView *, bool = false)
170 /// what appears in the minibuffer when opening
171 virtual string const editMessage() const;
173 virtual void edit(BufferView *, int x, int y, mouse_button::state button);
175 virtual void edit(BufferView *, bool front = true);
177 virtual EDITABLE editable() const;
179 virtual bool isTextInset() const { return false; }
180 /// return true if the inset should be removed automatically
181 virtual bool autoDelete() const;
182 /// returns true the inset can hold an inset of given type
183 virtual bool insetAllowed(Inset::Code) const { return false; }
184 /// wrapper around the above
185 bool insetAllowed(Inset * in) const;
187 virtual void write(Buffer const *, std::ostream &) const = 0;
189 virtual void read(Buffer const *, LyXLex & lex) = 0;
190 /** returns the number of rows (\n's) of generated tex code.
191 fragile == true means, that the inset should take care about
192 fragile commands by adding a \protect before.
193 If the free_spc (freespacing) variable is set, then this inset
194 is in a free-spacing paragraph.
196 virtual int latex(Buffer const *, std::ostream &, bool fragile,
197 bool free_spc) const = 0;
199 virtual int ascii(Buffer const *,
200 std::ostream &, int linelen = 0) const = 0;
202 virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
204 virtual int docbook(Buffer const *, std::ostream &, bool) const = 0;
205 /// Updates needed features for this inset.
206 virtual void validate(LaTeXFeatures & features) const;
208 /// returns LyX code associated with the inset. Used for TOC, ...)
209 virtual Inset::Code lyxCode() const { return NO_CODE; }
211 virtual std::vector<string> const getLabelList() const {
212 return std::vector<string>();
216 virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
218 /// returns true to override begin and end inset in file
219 virtual bool directWrite() const;
221 /// Returns true if the inset should be centered alone
222 virtual bool display() const { return false; }
223 /// Changes the display state of the inset
224 virtual void display(bool) {}
226 /// returns true if this inset needs a row on it's own
228 virtual bool needFullRow() const { return false; }
230 void setInsetName(string const & s) { name_ = s; }
232 string const & getInsetName() const { return name_; }
234 void setOwner(Inset * inset) { owner_ = inset; }
236 Inset * owner() const { return owner_; }
238 void parOwner(Paragraph * par) { par_owner_ = par; }
240 Paragraph * parOwner() const { return par_owner_; }
242 void setBackgroundColor(LColor::color);
244 LColor::color backgroundColor() const;
246 int x() const { return top_x; }
248 int y() const { return top_baseline; }
250 // because we could have fake text insets and have to call this
251 // inside them without cast!!!
253 virtual LyXText * getLyXText(BufferView const *,
254 bool const recursive = false) const;
256 virtual void deleteLyXText(BufferView *, bool = true) const {}
258 virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
259 /// returns the actuall scroll-value
260 virtual int scroll(bool recursive=true) const {
261 if (!recursive || !owner_)
266 /// try to get a inset pointer from it's id if we have
267 /// an inset to give back!
268 virtual Inset * getInsetFromID(int /*id*/) const { return 0; }
269 /// if this insets owns paragraphs (f.ex. InsetText) then it
270 /// should return it's very first one!
271 virtual ParagraphList * getParagraphs(int /*num*/) const { return 0; }
273 virtual bool haveParagraphs() const {
277 /// return the cursor if we own one otherwise giv'em just the
278 /// BufferView cursor to work with.
279 virtual LyXCursor const & cursor(BufferView * bview) const;
285 /// used to toggle insets
286 // is the inset open?
287 virtual bool isOpen() const { return false; }
289 virtual void open(BufferView *) {}
291 virtual void close(BufferView *) const {}
292 /// check if the font of the char we want inserting is correct
293 /// and modify it if it is not.
294 virtual bool checkInsertChar(LyXFont &);
295 /// we need this here because collapsed insets are only EDITABLE
296 virtual void setFont(BufferView *, LyXFont const &,
297 bool toggleall = false, bool selectall = false);
299 // needed for spellchecking text
301 virtual bool allowSpellcheck() const { return false; }
303 // should this inset be handled like a normal charater
304 virtual bool isChar() const { return false; }
305 // is this equivalent to a letter?
306 virtual bool isLetter() const { return false; }
307 // is this equivalent to a space (which is BTW different from
308 // a line separator)?
309 virtual bool isSpace() const { return false; }
310 // should we break lines after this inset?
311 virtual bool isLineSeparator() const { return false; }
312 // if this inset has paragraphs should they be output all as default
313 // paragraphs with "Standard" layout?
314 virtual bool forceDefaultParagraphs(Inset const *) const;
315 /** returns true if, when outputing LaTeX, font changes should
316 be closed before generating this inset. This is needed for
317 insets that may contain several paragraphs */
318 virtual bool noFontChange() const { return false; }
320 virtual void getDrawFont(LyXFont &) const {}
321 /* needed for widths which are % of something
322 returns the value of \textwidth in this inset. Most of the
323 time this is the width of the workarea, but if there is a
324 minipage somewhere, it will be the width of this minipage */
325 virtual int latexTextWidth(BufferView *) const;
327 /// mark the inset contents as erased (for change tracking)
328 virtual void markErased() {}
330 /** Adds a LaTeX snippet to the Preview Loader for transformation
331 * into a bitmap image. Does not start the laoding process.
333 * Most insets have no interest in this capability, so the method
336 virtual void addPreview(grfx::PreviewLoader &) const {}
338 /** Find the PreviewLoader, add a LaTeX snippet to it and
339 * start the loading process.
341 * Most insets have no interest in this capability, so the method
344 virtual void generatePreview() const {}
350 mutable int top_baseline;
356 static unsigned int inset_id;
361 /// the paragraph in which this inset has been inserted
362 Paragraph * par_owner_;
366 LColor::color background_color_;
371 bool Inset::insetAllowed(Inset * in) const
373 return insetAllowed(in->lyxCode());
378 bool Inset::checkInsertChar(LyXFont &)
384 * returns true if pointer argument is valid
385 * and points to an editable inset
387 inline bool isEditableInset(Inset const * i)
389 return i && i->editable();
393 * returns true if pointer argument is valid
394 * and points to a highly editable inset
396 inline bool isHighlyEditableInset(Inset const * i)
398 return i && i->editable() == Inset::HIGHLY_EDITABLE;