2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-2001 The LyX Team.
10 * ====================================================== */
16 #include "lyxcursor.h"
19 #include "insets/inset.h"
35 This class holds the mapping between buffer paragraphs and screen rows.
39 /// what repainting is needed
41 /// no repaint is needed
44 * A paint has caused a change in some rows
45 * and rebreaking is needed.
48 /// the refresh_row needs repainting
49 NEED_VERY_LITTLE_REFRESH = 2,
50 /// everything from refresh_y downwards needs repainting
56 // the word around the cursor, only if the cursor is
59 // the word around the cursor
61 /// the word begining from the cursor position
63 /// the word around the cursor or before the cursor
65 /// the next word (not yet used)
70 LyXText(BufferView *);
71 /// sets inset as owner
72 LyXText(BufferView *, InsetText *);
77 void init(BufferView *, bool reinit = false);
82 /// the current font settings
85 LyXFont real_current_font;
87 /** the 'anchor' row: the position of this row remains constant
88 * with respect to the top of the screen
91 /** the pixel offset with respect to this row of top_y
93 int anchor_row_offset_;
95 /// get the y coord. of the top of the screen (relative to doc start)
97 /// set the y coord. of the top of the screen (relative to doc start)
99 /// set the anchoring row. top_y will be computed relative to this
100 void anchor_row(Row * row);
102 InsetText * inset_owner;
104 UpdatableInset * the_locking_inset;
107 int getRealCursorX() const;
109 LyXFont const getFont(Buffer const *, Paragraph * par,
110 lyx::pos_type pos) const;
112 LyXFont const getLayoutFont(Buffer const *, Paragraph * par) const;
114 LyXFont const getLabelFont(Buffer const *, Paragraph * par) const;
116 void setCharFont(Buffer const *, Paragraph * par,
117 lyx::pos_type pos, LyXFont const & font);
118 void setCharFont(Paragraph * par,
119 lyx::pos_type pos, LyXFont const & font, bool toggleall);
121 /// return true if the row changed
122 void markChangeInDraw(Row * row, Row * next);
124 void breakAgainOneRow(Row * row);
125 /// what you expect when pressing <enter> at cursor position
126 void breakParagraph(ParagraphList & paragraphs, char keep_layout = 0);
128 /** set layout over selection and make a total rebreak of
131 Paragraph * setLayout(LyXCursor & actual_cursor,
132 LyXCursor & selection_start,
133 LyXCursor & selection_end,
134 string const & layout);
136 void setLayout(string const & layout);
138 /** increment depth over selection and make a total rebreak of those
143 /** decrement depth over selection and make a total rebreak of those
147 /// get the depth at current cursor position
148 int getDepth() const;
150 /** set font over selection and make a total rebreak of those
152 toggleall defaults to false.
154 void setFont(LyXFont const &, bool toggleall = false);
156 /** deletes and inserts again all paragaphs between the cursor
157 and the specified par. The Cursor is needed to set the refreshing
159 This function is needed after SetLayout and SetFont etc.
161 void redoParagraphs(LyXCursor const & cursor,
162 Paragraph const * end_par);
164 void redoParagraph();
167 void toggleFree(LyXFont const &, bool toggleall = false);
170 string getStringToIndex();
172 /** recalculates the heights of all previous rows of the
173 specified paragraph. needed, if the last characters font
176 void redoHeightOfParagraph();
178 /** insert a character, moves all the following breaks in the
179 same Paragraph one to the right and make a little rebreak
181 void insertChar(char c);
183 void insertInset(Inset * inset);
185 /// Completes the insertion with a full rebreak
189 Row * need_break_row;
191 /// clear any pending paints
194 /// post notice that we changed during a draw
195 void postChangedInDraw();
198 * Mark position y as the starting point for a repaint
200 void postPaint(int start_y);
203 * Mark the given row at position y as needing a repaint.
205 void postRowPaint(Row * row, int start_y);
208 Inset::RESULT dispatch(FuncRequest const & cmd);
212 BufferView * bv() const;
214 friend class LyXScreen;
217 * Return the status. This represents what repaints are
218 * pending after some operation (e.g. inserting a char).
220 text_status status() const;
224 * The pixel y position from which to repaint the screen.
225 * The position is absolute along the height of outermost
226 * lyxtext (I think). NEED_MORE_REFRESH and NEED_LITTLE_REFRESH
227 * repaints both use this as a starting point (if it's within
228 * the viewable portion of the lyxtext).
232 * The row from which to repaint the screen, used by screen.c.
233 * This must be set if the pending update is NEED_LITTLE_REFRESH.
234 * It doesn't make any difference for NEED_MORE_REFRESH.
241 /// only the top-level LyXText has this non-zero
242 BufferView * bv_owner;
245 /** returns a pointer to the row near the specified y-coordinate
246 (relative to the whole text). y is set to the real beginning
249 Row * getRowNearY(int & y) const;
251 /** returns the column near the specified x-coordinate of the row
252 x is set to the real beginning of this column
254 lyx::pos_type getColumnNearX(Row * row,
255 int & x, bool & boundary) const;
257 /** returns a pointer to a specified row. y is set to the beginning
260 Row * getRow(Paragraph * par, lyx::pos_type pos, int & y) const;
261 /** returns the firstrow, this could be done with the above too but
262 IMO it's stupid to have to allocate a dummy y all the time I need
265 Row * firstRow() const { return firstrow; }
268 Later this variable has to be removed. There should be now internal
269 cursor in a text (and thus not in a buffer). By keeping this it is
270 (I think) impossible to have several views with the same buffer, but
271 the cursor placed at different places.
273 Since the LyXText now has been moved from Buffer to BufferView
274 it should not be absolutely needed to move the cursor...
276 LyXCursor cursor; // actual cursor position
278 /** The structure that keeps track of the selections set. */
281 : set_(false), mark_(false)
295 LyXCursor cursor; // temporary cursor to hold a cursor position
296 // until setSelection is called!
297 LyXCursor start; // start of a REAL selection
298 LyXCursor end; // end of a REAL selection
300 bool set_; // former selection
301 bool mark_; // former mark_set
305 // this is used to handle XSelection events in the right manner
306 Selection xsel_cache;
308 /// needed for the toggling (cursor position on last selection made)
309 LyXCursor last_sel_cursor;
310 /// needed for toggling the selection in screen.C
311 LyXCursor toggle_cursor;
312 /// needed for toggling the selection in screen.C
313 LyXCursor toggle_end_cursor;
315 /// need the selection cursor:
318 void clearSelection();
320 string const selectionAsString(Buffer const *, bool label) const;
322 /// select the word we need depending on word_location
323 void getWord(LyXCursor & from, LyXCursor & to,
324 word_location const);
325 /// just selects the word the cursor is in
326 void selectWord(word_location const);
327 /// returns the inset at cursor (if it exists), 0 otherwise
328 Inset * getInset() const;
330 /// accept selected change
333 /// reject selected change
336 /** 'selects" the next word, where the cursor is not in
337 and returns this word as string. THe cursor will be moved
338 to the beginning of this word.
339 With SelectSelectedWord can this be highlighted really
341 WordLangTuple const selectNextWordToSpellcheck(float & value);
343 void selectSelectedWord();
344 /// returns true if par was empty and was removed
345 bool setCursor(Paragraph * par,
348 bool boundary = false);
350 void setCursor(LyXCursor &, Paragraph * par,
352 bool boundary = false);
354 void setCursorIntern(Paragraph * par,
357 bool boundary = false);
359 void setCurrentFont();
362 bool isBoundary(Buffer const *, Paragraph * par,
363 lyx::pos_type pos) const;
365 bool isBoundary(Buffer const *, Paragraph * par,
367 LyXFont const & font) const;
370 void setCursorFromCoordinates(int x, int y);
372 void setCursorFromCoordinates(LyXCursor &,
375 void cursorUp(bool selecting = false);
377 void cursorDown(bool selecting = false);
379 void cursorLeft(bool internal = true);
381 void cursorRight(bool internal = true);
383 void cursorLeftOneWord();
385 void cursorRightOneWord();
387 void cursorUpParagraph();
389 void cursorDownParagraph();
395 void cursorPrevious();
409 bool selectWordWhenUnderCursor(word_location);
415 text_capitalization = 1,
419 /// Change the case of the word at cursor position.
420 void changeCase(TextCase action);
422 void transposeChars();
427 void cutSelection(bool doclear = true, bool realcut = true);
429 void copySelection();
431 void pasteSelection();
433 void copyEnvironmentType();
435 void pasteEnvironmentType();
437 /** the DTP switches for paragraphs. LyX will store the top settings
438 always in the first physical paragraph, the bottom settings in the
439 last. When a paragraph is broken, the top settings rest, the bottom
440 settings are given to the new one. So I can make shure, they do not
441 duplicate themself (and you cannnot make dirty things with them! )
443 void setParagraph(bool line_top, bool line_bottom,
444 bool pagebreak_top, bool pagebreak_bottom,
445 VSpace const & space_top,
446 VSpace const & space_bottom,
447 Spacing const & spacing,
449 string const & labelwidthstring,
452 /* these things are for search and replace */
455 * Sets the selection from the current cursor position to length
456 * characters to the right. No safety checks.
458 void setSelectionRange(lyx::pos_type length);
460 /** simple replacing. The font of the first selected character
463 void replaceSelectionWithString(string const & str);
465 /// needed to insert the selection
466 void insertStringAsLines(string const & str);
467 /// needed to insert the selection
468 void insertStringAsParagraphs(string const & str);
470 /// Find next inset of some specified type.
471 bool gotoNextInset(std::vector<Inset::Code> const & codes,
472 string const & contents = string());
474 void gotoInset(std::vector<Inset::Code> const & codes,
477 void gotoInset(Inset::Code code, bool same_content);
480 /* for the greater insets */
482 /// returns false if inset wasn't found
483 bool updateInset(Inset *);
485 void checkParagraph(Paragraph * par, lyx::pos_type pos);
487 int workWidth() const;
489 int workWidth(Inset * inset) const;
492 void computeBidiTables(Buffer const *, Row * row) const;
493 /// Maps positions in the visual string to positions in logical string.
494 lyx::pos_type log2vis(lyx::pos_type pos) const;
495 /// Maps positions in the logical string to positions in visual string.
496 lyx::pos_type vis2log(lyx::pos_type pos) const;
498 lyx::pos_type bidi_level(lyx::pos_type pos) const;
500 bool bidi_InRange(lyx::pos_type pos) const;
508 void cursorLeftOneWord(LyXCursor &);
511 float getCursorX(Row *, lyx::pos_type pos,
512 lyx::pos_type last, bool boundary) const;
513 /// used in setlayout
514 void makeFontEntriesLayoutSpecific(Buffer const &, Paragraph & par);
516 /** forces the redrawing of a paragraph. Needed when manipulating a
519 void redoDrawingOfParagraph(LyXCursor const & cursor);
521 /** Copybuffer for copy environment type.
522 Asger has learned that this should be a buffer-property instead
523 Lgb has learned that 'char' is a lousy type for non-characters
525 string copylayouttype;
527 /** inserts a new row behind the specified row, increments
528 the touched counters */
529 void insertRow(Row * row, Paragraph * par, lyx::pos_type pos);
530 /// removes the row and reset the touched counters
531 void removeRow(Row * row);
533 /// remove all following rows of the paragraph of the specified row.
534 void removeParagraph(Row * row);
536 /// insert the specified paragraph behind the specified row
537 void insertParagraph(Paragraph * par, Row * row);
539 /** appends the implizit specified paragraph behind the specified row,
540 * start at the implizit given position */
541 void appendParagraph(Row * row);
544 void breakAgain(Row * row);
545 /// Calculate and set the height of the row
546 void setHeightOfRow(Row * row_ptr);
548 // fix the cursor `cur' after a characters has been deleted at `where'
549 // position. Called by deleteEmptyParagraphMechanism
550 void fixCursorAfterDelete(LyXCursor & cur,
551 LyXCursor const & where);
553 /// delete double space (false) or empty paragraphs (true) around old_cursor
554 bool deleteEmptyParagraphMechanism(LyXCursor const & old_cursor);
557 /** Updates all counters starting BEHIND the row. Changed paragraphs
558 * with a dynamic left margin will be rebroken. */
559 void updateCounters();
563 * Returns an inset if inset was hit, or 0 if not.
564 * If hit, the coordinates are changed relative to the inset.
566 Inset * checkInsetHit(int & x, int & y);
569 int singleWidth(Paragraph * par,
570 lyx::pos_type pos) const;
572 int singleWidth(Paragraph * par,
573 lyx::pos_type pos, char c) const;
575 /// return the color of the canvas
576 LColor::color backgroundColor() const;
579 mutable bool bidi_same_direction;
581 unsigned char transformChar(unsigned char c, Paragraph * par,
582 lyx::pos_type pos) const;
585 * Returns the left beginning of the text.
586 * This information cannot be taken from the layout object, because
587 * in LaTeX the beginning of the text fits in some cases
588 * (for example sections) exactly the label-width.
590 int leftMargin(Row const * row) const;
592 int rightMargin(Buffer const &, Row const & row) const;
594 /** this calculates the specified parameters. needed when setting
595 * the cursor and when creating a visible row */
596 void prepareToPrint(Row * row, float & x,
597 float & fill_separator,
599 float & fill_label_hfill,
600 bool bidi = true) const;
604 void setCounter(Buffer const *, Paragraph * par);
606 void deleteWordForward();
608 void deleteWordBackward();
610 void deleteLineForward();
613 * some low level functions
617 /// return the pos value *before* which a row should break.
618 /// for example, the pos at which IsNewLine(pos) == true
619 lyx::pos_type rowBreakPoint(Row const & row) const;
621 /// returns the minimum space a row needs on the screen in pixel
622 int fill(Row & row, int workwidth) const;
625 * returns the minimum space a manual label needs on the
628 int labelFill(Row const & row) const;
631 int labelEnd(Row const & row) const;
634 mutable std::vector<lyx::pos_type> log2vis_list;
636 mutable std::vector<lyx::pos_type> vis2log_list;
638 mutable std::vector<lyx::pos_type> bidi_levels;
640 mutable lyx::pos_type bidi_start;
642 mutable lyx::pos_type bidi_end;
648 // special owner functions
650 Paragraph * ownerParagraph() const;
652 void ownerParagraph(Paragraph *) const;
653 // set it searching first for the right owner using the paragraph id
654 void ownerParagraph(int id, Paragraph *) const;
656 /// return true if this is owned by an inset.
657 bool isInInset() const;
660 /// return the default height of a row in pixels, considering font zoom
661 extern int defaultRowHeight();