2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995-2001 the LyX Team.
9 * ====================================================== */
20 #include "commandtags.h"
21 #include "frontends/mouse_state.h"
42 /** This is not quite the correct place for this enum. I think
43 the correct would be to let each subclass of Inset declare
44 its own enum code. Actually the notion of an Inset::Code
45 should be avoided, but I am not sure how this could be done
51 TOC_CODE, // do these insets really need a code? (ale)
97 SPECIALCHAR_CODE, // 25
109 MATHMACRO_CODE, // 30
123 TEXT_TO_INSET_OFFSET = 2
139 Inset(Inset const & in, bool same_id = false);
143 virtual int ascent(BufferView *, LyXFont const &) const = 0;
145 virtual int descent(BufferView *, LyXFont const &) const = 0;
147 virtual int width(BufferView *, LyXFont const &) const = 0;
149 virtual void draw(BufferView *, LyXFont const &,
150 int baseline, float & x, bool cleared) const = 0;
151 /// update the inset representation
152 virtual void update(BufferView *, LyXFont const &, bool = false)
154 /// what appears in the minibuffer when opening
155 virtual string const editMessage() const;
157 virtual void edit(BufferView *, int x, int y, mouse_button::state button);
159 virtual void edit(BufferView *, bool front = true);
161 virtual EDITABLE editable() const;
162 /// This is called when the user clicks inside an inset
163 virtual void insetButtonPress(BufferView *, int, int, mouse_button::state) {}
164 /// This is called when the user releases the button inside an inset
165 // the bool return is used to see if we opened a dialog so that we can
166 // check this from an outer inset and open the dialog of the
167 // outer inset if that one has one!
168 virtual bool insetButtonRelease(BufferView *, int, int, mouse_button::state)
169 { return editable() == IS_EDITABLE; }
170 /// This is called when the user moves the mouse inside an inset
171 virtual void insetMotionNotify(BufferView *, int , int, mouse_button::state) {}
173 virtual bool isTextInset() const { return false; }
175 virtual bool doClearArea() const { return true; }
177 virtual bool autoDelete() const;
178 /// returns true the inset can hold an inset of given type
179 virtual bool insetAllowed(Inset::Code) const { return false; }
180 /// wrapper around the above
181 bool insetAllowed(Inset * in) const;
183 virtual void write(Buffer const *, std::ostream &) const = 0;
185 virtual void read(Buffer const *, LyXLex & lex) = 0;
186 /** returns the number of rows (\n's) of generated tex code.
187 fragile == true means, that the inset should take care about
188 fragile commands by adding a \protect before.
189 If the free_spc (freespacing) variable is set, then this inset
190 is in a free-spacing paragraph.
192 virtual int latex(Buffer const *, std::ostream &, bool fragile,
193 bool free_spc) const = 0;
195 virtual int ascii(Buffer const *,
196 std::ostream &, int linelen = 0) const = 0;
198 virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
200 virtual int docbook(Buffer const *, std::ostream &, bool) const = 0;
201 /// Updates needed features for this inset.
202 virtual void validate(LaTeXFeatures & features) const;
204 virtual bool deletable() const;
206 /// returns LyX code associated with the inset. Used for TOC, ...)
207 virtual Inset::Code lyxCode() const { return NO_CODE; }
209 virtual std::vector<string> const getLabelList() const {
210 return std::vector<string>();
214 virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
216 /// returns true to override begin and end inset in file
217 virtual bool directWrite() const;
219 /// Returns true if the inset should be centered alone
220 virtual bool display() const { return false; }
221 /// Changes the display state of the inset
222 virtual void display(bool) {}
224 /// returns true if this inset needs a row on it's own
226 virtual bool needFullRow() const { return false; }
228 void setInsetName(string const & s) { name_ = s; }
230 string const & getInsetName() const { return name_; }
232 void setOwner(Inset * inset) { owner_ = inset; }
234 Inset * owner() const { return owner_; }
236 void parOwner(Paragraph * par) { par_owner_ = par; }
238 Paragraph * parOwner() const {return par_owner_; }
240 void setBackgroundColor(LColor::color);
242 LColor::color backgroundColor() const;
244 int x() const { return top_x; }
246 int y() const { return top_baseline; }
248 // because we could have fake text insets and have to call this
249 // inside them without cast!!!
251 virtual LyXText * getLyXText(BufferView const *,
252 bool const recursive = false) const;
254 virtual void deleteLyXText(BufferView *, bool = true) const {}
256 virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
257 /// returns the actuall scroll-value
258 virtual int scroll(bool recursive=true) const {
259 if (!recursive || !owner_)
263 /// try to get a paragraph pointer from it's id if we have a
264 /// paragraph to give back!
265 virtual Paragraph * getParFromID(int /* id */) const {
268 /// try to get a inset pointer from it's id if we have
269 /// an inset to give back!
270 virtual Inset * getInsetFromID(int /* id */) const {
273 /// if this insets owns paragraphs (f.ex. InsetText) then it
274 /// should return it's very first one!
275 virtual Paragraph * firstParagraph() const {
280 virtual Paragraph * getFirstParagraph(int /*num*/) const {
284 /// return the cursor if we own one otherwise giv'em just the
285 /// BufferView cursor to work with.
286 virtual LyXCursor const & cursor(BufferView * bview) const;
292 /// used to toggle insets
293 // is the inset open?
294 virtual bool isOpen() const { return false; }
296 virtual void open(BufferView *) {}
298 virtual void close(BufferView *) const {}
299 /// check if the font of the char we want inserting is correct
300 /// and modify it if it is not.
301 virtual bool checkInsertChar(LyXFont &);
302 /// we need this here because collapsed insets are only EDITABLE
303 virtual void setFont(BufferView *, LyXFont const &,
304 bool toggleall = false, bool selectall = false);
306 // needed for spellchecking text
308 virtual bool allowSpellcheck() { return false; }
310 // should this inset be handled like a normal charater
311 virtual bool isChar() const { return false; }
312 // is this equivalent to a letter?
313 virtual bool isLetter() const { return false; }
314 // is this equivalent to a space (which is BTW different from
315 // a line separator)?
316 virtual bool isSpace() const { return false; }
317 // should we break lines after this inset?
318 virtual bool isLineSeparator() const { return false; }
319 // if this inset has paragraphs should they be output all as default
320 // paragraphs with "Standard" layout?
321 virtual bool forceDefaultParagraphs(Inset const *) const;
322 /** returns true if, when outputing LaTeX, font changes should
323 be closed before generating this inset. This is needed for
324 insets that may contain several paragraphs */
325 virtual bool noFontChange() const { return false; }
327 virtual void getDrawFont(LyXFont &) const {}
328 /* needed for widths which are % of something
329 returns the value of \textwidth in this inset. Most of the
330 time this is the width of the workarea, but if there is a
331 minipage somewhere, it will be the width of this minipage */
332 virtual int latexTextWidth(BufferView *) const;
334 /** Adds a LaTeX snippet to the Preview Loader for transformation
335 * into a bitmap image. Also connects to PreviewLoader::imageReady
336 * so that the inset is informed when the image has been generated
337 * in order to initiate its loading into LyX.
339 * Most insets have no interest in this capability, so the method
342 virtual void generatePreview(grfx::PreviewLoader &) const {}
348 mutable bool topx_set; /* have we already drawn ourself! */
350 mutable int top_baseline;
356 static unsigned int inset_id;
361 /// the paragraph in which this inset has been inserted
362 Paragraph * par_owner_;
366 LColor::color background_color_;
371 bool Inset::insetAllowed(Inset * in) const
373 return insetAllowed(in->lyxCode());
378 bool Inset::checkInsertChar(LyXFont &)
383 // Updatable Insets. These insets can be locked and receive
384 // directly user interaction. Currently used only for mathed.
385 // Note that all pure methods from Inset class are pure here too.
386 // [Alejandro 080596]
388 /** Extracted from Matthias notes:
390 * An inset can simple call LockInset in it's edit call and *ONLY*
393 * Unlocking is either done by LyX or the inset itself with a
396 * During the lock, all button and keyboard events will be modified
397 * and send to the inset through the following inset-features. Note that
398 * Inset::insetUnlock will be called from inside UnlockInset. It is meant
399 * to contain the code for restoring the menus and things like this.
401 * If a inset wishes any redraw and/or update it just has to call
404 * It's is completly irrelevant, where the inset is. UpdateInset will
405 * find it in any paragraph in any buffer.
406 * Of course the_locking_inset and the insets in the current paragraph/buffer
407 * are checked first, so no performance problem should occur.
409 class UpdatableInset : public Inset {
411 /** Dispatch result codes
412 Now that nested updatable insets are allowed, the local dispatch
413 becomes a bit complex, just two possible results (boolean)
416 DISPATCHED = the inset catched the action
417 DISPATCHED_NOUPDATE = the inset catched the action and no update
418 is needed here to redraw the inset
419 FINISHED = the inset must be unlocked as a result
421 FINISHED_RIGHT = FINISHED, but put the cursor to the RIGHT of
423 FINISHED_UP = FINISHED, but put the cursor UP of
425 FINISHED_DOWN = FINISHED, but put the cursor DOWN of
427 UNDISPATCHED = the action was not catched, it should be
428 dispatched by lower level insets
440 /// To convert old binary dispatch results
441 RESULT DISPATCH_RESULT(bool b) {
442 return b ? DISPATCHED : FINISHED;
448 UpdatableInset(UpdatableInset const & in, bool same_id = false);
450 /// check if the font of the char we want inserting is correct
451 /// and modify it if it is not.
452 virtual bool checkInsertChar(LyXFont &);
454 virtual EDITABLE editable() const;
457 virtual void toggleInsetCursor(BufferView *);
459 virtual void showInsetCursor(BufferView *, bool show = true);
461 virtual void hideInsetCursor(BufferView *);
463 virtual void fitInsetCursor(BufferView *) const;
465 virtual void getCursorPos(BufferView *, int &, int &) const {}
467 virtual void insetButtonPress(BufferView *, int x, int y, mouse_button::state button);
469 // the bool return is used to see if we opened a dialog so that we can
470 // check this from an outer inset and open the dialog of the outer inset
471 // if that one has one!
473 virtual bool insetButtonRelease(BufferView *,
474 int x, int y, mouse_button::state button);
476 virtual void insetMotionNotify(BufferView *, int x, int y, mouse_button::state state);
478 virtual void insetUnlock(BufferView *);
480 virtual void edit(BufferView *, int x, int y, mouse_button::state button);
482 virtual void edit(BufferView *, bool front = true);
484 virtual void draw(BufferView *, LyXFont const &,
485 int baseline, float & x, bool cleared) const;
487 virtual bool insertInset(BufferView *, Inset *) { return false; }
489 virtual UpdatableInset * getLockingInset() const {
490 return const_cast<UpdatableInset *>(this);
493 virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
494 { return (c == lyxCode()) ? this : 0; }
496 virtual int insetInInsetY() const { return 0; }
498 virtual bool updateInsetInInset(BufferView *, Inset *)
501 virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
504 virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
507 /// An updatable inset could handle lyx editing commands
508 virtual RESULT localDispatch(BufferView *, kb_action, string const &);
510 bool isCursorVisible() const { return cursor_visible_; }
512 virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
514 int scroll(bool recursive = true) const {
515 // We need this method to not clobber the real method in Inset
516 return Inset::scroll(recursive);
519 virtual bool showInsetDialog(BufferView *) const { return false; }
521 virtual void nodraw(bool b) const {
525 virtual bool nodraw() const {
526 return block_drawing_;
529 // needed for spellchecking text
531 virtual bool allowSpellcheck() { return false; }
533 virtual string const selectNextWordToSpellcheck(BufferView *, float & value) const;
535 virtual void selectSelectedWord(BufferView *) { return; }
537 virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
541 // needed for search/replace functionality
543 virtual bool searchForward(BufferView *, string const &,
544 bool = true, bool = false);
546 virtual bool searchBackward(BufferView *, string const &,
547 bool = true, bool = false);
551 void toggleCursorVisible() const {
552 cursor_visible_ = !cursor_visible_;
555 void setCursorVisible(bool b) const {
558 /// scrolls to absolute position in bufferview-workwidth * sx units
559 void scroll(BufferView *, float sx) const;
560 /// scrolls offset pixels
561 void scroll(BufferView *, int offset) const;
565 mutable bool cursor_visible_;
567 mutable bool block_drawing_;
571 bool UpdatableInset::checkInsertChar(LyXFont &)
577 * returns true if pointer argument is valid
578 * and points to an editable inset
580 inline bool isEditableInset(Inset * i)
582 return i && i->editable();
586 * returns true if pointer argument is valid
587 * and points to a highly editable inset
589 inline bool isHighlyEditableInset(Inset * i)
591 return i && i->editable() == Inset::HIGHLY_EDITABLE;