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"
39 This class holds the mapping between buffer paragraphs and screen rows.
43 /// what repainting is needed
45 /// no repaint is needed
47 /// the refresh_row needs repainting
49 /// everything from refresh_y downwards needs repainting
55 // the word around the cursor, only if the cursor is
58 // the word around the cursor
60 /// the word begining from the cursor position
62 /// the word around the cursor or before the cursor
64 /// the next word (not yet used)
69 LyXText(BufferView *);
70 /// sets inset as owner
71 LyXText(BufferView *, InsetText *);
73 void init(BufferView *, bool reinit = false);
78 /// the current font settings
81 LyXFont real_current_font;
83 /** the 'anchor' row: the position of this row remains constant
84 * with respect to the top of the screen
86 RowList::iterator anchor_row_;
87 /** the pixel offset with respect to this row of top_y
89 int anchor_row_offset_;
91 /// update all cached row positions
92 void updateRowPositions();
93 /// get the y coord. of the top of the screen (relative to doc start)
95 /// set the y coord. of the top of the screen (relative to doc start)
97 /// set the anchoring row. top_y will be computed relative to this
98 void anchor_row(RowList::iterator rit);
100 InsetText * inset_owner;
102 UpdatableInset * the_locking_inset;
105 int getRealCursorX() const;
107 LyXFont const getFont(Buffer const *, ParagraphList::iterator pit,
108 lyx::pos_type pos) const;
110 LyXFont const getLayoutFont(Buffer const *,
111 ParagraphList::iterator pit) const;
113 LyXFont const getLabelFont(Buffer const *,
114 ParagraphList::iterator pit) const;
116 void setCharFont(Buffer const *, ParagraphList::iterator pit,
117 lyx::pos_type pos, LyXFont const & font);
118 void setCharFont(ParagraphList::iterator pit,
120 LyXFont const & font, bool toggleall);
123 void breakAgainOneRow(RowList::iterator rit);
124 /// what you expect when pressing <enter> at cursor position
125 void breakParagraph(ParagraphList & paragraphs, char keep_layout = 0);
127 /** set layout over selection and make a total rebreak of
130 ParagraphList::iterator
131 setLayout(LyXCursor & actual_cursor,
132 LyXCursor & selection_start,
133 LyXCursor & selection_end,
134 string const & layout);
136 void setLayout(string const & layout);
139 * Increase or decrease the nesting depth of the selected paragraph(s)
140 * if test_only, don't change any depths. Returns whether something
141 * (would have) changed
143 bool changeDepth(bv_funcs::DEPTH_CHANGE type, bool test_only);
145 /// get the depth at current cursor position
146 int getDepth() const;
148 /** set font over selection and make a total rebreak of those
150 toggleall defaults to false.
152 void setFont(LyXFont const &, bool toggleall = false);
154 /** deletes and inserts again all paragaphs between the cursor
155 and the specified par. The Cursor is needed to set the refreshing
157 This function is needed after SetLayout and SetFont etc.
159 void redoParagraphs(LyXCursor const & cursor,
160 ParagraphList::iterator endpit);
162 void redoParagraph();
165 void toggleFree(LyXFont const &, bool toggleall = false);
168 string getStringToIndex();
170 /** recalculates the heights of all previous rows of the
171 specified paragraph. needed, if the last characters font
174 void redoHeightOfParagraph();
176 /** insert a character, moves all the following breaks in the
177 same Paragraph one to the right and make a little rebreak
179 void insertChar(char c);
181 void insertInset(Inset * inset);
183 /// Completes the insertion with a rebreak from 'need_break_row' on
184 void partialRebreak();
185 /// a full rebreak of the whole text
189 RowList::iterator need_break_row;
191 /// clear any pending paints
195 * Mark position y as the starting point for a repaint
197 void postPaint(int start_y);
200 * Mark the given row at position y as needing a repaint.
202 void postRowPaint(RowList::iterator rit, int start_y);
205 Inset::RESULT dispatch(FuncRequest const & cmd);
209 BufferView * bv() const;
211 friend class LyXScreen;
214 * Return the status. This represents what repaints are
215 * pending after some operation (e.g. inserting a char).
217 refresh_status refreshStatus() const;
221 * The pixel y position from which to repaint the screen.
222 * The position is absolute along the height of outermost
223 * lyxtext (I think). REFRESH_AREA and REFRESH_ROW
224 * repaints both use this as a starting point (if it's within
225 * the viewable portion of the lyxtext).
229 * The row from which to repaint the screen, used by screen.c.
230 * This must be set if the pending update is REFRESH_ROW.
231 * It doesn't make any difference for REFRESH_AREA.
233 RowList::iterator refresh_row;
235 refresh_status refresh_status_;
237 /// only the top-level LyXText has this non-zero
238 BufferView * bv_owner;
240 /// returns a pointer to a specified row.
242 getRow(ParagraphList::iterator pit, lyx::pos_type pos) const;
244 /// returns a pointer cursor row
245 RowList::iterator getRow(LyXCursor const & cursor) const;
247 RowList::iterator cursorRow() const;
249 /** returns a pointer to the row near the specified y-coordinate
250 (relative to the whole text). y is set to the real beginning
253 RowList::iterator getRowNearY(int & y) const;
255 /** returns the column near the specified x-coordinate of the row
256 x is set to the real beginning of this column
258 lyx::pos_type getColumnNearX(RowList::iterator rit,
259 int & x, bool & boundary) const;
261 /** returns a pointer to a specified row. y is set to the beginning
265 getRow(ParagraphList::iterator pit, lyx::pos_type pos, int & y) const;
267 RowList & rows() const {
272 Later this variable has to be removed. There should be now internal
273 cursor in a text (and thus not in a buffer). By keeping this it is
274 (I think) impossible to have several views with the same buffer, but
275 the cursor placed at different places.
277 Since the LyXText now has been moved from Buffer to BufferView
278 it should not be absolutely needed to move the cursor...
280 LyXCursor cursor; // actual cursor position
282 /** The structure that keeps track of the selections set. */
285 : set_(false), mark_(false)
299 LyXCursor cursor; // temporary cursor to hold a cursor position
300 // until setSelection is called!
301 LyXCursor start; // start of a REAL selection
302 LyXCursor end; // end of a REAL selection
304 bool set_; // former selection
305 bool mark_; // former mark_set
309 // this is used to handle XSelection events in the right manner
310 Selection xsel_cache;
312 /// needed for the toggling (cursor position on last selection made)
313 LyXCursor last_sel_cursor;
314 /// needed for toggling the selection in screen.C
315 LyXCursor toggle_cursor;
316 /// needed for toggling the selection in screen.C
317 LyXCursor toggle_end_cursor;
319 /// need the selection cursor:
322 void clearSelection();
324 string const selectionAsString(Buffer const *, bool label) const;
326 /// select the word we need depending on word_location
327 void getWord(LyXCursor & from, LyXCursor & to,
328 word_location const);
329 /// just selects the word the cursor is in
330 void selectWord(word_location const);
331 /// returns the inset at cursor (if it exists), 0 otherwise
332 Inset * getInset() const;
334 /// accept selected change
337 /// reject selected change
340 /** 'selects" the next word, where the cursor is not in
341 and returns this word as string. THe cursor will be moved
342 to the beginning of this word.
343 With SelectSelectedWord can this be highlighted really
345 WordLangTuple const selectNextWordToSpellcheck(float & value);
347 void selectSelectedWord();
348 /// returns true if par was empty and was removed
349 bool setCursor(ParagraphList::iterator pit,
352 bool boundary = false);
354 void setCursor(LyXCursor &, ParagraphList::iterator pit,
356 bool boundary = false);
358 void setCursorIntern(ParagraphList::iterator pit,
361 bool boundary = false);
363 void setCurrentFont();
366 bool isBoundary(Buffer const *, Paragraph const & par,
367 lyx::pos_type pos) const;
369 bool isBoundary(Buffer const *, Paragraph const & par,
371 LyXFont const & font) const;
374 void setCursorFromCoordinates(int x, int y);
376 void setCursorFromCoordinates(LyXCursor &,
379 void cursorUp(bool selecting = false);
381 void cursorDown(bool selecting = false);
383 void cursorLeft(bool internal = true);
385 void cursorRight(bool internal = true);
387 void cursorLeftOneWord();
389 void cursorRightOneWord();
391 void cursorUpParagraph();
393 void cursorDownParagraph();
399 void cursorPrevious();
411 bool selectWordWhenUnderCursor(word_location);
417 text_capitalization = 1,
421 /// Change the case of the word at cursor position.
422 void changeCase(TextCase action);
427 void cutSelection(bool doclear = true, bool realcut = true);
429 void copySelection();
431 void pasteSelection(size_t sel_index = 0);
433 /** the DTP switches for paragraphs. LyX will store the top settings
434 always in the first physical paragraph, the bottom settings in the
435 last. When a paragraph is broken, the top settings rest, the bottom
436 settings are given to the new one. So I can make shure, they do not
437 duplicate themself (and you cannnot make dirty things with them! )
439 void setParagraph(bool line_top, bool line_bottom,
440 bool pagebreak_top, bool pagebreak_bottom,
441 VSpace const & space_top,
442 VSpace const & space_bottom,
443 Spacing const & spacing,
445 string const & labelwidthstring,
448 /* these things are for search and replace */
451 * Sets the selection from the current cursor position to length
452 * characters to the right. No safety checks.
454 void setSelectionRange(lyx::pos_type length);
456 /** simple replacing. The font of the first selected character
459 void replaceSelectionWithString(string const & str);
461 /// needed to insert the selection
462 void insertStringAsLines(string const & str);
463 /// needed to insert the selection
464 void insertStringAsParagraphs(string const & str);
466 /// Find next inset of some specified type.
467 bool gotoNextInset(std::vector<Inset::Code> const & codes,
468 string const & contents = string());
470 void gotoInset(std::vector<Inset::Code> const & codes,
473 void gotoInset(Inset::Code code, bool same_content);
476 /* for the greater insets */
478 /// returns false if inset wasn't found
479 bool updateInset(Inset *);
481 void checkParagraph(ParagraphList::iterator pit, lyx::pos_type pos);
483 int workWidth() const;
484 /// returns width of row containing inset
485 int workWidth(Inset const * inset) const;
488 void computeBidiTables(Buffer const *, RowList::iterator row) const;
489 /// Maps positions in the visual string to positions in logical string.
490 lyx::pos_type log2vis(lyx::pos_type pos) const;
491 /// Maps positions in the logical string to positions in visual string.
492 lyx::pos_type vis2log(lyx::pos_type pos) const;
494 lyx::pos_type bidi_level(lyx::pos_type pos) const;
496 bool bidi_InRange(lyx::pos_type pos) const;
499 mutable RowList rowlist_;
501 void cursorLeftOneWord(LyXCursor &);
504 float getCursorX(RowList::iterator rit, lyx::pos_type pos,
505 lyx::pos_type last, bool boundary) const;
506 /// used in setlayout
507 void makeFontEntriesLayoutSpecific(Buffer const &, Paragraph & par);
509 /** forces the redrawing of a paragraph. Needed when manipulating a
512 void redoDrawingOfParagraph(LyXCursor const & cursor);
514 /// removes the row and reset the touched counters
515 void removeRow(RowList::iterator rit);
517 /// remove all following rows of the paragraph of the specified row.
518 void removeParagraph(RowList::iterator rit);
520 /// insert the specified paragraph behind the specified row
521 void insertParagraph(ParagraphList::iterator pit,
522 RowList::iterator rowit);
524 /** appends the implizit specified paragraph behind the specified row,
525 * start at the implizit given position */
526 void appendParagraph(RowList::iterator rowit);
529 void breakAgain(RowList::iterator rit);
530 /// Calculate and set the height of the row
531 void setHeightOfRow(RowList::iterator rit);
533 // fix the cursor `cur' after a characters has been deleted at `where'
534 // position. Called by deleteEmptyParagraphMechanism
535 void fixCursorAfterDelete(LyXCursor & cur,
536 LyXCursor const & where);
538 /// delete double space (false) or empty paragraphs (true) around old_cursor
539 bool deleteEmptyParagraphMechanism(LyXCursor const & old_cursor);
542 /** Updates all counters starting BEHIND the row. Changed paragraphs
543 * with a dynamic left margin will be rebroken. */
544 void updateCounters();
548 * Returns an inset if inset was hit, or 0 if not.
549 * If hit, the coordinates are changed relative to the inset.
551 Inset * checkInsetHit(int & x, int & y);
554 int singleWidth(ParagraphList::iterator pit,
555 lyx::pos_type pos) const;
557 int singleWidth(ParagraphList::iterator pit,
558 lyx::pos_type pos, char c) const;
560 /// return the color of the canvas
561 LColor::color backgroundColor() const;
564 mutable bool bidi_same_direction;
566 unsigned char transformChar(unsigned char c, Paragraph const & par,
567 lyx::pos_type pos) const;
570 * Returns the left beginning of the text.
571 * This information cannot be taken from the layout object, because
572 * in LaTeX the beginning of the text fits in some cases
573 * (for example sections) exactly the label-width.
575 int leftMargin(Row const & row) const;
577 int rightMargin(Buffer const &, Row const & row) const;
579 /** this calculates the specified parameters. needed when setting
580 * the cursor and when creating a visible row */
581 void prepareToPrint(RowList::iterator row, float & x,
582 float & fill_separator,
584 float & fill_label_hfill,
585 bool bidi = true) const;
589 void setCounter(Buffer const *, ParagraphList::iterator pit);
591 void deleteWordForward();
593 void deleteWordBackward();
595 void deleteLineForward();
598 * some low level functions
602 /// return the pos value *before* which a row should break.
603 /// for example, the pos at which IsNewLine(pos) == true
604 lyx::pos_type rowBreakPoint(Row const & row) const;
606 /// returns the minimum space a row needs on the screen in pixel
607 int fill(RowList::iterator row, int workwidth) const;
610 * returns the minimum space a manual label needs on the
613 int labelFill(Row const & row) const;
616 int labelEnd(Row const & row) const;
619 mutable std::vector<lyx::pos_type> log2vis_list;
621 mutable std::vector<lyx::pos_type> vis2log_list;
623 mutable std::vector<lyx::pos_type> bidi_levels;
625 mutable lyx::pos_type bidi_start;
627 mutable lyx::pos_type bidi_end;
633 // special owner functions
635 ParagraphList & ownerParagraphs() const;
637 /// return true if this is owned by an inset.
638 bool isInInset() const;
641 /// return the default height of a row in pixels, considering font zoom
642 extern int defaultRowHeight();