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"
36 This class holds the mapping between buffer paragraphs and screen rows.
40 /// what repainting is needed
42 /// no repaint is needed
44 /// the refresh_row needs repainting
46 /// everything from refresh_y downwards needs repainting
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
68 LyXText(BufferView *, InsetText *);
70 void init(BufferView *, bool reinit = false);
75 /// the current font settings
78 LyXFont real_current_font;
80 /** the 'anchor' row: the position of this row remains constant
81 * with respect to the top of the screen
84 /** the pixel offset with respect to this row of top_y
86 int anchor_row_offset_;
88 /// get the y coord. of the top of the screen (relative to doc start)
90 /// set the y coord. of the top of the screen (relative to doc start)
92 /// set the anchoring row. top_y will be computed relative to this
93 void anchor_row(Row * row);
95 InsetText * inset_owner;
97 UpdatableInset * the_locking_inset;
100 int getRealCursorX() const;
102 LyXFont const getFont(Buffer const *, Paragraph * par,
103 lyx::pos_type pos) const;
105 LyXFont const getLayoutFont(Buffer const *, Paragraph * par) const;
107 LyXFont const getLabelFont(Buffer const *, Paragraph * par) const;
109 void setCharFont(Buffer const *, Paragraph * par,
110 lyx::pos_type pos, LyXFont const & font);
111 void setCharFont(Paragraph * par,
112 lyx::pos_type pos, LyXFont const & font, bool toggleall);
115 void breakAgainOneRow(Row * row);
116 /// what you expect when pressing <enter> at cursor position
117 void breakParagraph(ParagraphList & paragraphs, char keep_layout = 0);
119 /** set layout over selection and make a total rebreak of
122 Paragraph * setLayout(LyXCursor & actual_cursor,
123 LyXCursor & selection_start,
124 LyXCursor & selection_end,
125 string const & layout);
127 void setLayout(string const & layout);
129 /** increment depth over selection and make a total rebreak of those
134 /** decrement depth over selection and make a total rebreak of those
138 /// get the depth at current cursor position
139 int getDepth() const;
141 /** set font over selection and make a total rebreak of those
143 toggleall defaults to false.
145 void setFont(LyXFont const &, bool toggleall = false);
147 /** deletes and inserts again all paragaphs between the cursor
148 and the specified par. The Cursor is needed to set the refreshing
150 This function is needed after SetLayout and SetFont etc.
152 void redoParagraphs(LyXCursor const & cursor,
153 Paragraph const * end_par);
155 void redoParagraph();
158 void toggleFree(LyXFont const &, bool toggleall = false);
161 string getStringToIndex();
163 /** recalculates the heights of all previous rows of the
164 specified paragraph. needed, if the last characters font
167 void redoHeightOfParagraph();
169 /** insert a character, moves all the following breaks in the
170 same Paragraph one to the right and make a little rebreak
172 void insertChar(char c);
174 void insertInset(Inset * inset);
176 /// Completes the insertion with a full rebreak
180 Row * need_break_row;
182 /// clear any pending paints
186 * Mark position y as the starting point for a repaint
188 void postPaint(int start_y);
191 * Mark the given row at position y as needing a repaint.
193 void postRowPaint(Row * row, int start_y);
196 Inset::RESULT dispatch(FuncRequest const & cmd);
200 BufferView * bv() const;
202 friend class LyXScreen;
205 * Return the status. This represents what repaints are
206 * pending after some operation (e.g. inserting a char).
208 refresh_status refreshStatus() const;
212 * The pixel y position from which to repaint the screen.
213 * The position is absolute along the height of outermost
214 * lyxtext (I think). REFRESH_AREA and REFRESH_ROW
215 * repaints both use this as a starting point (if it's within
216 * the viewable portion of the lyxtext).
220 * The row from which to repaint the screen, used by screen.c.
221 * This must be set if the pending update is REFRESH_ROW.
222 * It doesn't make any difference for REFRESH_AREA.
226 refresh_status refresh_status_;
228 /// only the top-level LyXText has this non-zero
229 BufferView * bv_owner;
232 /** returns a pointer to the row near the specified y-coordinate
233 (relative to the whole text). y is set to the real beginning
236 Row * getRowNearY(int & y) const;
238 /** returns the column near the specified x-coordinate of the row
239 x is set to the real beginning of this column
241 lyx::pos_type getColumnNearX(Row * row,
242 int & x, bool & boundary) const;
244 /** returns a pointer to a specified row. y is set to the beginning
247 Row * getRow(Paragraph * par, lyx::pos_type pos, int & y) const;
248 /** returns the firstrow, this could be done with the above too but
249 IMO it's stupid to have to allocate a dummy y all the time I need
252 Row * firstRow() const { return &*rowlist_.begin(); }
253 Row * lastRow() const { return &const_cast<LyXText*>(this)->rowlist_.back(); }
255 Later this variable has to be removed. There should be now internal
256 cursor in a text (and thus not in a buffer). By keeping this it is
257 (I think) impossible to have several views with the same buffer, but
258 the cursor placed at different places.
260 Since the LyXText now has been moved from Buffer to BufferView
261 it should not be absolutely needed to move the cursor...
263 LyXCursor cursor; // actual cursor position
265 /** The structure that keeps track of the selections set. */
268 : set_(false), mark_(false)
282 LyXCursor cursor; // temporary cursor to hold a cursor position
283 // until setSelection is called!
284 LyXCursor start; // start of a REAL selection
285 LyXCursor end; // end of a REAL selection
287 bool set_; // former selection
288 bool mark_; // former mark_set
292 // this is used to handle XSelection events in the right manner
293 Selection xsel_cache;
295 /// needed for the toggling (cursor position on last selection made)
296 LyXCursor last_sel_cursor;
297 /// needed for toggling the selection in screen.C
298 LyXCursor toggle_cursor;
299 /// needed for toggling the selection in screen.C
300 LyXCursor toggle_end_cursor;
302 /// need the selection cursor:
305 void clearSelection();
307 string const selectionAsString(Buffer const *, bool label) const;
309 /// select the word we need depending on word_location
310 void getWord(LyXCursor & from, LyXCursor & to,
311 word_location const);
312 /// just selects the word the cursor is in
313 void selectWord(word_location const);
314 /// returns the inset at cursor (if it exists), 0 otherwise
315 Inset * getInset() const;
317 /// accept selected change
320 /// reject selected change
323 /** 'selects" the next word, where the cursor is not in
324 and returns this word as string. THe cursor will be moved
325 to the beginning of this word.
326 With SelectSelectedWord can this be highlighted really
328 WordLangTuple const selectNextWordToSpellcheck(float & value);
330 void selectSelectedWord();
331 /// returns true if par was empty and was removed
332 bool setCursor(Paragraph * par,
335 bool boundary = false);
337 void setCursor(LyXCursor &, Paragraph * par,
339 bool boundary = false);
341 void setCursorIntern(Paragraph * par,
344 bool boundary = false);
346 void setCurrentFont();
349 bool isBoundary(Buffer const *, Paragraph * par,
350 lyx::pos_type pos) const;
352 bool isBoundary(Buffer const *, Paragraph * par,
354 LyXFont const & font) const;
357 void setCursorFromCoordinates(int x, int y);
359 void setCursorFromCoordinates(LyXCursor &,
362 void cursorUp(bool selecting = false);
364 void cursorDown(bool selecting = false);
366 void cursorLeft(bool internal = true);
368 void cursorRight(bool internal = true);
370 void cursorLeftOneWord();
372 void cursorRightOneWord();
374 void cursorUpParagraph();
376 void cursorDownParagraph();
382 void cursorPrevious();
396 bool selectWordWhenUnderCursor(word_location);
402 text_capitalization = 1,
406 /// Change the case of the word at cursor position.
407 void changeCase(TextCase action);
409 void transposeChars();
414 void cutSelection(bool doclear = true, bool realcut = true);
416 void copySelection();
418 void pasteSelection();
420 void copyEnvironmentType();
422 void pasteEnvironmentType();
424 /** the DTP switches for paragraphs. LyX will store the top settings
425 always in the first physical paragraph, the bottom settings in the
426 last. When a paragraph is broken, the top settings rest, the bottom
427 settings are given to the new one. So I can make shure, they do not
428 duplicate themself (and you cannnot make dirty things with them! )
430 void setParagraph(bool line_top, bool line_bottom,
431 bool pagebreak_top, bool pagebreak_bottom,
432 VSpace const & space_top,
433 VSpace const & space_bottom,
434 Spacing const & spacing,
436 string const & labelwidthstring,
439 /* these things are for search and replace */
442 * Sets the selection from the current cursor position to length
443 * characters to the right. No safety checks.
445 void setSelectionRange(lyx::pos_type length);
447 /** simple replacing. The font of the first selected character
450 void replaceSelectionWithString(string const & str);
452 /// needed to insert the selection
453 void insertStringAsLines(string const & str);
454 /// needed to insert the selection
455 void insertStringAsParagraphs(string const & str);
457 /// Find next inset of some specified type.
458 bool gotoNextInset(std::vector<Inset::Code> const & codes,
459 string const & contents = string());
461 void gotoInset(std::vector<Inset::Code> const & codes,
464 void gotoInset(Inset::Code code, bool same_content);
467 /* for the greater insets */
469 /// returns false if inset wasn't found
470 bool updateInset(Inset *);
472 void checkParagraph(Paragraph * par, lyx::pos_type pos);
474 int workWidth() const;
476 int workWidth(Inset * inset) const;
479 void computeBidiTables(Buffer const *, Row * row) const;
480 /// Maps positions in the visual string to positions in logical string.
481 lyx::pos_type log2vis(lyx::pos_type pos) const;
482 /// Maps positions in the logical string to positions in visual string.
483 lyx::pos_type vis2log(lyx::pos_type pos) const;
485 lyx::pos_type bidi_level(lyx::pos_type pos) const;
487 bool bidi_InRange(lyx::pos_type pos) const;
492 void cursorLeftOneWord(LyXCursor &);
495 float getCursorX(Row *, lyx::pos_type pos,
496 lyx::pos_type last, bool boundary) const;
497 /// used in setlayout
498 void makeFontEntriesLayoutSpecific(Buffer const &, Paragraph & par);
500 /** forces the redrawing of a paragraph. Needed when manipulating a
503 void redoDrawingOfParagraph(LyXCursor const & cursor);
505 /** Copybuffer for copy environment type.
506 Asger has learned that this should be a buffer-property instead
507 Lgb has learned that 'char' is a lousy type for non-characters
509 string copylayouttype;
511 /** inserts a new row behind the specified row, increments
512 the touched counters */
514 insertRow(RowList::iterator rowit,
515 Paragraph * par, lyx::pos_type pos);
516 /// removes the row and reset the touched counters
517 void removeRow(Row * row);
519 /// remove all following rows of the paragraph of the specified row.
520 void removeParagraph(Row * row);
522 /// insert the specified paragraph behind the specified row
523 void insertParagraph(Paragraph * par, RowList::iterator rowit);
525 /** appends the implizit specified paragraph behind the specified row,
526 * start at the implizit given position */
527 void appendParagraph(RowList::iterator rowit);
530 void breakAgain(Row * row);
531 /// Calculate and set the height of the row
532 void setHeightOfRow(Row * row_ptr);
534 // fix the cursor `cur' after a characters has been deleted at `where'
535 // position. Called by deleteEmptyParagraphMechanism
536 void fixCursorAfterDelete(LyXCursor & cur,
537 LyXCursor const & where);
539 /// delete double space (false) or empty paragraphs (true) around old_cursor
540 bool deleteEmptyParagraphMechanism(LyXCursor const & old_cursor);
543 /** Updates all counters starting BEHIND the row. Changed paragraphs
544 * with a dynamic left margin will be rebroken. */
545 void updateCounters();
549 * Returns an inset if inset was hit, or 0 if not.
550 * If hit, the coordinates are changed relative to the inset.
552 Inset * checkInsetHit(int & x, int & y);
555 int singleWidth(Paragraph * par,
556 lyx::pos_type pos) const;
558 int singleWidth(Paragraph * par,
559 lyx::pos_type pos, char c) const;
561 /// return the color of the canvas
562 LColor::color backgroundColor() const;
565 mutable bool bidi_same_direction;
567 unsigned char transformChar(unsigned char c, Paragraph * par,
568 lyx::pos_type pos) const;
571 * Returns the left beginning of the text.
572 * This information cannot be taken from the layout object, because
573 * in LaTeX the beginning of the text fits in some cases
574 * (for example sections) exactly the label-width.
576 int leftMargin(Row const * row) const;
578 int rightMargin(Buffer const &, Row const & row) const;
580 /** this calculates the specified parameters. needed when setting
581 * the cursor and when creating a visible row */
582 void prepareToPrint(Row * row, float & x,
583 float & fill_separator,
585 float & fill_label_hfill,
586 bool bidi = true) const;
590 void setCounter(Buffer const *, Paragraph * par);
592 void deleteWordForward();
594 void deleteWordBackward();
596 void deleteLineForward();
599 * some low level functions
603 /// return the pos value *before* which a row should break.
604 /// for example, the pos at which IsNewLine(pos) == true
605 lyx::pos_type rowBreakPoint(Row const & row) const;
607 /// returns the minimum space a row needs on the screen in pixel
608 int fill(Row & row, int workwidth) const;
611 * returns the minimum space a manual label needs on the
614 int labelFill(Row const & row) const;
617 int labelEnd(Row const & row) const;
620 mutable std::vector<lyx::pos_type> log2vis_list;
622 mutable std::vector<lyx::pos_type> vis2log_list;
624 mutable std::vector<lyx::pos_type> bidi_levels;
626 mutable lyx::pos_type bidi_start;
628 mutable lyx::pos_type bidi_end;
634 // special owner functions
636 Paragraph * ownerParagraph() const;
638 void ownerParagraph(Paragraph *) const;
639 // set it searching first for the right owner using the paragraph id
640 void ownerParagraph(int id, Paragraph *) const;
642 /// return true if this is owned by an inset.
643 bool isInInset() const;
646 /// return the default height of a row in pixels, considering font zoom
647 extern int defaultRowHeight();