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"
23 #include "insets/inset.h"
37 This class holds the mapping between buffer paragraphs and screen rows.
48 NEED_VERY_LITTLE_REFRESH = 2,
54 // the word around the cursor, only if the cursor is
57 // the word around the cursor
59 /// the word begining from the cursor position
61 /// the word around the cursor or before the cursor
63 /// the next word (not yet used)
68 LyXText(BufferView *);
75 void init(BufferView *, bool reinit = false);
77 mutable int number_of_rows;
81 mutable unsigned int width;
82 /// the current font settings
83 mutable LyXFont current_font;
85 mutable LyXFont real_current_font;
86 /// first visible pixel-row is set from LyXScreen!!!
87 // unsigned is wrong here for text-insets!
90 BufferView * bv_owner;
92 InsetText * inset_owner;
94 UpdatableInset * the_locking_inset;
97 int getRealCursorX(BufferView *) const;
99 LyXFont const getFont(Buffer const *, Paragraph * par,
100 lyx::pos_type pos) const;
102 LyXFont const getLayoutFont(Buffer const *, Paragraph * par) const;
104 LyXFont const getLabelFont(Buffer const *, Paragraph * par) const;
106 void setCharFont(Buffer const *, Paragraph * par,
107 lyx::pos_type pos, LyXFont const & font);
108 void setCharFont(BufferView *, Paragraph * par,
109 lyx::pos_type pos, LyXFont const & font, bool toggleall);
110 /// returns a pointer to the very first Paragraph
111 Paragraph * firstParagraph() const;
113 /// what you expect when pressing <enter> at cursor position
114 void breakParagraph(BufferView *, char keep_layout = 0);
116 /** set layout over selection and make a total rebreak of
119 Paragraph * setLayout(BufferView *, LyXCursor & actual_cursor,
120 LyXCursor & selection_start,
121 LyXCursor & selection_end,
122 lyx::layout_type layout);
124 void setLayout(BufferView *, lyx::layout_type layout);
126 /// used in setlayout
127 void makeFontEntriesLayoutSpecific(Buffer const *, Paragraph * par);
129 /** increment depth over selection and make a total rebreak of those
132 void incDepth(BufferView *);
134 /** decrement depth over selection and make a total rebreak of those
136 void decDepth(BufferView *);
138 /** Get the depth at current cursor position
140 int getDepth() const;
142 /** set font over selection and make a total rebreak of those
144 toggleall defaults to false.
146 void setFont(BufferView *, LyXFont const &, bool toggleall = false);
148 /** deletes and inserts again all paragaphs between the cursor
149 and the specified par. The Cursor is needed to set the refreshing
151 This function is needed after SetLayout and SetFont etc.
153 void redoParagraphs(BufferView *, LyXCursor const & cursor,
154 Paragraph const * end_par) const;
156 void redoParagraph(BufferView *) const;
159 void toggleFree(BufferView *, LyXFont const &, bool toggleall = false);
162 string getStringToIndex(BufferView *);
164 /** recalculates the heights of all previous rows of the
165 specified paragraph. needed, if the last characters font
168 void redoHeightOfParagraph(BufferView *, LyXCursor const & cursor);
170 /** forces the redrawing of a paragraph. Needed when manipulating a
173 void redoDrawingOfParagraph(BufferView *, LyXCursor const & cursor);
175 /** insert a character, moves all the following breaks in the
176 same Paragraph one to the right and make a little rebreak
178 void insertChar(BufferView *, char c);
180 void insertInset(BufferView *, Inset * inset);
182 /** Completes the insertion with a full rebreak.
183 Returns true if something was broken. */
184 bool fullRebreak(BufferView *);
187 mutable Row * need_break_row;
189 mutable int refresh_y;
197 mutable Row * refresh_row;
199 lyx::pos_type refresh_pos;
201 /// give and set the LyXText status
202 text_status status() const;
204 void status(BufferView *, text_status) const;
207 /** wether the screen needs a refresh,
208 starting with refresh_y
210 mutable text_status status_;
213 /** returns a pointer to the row near the specified y-coordinate
214 (relative to the whole text). y is set to the real beginning
217 Row * getRowNearY(int & y) const;
219 /** returns the column near the specified x-coordinate of the row
220 x is set to the real beginning of this column
222 lyx::pos_type getColumnNearX(BufferView *, Row * row,
223 int & x, bool & boundary) const;
225 /** returns a pointer to a specified row. y is set to the beginning
228 Row * getRow(Paragraph * par,
229 lyx::pos_type pos, int & y) const;
231 /** returns the height of a default row, needed for scrollbar
233 int defaultHeight() const;
236 Later this variable has to be removed. There should be now internal
237 cursor in a text (and thus not in a buffer). By keeping this it is
238 (I think) impossible to have several views with the same buffer, but
239 the cursor placed at different places.
241 Since the LyXText now has been moved from Buffer to BufferView
242 it should not be absolutely needed to move the cursor...
244 mutable LyXCursor cursor; // actual cursor position
246 /** The structure that keeps track of the selections set. */
249 : set_(false), mark_(false)
263 LyXCursor cursor; // temporary cursor to hold a cursor position
264 // until setSelection is called!
265 LyXCursor start; // start of a REAL selection
266 LyXCursor end; // end of a REAL selection
268 bool set_; // former selection
269 bool mark_; // former mark_set
272 mutable Selection selection;
274 /// needed for the toggling (cursor position on last selection made)
275 mutable LyXCursor last_sel_cursor;
276 /// needed for toggling the selection in screen.C
277 mutable LyXCursor toggle_cursor;
278 /// needed for toggling the selection in screen.C
279 mutable LyXCursor toggle_end_cursor;
281 /// need the selection cursor:
282 void setSelection(BufferView *);
284 void clearSelection() const;
286 string const selectionAsString(Buffer const *, bool label) const;
288 /// select the word we need depending on word_location
289 void getWord(LyXCursor & from, LyXCursor & to,
290 word_location const) const;
291 /// just selects the word the cursor is in
292 void selectWord(BufferView *, word_location const);
293 /// returns the inset at cursor (if it exists), 0 otherwise
294 Inset * getInset() const;
296 /** 'selects" the next word, where the cursor is not in
297 and returns this word as string. THe cursor will be moved
298 to the beginning of this word.
299 With SelectSelectedWord can this be highlighted really
301 string const selectNextWordToSpellcheck(BufferView *, float & value) const;
303 void selectSelectedWord(BufferView *);
305 void setCursor(BufferView *, Paragraph * par,
308 bool boundary = false) const;
310 void setCursor(BufferView *, LyXCursor &, Paragraph * par,
312 bool boundary = false) const;
314 void setCursorIntern(BufferView *, Paragraph * par,
317 bool boundary = false) const;
319 void setCurrentFont(BufferView *) const;
322 bool isBoundary(Buffer const *, Paragraph * par,
323 lyx::pos_type pos) const;
325 bool isBoundary(Buffer const *, Paragraph * par,
327 LyXFont const & font) const;
330 void setCursorFromCoordinates(BufferView *, int x, int y) const;
332 void setCursorFromCoordinates(BufferView *, LyXCursor &,
335 void cursorUp(BufferView *) const;
337 void cursorDown(BufferView *) const;
339 void cursorLeft(BufferView *, bool internal = true) const;
341 void cursorRight(BufferView *, bool internal = true) const;
343 void cursorLeftOneWord(BufferView *) const;
345 void cursorLeftOneWord(LyXCursor &) const;
347 void cursorRightOneWord(BufferView *) const;
349 void cursorUpParagraph(BufferView *) const;
351 void cursorDownParagraph(BufferView *) const;
353 void cursorHome(BufferView *) const;
355 void cursorEnd(BufferView *) const;
357 void cursorTab(BufferView *) const;
359 void cursorTop(BufferView *) const;
361 void cursorBottom(BufferView *) const;
363 void Delete(BufferView *);
365 void backspace(BufferView *);
367 void deleteWordForward(BufferView *);
369 void deleteWordBackward(BufferView *);
371 void deleteLineForward(BufferView *);
373 bool selectWordWhenUnderCursor(BufferView *,
374 word_location const);
380 text_capitalization = 1,
384 /// Change the case of the word at cursor position.
385 void changeCase(BufferView *, TextCase action);
387 void changeRegionCase(BufferView * bview,
388 LyXCursor const & from,
389 LyXCursor const & to,
390 LyXText::TextCase action);
392 void transposeChars(BufferView &);
394 /** returns a printed row in a pixmap. The y value is needed to
395 decide, wether it is selected text or not. This is a strange
398 void getVisibleRow(BufferView *, int y_offset, int x_offset,
399 Row * row_ptr, int y, bool cleared=false);
402 void toggleInset(BufferView *);
404 void cutSelection(BufferView *, bool doclear = true, bool realcut = true);
406 void copySelection(BufferView *);
408 void pasteSelection(BufferView *);
410 void copyEnvironmentType();
412 void pasteEnvironmentType(BufferView *);
414 /** the DTP switches for paragraphs. LyX will store the top settings
415 always in the first physical paragraph, the bottom settings in the
416 last. When a paragraph is broken, the top settings rest, the bottom
417 settings are given to the new one. So I can make shure, they do not
418 duplicate themself (and you cannnot make dirty things with them! )
420 void setParagraph(BufferView *,
421 bool line_top, bool line_bottom,
422 bool pagebreak_top, bool pagebreak_bottom,
423 VSpace const & space_top,
424 VSpace const & space_bottom,
425 Spacing const & spacing,
427 string labelwidthstring,
430 /* these things are for search and replace */
432 /** sets the selection over the number of characters of string,
435 void setSelectionOverString(BufferView *, string const & str);
437 /** simple replacing. The font of the first selected character
440 void replaceSelectionWithString(BufferView *, string const & str);
442 /// needed to insert the selection
443 void insertStringAsLines(BufferView *, string const & str);
444 /// needed to insert the selection
445 void insertStringAsParagraphs(BufferView *, string const & str);
447 /// Find next inset of some specified type.
448 bool gotoNextInset(BufferView *, std::vector<Inset::Code> const & codes,
449 string const & contents = string()) const;
452 /* for the greater insets */
454 /// returns false if inset wasn't found
455 bool updateInset(BufferView *, Inset *);
457 void checkParagraph(BufferView *, Paragraph * par, lyx::pos_type pos);
459 int numberOfCell(Paragraph * par, lyx::pos_type pos) const;
461 void removeTableRow(LyXCursor & cursor) const;
463 bool isEmptyTableCell() const;
465 void toggleAppendix(BufferView *);
467 int workWidth(BufferView *) const;
469 int workWidth(BufferView *, Inset * inset) const;
471 void computeBidiTables(Buffer const *, Row * row) const;
473 /// Maps positions in the visual string to positions in logical string.
475 lyx::pos_type log2vis(lyx::pos_type pos) const {
476 if (bidi_start == -1)
479 return log2vis_list[pos-bidi_start];
482 /// Maps positions in the logical string to positions in visual string.
484 lyx::pos_type vis2log(lyx::pos_type pos) const {
485 if (bidi_start == -1)
488 return vis2log_list[pos-bidi_start];
492 lyx::pos_type bidi_level(lyx::pos_type pos) const {
493 if (bidi_start == -1)
496 return bidi_levels[pos-bidi_start];
500 bool bidi_InRange(lyx::pos_type pos) const {
501 return bidi_start == -1 ||
502 (bidi_start <= pos && pos <= bidi_end);
506 mutable Row * firstrow;
508 mutable Row * lastrow;
510 /** Copybuffer for copy environment type.
511 Asger has learned that this should be a buffer-property instead
512 Lgb has learned that 'char' is a lousy type for non-characters
514 lyx::layout_type copylayouttype;
516 /** inserts a new row behind the specified row, increments
517 the touched counters */
518 void insertRow(Row * row, Paragraph * par, lyx::pos_type pos) const;
519 /** removes the row and reset the touched counters */
520 void removeRow(Row * row) const;
522 /** remove all following rows of the paragraph of the specified row. */
523 void removeParagraph(Row * row) const;
525 /** insert the specified paragraph behind the specified row */
526 void insertParagraph(BufferView *,
527 Paragraph * par, Row * row) const;
529 /** appends the implizit specified paragraph behind the specified row,
530 * start at the implizit given position */
531 void appendParagraph(BufferView *, Row * row) const;
534 void breakAgain(BufferView *, Row * row) const;
536 void breakAgainOneRow(BufferView *, Row * row);
537 /// Calculate and set the height of the row
538 void setHeightOfRow(BufferView *, Row * row_ptr) const;
540 /** this calculates the specified parameters. needed when setting
541 * the cursor and when creating a visible row */
542 void prepareToPrint(BufferView *, Row * row, float & x,
543 float & fill_separator,
545 float & fill_label_hfill,
546 bool bidi = true) const;
548 /// A struct used for drawing routines
549 struct DrawRowParams {
554 // the painter to use
556 // has the background been cleared
566 /// the inset/view full width
572 /// fill separator size
576 /// paint the background
577 bool paintRowBackground(DrawRowParams & p);
579 /// paint the selection background
580 void paintRowSelection(DrawRowParams & p);
582 /// paint appendix marker
583 void paintRowAppendix(DrawRowParams & p);
585 /// paint env depth bar
586 void paintRowDepthBar(DrawRowParams & p);
588 /// paint a first row in a paragraph
589 void paintFirstRow(DrawRowParams & p);
591 /// paint a last row in a paragraph
592 void paintLastRow(DrawRowParams & p);
595 void paintRowText(DrawRowParams & p);
597 // fix the cursor `cur' after a characters has been deleted at `where'
598 // position. Called by deleteEmptyParagraphMechanism
599 void fixCursorAfterDelete(BufferView * bview,
601 LyXCursor const & where) const;
604 void deleteEmptyParagraphMechanism(BufferView *,
605 LyXCursor const & old_cursor) const;
608 /** Updates all counters starting BEHIND the row. Changed paragraphs
609 * with a dynamic left margin will be rebroken. */
610 void updateCounters(BufferView *, Row * row) const;
613 void setCounter(Buffer const *, Paragraph * par) const;
616 * some low level functions
620 int singleWidth(BufferView *, Paragraph * par,
621 lyx::pos_type pos) const;
623 int singleWidth(BufferView *, Paragraph * par,
624 lyx::pos_type pos, char c) const;
626 void draw(BufferView *, Row const * row,
627 lyx::pos_type & pos, int offset, float & x, bool cleared);
629 /// get the next breakpoint in a given paragraph
630 lyx::pos_type nextBreakPoint(BufferView *, Row const * row, int width) const;
631 /// returns the minimum space a row needs on the screen in pixel
632 int fill(BufferView *, Row * row, int workwidth) const;
634 /** returns the minimum space a manual label needs on the
636 int labelFill(BufferView *, Row const * row) const;
639 lyx::pos_type beginningOfMainBody(Buffer const *, Paragraph const * par) const;
641 /** Returns the left beginning of the text.
642 This information cannot be taken from the layouts-objekt, because
643 in LaTeX the beginning of the text fits in some cases
644 (for example sections) exactly the label-width.
646 int leftMargin(BufferView *, Row const * row) const;
648 int rightMargin(Buffer const *, Row const * row) const;
650 int labelEnd (BufferView *, Row const * row) const;
652 /** returns the number of separators in the specified row.
653 The separator on the very last column doesnt count
655 int numberOfSeparators(Buffer const *, Row const * row) const;
657 /** returns the number of hfills in the specified row. The
658 LyX-Hfill is a LaTeX \hfill so that the hfills at the
659 beginning and at the end were ignored. This is {\em MUCH}
660 more usefull than not to ignore!
662 int numberOfHfills(Buffer const *, Row const * row) const;
664 /// like NumberOfHfills, but only those in the manual label!
665 int numberOfLabelHfills(Buffer const *, Row const * row) const;
666 /** returns true, if a expansion is needed. Rules are given by
669 bool hfillExpansion(Buffer const *, Row const * row_ptr,
670 lyx::pos_type pos) const;
672 LColor::color backgroundColor();
676 mutable std::vector<lyx::pos_type> log2vis_list;
679 mutable std::vector<lyx::pos_type> vis2log_list;
682 mutable std::vector<lyx::pos_type> bidi_levels;
685 mutable lyx::pos_type bidi_start;
688 mutable lyx::pos_type bidi_end;
691 mutable bool bidi_same_direction;
694 unsigned char transformChar(unsigned char c, Paragraph * par,
695 lyx::pos_type pos) const;
697 /** returns the paragraph position of the last character in the
700 lyx::pos_type rowLast(Row const * row) const;
702 lyx::pos_type rowLastPrintable(Row const * row) const;
708 // special owner functions
710 Paragraph * ownerParagraph() const;
712 void ownerParagraph(Paragraph *) const;
713 // set it searching first for the right owner using the paragraph id
714 void ownerParagraph(int id, Paragraph *) const;