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 transposeChars(BufferView const &);
359 /** returns a printed row in a pixmap. The y value is needed to
360 decide, wether it is selected text or not. This is a strange
363 void getVisibleRow(BufferView *, int y_offset, int x_offset,
364 Row * row_ptr, int y, bool cleared=false);
367 void cutSelection(BufferView *, bool = true);
369 void copySelection(BufferView *);
371 void pasteSelection(BufferView *);
373 void copyEnvironmentType();
375 void pasteEnvironmentType(BufferView *);
377 void insertFootnote();
379 void insertMarginpar();
383 void insertTabular();
385 /** the DTP switches for paragraphs. LyX will store the top settings
386 always in the first physical paragraph, the bottom settings in the
387 last. When a paragraph is broken, the top settings rest, the bottom
388 settings are given to the new one. So I can make shure, they do not
389 duplicate themself (and you cannnot make dirty things with them! )
391 void setParagraph(BufferView *,
392 bool line_top, bool line_bottom,
393 bool pagebreak_top, bool pagebreak_bottom,
394 VSpace const & space_top,
395 VSpace const & space_bottom,
397 string labelwidthstring,
400 /* these things are for search and replace */
402 /** sets the selection over the number of characters of string,
405 void setSelectionOverString(BufferView *, string const & str);
407 /** simple replacing. The font of the first selected character
410 void replaceSelectionWithString(BufferView *, string const & str);
412 /// needed to insert the selection
413 void insertStringAsLines(BufferView *, string const & str);
414 /// needed to insert the selection
415 void insertStringAsParagraphs(BufferView *, string const & str);
417 /// Find next inset of some specified type.
418 bool gotoNextInset(BufferView *, std::vector<Inset::Code> const & codes,
419 string const & contents = string()) const;
422 /* for the greater insets */
424 /// returns false if inset wasn't found
425 bool updateInset(BufferView *, Inset *);
427 void checkParagraph(BufferView *, Paragraph * par,
428 Paragraph::size_type pos);
430 int numberOfCell(Paragraph * par,
431 Paragraph::size_type pos) const;
433 Paragraph * getParFromID(int id);
436 /// returns false if no undo possible
437 bool textUndo(BufferView *);
438 /// returns false if no redo possible
439 bool textRedo(BufferView *);
440 /// used by TextUndo/TextRedo
441 bool textHandleUndo(BufferView *, Undo * undo);
442 /// makes sure the next operation will be stored
444 /// this is dangerous and for internal use only
446 /// this is dangerous and for internal use only
448 /// the flag used by FinishUndo();
449 mutable bool undo_finished;
453 void setUndo(Buffer *, Undo::undo_kind kind,
454 Paragraph const * before,
455 Paragraph const * end) const;
457 void setRedo(Buffer *, Undo::undo_kind kind,
458 Paragraph const * before,
459 Paragraph const * end);
461 Undo * createUndo(Buffer *, Undo::undo_kind kind,
462 Paragraph const * before,
463 Paragraph const * end) const;
464 /// for external use in lyx_cb.C
465 void setCursorParUndo(Buffer *);
467 void removeTableRow(LyXCursor & cursor) const;
469 bool isEmptyTableCell() const;
471 void toggleAppendix(BufferView *);
473 int workWidth(BufferView *) const;
475 void computeBidiTables(Buffer const *, Row * row) const;
477 /// Maps positions in the visual string to positions in logical string.
479 Paragraph::size_type log2vis(Paragraph::size_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 Paragraph::size_type vis2log(Paragraph::size_type pos) const {
489 if (bidi_start == -1)
492 return vis2log_list[pos-bidi_start];
496 Paragraph::size_type bidi_level(Paragraph::size_type pos) const {
497 if (bidi_start == -1)
500 return bidi_levels[pos-bidi_start];
504 bool bidi_InRange(Paragraph::size_type pos) const {
505 return bidi_start == -1 ||
506 (bidi_start <= pos && pos <= bidi_end);
512 mutable Row * firstrow;
514 mutable Row * lastrow;
516 /** Copybuffer for copy environment type.
517 Asger has learned that this should be a buffer-property instead
518 Lgb has learned that 'char' is a lousy type for non-characters
520 LyXTextClass::size_type copylayouttype;
522 /** inserts a new row behind the specified row, increments
523 the touched counters */
524 void insertRow(Row * row, Paragraph * par,
525 Paragraph::size_type pos) const;
526 /** removes the row and reset the touched counters */
527 void removeRow(Row * row) const;
529 /** remove all following rows of the paragraph of the specified row. */
530 void removeParagraph(Row * row) const;
532 /** insert the specified paragraph behind the specified row */
533 void insertParagraph(BufferView *,
534 Paragraph * par, Row * row) const;
536 /** appends the implizit specified paragraph behind the specified row,
537 * start at the implizit given position */
538 void appendParagraph(BufferView *, Row * row) const;
541 void breakAgain(BufferView *, Row * row) const;
543 void breakAgainOneRow(BufferView *, Row * row);
544 /// Calculate and set the height of the row
545 void setHeightOfRow(BufferView *, Row * row_ptr) const;
547 /** this calculates the specified parameters. needed when setting
548 * the cursor and when creating a visible row */
549 void prepareToPrint(BufferView *, Row * row, float & x,
550 float & fill_separator,
552 float & fill_label_hfill,
553 bool bidi = true) const;
556 void deleteEmptyParagraphMechanism(BufferView *,
557 LyXCursor const & old_cursor) const;
559 /** Updates all counters starting BEHIND the row. Changed paragraphs
560 * with a dynamic left margin will be rebroken. */
561 void updateCounters(BufferView *, Row * row) const;
563 void setCounter(Buffer const *, Paragraph * par) const;
566 * some low level functions
570 int singleWidth(BufferView *, Paragraph * par,
571 Paragraph::size_type pos) const;
573 int singleWidth(BufferView *, Paragraph * par,
574 Paragraph::size_type pos, char c) const;
576 void draw(BufferView *, Row const * row,
577 Paragraph::size_type & pos,
578 int offset, float & x, bool cleared);
580 /// get the next breakpoint in a given paragraph
581 Paragraph::size_type nextBreakPoint(BufferView *, Row const * row,
583 /// returns the minimum space a row needs on the screen in pixel
584 int fill(BufferView *, Row * row, int workwidth) const;
586 /** returns the minimum space a manual label needs on the
588 int labelFill(BufferView *, Row const * row) const;
592 beginningOfMainBody(Buffer const *, Paragraph const * par) const;
594 /** Returns the left beginning of the text.
595 This information cannot be taken from the layouts-objekt, because
596 in LaTeX the beginning of the text fits in some cases
597 (for example sections) exactly the label-width.
599 int leftMargin(BufferView *, Row const * row) const;
601 int rightMargin(Buffer const *, Row const * row) const;
603 int labelEnd (BufferView *, Row const * row) const;
605 /** returns the number of separators in the specified row.
606 The separator on the very last column doesnt count
608 int numberOfSeparators(Buffer const *, Row const * row) const;
610 /** returns the number of hfills in the specified row. The
611 LyX-Hfill is a LaTeX \hfill so that the hfills at the
612 beginning and at the end were ignored. This is {\em MUCH}
613 more usefull than not to ignore!
615 int numberOfHfills(Buffer const *, Row const * row) const;
617 /// like NumberOfHfills, but only those in the manual label!
618 int numberOfLabelHfills(Buffer const *, Row const * row) const;
619 /** returns true, if a expansion is needed. Rules are given by
622 bool hfillExpansion(Buffer const *, Row const * row_ptr,
623 Paragraph::size_type pos) const;
627 mutable std::vector<Paragraph::size_type> log2vis_list;
630 mutable std::vector<Paragraph::size_type> vis2log_list;
633 mutable std::vector<Paragraph::size_type> bidi_levels;
636 mutable Paragraph::size_type bidi_start;
639 mutable Paragraph::size_type bidi_end;
642 mutable bool bidi_same_direction;
645 unsigned char transformChar(unsigned char c, Paragraph * par,
646 Paragraph::size_type pos) const;
648 /** returns the paragraph position of the last character in the
651 Paragraph::size_type rowLast(Row const * row) const;
653 Paragraph::size_type rowLastPrintable(Row const * row) const;
658 // special owner functions
660 Paragraph * ownerParagraph() const;
662 Paragraph * ownerParagraph(Paragraph *) const;
666 /* returns a pointer to the row near the specified y-coordinate
667 * (relative to the whole text). y is set to the real beginning
670 Row * LyXText::getRowNearY(int & y) const
672 // If possible we should optimize this method. (Lgb)
673 Row * tmprow = firstrow;
676 while (tmprow->next() && tmpy + tmprow->height() <= y) {
677 tmpy += tmprow->height();
678 tmprow = tmprow->next();
681 y = tmpy; // return the real y