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 * ====================================================== */
21 #include "lyxcursor.h"
22 #include "paragraph.h"
34 This class holds the mapping between buffer paragraphs and screen rows.
45 NEED_VERY_LITTLE_REFRESH,
51 /// the word around the cursor
53 /// the word begining from the cursor position
55 /// the next word (not yet used)
60 LyXText(BufferView *);
67 void init(BufferView *);
69 mutable int number_of_rows;
73 mutable unsigned int width;
74 /// the current font settings
75 mutable LyXFont current_font;
77 mutable LyXFont real_current_font;
78 /// first visible pixel-row is set from LyXScreen!!!
79 // unsigned is wrong here for text-insets!
82 BufferView * bv_owner;
84 InsetText * inset_owner;
86 UpdatableInset * the_locking_inset;
89 int getRealCursorX(BufferView *) const;
91 LyXFont const getFont(Buffer const *, Paragraph * par,
92 Paragraph::size_type pos) const;
94 void setCharFont(Buffer const *, Paragraph * par,
95 Paragraph::size_type pos, LyXFont const & font);
96 void setCharFont(BufferView *, Paragraph * par,
97 Paragraph::size_type pos,
98 LyXFont const & font, bool toggleall);
99 /// returns a pointer to the very first Paragraph
100 Paragraph * firstParagraph() const;
102 /// what you expect when pressing <enter> at cursor position
103 void breakParagraph(BufferView *, char keep_layout = 0);
105 /** set layout over selection and make a total rebreak of
108 Paragraph * setLayout(BufferView *, LyXCursor & actual_cursor,
109 LyXCursor & selection_start,
110 LyXCursor & selection_end,
111 LyXTextClass::size_type layout);
112 void setLayout(BufferView *, LyXTextClass::size_type layout);
114 /// used in setlayout
115 void makeFontEntriesLayoutSpecific(Buffer const *, Paragraph * par);
117 /** increment depth over selection and make a total rebreak of those
120 void incDepth(BufferView *);
122 /** decrement depth over selection and make a total rebreak of those
124 void decDepth(BufferView *);
126 /** Get the depth at current cursor position
128 int getDepth() const { return cursor.par()->getDepth(); }
130 /** set font over selection and make a total rebreak of those
132 toggleall defaults to false.
134 void setFont(BufferView *, LyXFont const &, bool toggleall = false);
136 /** deletes and inserts again all paragaphs between the cursor
137 and the specified par. The Cursor is needed to set the refreshing
139 This function is needed after SetLayout and SetFont etc.
141 void redoParagraphs(BufferView *, LyXCursor const & cursor,
142 Paragraph const * end_par) const;
144 void redoParagraph(BufferView *) const;
147 void toggleFree(BufferView *, LyXFont const &, bool toggleall = false);
149 /** recalculates the heights of all previous rows of the
150 specified paragraph. needed, if the last characters font
153 void redoHeightOfParagraph(BufferView *, LyXCursor const & cursor);
155 /** forces the redrawing of a paragraph. Needed when manipulating a
158 void redoDrawingOfParagraph(BufferView *, LyXCursor const & cursor);
160 /** insert a character, moves all the following breaks in the
161 same Paragraph one to the right and make a little rebreak
163 void insertChar(BufferView *, char c);
165 void insertInset(BufferView *, Inset * inset);
167 /** Completes the insertion with a full rebreak.
168 Returns true if something was broken. */
169 bool fullRebreak(BufferView *);
172 Row * need_break_row;
174 mutable int refresh_y;
182 mutable Row * refresh_row;
186 /** wether the screen needs a refresh,
187 starting with refresh_y
189 mutable text_status status;
191 /** returns a pointer to the row near the specified y-coordinate
192 (relative to the whole text). y is set to the real beginning
195 Row * getRowNearY(int & y) const;
197 /** returns the column near the specified x-coordinate of the row
198 x is set to the real beginning of this column
200 int getColumnNearX(BufferView *, Row * row,
201 int & x, bool & boundary) const;
203 /** returns a pointer to a specified row. y is set to the beginning
206 Row * getRow(Paragraph * par,
207 Paragraph::size_type pos, int & y) const;
209 /** returns the height of a default row, needed for scrollbar
211 int defaultHeight() const;
214 Later this variable has to be removed. There should be now internal
215 cursor in a text (and thus not in a buffer). By keeping this it is
216 (I think) impossible to have several views with the same buffer, but
217 the cursor placed at different places.
219 Since the LyXText now has been moved from Buffer to BufferView
220 it should not be absolutely needed to move the cursor...
222 mutable LyXCursor cursor;
224 /** The structrue that keeps track of the selections set. */
242 bool set_; // former selection
243 bool mark_; // former mark_set
246 mutable Selection selection;
248 /// needed for the toggling
249 LyXCursor last_sel_cursor;
251 LyXCursor toggle_cursor;
253 LyXCursor toggle_end_cursor;
255 /// need the selection cursor:
256 void setSelection(BufferView *);
258 void clearSelection(BufferView *) const;
260 string const selectionAsString(Buffer const *) const;
262 /// select the word we need depending on word_location
263 void getWord(LyXCursor & from, LyXCursor & to, word_location) const;
264 /// just selects the word the cursor is in
265 void selectWord(BufferView *);
267 /** 'selects" the next word, where the cursor is not in
268 and returns this word as string. THe cursor will be moved
269 to the beginning of this word.
270 With SelectSelectedWord can this be highlighted really
272 string const selectNextWord(BufferView *, float & value) const;
274 void selectSelectedWord(BufferView *);
276 void setCursor(BufferView *, Paragraph * par,
277 Paragraph::size_type pos,
279 bool boundary = false) const;
281 void setCursor(BufferView *, LyXCursor &, Paragraph * par,
282 Paragraph::size_type pos,
283 bool boundary = false) const;
285 void setCursorIntern(BufferView *, Paragraph * par,
286 Paragraph::size_type pos,
288 bool boundary = false) const;
290 void setCurrentFont(BufferView *) const;
293 bool isBoundary(Buffer const *, Paragraph * par,
294 Paragraph::size_type pos) const;
296 bool isBoundary(Buffer const *, Paragraph * par,
297 Paragraph::size_type pos,
298 LyXFont const & font) const;
301 void setCursorFromCoordinates(BufferView *, int x, int y) const;
303 void setCursorFromCoordinates(BufferView *, LyXCursor &,
306 void cursorUp(BufferView *) const;
308 void cursorDown(BufferView *) const;
310 void cursorLeft(BufferView *, bool internal = true) const;
312 void cursorRight(BufferView *, bool internal = true) const;
314 void cursorLeftOneWord(BufferView *) const;
316 void cursorLeftOneWord(LyXCursor &) const;
318 void cursorRightOneWord(BufferView *) const;
320 void cursorUpParagraph(BufferView *) const;
322 void cursorDownParagraph(BufferView *) const;
324 void cursorHome(BufferView *) const;
326 void cursorEnd(BufferView *) const;
328 void cursorTab(BufferView *) const;
330 void cursorTop(BufferView *) const;
332 void cursorBottom(BufferView *) const;
334 void Delete(BufferView *);
336 void backspace(BufferView *);
338 void deleteWordForward(BufferView *);
340 void deleteWordBackward(BufferView *);
342 void deleteLineForward(BufferView *);
344 bool selectWordWhenUnderCursor(BufferView *);
350 text_capitalization = 1,
354 /// Change the case of the word at cursor position.
355 void changeCase(BufferView *, TextCase action);
357 void changeRegionCase(BufferView * bview,
358 LyXCursor const & from,
359 LyXCursor const & to,
360 LyXText::TextCase action);
362 void transposeChars(BufferView const &);
364 /** returns a printed row in a pixmap. The y value is needed to
365 decide, wether it is selected text or not. This is a strange
368 void getVisibleRow(BufferView *, int y_offset, int x_offset,
369 Row * row_ptr, int y, bool cleared=false);
372 void cutSelection(BufferView *, bool = true);
374 void copySelection(BufferView *);
376 void pasteSelection(BufferView *);
378 void copyEnvironmentType();
380 void pasteEnvironmentType(BufferView *);
382 void insertFootnote();
384 void insertMarginpar();
388 void insertTabular();
390 /** the DTP switches for paragraphs. LyX will store the top settings
391 always in the first physical paragraph, the bottom settings in the
392 last. When a paragraph is broken, the top settings rest, the bottom
393 settings are given to the new one. So I can make shure, they do not
394 duplicate themself (and you cannnot make dirty things with them! )
396 void setParagraph(BufferView *,
397 bool line_top, bool line_bottom,
398 bool pagebreak_top, bool pagebreak_bottom,
399 VSpace const & space_top,
400 VSpace const & space_bottom,
402 string labelwidthstring,
405 /* these things are for search and replace */
407 /** sets the selection over the number of characters of string,
410 void setSelectionOverString(BufferView *, string const & str);
412 /** simple replacing. The font of the first selected character
415 void replaceSelectionWithString(BufferView *, string const & str);
417 /// needed to insert the selection
418 void insertStringAsLines(BufferView *, string const & str);
419 /// needed to insert the selection
420 void insertStringAsParagraphs(BufferView *, string const & str);
422 /// Find next inset of some specified type.
423 bool gotoNextInset(BufferView *, std::vector<Inset::Code> const & codes,
424 string const & contents = string()) const;
427 /* for the greater insets */
429 /// returns false if inset wasn't found
430 bool updateInset(BufferView *, Inset *);
432 void checkParagraph(BufferView *, Paragraph * par,
433 Paragraph::size_type pos);
435 int numberOfCell(Paragraph * par,
436 Paragraph::size_type pos) const;
438 Paragraph * getParFromID(int id);
441 /// returns false if no undo possible
442 bool textUndo(BufferView *);
443 /// returns false if no redo possible
444 bool textRedo(BufferView *);
445 /// used by TextUndo/TextRedo
446 bool textHandleUndo(BufferView *, Undo * undo);
447 /// makes sure the next operation will be stored
449 /// this is dangerous and for internal use only
451 /// this is dangerous and for internal use only
453 /// the flag used by FinishUndo();
454 mutable bool undo_finished;
458 void setUndo(Buffer *, Undo::undo_kind kind,
459 Paragraph const * before,
460 Paragraph const * end) const;
462 void setRedo(Buffer *, Undo::undo_kind kind,
463 Paragraph const * before,
464 Paragraph const * end);
466 Undo * createUndo(Buffer *, Undo::undo_kind kind,
467 Paragraph const * before,
468 Paragraph const * end) const;
469 /// for external use in lyx_cb.C
470 void setCursorParUndo(Buffer *);
472 void removeTableRow(LyXCursor & cursor) const;
474 bool isEmptyTableCell() const;
476 void toggleAppendix(BufferView *);
478 int workWidth(BufferView *) const;
480 void computeBidiTables(Buffer const *, Row * row) const;
482 /// Maps positions in the visual string to positions in logical string.
484 Paragraph::size_type log2vis(Paragraph::size_type pos) const {
485 if (bidi_start == -1)
488 return log2vis_list[pos-bidi_start];
491 /// Maps positions in the logical string to positions in visual string.
493 Paragraph::size_type vis2log(Paragraph::size_type pos) const {
494 if (bidi_start == -1)
497 return vis2log_list[pos-bidi_start];
501 Paragraph::size_type bidi_level(Paragraph::size_type pos) const {
502 if (bidi_start == -1)
505 return bidi_levels[pos-bidi_start];
509 bool bidi_InRange(Paragraph::size_type pos) const {
510 return bidi_start == -1 ||
511 (bidi_start <= pos && pos <= bidi_end);
517 mutable Row * firstrow;
519 mutable Row * lastrow;
521 /** Copybuffer for copy environment type.
522 Asger has learned that this should be a buffer-property instead
523 Lgb has learned that 'char' is a lousy type for non-characters
525 LyXTextClass::size_type copylayouttype;
527 /** inserts a new row behind the specified row, increments
528 the touched counters */
529 void insertRow(Row * row, Paragraph * par,
530 Paragraph::size_type pos) const;
531 /** removes the row and reset the touched counters */
532 void removeRow(Row * row) const;
534 /** remove all following rows of the paragraph of the specified row. */
535 void removeParagraph(Row * row) const;
537 /** insert the specified paragraph behind the specified row */
538 void insertParagraph(BufferView *,
539 Paragraph * par, Row * row) const;
541 /** appends the implizit specified paragraph behind the specified row,
542 * start at the implizit given position */
543 void appendParagraph(BufferView *, Row * row) const;
546 void breakAgain(BufferView *, Row * row) const;
548 void breakAgainOneRow(BufferView *, Row * row);
549 /// Calculate and set the height of the row
550 void setHeightOfRow(BufferView *, Row * row_ptr) const;
552 /** this calculates the specified parameters. needed when setting
553 * the cursor and when creating a visible row */
554 void prepareToPrint(BufferView *, Row * row, float & x,
555 float & fill_separator,
557 float & fill_label_hfill,
558 bool bidi = true) const;
561 void deleteEmptyParagraphMechanism(BufferView *,
562 LyXCursor const & old_cursor) const;
564 /** Updates all counters starting BEHIND the row. Changed paragraphs
565 * with a dynamic left margin will be rebroken. */
566 void updateCounters(BufferView *, Row * row) const;
568 void setCounter(Buffer const *, Paragraph * par) const;
571 * some low level functions
575 int singleWidth(BufferView *, Paragraph * par,
576 Paragraph::size_type pos) const;
578 int singleWidth(BufferView *, Paragraph * par,
579 Paragraph::size_type pos, char c) const;
581 void draw(BufferView *, Row const * row,
582 Paragraph::size_type & pos,
583 int offset, float & x, bool cleared);
585 /// get the next breakpoint in a given paragraph
586 Paragraph::size_type nextBreakPoint(BufferView *, Row const * row,
588 /// returns the minimum space a row needs on the screen in pixel
589 int fill(BufferView *, Row * row, int workwidth) const;
591 /** returns the minimum space a manual label needs on the
593 int labelFill(BufferView *, Row const * row) const;
597 beginningOfMainBody(Buffer const *, Paragraph const * par) const;
599 /** Returns the left beginning of the text.
600 This information cannot be taken from the layouts-objekt, because
601 in LaTeX the beginning of the text fits in some cases
602 (for example sections) exactly the label-width.
604 int leftMargin(BufferView *, Row const * row) const;
606 int rightMargin(Buffer const *, Row const * row) const;
608 int labelEnd (BufferView *, Row const * row) const;
610 /** returns the number of separators in the specified row.
611 The separator on the very last column doesnt count
613 int numberOfSeparators(Buffer const *, Row const * row) const;
615 /** returns the number of hfills in the specified row. The
616 LyX-Hfill is a LaTeX \hfill so that the hfills at the
617 beginning and at the end were ignored. This is {\em MUCH}
618 more usefull than not to ignore!
620 int numberOfHfills(Buffer const *, Row const * row) const;
622 /// like NumberOfHfills, but only those in the manual label!
623 int numberOfLabelHfills(Buffer const *, Row const * row) const;
624 /** returns true, if a expansion is needed. Rules are given by
627 bool hfillExpansion(Buffer const *, Row const * row_ptr,
628 Paragraph::size_type pos) const;
632 mutable std::vector<Paragraph::size_type> log2vis_list;
635 mutable std::vector<Paragraph::size_type> vis2log_list;
638 mutable std::vector<Paragraph::size_type> bidi_levels;
641 mutable Paragraph::size_type bidi_start;
644 mutable Paragraph::size_type bidi_end;
647 mutable bool bidi_same_direction;
650 unsigned char transformChar(unsigned char c, Paragraph * par,
651 Paragraph::size_type pos) const;
653 /** returns the paragraph position of the last character in the
656 Paragraph::size_type rowLast(Row const * row) const;
658 Paragraph::size_type rowLastPrintable(Row const * row) const;
663 // special owner functions
665 Paragraph * ownerParagraph() const;
667 Paragraph * ownerParagraph(Paragraph *) const;
671 /* returns a pointer to the row near the specified y-coordinate
672 * (relative to the whole text). y is set to the real beginning
675 Row * LyXText::getRowNearY(int & y) const
677 // If possible we should optimize this method. (Lgb)
678 Row * tmprow = firstrow;
681 while (tmprow->next() && tmpy + tmprow->height() <= y) {
682 tmpy += tmprow->height();
683 tmprow = tmprow->next();
686 y = tmpy; // return the real y