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"
35 This class holds the mapping between buffer paragraphs and screen rows.
46 NEED_VERY_LITTLE_REFRESH = 2,
52 // the word around the cursor, only if the cursor is
55 // the word around the cursor
57 /// the word begining from the cursor position
59 /// the word around the cursor or before the cursor
61 /// the next word (not yet used)
66 LyXText(BufferView *);
73 void init(BufferView *, bool reinit = false);
75 mutable int number_of_rows;
79 mutable unsigned int width;
80 /// the current font settings
81 mutable LyXFont current_font;
83 mutable LyXFont real_current_font;
84 /// first visible pixel-row is set from LyXScreen!!!
85 // unsigned is wrong here for text-insets!
88 BufferView * bv_owner;
90 InsetText * inset_owner;
92 UpdatableInset * the_locking_inset;
95 int getRealCursorX(BufferView *) const;
97 LyXFont const getFont(Buffer const *, Paragraph * par,
98 Paragraph::size_type pos) const;
100 LyXFont const getLayoutFont(Buffer const *, Paragraph * par) const;
102 LyXFont const getLabelFont(Buffer const *, Paragraph * par) const;
104 void setCharFont(Buffer const *, Paragraph * par,
105 Paragraph::size_type pos, LyXFont const & font);
106 void setCharFont(BufferView *, Paragraph * par,
107 Paragraph::size_type pos,
108 LyXFont const & font, bool toggleall);
109 /// returns a pointer to the very first Paragraph
110 Paragraph * firstParagraph() const;
112 /// what you expect when pressing <enter> at cursor position
113 void breakParagraph(BufferView *, char keep_layout = 0);
115 /** set layout over selection and make a total rebreak of
118 Paragraph * setLayout(BufferView *, LyXCursor & actual_cursor,
119 LyXCursor & selection_start,
120 LyXCursor & selection_end,
121 LyXTextClass::size_type layout);
122 void setLayout(BufferView *, LyXTextClass::size_type layout);
124 /// used in setlayout
125 void makeFontEntriesLayoutSpecific(Buffer const *, Paragraph * par);
127 /** increment depth over selection and make a total rebreak of those
130 void incDepth(BufferView *);
132 /** decrement depth over selection and make a total rebreak of those
134 void decDepth(BufferView *);
136 /** Get the depth at current cursor position
138 int getDepth() const { return cursor.par()->getDepth(); }
140 /** set font over selection and make a total rebreak of those
142 toggleall defaults to false.
144 void setFont(BufferView *, LyXFont const &, bool toggleall = false);
146 /** deletes and inserts again all paragaphs between the cursor
147 and the specified par. The Cursor is needed to set the refreshing
149 This function is needed after SetLayout and SetFont etc.
151 void redoParagraphs(BufferView *, LyXCursor const & cursor,
152 Paragraph const * end_par) const;
154 void redoParagraph(BufferView *) const;
157 void toggleFree(BufferView *, LyXFont const &, bool toggleall = false);
160 string getStringToIndex(BufferView *);
162 /** recalculates the heights of all previous rows of the
163 specified paragraph. needed, if the last characters font
166 void redoHeightOfParagraph(BufferView *, LyXCursor const & cursor);
168 /** forces the redrawing of a paragraph. Needed when manipulating a
171 void redoDrawingOfParagraph(BufferView *, LyXCursor const & cursor);
173 /** insert a character, moves all the following breaks in the
174 same Paragraph one to the right and make a little rebreak
176 void insertChar(BufferView *, char c);
178 void insertInset(BufferView *, Inset * inset);
180 /** Completes the insertion with a full rebreak.
181 Returns true if something was broken. */
182 bool fullRebreak(BufferView *);
185 mutable Row * need_break_row;
187 mutable int refresh_y;
195 mutable Row * refresh_row;
199 /// give and set the LyXText status
200 text_status status() const;
202 void status(BufferView *, text_status) const;
205 /** wether the screen needs a refresh,
206 starting with refresh_y
208 mutable text_status status_;
211 /** returns a pointer to the row near the specified y-coordinate
212 (relative to the whole text). y is set to the real beginning
215 Row * getRowNearY(int & y) const;
217 /** returns the column near the specified x-coordinate of the row
218 x is set to the real beginning of this column
220 int getColumnNearX(BufferView *, Row * row,
221 int & x, bool & boundary) const;
223 /** returns a pointer to a specified row. y is set to the beginning
226 Row * getRow(Paragraph * par,
227 Paragraph::size_type pos, int & y) const;
229 /** returns the height of a default row, needed for scrollbar
231 int defaultHeight() const;
234 Later this variable has to be removed. There should be now internal
235 cursor in a text (and thus not in a buffer). By keeping this it is
236 (I think) impossible to have several views with the same buffer, but
237 the cursor placed at different places.
239 Since the LyXText now has been moved from Buffer to BufferView
240 it should not be absolutely needed to move the cursor...
242 mutable LyXCursor cursor;
244 /** The structrue that keeps track of the selections set. */
247 : set_(false), mark_(false)
265 bool set_; // former selection
266 bool mark_; // former mark_set
269 mutable Selection selection;
271 /// needed for the toggling
272 LyXCursor last_sel_cursor;
274 LyXCursor toggle_cursor;
276 LyXCursor toggle_end_cursor;
278 /// need the selection cursor:
279 void setSelection(BufferView *);
281 void clearSelection() const;
283 string const selectionAsString(Buffer const *) const;
285 /// select the word we need depending on word_location
286 void getWord(LyXCursor & from, LyXCursor & to,
287 word_location const) const;
288 /// just selects the word the cursor is in
289 void selectWord(BufferView *, word_location const);
290 /// returns the inset at cursor (if it exists), 0 otherwise
291 Inset * getInset() const;
293 /** 'selects" the next word, where the cursor is not in
294 and returns this word as string. THe cursor will be moved
295 to the beginning of this word.
296 With SelectSelectedWord can this be highlighted really
298 string const selectNextWord(BufferView *, float & value) const;
300 void selectSelectedWord(BufferView *);
302 void setCursor(BufferView *, Paragraph * par,
303 Paragraph::size_type pos,
305 bool boundary = false) const;
307 void setCursor(BufferView *, LyXCursor &, Paragraph * par,
308 Paragraph::size_type pos,
309 bool boundary = false) const;
311 void setCursorIntern(BufferView *, Paragraph * par,
312 Paragraph::size_type pos,
314 bool boundary = false) const;
316 void setCurrentFont(BufferView *) const;
319 bool isBoundary(Buffer const *, Paragraph * par,
320 Paragraph::size_type pos) const;
322 bool isBoundary(Buffer const *, Paragraph * par,
323 Paragraph::size_type pos,
324 LyXFont const & font) const;
327 void setCursorFromCoordinates(BufferView *, int x, int y) const;
329 void setCursorFromCoordinates(BufferView *, LyXCursor &,
332 void cursorUp(BufferView *) const;
334 void cursorDown(BufferView *) const;
336 void cursorLeft(BufferView *, bool internal = true) const;
338 void cursorRight(BufferView *, bool internal = true) const;
340 void cursorLeftOneWord(BufferView *) const;
342 void cursorLeftOneWord(LyXCursor &) const;
344 void cursorRightOneWord(BufferView *) const;
346 void cursorUpParagraph(BufferView *) const;
348 void cursorDownParagraph(BufferView *) const;
350 void cursorHome(BufferView *) const;
352 void cursorEnd(BufferView *) const;
354 void cursorTab(BufferView *) const;
356 void cursorTop(BufferView *) const;
358 void cursorBottom(BufferView *) const;
360 void Delete(BufferView *);
362 void backspace(BufferView *);
364 void deleteWordForward(BufferView *);
366 void deleteWordBackward(BufferView *);
368 void deleteLineForward(BufferView *);
370 bool selectWordWhenUnderCursor(BufferView *,
371 word_location const);
377 text_capitalization = 1,
381 /// Change the case of the word at cursor position.
382 void changeCase(BufferView *, TextCase action);
384 void changeRegionCase(BufferView * bview,
385 LyXCursor const & from,
386 LyXCursor const & to,
387 LyXText::TextCase action);
389 void transposeChars(BufferView &);
391 /** returns a printed row in a pixmap. The y value is needed to
392 decide, wether it is selected text or not. This is a strange
395 void getVisibleRow(BufferView *, int y_offset, int x_offset,
396 Row * row_ptr, int y, bool cleared=false);
399 void toggleInset(BufferView *);
401 void cutSelection(BufferView *, bool doclear = true, bool realcut = true);
403 void copySelection(BufferView *);
405 void pasteSelection(BufferView *);
407 void copyEnvironmentType();
409 void pasteEnvironmentType(BufferView *);
411 /** the DTP switches for paragraphs. LyX will store the top settings
412 always in the first physical paragraph, the bottom settings in the
413 last. When a paragraph is broken, the top settings rest, the bottom
414 settings are given to the new one. So I can make shure, they do not
415 duplicate themself (and you cannnot make dirty things with them! )
417 void setParagraph(BufferView *,
418 bool line_top, bool line_bottom,
419 bool pagebreak_top, bool pagebreak_bottom,
420 VSpace const & space_top,
421 VSpace const & space_bottom,
422 Spacing const & spacing,
424 string labelwidthstring,
427 /* these things are for search and replace */
429 /** sets the selection over the number of characters of string,
432 void setSelectionOverString(BufferView *, string const & str);
434 /** simple replacing. The font of the first selected character
437 void replaceSelectionWithString(BufferView *, string const & str);
439 /// needed to insert the selection
440 void insertStringAsLines(BufferView *, string const & str);
441 /// needed to insert the selection
442 void insertStringAsParagraphs(BufferView *, string const & str);
444 /// Find next inset of some specified type.
445 bool gotoNextInset(BufferView *, std::vector<Inset::Code> const & codes,
446 string const & contents = string()) const;
449 /* for the greater insets */
451 /// returns false if inset wasn't found
452 bool updateInset(BufferView *, Inset *);
454 void checkParagraph(BufferView *, Paragraph * par,
455 Paragraph::size_type pos);
457 int numberOfCell(Paragraph * par,
458 Paragraph::size_type pos) const;
460 void removeTableRow(LyXCursor & cursor) const;
462 bool isEmptyTableCell() const;
464 void toggleAppendix(BufferView *);
466 int workWidth(BufferView *) const;
468 void computeBidiTables(Buffer const *, Row * row) const;
470 /// Maps positions in the visual string to positions in logical string.
472 Paragraph::size_type log2vis(Paragraph::size_type pos) const {
473 if (bidi_start == -1)
476 return log2vis_list[pos-bidi_start];
479 /// Maps positions in the logical string to positions in visual string.
481 Paragraph::size_type vis2log(Paragraph::size_type pos) const {
482 if (bidi_start == -1)
485 return vis2log_list[pos-bidi_start];
489 Paragraph::size_type bidi_level(Paragraph::size_type pos) const {
490 if (bidi_start == -1)
493 return bidi_levels[pos-bidi_start];
497 bool bidi_InRange(Paragraph::size_type pos) const {
498 return bidi_start == -1 ||
499 (bidi_start <= pos && pos <= bidi_end);
503 mutable Row * firstrow;
505 mutable Row * lastrow;
507 /** Copybuffer for copy environment type.
508 Asger has learned that this should be a buffer-property instead
509 Lgb has learned that 'char' is a lousy type for non-characters
511 LyXTextClass::size_type copylayouttype;
513 /** inserts a new row behind the specified row, increments
514 the touched counters */
515 void insertRow(Row * row, Paragraph * par,
516 Paragraph::size_type pos) const;
517 /** removes the row and reset the touched counters */
518 void removeRow(Row * row) const;
520 /** remove all following rows of the paragraph of the specified row. */
521 void removeParagraph(Row * row) const;
523 /** insert the specified paragraph behind the specified row */
524 void insertParagraph(BufferView *,
525 Paragraph * par, Row * row) const;
527 /** appends the implizit specified paragraph behind the specified row,
528 * start at the implizit given position */
529 void appendParagraph(BufferView *, Row * row) const;
532 void breakAgain(BufferView *, Row * row) const;
534 void breakAgainOneRow(BufferView *, Row * row);
535 /// Calculate and set the height of the row
536 void setHeightOfRow(BufferView *, Row * row_ptr) const;
538 /** this calculates the specified parameters. needed when setting
539 * the cursor and when creating a visible row */
540 void prepareToPrint(BufferView *, Row * row, float & x,
541 float & fill_separator,
543 float & fill_label_hfill,
544 bool bidi = true) const;
547 void deleteEmptyParagraphMechanism(BufferView *,
548 LyXCursor const & old_cursor) const;
551 /** Updates all counters starting BEHIND the row. Changed paragraphs
552 * with a dynamic left margin will be rebroken. */
553 void updateCounters(BufferView *, Row * row) const;
556 void setCounter(Buffer const *, Paragraph * par) const;
559 * some low level functions
563 int singleWidth(BufferView *, Paragraph * par,
564 Paragraph::size_type pos) const;
566 int singleWidth(BufferView *, Paragraph * par,
567 Paragraph::size_type pos, char c) const;
569 void draw(BufferView *, Row const * row,
570 Paragraph::size_type & pos,
571 int offset, float & x, bool cleared);
573 /// get the next breakpoint in a given paragraph
574 Paragraph::size_type nextBreakPoint(BufferView *, Row const * row,
576 /// returns the minimum space a row needs on the screen in pixel
577 int fill(BufferView *, Row * row, int workwidth) const;
579 /** returns the minimum space a manual label needs on the
581 int labelFill(BufferView *, Row const * row) const;
585 beginningOfMainBody(Buffer const *, Paragraph const * par) const;
587 /** Returns the left beginning of the text.
588 This information cannot be taken from the layouts-objekt, because
589 in LaTeX the beginning of the text fits in some cases
590 (for example sections) exactly the label-width.
592 int leftMargin(BufferView *, Row const * row) const;
594 int rightMargin(Buffer const *, Row const * row) const;
596 int labelEnd (BufferView *, Row const * row) const;
598 /** returns the number of separators in the specified row.
599 The separator on the very last column doesnt count
601 int numberOfSeparators(Buffer const *, Row const * row) const;
603 /** returns the number of hfills in the specified row. The
604 LyX-Hfill is a LaTeX \hfill so that the hfills at the
605 beginning and at the end were ignored. This is {\em MUCH}
606 more usefull than not to ignore!
608 int numberOfHfills(Buffer const *, Row const * row) const;
610 /// like NumberOfHfills, but only those in the manual label!
611 int numberOfLabelHfills(Buffer const *, Row const * row) const;
612 /** returns true, if a expansion is needed. Rules are given by
615 bool hfillExpansion(Buffer const *, Row const * row_ptr,
616 Paragraph::size_type pos) const;
618 LColor::color backgroundColor();
622 mutable std::vector<Paragraph::size_type> log2vis_list;
625 mutable std::vector<Paragraph::size_type> vis2log_list;
628 mutable std::vector<Paragraph::size_type> bidi_levels;
631 mutable Paragraph::size_type bidi_start;
634 mutable Paragraph::size_type bidi_end;
637 mutable bool bidi_same_direction;
640 unsigned char transformChar(unsigned char c, Paragraph * par,
641 Paragraph::size_type pos) const;
643 /** returns the paragraph position of the last character in the
646 Paragraph::size_type rowLast(Row const * row) const;
648 Paragraph::size_type rowLastPrintable(Row const * row) const;
654 // special owner functions
656 Paragraph * ownerParagraph() const;
658 Paragraph * ownerParagraph(Paragraph *) const;
659 // set it searching first for the right owner using the paragraph id
660 Paragraph * ownerParagraph(int id, Paragraph *) const;
664 /* returns a pointer to the row near the specified y-coordinate
665 * (relative to the whole text). y is set to the real beginning
668 Row * LyXText::getRowNearY(int & y) const
670 // If possible we should optimize this method. (Lgb)
671 Row * tmprow = firstrow;
674 while (tmprow->next() && tmpy + tmprow->height() <= y) {
675 tmpy += tmprow->height();
676 tmprow = tmprow->next();
679 y = tmpy; // return the real y