4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
8 * \author Lars Gullik Bjønnes
11 * Full author contact details are available in file CREDITS
18 #include "lyxcursor.h"
21 #include "insets/inset.h"
23 #include "bufferview_funcs.h"
24 #include "textcursor.h"
40 This class holds the mapping between buffer paragraphs and screen rows.
43 // The inheritance from TextCursor should go. It's just there to ease
45 class LyXText : public TextCursor {
47 /// what repainting is needed
49 /// no repaint is needed
51 /// the refresh_row needs repainting
53 /// everything from refresh_y downwards needs repainting
58 LyXText(BufferView *);
59 /// sets inset as owner
60 LyXText(BufferView *, InsetText *);
62 void init(BufferView *, bool reinit = false);
67 /// the current font settings
70 LyXFont real_current_font;
72 /** the 'anchor' row: the position of this row remains constant
73 * with respect to the top of the screen
75 RowList::iterator anchor_row_;
76 /** the pixel offset with respect to this row of top_y
78 int anchor_row_offset_;
80 /// update all cached row positions
81 void updateRowPositions();
82 /// get the y coord. of the top of the screen (relative to doc start)
84 /// set the y coord. of the top of the screen (relative to doc start)
86 /// set the anchoring row. top_y will be computed relative to this
87 void anchor_row(RowList::iterator rit);
89 InsetText * inset_owner;
91 UpdatableInset * the_locking_inset;
94 int getRealCursorX() const;
96 LyXFont const getFont(Buffer const *, ParagraphList::iterator pit,
97 lyx::pos_type pos) const;
99 LyXFont const getLayoutFont(Buffer const *,
100 ParagraphList::iterator pit) const;
102 LyXFont const getLabelFont(Buffer const *,
103 ParagraphList::iterator pit) const;
105 void setCharFont(Buffer const *, ParagraphList::iterator pit,
106 lyx::pos_type pos, LyXFont const & font);
107 void setCharFont(ParagraphList::iterator pit,
109 LyXFont const & font, bool toggleall);
112 void breakAgainOneRow(RowList::iterator rit);
113 /// what you expect when pressing <enter> at cursor position
114 void breakParagraph(ParagraphList & paragraphs, char keep_layout = 0);
116 /** set layout over selection and make a total rebreak of
119 ParagraphList::iterator
120 setLayout(LyXCursor & actual_cursor,
121 LyXCursor & selection_start,
122 LyXCursor & selection_end,
123 string const & layout);
125 void setLayout(string const & layout);
128 * Increase or decrease the nesting depth of the selected paragraph(s)
129 * if test_only, don't change any depths. Returns whether something
130 * (would have) changed
132 bool changeDepth(bv_funcs::DEPTH_CHANGE type, bool test_only);
134 /// get the depth at current cursor position
135 int getDepth() const;
137 /** set font over selection and make a total rebreak of those
139 toggleall defaults to false.
141 void setFont(LyXFont const &, bool toggleall = false);
143 /** deletes and inserts again all paragaphs between the cursor
144 and the specified par. The Cursor is needed to set the refreshing
146 This function is needed after SetLayout and SetFont etc.
148 void redoParagraphs(LyXCursor const & cursor,
149 ParagraphList::iterator endpit);
151 void redoParagraph();
154 void toggleFree(LyXFont const &, bool toggleall = false);
157 string getStringToIndex();
159 /** recalculates the heights of all previous rows of the
160 specified paragraph. needed, if the last characters font
163 void redoHeightOfParagraph();
165 /** insert a character, moves all the following breaks in the
166 same Paragraph one to the right and make a little rebreak
168 void insertChar(char c);
170 void insertInset(Inset * inset);
172 /// Completes the insertion with a rebreak from 'need_break_row' on
173 void partialRebreak();
174 /// a full rebreak of the whole text
178 RowList::iterator need_break_row;
180 /// clear any pending paints
184 * Mark position y as the starting point for a repaint
186 void postPaint(int start_y);
189 * Mark the given row at position y as needing a repaint.
191 void postRowPaint(RowList::iterator rit, int start_y);
194 Inset::RESULT dispatch(FuncRequest const & cmd);
198 BufferView * bv() const;
200 friend class LyXScreen;
203 * Return the status. This represents what repaints are
204 * pending after some operation (e.g. inserting a char).
206 refresh_status refreshStatus() const;
210 * The pixel y position from which to repaint the screen.
211 * The position is absolute along the height of outermost
212 * lyxtext (I think). REFRESH_AREA and REFRESH_ROW
213 * repaints both use this as a starting point (if it's within
214 * the viewable portion of the lyxtext).
218 * The row from which to repaint the screen, used by screen.c.
219 * This must be set if the pending update is REFRESH_ROW.
220 * It doesn't make any difference for REFRESH_AREA.
222 RowList::iterator refresh_row;
224 refresh_status refresh_status_;
226 /// only the top-level LyXText has this non-zero
227 BufferView * bv_owner;
229 /// returns a pointer to a specified row.
231 getRow(ParagraphList::iterator pit, lyx::pos_type pos) const;
233 /// returns a pointer cursor row
234 RowList::iterator getRow(LyXCursor const & cursor) const;
236 RowList::iterator cursorRow() const;
238 /** returns a pointer to the row near the specified y-coordinate
239 (relative to the whole text). y is set to the real beginning
242 RowList::iterator getRowNearY(int & y) const;
244 /** returns the column near the specified x-coordinate of the row
245 x is set to the real beginning of this column
247 lyx::pos_type getColumnNearX(RowList::iterator rit,
248 int & x, bool & boundary) const;
250 /** returns a pointer to a specified row. y is set to the beginning
254 getRow(ParagraphList::iterator pit, lyx::pos_type pos, int & y) const;
256 RowList & rows() const {
261 /// need the selection cursor:
264 void clearSelection();
266 /// select the word we need depending on word_location
267 void getWord(LyXCursor & from, LyXCursor & to, lyx::word_location const);
268 /// just selects the word the cursor is in
269 void selectWord(lyx::word_location loc);
270 /// returns the inset at cursor (if it exists), 0 otherwise
271 Inset * getInset() const;
273 /// accept selected change
276 /// reject selected change
279 /** 'selects" the next word, where the cursor is not in
280 and returns this word as string. THe cursor will be moved
281 to the beginning of this word.
282 With SelectSelectedWord can this be highlighted really
284 WordLangTuple const selectNextWordToSpellcheck(float & value);
286 void selectSelectedWord();
287 /// returns true if par was empty and was removed
288 bool setCursor(ParagraphList::iterator pit,
291 bool boundary = false);
293 void setCursor(LyXCursor &, ParagraphList::iterator pit,
295 bool boundary = false);
297 void setCursorIntern(ParagraphList::iterator pit,
300 bool boundary = false);
302 void setCurrentFont();
305 bool isBoundary(Buffer const *, Paragraph const & par,
306 lyx::pos_type pos) const;
308 bool isBoundary(Buffer const *, Paragraph const & par,
310 LyXFont const & font) const;
313 void setCursorFromCoordinates(int x, int y);
315 void setCursorFromCoordinates(LyXCursor &,
318 void cursorUp(bool selecting = false);
320 void cursorDown(bool selecting = false);
322 void cursorLeft(bool internal = true);
324 void cursorRight(bool internal = true);
326 void cursorLeftOneWord();
328 void cursorRightOneWord();
330 void cursorUpParagraph();
332 void cursorDownParagraph();
338 void cursorPrevious();
350 bool selectWordWhenUnderCursor(lyx::word_location);
356 text_capitalization = 1,
360 /// Change the case of the word at cursor position.
361 void changeCase(TextCase action);
366 void cutSelection(bool doclear = true, bool realcut = true);
368 void copySelection();
370 void pasteSelection(size_t sel_index = 0);
372 /** the DTP switches for paragraphs. LyX will store the top settings
373 always in the first physical paragraph, the bottom settings in the
374 last. When a paragraph is broken, the top settings rest, the bottom
375 settings are given to the new one. So I can make shure, they do not
376 duplicate themself (and you cannnot make dirty things with them! )
378 void setParagraph(bool line_top, bool line_bottom,
379 bool pagebreak_top, bool pagebreak_bottom,
380 VSpace const & space_top,
381 VSpace const & space_bottom,
382 Spacing const & spacing,
384 string const & labelwidthstring,
387 /* these things are for search and replace */
390 * Sets the selection from the current cursor position to length
391 * characters to the right. No safety checks.
393 void setSelectionRange(lyx::pos_type length);
395 /** simple replacing. The font of the first selected character
398 void replaceSelectionWithString(string const & str);
400 /// needed to insert the selection
401 void insertStringAsLines(string const & str);
402 /// needed to insert the selection
403 void insertStringAsParagraphs(string const & str);
405 /// Find next inset of some specified type.
406 bool gotoNextInset(std::vector<Inset::Code> const & codes,
407 string const & contents = string());
409 void gotoInset(std::vector<Inset::Code> const & codes,
412 void gotoInset(Inset::Code code, bool same_content);
415 /* for the greater insets */
417 /// returns false if inset wasn't found
418 bool updateInset(Inset *);
420 void checkParagraph(ParagraphList::iterator pit, lyx::pos_type pos);
422 int workWidth() const;
423 /// returns width of row containing inset
424 int workWidth(Inset const * inset) const;
427 void computeBidiTables(Buffer const *, RowList::iterator row) const;
428 /// Maps positions in the visual string to positions in logical string.
429 lyx::pos_type log2vis(lyx::pos_type pos) const;
430 /// Maps positions in the logical string to positions in visual string.
431 lyx::pos_type vis2log(lyx::pos_type pos) const;
433 lyx::pos_type bidi_level(lyx::pos_type pos) const;
435 bool bidi_InRange(lyx::pos_type pos) const;
438 mutable RowList rowlist_;
441 float getCursorX(RowList::iterator rit, lyx::pos_type pos,
442 lyx::pos_type last, bool boundary) const;
443 /// used in setlayout
444 void makeFontEntriesLayoutSpecific(Buffer const &, Paragraph & par);
446 /** forces the redrawing of a paragraph. Needed when manipulating a
449 void redoDrawingOfParagraph(LyXCursor const & cursor);
451 /// removes the row and reset the touched counters
452 void removeRow(RowList::iterator rit);
454 /// remove all following rows of the paragraph of the specified row.
455 void removeParagraph(RowList::iterator rit);
457 /// insert the specified paragraph behind the specified row
458 void insertParagraph(ParagraphList::iterator pit,
459 RowList::iterator rowit);
461 /** appends the implizit specified paragraph behind the specified row,
462 * start at the implizit given position */
463 void appendParagraph(RowList::iterator rowit);
466 void breakAgain(RowList::iterator rit);
467 /// Calculate and set the height of the row
468 void setHeightOfRow(RowList::iterator rit);
470 // fix the cursor `cur' after a characters has been deleted at `where'
471 // position. Called by deleteEmptyParagraphMechanism
472 void fixCursorAfterDelete(LyXCursor & cur,
473 LyXCursor const & where);
475 /// delete double space (false) or empty paragraphs (true) around old_cursor
476 bool deleteEmptyParagraphMechanism(LyXCursor const & old_cursor);
479 /** Updates all counters starting BEHIND the row. Changed paragraphs
480 * with a dynamic left margin will be rebroken. */
481 void updateCounters();
485 * Returns an inset if inset was hit, or 0 if not.
486 * If hit, the coordinates are changed relative to the inset.
488 Inset * checkInsetHit(int & x, int & y);
491 int singleWidth(ParagraphList::iterator pit,
492 lyx::pos_type pos) const;
494 int singleWidth(ParagraphList::iterator pit,
495 lyx::pos_type pos, char c) const;
497 /// return the color of the canvas
498 LColor::color backgroundColor() const;
501 mutable bool bidi_same_direction;
503 unsigned char transformChar(unsigned char c, Paragraph const & par,
504 lyx::pos_type pos) const;
507 * Returns the left beginning of the text.
508 * This information cannot be taken from the layout object, because
509 * in LaTeX the beginning of the text fits in some cases
510 * (for example sections) exactly the label-width.
512 int leftMargin(Row const & row) const;
514 int rightMargin(Buffer const &, Row const & row) const;
516 /** this calculates the specified parameters. needed when setting
517 * the cursor and when creating a visible row */
518 void prepareToPrint(RowList::iterator row, float & x,
519 float & fill_separator,
521 float & fill_label_hfill,
522 bool bidi = true) const;
526 void setCounter(Buffer const *, ParagraphList::iterator pit);
528 void deleteWordForward();
530 void deleteWordBackward();
532 void deleteLineForward();
535 * some low level functions
539 /// return the pos value *before* which a row should break.
540 /// for example, the pos at which IsNewLine(pos) == true
541 lyx::pos_type rowBreakPoint(Row const & row) const;
543 /// returns the minimum space a row needs on the screen in pixel
544 int fill(RowList::iterator row, int workwidth) const;
547 * returns the minimum space a manual label needs on the
550 int labelFill(Row const & row) const;
553 int labelEnd(Row const & row) const;
556 mutable std::vector<lyx::pos_type> log2vis_list;
558 mutable std::vector<lyx::pos_type> vis2log_list;
560 mutable std::vector<lyx::pos_type> bidi_levels;
562 mutable lyx::pos_type bidi_start;
564 mutable lyx::pos_type bidi_end;
570 // special owner functions
572 ParagraphList & ownerParagraphs() const;
574 /// return true if this is owned by an inset.
575 bool isInInset() const;
578 /** Cursor related data.
579 Later this variable has to be removed. There should be now internal
582 ///TextCursor cursor_;
585 /// return the default height of a row in pixels, considering font zoom
586 extern int defaultRowHeight();