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 "frontends/mouse_state.h"
23 #include "support/types.h"
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)
102 SPECIALCHAR_CODE, // 25
114 MATHMACRO_CODE, // 30
130 TEXT_TO_INSET_OFFSET = 2
143 /** Dispatch result codes
144 Now that nested updatable insets are allowed, the local dispatch
145 becomes a bit complex, just two possible results (boolean)
148 DISPATCHED = the inset catched the action
149 DISPATCHED_NOUPDATE = the inset catched the action and no update
150 is needed here to redraw the inset
151 FINISHED = the inset must be unlocked as a result
153 FINISHED_RIGHT = FINISHED, but put the cursor to the RIGHT of
155 FINISHED_UP = FINISHED, but put the cursor UP of
157 FINISHED_DOWN = FINISHED, but put the cursor DOWN of
159 UNDISPATCHED = the action was not catched, it should be
160 dispatched by lower level insets
175 Inset(Inset const & in, bool same_id = false);
179 virtual int ascent(BufferView *, LyXFont const &) const = 0;
181 virtual int descent(BufferView *, LyXFont const &) const = 0;
183 virtual int width(BufferView *, LyXFont const &) const = 0;
185 virtual void draw(BufferView *, LyXFont const &,
186 int baseline, float & x, bool cleared) const = 0;
187 /// update the inset representation
188 virtual void update(BufferView *, LyXFont const &, bool = false)
190 /// what appears in the minibuffer when opening
191 virtual string const editMessage() const;
193 virtual void edit(BufferView *, int x, int y, mouse_button::state button);
195 virtual void edit(BufferView *, bool front = true);
197 virtual EDITABLE editable() const;
199 virtual RESULT localDispatch(FuncRequest const & cmd);
201 virtual bool isTextInset() const { return false; }
203 virtual bool doClearArea() const { return true; }
204 /// return true if the inset should be removed automatically
205 virtual bool autoDelete() const;
206 /// returns true the inset can hold an inset of given type
207 virtual bool insetAllowed(Inset::Code) const { return false; }
208 /// wrapper around the above
209 bool insetAllowed(Inset * in) const;
211 virtual void write(Buffer const *, std::ostream &) const = 0;
213 virtual void read(Buffer const *, LyXLex & lex) = 0;
214 /** returns the number of rows (\n's) of generated tex code.
215 fragile == true means, that the inset should take care about
216 fragile commands by adding a \protect before.
217 If the free_spc (freespacing) variable is set, then this inset
218 is in a free-spacing paragraph.
220 virtual int latex(Buffer const *, std::ostream &, bool fragile,
221 bool free_spc) const = 0;
223 virtual int ascii(Buffer const *,
224 std::ostream &, int linelen = 0) const = 0;
226 virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
228 virtual int docbook(Buffer const *, std::ostream &, bool) const = 0;
229 /// Updates needed features for this inset.
230 virtual void validate(LaTeXFeatures & features) const;
232 virtual bool deletable() const;
234 /// returns LyX code associated with the inset. Used for TOC, ...)
235 virtual Inset::Code lyxCode() const { return NO_CODE; }
237 virtual std::vector<string> const getLabelList() const {
238 return std::vector<string>();
242 virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
244 /// returns true to override begin and end inset in file
245 virtual bool directWrite() const;
247 /// Returns true if the inset should be centered alone
248 virtual bool display() const { return false; }
249 /// Changes the display state of the inset
250 virtual void display(bool) {}
252 /// returns true if this inset needs a row on it's own
254 virtual bool needFullRow() const { return false; }
256 void setInsetName(string const & s) { name_ = s; }
258 string const & getInsetName() const { return name_; }
260 void setOwner(Inset * inset) { owner_ = inset; }
262 Inset * owner() const { return owner_; }
264 void parOwner(Paragraph * par) { par_owner_ = par; }
266 Paragraph * parOwner() const {return par_owner_; }
268 void setBackgroundColor(LColor::color);
270 LColor::color backgroundColor() const;
272 int x() const { return top_x; }
274 int y() const { return top_baseline; }
276 // because we could have fake text insets and have to call this
277 // inside them without cast!!!
279 virtual LyXText * getLyXText(BufferView const *,
280 bool const recursive = false) const;
282 virtual void deleteLyXText(BufferView *, bool = true) const {}
284 virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
285 /// returns the actuall scroll-value
286 virtual int scroll(bool recursive=true) const {
287 if (!recursive || !owner_)
292 /// try to get a inset pointer from it's id if we have
293 /// an inset to give back!
294 virtual Inset * getInsetFromID(int /* id */) const {
297 /// if this insets owns paragraphs (f.ex. InsetText) then it
298 /// should return it's very first one!
299 virtual Paragraph * firstParagraph() const {
304 virtual Paragraph * getFirstParagraph(int /*num*/) const {
308 /// return the cursor if we own one otherwise giv'em just the
309 /// BufferView cursor to work with.
310 virtual LyXCursor const & cursor(BufferView * bview) const;
316 /// used to toggle insets
317 // is the inset open?
318 virtual bool isOpen() const { return false; }
320 virtual void open(BufferView *) {}
322 virtual void close(BufferView *) const {}
323 /// check if the font of the char we want inserting is correct
324 /// and modify it if it is not.
325 virtual bool checkInsertChar(LyXFont &);
326 /// we need this here because collapsed insets are only EDITABLE
327 virtual void setFont(BufferView *, LyXFont const &,
328 bool toggleall = false, bool selectall = false);
330 // needed for spellchecking text
332 virtual bool allowSpellcheck() const { return false; }
334 // should this inset be handled like a normal charater
335 virtual bool isChar() const { return false; }
336 // is this equivalent to a letter?
337 virtual bool isLetter() const { return false; }
338 // is this equivalent to a space (which is BTW different from
339 // a line separator)?
340 virtual bool isSpace() const { return false; }
341 // should we break lines after this inset?
342 virtual bool isLineSeparator() const { return false; }
343 // if this inset has paragraphs should they be output all as default
344 // paragraphs with "Standard" layout?
345 virtual bool forceDefaultParagraphs(Inset const *) const;
346 /** returns true if, when outputing LaTeX, font changes should
347 be closed before generating this inset. This is needed for
348 insets that may contain several paragraphs */
349 virtual bool noFontChange() const { return false; }
351 virtual void getDrawFont(LyXFont &) const {}
352 /* needed for widths which are % of something
353 returns the value of \textwidth in this inset. Most of the
354 time this is the width of the workarea, but if there is a
355 minipage somewhere, it will be the width of this minipage */
356 virtual int latexTextWidth(BufferView *) const;
358 /// mark the inset contents as erased (for change tracking)
359 virtual void markErased() {}
361 /** Adds a LaTeX snippet to the Preview Loader for transformation
362 * into a bitmap image. Does not start the laoding process.
364 * Most insets have no interest in this capability, so the method
367 virtual void addPreview(grfx::PreviewLoader &) const {}
369 /** Find the PreviewLoader, add a LaTeX snippet to it and
370 * start the loading process.
372 * Most insets have no interest in this capability, so the method
375 virtual void generatePreview() const {}
381 mutable bool topx_set; /* have we already drawn ourself! */
383 mutable int top_baseline;
389 static unsigned int inset_id;
394 /// the paragraph in which this inset has been inserted
395 Paragraph * par_owner_;
399 LColor::color background_color_;
404 bool Inset::insetAllowed(Inset * in) const
406 return insetAllowed(in->lyxCode());
411 bool Inset::checkInsertChar(LyXFont &)
416 // Updatable Insets. These insets can be locked and receive
417 // directly user interaction. Currently used only for mathed.
418 // Note that all pure methods from Inset class are pure here too.
419 // [Alejandro 080596]
421 /** Extracted from Matthias notes:
423 * An inset can simple call LockInset in it's edit call and *ONLY*
426 * Unlocking is either done by LyX or the inset itself with a
429 * During the lock, all button and keyboard events will be modified
430 * and send to the inset through the following inset-features. Note that
431 * Inset::insetUnlock will be called from inside UnlockInset. It is meant
432 * to contain the code for restoring the menus and things like this.
434 * If a inset wishes any redraw and/or update it just has to call
437 * It's is completly irrelevant, where the inset is. UpdateInset will
438 * find it in any paragraph in any buffer.
439 * Of course the_locking_inset and the insets in the current paragraph/buffer
440 * are checked first, so no performance problem should occur.
442 class UpdatableInset : public Inset {
447 UpdatableInset(UpdatableInset const & in, bool same_id = false);
449 /// check if the font of the char we want inserting is correct
450 /// and modify it if it is not.
451 virtual bool checkInsertChar(LyXFont &);
453 virtual EDITABLE editable() const;
456 virtual void toggleInsetCursor(BufferView *);
458 virtual void showInsetCursor(BufferView *, bool show = true);
460 virtual void hideInsetCursor(BufferView *);
462 virtual void fitInsetCursor(BufferView *) const;
464 virtual void getCursorPos(BufferView *, int &, int &) const {}
466 virtual void insetUnlock(BufferView *);
468 virtual void edit(BufferView *, int x, int y, mouse_button::state button);
470 virtual void edit(BufferView *, bool front = true);
472 virtual void draw(BufferView *, LyXFont const &,
473 int baseline, float & x, bool cleared) const;
475 virtual bool insertInset(BufferView *, Inset *) { return false; }
477 virtual UpdatableInset * getLockingInset() const {
478 return const_cast<UpdatableInset *>(this);
481 virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
482 { return (c == lyxCode()) ? this : 0; }
484 virtual int insetInInsetY() const { return 0; }
486 virtual bool updateInsetInInset(BufferView *, Inset *)
489 virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
492 virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
495 /// An updatable inset could handle lyx editing commands
496 virtual RESULT localDispatch(FuncRequest const & cmd);
498 bool isCursorVisible() const { return cursor_visible_; }
500 virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
502 int scroll(bool recursive = true) const {
503 // We need this method to not clobber the real method in Inset
504 return Inset::scroll(recursive);
507 virtual bool showInsetDialog(BufferView *) const { return false; }
509 virtual void nodraw(bool b) const {
513 virtual bool nodraw() const {
514 return block_drawing_;
517 // needed for spellchecking text
519 virtual bool allowSpellcheck() const { return false; }
521 virtual WordLangTuple const
522 selectNextWordToSpellcheck(BufferView *, float & value) const;
524 virtual void selectSelectedWord(BufferView *) {}
526 virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
530 /// find the next change in the inset
531 virtual bool nextChange(BufferView * bv, lyx::pos_type & length);
534 // needed for search/replace functionality
536 virtual bool searchForward(BufferView *, string const &,
537 bool = true, bool = false);
539 virtual bool searchBackward(BufferView *, string const &,
540 bool = true, bool = false);
544 void toggleCursorVisible() const {
545 cursor_visible_ = !cursor_visible_;
548 void setCursorVisible(bool b) const {
551 /// scrolls to absolute position in bufferview-workwidth * sx units
552 void scroll(BufferView *, float sx) const;
553 /// scrolls offset pixels
554 void scroll(BufferView *, int offset) const;
558 mutable bool cursor_visible_;
560 mutable bool block_drawing_;
564 bool UpdatableInset::checkInsertChar(LyXFont &)
570 * returns true if pointer argument is valid
571 * and points to an editable inset
573 inline bool isEditableInset(Inset * i)
575 return i && i->editable();
579 * returns true if pointer argument is valid
580 * and points to a highly editable inset
582 inline bool isHighlyEditableInset(Inset * i)
584 return i && i->editable() == Inset::HIGHLY_EDITABLE;