2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995-2001 the LyX Team.
9 * ====================================================== */
21 #include "commandtags.h"
39 /** This is not quite the correct place for this enum. I think
40 the correct would be to let each subclass of Inset declare
41 its own enum code. Actually the notion of an Inset::Code
42 should be avoided, but I am not sure how this could be done
48 TOC_CODE, // do these insets really need a code? (ale)
94 SPECIALCHAR_CODE, // 25
106 MATHMACRO_CODE, // 30
120 TEXT_TO_INSET_OFFSET = 2
136 Inset(Inset const & in, bool same_id = false);
140 virtual int ascent(BufferView *, LyXFont const &) const = 0;
142 virtual int descent(BufferView *, LyXFont const &) const = 0;
144 virtual int width(BufferView *, LyXFont const &) const = 0;
146 virtual void draw(BufferView *, LyXFont const &,
147 int baseline, float & x, bool cleared) const = 0;
148 /// update the inset representation
149 virtual void update(BufferView *, LyXFont const &, bool = false)
151 /// what appears in the minibuffer when opening
152 virtual string const editMessage() const;
154 virtual void edit(BufferView *, int x, int y, unsigned int button);
156 virtual void edit(BufferView *, bool front = true);
158 virtual EDITABLE editable() const;
159 /// This is called when the user clicks inside an inset
160 virtual void insetButtonPress(BufferView *, int, int, int) {}
161 /// This is called when the user releases the button inside an inset
162 virtual void insetButtonRelease(BufferView *, int, int, int) {}
163 /// This is called when the user moves the mouse inside an inset
164 virtual void insetMotionNotify(BufferView *, int , int , int) {}
166 virtual bool isTextInset() const { return false; }
168 virtual bool doClearArea() const { return true; }
170 virtual bool autoDelete() const;
171 /// returns true the inset can hold an inset of given type
172 virtual bool insetAllowed(Inset::Code) const { return false; }
173 /// wrapper around the above
174 bool insetAllowed(Inset * in) const;
176 virtual void write(Buffer const *, std::ostream &) const = 0;
178 virtual void read(Buffer const *, LyXLex & lex) = 0;
179 /** returns the number of rows (\n's) of generated tex code.
180 fragile == true means, that the inset should take care about
181 fragile commands by adding a \protect before.
182 If the free_spc (freespacing) variable is set, then this inset
183 is in a free-spacing paragraph.
185 virtual int latex(Buffer const *, std::ostream &, bool fragile,
186 bool free_spc) const = 0;
188 virtual int ascii(Buffer const *,
189 std::ostream &, int linelen = 0) const = 0;
191 virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
193 virtual int docbook(Buffer const *, std::ostream &) const = 0;
194 /// Updates needed features for this inset.
195 virtual void validate(LaTeXFeatures & features) const;
197 virtual bool deletable() const;
199 /// returns LyX code associated with the inset. Used for TOC, ...)
200 virtual Inset::Code lyxCode() const { return NO_CODE; }
202 virtual std::vector<string> const getLabelList() const {
203 return std::vector<string>();
207 virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
209 /// returns true to override begin and end inset in file
210 virtual bool directWrite() const;
212 /// Returns true if the inset should be centered alone
213 virtual bool display() const { return false; }
214 /// Changes the display state of the inset
215 virtual void display(bool) {}
217 /// returns true if this inset needs a row on it's own
219 virtual bool needFullRow() const { return false; }
221 void setInsetName(string const & s) { name_ = s; }
223 string const & getInsetName() const { return name_; }
225 void setOwner(Inset * inset) { owner_ = inset; }
227 Inset * owner() const { return owner_; }
229 void parOwner(Paragraph * par) { par_owner_ = par; }
231 Paragraph * parOwner() const {return par_owner_; }
233 void setBackgroundColor(LColor::color);
235 LColor::color backgroundColor() const;
237 int x() const { return top_x; }
239 int y() const { return top_baseline; }
241 // because we could have fake text insets and have to call this
242 // inside them without cast!!!
244 virtual LyXText * getLyXText(BufferView const *,
245 bool const recursive = false) const;
247 virtual void deleteLyXText(BufferView *, bool = true) const {}
249 virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
250 /// returns the actuall scroll-value
251 virtual int scroll(bool recursive=true) const {
252 if (!recursive || !owner_)
256 /// try to get a paragraph pointer from it's id if we have a
257 /// paragraph to give back!
258 virtual Paragraph * getParFromID(int /* id */) const {
261 /// try to get a inset pointer from it's id if we have
262 /// an inset to give back!
263 virtual Inset * getInsetFromID(int /* id */) const {
266 /// if this insets owns paragraphs (f.ex. InsetText) then it
267 /// should return it's very first one!
268 virtual Paragraph * firstParagraph() const {
273 virtual Paragraph * getFirstParagraph(int /*num*/) const {
277 /// return the cursor if we own one otherwise giv'em just the
278 /// BufferView cursor to work with.
279 virtual LyXCursor const & cursor(BufferView * bview) const;
285 /// used to toggle insets
286 // is the inset open?
287 virtual bool isOpen() const { return false; }
289 virtual void open(BufferView *) {}
291 virtual void close(BufferView *) const {}
292 /// check if the font of the char we want inserting is correct
293 /// and modify it if it is not.
294 virtual bool checkInsertChar(LyXFont &);
295 /// we need this here because collapsed insets are only EDITABLE
296 virtual void setFont(BufferView *, LyXFont const &,
297 bool toggleall = false, bool selectall = false);
299 // needed for spellchecking text
301 virtual bool allowSpellcheck() { return false; }
303 // is this equivalent to a letter?
304 virtual bool isLetter() const { return false; }
305 // is this equivalent to a space?
306 virtual bool isSpace() const { return false; }
312 mutable bool topx_set; /* have we already drawn ourself! */
314 mutable int top_baseline;
320 static unsigned int inset_id;
325 /// the paragraph in which this inset has been inserted
326 Paragraph * par_owner_;
330 LColor::color background_color_;
335 bool Inset::insetAllowed(Inset * in) const
337 return insetAllowed(in->lyxCode());
342 bool Inset::checkInsertChar(LyXFont &)
347 // Updatable Insets. These insets can be locked and receive
348 // directly user interaction. Currently used only for mathed.
349 // Note that all pure methods from Inset class are pure here too.
350 // [Alejandro 080596]
352 /** Extracted from Matthias notes:
354 * An inset can simple call LockInset in it's edit call and *ONLY*
357 * Unlocking is either done by LyX or the inset itself with a
360 * During the lock, all button and keyboard events will be modified
361 * and send to the inset through the following inset-features. Note that
362 * Inset::insetUnlock will be called from inside UnlockInset. It is meant
363 * to contain the code for restoring the menus and things like this.
365 * If a inset wishes any redraw and/or update it just has to call
368 * It's is completly irrelevant, where the inset is. UpdateInset will
369 * find it in any paragraph in any buffer.
370 * Of course the_locking_inset and the insets in the current paragraph/buffer
371 * are checked first, so no performance problem should occur.
373 class UpdatableInset : public Inset {
375 /** Dispatch result codes
376 Now that nested updatable insets are allowed, the local dispatch
377 becomes a bit complex, just two possible results (boolean)
380 DISPATCHED = the inset catched the action
381 DISPATCHED_NOUPDATE = the inset catched the action and no update
382 is needed here to redraw the inset
383 FINISHED = the inset must be unlocked as a result
385 FINISHED_RIGHT = FINISHED, but put the cursor to the RIGHT of
387 FINISHED_UP = FINISHED, but put the cursor UP of
389 FINISHED_DOWN = FINISHED, but put the cursor DOWN of
391 UNDISPATCHED = the action was not catched, it should be
392 dispatched by lower level insets
404 /// To convert old binary dispatch results
405 RESULT DISPATCH_RESULT(bool b) {
406 return b ? DISPATCHED : FINISHED;
412 UpdatableInset(UpdatableInset const & in, bool same_id = false);
414 /// check if the font of the char we want inserting is correct
415 /// and modify it if it is not.
416 virtual bool checkInsertChar(LyXFont &);
418 virtual EDITABLE editable() const;
420 /// may call ToggleLockedInsetCursor
421 virtual void toggleInsetCursor(BufferView *);
423 virtual void showInsetCursor(BufferView *, bool show = true);
425 virtual void hideInsetCursor(BufferView *);
427 virtual void fitInsetCursor(BufferView *) const;
429 virtual void getCursorPos(BufferView *, int &, int &) const {}
431 virtual void insetButtonPress(BufferView *, int x, int y, int button);
433 virtual void insetButtonRelease(BufferView *,
434 int x, int y, int button);
436 virtual void insetKeyPress(XKeyEvent * ev);
438 virtual void insetMotionNotify(BufferView *, int x, int y, int state);
440 virtual void insetUnlock(BufferView *);
442 virtual void edit(BufferView *, int x, int y, unsigned int button);
444 virtual void edit(BufferView *, bool front = true);
446 virtual void draw(BufferView *, LyXFont const &,
447 int baseline, float & x, bool cleared) const;
449 virtual bool insertInset(BufferView *, Inset *) { return false; }
451 virtual UpdatableInset * getLockingInset() const {
452 return const_cast<UpdatableInset *>(this);
455 virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
456 { return (c == lyxCode()) ? this : 0; }
458 virtual unsigned int insetInInsetY() { return 0; }
460 virtual bool updateInsetInInset(BufferView *, Inset *)
463 virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
466 virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
469 /// An updatable inset could handle lyx editing commands
470 virtual RESULT localDispatch(BufferView *, kb_action, string const &);
472 bool isCursorVisible() const { return cursor_visible_; }
474 virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
476 int scroll(bool recursive = true) const {
477 // We need this method to not clobber the real method in Inset
478 return Inset::scroll(recursive);
481 virtual bool showInsetDialog(BufferView *) const { return false; }
483 virtual void nodraw(bool b) const {
487 virtual bool nodraw() const {
488 return block_drawing_;
491 // needed for spellchecking text
493 virtual bool allowSpellcheck() { return false; }
495 virtual string const selectNextWordToSpellcheck(BufferView *, float & value) const;
497 virtual void selectSelectedWord(BufferView *) { return; }
499 virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
503 // needed for search/replace functionality
505 virtual bool searchForward(BufferView *, string const &,
506 bool const & = true, bool const & = false);
508 virtual bool searchBackward(BufferView *, string const &,
509 bool const & = true, bool const & = false);
513 void toggleCursorVisible() const {
514 cursor_visible_ = !cursor_visible_;
517 void setCursorVisible(bool b) const {
520 /// scrolls to absolute position in bufferview-workwidth * sx units
521 void scroll(BufferView *, float sx) const;
522 /// scrolls offset pixels
523 void scroll(BufferView *, int offset) const;
527 mutable bool cursor_visible_;
529 mutable bool block_drawing_;
533 bool UpdatableInset::checkInsertChar(LyXFont &)
539 * returns true if pointer argument is valid
540 * and points to an editable inset
542 inline bool isEditableInset(Inset * i)
544 return i && i->editable();
548 * returns true if pointer argument is valid
549 * and points to a highly editable inset
551 inline bool isHighlyEditableInset(Inset * i)
553 return i && i->editable() == Inset::HIGHLY_EDITABLE;