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"
33 This class holds the mapping between buffer paragraphs and screen rows.
44 NEED_VERY_LITTLE_REFRESH = 2,
50 /// the word around the cursor
52 /// the word begining from the cursor position
54 /// the next word (not yet used)
59 LyXText(BufferView *);
66 void init(BufferView *, bool reinit = false);
68 mutable int number_of_rows;
72 mutable unsigned int width;
73 /// the current font settings
74 mutable LyXFont current_font;
76 mutable LyXFont real_current_font;
77 /// first visible pixel-row is set from LyXScreen!!!
78 // unsigned is wrong here for text-insets!
81 BufferView * bv_owner;
83 InsetText * inset_owner;
85 UpdatableInset * the_locking_inset;
88 int getRealCursorX(BufferView *) const;
90 LyXFont const getFont(Buffer const *, Paragraph * par,
91 Paragraph::size_type pos) const;
93 void setCharFont(Buffer const *, Paragraph * par,
94 Paragraph::size_type pos, LyXFont const & font);
95 void setCharFont(BufferView *, Paragraph * par,
96 Paragraph::size_type pos,
97 LyXFont const & font, bool toggleall);
98 /// returns a pointer to the very first Paragraph
99 Paragraph * firstParagraph() const;
101 /// what you expect when pressing <enter> at cursor position
102 void breakParagraph(BufferView *, char keep_layout = 0);
104 /** set layout over selection and make a total rebreak of
107 Paragraph * setLayout(BufferView *, LyXCursor & actual_cursor,
108 LyXCursor & selection_start,
109 LyXCursor & selection_end,
110 LyXTextClass::size_type layout);
111 void setLayout(BufferView *, LyXTextClass::size_type layout);
113 /// used in setlayout
114 void makeFontEntriesLayoutSpecific(Buffer const *, Paragraph * par);
116 /** increment depth over selection and make a total rebreak of those
119 void incDepth(BufferView *);
121 /** decrement depth over selection and make a total rebreak of those
123 void decDepth(BufferView *);
125 /** Get the depth at current cursor position
127 int getDepth() const { return cursor.par()->getDepth(); }
129 /** set font over selection and make a total rebreak of those
131 toggleall defaults to false.
133 void setFont(BufferView *, LyXFont const &, bool toggleall = false);
135 /** deletes and inserts again all paragaphs between the cursor
136 and the specified par. The Cursor is needed to set the refreshing
138 This function is needed after SetLayout and SetFont etc.
140 void redoParagraphs(BufferView *, LyXCursor const & cursor,
141 Paragraph const * end_par) const;
143 void redoParagraph(BufferView *) const;
146 void toggleFree(BufferView *, LyXFont const &, bool toggleall = false);
148 /** recalculates the heights of all previous rows of the
149 specified paragraph. needed, if the last characters font
152 void redoHeightOfParagraph(BufferView *, LyXCursor const & cursor);
154 /** forces the redrawing of a paragraph. Needed when manipulating a
157 void redoDrawingOfParagraph(BufferView *, LyXCursor const & cursor);
159 /** insert a character, moves all the following breaks in the
160 same Paragraph one to the right and make a little rebreak
162 void insertChar(BufferView *, char c);
164 void insertInset(BufferView *, Inset * inset);
166 /** Completes the insertion with a full rebreak.
167 Returns true if something was broken. */
168 bool fullRebreak(BufferView *);
171 Row * need_break_row;
173 mutable int refresh_y;
181 mutable Row * refresh_row;
185 /// give and set the LyXText status
186 text_status status() const;
187 void status(BufferView *, text_status) const;
190 /** wether the screen needs a refresh,
191 starting with refresh_y
193 mutable text_status status_;
196 /** returns a pointer to the row near the specified y-coordinate
197 (relative to the whole text). y is set to the real beginning
200 Row * getRowNearY(int & y) const;
202 /** returns the column near the specified x-coordinate of the row
203 x is set to the real beginning of this column
205 int getColumnNearX(BufferView *, Row * row,
206 int & x, bool & boundary) const;
208 /** returns a pointer to a specified row. y is set to the beginning
211 Row * getRow(Paragraph * par,
212 Paragraph::size_type pos, int & y) const;
214 /** returns the height of a default row, needed for scrollbar
216 int defaultHeight() const;
219 Later this variable has to be removed. There should be now internal
220 cursor in a text (and thus not in a buffer). By keeping this it is
221 (I think) impossible to have several views with the same buffer, but
222 the cursor placed at different places.
224 Since the LyXText now has been moved from Buffer to BufferView
225 it should not be absolutely needed to move the cursor...
227 mutable LyXCursor cursor;
229 /** The structrue that keeps track of the selections set. */
232 : set_(false), mark_(false)
250 bool set_; // former selection
251 bool mark_; // former mark_set
254 mutable Selection selection;
256 /// needed for the toggling
257 LyXCursor last_sel_cursor;
259 LyXCursor toggle_cursor;
261 LyXCursor toggle_end_cursor;
263 /// need the selection cursor:
264 void setSelection(BufferView *);
266 void clearSelection(BufferView *) const;
268 string const selectionAsString(Buffer const *) const;
270 /// select the word we need depending on word_location
271 void getWord(LyXCursor & from, LyXCursor & to,
272 word_location const) const;
273 /// just selects the word the cursor is in
274 void selectWord(BufferView *, word_location const = WHOLE_WORD);
275 /// returns the inset at cursor (if it exists), 0 otherwise
276 Inset * getInset() const;
278 /** 'selects" the next word, where the cursor is not in
279 and returns this word as string. THe cursor will be moved
280 to the beginning of this word.
281 With SelectSelectedWord can this be highlighted really
283 string const selectNextWord(BufferView *, float & value) const;
285 void selectSelectedWord(BufferView *);
287 void setCursor(BufferView *, Paragraph * par,
288 Paragraph::size_type pos,
290 bool boundary = false) const;
292 void setCursor(BufferView *, LyXCursor &, Paragraph * par,
293 Paragraph::size_type pos,
294 bool boundary = false) const;
296 void setCursorIntern(BufferView *, Paragraph * par,
297 Paragraph::size_type pos,
299 bool boundary = false) const;
301 void setCurrentFont(BufferView *) const;
304 bool isBoundary(Buffer const *, Paragraph * par,
305 Paragraph::size_type pos) const;
307 bool isBoundary(Buffer const *, Paragraph * par,
308 Paragraph::size_type pos,
309 LyXFont const & font) const;
312 void setCursorFromCoordinates(BufferView *, int x, int y) const;
314 void setCursorFromCoordinates(BufferView *, LyXCursor &,
317 void cursorUp(BufferView *) const;
319 void cursorDown(BufferView *) const;
321 void cursorLeft(BufferView *, bool internal = true) const;
323 void cursorRight(BufferView *, bool internal = true) const;
325 void cursorLeftOneWord(BufferView *) const;
327 void cursorLeftOneWord(LyXCursor &) const;
329 void cursorRightOneWord(BufferView *) const;
331 void cursorUpParagraph(BufferView *) const;
333 void cursorDownParagraph(BufferView *) const;
335 void cursorHome(BufferView *) const;
337 void cursorEnd(BufferView *) const;
339 void cursorTab(BufferView *) const;
341 void cursorTop(BufferView *) const;
343 void cursorBottom(BufferView *) const;
345 void Delete(BufferView *);
347 void backspace(BufferView *);
349 void deleteWordForward(BufferView *);
351 void deleteWordBackward(BufferView *);
353 void deleteLineForward(BufferView *);
355 bool selectWordWhenUnderCursor(BufferView *,
356 word_location const = WHOLE_WORD);
362 text_capitalization = 1,
366 /// Change the case of the word at cursor position.
367 void changeCase(BufferView *, TextCase action);
369 void changeRegionCase(BufferView * bview,
370 LyXCursor const & from,
371 LyXCursor const & to,
372 LyXText::TextCase action);
374 void transposeChars(BufferView &);
376 /** returns a printed row in a pixmap. The y value is needed to
377 decide, wether it is selected text or not. This is a strange
380 void getVisibleRow(BufferView *, int y_offset, int x_offset,
381 Row * row_ptr, int y, bool cleared=false);
384 void toggleInset(BufferView *);
386 void cutSelection(BufferView *, bool = true);
388 void copySelection(BufferView *);
390 void pasteSelection(BufferView *);
392 void copyEnvironmentType();
394 void pasteEnvironmentType(BufferView *);
396 void insertFootnote();
398 void insertMarginpar();
402 void insertTabular();
404 /** the DTP switches for paragraphs. LyX will store the top settings
405 always in the first physical paragraph, the bottom settings in the
406 last. When a paragraph is broken, the top settings rest, the bottom
407 settings are given to the new one. So I can make shure, they do not
408 duplicate themself (and you cannnot make dirty things with them! )
410 void setParagraph(BufferView *,
411 bool line_top, bool line_bottom,
412 bool pagebreak_top, bool pagebreak_bottom,
413 VSpace const & space_top,
414 VSpace const & space_bottom,
416 string labelwidthstring,
419 /* these things are for search and replace */
421 /** sets the selection over the number of characters of string,
424 void setSelectionOverString(BufferView *, string const & str);
426 /** simple replacing. The font of the first selected character
429 void replaceSelectionWithString(BufferView *, string const & str);
431 /// needed to insert the selection
432 void insertStringAsLines(BufferView *, string const & str);
433 /// needed to insert the selection
434 void insertStringAsParagraphs(BufferView *, string const & str);
436 /// Find next inset of some specified type.
437 bool gotoNextInset(BufferView *, std::vector<Inset::Code> const & codes,
438 string const & contents = string()) const;
441 /* for the greater insets */
443 /// returns false if inset wasn't found
444 bool updateInset(BufferView *, Inset *);
446 void checkParagraph(BufferView *, Paragraph * par,
447 Paragraph::size_type pos);
449 int numberOfCell(Paragraph * par,
450 Paragraph::size_type pos) const;
452 void removeTableRow(LyXCursor & cursor) const;
454 bool isEmptyTableCell() const;
456 void toggleAppendix(BufferView *);
458 int workWidth(BufferView *) const;
460 void computeBidiTables(Buffer const *, Row * row) const;
462 /// Maps positions in the visual string to positions in logical string.
464 Paragraph::size_type log2vis(Paragraph::size_type pos) const {
465 if (bidi_start == -1)
468 return log2vis_list[pos-bidi_start];
471 /// Maps positions in the logical string to positions in visual string.
473 Paragraph::size_type vis2log(Paragraph::size_type pos) const {
474 if (bidi_start == -1)
477 return vis2log_list[pos-bidi_start];
481 Paragraph::size_type bidi_level(Paragraph::size_type pos) const {
482 if (bidi_start == -1)
485 return bidi_levels[pos-bidi_start];
489 bool bidi_InRange(Paragraph::size_type pos) const {
490 return bidi_start == -1 ||
491 (bidi_start <= pos && pos <= bidi_end);
495 mutable Row * firstrow;
497 mutable Row * lastrow;
499 /** Copybuffer for copy environment type.
500 Asger has learned that this should be a buffer-property instead
501 Lgb has learned that 'char' is a lousy type for non-characters
503 LyXTextClass::size_type copylayouttype;
505 /** inserts a new row behind the specified row, increments
506 the touched counters */
507 void insertRow(Row * row, Paragraph * par,
508 Paragraph::size_type pos) const;
509 /** removes the row and reset the touched counters */
510 void removeRow(Row * row) const;
512 /** remove all following rows of the paragraph of the specified row. */
513 void removeParagraph(Row * row) const;
515 /** insert the specified paragraph behind the specified row */
516 void insertParagraph(BufferView *,
517 Paragraph * par, Row * row) const;
519 /** appends the implizit specified paragraph behind the specified row,
520 * start at the implizit given position */
521 void appendParagraph(BufferView *, Row * row) const;
524 void breakAgain(BufferView *, Row * row) const;
526 void breakAgainOneRow(BufferView *, Row * row);
527 /// Calculate and set the height of the row
528 void setHeightOfRow(BufferView *, Row * row_ptr) const;
530 /** this calculates the specified parameters. needed when setting
531 * the cursor and when creating a visible row */
532 void prepareToPrint(BufferView *, Row * row, float & x,
533 float & fill_separator,
535 float & fill_label_hfill,
536 bool bidi = true) const;
539 void deleteEmptyParagraphMechanism(BufferView *,
540 LyXCursor const & old_cursor) const;
543 /** Updates all counters starting BEHIND the row. Changed paragraphs
544 * with a dynamic left margin will be rebroken. */
545 void updateCounters(BufferView *, Row * row) const;
548 void setCounter(Buffer const *, Paragraph * par) const;
551 * some low level functions
555 int singleWidth(BufferView *, Paragraph * par,
556 Paragraph::size_type pos) const;
558 int singleWidth(BufferView *, Paragraph * par,
559 Paragraph::size_type pos, char c) const;
561 void draw(BufferView *, Row const * row,
562 Paragraph::size_type & pos,
563 int offset, float & x, bool cleared);
565 /// get the next breakpoint in a given paragraph
566 Paragraph::size_type nextBreakPoint(BufferView *, Row const * row,
568 /// returns the minimum space a row needs on the screen in pixel
569 int fill(BufferView *, Row * row, int workwidth) const;
571 /** returns the minimum space a manual label needs on the
573 int labelFill(BufferView *, Row const * row) const;
577 beginningOfMainBody(Buffer const *, Paragraph const * par) const;
579 /** Returns the left beginning of the text.
580 This information cannot be taken from the layouts-objekt, because
581 in LaTeX the beginning of the text fits in some cases
582 (for example sections) exactly the label-width.
584 int leftMargin(BufferView *, Row const * row) const;
586 int rightMargin(Buffer const *, Row const * row) const;
588 int labelEnd (BufferView *, Row const * row) const;
590 /** returns the number of separators in the specified row.
591 The separator on the very last column doesnt count
593 int numberOfSeparators(Buffer const *, Row const * row) const;
595 /** returns the number of hfills in the specified row. The
596 LyX-Hfill is a LaTeX \hfill so that the hfills at the
597 beginning and at the end were ignored. This is {\em MUCH}
598 more usefull than not to ignore!
600 int numberOfHfills(Buffer const *, Row const * row) const;
602 /// like NumberOfHfills, but only those in the manual label!
603 int numberOfLabelHfills(Buffer const *, Row const * row) const;
604 /** returns true, if a expansion is needed. Rules are given by
607 bool hfillExpansion(Buffer const *, Row const * row_ptr,
608 Paragraph::size_type pos) const;
612 mutable std::vector<Paragraph::size_type> log2vis_list;
615 mutable std::vector<Paragraph::size_type> vis2log_list;
618 mutable std::vector<Paragraph::size_type> bidi_levels;
621 mutable Paragraph::size_type bidi_start;
624 mutable Paragraph::size_type bidi_end;
627 mutable bool bidi_same_direction;
630 unsigned char transformChar(unsigned char c, Paragraph * par,
631 Paragraph::size_type pos) const;
633 /** returns the paragraph position of the last character in the
636 Paragraph::size_type rowLast(Row const * row) const;
638 Paragraph::size_type rowLastPrintable(Row const * row) const;
644 // special owner functions
646 Paragraph * ownerParagraph() const;
648 Paragraph * ownerParagraph(Paragraph *) const;
649 // set it searching first for the right owner using the paragraph id
650 Paragraph * ownerParagraph(int id, Paragraph *) const;
654 /* returns a pointer to the row near the specified y-coordinate
655 * (relative to the whole text). y is set to the real beginning
658 Row * LyXText::getRowNearY(int & y) const
660 // If possible we should optimize this method. (Lgb)
661 Row * tmprow = firstrow;
664 while (tmprow->next() && tmpy + tmprow->height() <= y) {
665 tmpy += tmprow->height();
666 tmprow = tmprow->next();
669 y = tmpy; // return the real y