2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995-2001 the LyX Team.
9 * ====================================================== */
21 #include "commandtags.h"
38 /** This is not quite the correct place for this enum. I think
39 the correct would be to let each subclass of Inset declare
40 its own enum code. Actually the notion of an Inset::Code
41 should be avoided, but I am not sure how this could be done
47 TOC_CODE, // do these insets really need a code? (ale)
93 SPECIALCHAR_CODE, // 25
103 MATHMACRO_CODE, // 30
115 TEXT_TO_INSET_OFFSET = 2
129 Inset() : top_x(0), top_baseline(0), scx(0), id_(inset_id++), owner_(0) {}
130 /// Virtual base destructor
133 virtual int ascent(BufferView *, LyXFont const &) const = 0;
135 virtual int descent(BufferView *, LyXFont const &) const = 0;
137 virtual int width(BufferView *, LyXFont const &) const = 0;
139 virtual void draw(BufferView *, LyXFont const &,
140 int baseline, float & x, bool cleared) const = 0;
141 /// update the inset representation
142 virtual void update(BufferView *, LyXFont const &, bool = false)
145 virtual LyXFont const convertFont(LyXFont const & font) const;
146 /// what appears in the minibuffer when opening
147 virtual string const editMessage() const;
149 virtual void edit(BufferView *, int x, int y, unsigned int button);
151 virtual EDITABLE editable() const;
152 /// This is called when the user clicks inside an inset
153 virtual void insetButtonPress(BufferView *, int, int, int) {}
154 /// This is called when the user releases the button inside an inset
155 virtual void insetButtonRelease(BufferView *, int, int, int) {}
156 /// This is called when the user moves the mouse inside an inset
157 virtual void insetMotionNotify(BufferView *, int , int , int) {}
159 virtual bool isTextInset() const { return false; }
161 virtual bool doClearArea() const { return true; }
163 virtual bool autoDelete() const;
165 virtual void write(Buffer const *, std::ostream &) const = 0;
167 virtual void read(Buffer const *, LyXLex & lex) = 0;
168 /** returns the number of rows (\n's) of generated tex code.
169 fragile == true means, that the inset should take care about
170 fragile commands by adding a \protect before.
171 If the free_spc (freespacing) variable is set, then this inset
172 is in a free-spacing paragraph.
174 virtual int latex(Buffer const *, std::ostream &, bool fragile,
175 bool free_spc) const = 0;
177 virtual int ascii(Buffer const *,
178 std::ostream &, int linelen = 0) const = 0;
180 virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
182 virtual int docBook(Buffer const *, std::ostream &) const = 0;
183 /// Updates needed features for this inset.
184 virtual void validate(LaTeXFeatures & features) const;
186 virtual bool deletable() const;
188 /// returns LyX code associated with the inset. Used for TOC, ...)
189 virtual Inset::Code lyxCode() const { return NO_CODE; }
191 virtual std::vector<string> const getLabelList() const {
192 return std::vector<string>();
196 virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
198 /// returns true to override begin and end inset in file
199 virtual bool directWrite() const;
201 /// Returns true if the inset should be centered alone
202 virtual bool display() const { return false; }
203 /// Changes the display state of the inset
204 virtual void display(bool) {}
206 /// returns true if this inset needs a row on it's own
208 virtual bool needFullRow() const { return false; }
210 virtual bool insertInsetAllowed(Inset *) const { return false; }
212 void setInsetName(string const & s) { name = s; }
214 string const getInsetName() const { return name; }
216 void setOwner(Inset * inset) { owner_ = inset; }
218 Inset * owner() const { return owner_; }
220 int x() const { return top_x; }
222 int y() const { return top_baseline; }
224 // because we could have fake text insets and have to call this
225 // inside them without cast!!!
227 virtual LyXText * getLyXText(BufferView const *,
228 bool const recursive = false) const;
230 virtual void deleteLyXText(BufferView *, bool = true) const {}
232 virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
233 /// returns the actuall scroll-value
234 virtual int scroll(bool recursive=true) const {
235 if (!recursive || !owner_)
239 /// try to get a paragraph pointer from it's id if we have a
240 /// paragraph to give back!
241 virtual Paragraph * getParFromID(int /* id */) const {
244 /// try to get a inset pointer from it's id if we have
245 /// an inset to give back!
246 virtual Inset * getInsetFromID(int /* id */) const {
249 /// if this insets owns paragraphs (f.ex. InsetText) then it
250 /// should return it's very first one!
251 virtual Paragraph * firstParagraph() const {
254 /// return the cursor if we own one otherwise giv'em just the
255 /// BufferView cursor to work with.
256 virtual LyXCursor const & cursor(BufferView * bview) const;
265 mutable int top_baseline;
271 static unsigned int inset_id;
281 // Updatable Insets. These insets can be locked and receive
282 // directly user interaction. Currently used only for mathed.
283 // Note that all pure methods from Inset class are pure here too.
284 // [Alejandro 080596]
286 /** Extracted from Matthias notes:
288 * An inset can simple call LockInset in it's edit call and *ONLY*
291 * Unlocking is either done by LyX or the inset itself with a
294 * During the lock, all button and keyboard events will be modified
295 * and send to the inset through the following inset-features. Note that
296 * Inset::insetUnlock will be called from inside UnlockInset. It is meant
297 * to contain the code for restoring the menus and things like this.
299 * If a inset wishes any redraw and/or update it just has to call
302 * It's is completly irrelevant, where the inset is. UpdateInset will
303 * find it in any paragraph in any buffer.
304 * Of course the_locking_inset and the insets in the current paragraph/buffer
305 * are checked first, so no performance problem should occur.
307 class UpdatableInset : public Inset {
309 /** Dispatch result codes
310 Now that nested updatable insets are allowed, the local dispatch
311 becomes a bit complex, just two possible results (boolean)
314 DISPATCHED = the inset catched the action
315 DISPATCHED_NOUPDATE = the inset catched the action and no update
316 is needed here to redraw the inset
317 FINISHED = the inset must be unlocked as a result
319 UNDISPATCHED = the action was not catched, it should be
320 dispatched by lower level insets
329 /// To convert old binary dispatch results
330 RESULT DISPATCH_RESULT(bool b) {
331 return b ? DISPATCHED : FINISHED;
335 UpdatableInset() : cursor_visible_(false), block_drawing_(false) {}
338 virtual EDITABLE editable() const;
340 /// may call ToggleLockedInsetCursor
341 virtual void toggleInsetCursor(BufferView *);
343 virtual void showInsetCursor(BufferView *, bool show = true);
345 virtual void hideInsetCursor(BufferView *);
347 virtual void getCursorPos(BufferView *, int &, int &) const {}
349 virtual void insetButtonPress(BufferView *, int x, int y, int button);
351 virtual void insetButtonRelease(BufferView *,
352 int x, int y, int button);
354 virtual void insetKeyPress(XKeyEvent * ev);
356 virtual void insetMotionNotify(BufferView *, int x, int y, int state);
358 virtual void insetUnlock(BufferView *);
360 virtual void edit(BufferView *, int x, int y, unsigned int button);
362 virtual void draw(BufferView *, LyXFont const &,
363 int baseline, float & x, bool cleared) const;
365 virtual void setFont(BufferView *, LyXFont const &,
366 bool toggleall = false, bool selectall = false);
368 virtual bool insertInset(BufferView *, Inset *) { return false; }
370 virtual bool insertInsetAllowed(Inset *) const { return false; }
372 virtual UpdatableInset * getLockingInset() const {
373 return const_cast<UpdatableInset *>(this);
376 virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
377 { return (c == lyxCode()) ? this : 0; }
379 virtual unsigned int insetInInsetY() { return 0; }
381 virtual bool updateInsetInInset(BufferView *, Inset *)
384 virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
387 virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
390 /// An updatable inset could handle lyx editing commands
391 virtual RESULT localDispatch(BufferView *, kb_action, string const &);
393 bool isCursorVisible() const { return cursor_visible_; }
395 virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
397 int scroll(bool recursive = true) const {
398 // We need this method to not clobber the real method in Inset
399 return Inset::scroll(recursive);
402 virtual bool showInsetDialog(BufferView *) const { return false; }
404 virtual void nodraw(bool b) {
408 virtual bool nodraw() const {
409 return block_drawing_;
414 void toggleCursorVisible() const {
415 cursor_visible_ = !cursor_visible_;
418 void setCursorVisible(bool b) const {
421 /// scrolls to absolute position in bufferview-workwidth * sx units
422 void scroll(BufferView *, float sx) const;
423 /// scrolls offset pixels
424 void scroll(BufferView *, int offset) const;
428 mutable bool cursor_visible_;