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
124 TEXT_TO_INSET_OFFSET = 2
137 /** Dispatch result codes
138 Now that nested updatable insets are allowed, the local dispatch
139 becomes a bit complex, just two possible results (boolean)
142 DISPATCHED = the inset catched the action
143 DISPATCHED_NOUPDATE = the inset catched the action and no update
144 is needed here to redraw the inset
145 FINISHED = the inset must be unlocked as a result
147 FINISHED_RIGHT = FINISHED, but put the cursor to the RIGHT of
149 FINISHED_UP = FINISHED, but put the cursor UP of
151 FINISHED_DOWN = FINISHED, but put the cursor DOWN of
153 UNDISPATCHED = the action was not catched, it should be
154 dispatched by lower level insets
166 /// To convert old binary dispatch results
167 RESULT DISPATCH_RESULT(bool b) {
168 return b ? DISPATCHED : FINISHED;
174 Inset(Inset const & in, bool same_id = false);
178 virtual int ascent(BufferView *, LyXFont const &) const = 0;
180 virtual int descent(BufferView *, LyXFont const &) const = 0;
182 virtual int width(BufferView *, LyXFont const &) const = 0;
184 virtual void draw(BufferView *, LyXFont const &,
185 int baseline, float & x, bool cleared) const = 0;
186 /// update the inset representation
187 virtual void update(BufferView *, LyXFont const &, bool = false)
189 /// what appears in the minibuffer when opening
190 virtual string const editMessage() const;
192 virtual void edit(BufferView *, int x, int y, mouse_button::state button);
194 virtual void edit(BufferView *, bool front = true);
196 virtual EDITABLE editable() const;
198 virtual RESULT localDispatch(FuncRequest const & cmd);
200 virtual bool isTextInset() const { return false; }
202 virtual bool doClearArea() const { return true; }
204 virtual bool autoDelete() const;
205 /// returns true the inset can hold an inset of given type
206 virtual bool insetAllowed(Inset::Code) const { return false; }
207 /// wrapper around the above
208 bool insetAllowed(Inset * in) const;
210 virtual void write(Buffer const *, std::ostream &) const = 0;
212 virtual void read(Buffer const *, LyXLex & lex) = 0;
213 /** returns the number of rows (\n's) of generated tex code.
214 fragile == true means, that the inset should take care about
215 fragile commands by adding a \protect before.
216 If the free_spc (freespacing) variable is set, then this inset
217 is in a free-spacing paragraph.
219 virtual int latex(Buffer const *, std::ostream &, bool fragile,
220 bool free_spc) const = 0;
222 virtual int ascii(Buffer const *,
223 std::ostream &, int linelen = 0) const = 0;
225 virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
227 virtual int docbook(Buffer const *, std::ostream &, bool) const = 0;
228 /// Updates needed features for this inset.
229 virtual void validate(LaTeXFeatures & features) const;
231 virtual bool deletable() const;
233 /// returns LyX code associated with the inset. Used for TOC, ...)
234 virtual Inset::Code lyxCode() const { return NO_CODE; }
236 virtual std::vector<string> const getLabelList() const {
237 return std::vector<string>();
241 virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
243 /// returns true to override begin and end inset in file
244 virtual bool directWrite() const;
246 /// Returns true if the inset should be centered alone
247 virtual bool display() const { return false; }
248 /// Changes the display state of the inset
249 virtual void display(bool) {}
251 /// returns true if this inset needs a row on it's own
253 virtual bool needFullRow() const { return false; }
255 void setInsetName(string const & s) { name_ = s; }
257 string const & getInsetName() const { return name_; }
259 void setOwner(Inset * inset) { owner_ = inset; }
261 Inset * owner() const { return owner_; }
263 void parOwner(Paragraph * par) { par_owner_ = par; }
265 Paragraph * parOwner() const {return par_owner_; }
267 void setBackgroundColor(LColor::color);
269 LColor::color backgroundColor() const;
271 int x() const { return top_x; }
273 int y() const { return top_baseline; }
275 // because we could have fake text insets and have to call this
276 // inside them without cast!!!
278 virtual LyXText * getLyXText(BufferView const *,
279 bool const recursive = false) const;
281 virtual void deleteLyXText(BufferView *, bool = true) const {}
283 virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
284 /// returns the actuall scroll-value
285 virtual int scroll(bool recursive=true) const {
286 if (!recursive || !owner_)
290 /// try to get a paragraph pointer from it's id if we have a
291 /// paragraph to give back!
292 virtual Paragraph * getParFromID(int /* id */) const {
295 /// try to get a inset pointer from it's id if we have
296 /// an inset to give back!
297 virtual Inset * getInsetFromID(int /* id */) const {
300 /// if this insets owns paragraphs (f.ex. InsetText) then it
301 /// should return it's very first one!
302 virtual Paragraph * firstParagraph() const {
307 virtual Paragraph * getFirstParagraph(int /*num*/) const {
311 /// return the cursor if we own one otherwise giv'em just the
312 /// BufferView cursor to work with.
313 virtual LyXCursor const & cursor(BufferView * bview) const;
319 /// used to toggle insets
320 // is the inset open?
321 virtual bool isOpen() const { return false; }
323 virtual void open(BufferView *) {}
325 virtual void close(BufferView *) const {}
326 /// check if the font of the char we want inserting is correct
327 /// and modify it if it is not.
328 virtual bool checkInsertChar(LyXFont &);
329 /// we need this here because collapsed insets are only EDITABLE
330 virtual void setFont(BufferView *, LyXFont const &,
331 bool toggleall = false, bool selectall = false);
333 // needed for spellchecking text
335 virtual bool allowSpellcheck() { return false; }
337 // should this inset be handled like a normal charater
338 virtual bool isChar() const { return false; }
339 // is this equivalent to a letter?
340 virtual bool isLetter() const { return false; }
341 // is this equivalent to a space (which is BTW different from
342 // a line separator)?
343 virtual bool isSpace() const { return false; }
344 // should we break lines after this inset?
345 virtual bool isLineSeparator() const { return false; }
346 // if this inset has paragraphs should they be output all as default
347 // paragraphs with "Standard" layout?
348 virtual bool forceDefaultParagraphs(Inset const *) const;
349 /** returns true if, when outputing LaTeX, font changes should
350 be closed before generating this inset. This is needed for
351 insets that may contain several paragraphs */
352 virtual bool noFontChange() const { return false; }
354 virtual void getDrawFont(LyXFont &) const {}
355 /* needed for widths which are % of something
356 returns the value of \textwidth in this inset. Most of the
357 time this is the width of the workarea, but if there is a
358 minipage somewhere, it will be the width of this minipage */
359 virtual int latexTextWidth(BufferView *) const;
361 /** Adds a LaTeX snippet to the Preview Loader for transformation
362 * into a bitmap image. Does not start the laoding process.
364 * Most insets have no interest in this capability, so the method
367 virtual void addPreview(grfx::PreviewLoader &) const {}
369 /** Find the PreviewLoader, add a LaTeX snippet to it and
370 * start the loading process.
372 * Most insets have no interest in this capability, so the method
375 virtual void generatePreview() const {}
381 mutable bool topx_set; /* have we already drawn ourself! */
383 mutable int top_baseline;
389 static unsigned int inset_id;
394 /// the paragraph in which this inset has been inserted
395 Paragraph * par_owner_;
399 LColor::color background_color_;
404 bool Inset::insetAllowed(Inset * in) const
406 return insetAllowed(in->lyxCode());
411 bool Inset::checkInsertChar(LyXFont &)
416 // Updatable Insets. These insets can be locked and receive
417 // directly user interaction. Currently used only for mathed.
418 // Note that all pure methods from Inset class are pure here too.
419 // [Alejandro 080596]
421 /** Extracted from Matthias notes:
423 * An inset can simple call LockInset in it's edit call and *ONLY*
426 * Unlocking is either done by LyX or the inset itself with a
429 * During the lock, all button and keyboard events will be modified
430 * and send to the inset through the following inset-features. Note that
431 * Inset::insetUnlock will be called from inside UnlockInset. It is meant
432 * to contain the code for restoring the menus and things like this.
434 * If a inset wishes any redraw and/or update it just has to call
437 * It's is completly irrelevant, where the inset is. UpdateInset will
438 * find it in any paragraph in any buffer.
439 * Of course the_locking_inset and the insets in the current paragraph/buffer
440 * are checked first, so no performance problem should occur.
442 class UpdatableInset : public Inset {
447 UpdatableInset(UpdatableInset const & in, bool same_id = false);
449 /// check if the font of the char we want inserting is correct
450 /// and modify it if it is not.
451 virtual bool checkInsertChar(LyXFont &);
453 virtual EDITABLE editable() const;
456 virtual void toggleInsetCursor(BufferView *);
458 virtual void showInsetCursor(BufferView *, bool show = true);
460 virtual void hideInsetCursor(BufferView *);
462 virtual void fitInsetCursor(BufferView *) const;
464 virtual void getCursorPos(BufferView *, int &, int &) const {}
466 virtual void insetUnlock(BufferView *);
468 virtual void edit(BufferView *, int x, int y, mouse_button::state button);
470 virtual void edit(BufferView *, bool front = true);
472 virtual void draw(BufferView *, LyXFont const &,
473 int baseline, float & x, bool cleared) const;
475 virtual bool insertInset(BufferView *, Inset *) { return false; }
477 virtual UpdatableInset * getLockingInset() const {
478 return const_cast<UpdatableInset *>(this);
481 virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
482 { return (c == lyxCode()) ? this : 0; }
484 virtual int insetInInsetY() const { return 0; }
486 virtual bool updateInsetInInset(BufferView *, Inset *)
489 virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
492 virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
495 /// An updatable inset could handle lyx editing commands
496 virtual RESULT localDispatch(FuncRequest const & cmd);
498 bool isCursorVisible() const { return cursor_visible_; }
500 virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
502 int scroll(bool recursive = true) const {
503 // We need this method to not clobber the real method in Inset
504 return Inset::scroll(recursive);
507 virtual bool showInsetDialog(BufferView *) const { return false; }
509 virtual void nodraw(bool b) const {
513 virtual bool nodraw() const {
514 return block_drawing_;
517 // needed for spellchecking text
519 virtual bool allowSpellcheck() { return false; }
521 virtual WordLangTuple const
522 selectNextWordToSpellcheck(BufferView *, float & value) const;
524 virtual void selectSelectedWord(BufferView *) { return; }
526 virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
530 // needed for search/replace functionality
532 virtual bool searchForward(BufferView *, string const &,
533 bool = true, bool = false);
535 virtual bool searchBackward(BufferView *, string const &,
536 bool = true, bool = false);
540 void toggleCursorVisible() const {
541 cursor_visible_ = !cursor_visible_;
544 void setCursorVisible(bool b) const {
547 /// scrolls to absolute position in bufferview-workwidth * sx units
548 void scroll(BufferView *, float sx) const;
549 /// scrolls offset pixels
550 void scroll(BufferView *, int offset) const;
554 mutable bool cursor_visible_;
556 mutable bool block_drawing_;
560 bool UpdatableInset::checkInsertChar(LyXFont &)
566 * returns true if pointer argument is valid
567 * and points to an editable inset
569 inline bool isEditableInset(Inset * i)
571 return i && i->editable();
575 * returns true if pointer argument is valid
576 * and points to a highly editable inset
578 inline bool isHighlyEditableInset(Inset * i)
580 return i && i->editable() == Inset::HIGHLY_EDITABLE;