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 * ====================================================== */
20 #include "lyxcursor.h"
21 #include "paragraph.h"
34 This class holds the mapping between buffer paragraphs and screen rows.
45 NEED_VERY_LITTLE_REFRESH = 2,
51 // the word around the cursor, only if the cursor is
54 // the word around the cursor
56 /// the word begining from the cursor position
58 /// the word around the cursor or before the cursor
60 /// the next word (not yet used)
65 LyXText(BufferView *);
72 void init(BufferView *, bool reinit = false);
74 mutable int number_of_rows;
78 mutable unsigned int width;
79 /// the current font settings
80 mutable LyXFont current_font;
82 mutable LyXFont real_current_font;
83 /// first visible pixel-row is set from LyXScreen!!!
84 // unsigned is wrong here for text-insets!
87 BufferView * bv_owner;
89 InsetText * inset_owner;
91 UpdatableInset * the_locking_inset;
94 int getRealCursorX(BufferView *) const;
96 LyXFont const getFont(Buffer const *, Paragraph * par,
97 Paragraph::size_type pos) const;
99 void setCharFont(Buffer const *, Paragraph * par,
100 Paragraph::size_type pos, LyXFont const & font);
101 void setCharFont(BufferView *, Paragraph * par,
102 Paragraph::size_type pos,
103 LyXFont const & font, bool toggleall);
104 /// returns a pointer to the very first Paragraph
105 Paragraph * firstParagraph() const;
107 /// what you expect when pressing <enter> at cursor position
108 void breakParagraph(BufferView *, char keep_layout = 0);
110 /** set layout over selection and make a total rebreak of
113 Paragraph * setLayout(BufferView *, LyXCursor & actual_cursor,
114 LyXCursor & selection_start,
115 LyXCursor & selection_end,
116 LyXTextClass::size_type layout);
117 void setLayout(BufferView *, LyXTextClass::size_type layout);
119 /// used in setlayout
120 void makeFontEntriesLayoutSpecific(Buffer const *, Paragraph * par);
122 /** increment depth over selection and make a total rebreak of those
125 void incDepth(BufferView *);
127 /** decrement depth over selection and make a total rebreak of those
129 void decDepth(BufferView *);
131 /** Get the depth at current cursor position
133 int getDepth() const { return cursor.par()->getDepth(); }
135 /** set font over selection and make a total rebreak of those
137 toggleall defaults to false.
139 void setFont(BufferView *, LyXFont const &, bool toggleall = false);
141 /** deletes and inserts again all paragaphs between the cursor
142 and the specified par. The Cursor is needed to set the refreshing
144 This function is needed after SetLayout and SetFont etc.
146 void redoParagraphs(BufferView *, LyXCursor const & cursor,
147 Paragraph const * end_par) const;
149 void redoParagraph(BufferView *) const;
152 void toggleFree(BufferView *, LyXFont const &, bool toggleall = false);
155 string getStringToIndex(BufferView *);
157 /** recalculates the heights of all previous rows of the
158 specified paragraph. needed, if the last characters font
161 void redoHeightOfParagraph(BufferView *, LyXCursor const & cursor);
163 /** forces the redrawing of a paragraph. Needed when manipulating a
166 void redoDrawingOfParagraph(BufferView *, LyXCursor const & cursor);
168 /** insert a character, moves all the following breaks in the
169 same Paragraph one to the right and make a little rebreak
171 void insertChar(BufferView *, char c);
173 void insertInset(BufferView *, Inset * inset);
175 /** Completes the insertion with a full rebreak.
176 Returns true if something was broken. */
177 bool fullRebreak(BufferView *);
180 Row * need_break_row;
182 mutable int refresh_y;
190 mutable Row * refresh_row;
194 /// give and set the LyXText status
195 text_status status() const;
196 void status(BufferView *, text_status) const;
199 /** wether the screen needs a refresh,
200 starting with refresh_y
202 mutable text_status status_;
205 /** returns a pointer to the row near the specified y-coordinate
206 (relative to the whole text). y is set to the real beginning
209 Row * getRowNearY(int & y) const;
211 /** returns the column near the specified x-coordinate of the row
212 x is set to the real beginning of this column
214 int getColumnNearX(BufferView *, Row * row,
215 int & x, bool & boundary) const;
217 /** returns a pointer to a specified row. y is set to the beginning
220 Row * getRow(Paragraph * par,
221 Paragraph::size_type pos, int & y) const;
223 /** returns the height of a default row, needed for scrollbar
225 int defaultHeight() const;
228 Later this variable has to be removed. There should be now internal
229 cursor in a text (and thus not in a buffer). By keeping this it is
230 (I think) impossible to have several views with the same buffer, but
231 the cursor placed at different places.
233 Since the LyXText now has been moved from Buffer to BufferView
234 it should not be absolutely needed to move the cursor...
236 mutable LyXCursor cursor;
238 /** The structrue that keeps track of the selections set. */
241 : set_(false), mark_(false)
259 bool set_; // former selection
260 bool mark_; // former mark_set
263 mutable Selection selection;
265 /// needed for the toggling
266 LyXCursor last_sel_cursor;
268 LyXCursor toggle_cursor;
270 LyXCursor toggle_end_cursor;
272 /// need the selection cursor:
273 void setSelection(BufferView *);
275 void clearSelection(BufferView *) const;
277 string const selectionAsString(Buffer const *) const;
279 /// select the word we need depending on word_location
280 void getWord(LyXCursor & from, LyXCursor & to,
281 word_location const) const;
282 /// just selects the word the cursor is in
283 void selectWord(BufferView *, word_location const);
284 /// returns the inset at cursor (if it exists), 0 otherwise
285 Inset * getInset() const;
287 /** 'selects" the next word, where the cursor is not in
288 and returns this word as string. THe cursor will be moved
289 to the beginning of this word.
290 With SelectSelectedWord can this be highlighted really
292 string const selectNextWord(BufferView *, float & value) const;
294 void selectSelectedWord(BufferView *);
296 void setCursor(BufferView *, Paragraph * par,
297 Paragraph::size_type pos,
299 bool boundary = false) const;
301 void setCursor(BufferView *, LyXCursor &, Paragraph * par,
302 Paragraph::size_type pos,
303 bool boundary = false) const;
305 void setCursorIntern(BufferView *, Paragraph * par,
306 Paragraph::size_type pos,
308 bool boundary = false) const;
310 void setCurrentFont(BufferView *) const;
313 bool isBoundary(Buffer const *, Paragraph * par,
314 Paragraph::size_type pos) const;
316 bool isBoundary(Buffer const *, Paragraph * par,
317 Paragraph::size_type pos,
318 LyXFont const & font) const;
321 void setCursorFromCoordinates(BufferView *, int x, int y) const;
323 void setCursorFromCoordinates(BufferView *, LyXCursor &,
326 void cursorUp(BufferView *) const;
328 void cursorDown(BufferView *) const;
330 void cursorLeft(BufferView *, bool internal = true) const;
332 void cursorRight(BufferView *, bool internal = true) const;
334 void cursorLeftOneWord(BufferView *) const;
336 void cursorLeftOneWord(LyXCursor &) const;
338 void cursorRightOneWord(BufferView *) const;
340 void cursorUpParagraph(BufferView *) const;
342 void cursorDownParagraph(BufferView *) const;
344 void cursorHome(BufferView *) const;
346 void cursorEnd(BufferView *) const;
348 void cursorTab(BufferView *) const;
350 void cursorTop(BufferView *) const;
352 void cursorBottom(BufferView *) const;
354 void Delete(BufferView *);
356 void backspace(BufferView *);
358 void deleteWordForward(BufferView *);
360 void deleteWordBackward(BufferView *);
362 void deleteLineForward(BufferView *);
364 bool selectWordWhenUnderCursor(BufferView *,
365 word_location const);
371 text_capitalization = 1,
375 /// Change the case of the word at cursor position.
376 void changeCase(BufferView *, TextCase action);
378 void changeRegionCase(BufferView * bview,
379 LyXCursor const & from,
380 LyXCursor const & to,
381 LyXText::TextCase action);
383 void transposeChars(BufferView &);
385 /** returns a printed row in a pixmap. The y value is needed to
386 decide, wether it is selected text or not. This is a strange
389 void getVisibleRow(BufferView *, int y_offset, int x_offset,
390 Row * row_ptr, int y, bool cleared=false);
393 void toggleInset(BufferView *);
395 void cutSelection(BufferView *, bool = true);
397 void copySelection(BufferView *);
399 void pasteSelection(BufferView *);
401 void copyEnvironmentType();
403 void pasteEnvironmentType(BufferView *);
405 /** the DTP switches for paragraphs. LyX will store the top settings
406 always in the first physical paragraph, the bottom settings in the
407 last. When a paragraph is broken, the top settings rest, the bottom
408 settings are given to the new one. So I can make shure, they do not
409 duplicate themself (and you cannnot make dirty things with them! )
411 void setParagraph(BufferView *,
412 bool line_top, bool line_bottom,
413 bool pagebreak_top, bool pagebreak_bottom,
414 VSpace const & space_top,
415 VSpace const & space_bottom,
417 string labelwidthstring,
420 /* these things are for search and replace */
422 /** sets the selection over the number of characters of string,
425 void setSelectionOverString(BufferView *, string const & str);
427 /** simple replacing. The font of the first selected character
430 void replaceSelectionWithString(BufferView *, string const & str);
432 /// needed to insert the selection
433 void insertStringAsLines(BufferView *, string const & str);
434 /// needed to insert the selection
435 void insertStringAsParagraphs(BufferView *, string const & str);
437 /// Find next inset of some specified type.
438 bool gotoNextInset(BufferView *, std::vector<Inset::Code> const & codes,
439 string const & contents = string()) const;
442 /* for the greater insets */
444 /// returns false if inset wasn't found
445 bool updateInset(BufferView *, Inset *);
447 void checkParagraph(BufferView *, Paragraph * par,
448 Paragraph::size_type pos);
450 int numberOfCell(Paragraph * par,
451 Paragraph::size_type pos) const;
453 void removeTableRow(LyXCursor & cursor) const;
455 bool isEmptyTableCell() const;
457 void toggleAppendix(BufferView *);
459 int workWidth(BufferView *) const;
461 void computeBidiTables(Buffer const *, Row * row) const;
463 /// Maps positions in the visual string to positions in logical string.
465 Paragraph::size_type log2vis(Paragraph::size_type pos) const {
466 if (bidi_start == -1)
469 return log2vis_list[pos-bidi_start];
472 /// Maps positions in the logical string to positions in visual string.
474 Paragraph::size_type vis2log(Paragraph::size_type pos) const {
475 if (bidi_start == -1)
478 return vis2log_list[pos-bidi_start];
482 Paragraph::size_type bidi_level(Paragraph::size_type pos) const {
483 if (bidi_start == -1)
486 return bidi_levels[pos-bidi_start];
490 bool bidi_InRange(Paragraph::size_type pos) const {
491 return bidi_start == -1 ||
492 (bidi_start <= pos && pos <= bidi_end);
496 mutable Row * firstrow;
498 mutable Row * lastrow;
500 /** Copybuffer for copy environment type.
501 Asger has learned that this should be a buffer-property instead
502 Lgb has learned that 'char' is a lousy type for non-characters
504 LyXTextClass::size_type copylayouttype;
506 /** inserts a new row behind the specified row, increments
507 the touched counters */
508 void insertRow(Row * row, Paragraph * par,
509 Paragraph::size_type pos) const;
510 /** removes the row and reset the touched counters */
511 void removeRow(Row * row) const;
513 /** remove all following rows of the paragraph of the specified row. */
514 void removeParagraph(Row * row) const;
516 /** insert the specified paragraph behind the specified row */
517 void insertParagraph(BufferView *,
518 Paragraph * par, Row * row) const;
520 /** appends the implizit specified paragraph behind the specified row,
521 * start at the implizit given position */
522 void appendParagraph(BufferView *, Row * row) const;
525 void breakAgain(BufferView *, Row * row) const;
527 void breakAgainOneRow(BufferView *, Row * row);
528 /// Calculate and set the height of the row
529 void setHeightOfRow(BufferView *, Row * row_ptr) const;
531 /** this calculates the specified parameters. needed when setting
532 * the cursor and when creating a visible row */
533 void prepareToPrint(BufferView *, Row * row, float & x,
534 float & fill_separator,
536 float & fill_label_hfill,
537 bool bidi = true) const;
540 void deleteEmptyParagraphMechanism(BufferView *,
541 LyXCursor const & old_cursor) const;
544 /** Updates all counters starting BEHIND the row. Changed paragraphs
545 * with a dynamic left margin will be rebroken. */
546 void updateCounters(BufferView *, Row * row) const;
549 void setCounter(Buffer const *, Paragraph * par) const;
552 * some low level functions
556 int singleWidth(BufferView *, Paragraph * par,
557 Paragraph::size_type pos) const;
559 int singleWidth(BufferView *, Paragraph * par,
560 Paragraph::size_type pos, char c) const;
562 void draw(BufferView *, Row const * row,
563 Paragraph::size_type & pos,
564 int offset, float & x, bool cleared);
566 /// get the next breakpoint in a given paragraph
567 Paragraph::size_type nextBreakPoint(BufferView *, Row const * row,
569 /// returns the minimum space a row needs on the screen in pixel
570 int fill(BufferView *, Row * row, int workwidth) const;
572 /** returns the minimum space a manual label needs on the
574 int labelFill(BufferView *, Row const * row) const;
578 beginningOfMainBody(Buffer const *, Paragraph const * par) const;
580 /** Returns the left beginning of the text.
581 This information cannot be taken from the layouts-objekt, because
582 in LaTeX the beginning of the text fits in some cases
583 (for example sections) exactly the label-width.
585 int leftMargin(BufferView *, Row const * row) const;
587 int rightMargin(Buffer const *, Row const * row) const;
589 int labelEnd (BufferView *, Row const * row) const;
591 /** returns the number of separators in the specified row.
592 The separator on the very last column doesnt count
594 int numberOfSeparators(Buffer const *, Row const * row) const;
596 /** returns the number of hfills in the specified row. The
597 LyX-Hfill is a LaTeX \hfill so that the hfills at the
598 beginning and at the end were ignored. This is {\em MUCH}
599 more usefull than not to ignore!
601 int numberOfHfills(Buffer const *, Row const * row) const;
603 /// like NumberOfHfills, but only those in the manual label!
604 int numberOfLabelHfills(Buffer const *, Row const * row) const;
605 /** returns true, if a expansion is needed. Rules are given by
608 bool hfillExpansion(Buffer const *, Row const * row_ptr,
609 Paragraph::size_type pos) const;
611 LColor::color backgroundColor();
615 mutable std::vector<Paragraph::size_type> log2vis_list;
618 mutable std::vector<Paragraph::size_type> vis2log_list;
621 mutable std::vector<Paragraph::size_type> bidi_levels;
624 mutable Paragraph::size_type bidi_start;
627 mutable Paragraph::size_type bidi_end;
630 mutable bool bidi_same_direction;
633 unsigned char transformChar(unsigned char c, Paragraph * par,
634 Paragraph::size_type pos) const;
636 /** returns the paragraph position of the last character in the
639 Paragraph::size_type rowLast(Row const * row) const;
641 Paragraph::size_type rowLastPrintable(Row const * row) const;
647 // special owner functions
649 Paragraph * ownerParagraph() const;
651 Paragraph * ownerParagraph(Paragraph *) const;
652 // set it searching first for the right owner using the paragraph id
653 Paragraph * ownerParagraph(int id, Paragraph *) const;
657 /* returns a pointer to the row near the specified y-coordinate
658 * (relative to the whole text). y is set to the real beginning
661 Row * LyXText::getRowNearY(int & y) const
663 // If possible we should optimize this method. (Lgb)
664 Row * tmprow = firstrow;
667 while (tmprow->next() && tmpy + tmprow->height() <= y) {
668 tmpy += tmprow->height();
669 tmprow = tmprow->next();
672 y = tmpy; // return the real y