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 void edit(BufferView *, bool front = true);
153 virtual EDITABLE editable() const;
154 /// This is called when the user clicks inside an inset
155 virtual void insetButtonPress(BufferView *, int, int, int) {}
156 /// This is called when the user releases the button inside an inset
157 virtual void insetButtonRelease(BufferView *, int, int, int) {}
158 /// This is called when the user moves the mouse inside an inset
159 virtual void insetMotionNotify(BufferView *, int , int , int) {}
161 virtual bool isTextInset() const { return false; }
163 virtual bool doClearArea() const { return true; }
165 virtual bool autoDelete() const;
166 /// returns true the inset can hold an inset of given type
167 virtual bool insetAllowed(Inset::Code) const { return false; }
168 /// wrapper around the above
169 bool insetAllowed(Inset * in) const {
170 return insetAllowed(in->lyxCode());
173 virtual void write(Buffer const *, std::ostream &) const = 0;
175 virtual void read(Buffer const *, LyXLex & lex) = 0;
176 /** returns the number of rows (\n's) of generated tex code.
177 fragile == true means, that the inset should take care about
178 fragile commands by adding a \protect before.
179 If the free_spc (freespacing) variable is set, then this inset
180 is in a free-spacing paragraph.
182 virtual int latex(Buffer const *, std::ostream &, bool fragile,
183 bool free_spc) const = 0;
185 virtual int ascii(Buffer const *,
186 std::ostream &, int linelen = 0) const = 0;
188 virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
190 virtual int docBook(Buffer const *, std::ostream &) const = 0;
191 /// Updates needed features for this inset.
192 virtual void validate(LaTeXFeatures & features) const;
194 virtual bool deletable() const;
196 /// returns LyX code associated with the inset. Used for TOC, ...)
197 virtual Inset::Code lyxCode() const { return NO_CODE; }
199 virtual std::vector<string> const getLabelList() const {
200 return std::vector<string>();
204 virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
206 /// returns true to override begin and end inset in file
207 virtual bool directWrite() const;
209 /// Returns true if the inset should be centered alone
210 virtual bool display() const { return false; }
211 /// Changes the display state of the inset
212 virtual void display(bool) {}
214 /// returns true if this inset needs a row on it's own
216 virtual bool needFullRow() const { return false; }
218 void setInsetName(string const & s) { name = s; }
220 string const getInsetName() const { return name; }
222 void setOwner(Inset * inset) { owner_ = inset; }
224 Inset * owner() const { return owner_; }
226 int x() const { return top_x; }
228 int y() const { return top_baseline; }
230 // because we could have fake text insets and have to call this
231 // inside them without cast!!!
233 virtual LyXText * getLyXText(BufferView const *,
234 bool const recursive = false) const;
236 virtual void deleteLyXText(BufferView *, bool = true) const {}
238 virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
239 /// returns the actuall scroll-value
240 virtual int scroll(bool recursive=true) const {
241 if (!recursive || !owner_)
245 /// try to get a paragraph pointer from it's id if we have a
246 /// paragraph to give back!
247 virtual Paragraph * getParFromID(int /* id */) const {
250 /// try to get a inset pointer from it's id if we have
251 /// an inset to give back!
252 virtual Inset * getInsetFromID(int /* id */) const {
255 /// if this insets owns paragraphs (f.ex. InsetText) then it
256 /// should return it's very first one!
257 virtual Paragraph * firstParagraph() const {
260 /// return the cursor if we own one otherwise giv'em just the
261 /// BufferView cursor to work with.
262 virtual LyXCursor const & cursor(BufferView * bview) const;
267 /// used to toggle insets
268 // is the inset open?
269 virtual bool isOpen() const { return false; }
270 // open or close the inset, depending on the bool
271 virtual void open(BufferView *, bool) {}
276 mutable int top_baseline;
282 static unsigned int inset_id;
292 // Updatable Insets. These insets can be locked and receive
293 // directly user interaction. Currently used only for mathed.
294 // Note that all pure methods from Inset class are pure here too.
295 // [Alejandro 080596]
297 /** Extracted from Matthias notes:
299 * An inset can simple call LockInset in it's edit call and *ONLY*
302 * Unlocking is either done by LyX or the inset itself with a
305 * During the lock, all button and keyboard events will be modified
306 * and send to the inset through the following inset-features. Note that
307 * Inset::insetUnlock will be called from inside UnlockInset. It is meant
308 * to contain the code for restoring the menus and things like this.
310 * If a inset wishes any redraw and/or update it just has to call
313 * It's is completly irrelevant, where the inset is. UpdateInset will
314 * find it in any paragraph in any buffer.
315 * Of course the_locking_inset and the insets in the current paragraph/buffer
316 * are checked first, so no performance problem should occur.
318 class UpdatableInset : public Inset {
320 /** Dispatch result codes
321 Now that nested updatable insets are allowed, the local dispatch
322 becomes a bit complex, just two possible results (boolean)
325 DISPATCHED = the inset catched the action
326 DISPATCHED_NOUPDATE = the inset catched the action and no update
327 is needed here to redraw the inset
328 FINISHED = the inset must be unlocked as a result
330 UNDISPATCHED = the action was not catched, it should be
331 dispatched by lower level insets
340 /// To convert old binary dispatch results
341 RESULT DISPATCH_RESULT(bool b) {
342 return b ? DISPATCHED : FINISHED;
346 UpdatableInset() : cursor_visible_(false), block_drawing_(false) {}
349 virtual EDITABLE editable() const;
351 /// may call ToggleLockedInsetCursor
352 virtual void toggleInsetCursor(BufferView *);
354 virtual void showInsetCursor(BufferView *, bool show = true);
356 virtual void hideInsetCursor(BufferView *);
358 virtual void fitInsetCursor(BufferView *) const;
360 virtual void getCursorPos(BufferView *, int &, int &) const {}
362 virtual void insetButtonPress(BufferView *, int x, int y, int button);
364 virtual void insetButtonRelease(BufferView *,
365 int x, int y, int button);
367 virtual void insetKeyPress(XKeyEvent * ev);
369 virtual void insetMotionNotify(BufferView *, int x, int y, int state);
371 virtual void insetUnlock(BufferView *);
373 virtual void edit(BufferView *, int x, int y, unsigned int button);
375 virtual void edit(BufferView *, bool front = true);
377 virtual void draw(BufferView *, LyXFont const &,
378 int baseline, float & x, bool cleared) const;
380 virtual void setFont(BufferView *, LyXFont const &,
381 bool toggleall = false, bool selectall = false);
383 virtual bool insertInset(BufferView *, Inset *) { return false; }
385 virtual UpdatableInset * getLockingInset() const {
386 return const_cast<UpdatableInset *>(this);
389 virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
390 { return (c == lyxCode()) ? this : 0; }
392 virtual unsigned int insetInInsetY() { return 0; }
394 virtual bool updateInsetInInset(BufferView *, Inset *)
397 virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
400 virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
403 /// An updatable inset could handle lyx editing commands
404 virtual RESULT localDispatch(BufferView *, kb_action, string const &);
406 bool isCursorVisible() const { return cursor_visible_; }
408 virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
410 int scroll(bool recursive = true) const {
411 // We need this method to not clobber the real method in Inset
412 return Inset::scroll(recursive);
415 virtual bool showInsetDialog(BufferView *) const { return false; }
417 virtual void nodraw(bool b) {
421 virtual bool nodraw() const {
422 return block_drawing_;
425 // needed for spellchecking text
427 virtual string selectNextWord(BufferView *, float & value) const;
428 virtual void selectSelectedWord(BufferView *) { return; }
429 virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
433 // needed for search/replace functionality
435 virtual bool searchForward(BufferView *, string const &,
436 bool const & = true, bool const & = false);
437 virtual bool searchBackward(BufferView *, string const &,
438 bool const & = true, bool const & = false);
442 void toggleCursorVisible() const {
443 cursor_visible_ = !cursor_visible_;
446 void setCursorVisible(bool b) const {
449 /// scrolls to absolute position in bufferview-workwidth * sx units
450 void scroll(BufferView *, float sx) const;
451 /// scrolls offset pixels
452 void scroll(BufferView *, int offset) const;
456 mutable bool cursor_visible_;