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"
22 #include "WordLangTuple.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
140 Inset(Inset const & in, bool same_id = false);
144 virtual int ascent(BufferView *, LyXFont const &) const = 0;
146 virtual int descent(BufferView *, LyXFont const &) const = 0;
148 virtual int width(BufferView *, LyXFont const &) const = 0;
150 virtual void draw(BufferView *, LyXFont const &,
151 int baseline, float & x, bool cleared) const = 0;
152 /// update the inset representation
153 virtual void update(BufferView *, LyXFont const &, bool = false)
155 /// what appears in the minibuffer when opening
156 virtual string const editMessage() const;
158 virtual void edit(BufferView *, int x, int y, mouse_button::state button);
160 virtual void edit(BufferView *, bool front = true);
162 virtual EDITABLE editable() const;
163 /// This is called when the user clicks inside an inset
164 virtual void insetButtonPress(BufferView *, int, int, mouse_button::state) {}
165 /// This is called when the user releases the button inside an inset
166 // the bool return is used to see if we opened a dialog so that we can
167 // check this from an outer inset and open the dialog of the
168 // outer inset if that one has one!
169 virtual bool insetButtonRelease(BufferView *, int, int, mouse_button::state)
170 { return editable() == IS_EDITABLE; }
171 /// This is called when the user moves the mouse inside an inset
172 virtual void insetMotionNotify(BufferView *, int , int, mouse_button::state) {}
174 virtual bool isTextInset() const { return false; }
176 virtual bool doClearArea() const { return true; }
178 virtual bool autoDelete() const;
179 /// returns true the inset can hold an inset of given type
180 virtual bool insetAllowed(Inset::Code) const { return false; }
181 /// wrapper around the above
182 bool insetAllowed(Inset * in) const;
184 virtual void write(Buffer const *, std::ostream &) const = 0;
186 virtual void read(Buffer const *, LyXLex & lex) = 0;
187 /** returns the number of rows (\n's) of generated tex code.
188 fragile == true means, that the inset should take care about
189 fragile commands by adding a \protect before.
190 If the free_spc (freespacing) variable is set, then this inset
191 is in a free-spacing paragraph.
193 virtual int latex(Buffer const *, std::ostream &, bool fragile,
194 bool free_spc) const = 0;
196 virtual int ascii(Buffer const *,
197 std::ostream &, int linelen = 0) const = 0;
199 virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
201 virtual int docbook(Buffer const *, std::ostream &, bool) const = 0;
202 /// Updates needed features for this inset.
203 virtual void validate(LaTeXFeatures & features) const;
205 virtual bool deletable() const;
207 /// returns LyX code associated with the inset. Used for TOC, ...)
208 virtual Inset::Code lyxCode() const { return NO_CODE; }
210 virtual std::vector<string> const getLabelList() const {
211 return std::vector<string>();
215 virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
217 /// returns true to override begin and end inset in file
218 virtual bool directWrite() const;
220 /// Returns true if the inset should be centered alone
221 virtual bool display() const { return false; }
222 /// Changes the display state of the inset
223 virtual void display(bool) {}
225 /// returns true if this inset needs a row on it's own
227 virtual bool needFullRow() const { return false; }
229 void setInsetName(string const & s) { name_ = s; }
231 string const & getInsetName() const { return name_; }
233 void setOwner(Inset * inset) { owner_ = inset; }
235 Inset * owner() const { return owner_; }
237 void parOwner(Paragraph * par) { par_owner_ = par; }
239 Paragraph * parOwner() const {return par_owner_; }
241 void setBackgroundColor(LColor::color);
243 LColor::color backgroundColor() const;
245 int x() const { return top_x; }
247 int y() const { return top_baseline; }
249 // because we could have fake text insets and have to call this
250 // inside them without cast!!!
252 virtual LyXText * getLyXText(BufferView const *,
253 bool const recursive = false) const;
255 virtual void deleteLyXText(BufferView *, bool = true) const {}
257 virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
258 /// returns the actuall scroll-value
259 virtual int scroll(bool recursive=true) const {
260 if (!recursive || !owner_)
264 /// try to get a paragraph pointer from it's id if we have a
265 /// paragraph to give back!
266 virtual Paragraph * getParFromID(int /* id */) const {
269 /// try to get a inset pointer from it's id if we have
270 /// an inset to give back!
271 virtual Inset * getInsetFromID(int /* id */) const {
274 /// if this insets owns paragraphs (f.ex. InsetText) then it
275 /// should return it's very first one!
276 virtual Paragraph * firstParagraph() const {
281 virtual Paragraph * getFirstParagraph(int /*num*/) const {
285 /// return the cursor if we own one otherwise giv'em just the
286 /// BufferView cursor to work with.
287 virtual LyXCursor const & cursor(BufferView * bview) const;
293 /// used to toggle insets
294 // is the inset open?
295 virtual bool isOpen() const { return false; }
297 virtual void open(BufferView *) {}
299 virtual void close(BufferView *) const {}
300 /// check if the font of the char we want inserting is correct
301 /// and modify it if it is not.
302 virtual bool checkInsertChar(LyXFont &);
303 /// we need this here because collapsed insets are only EDITABLE
304 virtual void setFont(BufferView *, LyXFont const &,
305 bool toggleall = false, bool selectall = false);
307 // needed for spellchecking text
309 virtual bool allowSpellcheck() { return false; }
311 // should this inset be handled like a normal charater
312 virtual bool isChar() const { return false; }
313 // is this equivalent to a letter?
314 virtual bool isLetter() const { return false; }
315 // is this equivalent to a space (which is BTW different from
316 // a line separator)?
317 virtual bool isSpace() const { return false; }
318 // should we break lines after this inset?
319 virtual bool isLineSeparator() const { return false; }
320 // if this inset has paragraphs should they be output all as default
321 // paragraphs with "Standard" layout?
322 virtual bool forceDefaultParagraphs(Inset const *) const;
323 /** returns true if, when outputing LaTeX, font changes should
324 be closed before generating this inset. This is needed for
325 insets that may contain several paragraphs */
326 virtual bool noFontChange() const { return false; }
328 virtual void getDrawFont(LyXFont &) const {}
329 /* needed for widths which are % of something
330 returns the value of \textwidth in this inset. Most of the
331 time this is the width of the workarea, but if there is a
332 minipage somewhere, it will be the width of this minipage */
333 virtual int latexTextWidth(BufferView *) const;
335 /** Adds a LaTeX snippet to the Preview Loader for transformation
336 * into a bitmap image. Does not start the laoding process.
338 * Most insets have no interest in this capability, so the method
341 virtual void addPreview(grfx::PreviewLoader &) const {}
343 /** Find the PreviewLoader, add a LaTeX snippet to it and
344 * start the loading process.
346 * Most insets have no interest in this capability, so the method
349 virtual void generatePreview() const {}
355 mutable bool topx_set; /* have we already drawn ourself! */
357 mutable int top_baseline;
363 static unsigned int inset_id;
368 /// the paragraph in which this inset has been inserted
369 Paragraph * par_owner_;
373 LColor::color background_color_;
378 bool Inset::insetAllowed(Inset * in) const
380 return insetAllowed(in->lyxCode());
385 bool Inset::checkInsertChar(LyXFont &)
390 // Updatable Insets. These insets can be locked and receive
391 // directly user interaction. Currently used only for mathed.
392 // Note that all pure methods from Inset class are pure here too.
393 // [Alejandro 080596]
395 /** Extracted from Matthias notes:
397 * An inset can simple call LockInset in it's edit call and *ONLY*
400 * Unlocking is either done by LyX or the inset itself with a
403 * During the lock, all button and keyboard events will be modified
404 * and send to the inset through the following inset-features. Note that
405 * Inset::insetUnlock will be called from inside UnlockInset. It is meant
406 * to contain the code for restoring the menus and things like this.
408 * If a inset wishes any redraw and/or update it just has to call
411 * It's is completly irrelevant, where the inset is. UpdateInset will
412 * find it in any paragraph in any buffer.
413 * Of course the_locking_inset and the insets in the current paragraph/buffer
414 * are checked first, so no performance problem should occur.
416 class UpdatableInset : public Inset {
418 /** Dispatch result codes
419 Now that nested updatable insets are allowed, the local dispatch
420 becomes a bit complex, just two possible results (boolean)
423 DISPATCHED = the inset catched the action
424 DISPATCHED_NOUPDATE = the inset catched the action and no update
425 is needed here to redraw the inset
426 FINISHED = the inset must be unlocked as a result
428 FINISHED_RIGHT = FINISHED, but put the cursor to the RIGHT of
430 FINISHED_UP = FINISHED, but put the cursor UP of
432 FINISHED_DOWN = FINISHED, but put the cursor DOWN of
434 UNDISPATCHED = the action was not catched, it should be
435 dispatched by lower level insets
447 /// To convert old binary dispatch results
448 RESULT DISPATCH_RESULT(bool b) {
449 return b ? DISPATCHED : FINISHED;
455 UpdatableInset(UpdatableInset const & in, bool same_id = false);
457 /// check if the font of the char we want inserting is correct
458 /// and modify it if it is not.
459 virtual bool checkInsertChar(LyXFont &);
461 virtual EDITABLE editable() const;
464 virtual void toggleInsetCursor(BufferView *);
466 virtual void showInsetCursor(BufferView *, bool show = true);
468 virtual void hideInsetCursor(BufferView *);
470 virtual void fitInsetCursor(BufferView *) const;
472 virtual void getCursorPos(BufferView *, int &, int &) const {}
474 virtual void insetButtonPress(BufferView *, int x, int y, mouse_button::state button);
476 // the bool return is used to see if we opened a dialog so that we can
477 // check this from an outer inset and open the dialog of the outer inset
478 // if that one has one!
480 virtual bool insetButtonRelease(BufferView *,
481 int x, int y, mouse_button::state button);
483 virtual void insetMotionNotify(BufferView *, int x, int y, mouse_button::state state);
485 virtual void insetUnlock(BufferView *);
487 virtual void edit(BufferView *, int x, int y, mouse_button::state button);
489 virtual void edit(BufferView *, bool front = true);
491 virtual void draw(BufferView *, LyXFont const &,
492 int baseline, float & x, bool cleared) const;
494 virtual bool insertInset(BufferView *, Inset *) { return false; }
496 virtual UpdatableInset * getLockingInset() const {
497 return const_cast<UpdatableInset *>(this);
500 virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
501 { return (c == lyxCode()) ? this : 0; }
503 virtual int insetInInsetY() const { return 0; }
505 virtual bool updateInsetInInset(BufferView *, Inset *)
508 virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
511 virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
514 /// An updatable inset could handle lyx editing commands
515 virtual RESULT localDispatch(BufferView *, FuncRequest const & ev);
517 bool isCursorVisible() const { return cursor_visible_; }
519 virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
521 int scroll(bool recursive = true) const {
522 // We need this method to not clobber the real method in Inset
523 return Inset::scroll(recursive);
526 virtual bool showInsetDialog(BufferView *) const { return false; }
528 virtual void nodraw(bool b) const {
532 virtual bool nodraw() const {
533 return block_drawing_;
536 // needed for spellchecking text
538 virtual bool allowSpellcheck() { return false; }
540 virtual WordLangTuple selectNextWordToSpellcheck(BufferView *, float & value) const;
542 virtual void selectSelectedWord(BufferView *) { return; }
544 virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
548 // needed for search/replace functionality
550 virtual bool searchForward(BufferView *, string const &,
551 bool = true, bool = false);
553 virtual bool searchBackward(BufferView *, string const &,
554 bool = true, bool = false);
558 void toggleCursorVisible() const {
559 cursor_visible_ = !cursor_visible_;
562 void setCursorVisible(bool b) const {
565 /// scrolls to absolute position in bufferview-workwidth * sx units
566 void scroll(BufferView *, float sx) const;
567 /// scrolls offset pixels
568 void scroll(BufferView *, int offset) const;
572 mutable bool cursor_visible_;
574 mutable bool block_drawing_;
578 bool UpdatableInset::checkInsertChar(LyXFont &)
584 * returns true if pointer argument is valid
585 * and points to an editable inset
587 inline bool isEditableInset(Inset * i)
589 return i && i->editable();
593 * returns true if pointer argument is valid
594 * and points to a highly editable inset
596 inline bool isHighlyEditableInset(Inset * i)
598 return i && i->editable() == Inset::HIGHLY_EDITABLE;