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 /// get the y coord. of the top of the screen (relative to doc start)
93 /// set the y coord. of the top of the screen (relative to doc start)
95 /// set the anchoring row. top_y will be computed relative to this
96 void anchor_row(RowList::iterator rit);
98 InsetText * inset_owner;
100 UpdatableInset * the_locking_inset;
103 int getRealCursorX() const;
105 LyXFont const getFont(Buffer const *, Paragraph * par,
106 lyx::pos_type pos) const;
108 LyXFont const getLayoutFont(Buffer const *, Paragraph * par) const;
110 LyXFont const getLabelFont(Buffer const *, Paragraph * par) const;
112 void setCharFont(Buffer const *, Paragraph * par,
113 lyx::pos_type pos, LyXFont const & font);
114 void setCharFont(Paragraph * par,
115 lyx::pos_type pos, LyXFont const & font, bool toggleall);
118 void breakAgainOneRow(RowList::iterator rit);
119 /// what you expect when pressing <enter> at cursor position
120 void breakParagraph(ParagraphList & paragraphs, char keep_layout = 0);
122 /** set layout over selection and make a total rebreak of
125 Paragraph * setLayout(LyXCursor & actual_cursor,
126 LyXCursor & selection_start,
127 LyXCursor & selection_end,
128 string const & layout);
130 void setLayout(string const & layout);
133 * Increase or decrease the nesting depth of the selected paragraph(s)
134 * if test_only, don't change any depths. Returns whether something
135 * (would have) changed
137 bool changeDepth(bv_funcs::DEPTH_CHANGE type, bool test_only);
139 /// 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(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(LyXCursor const & cursor,
154 Paragraph const * end_par);
156 void redoParagraph();
159 void toggleFree(LyXFont const &, bool toggleall = false);
162 string getStringToIndex();
164 /** recalculates the heights of all previous rows of the
165 specified paragraph. needed, if the last characters font
168 void redoHeightOfParagraph();
170 /** insert a character, moves all the following breaks in the
171 same Paragraph one to the right and make a little rebreak
173 void insertChar(char c);
175 void insertInset(Inset * inset);
177 /// Completes the insertion with a full rebreak
181 RowList::iterator need_break_row;
183 /// clear any pending paints
187 * Mark position y as the starting point for a repaint
189 void postPaint(int start_y);
192 * Mark the given row at position y as needing a repaint.
194 void postRowPaint(RowList::iterator rit, int start_y);
197 Inset::RESULT dispatch(FuncRequest const & cmd);
201 BufferView * bv() const;
203 friend class LyXScreen;
206 * Return the status. This represents what repaints are
207 * pending after some operation (e.g. inserting a char).
209 refresh_status refreshStatus() const;
213 * The pixel y position from which to repaint the screen.
214 * The position is absolute along the height of outermost
215 * lyxtext (I think). REFRESH_AREA and REFRESH_ROW
216 * repaints both use this as a starting point (if it's within
217 * the viewable portion of the lyxtext).
221 * The row from which to repaint the screen, used by screen.c.
222 * This must be set if the pending update is REFRESH_ROW.
223 * It doesn't make any difference for REFRESH_AREA.
225 RowList::iterator refresh_row;
227 refresh_status refresh_status_;
229 /// only the top-level LyXText has this non-zero
230 BufferView * bv_owner;
233 /** returns a pointer to the row near the specified y-coordinate
234 (relative to the whole text). y is set to the real beginning
237 RowList::iterator getRowNearY(int & y) const;
239 /** returns the column near the specified x-coordinate of the row
240 x is set to the real beginning of this column
242 lyx::pos_type getColumnNearX(RowList::iterator rit,
243 int & x, bool & boundary) const;
245 /** returns a pointer to a specified row. y is set to the beginning
249 getRow(Paragraph * par, lyx::pos_type pos, int & y) const;
251 RowList & rows() const {
256 Later this variable has to be removed. There should be now internal
257 cursor in a text (and thus not in a buffer). By keeping this it is
258 (I think) impossible to have several views with the same buffer, but
259 the cursor placed at different places.
261 Since the LyXText now has been moved from Buffer to BufferView
262 it should not be absolutely needed to move the cursor...
264 LyXCursor cursor; // actual cursor position
266 /** The structure that keeps track of the selections set. */
269 : set_(false), mark_(false)
283 LyXCursor cursor; // temporary cursor to hold a cursor position
284 // until setSelection is called!
285 LyXCursor start; // start of a REAL selection
286 LyXCursor end; // end of a REAL selection
288 bool set_; // former selection
289 bool mark_; // former mark_set
293 // this is used to handle XSelection events in the right manner
294 Selection xsel_cache;
296 /// needed for the toggling (cursor position on last selection made)
297 LyXCursor last_sel_cursor;
298 /// needed for toggling the selection in screen.C
299 LyXCursor toggle_cursor;
300 /// needed for toggling the selection in screen.C
301 LyXCursor toggle_end_cursor;
303 /// need the selection cursor:
306 void clearSelection();
308 string const selectionAsString(Buffer const *, bool label) const;
310 /// select the word we need depending on word_location
311 void getWord(LyXCursor & from, LyXCursor & to,
312 word_location const);
313 /// just selects the word the cursor is in
314 void selectWord(word_location const);
315 /// returns the inset at cursor (if it exists), 0 otherwise
316 Inset * getInset() const;
318 /// accept selected change
321 /// reject selected change
324 /** 'selects" the next word, where the cursor is not in
325 and returns this word as string. THe cursor will be moved
326 to the beginning of this word.
327 With SelectSelectedWord can this be highlighted really
329 WordLangTuple const selectNextWordToSpellcheck(float & value);
331 void selectSelectedWord();
332 /// returns true if par was empty and was removed
333 bool setCursor(Paragraph * par,
336 bool boundary = false);
338 void setCursor(LyXCursor &, Paragraph * par,
340 bool boundary = false);
342 void setCursorIntern(Paragraph * par,
345 bool boundary = false);
347 void setCurrentFont();
350 bool isBoundary(Buffer const *, Paragraph * par,
351 lyx::pos_type pos) const;
353 bool isBoundary(Buffer const *, Paragraph * par,
355 LyXFont const & font) const;
358 void setCursorFromCoordinates(int x, int y);
360 void setCursorFromCoordinates(LyXCursor &,
363 void cursorUp(bool selecting = false);
365 void cursorDown(bool selecting = false);
367 void cursorLeft(bool internal = true);
369 void cursorRight(bool internal = true);
371 void cursorLeftOneWord();
373 void cursorRightOneWord();
375 void cursorUpParagraph();
377 void cursorDownParagraph();
383 void cursorPrevious();
395 bool selectWordWhenUnderCursor(word_location);
401 text_capitalization = 1,
405 /// Change the case of the word at cursor position.
406 void changeCase(TextCase action);
411 void cutSelection(bool doclear = true, bool realcut = true);
413 void copySelection();
415 void pasteSelection();
417 void copyEnvironmentType();
419 void pasteEnvironmentType();
421 /** the DTP switches for paragraphs. LyX will store the top settings
422 always in the first physical paragraph, the bottom settings in the
423 last. When a paragraph is broken, the top settings rest, the bottom
424 settings are given to the new one. So I can make shure, they do not
425 duplicate themself (and you cannnot make dirty things with them! )
427 void setParagraph(bool line_top, bool line_bottom,
428 bool pagebreak_top, bool pagebreak_bottom,
429 VSpace const & space_top,
430 VSpace const & space_bottom,
431 Spacing const & spacing,
433 string const & labelwidthstring,
436 /* these things are for search and replace */
439 * Sets the selection from the current cursor position to length
440 * characters to the right. No safety checks.
442 void setSelectionRange(lyx::pos_type length);
444 /** simple replacing. The font of the first selected character
447 void replaceSelectionWithString(string const & str);
449 /// needed to insert the selection
450 void insertStringAsLines(string const & str);
451 /// needed to insert the selection
452 void insertStringAsParagraphs(string const & str);
454 /// Find next inset of some specified type.
455 bool gotoNextInset(std::vector<Inset::Code> const & codes,
456 string const & contents = string());
458 void gotoInset(std::vector<Inset::Code> const & codes,
461 void gotoInset(Inset::Code code, bool same_content);
464 /* for the greater insets */
466 /// returns false if inset wasn't found
467 bool updateInset(Inset *);
469 void checkParagraph(Paragraph * par, lyx::pos_type pos);
471 int workWidth() const;
473 int workWidth(Inset * inset) const;
476 void computeBidiTables(Buffer const *, RowList::iterator row) const;
477 /// Maps positions in the visual string to positions in logical string.
478 lyx::pos_type log2vis(lyx::pos_type pos) const;
479 /// Maps positions in the logical string to positions in visual string.
480 lyx::pos_type vis2log(lyx::pos_type pos) const;
482 lyx::pos_type bidi_level(lyx::pos_type pos) const;
484 bool bidi_InRange(lyx::pos_type pos) const;
487 mutable RowList rowlist_;
489 void cursorLeftOneWord(LyXCursor &);
492 float getCursorX(RowList::iterator rit, lyx::pos_type pos,
493 lyx::pos_type last, bool boundary) const;
494 /// used in setlayout
495 void makeFontEntriesLayoutSpecific(Buffer const &, Paragraph & par);
497 /** forces the redrawing of a paragraph. Needed when manipulating a
500 void redoDrawingOfParagraph(LyXCursor const & cursor);
502 /** Copybuffer for copy environment type.
503 Asger has learned that this should be a buffer-property instead
504 Lgb has learned that 'char' is a lousy type for non-characters
506 string copylayouttype;
508 /// removes the row and reset the touched counters
509 void removeRow(RowList::iterator rit);
511 /// remove all following rows of the paragraph of the specified row.
512 void removeParagraph(RowList::iterator rit);
514 /// insert the specified paragraph behind the specified row
515 void insertParagraph(Paragraph * par, RowList::iterator rowit);
517 /** appends the implizit specified paragraph behind the specified row,
518 * start at the implizit given position */
519 void appendParagraph(RowList::iterator rowit);
522 void breakAgain(RowList::iterator rit);
523 /// Calculate and set the height of the row
524 void setHeightOfRow(RowList::iterator rit);
526 // fix the cursor `cur' after a characters has been deleted at `where'
527 // position. Called by deleteEmptyParagraphMechanism
528 void fixCursorAfterDelete(LyXCursor & cur,
529 LyXCursor const & where);
531 /// delete double space (false) or empty paragraphs (true) around old_cursor
532 bool deleteEmptyParagraphMechanism(LyXCursor const & old_cursor);
535 /** Updates all counters starting BEHIND the row. Changed paragraphs
536 * with a dynamic left margin will be rebroken. */
537 void updateCounters();
541 * Returns an inset if inset was hit, or 0 if not.
542 * If hit, the coordinates are changed relative to the inset.
544 Inset * checkInsetHit(int & x, int & y);
547 int singleWidth(Paragraph * par,
548 lyx::pos_type pos) const;
550 int singleWidth(Paragraph * par,
551 lyx::pos_type pos, char c) const;
553 /// return the color of the canvas
554 LColor::color backgroundColor() const;
557 mutable bool bidi_same_direction;
559 unsigned char transformChar(unsigned char c, Paragraph * par,
560 lyx::pos_type pos) const;
563 * Returns the left beginning of the text.
564 * This information cannot be taken from the layout object, because
565 * in LaTeX the beginning of the text fits in some cases
566 * (for example sections) exactly the label-width.
568 int leftMargin(Row const & row) const;
570 int rightMargin(Buffer const &, Row const & row) const;
572 /** this calculates the specified parameters. needed when setting
573 * the cursor and when creating a visible row */
574 void prepareToPrint(RowList::iterator row, float & x,
575 float & fill_separator,
577 float & fill_label_hfill,
578 bool bidi = true) const;
582 void setCounter(Buffer const *, Paragraph * par);
584 void deleteWordForward();
586 void deleteWordBackward();
588 void deleteLineForward();
591 * some low level functions
595 /// return the pos value *before* which a row should break.
596 /// for example, the pos at which IsNewLine(pos) == true
597 lyx::pos_type rowBreakPoint(Row const & row) const;
599 /// returns the minimum space a row needs on the screen in pixel
600 int fill(RowList::iterator row, int workwidth) const;
603 * returns the minimum space a manual label needs on the
606 int labelFill(Row const & row) const;
609 int labelEnd(Row const & row) const;
612 mutable std::vector<lyx::pos_type> log2vis_list;
614 mutable std::vector<lyx::pos_type> vis2log_list;
616 mutable std::vector<lyx::pos_type> bidi_levels;
618 mutable lyx::pos_type bidi_start;
620 mutable lyx::pos_type bidi_end;
626 // special owner functions
628 ParagraphList & ownerParagraphs() const;
630 void ownerParagraph(Paragraph *) const;
631 // set it searching first for the right owner using the paragraph id
632 void ownerParagraph(int id, Paragraph *) const;
634 /// return true if this is owned by an inset.
635 bool isInInset() const;
638 /// return the default height of a row in pixels, considering font zoom
639 extern int defaultRowHeight();