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 // the bool return is used to see if we opened a dialog so that we can
163 // check this from an outer inset and open the dialog of the outer inset
164 // if that one has one!
166 virtual bool insetButtonRelease(BufferView *, int, int, int)
168 /// This is called when the user moves the mouse inside an inset
169 virtual void insetMotionNotify(BufferView *, int , int , int) {}
171 virtual bool isTextInset() const { return false; }
173 virtual bool doClearArea() const { return true; }
175 virtual bool autoDelete() const;
176 /// returns true the inset can hold an inset of given type
177 virtual bool insetAllowed(Inset::Code) const { return false; }
178 /// wrapper around the above
179 bool insetAllowed(Inset * in) const;
181 virtual void write(Buffer const *, std::ostream &) const = 0;
183 virtual void read(Buffer const *, LyXLex & lex) = 0;
184 /** returns the number of rows (\n's) of generated tex code.
185 fragile == true means, that the inset should take care about
186 fragile commands by adding a \protect before.
187 If the free_spc (freespacing) variable is set, then this inset
188 is in a free-spacing paragraph.
190 virtual int latex(Buffer const *, std::ostream &, bool fragile,
191 bool free_spc) const = 0;
193 virtual int ascii(Buffer const *,
194 std::ostream &, int linelen = 0) const = 0;
196 virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
198 virtual int docbook(Buffer const *, std::ostream &) const = 0;
199 /// Updates needed features for this inset.
200 virtual void validate(LaTeXFeatures & features) const;
202 virtual bool deletable() const;
204 /// returns LyX code associated with the inset. Used for TOC, ...)
205 virtual Inset::Code lyxCode() const { return NO_CODE; }
207 virtual std::vector<string> const getLabelList() const {
208 return std::vector<string>();
212 virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
214 /// returns true to override begin and end inset in file
215 virtual bool directWrite() const;
217 /// Returns true if the inset should be centered alone
218 virtual bool display() const { return false; }
219 /// Changes the display state of the inset
220 virtual void display(bool) {}
222 /// returns true if this inset needs a row on it's own
224 virtual bool needFullRow() const { return false; }
226 void setInsetName(string const & s) { name_ = s; }
228 string const & getInsetName() const { return name_; }
230 void setOwner(Inset * inset) { owner_ = inset; }
232 Inset * owner() const { return owner_; }
234 void parOwner(Paragraph * par) { par_owner_ = par; }
236 Paragraph * parOwner() const {return par_owner_; }
238 void setBackgroundColor(LColor::color);
240 LColor::color backgroundColor() const;
242 int x() const { return top_x; }
244 int y() const { return top_baseline; }
246 // because we could have fake text insets and have to call this
247 // inside them without cast!!!
249 virtual LyXText * getLyXText(BufferView const *,
250 bool const recursive = false) const;
252 virtual void deleteLyXText(BufferView *, bool = true) const {}
254 virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
255 /// returns the actuall scroll-value
256 virtual int scroll(bool recursive=true) const {
257 if (!recursive || !owner_)
261 /// try to get a paragraph pointer from it's id if we have a
262 /// paragraph to give back!
263 virtual Paragraph * getParFromID(int /* id */) const {
266 /// try to get a inset pointer from it's id if we have
267 /// an inset to give back!
268 virtual Inset * getInsetFromID(int /* id */) const {
271 /// if this insets owns paragraphs (f.ex. InsetText) then it
272 /// should return it's very first one!
273 virtual Paragraph * firstParagraph() const {
278 virtual Paragraph * getFirstParagraph(int /*num*/) const {
282 /// return the cursor if we own one otherwise giv'em just the
283 /// BufferView cursor to work with.
284 virtual LyXCursor const & cursor(BufferView * bview) const;
290 /// used to toggle insets
291 // is the inset open?
292 virtual bool isOpen() const { return false; }
294 virtual void open(BufferView *) {}
296 virtual void close(BufferView *) const {}
297 /// check if the font of the char we want inserting is correct
298 /// and modify it if it is not.
299 virtual bool checkInsertChar(LyXFont &);
300 /// we need this here because collapsed insets are only EDITABLE
301 virtual void setFont(BufferView *, LyXFont const &,
302 bool toggleall = false, bool selectall = false);
304 // needed for spellchecking text
306 virtual bool allowSpellcheck() { return false; }
308 // should this inset be handled like a normal charater
309 virtual bool isChar() const { return false; }
310 // is this equivalent to a letter?
311 virtual bool isLetter() const { return false; }
312 // is this equivalent to a space?
313 virtual bool isSpace() const { return false; }
314 // if this inset has paragraphs should they be outputed all as default
315 // paragraps with "Standard" layout?
316 virtual bool forceDefaultParagraphs(Inset const *) const;
322 mutable bool topx_set; /* have we already drawn ourself! */
324 mutable int top_baseline;
330 static unsigned int inset_id;
335 /// the paragraph in which this inset has been inserted
336 Paragraph * par_owner_;
340 LColor::color background_color_;
345 bool Inset::insetAllowed(Inset * in) const
347 return insetAllowed(in->lyxCode());
352 bool Inset::checkInsertChar(LyXFont &)
357 // Updatable Insets. These insets can be locked and receive
358 // directly user interaction. Currently used only for mathed.
359 // Note that all pure methods from Inset class are pure here too.
360 // [Alejandro 080596]
362 /** Extracted from Matthias notes:
364 * An inset can simple call LockInset in it's edit call and *ONLY*
367 * Unlocking is either done by LyX or the inset itself with a
370 * During the lock, all button and keyboard events will be modified
371 * and send to the inset through the following inset-features. Note that
372 * Inset::insetUnlock will be called from inside UnlockInset. It is meant
373 * to contain the code for restoring the menus and things like this.
375 * If a inset wishes any redraw and/or update it just has to call
378 * It's is completly irrelevant, where the inset is. UpdateInset will
379 * find it in any paragraph in any buffer.
380 * Of course the_locking_inset and the insets in the current paragraph/buffer
381 * are checked first, so no performance problem should occur.
383 class UpdatableInset : public Inset {
385 /** Dispatch result codes
386 Now that nested updatable insets are allowed, the local dispatch
387 becomes a bit complex, just two possible results (boolean)
390 DISPATCHED = the inset catched the action
391 DISPATCHED_NOUPDATE = the inset catched the action and no update
392 is needed here to redraw the inset
393 FINISHED = the inset must be unlocked as a result
395 FINISHED_RIGHT = FINISHED, but put the cursor to the RIGHT of
397 FINISHED_UP = FINISHED, but put the cursor UP of
399 FINISHED_DOWN = FINISHED, but put the cursor DOWN of
401 UNDISPATCHED = the action was not catched, it should be
402 dispatched by lower level insets
414 /// To convert old binary dispatch results
415 RESULT DISPATCH_RESULT(bool b) {
416 return b ? DISPATCHED : FINISHED;
422 UpdatableInset(UpdatableInset const & in, bool same_id = false);
424 /// check if the font of the char we want inserting is correct
425 /// and modify it if it is not.
426 virtual bool checkInsertChar(LyXFont &);
428 virtual EDITABLE editable() const;
430 /// may call ToggleLockedInsetCursor
431 virtual void toggleInsetCursor(BufferView *);
433 virtual void showInsetCursor(BufferView *, bool show = true);
435 virtual void hideInsetCursor(BufferView *);
437 virtual void fitInsetCursor(BufferView *) const;
439 virtual void getCursorPos(BufferView *, int &, int &) const {}
441 virtual void insetButtonPress(BufferView *, int x, int y, int button);
443 // the bool return is used to see if we opened a dialog so that we can
444 // check this from an outer inset and open the dialog of the outer inset
445 // if that one has one!
447 virtual bool insetButtonRelease(BufferView *,
448 int x, int y, int button);
450 virtual void insetKeyPress(XKeyEvent * ev);
452 virtual void insetMotionNotify(BufferView *, int x, int y, int state);
454 virtual void insetUnlock(BufferView *);
456 virtual void edit(BufferView *, int x, int y, unsigned int button);
458 virtual void edit(BufferView *, bool front = true);
460 virtual void draw(BufferView *, LyXFont const &,
461 int baseline, float & x, bool cleared) const;
463 virtual bool insertInset(BufferView *, Inset *) { return false; }
465 virtual UpdatableInset * getLockingInset() const {
466 return const_cast<UpdatableInset *>(this);
469 virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
470 { return (c == lyxCode()) ? this : 0; }
472 virtual unsigned int insetInInsetY() { return 0; }
474 virtual bool updateInsetInInset(BufferView *, Inset *)
477 virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
480 virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
483 /// An updatable inset could handle lyx editing commands
484 virtual RESULT localDispatch(BufferView *, kb_action, string const &);
486 bool isCursorVisible() const { return cursor_visible_; }
488 virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
490 int scroll(bool recursive = true) const {
491 // We need this method to not clobber the real method in Inset
492 return Inset::scroll(recursive);
495 virtual bool showInsetDialog(BufferView *) const { return false; }
497 virtual void nodraw(bool b) const {
501 virtual bool nodraw() const {
502 return block_drawing_;
505 // needed for spellchecking text
507 virtual bool allowSpellcheck() { return false; }
509 virtual string const selectNextWordToSpellcheck(BufferView *, float & value) const;
511 virtual void selectSelectedWord(BufferView *) { return; }
513 virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
517 // needed for search/replace functionality
519 virtual bool searchForward(BufferView *, string const &,
520 bool const & = true, bool const & = false);
522 virtual bool searchBackward(BufferView *, string const &,
523 bool const & = true, bool const & = false);
527 void toggleCursorVisible() const {
528 cursor_visible_ = !cursor_visible_;
531 void setCursorVisible(bool b) const {
534 /// scrolls to absolute position in bufferview-workwidth * sx units
535 void scroll(BufferView *, float sx) const;
536 /// scrolls offset pixels
537 void scroll(BufferView *, int offset) const;
541 mutable bool cursor_visible_;
543 mutable bool block_drawing_;
547 bool UpdatableInset::checkInsertChar(LyXFont &)
553 * returns true if pointer argument is valid
554 * and points to an editable inset
556 inline bool isEditableInset(Inset * i)
558 return i && i->editable();
562 * returns true if pointer argument is valid
563 * and points to a highly editable inset
565 inline bool isHighlyEditableInset(Inset * i)
567 return i && i->editable() == Inset::HIGHLY_EDITABLE;