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
77 void init(BufferView *, bool reinit = false);
81 mutable unsigned int width;
82 /// the current font settings
83 mutable LyXFont current_font;
85 mutable LyXFont real_current_font;
87 /** the first visible row on screen
88 * declared mutable because removeRow is const
90 mutable Row * top_row_;
91 /** the pixel offset with respect to this row of top_y
92 * declared mutable because removeRow is const
94 mutable int top_row_offset_;
96 /// get the y coord. of the top of the screen (relative to doc start)
101 InsetText * inset_owner;
103 UpdatableInset * the_locking_inset;
106 int getRealCursorX(BufferView *) const;
108 LyXFont const getFont(Buffer const *, Paragraph * par,
109 lyx::pos_type pos) const;
111 LyXFont const getLayoutFont(Buffer const *, Paragraph * par) const;
113 LyXFont const getLabelFont(Buffer const *, Paragraph * par) const;
115 void setCharFont(Buffer const *, Paragraph * par,
116 lyx::pos_type pos, LyXFont const & font);
117 void setCharFont(BufferView *, Paragraph * par,
118 lyx::pos_type pos, LyXFont const & font, bool toggleall);
120 /// return true if the row changed
121 void markChangeInDraw(BufferView * bv, Row * row, Row * next);
123 void breakAgainOneRow(BufferView *, Row * row);
124 /// what you expect when pressing <enter> at cursor position
125 void breakParagraph(BufferView *,
126 ParagraphList & paragraphs, char keep_layout = 0);
128 /** set layout over selection and make a total rebreak of
131 Paragraph * setLayout(BufferView *, LyXCursor & actual_cursor,
132 LyXCursor & selection_start,
133 LyXCursor & selection_end,
134 string const & layout);
136 void setLayout(BufferView *, string const & layout);
138 /** increment depth over selection and make a total rebreak of those
141 void incDepth(BufferView *);
143 /** decrement depth over selection and make a total rebreak of those
145 void decDepth(BufferView *);
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(BufferView *, 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(BufferView *, LyXCursor const & cursor,
162 Paragraph const * end_par) const;
164 void redoParagraph(BufferView *) const;
167 void toggleFree(BufferView *, LyXFont const &, bool toggleall = false);
170 string getStringToIndex(BufferView *);
172 /** recalculates the heights of all previous rows of the
173 specified paragraph. needed, if the last characters font
176 void redoHeightOfParagraph(BufferView *);
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(BufferView *, char c);
183 void insertInset(BufferView *, Inset * inset);
185 /// Completes the insertion with a full rebreak
186 void fullRebreak(BufferView *);
189 mutable Row * need_break_row;
191 * The pixel y position from which to repaint the screen.
192 * The position is absolute along the height of outermost
193 * lyxtext (I think). NEED_MORE_REFRESH and NEED_LITTLE_REFRESH
194 * repaints both use this as a starting point (if it's within
195 * the viewable portion of the lyxtext).
197 mutable int refresh_y;
199 * The row from which to repaint the screen, used by screen.c.
200 * This must be set if the pending update is NEED_LITTLE_REFRESH.
201 * It doesn't make any difference for NEED_MORE_REFRESH.
203 mutable Row * refresh_row;
206 * Return the status. This represents what repaints are
207 * pending after some operation (e.g. inserting a char).
209 text_status status() const;
211 /// Set the status to make a paint pending.
212 void status(BufferView *, text_status) const;
214 /// clear any pending paints
217 /// post notice that we changed during a draw
218 void postChangedInDraw();
221 * Mark position y as the starting point for a repaint
223 void postPaint(BufferView & bv, int start_y);
226 * Mark the given row at position y as needing a repaint.
228 void postRowPaint(BufferView & bv, Row * row, int start_y);
231 Inset::RESULT dispatch(FuncRequest const & cmd);
234 /// only the top-level LyXText has this non-zero
235 BufferView * bv_owner;
237 /** wether the screen needs a refresh,
238 starting with refresh_y
240 mutable text_status status_;
243 /** returns a pointer to the row near the specified y-coordinate
244 (relative to the whole text). y is set to the real beginning
247 Row * getRowNearY(int & y) const;
249 /** returns the column near the specified x-coordinate of the row
250 x is set to the real beginning of this column
252 lyx::pos_type getColumnNearX(BufferView *, Row * row,
253 int & x, bool & boundary) const;
255 /** returns a pointer to a specified row. y is set to the beginning
258 Row * getRow(Paragraph * par, lyx::pos_type pos, int & y) const;
259 /** returns the firstrow, this could be done with the above too but
260 IMO it's stupid to have to allocate a dummy y all the time I need
263 Row * firstRow() { return firstrow; }
266 Later this variable has to be removed. There should be now internal
267 cursor in a text (and thus not in a buffer). By keeping this it is
268 (I think) impossible to have several views with the same buffer, but
269 the cursor placed at different places.
271 Since the LyXText now has been moved from Buffer to BufferView
272 it should not be absolutely needed to move the cursor...
274 mutable LyXCursor cursor; // actual cursor position
276 /** The structure that keeps track of the selections set. */
279 : set_(false), mark_(false)
293 LyXCursor cursor; // temporary cursor to hold a cursor position
294 // until setSelection is called!
295 LyXCursor start; // start of a REAL selection
296 LyXCursor end; // end of a REAL selection
298 bool set_; // former selection
299 bool mark_; // former mark_set
302 mutable Selection selection;
303 // this is used to handle XSelection events in the right manner
304 mutable Selection xsel_cache;
306 /// needed for the toggling (cursor position on last selection made)
307 mutable LyXCursor last_sel_cursor;
308 /// needed for toggling the selection in screen.C
309 mutable LyXCursor toggle_cursor;
310 /// needed for toggling the selection in screen.C
311 mutable LyXCursor toggle_end_cursor;
313 /// need the selection cursor:
314 void setSelection(BufferView *);
316 void clearSelection() const;
318 string const selectionAsString(Buffer const *, bool label) const;
320 /// select the word we need depending on word_location
321 void getWord(LyXCursor & from, LyXCursor & to,
322 word_location const) const;
323 /// just selects the word the cursor is in
324 void selectWord(BufferView *, word_location const);
325 /// returns the inset at cursor (if it exists), 0 otherwise
326 Inset * getInset() const;
328 /// accept selected change
329 void acceptChange(BufferView * bv);
331 /// reject selected change
332 void rejectChange(BufferView * bv);
334 /** 'selects" the next word, where the cursor is not in
335 and returns this word as string. THe cursor will be moved
336 to the beginning of this word.
337 With SelectSelectedWord can this be highlighted really
339 WordLangTuple const selectNextWordToSpellcheck(BufferView *, float & value) const;
341 void selectSelectedWord(BufferView *);
342 /// returns true if par was empty and was removed
343 bool setCursor(BufferView *, Paragraph * par,
346 bool boundary = false) const;
348 void setCursor(BufferView *, LyXCursor &, Paragraph * par,
350 bool boundary = false) const;
352 void setCursorIntern(BufferView *, Paragraph * par,
355 bool boundary = false) const;
357 void setCurrentFont(BufferView *) const;
360 bool isBoundary(Buffer const *, Paragraph * par,
361 lyx::pos_type pos) const;
363 bool isBoundary(Buffer const *, Paragraph * par,
365 LyXFont const & font) const;
368 void setCursorFromCoordinates(BufferView *, int x, int y) const;
370 void setCursorFromCoordinates(BufferView *, LyXCursor &,
373 void cursorUp(BufferView *, bool selecting = false) const;
375 void cursorDown(BufferView *, bool selecting = false) const;
377 void cursorLeft(BufferView *, bool internal = true) const;
379 void cursorRight(BufferView *, bool internal = true) const;
381 void cursorLeftOneWord(BufferView *) const;
383 void cursorRightOneWord(BufferView *) const;
385 void cursorUpParagraph(BufferView *) const;
387 void cursorDownParagraph(BufferView *) const;
389 void cursorHome(BufferView *) const;
391 void cursorEnd(BufferView *) const;
393 void cursorPrevious(BufferView * bv);
395 void cursorNext(BufferView * bv);
397 void cursorTab(BufferView *) const;
399 void cursorTop(BufferView *) const;
401 void cursorBottom(BufferView *) const;
403 void Delete(BufferView *);
405 void backspace(BufferView *);
407 bool selectWordWhenUnderCursor(BufferView *,
408 word_location const);
414 text_capitalization = 1,
418 /// Change the case of the word at cursor position.
419 void changeCase(BufferView &, TextCase action);
421 void transposeChars(BufferView &);
424 void toggleInset(BufferView *);
426 void cutSelection(BufferView *, bool doclear = true, bool realcut = true);
428 void copySelection(BufferView *);
430 void pasteSelection(BufferView *);
432 void copyEnvironmentType();
434 void pasteEnvironmentType(BufferView *);
436 /** the DTP switches for paragraphs. LyX will store the top settings
437 always in the first physical paragraph, the bottom settings in the
438 last. When a paragraph is broken, the top settings rest, the bottom
439 settings are given to the new one. So I can make shure, they do not
440 duplicate themself (and you cannnot make dirty things with them! )
442 void setParagraph(BufferView *,
443 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 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(BufferView *, lyx::pos_type length);
460 /** simple replacing. The font of the first selected character
463 void replaceSelectionWithString(BufferView *, string const & str);
465 /// needed to insert the selection
466 void insertStringAsLines(BufferView *, string const & str);
467 /// needed to insert the selection
468 void insertStringAsParagraphs(BufferView *, string const & str);
470 /// Find next inset of some specified type.
471 bool gotoNextInset(BufferView *, std::vector<Inset::Code> const & codes,
472 string const & contents = string()) const;
474 void gotoInset(BufferView * bv, std::vector<Inset::Code> const & codes,
477 void gotoInset(BufferView * bv, Inset::Code code, bool same_content);
480 /* for the greater insets */
482 /// returns false if inset wasn't found
483 bool updateInset(BufferView *, Inset *);
485 void checkParagraph(BufferView *, Paragraph * par, lyx::pos_type pos);
487 int workWidth(BufferView &) const;
489 int workWidth(BufferView &, Inset * inset) const;
491 void computeBidiTables(Buffer const *, Row * row) const;
493 /// Maps positions in the visual string to positions in logical string.
495 lyx::pos_type log2vis(lyx::pos_type pos) const {
496 if (bidi_start == -1)
499 return log2vis_list[pos-bidi_start];
502 /// Maps positions in the logical string to positions in visual string.
504 lyx::pos_type vis2log(lyx::pos_type pos) const {
505 if (bidi_start == -1)
508 return vis2log_list[pos-bidi_start];
512 lyx::pos_type bidi_level(lyx::pos_type pos) const {
513 if (bidi_start == -1)
516 return bidi_levels[pos-bidi_start];
520 bool bidi_InRange(lyx::pos_type pos) const {
521 return bidi_start == -1 ||
522 (bidi_start <= pos && pos <= bidi_end);
526 mutable Row * firstrow;
528 mutable Row * lastrow;
531 void cursorLeftOneWord(LyXCursor &) const;
534 float getCursorX(BufferView *, Row *, lyx::pos_type pos,
535 lyx::pos_type last, bool boundary) const;
536 /// used in setlayout
537 void makeFontEntriesLayoutSpecific(Buffer const &, Paragraph & par);
539 /** forces the redrawing of a paragraph. Needed when manipulating a
542 void redoDrawingOfParagraph(BufferView *, LyXCursor const & cursor);
544 /** Copybuffer for copy environment type.
545 Asger has learned that this should be a buffer-property instead
546 Lgb has learned that 'char' is a lousy type for non-characters
548 string copylayouttype;
550 /** inserts a new row behind the specified row, increments
551 the touched counters */
552 void insertRow(Row * row, Paragraph * par, lyx::pos_type pos) const;
553 /// removes the row and reset the touched counters
554 void removeRow(Row * row) const;
556 /// remove all following rows of the paragraph of the specified row.
557 void removeParagraph(Row * row) const;
559 /// insert the specified paragraph behind the specified row
560 void insertParagraph(BufferView *,
561 Paragraph * par, Row * row) const;
563 /** appends the implizit specified paragraph behind the specified row,
564 * start at the implizit given position */
565 void appendParagraph(BufferView *, Row * row) const;
568 void breakAgain(BufferView *, Row * row) const;
569 /// Calculate and set the height of the row
570 void setHeightOfRow(BufferView *, Row * row_ptr) const;
572 // fix the cursor `cur' after a characters has been deleted at `where'
573 // position. Called by deleteEmptyParagraphMechanism
574 void fixCursorAfterDelete(BufferView * bv,
576 LyXCursor const & where) const;
578 /// delete double space (false) or empty paragraphs (true) around old_cursor
579 bool deleteEmptyParagraphMechanism(BufferView *,
580 LyXCursor const & old_cursor) const;
583 /** Updates all counters starting BEHIND the row. Changed paragraphs
584 * with a dynamic left margin will be rebroken. */
585 void updateCounters(BufferView *) const;
587 void update(BufferView * bv, bool changed = true);
589 * Returns an inset if inset was hit, or 0 if not.
590 * If hit, the coordinates are changed relative to the inset.
592 Inset * checkInsetHit(BufferView * bv, int & x, int & y) const;
595 int singleWidth(BufferView *, Paragraph * par,
596 lyx::pos_type pos) const;
598 int singleWidth(BufferView *, Paragraph * par,
599 lyx::pos_type pos, char c) const;
601 /// return the color of the canvas
602 LColor::color backgroundColor() const;
605 mutable bool bidi_same_direction;
607 unsigned char transformChar(unsigned char c, Paragraph * par,
608 lyx::pos_type pos) const;
611 * Returns the left beginning of the text.
612 * This information cannot be taken from the layout object, because
613 * in LaTeX the beginning of the text fits in some cases
614 * (for example sections) exactly the label-width.
616 int leftMargin(BufferView *, Row const * row) const;
618 int rightMargin(Buffer const &, Row const & row) const;
620 /** this calculates the specified parameters. needed when setting
621 * the cursor and when creating a visible row */
622 void prepareToPrint(BufferView *, Row * row, float & x,
623 float & fill_separator,
625 float & fill_label_hfill,
626 bool bidi = true) const;
630 void setCounter(Buffer const *, Paragraph * par) const;
632 void deleteWordForward(BufferView *);
634 void deleteWordBackward(BufferView *);
636 void deleteLineForward(BufferView *);
639 * some low level functions
643 /// return the pos value *before* which a row should break.
644 /// for example, the pos at which IsNewLine(pos) == true
645 lyx::pos_type rowBreakPoint(BufferView &, Row const & row) const;
647 /// returns the minimum space a row needs on the screen in pixel
648 int fill(BufferView &, Row & row, int workwidth) const;
651 * returns the minimum space a manual label needs on the
654 int labelFill(BufferView &, Row const & row) const;
657 int labelEnd(BufferView &, Row const & row) const;
660 mutable std::vector<lyx::pos_type> log2vis_list;
663 mutable std::vector<lyx::pos_type> vis2log_list;
666 mutable std::vector<lyx::pos_type> bidi_levels;
669 mutable lyx::pos_type bidi_start;
672 mutable lyx::pos_type bidi_end;
678 // special owner functions
680 Paragraph * ownerParagraph() const;
682 void ownerParagraph(Paragraph *) const;
683 // set it searching first for the right owner using the paragraph id
684 void ownerParagraph(int id, Paragraph *) const;
686 /// return true if this is the outer-most lyxtext
687 bool isTopLevel() const;
689 /// return true if this is owned by an inset. FIXME: why the difference
690 /// with isTopLevel() ??
691 bool isInInset() const;
694 /// return the default height of a row in pixels, considering font zoom
695 extern int defaultRowHeight();