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. */
227 : set_(false), mark_(false)
245 bool set_; // former selection
246 bool mark_; // former mark_set
249 mutable Selection selection;
251 /// needed for the toggling
252 LyXCursor last_sel_cursor;
254 LyXCursor toggle_cursor;
256 LyXCursor toggle_end_cursor;
258 /// need the selection cursor:
259 void setSelection(BufferView *);
261 void clearSelection(BufferView *) const;
263 string const selectionAsString(Buffer const *) const;
265 /// select the word we need depending on word_location
266 void getWord(LyXCursor & from, LyXCursor & to, word_location) const;
267 /// just selects the word the cursor is in
268 void selectWord(BufferView *);
270 /** 'selects" the next word, where the cursor is not in
271 and returns this word as string. THe cursor will be moved
272 to the beginning of this word.
273 With SelectSelectedWord can this be highlighted really
275 string const selectNextWord(BufferView *, float & value) const;
277 void selectSelectedWord(BufferView *);
279 void setCursor(BufferView *, Paragraph * par,
280 Paragraph::size_type pos,
282 bool boundary = false) const;
284 void setCursor(BufferView *, LyXCursor &, Paragraph * par,
285 Paragraph::size_type pos,
286 bool boundary = false) const;
288 void setCursorIntern(BufferView *, Paragraph * par,
289 Paragraph::size_type pos,
291 bool boundary = false) const;
293 void setCurrentFont(BufferView *) const;
296 bool isBoundary(Buffer const *, Paragraph * par,
297 Paragraph::size_type pos) const;
299 bool isBoundary(Buffer const *, Paragraph * par,
300 Paragraph::size_type pos,
301 LyXFont const & font) const;
304 void setCursorFromCoordinates(BufferView *, int x, int y) const;
306 void setCursorFromCoordinates(BufferView *, LyXCursor &,
309 void cursorUp(BufferView *) const;
311 void cursorDown(BufferView *) const;
313 void cursorLeft(BufferView *, bool internal = true) const;
315 void cursorRight(BufferView *, bool internal = true) const;
317 void cursorLeftOneWord(BufferView *) const;
319 void cursorLeftOneWord(LyXCursor &) const;
321 void cursorRightOneWord(BufferView *) const;
323 void cursorUpParagraph(BufferView *) const;
325 void cursorDownParagraph(BufferView *) const;
327 void cursorHome(BufferView *) const;
329 void cursorEnd(BufferView *) const;
331 void cursorTab(BufferView *) const;
333 void cursorTop(BufferView *) const;
335 void cursorBottom(BufferView *) const;
337 void Delete(BufferView *);
339 void backspace(BufferView *);
341 void deleteWordForward(BufferView *);
343 void deleteWordBackward(BufferView *);
345 void deleteLineForward(BufferView *);
347 bool selectWordWhenUnderCursor(BufferView *);
353 text_capitalization = 1,
357 /// Change the case of the word at cursor position.
358 void changeCase(BufferView *, TextCase action);
360 void changeRegionCase(BufferView * bview,
361 LyXCursor const & from,
362 LyXCursor const & to,
363 LyXText::TextCase action);
365 void transposeChars(BufferView const &);
367 /** returns a printed row in a pixmap. The y value is needed to
368 decide, wether it is selected text or not. This is a strange
371 void getVisibleRow(BufferView *, int y_offset, int x_offset,
372 Row * row_ptr, int y, bool cleared=false);
375 void cutSelection(BufferView *, bool = true);
377 void copySelection(BufferView *);
379 void pasteSelection(BufferView *);
381 void copyEnvironmentType();
383 void pasteEnvironmentType(BufferView *);
385 void insertFootnote();
387 void insertMarginpar();
391 void insertTabular();
393 /** the DTP switches for paragraphs. LyX will store the top settings
394 always in the first physical paragraph, the bottom settings in the
395 last. When a paragraph is broken, the top settings rest, the bottom
396 settings are given to the new one. So I can make shure, they do not
397 duplicate themself (and you cannnot make dirty things with them! )
399 void setParagraph(BufferView *,
400 bool line_top, bool line_bottom,
401 bool pagebreak_top, bool pagebreak_bottom,
402 VSpace const & space_top,
403 VSpace const & space_bottom,
405 string labelwidthstring,
408 /* these things are for search and replace */
410 /** sets the selection over the number of characters of string,
413 void setSelectionOverString(BufferView *, string const & str);
415 /** simple replacing. The font of the first selected character
418 void replaceSelectionWithString(BufferView *, string const & str);
420 /// needed to insert the selection
421 void insertStringAsLines(BufferView *, string const & str);
422 /// needed to insert the selection
423 void insertStringAsParagraphs(BufferView *, string const & str);
425 /// Find next inset of some specified type.
426 bool gotoNextInset(BufferView *, std::vector<Inset::Code> const & codes,
427 string const & contents = string()) const;
430 /* for the greater insets */
432 /// returns false if inset wasn't found
433 bool updateInset(BufferView *, Inset *);
435 void checkParagraph(BufferView *, Paragraph * par,
436 Paragraph::size_type pos);
438 int numberOfCell(Paragraph * par,
439 Paragraph::size_type pos) const;
441 Paragraph * getParFromID(int id);
444 /// returns false if no undo possible
445 bool textUndo(BufferView *);
446 /// returns false if no redo possible
447 bool textRedo(BufferView *);
448 /// used by TextUndo/TextRedo
449 bool textHandleUndo(BufferView *, Undo * undo);
450 /// makes sure the next operation will be stored
452 /// this is dangerous and for internal use only
454 /// this is dangerous and for internal use only
456 /// the flag used by FinishUndo();
457 mutable bool undo_finished;
461 void setUndo(Buffer *, Undo::undo_kind kind,
462 Paragraph const * before,
463 Paragraph const * end) const;
465 void setRedo(Buffer *, Undo::undo_kind kind,
466 Paragraph const * before,
467 Paragraph const * end);
469 Undo * createUndo(Buffer *, Undo::undo_kind kind,
470 Paragraph const * before,
471 Paragraph const * end) const;
472 /// for external use in lyx_cb.C
473 void setCursorParUndo(Buffer *);
475 void removeTableRow(LyXCursor & cursor) const;
477 bool isEmptyTableCell() const;
479 void toggleAppendix(BufferView *);
481 int workWidth(BufferView *) const;
483 void computeBidiTables(Buffer const *, Row * row) const;
485 /// Maps positions in the visual string to positions in logical string.
487 Paragraph::size_type log2vis(Paragraph::size_type pos) const {
488 if (bidi_start == -1)
491 return log2vis_list[pos-bidi_start];
494 /// Maps positions in the logical string to positions in visual string.
496 Paragraph::size_type vis2log(Paragraph::size_type pos) const {
497 if (bidi_start == -1)
500 return vis2log_list[pos-bidi_start];
504 Paragraph::size_type bidi_level(Paragraph::size_type pos) const {
505 if (bidi_start == -1)
508 return bidi_levels[pos-bidi_start];
512 bool bidi_InRange(Paragraph::size_type pos) const {
513 return bidi_start == -1 ||
514 (bidi_start <= pos && pos <= bidi_end);
518 mutable Row * firstrow;
520 mutable Row * lastrow;
522 /** Copybuffer for copy environment type.
523 Asger has learned that this should be a buffer-property instead
524 Lgb has learned that 'char' is a lousy type for non-characters
526 LyXTextClass::size_type copylayouttype;
528 /** inserts a new row behind the specified row, increments
529 the touched counters */
530 void insertRow(Row * row, Paragraph * par,
531 Paragraph::size_type pos) const;
532 /** removes the row and reset the touched counters */
533 void removeRow(Row * row) const;
535 /** remove all following rows of the paragraph of the specified row. */
536 void removeParagraph(Row * row) const;
538 /** insert the specified paragraph behind the specified row */
539 void insertParagraph(BufferView *,
540 Paragraph * par, Row * row) const;
542 /** appends the implizit specified paragraph behind the specified row,
543 * start at the implizit given position */
544 void appendParagraph(BufferView *, Row * row) const;
547 void breakAgain(BufferView *, Row * row) const;
549 void breakAgainOneRow(BufferView *, Row * row);
550 /// Calculate and set the height of the row
551 void setHeightOfRow(BufferView *, Row * row_ptr) const;
553 /** this calculates the specified parameters. needed when setting
554 * the cursor and when creating a visible row */
555 void prepareToPrint(BufferView *, Row * row, float & x,
556 float & fill_separator,
558 float & fill_label_hfill,
559 bool bidi = true) const;
562 void deleteEmptyParagraphMechanism(BufferView *,
563 LyXCursor const & old_cursor) const;
565 /** Updates all counters starting BEHIND the row. Changed paragraphs
566 * with a dynamic left margin will be rebroken. */
567 void updateCounters(BufferView *, Row * row) const;
569 void setCounter(Buffer const *, Paragraph * par) const;
572 * some low level functions
576 int singleWidth(BufferView *, Paragraph * par,
577 Paragraph::size_type pos) const;
579 int singleWidth(BufferView *, Paragraph * par,
580 Paragraph::size_type pos, char c) const;
582 void draw(BufferView *, Row const * row,
583 Paragraph::size_type & pos,
584 int offset, float & x, bool cleared);
586 /// get the next breakpoint in a given paragraph
587 Paragraph::size_type nextBreakPoint(BufferView *, Row const * row,
589 /// returns the minimum space a row needs on the screen in pixel
590 int fill(BufferView *, Row * row, int workwidth) const;
592 /** returns the minimum space a manual label needs on the
594 int labelFill(BufferView *, Row const * row) const;
598 beginningOfMainBody(Buffer const *, Paragraph const * par) const;
600 /** Returns the left beginning of the text.
601 This information cannot be taken from the layouts-objekt, because
602 in LaTeX the beginning of the text fits in some cases
603 (for example sections) exactly the label-width.
605 int leftMargin(BufferView *, Row const * row) const;
607 int rightMargin(Buffer const *, Row const * row) const;
609 int labelEnd (BufferView *, Row const * row) const;
611 /** returns the number of separators in the specified row.
612 The separator on the very last column doesnt count
614 int numberOfSeparators(Buffer const *, Row const * row) const;
616 /** returns the number of hfills in the specified row. The
617 LyX-Hfill is a LaTeX \hfill so that the hfills at the
618 beginning and at the end were ignored. This is {\em MUCH}
619 more usefull than not to ignore!
621 int numberOfHfills(Buffer const *, Row const * row) const;
623 /// like NumberOfHfills, but only those in the manual label!
624 int numberOfLabelHfills(Buffer const *, Row const * row) const;
625 /** returns true, if a expansion is needed. Rules are given by
628 bool hfillExpansion(Buffer const *, Row const * row_ptr,
629 Paragraph::size_type pos) const;
633 mutable std::vector<Paragraph::size_type> log2vis_list;
636 mutable std::vector<Paragraph::size_type> vis2log_list;
639 mutable std::vector<Paragraph::size_type> bidi_levels;
642 mutable Paragraph::size_type bidi_start;
645 mutable Paragraph::size_type bidi_end;
648 mutable bool bidi_same_direction;
651 unsigned char transformChar(unsigned char c, Paragraph * par,
652 Paragraph::size_type pos) const;
654 /** returns the paragraph position of the last character in the
657 Paragraph::size_type rowLast(Row const * row) const;
659 Paragraph::size_type rowLastPrintable(Row const * row) const;
664 // special owner functions
666 Paragraph * ownerParagraph() const;
668 Paragraph * ownerParagraph(Paragraph *) const;
672 /* returns a pointer to the row near the specified y-coordinate
673 * (relative to the whole text). y is set to the real beginning
676 Row * LyXText::getRowNearY(int & y) const
678 // If possible we should optimize this method. (Lgb)
679 Row * tmprow = firstrow;
682 while (tmprow->next() && tmpy + tmprow->height() <= y) {
683 tmpy += tmprow->height();
684 tmprow = tmprow->next();
687 y = tmpy; // return the real y