4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Alejandro Aguilar Sierra
9 * \author Lars Gullik Bjønnes
10 * \author Matthias Ettrich
12 * Full author contact details are available in file CREDITS
22 #include "insetbase.h"
23 #include "frontends/mouse_state.h"
24 #include "support/types.h"
44 class Inset : public InsetBase {
46 /** This is not quite the correct place for this enum. I think
47 the correct would be to let each subclass of Inset declare
48 its own enum code. Actually the notion of an Inset::Code
49 should be avoided, but I am not sure how this could be done
55 TOC_CODE, // do these insets really need a code? (ale)
103 SPECIALCHAR_CODE, // 25
115 MATHMACRO_CODE, // 30
131 TEXT_TO_INSET_OFFSET = 2
145 typedef InsetBase::dispatch_result RESULT;
150 Inset(Inset const & in, bool same_id = false);
154 virtual int ascent(BufferView *, LyXFont const &) const = 0;
156 virtual int descent(BufferView *, LyXFont const &) const = 0;
158 virtual int width(BufferView *, LyXFont const &) const = 0;
160 virtual void draw(BufferView *, LyXFont const &,
161 int baseline, float & x, bool cleared) const = 0;
162 /// update the inset representation
163 virtual void update(BufferView *, LyXFont const &, bool = false)
165 /// what appears in the minibuffer when opening
166 virtual string const editMessage() const;
168 virtual void edit(BufferView *, int x, int y, mouse_button::state button);
170 virtual void edit(BufferView *, bool front = true);
172 virtual EDITABLE editable() const;
174 virtual RESULT localDispatch(FuncRequest const & cmd);
176 virtual bool isTextInset() const { return false; }
178 virtual bool doClearArea() const { return true; }
179 /// return true if the inset should be removed automatically
180 virtual bool autoDelete() const;
181 /// returns true the inset can hold an inset of given type
182 virtual bool insetAllowed(Inset::Code) const { return false; }
183 /// wrapper around the above
184 bool insetAllowed(Inset * in) const;
186 virtual void write(Buffer const *, std::ostream &) const = 0;
188 virtual void read(Buffer const *, LyXLex & lex) = 0;
189 /** returns the number of rows (\n's) of generated tex code.
190 fragile == true means, that the inset should take care about
191 fragile commands by adding a \protect before.
192 If the free_spc (freespacing) variable is set, then this inset
193 is in a free-spacing paragraph.
195 virtual int latex(Buffer const *, std::ostream &, bool fragile,
196 bool free_spc) const = 0;
198 virtual int ascii(Buffer const *,
199 std::ostream &, int linelen = 0) const = 0;
201 virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
203 virtual int docbook(Buffer const *, std::ostream &, bool) const = 0;
204 /// Updates needed features for this inset.
205 virtual void validate(LaTeXFeatures & features) 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_)
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() const { 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;
319 /** returns true if, when outputing LaTeX, font changes should
320 be closed before generating this inset. This is needed for
321 insets that may contain several paragraphs */
322 virtual bool noFontChange() const { return false; }
324 virtual void getDrawFont(LyXFont &) const {}
325 /* needed for widths which are % of something
326 returns the value of \textwidth in this inset. Most of the
327 time this is the width of the workarea, but if there is a
328 minipage somewhere, it will be the width of this minipage */
329 virtual int latexTextWidth(BufferView *) const;
331 /// mark the inset contents as erased (for change tracking)
332 virtual void markErased() {}
334 /** Adds a LaTeX snippet to the Preview Loader for transformation
335 * into a bitmap image. Does not start the laoding process.
337 * Most insets have no interest in this capability, so the method
340 virtual void addPreview(grfx::PreviewLoader &) const {}
342 /** Find the PreviewLoader, add a LaTeX snippet to it and
343 * start the loading process.
345 * Most insets have no interest in this capability, so the method
348 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 {
421 UpdatableInset(UpdatableInset const & in, bool same_id = false);
423 /// check if the font of the char we want inserting is correct
424 /// and modify it if it is not.
425 virtual bool checkInsertChar(LyXFont &);
427 virtual EDITABLE editable() const;
430 virtual void toggleInsetCursor(BufferView *);
432 virtual void showInsetCursor(BufferView *, bool show = true);
434 virtual void hideInsetCursor(BufferView *);
436 virtual void fitInsetCursor(BufferView *) const;
438 virtual void getCursorPos(BufferView *, int &, int &) const {}
440 virtual void insetUnlock(BufferView *);
442 virtual void edit(BufferView *, int x, int y, mouse_button::state 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 int insetInInsetY() const { 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(FuncRequest const & cmd);
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() const { return false; }
495 virtual WordLangTuple const
496 selectNextWordToSpellcheck(BufferView *, float & value) const;
498 virtual void selectSelectedWord(BufferView *) {}
500 virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
504 /// find the next change in the inset
505 virtual bool nextChange(BufferView * bv, lyx::pos_type & length);
508 // needed for search/replace functionality
510 virtual bool searchForward(BufferView *, string const &,
511 bool = true, bool = false);
513 virtual bool searchBackward(BufferView *, string const &,
514 bool = true, bool = false);
519 void toggleCursorVisible() const {
520 cursor_visible_ = !cursor_visible_;
523 void setCursorVisible(bool b) const {
526 /// scrolls to absolute position in bufferview-workwidth * sx units
527 void scroll(BufferView *, float sx) const;
528 /// scrolls offset pixels
529 void scroll(BufferView *, int offset) const;
533 mutable bool cursor_visible_;
535 mutable bool block_drawing_;
539 bool UpdatableInset::checkInsertChar(LyXFont &)
545 * returns true if pointer argument is valid
546 * and points to an editable inset
548 inline bool isEditableInset(Inset * i)
550 return i && i->editable();
554 * returns true if pointer argument is valid
555 * and points to a highly editable inset
557 inline bool isHighlyEditableInset(Inset * i)
559 return i && i->editable() == Inset::HIGHLY_EDITABLE;