2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995-2001 the LyX Team.
9 * ====================================================== */
21 #include "frontends/mouse_state.h"
43 /** This is not quite the correct place for this enum. I think
44 the correct would be to let each subclass of Inset declare
45 its own enum code. Actually the notion of an Inset::Code
46 should be avoided, but I am not sure how this could be done
52 TOC_CODE, // do these insets really need a code? (ale)
98 SPECIALCHAR_CODE, // 25
110 MATHMACRO_CODE, // 30
126 TEXT_TO_INSET_OFFSET = 2
139 /** Dispatch result codes
140 Now that nested updatable insets are allowed, the local dispatch
141 becomes a bit complex, just two possible results (boolean)
144 DISPATCHED = the inset catched the action
145 DISPATCHED_NOUPDATE = the inset catched the action and no update
146 is needed here to redraw the inset
147 FINISHED = the inset must be unlocked as a result
149 FINISHED_RIGHT = FINISHED, but put the cursor to the RIGHT of
151 FINISHED_UP = FINISHED, but put the cursor UP of
153 FINISHED_DOWN = FINISHED, but put the cursor DOWN of
155 UNDISPATCHED = the action was not catched, it should be
156 dispatched by lower level insets
168 /// To convert old binary dispatch results
169 RESULT DISPATCH_RESULT(bool b) {
170 return b ? DISPATCHED : FINISHED;
176 Inset(Inset const & in, bool same_id = false);
180 virtual int ascent(BufferView *, LyXFont const &) const = 0;
182 virtual int descent(BufferView *, LyXFont const &) const = 0;
184 virtual int width(BufferView *, LyXFont const &) const = 0;
186 virtual void draw(BufferView *, LyXFont const &,
187 int baseline, float & x, bool cleared) const = 0;
188 /// update the inset representation
189 virtual void update(BufferView *, LyXFont const &, bool = false)
191 /// what appears in the minibuffer when opening
192 virtual string const editMessage() const;
194 virtual void edit(BufferView *, int x, int y, mouse_button::state button);
196 virtual void edit(BufferView *, bool front = true);
198 virtual EDITABLE editable() const;
200 virtual RESULT localDispatch(FuncRequest const & cmd);
202 virtual bool isTextInset() const { return false; }
204 virtual bool doClearArea() const { return true; }
206 virtual bool autoDelete() const;
207 /// returns true the inset can hold an inset of given type
208 virtual bool insetAllowed(Inset::Code) const { return false; }
209 /// wrapper around the above
210 bool insetAllowed(Inset * in) const;
212 virtual void write(Buffer const *, std::ostream &) const = 0;
214 virtual void read(Buffer const *, LyXLex & lex) = 0;
215 /** returns the number of rows (\n's) of generated tex code.
216 fragile == true means, that the inset should take care about
217 fragile commands by adding a \protect before.
218 If the free_spc (freespacing) variable is set, then this inset
219 is in a free-spacing paragraph.
221 virtual int latex(Buffer const *, std::ostream &, bool fragile,
222 bool free_spc) const = 0;
224 virtual int ascii(Buffer const *,
225 std::ostream &, int linelen = 0) const = 0;
227 virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
229 virtual int docbook(Buffer const *, std::ostream &, bool) const = 0;
230 /// Updates needed features for this inset.
231 virtual void validate(LaTeXFeatures & features) const;
233 virtual bool deletable() const;
235 /// returns LyX code associated with the inset. Used for TOC, ...)
236 virtual Inset::Code lyxCode() const { return NO_CODE; }
238 virtual std::vector<string> const getLabelList() const {
239 return std::vector<string>();
243 virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
245 /// returns true to override begin and end inset in file
246 virtual bool directWrite() const;
248 /// Returns true if the inset should be centered alone
249 virtual bool display() const { return false; }
250 /// Changes the display state of the inset
251 virtual void display(bool) {}
253 /// returns true if this inset needs a row on it's own
255 virtual bool needFullRow() const { return false; }
257 void setInsetName(string const & s) { name_ = s; }
259 string const & getInsetName() const { return name_; }
261 void setOwner(Inset * inset) { owner_ = inset; }
263 Inset * owner() const { return owner_; }
265 void parOwner(Paragraph * par) { par_owner_ = par; }
267 Paragraph * parOwner() const {return par_owner_; }
269 void setBackgroundColor(LColor::color);
271 LColor::color backgroundColor() const;
273 int x() const { return top_x; }
275 int y() const { return top_baseline; }
277 // because we could have fake text insets and have to call this
278 // inside them without cast!!!
280 virtual LyXText * getLyXText(BufferView const *,
281 bool const recursive = false) const;
283 virtual void deleteLyXText(BufferView *, bool = true) const {}
285 virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
286 /// returns the actuall scroll-value
287 virtual int scroll(bool recursive=true) const {
288 if (!recursive || !owner_)
292 /// try to get a paragraph pointer from it's id if we have a
293 /// paragraph to give back!
294 virtual Paragraph * getParFromID(int /* id */) const {
297 /// try to get a inset pointer from it's id if we have
298 /// an inset to give back!
299 virtual Inset * getInsetFromID(int /* id */) const {
302 /// if this insets owns paragraphs (f.ex. InsetText) then it
303 /// should return it's very first one!
304 virtual Paragraph * firstParagraph() const {
309 virtual Paragraph * getFirstParagraph(int /*num*/) const {
313 /// return the cursor if we own one otherwise giv'em just the
314 /// BufferView cursor to work with.
315 virtual LyXCursor const & cursor(BufferView * bview) const;
321 /// used to toggle insets
322 // is the inset open?
323 virtual bool isOpen() const { return false; }
325 virtual void open(BufferView *) {}
327 virtual void close(BufferView *) const {}
328 /// check if the font of the char we want inserting is correct
329 /// and modify it if it is not.
330 virtual bool checkInsertChar(LyXFont &);
331 /// we need this here because collapsed insets are only EDITABLE
332 virtual void setFont(BufferView *, LyXFont const &,
333 bool toggleall = false, bool selectall = false);
335 // needed for spellchecking text
337 virtual bool allowSpellcheck() { return false; }
339 // should this inset be handled like a normal charater
340 virtual bool isChar() const { return false; }
341 // is this equivalent to a letter?
342 virtual bool isLetter() const { return false; }
343 // is this equivalent to a space (which is BTW different from
344 // a line separator)?
345 virtual bool isSpace() const { return false; }
346 // should we break lines after this inset?
347 virtual bool isLineSeparator() const { return false; }
348 // if this inset has paragraphs should they be output all as default
349 // paragraphs with "Standard" layout?
350 virtual bool forceDefaultParagraphs(Inset const *) const;
351 /** returns true if, when outputing LaTeX, font changes should
352 be closed before generating this inset. This is needed for
353 insets that may contain several paragraphs */
354 virtual bool noFontChange() const { return false; }
356 virtual void getDrawFont(LyXFont &) const {}
357 /* needed for widths which are % of something
358 returns the value of \textwidth in this inset. Most of the
359 time this is the width of the workarea, but if there is a
360 minipage somewhere, it will be the width of this minipage */
361 virtual int latexTextWidth(BufferView *) const;
363 /** Adds a LaTeX snippet to the Preview Loader for transformation
364 * into a bitmap image. Does not start the laoding process.
366 * Most insets have no interest in this capability, so the method
369 virtual void addPreview(grfx::PreviewLoader &) const {}
371 /** Find the PreviewLoader, add a LaTeX snippet to it and
372 * start the loading process.
374 * Most insets have no interest in this capability, so the method
377 virtual void generatePreview() const {}
383 mutable bool topx_set; /* have we already drawn ourself! */
385 mutable int top_baseline;
391 static unsigned int inset_id;
396 /// the paragraph in which this inset has been inserted
397 Paragraph * par_owner_;
401 LColor::color background_color_;
406 bool Inset::insetAllowed(Inset * in) const
408 return insetAllowed(in->lyxCode());
413 bool Inset::checkInsertChar(LyXFont &)
418 // Updatable Insets. These insets can be locked and receive
419 // directly user interaction. Currently used only for mathed.
420 // Note that all pure methods from Inset class are pure here too.
421 // [Alejandro 080596]
423 /** Extracted from Matthias notes:
425 * An inset can simple call LockInset in it's edit call and *ONLY*
428 * Unlocking is either done by LyX or the inset itself with a
431 * During the lock, all button and keyboard events will be modified
432 * and send to the inset through the following inset-features. Note that
433 * Inset::insetUnlock will be called from inside UnlockInset. It is meant
434 * to contain the code for restoring the menus and things like this.
436 * If a inset wishes any redraw and/or update it just has to call
439 * It's is completly irrelevant, where the inset is. UpdateInset will
440 * find it in any paragraph in any buffer.
441 * Of course the_locking_inset and the insets in the current paragraph/buffer
442 * are checked first, so no performance problem should occur.
444 class UpdatableInset : public Inset {
449 UpdatableInset(UpdatableInset const & in, bool same_id = false);
451 /// check if the font of the char we want inserting is correct
452 /// and modify it if it is not.
453 virtual bool checkInsertChar(LyXFont &);
455 virtual EDITABLE editable() const;
458 virtual void toggleInsetCursor(BufferView *);
460 virtual void showInsetCursor(BufferView *, bool show = true);
462 virtual void hideInsetCursor(BufferView *);
464 virtual void fitInsetCursor(BufferView *) const;
466 virtual void getCursorPos(BufferView *, int &, int &) const {}
468 virtual void insetUnlock(BufferView *);
470 virtual void edit(BufferView *, int x, int y, mouse_button::state button);
472 virtual void edit(BufferView *, bool front = true);
474 virtual void draw(BufferView *, LyXFont const &,
475 int baseline, float & x, bool cleared) const;
477 virtual bool insertInset(BufferView *, Inset *) { return false; }
479 virtual UpdatableInset * getLockingInset() const {
480 return const_cast<UpdatableInset *>(this);
483 virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
484 { return (c == lyxCode()) ? this : 0; }
486 virtual int insetInInsetY() const { return 0; }
488 virtual bool updateInsetInInset(BufferView *, Inset *)
491 virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
494 virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
497 /// An updatable inset could handle lyx editing commands
498 virtual RESULT localDispatch(FuncRequest const & cmd);
500 bool isCursorVisible() const { return cursor_visible_; }
502 virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
504 int scroll(bool recursive = true) const {
505 // We need this method to not clobber the real method in Inset
506 return Inset::scroll(recursive);
509 virtual bool showInsetDialog(BufferView *) const { return false; }
511 virtual void nodraw(bool b) const {
515 virtual bool nodraw() const {
516 return block_drawing_;
519 // needed for spellchecking text
521 virtual bool allowSpellcheck() { return false; }
523 virtual WordLangTuple const
524 selectNextWordToSpellcheck(BufferView *, float & value) const;
526 virtual void selectSelectedWord(BufferView *) { return; }
528 virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
532 // needed for search/replace functionality
534 virtual bool searchForward(BufferView *, string const &,
535 bool = true, bool = false);
537 virtual bool searchBackward(BufferView *, string const &,
538 bool = true, bool = false);
542 void toggleCursorVisible() const {
543 cursor_visible_ = !cursor_visible_;
546 void setCursorVisible(bool b) const {
549 /// scrolls to absolute position in bufferview-workwidth * sx units
550 void scroll(BufferView *, float sx) const;
551 /// scrolls offset pixels
552 void scroll(BufferView *, int offset) const;
556 mutable bool cursor_visible_;
558 mutable bool block_drawing_;
562 bool UpdatableInset::checkInsertChar(LyXFont &)
568 * returns true if pointer argument is valid
569 * and points to an editable inset
571 inline bool isEditableInset(Inset * i)
573 return i && i->editable();
577 * returns true if pointer argument is valid
578 * and points to a highly editable inset
580 inline bool isHighlyEditableInset(Inset * i)
582 return i && i->editable() == Inset::HIGHLY_EDITABLE;