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 full rebreak
187 RowList::iterator need_break_row;
189 /// clear any pending paints
193 * Mark position y as the starting point for a repaint
195 void postPaint(int start_y);
198 * Mark the given row at position y as needing a repaint.
200 void postRowPaint(RowList::iterator rit, int start_y);
203 Inset::RESULT dispatch(FuncRequest const & cmd);
207 BufferView * bv() const;
209 friend class LyXScreen;
212 * Return the status. This represents what repaints are
213 * pending after some operation (e.g. inserting a char).
215 refresh_status refreshStatus() const;
219 * The pixel y position from which to repaint the screen.
220 * The position is absolute along the height of outermost
221 * lyxtext (I think). REFRESH_AREA and REFRESH_ROW
222 * repaints both use this as a starting point (if it's within
223 * the viewable portion of the lyxtext).
227 * The row from which to repaint the screen, used by screen.c.
228 * This must be set if the pending update is REFRESH_ROW.
229 * It doesn't make any difference for REFRESH_AREA.
231 RowList::iterator refresh_row;
233 refresh_status refresh_status_;
235 /// only the top-level LyXText has this non-zero
236 BufferView * bv_owner;
239 /** returns a pointer to the row near the specified y-coordinate
240 (relative to the whole text). y is set to the real beginning
243 RowList::iterator getRowNearY(int & y) const;
245 /** returns the column near the specified x-coordinate of the row
246 x is set to the real beginning of this column
248 lyx::pos_type getColumnNearX(RowList::iterator rit,
249 int & x, bool & boundary) const;
251 /** returns a pointer to a specified row. y is set to the beginning
255 getRow(ParagraphList::iterator pit, lyx::pos_type pos, int & y) const;
257 RowList & rows() const {
262 Later this variable has to be removed. There should be now internal
263 cursor in a text (and thus not in a buffer). By keeping this it is
264 (I think) impossible to have several views with the same buffer, but
265 the cursor placed at different places.
267 Since the LyXText now has been moved from Buffer to BufferView
268 it should not be absolutely needed to move the cursor...
270 LyXCursor cursor; // actual cursor position
272 /** The structure that keeps track of the selections set. */
275 : set_(false), mark_(false)
289 LyXCursor cursor; // temporary cursor to hold a cursor position
290 // until setSelection is called!
291 LyXCursor start; // start of a REAL selection
292 LyXCursor end; // end of a REAL selection
294 bool set_; // former selection
295 bool mark_; // former mark_set
299 // this is used to handle XSelection events in the right manner
300 Selection xsel_cache;
302 /// needed for the toggling (cursor position on last selection made)
303 LyXCursor last_sel_cursor;
304 /// needed for toggling the selection in screen.C
305 LyXCursor toggle_cursor;
306 /// needed for toggling the selection in screen.C
307 LyXCursor toggle_end_cursor;
309 /// need the selection cursor:
312 void clearSelection();
314 string const selectionAsString(Buffer const *, bool label) const;
316 /// select the word we need depending on word_location
317 void getWord(LyXCursor & from, LyXCursor & to,
318 word_location const);
319 /// just selects the word the cursor is in
320 void selectWord(word_location const);
321 /// returns the inset at cursor (if it exists), 0 otherwise
322 Inset * getInset() const;
324 /// accept selected change
327 /// reject selected change
330 /** 'selects" the next word, where the cursor is not in
331 and returns this word as string. THe cursor will be moved
332 to the beginning of this word.
333 With SelectSelectedWord can this be highlighted really
335 WordLangTuple const selectNextWordToSpellcheck(float & value);
337 void selectSelectedWord();
338 /// returns true if par was empty and was removed
339 bool setCursor(ParagraphList::iterator pit,
342 bool boundary = false);
344 void setCursor(LyXCursor &, ParagraphList::iterator pit,
346 bool boundary = false);
348 void setCursorIntern(ParagraphList::iterator pit,
351 bool boundary = false);
353 void setCurrentFont();
356 bool isBoundary(Buffer const *, Paragraph const & par,
357 lyx::pos_type pos) const;
359 bool isBoundary(Buffer const *, Paragraph const & par,
361 LyXFont const & font) const;
364 void setCursorFromCoordinates(int x, int y);
366 void setCursorFromCoordinates(LyXCursor &,
369 void cursorUp(bool selecting = false);
371 void cursorDown(bool selecting = false);
373 void cursorLeft(bool internal = true);
375 void cursorRight(bool internal = true);
377 void cursorLeftOneWord();
379 void cursorRightOneWord();
381 void cursorUpParagraph();
383 void cursorDownParagraph();
389 void cursorPrevious();
401 bool selectWordWhenUnderCursor(word_location);
407 text_capitalization = 1,
411 /// Change the case of the word at cursor position.
412 void changeCase(TextCase action);
417 void cutSelection(bool doclear = true, bool realcut = true);
419 void copySelection();
421 void pasteSelection(size_t sel_index = 0);
423 /** the DTP switches for paragraphs. LyX will store the top settings
424 always in the first physical paragraph, the bottom settings in the
425 last. When a paragraph is broken, the top settings rest, the bottom
426 settings are given to the new one. So I can make shure, they do not
427 duplicate themself (and you cannnot make dirty things with them! )
429 void setParagraph(bool line_top, bool line_bottom,
430 bool pagebreak_top, bool pagebreak_bottom,
431 VSpace const & space_top,
432 VSpace const & space_bottom,
433 Spacing const & spacing,
435 string const & labelwidthstring,
438 /* these things are for search and replace */
441 * Sets the selection from the current cursor position to length
442 * characters to the right. No safety checks.
444 void setSelectionRange(lyx::pos_type length);
446 /** simple replacing. The font of the first selected character
449 void replaceSelectionWithString(string const & str);
451 /// needed to insert the selection
452 void insertStringAsLines(string const & str);
453 /// needed to insert the selection
454 void insertStringAsParagraphs(string const & str);
456 /// Find next inset of some specified type.
457 bool gotoNextInset(std::vector<Inset::Code> const & codes,
458 string const & contents = string());
460 void gotoInset(std::vector<Inset::Code> const & codes,
463 void gotoInset(Inset::Code code, bool same_content);
466 /* for the greater insets */
468 /// returns false if inset wasn't found
469 bool updateInset(Inset *);
471 void checkParagraph(ParagraphList::iterator pit, lyx::pos_type pos);
473 int workWidth() const;
474 /// returns width of row containing inset
475 int workWidth(Inset const * inset) const;
478 void computeBidiTables(Buffer const *, RowList::iterator row) const;
479 /// Maps positions in the visual string to positions in logical string.
480 lyx::pos_type log2vis(lyx::pos_type pos) const;
481 /// Maps positions in the logical string to positions in visual string.
482 lyx::pos_type vis2log(lyx::pos_type pos) const;
484 lyx::pos_type bidi_level(lyx::pos_type pos) const;
486 bool bidi_InRange(lyx::pos_type pos) const;
489 mutable RowList rowlist_;
491 void cursorLeftOneWord(LyXCursor &);
494 float getCursorX(RowList::iterator rit, lyx::pos_type pos,
495 lyx::pos_type last, bool boundary) const;
496 /// used in setlayout
497 void makeFontEntriesLayoutSpecific(Buffer const &, Paragraph & par);
499 /** forces the redrawing of a paragraph. Needed when manipulating a
502 void redoDrawingOfParagraph(LyXCursor const & cursor);
504 /// removes the row and reset the touched counters
505 void removeRow(RowList::iterator rit);
507 /// remove all following rows of the paragraph of the specified row.
508 void removeParagraph(RowList::iterator rit);
510 /// insert the specified paragraph behind the specified row
511 void insertParagraph(ParagraphList::iterator pit,
512 RowList::iterator rowit);
514 /** appends the implizit specified paragraph behind the specified row,
515 * start at the implizit given position */
516 void appendParagraph(RowList::iterator rowit);
519 void breakAgain(RowList::iterator rit);
520 /// Calculate and set the height of the row
521 void setHeightOfRow(RowList::iterator rit);
523 // fix the cursor `cur' after a characters has been deleted at `where'
524 // position. Called by deleteEmptyParagraphMechanism
525 void fixCursorAfterDelete(LyXCursor & cur,
526 LyXCursor const & where);
528 /// delete double space (false) or empty paragraphs (true) around old_cursor
529 bool deleteEmptyParagraphMechanism(LyXCursor const & old_cursor);
532 /** Updates all counters starting BEHIND the row. Changed paragraphs
533 * with a dynamic left margin will be rebroken. */
534 void updateCounters();
538 * Returns an inset if inset was hit, or 0 if not.
539 * If hit, the coordinates are changed relative to the inset.
541 Inset * checkInsetHit(int & x, int & y);
544 int singleWidth(ParagraphList::iterator pit,
545 lyx::pos_type pos) const;
547 int singleWidth(ParagraphList::iterator pit,
548 lyx::pos_type pos, char c) const;
550 /// return the color of the canvas
551 LColor::color backgroundColor() const;
554 mutable bool bidi_same_direction;
556 unsigned char transformChar(unsigned char c, Paragraph const & par,
557 lyx::pos_type pos) const;
560 * Returns the left beginning of the text.
561 * This information cannot be taken from the layout object, because
562 * in LaTeX the beginning of the text fits in some cases
563 * (for example sections) exactly the label-width.
565 int leftMargin(Row const & row) const;
567 int rightMargin(Buffer const &, Row const & row) const;
569 /** this calculates the specified parameters. needed when setting
570 * the cursor and when creating a visible row */
571 void prepareToPrint(RowList::iterator row, float & x,
572 float & fill_separator,
574 float & fill_label_hfill,
575 bool bidi = true) const;
579 void setCounter(Buffer const *, ParagraphList::iterator pit);
581 void deleteWordForward();
583 void deleteWordBackward();
585 void deleteLineForward();
588 * some low level functions
592 /// return the pos value *before* which a row should break.
593 /// for example, the pos at which IsNewLine(pos) == true
594 lyx::pos_type rowBreakPoint(Row const & row) const;
596 /// returns the minimum space a row needs on the screen in pixel
597 int fill(RowList::iterator row, int workwidth) const;
600 * returns the minimum space a manual label needs on the
603 int labelFill(Row const & row) const;
606 int labelEnd(Row const & row) const;
609 mutable std::vector<lyx::pos_type> log2vis_list;
611 mutable std::vector<lyx::pos_type> vis2log_list;
613 mutable std::vector<lyx::pos_type> bidi_levels;
615 mutable lyx::pos_type bidi_start;
617 mutable lyx::pos_type bidi_end;
623 // special owner functions
625 ParagraphList & ownerParagraphs() const;
627 /// return true if this is owned by an inset.
628 bool isInInset() const;
631 /// return the default height of a row in pixels, considering font zoom
632 extern int defaultRowHeight();