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 LyXFont const getLayoutFont(Buffer const *, Paragraph * par) const;
101 LyXFont const getLabelFont(Buffer const *, Paragraph * par) const;
103 void setCharFont(Buffer const *, Paragraph * par,
104 Paragraph::size_type pos, LyXFont const & font);
105 void setCharFont(BufferView *, Paragraph * par,
106 Paragraph::size_type pos,
107 LyXFont const & font, bool toggleall);
108 /// returns a pointer to the very first Paragraph
109 Paragraph * firstParagraph() const;
111 /// what you expect when pressing <enter> at cursor position
112 void breakParagraph(BufferView *, char keep_layout = 0);
114 /** set layout over selection and make a total rebreak of
117 Paragraph * setLayout(BufferView *, LyXCursor & actual_cursor,
118 LyXCursor & selection_start,
119 LyXCursor & selection_end,
120 LyXTextClass::size_type layout);
121 void setLayout(BufferView *, LyXTextClass::size_type layout);
123 /// used in setlayout
124 void makeFontEntriesLayoutSpecific(Buffer const *, Paragraph * par);
126 /** increment depth over selection and make a total rebreak of those
129 void incDepth(BufferView *);
131 /** decrement depth over selection and make a total rebreak of those
133 void decDepth(BufferView *);
135 /** Get the depth at current cursor position
137 int getDepth() const { return cursor.par()->getDepth(); }
139 /** set font over selection and make a total rebreak of those
141 toggleall defaults to false.
143 void setFont(BufferView *, LyXFont const &, bool toggleall = false);
145 /** deletes and inserts again all paragaphs between the cursor
146 and the specified par. The Cursor is needed to set the refreshing
148 This function is needed after SetLayout and SetFont etc.
150 void redoParagraphs(BufferView *, LyXCursor const & cursor,
151 Paragraph const * end_par) const;
153 void redoParagraph(BufferView *) const;
156 void toggleFree(BufferView *, LyXFont const &, bool toggleall = false);
159 string getStringToIndex(BufferView *);
161 /** recalculates the heights of all previous rows of the
162 specified paragraph. needed, if the last characters font
165 void redoHeightOfParagraph(BufferView *, LyXCursor const & cursor);
167 /** forces the redrawing of a paragraph. Needed when manipulating a
170 void redoDrawingOfParagraph(BufferView *, LyXCursor const & cursor);
172 /** insert a character, moves all the following breaks in the
173 same Paragraph one to the right and make a little rebreak
175 void insertChar(BufferView *, char c);
177 void insertInset(BufferView *, Inset * inset);
179 /** Completes the insertion with a full rebreak.
180 Returns true if something was broken. */
181 bool fullRebreak(BufferView *);
184 mutable Row * need_break_row;
186 mutable int refresh_y;
194 mutable Row * refresh_row;
198 /// give and set the LyXText status
199 text_status status() const;
201 void status(BufferView *, text_status) const;
204 /** wether the screen needs a refresh,
205 starting with refresh_y
207 mutable text_status status_;
210 /** returns a pointer to the row near the specified y-coordinate
211 (relative to the whole text). y is set to the real beginning
214 Row * getRowNearY(int & y) const;
216 /** returns the column near the specified x-coordinate of the row
217 x is set to the real beginning of this column
219 int getColumnNearX(BufferView *, Row * row,
220 int & x, bool & boundary) const;
222 /** returns a pointer to a specified row. y is set to the beginning
225 Row * getRow(Paragraph * par,
226 Paragraph::size_type pos, int & y) const;
228 /** returns the height of a default row, needed for scrollbar
230 int defaultHeight() const;
233 Later this variable has to be removed. There should be now internal
234 cursor in a text (and thus not in a buffer). By keeping this it is
235 (I think) impossible to have several views with the same buffer, but
236 the cursor placed at different places.
238 Since the LyXText now has been moved from Buffer to BufferView
239 it should not be absolutely needed to move the cursor...
241 mutable LyXCursor cursor;
243 /** The structrue that keeps track of the selections set. */
246 : set_(false), mark_(false)
264 bool set_; // former selection
265 bool mark_; // former mark_set
268 mutable Selection selection;
270 /// needed for the toggling
271 LyXCursor last_sel_cursor;
273 LyXCursor toggle_cursor;
275 LyXCursor toggle_end_cursor;
277 /// need the selection cursor:
278 void setSelection(BufferView *);
280 void clearSelection() const;
282 string const selectionAsString(Buffer const *) const;
284 /// select the word we need depending on word_location
285 void getWord(LyXCursor & from, LyXCursor & to,
286 word_location const) const;
287 /// just selects the word the cursor is in
288 void selectWord(BufferView *, word_location const);
289 /// returns the inset at cursor (if it exists), 0 otherwise
290 Inset * getInset() const;
292 /** 'selects" the next word, where the cursor is not in
293 and returns this word as string. THe cursor will be moved
294 to the beginning of this word.
295 With SelectSelectedWord can this be highlighted really
297 string const selectNextWord(BufferView *, float & value) const;
299 void selectSelectedWord(BufferView *);
301 void setCursor(BufferView *, Paragraph * par,
302 Paragraph::size_type pos,
304 bool boundary = false) const;
306 void setCursor(BufferView *, LyXCursor &, Paragraph * par,
307 Paragraph::size_type pos,
308 bool boundary = false) const;
310 void setCursorIntern(BufferView *, Paragraph * par,
311 Paragraph::size_type pos,
313 bool boundary = false) const;
315 void setCurrentFont(BufferView *) const;
318 bool isBoundary(Buffer const *, Paragraph * par,
319 Paragraph::size_type pos) const;
321 bool isBoundary(Buffer const *, Paragraph * par,
322 Paragraph::size_type pos,
323 LyXFont const & font) const;
326 void setCursorFromCoordinates(BufferView *, int x, int y) const;
328 void setCursorFromCoordinates(BufferView *, LyXCursor &,
331 void cursorUp(BufferView *) const;
333 void cursorDown(BufferView *) const;
335 void cursorLeft(BufferView *, bool internal = true) const;
337 void cursorRight(BufferView *, bool internal = true) const;
339 void cursorLeftOneWord(BufferView *) const;
341 void cursorLeftOneWord(LyXCursor &) const;
343 void cursorRightOneWord(BufferView *) const;
345 void cursorUpParagraph(BufferView *) const;
347 void cursorDownParagraph(BufferView *) const;
349 void cursorHome(BufferView *) const;
351 void cursorEnd(BufferView *) const;
353 void cursorTab(BufferView *) const;
355 void cursorTop(BufferView *) const;
357 void cursorBottom(BufferView *) const;
359 void Delete(BufferView *);
361 void backspace(BufferView *);
363 void deleteWordForward(BufferView *);
365 void deleteWordBackward(BufferView *);
367 void deleteLineForward(BufferView *);
369 bool selectWordWhenUnderCursor(BufferView *,
370 word_location const);
376 text_capitalization = 1,
380 /// Change the case of the word at cursor position.
381 void changeCase(BufferView *, TextCase action);
383 void changeRegionCase(BufferView * bview,
384 LyXCursor const & from,
385 LyXCursor const & to,
386 LyXText::TextCase action);
388 void transposeChars(BufferView &);
390 /** returns a printed row in a pixmap. The y value is needed to
391 decide, wether it is selected text or not. This is a strange
394 void getVisibleRow(BufferView *, int y_offset, int x_offset,
395 Row * row_ptr, int y, bool cleared=false);
398 void toggleInset(BufferView *);
400 void cutSelection(BufferView *, bool doclear = true, bool realcut = true);
402 void copySelection(BufferView *);
404 void pasteSelection(BufferView *);
406 void copyEnvironmentType();
408 void pasteEnvironmentType(BufferView *);
410 /** the DTP switches for paragraphs. LyX will store the top settings
411 always in the first physical paragraph, the bottom settings in the
412 last. When a paragraph is broken, the top settings rest, the bottom
413 settings are given to the new one. So I can make shure, they do not
414 duplicate themself (and you cannnot make dirty things with them! )
416 void setParagraph(BufferView *,
417 bool line_top, bool line_bottom,
418 bool pagebreak_top, bool pagebreak_bottom,
419 VSpace const & space_top,
420 VSpace const & space_bottom,
422 string labelwidthstring,
425 /* these things are for search and replace */
427 /** sets the selection over the number of characters of string,
430 void setSelectionOverString(BufferView *, string const & str);
432 /** simple replacing. The font of the first selected character
435 void replaceSelectionWithString(BufferView *, string const & str);
437 /// needed to insert the selection
438 void insertStringAsLines(BufferView *, string const & str);
439 /// needed to insert the selection
440 void insertStringAsParagraphs(BufferView *, string const & str);
442 /// Find next inset of some specified type.
443 bool gotoNextInset(BufferView *, std::vector<Inset::Code> const & codes,
444 string const & contents = string()) const;
447 /* for the greater insets */
449 /// returns false if inset wasn't found
450 bool updateInset(BufferView *, Inset *);
452 void checkParagraph(BufferView *, Paragraph * par,
453 Paragraph::size_type pos);
455 int numberOfCell(Paragraph * par,
456 Paragraph::size_type pos) const;
458 void removeTableRow(LyXCursor & cursor) const;
460 bool isEmptyTableCell() const;
462 void toggleAppendix(BufferView *);
464 int workWidth(BufferView *) const;
466 void computeBidiTables(Buffer const *, Row * row) const;
468 /// Maps positions in the visual string to positions in logical string.
470 Paragraph::size_type log2vis(Paragraph::size_type pos) const {
471 if (bidi_start == -1)
474 return log2vis_list[pos-bidi_start];
477 /// Maps positions in the logical string to positions in visual string.
479 Paragraph::size_type vis2log(Paragraph::size_type pos) const {
480 if (bidi_start == -1)
483 return vis2log_list[pos-bidi_start];
487 Paragraph::size_type bidi_level(Paragraph::size_type pos) const {
488 if (bidi_start == -1)
491 return bidi_levels[pos-bidi_start];
495 bool bidi_InRange(Paragraph::size_type pos) const {
496 return bidi_start == -1 ||
497 (bidi_start <= pos && pos <= bidi_end);
501 mutable Row * firstrow;
503 mutable Row * lastrow;
505 /** Copybuffer for copy environment type.
506 Asger has learned that this should be a buffer-property instead
507 Lgb has learned that 'char' is a lousy type for non-characters
509 LyXTextClass::size_type copylayouttype;
511 /** inserts a new row behind the specified row, increments
512 the touched counters */
513 void insertRow(Row * row, Paragraph * par,
514 Paragraph::size_type pos) const;
515 /** removes the row and reset the touched counters */
516 void removeRow(Row * row) const;
518 /** remove all following rows of the paragraph of the specified row. */
519 void removeParagraph(Row * row) const;
521 /** insert the specified paragraph behind the specified row */
522 void insertParagraph(BufferView *,
523 Paragraph * par, Row * row) const;
525 /** appends the implizit specified paragraph behind the specified row,
526 * start at the implizit given position */
527 void appendParagraph(BufferView *, Row * row) const;
530 void breakAgain(BufferView *, Row * row) const;
532 void breakAgainOneRow(BufferView *, Row * row);
533 /// Calculate and set the height of the row
534 void setHeightOfRow(BufferView *, Row * row_ptr) const;
536 /** this calculates the specified parameters. needed when setting
537 * the cursor and when creating a visible row */
538 void prepareToPrint(BufferView *, Row * row, float & x,
539 float & fill_separator,
541 float & fill_label_hfill,
542 bool bidi = true) const;
545 void deleteEmptyParagraphMechanism(BufferView *,
546 LyXCursor const & old_cursor) const;
549 /** Updates all counters starting BEHIND the row. Changed paragraphs
550 * with a dynamic left margin will be rebroken. */
551 void updateCounters(BufferView *, Row * row) const;
554 void setCounter(Buffer const *, Paragraph * par) const;
557 * some low level functions
561 int singleWidth(BufferView *, Paragraph * par,
562 Paragraph::size_type pos) const;
564 int singleWidth(BufferView *, Paragraph * par,
565 Paragraph::size_type pos, char c) const;
567 void draw(BufferView *, Row const * row,
568 Paragraph::size_type & pos,
569 int offset, float & x, bool cleared);
571 /// get the next breakpoint in a given paragraph
572 Paragraph::size_type nextBreakPoint(BufferView *, Row const * row,
574 /// returns the minimum space a row needs on the screen in pixel
575 int fill(BufferView *, Row * row, int workwidth) const;
577 /** returns the minimum space a manual label needs on the
579 int labelFill(BufferView *, Row const * row) const;
583 beginningOfMainBody(Buffer const *, Paragraph const * par) const;
585 /** Returns the left beginning of the text.
586 This information cannot be taken from the layouts-objekt, because
587 in LaTeX the beginning of the text fits in some cases
588 (for example sections) exactly the label-width.
590 int leftMargin(BufferView *, Row const * row) const;
592 int rightMargin(Buffer const *, Row const * row) const;
594 int labelEnd (BufferView *, Row const * row) const;
596 /** returns the number of separators in the specified row.
597 The separator on the very last column doesnt count
599 int numberOfSeparators(Buffer const *, Row const * row) const;
601 /** returns the number of hfills in the specified row. The
602 LyX-Hfill is a LaTeX \hfill so that the hfills at the
603 beginning and at the end were ignored. This is {\em MUCH}
604 more usefull than not to ignore!
606 int numberOfHfills(Buffer const *, Row const * row) const;
608 /// like NumberOfHfills, but only those in the manual label!
609 int numberOfLabelHfills(Buffer const *, Row const * row) const;
610 /** returns true, if a expansion is needed. Rules are given by
613 bool hfillExpansion(Buffer const *, Row const * row_ptr,
614 Paragraph::size_type pos) const;
616 LColor::color backgroundColor();
620 mutable std::vector<Paragraph::size_type> log2vis_list;
623 mutable std::vector<Paragraph::size_type> vis2log_list;
626 mutable std::vector<Paragraph::size_type> bidi_levels;
629 mutable Paragraph::size_type bidi_start;
632 mutable Paragraph::size_type bidi_end;
635 mutable bool bidi_same_direction;
638 unsigned char transformChar(unsigned char c, Paragraph * par,
639 Paragraph::size_type pos) const;
641 /** returns the paragraph position of the last character in the
644 Paragraph::size_type rowLast(Row const * row) const;
646 Paragraph::size_type rowLastPrintable(Row const * row) const;
652 // special owner functions
654 Paragraph * ownerParagraph() const;
656 Paragraph * ownerParagraph(Paragraph *) const;
657 // set it searching first for the right owner using the paragraph id
658 Paragraph * ownerParagraph(int id, Paragraph *) const;
662 /* returns a pointer to the row near the specified y-coordinate
663 * (relative to the whole text). y is set to the real beginning
666 Row * LyXText::getRowNearY(int & y) const
668 // If possible we should optimize this method. (Lgb)
669 Row * tmprow = firstrow;
672 while (tmprow->next() && tmpy + tmprow->height() <= y) {
673 tmpy += tmprow->height();
674 tmprow = tmprow->next();
677 y = tmpy; // return the real y