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"
42 This class holds the mapping between buffer paragraphs and screen rows.
45 // The inheritance from TextCursor should go. It's just there to ease
47 class LyXText : public TextCursor {
50 LyXText(BufferView *);
51 /// sets inset as owner
52 LyXText(BufferView *, InsetText *);
54 void init(BufferView *);
59 /// the current font settings
62 LyXFont real_current_font;
63 /// our buffer's default layout font
66 /** the 'anchor' row: the position of this row remains constant
67 * with respect to the top of the screen
69 RowList::iterator anchor_row_;
70 /** the pixel offset with respect to this row of top_y
72 int anchor_row_offset_;
74 /// update all cached row positions
75 void updateRowPositions();
76 /// get the y coord. of the top of the screen (relative to doc start)
78 /// set the y coord. of the top of the screen (relative to doc start)
80 /// set the anchoring row. top_y will be computed relative to this
81 void anchor_row(RowList::iterator rit);
83 InsetText * inset_owner;
85 UpdatableInset * the_locking_inset;
88 int getRealCursorX() const;
90 LyXFont getFont(ParagraphList::iterator pit, lyx::pos_type pos) const;
92 LyXFont getLayoutFont(ParagraphList::iterator pit) const;
94 LyXFont getLabelFont(ParagraphList::iterator pit) const;
96 void setCharFont(ParagraphList::iterator pit,
97 lyx::pos_type pos, LyXFont const & font);
98 void setCharFont(ParagraphList::iterator pit,
100 LyXFont const & font, bool toggleall);
103 void breakAgainOneRow(RowList::iterator rit);
104 /// what you expect when pressing <enter> at cursor position
105 void breakParagraph(ParagraphList & paragraphs, char keep_layout = 0);
107 /** set layout over selection and make a total rebreak of
110 ParagraphList::iterator
111 setLayout(LyXCursor & actual_cursor,
112 LyXCursor & selection_start,
113 LyXCursor & selection_end,
114 string const & layout);
116 void setLayout(string const & layout);
119 * Increase or decrease the nesting depth of the selected paragraph(s)
120 * if test_only, don't change any depths. Returns whether something
121 * (would have) changed
123 bool changeDepth(bv_funcs::DEPTH_CHANGE type, bool test_only);
125 /// get the depth at current cursor position
126 int getDepth() const;
128 /** set font over selection and make a total rebreak of those
130 toggleall defaults to false.
132 void setFont(LyXFont const &, bool toggleall = false);
134 /** deletes and inserts again all paragaphs between the cursor
135 and the specified par. The Cursor is needed to set the refreshing
137 This function is needed after SetLayout and SetFont etc.
139 void redoParagraphs(LyXCursor const & cursor,
140 ParagraphList::iterator endpit);
142 void redoParagraph();
145 void toggleFree(LyXFont const &, bool toggleall = false);
148 string getStringToIndex();
150 /** recalculates the heights of all previous rows of the
151 specified paragraph. needed, if the last characters font
154 void redoHeightOfParagraph();
156 /** insert a character, moves all the following breaks in the
157 same Paragraph one to the right and make a little rebreak
159 void insertChar(char c);
161 void insertInset(InsetOld * inset);
163 /// Completes the insertion with a rebreak
164 void partialRebreak();
165 /// a full rebreak of the whole text
167 /// compute text metrics
168 void metrics(MetricsInfo & mi, Dimension & dim);
170 /// clear any pending paints
173 /// submit repaint request
177 InsetOld::RESULT dispatch(FuncRequest const & cmd);
181 BufferView * bv() const;
183 friend class LyXScreen;
186 * Return the status. This represents what repaints are
187 * pending after some operation (e.g. inserting a char).
189 bool needRefresh() const;
192 // do we need a refresh?
196 /// only the top-level LyXText has this non-zero
197 BufferView * bv_owner;
200 /// returns a pointer to a specified row.
202 getRow(ParagraphList::iterator pit, lyx::pos_type pos) const;
204 /// returns a pointer cursor row
205 RowList::iterator getRow(LyXCursor const & cursor) const;
207 RowList::iterator cursorRow() const;
209 * Return the next row, when cursor is at the end of the
210 * previous row, for insets that take a full row.
212 * FIXME: explain why we need this ? especially for y...
214 RowList::iterator cursorIRow() const;
216 /** returns a pointer to the row near the specified y-coordinate
217 (relative to the whole text). y is set to the real beginning
220 RowList::iterator getRowNearY(int & y) const;
222 /** returns the column near the specified x-coordinate of the row
223 x is set to the real beginning of this column
225 lyx::pos_type getColumnNearX(RowList::iterator rit,
226 int & x, bool & boundary) const;
228 /** returns a pointer to a specified row. y is set to the beginning
232 getRow(ParagraphList::iterator pit, lyx::pos_type pos, int & y) const;
234 RowList & rows() const {
239 /// need the selection cursor:
242 void clearSelection();
244 /// select the word we need depending on word_location
245 void getWord(LyXCursor & from, LyXCursor & to, lyx::word_location const);
246 /// just selects the word the cursor is in
247 void selectWord(lyx::word_location loc);
248 /// returns the inset at cursor (if it exists), 0 otherwise
249 InsetOld * getInset() const;
251 /// accept selected change
254 /// reject selected change
257 /** 'selects" the next word, where the cursor is not in
258 and returns this word as string. THe cursor will be moved
259 to the beginning of this word.
260 With SelectSelectedWord can this be highlighted really
262 WordLangTuple const selectNextWordToSpellcheck(float & value);
264 void selectSelectedWord();
265 /// returns true if par was empty and was removed
266 bool setCursor(ParagraphList::iterator pit,
269 bool boundary = false);
271 void setCursor(LyXCursor &, ParagraphList::iterator pit,
273 bool boundary = false);
275 void setCursorIntern(ParagraphList::iterator pit,
278 bool boundary = false);
280 void setCurrentFont();
283 bool isBoundary(Buffer const *, Paragraph const & par,
284 lyx::pos_type pos) const;
286 bool isBoundary(Buffer const *, Paragraph const & par,
288 LyXFont const & font) const;
291 void setCursorFromCoordinates(int x, int y);
293 void setCursorFromCoordinates(LyXCursor &,
296 void cursorUp(bool selecting = false);
298 void cursorDown(bool selecting = false);
300 void cursorLeft(bool internal = true);
302 void cursorRight(bool internal = true);
304 void cursorLeftOneWord();
306 void cursorRightOneWord();
308 void cursorUpParagraph();
310 void cursorDownParagraph();
316 void cursorPrevious();
328 bool selectWordWhenUnderCursor(lyx::word_location);
334 text_capitalization = 1,
338 /// Change the case of the word at cursor position.
339 void changeCase(TextCase action);
344 void cutSelection(bool doclear = true, bool realcut = true);
346 void copySelection();
348 void pasteSelection(size_t sel_index = 0);
350 /** the DTP switches for paragraphs. LyX will store the top settings
351 always in the first physical paragraph, the bottom settings in the
352 last. When a paragraph is broken, the top settings rest, the bottom
353 settings are given to the new one. So I can make shure, they do not
354 duplicate themself (and you cannnot make dirty things with them! )
356 void setParagraph(bool line_top, bool line_bottom,
357 bool pagebreak_top, bool pagebreak_bottom,
358 VSpace const & space_top,
359 VSpace const & space_bottom,
360 Spacing const & spacing,
362 string const & labelwidthstring,
365 /* these things are for search and replace */
368 * Sets the selection from the current cursor position to length
369 * characters to the right. No safety checks.
371 void setSelectionRange(lyx::pos_type length);
373 /** simple replacing. The font of the first selected character
376 void replaceSelectionWithString(string const & str);
378 /// needed to insert the selection
379 void insertStringAsLines(string const & str);
380 /// needed to insert the selection
381 void insertStringAsParagraphs(string const & str);
383 /// Find next inset of some specified type.
384 bool gotoNextInset(std::vector<InsetOld::Code> const & codes,
385 string const & contents = string());
387 void gotoInset(std::vector<InsetOld::Code> const & codes,
390 void gotoInset(InsetOld::Code code, bool same_content);
393 /* for the greater insets */
395 /// returns false if inset wasn't found
396 bool updateInset(InsetOld *);
398 void checkParagraph(ParagraphList::iterator pit, lyx::pos_type pos);
400 int workWidth() const;
403 void computeBidiTables(Buffer const *, RowList::iterator row) const;
404 /// Maps positions in the visual string to positions in logical string.
405 lyx::pos_type log2vis(lyx::pos_type pos) const;
406 /// Maps positions in the logical string to positions in visual string.
407 lyx::pos_type vis2log(lyx::pos_type pos) const;
409 lyx::pos_type bidi_level(lyx::pos_type pos) const;
411 bool bidi_InRange(lyx::pos_type pos) const;
414 mutable RowList rowlist_;
417 float getCursorX(RowList::iterator rit, lyx::pos_type pos,
418 lyx::pos_type last, bool boundary) const;
419 /// used in setlayout
420 void makeFontEntriesLayoutSpecific(BufferParams const &, Paragraph & par);
422 /** forces the redrawing of a paragraph. Needed when manipulating a
425 void redoDrawingOfParagraph(LyXCursor const & cursor);
427 /// removes the row and reset the touched counters
428 void removeRow(RowList::iterator rit);
430 /// remove all following rows of the paragraph of the specified row.
431 void removeParagraph(RowList::iterator rit);
433 /// insert the specified paragraph behind the specified row
434 void insertParagraph(ParagraphList::iterator pit,
435 RowList::iterator rowit);
437 /** appends the implizit specified paragraph behind the specified row,
438 * start at the implizit given position */
439 void appendParagraph(RowList::iterator rowit);
442 void breakAgain(RowList::iterator rit);
443 /// Calculate and set the height of the row
444 void setHeightOfRow(RowList::iterator rit);
446 // fix the cursor `cur' after a characters has been deleted at `where'
447 // position. Called by deleteEmptyParagraphMechanism
448 void fixCursorAfterDelete(LyXCursor & cur,
449 LyXCursor const & where);
451 /// delete double space (false) or empty paragraphs (true) around old_cursor
452 bool deleteEmptyParagraphMechanism(LyXCursor const & old_cursor);
455 /** Updates all counters starting BEHIND the row. Changed paragraphs
456 * with a dynamic left margin will be rebroken. */
457 void updateCounters();
461 * Returns an inset if inset was hit, or 0 if not.
462 * If hit, the coordinates are changed relative to the inset.
464 InsetOld * checkInsetHit(int & x, int & y);
467 int singleWidth(ParagraphList::iterator pit, lyx::pos_type pos) const;
469 int singleWidth(ParagraphList::iterator pit,
470 lyx::pos_type pos, char c) const;
471 /// rebuild row cache
472 void rebuildRows(ParagraphList::iterator pit);
474 /// return the color of the canvas
475 LColor::color backgroundColor() const;
478 mutable bool bidi_same_direction;
480 unsigned char transformChar(unsigned char c, Paragraph const & par,
481 lyx::pos_type pos) const;
484 * Returns the left beginning of the text.
485 * This information cannot be taken from the layout object, because
486 * in LaTeX the beginning of the text fits in some cases
487 * (for example sections) exactly the label-width.
489 int leftMargin(Row const & row) const;
491 int rightMargin(Buffer const &, Row const & row) const;
493 /** this calculates the specified parameters. needed when setting
494 * the cursor and when creating a visible row */
495 void prepareToPrint(RowList::iterator row, double & x,
496 double & fill_separator,
498 double & fill_label_hfill,
499 bool bidi = true) const;
503 void setCounter(Buffer const *, ParagraphList::iterator pit);
505 void deleteWordForward();
507 void deleteWordBackward();
509 void deleteLineForward();
512 * some low level functions
516 /// return the pos value *before* which a row should break.
517 /// for example, the pos at which IsNewLine(pos) == true
518 lyx::pos_type rowBreakPoint(Row const & row) const;
520 /// returns the minimum space a row needs on the screen in pixel
521 int fill(RowList::iterator row, int workwidth) const;
524 * returns the minimum space a manual label needs on the
527 int labelFill(Row const & row) const;
530 int labelEnd(Row const & row) const;
533 mutable std::vector<lyx::pos_type> log2vis_list;
535 mutable std::vector<lyx::pos_type> vis2log_list;
537 mutable std::vector<lyx::pos_type> bidi_levels;
539 mutable lyx::pos_type bidi_start;
541 mutable lyx::pos_type bidi_end;
547 // special owner functions
549 ParagraphList & ownerParagraphs() const;
551 /// return true if this is owned by an inset.
552 bool isInInset() const;
555 /** Cursor related data.
556 Later this variable has to be removed. There should be now internal
559 ///TextCursor cursor_;
562 /// return the default height of a row in pixels, considering font zoom
563 extern int defaultRowHeight();