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
25 #include "frontends/mouse_state.h"
26 #include "support/types.h"
48 /** This is not quite the correct place for this enum. I think
49 the correct would be to let each subclass of Inset declare
50 its own enum code. Actually the notion of an Inset::Code
51 should be avoided, but I am not sure how this could be done
57 TOC_CODE, // do these insets really need a code? (ale)
105 SPECIALCHAR_CODE, // 25
117 MATHMACRO_CODE, // 30
133 TEXT_TO_INSET_OFFSET = 2
146 /** Dispatch result codes
147 Now that nested updatable insets are allowed, the local dispatch
148 becomes a bit complex, just two possible results (boolean)
151 DISPATCHED = the inset catched the action
152 DISPATCHED_NOUPDATE = the inset catched the action and no update
153 is needed here to redraw the inset
154 FINISHED = the inset must be unlocked as a result
156 FINISHED_RIGHT = FINISHED, but put the cursor to the RIGHT of
158 FINISHED_UP = FINISHED, but put the cursor UP of
160 FINISHED_DOWN = FINISHED, but put the cursor DOWN of
162 UNDISPATCHED = the action was not catched, it should be
163 dispatched by lower level insets
178 Inset(Inset const & in, bool same_id = false);
182 virtual int ascent(BufferView *, LyXFont const &) const = 0;
184 virtual int descent(BufferView *, LyXFont const &) const = 0;
186 virtual int width(BufferView *, LyXFont const &) const = 0;
188 virtual void draw(BufferView *, LyXFont const &,
189 int baseline, float & x, bool cleared) const = 0;
190 /// update the inset representation
191 virtual void update(BufferView *, LyXFont const &, bool = false)
193 /// what appears in the minibuffer when opening
194 virtual string const editMessage() const;
196 virtual void edit(BufferView *, int x, int y, mouse_button::state button);
198 virtual void edit(BufferView *, bool front = true);
200 virtual EDITABLE editable() const;
202 virtual RESULT localDispatch(FuncRequest const & cmd);
204 virtual bool isTextInset() const { return false; }
206 virtual bool doClearArea() const { return true; }
208 virtual bool autoDelete() const;
209 /// returns true the inset can hold an inset of given type
210 virtual bool insetAllowed(Inset::Code) const { return false; }
211 /// wrapper around the above
212 bool insetAllowed(Inset * in) const;
214 virtual void write(Buffer const *, std::ostream &) const = 0;
216 virtual void read(Buffer const *, LyXLex & lex) = 0;
217 /** returns the number of rows (\n's) of generated tex code.
218 fragile == true means, that the inset should take care about
219 fragile commands by adding a \protect before.
220 If the free_spc (freespacing) variable is set, then this inset
221 is in a free-spacing paragraph.
223 virtual int latex(Buffer const *, std::ostream &, bool fragile,
224 bool free_spc) const = 0;
226 virtual int ascii(Buffer const *,
227 std::ostream &, int linelen = 0) const = 0;
229 virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
231 virtual int docbook(Buffer const *, std::ostream &, bool) const = 0;
232 /// Updates needed features for this inset.
233 virtual void validate(LaTeXFeatures & features) const;
235 virtual bool deletable() const;
237 /// returns LyX code associated with the inset. Used for TOC, ...)
238 virtual Inset::Code lyxCode() const { return NO_CODE; }
240 virtual std::vector<string> const getLabelList() const {
241 return std::vector<string>();
245 virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
247 /// returns true to override begin and end inset in file
248 virtual bool directWrite() const;
250 /// Returns true if the inset should be centered alone
251 virtual bool display() const { return false; }
252 /// Changes the display state of the inset
253 virtual void display(bool) {}
255 /// returns true if this inset needs a row on it's own
257 virtual bool needFullRow() const { return false; }
259 void setInsetName(string const & s) { name_ = s; }
261 string const & getInsetName() const { return name_; }
263 void setOwner(Inset * inset) { owner_ = inset; }
265 Inset * owner() const { return owner_; }
267 void parOwner(Paragraph * par) { par_owner_ = par; }
269 Paragraph * parOwner() const {return par_owner_; }
271 void setBackgroundColor(LColor::color);
273 LColor::color backgroundColor() const;
275 int x() const { return top_x; }
277 int y() const { return top_baseline; }
279 // because we could have fake text insets and have to call this
280 // inside them without cast!!!
282 virtual LyXText * getLyXText(BufferView const *,
283 bool const recursive = false) const;
285 virtual void deleteLyXText(BufferView *, bool = true) const {}
287 virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
288 /// returns the actuall scroll-value
289 virtual int scroll(bool recursive=true) const {
290 if (!recursive || !owner_)
294 /// try to get a paragraph pointer from it's id if we have a
295 /// paragraph to give back!
296 virtual Paragraph * getParFromID(int /* id */) const {
299 /// try to get a inset pointer from it's id if we have
300 /// an inset to give back!
301 virtual Inset * getInsetFromID(int /* id */) const {
304 /// if this insets owns paragraphs (f.ex. InsetText) then it
305 /// should return it's very first one!
306 virtual Paragraph * firstParagraph() const {
311 virtual Paragraph * getFirstParagraph(int /*num*/) const {
315 /// return the cursor if we own one otherwise giv'em just the
316 /// BufferView cursor to work with.
317 virtual LyXCursor const & cursor(BufferView * bview) const;
323 /// used to toggle insets
324 // is the inset open?
325 virtual bool isOpen() const { return false; }
327 virtual void open(BufferView *) {}
329 virtual void close(BufferView *) const {}
330 /// check if the font of the char we want inserting is correct
331 /// and modify it if it is not.
332 virtual bool checkInsertChar(LyXFont &);
333 /// we need this here because collapsed insets are only EDITABLE
334 virtual void setFont(BufferView *, LyXFont const &,
335 bool toggleall = false, bool selectall = false);
337 // needed for spellchecking text
339 virtual bool allowSpellcheck() const { return false; }
341 // should this inset be handled like a normal charater
342 virtual bool isChar() const { return false; }
343 // is this equivalent to a letter?
344 virtual bool isLetter() const { return false; }
345 // is this equivalent to a space (which is BTW different from
346 // a line separator)?
347 virtual bool isSpace() const { return false; }
348 // should we break lines after this inset?
349 virtual bool isLineSeparator() const { return false; }
350 // if this inset has paragraphs should they be output all as default
351 // paragraphs with "Standard" layout?
352 virtual bool forceDefaultParagraphs(Inset const *) const;
353 /** returns true if, when outputing LaTeX, font changes should
354 be closed before generating this inset. This is needed for
355 insets that may contain several paragraphs */
356 virtual bool noFontChange() const { return false; }
358 virtual void getDrawFont(LyXFont &) const {}
359 /* needed for widths which are % of something
360 returns the value of \textwidth in this inset. Most of the
361 time this is the width of the workarea, but if there is a
362 minipage somewhere, it will be the width of this minipage */
363 virtual int latexTextWidth(BufferView *) const;
365 /// mark the inset contents as erased (for change tracking)
366 virtual void markErased() {}
368 /** Adds a LaTeX snippet to the Preview Loader for transformation
369 * into a bitmap image. Does not start the laoding process.
371 * Most insets have no interest in this capability, so the method
374 virtual void addPreview(grfx::PreviewLoader &) const {}
376 /** Find the PreviewLoader, add a LaTeX snippet to it and
377 * start the loading process.
379 * Most insets have no interest in this capability, so the method
382 virtual void generatePreview() const {}
388 mutable bool topx_set; /* have we already drawn ourself! */
390 mutable int top_baseline;
396 static unsigned int inset_id;
401 /// the paragraph in which this inset has been inserted
402 Paragraph * par_owner_;
406 LColor::color background_color_;
411 bool Inset::insetAllowed(Inset * in) const
413 return insetAllowed(in->lyxCode());
418 bool Inset::checkInsertChar(LyXFont &)
423 // Updatable Insets. These insets can be locked and receive
424 // directly user interaction. Currently used only for mathed.
425 // Note that all pure methods from Inset class are pure here too.
426 // [Alejandro 080596]
428 /** Extracted from Matthias notes:
430 * An inset can simple call LockInset in it's edit call and *ONLY*
433 * Unlocking is either done by LyX or the inset itself with a
436 * During the lock, all button and keyboard events will be modified
437 * and send to the inset through the following inset-features. Note that
438 * Inset::insetUnlock will be called from inside UnlockInset. It is meant
439 * to contain the code for restoring the menus and things like this.
441 * If a inset wishes any redraw and/or update it just has to call
444 * It's is completly irrelevant, where the inset is. UpdateInset will
445 * find it in any paragraph in any buffer.
446 * Of course the_locking_inset and the insets in the current paragraph/buffer
447 * are checked first, so no performance problem should occur.
449 class UpdatableInset : public Inset {
454 UpdatableInset(UpdatableInset const & in, bool same_id = false);
456 /// check if the font of the char we want inserting is correct
457 /// and modify it if it is not.
458 virtual bool checkInsertChar(LyXFont &);
460 virtual EDITABLE editable() const;
463 virtual void toggleInsetCursor(BufferView *);
465 virtual void showInsetCursor(BufferView *, bool show = true);
467 virtual void hideInsetCursor(BufferView *);
469 virtual void fitInsetCursor(BufferView *) const;
471 virtual void getCursorPos(BufferView *, int &, int &) const {}
473 virtual void insetUnlock(BufferView *);
475 virtual void edit(BufferView *, int x, int y, mouse_button::state button);
477 virtual void edit(BufferView *, bool front = true);
479 virtual void draw(BufferView *, LyXFont const &,
480 int baseline, float & x, bool cleared) const;
482 virtual bool insertInset(BufferView *, Inset *) { return false; }
484 virtual UpdatableInset * getLockingInset() const {
485 return const_cast<UpdatableInset *>(this);
488 virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
489 { return (c == lyxCode()) ? this : 0; }
491 virtual int insetInInsetY() const { return 0; }
493 virtual bool updateInsetInInset(BufferView *, Inset *)
496 virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
499 virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
502 /// An updatable inset could handle lyx editing commands
503 virtual RESULT localDispatch(FuncRequest const & cmd);
505 bool isCursorVisible() const { return cursor_visible_; }
507 virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
509 int scroll(bool recursive = true) const {
510 // We need this method to not clobber the real method in Inset
511 return Inset::scroll(recursive);
514 virtual bool showInsetDialog(BufferView *) const { return false; }
516 virtual void nodraw(bool b) const {
520 virtual bool nodraw() const {
521 return block_drawing_;
524 // needed for spellchecking text
526 virtual bool allowSpellcheck() const { return false; }
528 virtual WordLangTuple const
529 selectNextWordToSpellcheck(BufferView *, float & value) const;
531 virtual void selectSelectedWord(BufferView *) {}
533 virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
537 /// find the next change in the inset
538 virtual bool nextChange(BufferView * bv, lyx::pos_type & length);
541 // needed for search/replace functionality
543 virtual bool searchForward(BufferView *, string const &,
544 bool = true, bool = false);
546 virtual bool searchBackward(BufferView *, string const &,
547 bool = true, bool = false);
551 void toggleCursorVisible() const {
552 cursor_visible_ = !cursor_visible_;
555 void setCursorVisible(bool b) const {
558 /// scrolls to absolute position in bufferview-workwidth * sx units
559 void scroll(BufferView *, float sx) const;
560 /// scrolls offset pixels
561 void scroll(BufferView *, int offset) const;
565 mutable bool cursor_visible_;
567 mutable bool block_drawing_;
571 bool UpdatableInset::checkInsertChar(LyXFont &)
577 * returns true if pointer argument is valid
578 * and points to an editable inset
580 inline bool isEditableInset(Inset * i)
582 return i && i->editable();
586 * returns true if pointer argument is valid
587 * and points to a highly editable inset
589 inline bool isHighlyEditableInset(Inset * i)
591 return i && i->editable() == Inset::HIGHLY_EDITABLE;