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; }
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_)
291 /// try to get a paragraph pointer from it's id if we have a
292 /// paragraph to give back!
293 virtual Paragraph * getParFromID(int /* id */) const {
296 /// try to get a inset pointer from it's id if we have
297 /// an inset to give back!
298 virtual Inset * getInsetFromID(int /* id */) const {
301 /// if this insets owns paragraphs (f.ex. InsetText) then it
302 /// should return it's very first one!
303 virtual Paragraph * firstParagraph() const {
308 virtual Paragraph * getFirstParagraph(int /*num*/) const {
312 /// return the cursor if we own one otherwise giv'em just the
313 /// BufferView cursor to work with.
314 virtual LyXCursor const & cursor(BufferView * bview) const;
320 /// used to toggle insets
321 // is the inset open?
322 virtual bool isOpen() const { return false; }
324 virtual void open(BufferView *) {}
326 virtual void close(BufferView *) const {}
327 /// check if the font of the char we want inserting is correct
328 /// and modify it if it is not.
329 virtual bool checkInsertChar(LyXFont &);
330 /// we need this here because collapsed insets are only EDITABLE
331 virtual void setFont(BufferView *, LyXFont const &,
332 bool toggleall = false, bool selectall = false);
334 // needed for spellchecking text
336 virtual bool allowSpellcheck() const { return false; }
338 // should this inset be handled like a normal charater
339 virtual bool isChar() const { return false; }
340 // is this equivalent to a letter?
341 virtual bool isLetter() const { return false; }
342 // is this equivalent to a space (which is BTW different from
343 // a line separator)?
344 virtual bool isSpace() const { return false; }
345 // should we break lines after this inset?
346 virtual bool isLineSeparator() const { return false; }
347 // if this inset has paragraphs should they be output all as default
348 // paragraphs with "Standard" layout?
349 virtual bool forceDefaultParagraphs(Inset const *) const;
350 /** returns true if, when outputing LaTeX, font changes should
351 be closed before generating this inset. This is needed for
352 insets that may contain several paragraphs */
353 virtual bool noFontChange() const { return false; }
355 virtual void getDrawFont(LyXFont &) const {}
356 /* needed for widths which are % of something
357 returns the value of \textwidth in this inset. Most of the
358 time this is the width of the workarea, but if there is a
359 minipage somewhere, it will be the width of this minipage */
360 virtual int latexTextWidth(BufferView *) const;
362 /// mark the inset contents as erased (for change tracking)
363 virtual void markErased() {}
365 /** Adds a LaTeX snippet to the Preview Loader for transformation
366 * into a bitmap image. Does not start the laoding process.
368 * Most insets have no interest in this capability, so the method
371 virtual void addPreview(grfx::PreviewLoader &) const {}
373 /** Find the PreviewLoader, add a LaTeX snippet to it and
374 * start the loading process.
376 * Most insets have no interest in this capability, so the method
379 virtual void generatePreview() const {}
385 mutable bool topx_set; /* have we already drawn ourself! */
387 mutable int top_baseline;
393 static unsigned int inset_id;
398 /// the paragraph in which this inset has been inserted
399 Paragraph * par_owner_;
403 LColor::color background_color_;
408 bool Inset::insetAllowed(Inset * in) const
410 return insetAllowed(in->lyxCode());
415 bool Inset::checkInsertChar(LyXFont &)
420 // Updatable Insets. These insets can be locked and receive
421 // directly user interaction. Currently used only for mathed.
422 // Note that all pure methods from Inset class are pure here too.
423 // [Alejandro 080596]
425 /** Extracted from Matthias notes:
427 * An inset can simple call LockInset in it's edit call and *ONLY*
430 * Unlocking is either done by LyX or the inset itself with a
433 * During the lock, all button and keyboard events will be modified
434 * and send to the inset through the following inset-features. Note that
435 * Inset::insetUnlock will be called from inside UnlockInset. It is meant
436 * to contain the code for restoring the menus and things like this.
438 * If a inset wishes any redraw and/or update it just has to call
441 * It's is completly irrelevant, where the inset is. UpdateInset will
442 * find it in any paragraph in any buffer.
443 * Of course the_locking_inset and the insets in the current paragraph/buffer
444 * are checked first, so no performance problem should occur.
446 class UpdatableInset : public Inset {
451 UpdatableInset(UpdatableInset const & in, bool same_id = false);
453 /// check if the font of the char we want inserting is correct
454 /// and modify it if it is not.
455 virtual bool checkInsertChar(LyXFont &);
457 virtual EDITABLE editable() const;
460 virtual void toggleInsetCursor(BufferView *);
462 virtual void showInsetCursor(BufferView *, bool show = true);
464 virtual void hideInsetCursor(BufferView *);
466 virtual void fitInsetCursor(BufferView *) const;
468 virtual void getCursorPos(BufferView *, int &, int &) const {}
470 virtual void insetUnlock(BufferView *);
472 virtual void edit(BufferView *, int x, int y, mouse_button::state button);
474 virtual void edit(BufferView *, bool front = true);
476 virtual void draw(BufferView *, LyXFont const &,
477 int baseline, float & x, bool cleared) const;
479 virtual bool insertInset(BufferView *, Inset *) { return false; }
481 virtual UpdatableInset * getLockingInset() const {
482 return const_cast<UpdatableInset *>(this);
485 virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
486 { return (c == lyxCode()) ? this : 0; }
488 virtual int insetInInsetY() const { return 0; }
490 virtual bool updateInsetInInset(BufferView *, Inset *)
493 virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
496 virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
499 /// An updatable inset could handle lyx editing commands
500 virtual RESULT localDispatch(FuncRequest const & cmd);
502 bool isCursorVisible() const { return cursor_visible_; }
504 virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
506 int scroll(bool recursive = true) const {
507 // We need this method to not clobber the real method in Inset
508 return Inset::scroll(recursive);
511 virtual bool showInsetDialog(BufferView *) const { return false; }
513 virtual void nodraw(bool b) const {
517 virtual bool nodraw() const {
518 return block_drawing_;
521 // needed for spellchecking text
523 virtual bool allowSpellcheck() const { return false; }
525 virtual WordLangTuple const
526 selectNextWordToSpellcheck(BufferView *, float & value) const;
528 virtual void selectSelectedWord(BufferView *) {}
530 virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
534 /// find the next change in the inset
535 virtual bool nextChange(BufferView * bv, lyx::pos_type & length);
538 // needed for search/replace functionality
540 virtual bool searchForward(BufferView *, string const &,
541 bool = true, bool = false);
543 virtual bool searchBackward(BufferView *, string const &,
544 bool = true, bool = false);
548 void toggleCursorVisible() const {
549 cursor_visible_ = !cursor_visible_;
552 void setCursorVisible(bool b) const {
555 /// scrolls to absolute position in bufferview-workwidth * sx units
556 void scroll(BufferView *, float sx) const;
557 /// scrolls offset pixels
558 void scroll(BufferView *, int offset) const;
562 mutable bool cursor_visible_;
564 mutable bool block_drawing_;
568 bool UpdatableInset::checkInsertChar(LyXFont &)
574 * returns true if pointer argument is valid
575 * and points to an editable inset
577 inline bool isEditableInset(Inset * i)
579 return i && i->editable();
583 * returns true if pointer argument is valid
584 * and points to a highly editable inset
586 inline bool isHighlyEditableInset(Inset * i)
588 return i && i->editable() == Inset::HIGHLY_EDITABLE;