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)
100 SPECIALCHAR_CODE, // 25
112 MATHMACRO_CODE, // 30
128 TEXT_TO_INSET_OFFSET = 2
141 /** Dispatch result codes
142 Now that nested updatable insets are allowed, the local dispatch
143 becomes a bit complex, just two possible results (boolean)
146 DISPATCHED = the inset catched the action
147 DISPATCHED_NOUPDATE = the inset catched the action and no update
148 is needed here to redraw the inset
149 FINISHED = the inset must be unlocked as a result
151 FINISHED_RIGHT = FINISHED, but put the cursor to the RIGHT of
153 FINISHED_UP = FINISHED, but put the cursor UP of
155 FINISHED_DOWN = FINISHED, but put the cursor DOWN of
157 UNDISPATCHED = the action was not catched, it should be
158 dispatched by lower level insets
170 /// To convert old binary dispatch results
171 RESULT DISPATCH_RESULT(bool b) {
172 return b ? DISPATCHED : FINISHED;
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() { 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 /** 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() { return false; }
525 virtual WordLangTuple const
526 selectNextWordToSpellcheck(BufferView *, float & value) const;
528 virtual void selectSelectedWord(BufferView *) { return; }
530 virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
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;