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
164 // outer inset if that one has one!
165 virtual bool insetButtonRelease(BufferView *, int, int, int)
166 { return editable() == IS_EDITABLE; }
167 /// This is called when the user moves the mouse inside an inset
168 virtual void insetMotionNotify(BufferView *, int , int , int) {}
170 virtual bool isTextInset() const { return false; }
172 virtual bool doClearArea() const { return true; }
174 virtual bool autoDelete() const;
175 /// returns true the inset can hold an inset of given type
176 virtual bool insetAllowed(Inset::Code) const { return false; }
177 /// wrapper around the above
178 bool insetAllowed(Inset * in) const;
180 virtual void write(Buffer const *, std::ostream &) const = 0;
182 virtual void read(Buffer const *, LyXLex & lex) = 0;
183 /** returns the number of rows (\n's) of generated tex code.
184 fragile == true means, that the inset should take care about
185 fragile commands by adding a \protect before.
186 If the free_spc (freespacing) variable is set, then this inset
187 is in a free-spacing paragraph.
189 virtual int latex(Buffer const *, std::ostream &, bool fragile,
190 bool free_spc) const = 0;
192 virtual int ascii(Buffer const *,
193 std::ostream &, int linelen = 0) const = 0;
195 virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
197 virtual int docbook(Buffer const *, std::ostream &) const = 0;
198 /// Updates needed features for this inset.
199 virtual void validate(LaTeXFeatures & features) const;
201 virtual bool deletable() const;
203 /// returns LyX code associated with the inset. Used for TOC, ...)
204 virtual Inset::Code lyxCode() const { return NO_CODE; }
206 virtual std::vector<string> const getLabelList() const {
207 return std::vector<string>();
211 virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
213 /// returns true to override begin and end inset in file
214 virtual bool directWrite() const;
216 /// Returns true if the inset should be centered alone
217 virtual bool display() const { return false; }
218 /// Changes the display state of the inset
219 virtual void display(bool) {}
221 /// returns true if this inset needs a row on it's own
223 virtual bool needFullRow() const { return false; }
225 void setInsetName(string const & s) { name_ = s; }
227 string const & getInsetName() const { return name_; }
229 void setOwner(Inset * inset) { owner_ = inset; }
231 Inset * owner() const { return owner_; }
233 void parOwner(Paragraph * par) { par_owner_ = par; }
235 Paragraph * parOwner() const {return par_owner_; }
237 void setBackgroundColor(LColor::color);
239 LColor::color backgroundColor() const;
241 int x() const { return top_x; }
243 int y() const { return top_baseline; }
245 // because we could have fake text insets and have to call this
246 // inside them without cast!!!
248 virtual LyXText * getLyXText(BufferView const *,
249 bool const recursive = false) const;
251 virtual void deleteLyXText(BufferView *, bool = true) const {}
253 virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
254 /// returns the actuall scroll-value
255 virtual int scroll(bool recursive=true) const {
256 if (!recursive || !owner_)
260 /// try to get a paragraph pointer from it's id if we have a
261 /// paragraph to give back!
262 virtual Paragraph * getParFromID(int /* id */) const {
265 /// try to get a inset pointer from it's id if we have
266 /// an inset to give back!
267 virtual Inset * getInsetFromID(int /* id */) const {
270 /// if this insets owns paragraphs (f.ex. InsetText) then it
271 /// should return it's very first one!
272 virtual Paragraph * firstParagraph() const {
277 virtual Paragraph * getFirstParagraph(int /*num*/) const {
281 /// return the cursor if we own one otherwise giv'em just the
282 /// BufferView cursor to work with.
283 virtual LyXCursor const & cursor(BufferView * bview) const;
289 /// used to toggle insets
290 // is the inset open?
291 virtual bool isOpen() const { return false; }
293 virtual void open(BufferView *) {}
295 virtual void close(BufferView *) const {}
296 /// check if the font of the char we want inserting is correct
297 /// and modify it if it is not.
298 virtual bool checkInsertChar(LyXFont &);
299 /// we need this here because collapsed insets are only EDITABLE
300 virtual void setFont(BufferView *, LyXFont const &,
301 bool toggleall = false, bool selectall = false);
303 // needed for spellchecking text
305 virtual bool allowSpellcheck() { return false; }
307 // should this inset be handled like a normal charater
308 virtual bool isChar() const { return false; }
309 // is this equivalent to a letter?
310 virtual bool isLetter() const { return false; }
311 // is this equivalent to a space (which is BTW different from
312 // a line separator)?
313 virtual bool isSpace() const { return false; }
314 // should we break lines after this inset?
315 virtual bool isLineSeparator() const { return false; }
316 // if this inset has paragraphs should they be output all as default
317 // paragraphs with "Standard" layout?
318 virtual bool forceDefaultParagraphs(Inset const *) const;
320 virtual void getDrawFont(LyXFont &) const {}
321 /* needed for widths which are % of something
322 returns the value of \textwidth in this inset. Most of the
323 time this is the width of the workarea, but if there is a
324 minipage somewhere, it will be the width of this minipage */
325 virtual int latexTextWidth(BufferView *) const;
331 mutable bool topx_set; /* have we already drawn ourself! */
333 mutable int top_baseline;
339 static unsigned int inset_id;
344 /// the paragraph in which this inset has been inserted
345 Paragraph * par_owner_;
349 LColor::color background_color_;
354 bool Inset::insetAllowed(Inset * in) const
356 return insetAllowed(in->lyxCode());
361 bool Inset::checkInsertChar(LyXFont &)
366 // Updatable Insets. These insets can be locked and receive
367 // directly user interaction. Currently used only for mathed.
368 // Note that all pure methods from Inset class are pure here too.
369 // [Alejandro 080596]
371 /** Extracted from Matthias notes:
373 * An inset can simple call LockInset in it's edit call and *ONLY*
376 * Unlocking is either done by LyX or the inset itself with a
379 * During the lock, all button and keyboard events will be modified
380 * and send to the inset through the following inset-features. Note that
381 * Inset::insetUnlock will be called from inside UnlockInset. It is meant
382 * to contain the code for restoring the menus and things like this.
384 * If a inset wishes any redraw and/or update it just has to call
387 * It's is completly irrelevant, where the inset is. UpdateInset will
388 * find it in any paragraph in any buffer.
389 * Of course the_locking_inset and the insets in the current paragraph/buffer
390 * are checked first, so no performance problem should occur.
392 class UpdatableInset : public Inset {
394 /** Dispatch result codes
395 Now that nested updatable insets are allowed, the local dispatch
396 becomes a bit complex, just two possible results (boolean)
399 DISPATCHED = the inset catched the action
400 DISPATCHED_NOUPDATE = the inset catched the action and no update
401 is needed here to redraw the inset
402 FINISHED = the inset must be unlocked as a result
404 FINISHED_RIGHT = FINISHED, but put the cursor to the RIGHT of
406 FINISHED_UP = FINISHED, but put the cursor UP of
408 FINISHED_DOWN = FINISHED, but put the cursor DOWN of
410 UNDISPATCHED = the action was not catched, it should be
411 dispatched by lower level insets
423 /// To convert old binary dispatch results
424 RESULT DISPATCH_RESULT(bool b) {
425 return b ? DISPATCHED : FINISHED;
431 UpdatableInset(UpdatableInset const & in, bool same_id = false);
433 /// check if the font of the char we want inserting is correct
434 /// and modify it if it is not.
435 virtual bool checkInsertChar(LyXFont &);
437 virtual EDITABLE editable() const;
440 virtual void toggleInsetCursor(BufferView *);
442 virtual void showInsetCursor(BufferView *, bool show = true);
444 virtual void hideInsetCursor(BufferView *);
446 virtual void fitInsetCursor(BufferView *) const;
448 virtual void getCursorPos(BufferView *, int &, int &) const {}
450 virtual void insetButtonPress(BufferView *, int x, int y, int button);
452 // the bool return is used to see if we opened a dialog so that we can
453 // check this from an outer inset and open the dialog of the outer inset
454 // if that one has one!
456 virtual bool insetButtonRelease(BufferView *,
457 int x, int y, int button);
459 virtual void insetKeyPress(XKeyEvent * ev);
461 virtual void insetMotionNotify(BufferView *, int x, int y, int state);
463 virtual void insetUnlock(BufferView *);
465 virtual void edit(BufferView *, int x, int y, unsigned int button);
467 virtual void edit(BufferView *, bool front = true);
469 virtual void draw(BufferView *, LyXFont const &,
470 int baseline, float & x, bool cleared) const;
472 virtual bool insertInset(BufferView *, Inset *) { return false; }
474 virtual UpdatableInset * getLockingInset() const {
475 return const_cast<UpdatableInset *>(this);
478 virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
479 { return (c == lyxCode()) ? this : 0; }
481 virtual int insetInInsetY() const { return 0; }
483 virtual bool updateInsetInInset(BufferView *, Inset *)
486 virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
489 virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
492 /// An updatable inset could handle lyx editing commands
493 virtual RESULT localDispatch(BufferView *, kb_action, string const &);
495 bool isCursorVisible() const { return cursor_visible_; }
497 virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
499 int scroll(bool recursive = true) const {
500 // We need this method to not clobber the real method in Inset
501 return Inset::scroll(recursive);
504 virtual bool showInsetDialog(BufferView *) const { return false; }
506 virtual void nodraw(bool b) const {
510 virtual bool nodraw() const {
511 return block_drawing_;
514 // needed for spellchecking text
516 virtual bool allowSpellcheck() { return false; }
518 virtual string const selectNextWordToSpellcheck(BufferView *, float & value) const;
520 virtual void selectSelectedWord(BufferView *) { return; }
522 virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
526 // needed for search/replace functionality
528 virtual bool searchForward(BufferView *, string const &,
529 bool = true, bool = false);
531 virtual bool searchBackward(BufferView *, string const &,
532 bool = true, bool = false);
536 void toggleCursorVisible() const {
537 cursor_visible_ = !cursor_visible_;
540 void setCursorVisible(bool b) const {
543 /// scrolls to absolute position in bufferview-workwidth * sx units
544 void scroll(BufferView *, float sx) const;
545 /// scrolls offset pixels
546 void scroll(BufferView *, int offset) const;
550 mutable bool cursor_visible_;
552 mutable bool block_drawing_;
556 bool UpdatableInset::checkInsertChar(LyXFont &)
562 * returns true if pointer argument is valid
563 * and points to an editable inset
565 inline bool isEditableInset(Inset * i)
567 return i && i->editable();
571 * returns true if pointer argument is valid
572 * and points to a highly editable inset
574 inline bool isHighlyEditableInset(Inset * i)
576 return i && i->editable() == Inset::HIGHLY_EDITABLE;