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 &,
166 int baseline, float & x, bool cleared) const = 0;
167 /// update the inset representation
168 virtual void update(BufferView *, LyXFont const &, 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; }
181 virtual bool doClearArea() const { return true; }
182 /// return true if the inset should be removed automatically
183 virtual bool autoDelete() const;
184 /// returns true the inset can hold an inset of given type
185 virtual bool insetAllowed(Inset::Code) const { return false; }
186 /// wrapper around the above
187 bool insetAllowed(Inset * in) const;
189 virtual void write(Buffer const *, std::ostream &) const = 0;
191 virtual void read(Buffer const *, LyXLex & lex) = 0;
192 /** returns the number of rows (\n's) of generated tex code.
193 fragile == true means, that the inset should take care about
194 fragile commands by adding a \protect before.
195 If the free_spc (freespacing) variable is set, then this inset
196 is in a free-spacing paragraph.
198 virtual int latex(Buffer const *, std::ostream &, bool fragile,
199 bool free_spc) const = 0;
201 virtual int ascii(Buffer const *,
202 std::ostream &, int linelen = 0) const = 0;
204 virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
206 virtual int docbook(Buffer const *, std::ostream &, bool) const = 0;
207 /// Updates needed features for this inset.
208 virtual void validate(LaTeXFeatures & features) const;
210 /// returns LyX code associated with the inset. Used for TOC, ...)
211 virtual Inset::Code lyxCode() const { return NO_CODE; }
213 virtual std::vector<string> const getLabelList() const {
214 return std::vector<string>();
218 virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
220 /// returns true to override begin and end inset in file
221 virtual bool directWrite() const;
223 /// Returns true if the inset should be centered alone
224 virtual bool display() const { return false; }
225 /// Changes the display state of the inset
226 virtual void display(bool) {}
228 /// returns true if this inset needs a row on it's own
230 virtual bool needFullRow() const { return false; }
232 void setInsetName(string const & s) { name_ = s; }
234 string const & getInsetName() const { return name_; }
236 void setOwner(Inset * inset) { owner_ = inset; }
238 Inset * owner() const { return owner_; }
240 void parOwner(Paragraph * par) { par_owner_ = par; }
242 Paragraph * parOwner() const { return par_owner_; }
244 void setBackgroundColor(LColor::color);
246 LColor::color backgroundColor() const;
248 int x() const { return top_x; }
250 int y() const { return top_baseline; }
252 // because we could have fake text insets and have to call this
253 // inside them without cast!!!
255 virtual LyXText * getLyXText(BufferView const *,
256 bool const recursive = false) const;
258 virtual void deleteLyXText(BufferView *, bool = true) const {}
260 virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
261 /// returns the actuall scroll-value
262 virtual int scroll(bool recursive=true) const {
263 if (!recursive || !owner_)
268 /// try to get a inset pointer from it's id if we have
269 /// an inset to give back!
270 virtual Inset * getInsetFromID(int /*id*/) const { return 0; }
271 /// if this insets owns paragraphs (f.ex. InsetText) then it
272 /// should return it's very first one!
273 virtual Paragraph * firstParagraph() const { return 0; }
275 virtual Paragraph * getFirstParagraph(int /*num*/) const { return 0; }
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 bool topx_set; /* have we already drawn ourself! */
352 mutable int top_baseline;
358 static unsigned int inset_id;
363 /// the paragraph in which this inset has been inserted
364 Paragraph * par_owner_;
368 LColor::color background_color_;
373 bool Inset::insetAllowed(Inset * in) const
375 return insetAllowed(in->lyxCode());
380 bool Inset::checkInsertChar(LyXFont &)
386 * returns true if pointer argument is valid
387 * and points to an editable inset
389 inline bool isEditableInset(Inset const * i)
391 return i && i->editable();
395 * returns true if pointer argument is valid
396 * and points to a highly editable inset
398 inline bool isHighlyEditableInset(Inset const * i)
400 return i && i->editable() == Inset::HIGHLY_EDITABLE;