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.
46 NEED_VERY_LITTLE_REFRESH = 2,
52 // the word around the cursor, only if the cursor is
55 // the word around the cursor
57 /// the word begining from the cursor position
59 /// the word around the cursor or before the cursor
61 /// the next word (not yet used)
66 LyXText(BufferView *);
67 /// sets inset as owner
73 void init(BufferView *, bool reinit = false);
77 mutable unsigned int width;
78 /// the current font settings
79 mutable LyXFont current_font;
81 mutable LyXFont real_current_font;
83 /** the first visible row on screen
84 * declared mutable because removeRow is const
86 mutable Row * top_row_;
87 /** the pixel offset with respect to this row of top_y
88 * declared mutable because removeRow is const
90 mutable int top_row_offset_;
92 /// get the y coord. of the top of the screen (relative to doc start)
97 InsetText * inset_owner;
99 UpdatableInset * the_locking_inset;
102 int getRealCursorX(BufferView *) const;
104 LyXFont const getFont(Buffer const *, Paragraph * par,
105 lyx::pos_type pos) const;
107 LyXFont const getLayoutFont(Buffer const *, Paragraph * par) const;
109 LyXFont const getLabelFont(Buffer const *, Paragraph * par) const;
111 void setCharFont(Buffer const *, Paragraph * par,
112 lyx::pos_type pos, LyXFont const & font);
113 void setCharFont(BufferView *, Paragraph * par,
114 lyx::pos_type pos, LyXFont const & font, bool toggleall);
116 /// return true if the row changed
117 void markChangeInDraw(BufferView * bv, Row * row, Row * next);
119 void breakAgainOneRow(BufferView *, Row * row);
120 /// what you expect when pressing <enter> at cursor position
121 void breakParagraph(BufferView *,
122 ParagraphList & paragraphs, char keep_layout = 0);
124 /** set layout over selection and make a total rebreak of
127 Paragraph * setLayout(BufferView *, LyXCursor & actual_cursor,
128 LyXCursor & selection_start,
129 LyXCursor & selection_end,
130 string const & layout);
132 void setLayout(BufferView *, string const & layout);
134 /** increment depth over selection and make a total rebreak of those
137 void incDepth(BufferView *);
139 /** decrement depth over selection and make a total rebreak of those
141 void decDepth(BufferView *);
143 /// get the depth at current cursor position
144 int getDepth() const;
146 /** set font over selection and make a total rebreak of those
148 toggleall defaults to false.
150 void setFont(BufferView *, LyXFont const &, bool toggleall = false);
152 /** deletes and inserts again all paragaphs between the cursor
153 and the specified par. The Cursor is needed to set the refreshing
155 This function is needed after SetLayout and SetFont etc.
157 void redoParagraphs(BufferView *, LyXCursor const & cursor,
158 Paragraph const * end_par) const;
160 void redoParagraph(BufferView *) const;
163 void toggleFree(BufferView *, LyXFont const &, bool toggleall = false);
166 string getStringToIndex(BufferView *);
168 /** recalculates the heights of all previous rows of the
169 specified paragraph. needed, if the last characters font
172 void redoHeightOfParagraph(BufferView *, LyXCursor const & cursor);
174 /** insert a character, moves all the following breaks in the
175 same Paragraph one to the right and make a little rebreak
177 void insertChar(BufferView *, char c);
179 void insertInset(BufferView *, Inset * inset);
181 /// Completes the insertion with a full rebreak
182 void fullRebreak(BufferView *);
185 mutable Row * need_break_row;
187 mutable int refresh_y;
189 mutable Row * refresh_row;
191 /// give and set the LyXText status
192 text_status status() const;
194 void status(BufferView *, text_status) const;
197 Inset::RESULT dispatch(FuncRequest const & cmd);
200 /// only the top-level LyXText has this non-zero
201 BufferView * bv_owner;
203 /** wether the screen needs a refresh,
204 starting with refresh_y
206 mutable text_status status_;
209 /** returns a pointer to the row near the specified y-coordinate
210 (relative to the whole text). y is set to the real beginning
213 Row * getRowNearY(int & y) const;
215 /** returns the column near the specified x-coordinate of the row
216 x is set to the real beginning of this column
218 lyx::pos_type getColumnNearX(BufferView *, Row * row,
219 int & x, bool & boundary) const;
221 /** returns a pointer to a specified row. y is set to the beginning
224 Row * getRow(Paragraph * par, lyx::pos_type pos, int & y) const;
225 /** returns the firstrow, this could be done with the above too but
226 IMO it's stupid to have to allocate a dummy y all the time I need
229 Row * firstRow() { return firstrow; }
232 Later this variable has to be removed. There should be now internal
233 cursor in a text (and thus not in a buffer). By keeping this it is
234 (I think) impossible to have several views with the same buffer, but
235 the cursor placed at different places.
237 Since the LyXText now has been moved from Buffer to BufferView
238 it should not be absolutely needed to move the cursor...
240 mutable LyXCursor cursor; // actual cursor position
242 /** The structure that keeps track of the selections set. */
245 : set_(false), mark_(false)
259 LyXCursor cursor; // temporary cursor to hold a cursor position
260 // until setSelection is called!
261 LyXCursor start; // start of a REAL selection
262 LyXCursor end; // end of a REAL selection
264 bool set_; // former selection
265 bool mark_; // former mark_set
268 mutable Selection selection;
269 // this is used to handle XSelection events in the right manner
270 mutable Selection xsel_cache;
272 /// needed for the toggling (cursor position on last selection made)
273 mutable LyXCursor last_sel_cursor;
274 /// needed for toggling the selection in screen.C
275 mutable LyXCursor toggle_cursor;
276 /// needed for toggling the selection in screen.C
277 mutable LyXCursor toggle_end_cursor;
279 /// need the selection cursor:
280 void setSelection(BufferView *);
282 void clearSelection() const;
284 string const selectionAsString(Buffer const *, bool label) const;
286 /// select the word we need depending on word_location
287 void getWord(LyXCursor & from, LyXCursor & to,
288 word_location const) const;
289 /// just selects the word the cursor is in
290 void selectWord(BufferView *, word_location const);
291 /// returns the inset at cursor (if it exists), 0 otherwise
292 Inset * getInset() const;
294 /// accept selected change
295 void acceptChange(BufferView * bv);
297 /// reject selected change
298 void rejectChange(BufferView * bv);
300 /** 'selects" the next word, where the cursor is not in
301 and returns this word as string. THe cursor will be moved
302 to the beginning of this word.
303 With SelectSelectedWord can this be highlighted really
305 WordLangTuple const selectNextWordToSpellcheck(BufferView *, float & value) const;
307 void selectSelectedWord(BufferView *);
308 /// returns true if par was empty and was removed
309 bool setCursor(BufferView *, Paragraph * par,
312 bool boundary = false) const;
314 void setCursor(BufferView *, LyXCursor &, Paragraph * par,
316 bool boundary = false) const;
318 void setCursorIntern(BufferView *, Paragraph * par,
321 bool boundary = false) const;
323 void setCurrentFont(BufferView *) const;
326 bool isBoundary(Buffer const *, Paragraph * par,
327 lyx::pos_type pos) const;
329 bool isBoundary(Buffer const *, Paragraph * par,
331 LyXFont const & font) const;
334 void setCursorFromCoordinates(BufferView *, int x, int y) const;
336 void setCursorFromCoordinates(BufferView *, LyXCursor &,
339 void cursorUp(BufferView *, bool selecting = false) const;
341 void cursorDown(BufferView *, bool selecting = false) const;
343 void cursorLeft(BufferView *, bool internal = true) const;
345 void cursorRight(BufferView *, bool internal = true) const;
347 void cursorLeftOneWord(BufferView *) const;
349 void cursorRightOneWord(BufferView *) const;
351 void cursorUpParagraph(BufferView *) const;
353 void cursorDownParagraph(BufferView *) const;
355 void cursorHome(BufferView *) const;
357 void cursorEnd(BufferView *) const;
359 void cursorPrevious(BufferView * bv);
361 void cursorNext(BufferView * bv);
363 void cursorTab(BufferView *) const;
365 void cursorTop(BufferView *) const;
367 void cursorBottom(BufferView *) const;
369 void Delete(BufferView *);
371 void backspace(BufferView *);
373 bool selectWordWhenUnderCursor(BufferView *,
374 word_location const);
380 text_capitalization = 1,
384 /// Change the case of the word at cursor position.
385 void changeCase(BufferView &, TextCase action);
387 void transposeChars(BufferView &);
390 void toggleInset(BufferView *);
392 void cutSelection(BufferView *, bool doclear = true, bool realcut = true);
394 void copySelection(BufferView *);
396 void pasteSelection(BufferView *);
398 void copyEnvironmentType();
400 void pasteEnvironmentType(BufferView *);
402 /** the DTP switches for paragraphs. LyX will store the top settings
403 always in the first physical paragraph, the bottom settings in the
404 last. When a paragraph is broken, the top settings rest, the bottom
405 settings are given to the new one. So I can make shure, they do not
406 duplicate themself (and you cannnot make dirty things with them! )
408 void setParagraph(BufferView *,
409 bool line_top, bool line_bottom,
410 bool pagebreak_top, bool pagebreak_bottom,
411 VSpace const & space_top,
412 VSpace const & space_bottom,
413 Spacing const & spacing,
415 string labelwidthstring,
418 /* these things are for search and replace */
421 * Sets the selection from the current cursor position to length
422 * characters to the right. No safety checks.
424 void setSelectionRange(BufferView *, lyx::pos_type length);
426 /** simple replacing. The font of the first selected character
429 void replaceSelectionWithString(BufferView *, string const & str);
431 /// needed to insert the selection
432 void insertStringAsLines(BufferView *, string const & str);
433 /// needed to insert the selection
434 void insertStringAsParagraphs(BufferView *, string const & str);
436 /// Find next inset of some specified type.
437 bool gotoNextInset(BufferView *, std::vector<Inset::Code> const & codes,
438 string const & contents = string()) const;
440 void gotoInset(BufferView * bv, std::vector<Inset::Code> const & codes,
443 void gotoInset(BufferView * bv, Inset::Code code, bool same_content);
446 /* for the greater insets */
448 /// returns false if inset wasn't found
449 bool updateInset(BufferView *, Inset *);
451 void checkParagraph(BufferView *, Paragraph * par, lyx::pos_type pos);
453 int workWidth(BufferView &) const;
455 int workWidth(BufferView &, Inset * inset) const;
457 void computeBidiTables(Buffer const *, Row * row) const;
459 /// Maps positions in the visual string to positions in logical string.
461 lyx::pos_type log2vis(lyx::pos_type pos) const {
462 if (bidi_start == -1)
465 return log2vis_list[pos-bidi_start];
468 /// Maps positions in the logical string to positions in visual string.
470 lyx::pos_type vis2log(lyx::pos_type pos) const {
471 if (bidi_start == -1)
474 return vis2log_list[pos-bidi_start];
478 lyx::pos_type bidi_level(lyx::pos_type pos) const {
479 if (bidi_start == -1)
482 return bidi_levels[pos-bidi_start];
486 bool bidi_InRange(lyx::pos_type pos) const {
487 return bidi_start == -1 ||
488 (bidi_start <= pos && pos <= bidi_end);
492 mutable Row * firstrow;
494 mutable Row * lastrow;
497 void cursorLeftOneWord(LyXCursor &) const;
500 float getCursorX(BufferView *, Row *, lyx::pos_type pos,
501 lyx::pos_type last, bool boundary) const;
502 /// used in setlayout
503 void makeFontEntriesLayoutSpecific(Buffer const &, Paragraph & par);
505 /** forces the redrawing of a paragraph. Needed when manipulating a
508 void redoDrawingOfParagraph(BufferView *, LyXCursor const & cursor);
510 /** Copybuffer for copy environment type.
511 Asger has learned that this should be a buffer-property instead
512 Lgb has learned that 'char' is a lousy type for non-characters
514 string copylayouttype;
516 /** inserts a new row behind the specified row, increments
517 the touched counters */
518 void insertRow(Row * row, Paragraph * par, lyx::pos_type pos) const;
519 /// removes the row and reset the touched counters
520 void removeRow(Row * row) const;
522 /// remove all following rows of the paragraph of the specified row.
523 void removeParagraph(Row * row) const;
525 /// insert the specified paragraph behind the specified row
526 void insertParagraph(BufferView *,
527 Paragraph * par, Row * row) const;
529 /** appends the implizit specified paragraph behind the specified row,
530 * start at the implizit given position */
531 void appendParagraph(BufferView *, Row * row) const;
534 void breakAgain(BufferView *, Row * row) const;
535 /// Calculate and set the height of the row
536 void setHeightOfRow(BufferView *, Row * row_ptr) const;
538 // fix the cursor `cur' after a characters has been deleted at `where'
539 // position. Called by deleteEmptyParagraphMechanism
540 void fixCursorAfterDelete(BufferView * bv,
542 LyXCursor const & where) const;
544 /// delete double space (false) or empty paragraphs (true) around old_cursor
545 bool deleteEmptyParagraphMechanism(BufferView *,
546 LyXCursor const & old_cursor) const;
549 /** Updates all counters starting BEHIND the row. Changed paragraphs
550 * with a dynamic left margin will be rebroken. */
551 void updateCounters(BufferView *) const;
553 void update(BufferView * bv, bool changed = true);
555 * Returns an inset if inset was hit, or 0 if not.
556 * If hit, the coordinates are changed relative to the inset.
558 Inset * checkInsetHit(BufferView * bv, int & x, int & y) const;
561 int singleWidth(BufferView *, Paragraph * par,
562 lyx::pos_type pos) const;
564 int singleWidth(BufferView *, Paragraph * par,
565 lyx::pos_type pos, char c) const;
567 /// return the color of the canvas
568 LColor::color backgroundColor() const;
571 mutable bool bidi_same_direction;
573 unsigned char transformChar(unsigned char c, Paragraph * par,
574 lyx::pos_type pos) const;
577 * Returns the left beginning of the text.
578 * This information cannot be taken from the layout object, because
579 * in LaTeX the beginning of the text fits in some cases
580 * (for example sections) exactly the label-width.
582 int leftMargin(BufferView *, Row const * row) const;
584 int rightMargin(Buffer const &, Row const & row) const;
586 /** this calculates the specified parameters. needed when setting
587 * the cursor and when creating a visible row */
588 void prepareToPrint(BufferView *, Row * row, float & x,
589 float & fill_separator,
591 float & fill_label_hfill,
592 bool bidi = true) const;
596 void setCounter(Buffer const *, Paragraph * par) const;
598 void deleteWordForward(BufferView *);
600 void deleteWordBackward(BufferView *);
602 void deleteLineForward(BufferView *);
605 * some low level functions
609 /// get the next breakpoint in a given paragraph
610 lyx::pos_type nextBreakPoint(BufferView *, Row const * row, int width) const;
611 /// returns the minimum space a row needs on the screen in pixel
612 int fill(BufferView &, Row & row, int workwidth) const;
615 * returns the minimum space a manual label needs on the
618 int labelFill(BufferView &, Row const & row) const;
621 int labelEnd(BufferView &, Row const & row) const;
624 mutable std::vector<lyx::pos_type> log2vis_list;
627 mutable std::vector<lyx::pos_type> vis2log_list;
630 mutable std::vector<lyx::pos_type> bidi_levels;
633 mutable lyx::pos_type bidi_start;
636 mutable lyx::pos_type bidi_end;
642 // special owner functions
644 Paragraph * ownerParagraph() const;
646 void ownerParagraph(Paragraph *) const;
647 // set it searching first for the right owner using the paragraph id
648 void ownerParagraph(int id, Paragraph *) const;
650 /// return true if this is the outer-most lyxtext
651 bool isTopLevel() const;
653 /// return true if this is owned by an inset. FIXME: why the difference
654 /// with isTopLevel() ??
655 bool isInInset() const;
658 /// return the default height of a row in pixels, considering font zoom
659 extern int defaultRowHeight();