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)
103 SPECIALCHAR_CODE, // 25
115 MATHMACRO_CODE, // 30
131 TEXT_TO_INSET_OFFSET = 2
145 typedef dispatch_result RESULT;
150 Inset(Inset const & in, bool same_id = false);
152 virtual int ascent(BufferView *, LyXFont const &) const = 0;
154 virtual int descent(BufferView *, LyXFont const &) const = 0;
156 virtual int width(BufferView *, LyXFont const &) const = 0;
158 virtual void draw(BufferView *, LyXFont const &,
159 int baseline, float & x, bool cleared) const = 0;
160 /// update the inset representation
161 virtual void update(BufferView *, LyXFont const &, bool = false)
163 /// what appears in the minibuffer when opening
164 virtual string const editMessage() const;
166 virtual void edit(BufferView *, int x, int y, mouse_button::state button);
168 virtual void edit(BufferView *, bool front = true);
170 virtual EDITABLE editable() const;
172 virtual bool isTextInset() const { return false; }
174 virtual bool doClearArea() const { return true; }
175 /// return true if the inset should be removed automatically
176 virtual bool autoDelete() const;
177 /// returns true the inset can hold an inset of given type
178 virtual bool insetAllowed(Inset::Code) const { return false; }
179 /// wrapper around the above
180 bool insetAllowed(Inset * in) const;
182 virtual void write(Buffer const *, std::ostream &) const = 0;
184 virtual void read(Buffer const *, LyXLex & lex) = 0;
185 /** returns the number of rows (\n's) of generated tex code.
186 fragile == true means, that the inset should take care about
187 fragile commands by adding a \protect before.
188 If the free_spc (freespacing) variable is set, then this inset
189 is in a free-spacing paragraph.
191 virtual int latex(Buffer const *, std::ostream &, bool fragile,
192 bool free_spc) const = 0;
194 virtual int ascii(Buffer const *,
195 std::ostream &, int linelen = 0) const = 0;
197 virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
199 virtual int docbook(Buffer const *, std::ostream &, bool) const = 0;
200 /// Updates needed features for this inset.
201 virtual void validate(LaTeXFeatures & features) const;
203 /// returns LyX code associated with the inset. Used for TOC, ...)
204 virtual Inset::Code lyxCode() const { return NO_CODE; }
206 virtual std::vector<string> const getLabelList() const {
207 return std::vector<string>();
211 virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
213 /// returns true to override begin and end inset in file
214 virtual bool directWrite() const;
216 /// Returns true if the inset should be centered alone
217 virtual bool display() const { return false; }
218 /// Changes the display state of the inset
219 virtual void display(bool) {}
221 /// returns true if this inset needs a row on it's own
223 virtual bool needFullRow() const { return false; }
225 void setInsetName(string const & s) { name_ = s; }
227 string const & getInsetName() const { return name_; }
229 void setOwner(Inset * inset) { owner_ = inset; }
231 Inset * owner() const { return owner_; }
233 void parOwner(Paragraph * par) { par_owner_ = par; }
235 Paragraph * parOwner() const {return par_owner_; }
237 void setBackgroundColor(LColor::color);
239 LColor::color backgroundColor() const;
241 int x() const { return top_x; }
243 int y() const { return top_baseline; }
245 // because we could have fake text insets and have to call this
246 // inside them without cast!!!
248 virtual LyXText * getLyXText(BufferView const *,
249 bool const recursive = false) const;
251 virtual void deleteLyXText(BufferView *, bool = true) const {}
253 virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
254 /// returns the actuall scroll-value
255 virtual int scroll(bool recursive=true) const {
256 if (!recursive || !owner_)
261 /// try to get a inset pointer from it's id if we have
262 /// an inset to give back!
263 virtual Inset * getInsetFromID(int /* id */) const {
266 /// if this insets owns paragraphs (f.ex. InsetText) then it
267 /// should return it's very first one!
268 virtual Paragraph * firstParagraph() const {
273 virtual Paragraph * getFirstParagraph(int /*num*/) 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 {}
351 mutable bool topx_set; /* have we already drawn ourself! */
353 mutable int top_baseline;
359 static unsigned int inset_id;
364 /// the paragraph in which this inset has been inserted
365 Paragraph * par_owner_;
369 LColor::color background_color_;
374 bool Inset::insetAllowed(Inset * in) const
376 return insetAllowed(in->lyxCode());
381 bool Inset::checkInsertChar(LyXFont &)
386 // Updatable Insets. These insets can be locked and receive
387 // directly user interaction. Currently used only for mathed.
388 // Note that all pure methods from Inset class are pure here too.
389 // [Alejandro 080596]
391 /** Extracted from Matthias notes:
393 * An inset can simple call LockInset in it's edit call and *ONLY*
396 * Unlocking is either done by LyX or the inset itself with a
399 * During the lock, all button and keyboard events will be modified
400 * and send to the inset through the following inset-features. Note that
401 * Inset::insetUnlock will be called from inside UnlockInset. It is meant
402 * to contain the code for restoring the menus and things like this.
404 * If a inset wishes any redraw and/or update it just has to call
407 * It's is completly irrelevant, where the inset is. UpdateInset will
408 * find it in any paragraph in any buffer.
409 * Of course the_locking_inset and the insets in the current paragraph/buffer
410 * are checked first, so no performance problem should occur.
412 class UpdatableInset : public Inset {
417 UpdatableInset(UpdatableInset const & in, bool same_id = false);
419 /// check if the font of the char we want inserting is correct
420 /// and modify it if it is not.
421 virtual bool checkInsertChar(LyXFont &);
423 virtual EDITABLE editable() const;
426 virtual void toggleInsetCursor(BufferView *);
428 virtual void showInsetCursor(BufferView *, bool show = true);
430 virtual void hideInsetCursor(BufferView *);
432 virtual void fitInsetCursor(BufferView *) const;
434 virtual void getCursorPos(BufferView *, int &, int &) const {}
436 virtual void insetUnlock(BufferView *);
438 virtual void edit(BufferView *, int x, int y, mouse_button::state button);
440 virtual void edit(BufferView *, bool front = true);
442 virtual void draw(BufferView *, LyXFont const &,
443 int baseline, float & x, bool cleared) const;
445 virtual bool insertInset(BufferView *, Inset *) { return false; }
447 virtual UpdatableInset * getLockingInset() const {
448 return const_cast<UpdatableInset *>(this);
451 virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
452 { return (c == lyxCode()) ? this : 0; }
454 virtual int insetInInsetY() const { return 0; }
456 virtual bool updateInsetInInset(BufferView *, Inset *)
459 virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
462 virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
465 /// An updatable inset could handle lyx editing commands
466 virtual RESULT localDispatch(FuncRequest const & cmd);
468 bool isCursorVisible() const { return cursor_visible_; }
470 virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
472 int scroll(bool recursive = true) const {
473 // We need this method to not clobber the real method in Inset
474 return Inset::scroll(recursive);
477 virtual bool showInsetDialog(BufferView *) const { return false; }
479 virtual void nodraw(bool b) const {
483 virtual bool nodraw() const {
484 return block_drawing_;
487 // needed for spellchecking text
489 virtual bool allowSpellcheck() const { return false; }
491 virtual WordLangTuple const
492 selectNextWordToSpellcheck(BufferView *, float & value) const;
494 virtual void selectSelectedWord(BufferView *) {}
496 virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
500 /// find the next change in the inset
501 virtual bool nextChange(BufferView * bv, lyx::pos_type & length);
504 // needed for search/replace functionality
506 virtual bool searchForward(BufferView *, string const &,
507 bool = true, bool = false);
509 virtual bool searchBackward(BufferView *, string const &,
510 bool = true, bool = false);
515 void toggleCursorVisible() const {
516 cursor_visible_ = !cursor_visible_;
519 void setCursorVisible(bool b) const {
522 /// scrolls to absolute position in bufferview-workwidth * sx units
523 void scroll(BufferView *, float sx) const;
524 /// scrolls offset pixels
525 void scroll(BufferView *, int offset) const;
529 mutable bool cursor_visible_;
531 mutable bool block_drawing_;
535 bool UpdatableInset::checkInsertChar(LyXFont &)
541 * returns true if pointer argument is valid
542 * and points to an editable inset
544 inline bool isEditableInset(Inset * i)
546 return i && i->editable();
550 * returns true if pointer argument is valid
551 * and points to a highly editable inset
553 inline bool isHighlyEditableInset(Inset * i)
555 return i && i->editable() == Inset::HIGHLY_EDITABLE;