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"
47 /** This is not quite the correct place for this enum. I think
48 the correct would be to let each subclass of Inset declare
49 its own enum code. Actually the notion of an Inset::Code
50 should be avoided, but I am not sure how this could be done
56 TOC_CODE, // do these insets really need a code? (ale)
104 SPECIALCHAR_CODE, // 25
116 MATHMACRO_CODE, // 30
132 TEXT_TO_INSET_OFFSET = 2
145 /** Dispatch result codes
146 Now that nested updatable insets are allowed, the local dispatch
147 becomes a bit complex, just two possible results (boolean)
150 DISPATCHED = the inset catched the action
151 DISPATCHED_NOUPDATE = the inset catched the action and no update
152 is needed here to redraw the inset
153 FINISHED = the inset must be unlocked as a result
155 FINISHED_RIGHT = FINISHED, but put the cursor to the RIGHT of
157 FINISHED_UP = FINISHED, but put the cursor UP of
159 FINISHED_DOWN = FINISHED, but put the cursor DOWN of
161 UNDISPATCHED = the action was not catched, it should be
162 dispatched by lower level insets
177 Inset(Inset const & in, bool same_id = false);
181 virtual int ascent(BufferView *, LyXFont const &) const = 0;
183 virtual int descent(BufferView *, LyXFont const &) const = 0;
185 virtual int width(BufferView *, LyXFont const &) const = 0;
187 virtual void draw(BufferView *, LyXFont const &,
188 int baseline, float & x, bool cleared) const = 0;
189 /// update the inset representation
190 virtual void update(BufferView *, LyXFont const &, bool = false)
192 /// what appears in the minibuffer when opening
193 virtual string const editMessage() const;
195 virtual void edit(BufferView *, int x, int y, mouse_button::state button);
197 virtual void edit(BufferView *, bool front = true);
199 virtual EDITABLE editable() const;
201 virtual RESULT localDispatch(FuncRequest const & cmd);
203 virtual bool isTextInset() const { return false; }
205 virtual bool doClearArea() const { return true; }
207 virtual bool autoDelete() const;
208 /// returns true the inset can hold an inset of given type
209 virtual bool insetAllowed(Inset::Code) const { return false; }
210 /// wrapper around the above
211 bool insetAllowed(Inset * in) const;
213 virtual void write(Buffer const *, std::ostream &) const = 0;
215 virtual void read(Buffer const *, LyXLex & lex) = 0;
216 /** returns the number of rows (\n's) of generated tex code.
217 fragile == true means, that the inset should take care about
218 fragile commands by adding a \protect before.
219 If the free_spc (freespacing) variable is set, then this inset
220 is in a free-spacing paragraph.
222 virtual int latex(Buffer const *, std::ostream &, bool fragile,
223 bool free_spc) const = 0;
225 virtual int ascii(Buffer const *,
226 std::ostream &, int linelen = 0) const = 0;
228 virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
230 virtual int docbook(Buffer const *, std::ostream &, bool) const = 0;
231 /// Updates needed features for this inset.
232 virtual void validate(LaTeXFeatures & features) const;
234 virtual bool deletable() const;
236 /// returns LyX code associated with the inset. Used for TOC, ...)
237 virtual Inset::Code lyxCode() const { return NO_CODE; }
239 virtual std::vector<string> const getLabelList() const {
240 return std::vector<string>();
244 virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
246 /// returns true to override begin and end inset in file
247 virtual bool directWrite() const;
249 /// Returns true if the inset should be centered alone
250 virtual bool display() const { return false; }
251 /// Changes the display state of the inset
252 virtual void display(bool) {}
254 /// returns true if this inset needs a row on it's own
256 virtual bool needFullRow() const { return false; }
258 void setInsetName(string const & s) { name_ = s; }
260 string const & getInsetName() const { return name_; }
262 void setOwner(Inset * inset) { owner_ = inset; }
264 Inset * owner() const { return owner_; }
266 void parOwner(Paragraph * par) { par_owner_ = par; }
268 Paragraph * parOwner() const {return par_owner_; }
270 void setBackgroundColor(LColor::color);
272 LColor::color backgroundColor() const;
274 int x() const { return top_x; }
276 int y() const { return top_baseline; }
278 // because we could have fake text insets and have to call this
279 // inside them without cast!!!
281 virtual LyXText * getLyXText(BufferView const *,
282 bool const recursive = false) const;
284 virtual void deleteLyXText(BufferView *, bool = true) const {}
286 virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
287 /// returns the actuall scroll-value
288 virtual int scroll(bool recursive=true) const {
289 if (!recursive || !owner_)
293 /// try to get a paragraph pointer from it's id if we have a
294 /// paragraph to give back!
295 virtual Paragraph * getParFromID(int /* id */) const {
298 /// try to get a inset pointer from it's id if we have
299 /// an inset to give back!
300 virtual Inset * getInsetFromID(int /* id */) const {
303 /// if this insets owns paragraphs (f.ex. InsetText) then it
304 /// should return it's very first one!
305 virtual Paragraph * firstParagraph() const {
310 virtual Paragraph * getFirstParagraph(int /*num*/) const {
314 /// return the cursor if we own one otherwise giv'em just the
315 /// BufferView cursor to work with.
316 virtual LyXCursor const & cursor(BufferView * bview) const;
322 /// used to toggle insets
323 // is the inset open?
324 virtual bool isOpen() const { return false; }
326 virtual void open(BufferView *) {}
328 virtual void close(BufferView *) const {}
329 /// check if the font of the char we want inserting is correct
330 /// and modify it if it is not.
331 virtual bool checkInsertChar(LyXFont &);
332 /// we need this here because collapsed insets are only EDITABLE
333 virtual void setFont(BufferView *, LyXFont const &,
334 bool toggleall = false, bool selectall = false);
336 // needed for spellchecking text
338 virtual bool allowSpellcheck() { return false; }
340 // should this inset be handled like a normal charater
341 virtual bool isChar() const { return false; }
342 // is this equivalent to a letter?
343 virtual bool isLetter() const { return false; }
344 // is this equivalent to a space (which is BTW different from
345 // a line separator)?
346 virtual bool isSpace() const { return false; }
347 // should we break lines after this inset?
348 virtual bool isLineSeparator() const { return false; }
349 // if this inset has paragraphs should they be output all as default
350 // paragraphs with "Standard" layout?
351 virtual bool forceDefaultParagraphs(Inset const *) const;
352 /** returns true if, when outputing LaTeX, font changes should
353 be closed before generating this inset. This is needed for
354 insets that may contain several paragraphs */
355 virtual bool noFontChange() const { return false; }
357 virtual void getDrawFont(LyXFont &) const {}
358 /* needed for widths which are % of something
359 returns the value of \textwidth in this inset. Most of the
360 time this is the width of the workarea, but if there is a
361 minipage somewhere, it will be the width of this minipage */
362 virtual int latexTextWidth(BufferView *) const;
364 /** Adds a LaTeX snippet to the Preview Loader for transformation
365 * into a bitmap image. Does not start the laoding process.
367 * Most insets have no interest in this capability, so the method
370 virtual void addPreview(grfx::PreviewLoader &) const {}
372 /** Find the PreviewLoader, add a LaTeX snippet to it and
373 * start the loading process.
375 * Most insets have no interest in this capability, so the method
378 virtual void generatePreview() const {}
384 mutable bool topx_set; /* have we already drawn ourself! */
386 mutable int top_baseline;
392 static unsigned int inset_id;
397 /// the paragraph in which this inset has been inserted
398 Paragraph * par_owner_;
402 LColor::color background_color_;
407 bool Inset::insetAllowed(Inset * in) const
409 return insetAllowed(in->lyxCode());
414 bool Inset::checkInsertChar(LyXFont &)
419 // Updatable Insets. These insets can be locked and receive
420 // directly user interaction. Currently used only for mathed.
421 // Note that all pure methods from Inset class are pure here too.
422 // [Alejandro 080596]
424 /** Extracted from Matthias notes:
426 * An inset can simple call LockInset in it's edit call and *ONLY*
429 * Unlocking is either done by LyX or the inset itself with a
432 * During the lock, all button and keyboard events will be modified
433 * and send to the inset through the following inset-features. Note that
434 * Inset::insetUnlock will be called from inside UnlockInset. It is meant
435 * to contain the code for restoring the menus and things like this.
437 * If a inset wishes any redraw and/or update it just has to call
440 * It's is completly irrelevant, where the inset is. UpdateInset will
441 * find it in any paragraph in any buffer.
442 * Of course the_locking_inset and the insets in the current paragraph/buffer
443 * are checked first, so no performance problem should occur.
445 class UpdatableInset : public Inset {
450 UpdatableInset(UpdatableInset const & in, bool same_id = false);
452 /// check if the font of the char we want inserting is correct
453 /// and modify it if it is not.
454 virtual bool checkInsertChar(LyXFont &);
456 virtual EDITABLE editable() const;
459 virtual void toggleInsetCursor(BufferView *);
461 virtual void showInsetCursor(BufferView *, bool show = true);
463 virtual void hideInsetCursor(BufferView *);
465 virtual void fitInsetCursor(BufferView *) const;
467 virtual void getCursorPos(BufferView *, int &, int &) const {}
469 virtual void insetUnlock(BufferView *);
471 virtual void edit(BufferView *, int x, int y, mouse_button::state button);
473 virtual void edit(BufferView *, bool front = true);
475 virtual void draw(BufferView *, LyXFont const &,
476 int baseline, float & x, bool cleared) const;
478 virtual bool insertInset(BufferView *, Inset *) { return false; }
480 virtual UpdatableInset * getLockingInset() const {
481 return const_cast<UpdatableInset *>(this);
484 virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
485 { return (c == lyxCode()) ? this : 0; }
487 virtual int insetInInsetY() const { return 0; }
489 virtual bool updateInsetInInset(BufferView *, Inset *)
492 virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
495 virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
498 /// An updatable inset could handle lyx editing commands
499 virtual RESULT localDispatch(FuncRequest const & cmd);
501 bool isCursorVisible() const { return cursor_visible_; }
503 virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
505 int scroll(bool recursive = true) const {
506 // We need this method to not clobber the real method in Inset
507 return Inset::scroll(recursive);
510 virtual bool showInsetDialog(BufferView *) const { return false; }
512 virtual void nodraw(bool b) const {
516 virtual bool nodraw() const {
517 return block_drawing_;
520 // needed for spellchecking text
522 virtual bool allowSpellcheck() { return false; }
524 virtual WordLangTuple const
525 selectNextWordToSpellcheck(BufferView *, float & value) const;
527 virtual void selectSelectedWord(BufferView *) { return; }
529 virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
533 // needed for search/replace functionality
535 virtual bool searchForward(BufferView *, string const &,
536 bool = true, bool = false);
538 virtual bool searchBackward(BufferView *, string const &,
539 bool = true, bool = false);
543 void toggleCursorVisible() const {
544 cursor_visible_ = !cursor_visible_;
547 void setCursorVisible(bool b) const {
550 /// scrolls to absolute position in bufferview-workwidth * sx units
551 void scroll(BufferView *, float sx) const;
552 /// scrolls offset pixels
553 void scroll(BufferView *, int offset) const;
557 mutable bool cursor_visible_;
559 mutable bool block_drawing_;
563 bool UpdatableInset::checkInsertChar(LyXFont &)
569 * returns true if pointer argument is valid
570 * and points to an editable inset
572 inline bool isEditableInset(Inset * i)
574 return i && i->editable();
578 * returns true if pointer argument is valid
579 * and points to a highly editable inset
581 inline bool isHighlyEditableInset(Inset * i)
583 return i && i->editable() == Inset::HIGHLY_EDITABLE;