2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-2001 The LyX Team.
10 * ====================================================== */
16 #include "lyxcursor.h"
19 #include "insets/inset.h"
35 This class holds the mapping between buffer paragraphs and screen rows.
39 /// what repainting is needed
41 /// no repaint is needed
44 * A paint has caused a change in some rows
45 * and rebreaking is needed.
48 /// the refresh_row needs repainting
49 NEED_VERY_LITTLE_REFRESH = 2,
50 /// everything from refresh_y downwards needs repainting
56 // the word around the cursor, only if the cursor is
59 // the word around the cursor
61 /// the word begining from the cursor position
63 /// the word around the cursor or before the cursor
65 /// the next word (not yet used)
70 LyXText(BufferView *);
71 /// sets inset as owner
77 void init(BufferView *, bool reinit = false);
81 mutable unsigned int width;
82 /// the current font settings
83 mutable LyXFont current_font;
85 mutable LyXFont real_current_font;
87 /** the first visible row on screen
88 * declared mutable because removeRow is const
90 mutable Row * top_row_;
91 /** the pixel offset with respect to this row of top_y
92 * declared mutable because removeRow is const
94 mutable int top_row_offset_;
96 /// get the y coord. of the top of the screen (relative to doc start)
101 InsetText * inset_owner;
103 UpdatableInset * the_locking_inset;
106 int getRealCursorX(BufferView *) const;
108 LyXFont const getFont(Buffer const *, Paragraph * par,
109 lyx::pos_type pos) const;
111 LyXFont const getLayoutFont(Buffer const *, Paragraph * par) const;
113 LyXFont const getLabelFont(Buffer const *, Paragraph * par) const;
115 void setCharFont(Buffer const *, Paragraph * par,
116 lyx::pos_type pos, LyXFont const & font);
117 void setCharFont(BufferView *, Paragraph * par,
118 lyx::pos_type pos, LyXFont const & font, bool toggleall);
120 /// return true if the row changed
121 void markChangeInDraw(BufferView * bv, Row * row, Row * next);
123 void breakAgainOneRow(BufferView *, Row * row);
124 /// what you expect when pressing <enter> at cursor position
125 void breakParagraph(BufferView *,
126 ParagraphList & paragraphs, char keep_layout = 0);
128 /** set layout over selection and make a total rebreak of
131 Paragraph * setLayout(BufferView *, LyXCursor & actual_cursor,
132 LyXCursor & selection_start,
133 LyXCursor & selection_end,
134 string const & layout);
136 void setLayout(BufferView *, string const & layout);
138 /** increment depth over selection and make a total rebreak of those
141 void incDepth(BufferView *);
143 /** decrement depth over selection and make a total rebreak of those
145 void decDepth(BufferView *);
147 /// get the depth at current cursor position
148 int getDepth() const;
150 /** set font over selection and make a total rebreak of those
152 toggleall defaults to false.
154 void setFont(BufferView *, LyXFont const &, bool toggleall = false);
156 /** deletes and inserts again all paragaphs between the cursor
157 and the specified par. The Cursor is needed to set the refreshing
159 This function is needed after SetLayout and SetFont etc.
161 void redoParagraphs(BufferView *, LyXCursor const & cursor,
162 Paragraph const * end_par) const;
164 void redoParagraph(BufferView *) const;
167 void toggleFree(BufferView *, LyXFont const &, bool toggleall = false);
170 string getStringToIndex(BufferView *);
172 /** recalculates the heights of all previous rows of the
173 specified paragraph. needed, if the last characters font
176 void redoHeightOfParagraph(BufferView *);
178 /** insert a character, moves all the following breaks in the
179 same Paragraph one to the right and make a little rebreak
181 void insertChar(BufferView *, char c);
183 void insertInset(BufferView *, Inset * inset);
185 /// Completes the insertion with a full rebreak
186 void fullRebreak(BufferView *);
189 mutable Row * need_break_row;
191 * The pixel y position from which to repaint the screen.
192 * The position is absolute along the height of outermost
193 * lyxtext (I think). NEED_MORE_REFRESH and NEED_LITTLE_REFRESH
194 * repaints both use this as a starting point (if it's within
195 * the viewable portion of the lyxtext).
197 mutable int refresh_y;
199 * The row from which to repaint the screen, used by screen.c.
200 * This must be set if the pending update is NEED_LITTLE_REFRESH.
201 * It doesn't make any difference for NEED_MORE_REFRESH.
203 mutable Row * refresh_row;
206 * Return the status. This represents what repaints are
207 * pending after some operation (e.g. inserting a char).
209 text_status status() const;
211 /// Set the status to make a paint pending.
212 void status(BufferView *, text_status) const;
215 Inset::RESULT dispatch(FuncRequest const & cmd);
218 /// only the top-level LyXText has this non-zero
219 BufferView * bv_owner;
221 /** wether the screen needs a refresh,
222 starting with refresh_y
224 mutable text_status status_;
227 /** returns a pointer to the row near the specified y-coordinate
228 (relative to the whole text). y is set to the real beginning
231 Row * getRowNearY(int & y) const;
233 /** returns the column near the specified x-coordinate of the row
234 x is set to the real beginning of this column
236 lyx::pos_type getColumnNearX(BufferView *, Row * row,
237 int & x, bool & boundary) const;
239 /** returns a pointer to a specified row. y is set to the beginning
242 Row * getRow(Paragraph * par, lyx::pos_type pos, int & y) const;
243 /** returns the firstrow, this could be done with the above too but
244 IMO it's stupid to have to allocate a dummy y all the time I need
247 Row * firstRow() { return firstrow; }
250 Later this variable has to be removed. There should be now internal
251 cursor in a text (and thus not in a buffer). By keeping this it is
252 (I think) impossible to have several views with the same buffer, but
253 the cursor placed at different places.
255 Since the LyXText now has been moved from Buffer to BufferView
256 it should not be absolutely needed to move the cursor...
258 mutable LyXCursor cursor; // actual cursor position
260 /** The structure that keeps track of the selections set. */
263 : set_(false), mark_(false)
277 LyXCursor cursor; // temporary cursor to hold a cursor position
278 // until setSelection is called!
279 LyXCursor start; // start of a REAL selection
280 LyXCursor end; // end of a REAL selection
282 bool set_; // former selection
283 bool mark_; // former mark_set
286 mutable Selection selection;
287 // this is used to handle XSelection events in the right manner
288 mutable Selection xsel_cache;
290 /// needed for the toggling (cursor position on last selection made)
291 mutable LyXCursor last_sel_cursor;
292 /// needed for toggling the selection in screen.C
293 mutable LyXCursor toggle_cursor;
294 /// needed for toggling the selection in screen.C
295 mutable LyXCursor toggle_end_cursor;
297 /// need the selection cursor:
298 void setSelection(BufferView *);
300 void clearSelection() const;
302 string const selectionAsString(Buffer const *, bool label) const;
304 /// select the word we need depending on word_location
305 void getWord(LyXCursor & from, LyXCursor & to,
306 word_location const) const;
307 /// just selects the word the cursor is in
308 void selectWord(BufferView *, word_location const);
309 /// returns the inset at cursor (if it exists), 0 otherwise
310 Inset * getInset() const;
312 /// accept selected change
313 void acceptChange(BufferView * bv);
315 /// reject selected change
316 void rejectChange(BufferView * bv);
318 /** 'selects" the next word, where the cursor is not in
319 and returns this word as string. THe cursor will be moved
320 to the beginning of this word.
321 With SelectSelectedWord can this be highlighted really
323 WordLangTuple const selectNextWordToSpellcheck(BufferView *, float & value) const;
325 void selectSelectedWord(BufferView *);
326 /// returns true if par was empty and was removed
327 bool setCursor(BufferView *, Paragraph * par,
330 bool boundary = false) const;
332 void setCursor(BufferView *, LyXCursor &, Paragraph * par,
334 bool boundary = false) const;
336 void setCursorIntern(BufferView *, Paragraph * par,
339 bool boundary = false) const;
341 void setCurrentFont(BufferView *) const;
344 bool isBoundary(Buffer const *, Paragraph * par,
345 lyx::pos_type pos) const;
347 bool isBoundary(Buffer const *, Paragraph * par,
349 LyXFont const & font) const;
352 void setCursorFromCoordinates(BufferView *, int x, int y) const;
354 void setCursorFromCoordinates(BufferView *, LyXCursor &,
357 void cursorUp(BufferView *, bool selecting = false) const;
359 void cursorDown(BufferView *, bool selecting = false) const;
361 void cursorLeft(BufferView *, bool internal = true) const;
363 void cursorRight(BufferView *, bool internal = true) const;
365 void cursorLeftOneWord(BufferView *) const;
367 void cursorRightOneWord(BufferView *) const;
369 void cursorUpParagraph(BufferView *) const;
371 void cursorDownParagraph(BufferView *) const;
373 void cursorHome(BufferView *) const;
375 void cursorEnd(BufferView *) const;
377 void cursorPrevious(BufferView * bv);
379 void cursorNext(BufferView * bv);
381 void cursorTab(BufferView *) const;
383 void cursorTop(BufferView *) const;
385 void cursorBottom(BufferView *) const;
387 void Delete(BufferView *);
389 void backspace(BufferView *);
391 bool selectWordWhenUnderCursor(BufferView *,
392 word_location const);
398 text_capitalization = 1,
402 /// Change the case of the word at cursor position.
403 void changeCase(BufferView &, TextCase action);
405 void transposeChars(BufferView &);
408 void toggleInset(BufferView *);
410 void cutSelection(BufferView *, bool doclear = true, bool realcut = true);
412 void copySelection(BufferView *);
414 void pasteSelection(BufferView *);
416 void copyEnvironmentType();
418 void pasteEnvironmentType(BufferView *);
420 /** the DTP switches for paragraphs. LyX will store the top settings
421 always in the first physical paragraph, the bottom settings in the
422 last. When a paragraph is broken, the top settings rest, the bottom
423 settings are given to the new one. So I can make shure, they do not
424 duplicate themself (and you cannnot make dirty things with them! )
426 void setParagraph(BufferView *,
427 bool line_top, bool line_bottom,
428 bool pagebreak_top, bool pagebreak_bottom,
429 VSpace const & space_top,
430 VSpace const & space_bottom,
431 Spacing const & spacing,
433 string labelwidthstring,
436 /* these things are for search and replace */
439 * Sets the selection from the current cursor position to length
440 * characters to the right. No safety checks.
442 void setSelectionRange(BufferView *, lyx::pos_type length);
444 /** simple replacing. The font of the first selected character
447 void replaceSelectionWithString(BufferView *, string const & str);
449 /// needed to insert the selection
450 void insertStringAsLines(BufferView *, string const & str);
451 /// needed to insert the selection
452 void insertStringAsParagraphs(BufferView *, string const & str);
454 /// Find next inset of some specified type.
455 bool gotoNextInset(BufferView *, std::vector<Inset::Code> const & codes,
456 string const & contents = string()) const;
458 void gotoInset(BufferView * bv, std::vector<Inset::Code> const & codes,
461 void gotoInset(BufferView * bv, Inset::Code code, bool same_content);
464 /* for the greater insets */
466 /// returns false if inset wasn't found
467 bool updateInset(BufferView *, Inset *);
469 void checkParagraph(BufferView *, Paragraph * par, lyx::pos_type pos);
471 int workWidth(BufferView &) const;
473 int workWidth(BufferView &, Inset * inset) const;
475 void computeBidiTables(Buffer const *, Row * row) const;
477 /// Maps positions in the visual string to positions in logical string.
479 lyx::pos_type log2vis(lyx::pos_type pos) const {
480 if (bidi_start == -1)
483 return log2vis_list[pos-bidi_start];
486 /// Maps positions in the logical string to positions in visual string.
488 lyx::pos_type vis2log(lyx::pos_type pos) const {
489 if (bidi_start == -1)
492 return vis2log_list[pos-bidi_start];
496 lyx::pos_type bidi_level(lyx::pos_type pos) const {
497 if (bidi_start == -1)
500 return bidi_levels[pos-bidi_start];
504 bool bidi_InRange(lyx::pos_type pos) const {
505 return bidi_start == -1 ||
506 (bidi_start <= pos && pos <= bidi_end);
510 mutable Row * firstrow;
512 mutable Row * lastrow;
515 void cursorLeftOneWord(LyXCursor &) const;
518 float getCursorX(BufferView *, Row *, lyx::pos_type pos,
519 lyx::pos_type last, bool boundary) const;
520 /// used in setlayout
521 void makeFontEntriesLayoutSpecific(Buffer const &, Paragraph & par);
523 /** forces the redrawing of a paragraph. Needed when manipulating a
526 void redoDrawingOfParagraph(BufferView *, LyXCursor const & cursor);
528 /** Copybuffer for copy environment type.
529 Asger has learned that this should be a buffer-property instead
530 Lgb has learned that 'char' is a lousy type for non-characters
532 string copylayouttype;
534 /** inserts a new row behind the specified row, increments
535 the touched counters */
536 void insertRow(Row * row, Paragraph * par, lyx::pos_type pos) const;
537 /// removes the row and reset the touched counters
538 void removeRow(Row * row) const;
540 /// remove all following rows of the paragraph of the specified row.
541 void removeParagraph(Row * row) const;
543 /// insert the specified paragraph behind the specified row
544 void insertParagraph(BufferView *,
545 Paragraph * par, Row * row) const;
547 /** appends the implizit specified paragraph behind the specified row,
548 * start at the implizit given position */
549 void appendParagraph(BufferView *, Row * row) const;
552 void breakAgain(BufferView *, Row * row) const;
553 /// Calculate and set the height of the row
554 void setHeightOfRow(BufferView *, Row * row_ptr) const;
556 // fix the cursor `cur' after a characters has been deleted at `where'
557 // position. Called by deleteEmptyParagraphMechanism
558 void fixCursorAfterDelete(BufferView * bv,
560 LyXCursor const & where) const;
562 /// delete double space (false) or empty paragraphs (true) around old_cursor
563 bool deleteEmptyParagraphMechanism(BufferView *,
564 LyXCursor const & old_cursor) const;
567 /** Updates all counters starting BEHIND the row. Changed paragraphs
568 * with a dynamic left margin will be rebroken. */
569 void updateCounters(BufferView *) const;
571 void update(BufferView * bv, bool changed = true);
573 * Returns an inset if inset was hit, or 0 if not.
574 * If hit, the coordinates are changed relative to the inset.
576 Inset * checkInsetHit(BufferView * bv, int & x, int & y) const;
579 int singleWidth(BufferView *, Paragraph * par,
580 lyx::pos_type pos) const;
582 int singleWidth(BufferView *, Paragraph * par,
583 lyx::pos_type pos, char c) const;
585 /// return the color of the canvas
586 LColor::color backgroundColor() const;
589 mutable bool bidi_same_direction;
591 unsigned char transformChar(unsigned char c, Paragraph * par,
592 lyx::pos_type pos) const;
595 * Returns the left beginning of the text.
596 * This information cannot be taken from the layout object, because
597 * in LaTeX the beginning of the text fits in some cases
598 * (for example sections) exactly the label-width.
600 int leftMargin(BufferView *, Row const * row) const;
602 int rightMargin(Buffer const &, Row const & row) const;
604 /** this calculates the specified parameters. needed when setting
605 * the cursor and when creating a visible row */
606 void prepareToPrint(BufferView *, Row * row, float & x,
607 float & fill_separator,
609 float & fill_label_hfill,
610 bool bidi = true) const;
614 void setCounter(Buffer const *, Paragraph * par) const;
616 void deleteWordForward(BufferView *);
618 void deleteWordBackward(BufferView *);
620 void deleteLineForward(BufferView *);
623 * some low level functions
627 /// return the pos value *before* which a row should break.
628 /// for example, the pos at which IsNewLine(pos) == true
629 lyx::pos_type rowBreakPoint(BufferView &, Row const & row) const;
631 /// returns the minimum space a row needs on the screen in pixel
632 int fill(BufferView &, Row & row, int workwidth) const;
635 * returns the minimum space a manual label needs on the
638 int labelFill(BufferView &, Row const & row) const;
641 int labelEnd(BufferView &, Row const & row) const;
644 mutable std::vector<lyx::pos_type> log2vis_list;
647 mutable std::vector<lyx::pos_type> vis2log_list;
650 mutable std::vector<lyx::pos_type> bidi_levels;
653 mutable lyx::pos_type bidi_start;
656 mutable lyx::pos_type bidi_end;
662 // special owner functions
664 Paragraph * ownerParagraph() const;
666 void ownerParagraph(Paragraph *) const;
667 // set it searching first for the right owner using the paragraph id
668 void ownerParagraph(int id, Paragraph *) const;
670 /// return true if this is the outer-most lyxtext
671 bool isTopLevel() const;
673 /// return true if this is owned by an inset. FIXME: why the difference
674 /// with isTopLevel() ??
675 bool isInInset() const;
678 /// return the default height of a row in pixels, considering font zoom
679 extern int defaultRowHeight();