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
83 RowList::iterator anchor_row_;
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(RowList::iterator rit);
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(RowList::iterator rit);
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 RowList::iterator 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(RowList::iterator rit, 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.
224 RowList::iterator refresh_row;
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 RowList::iterator 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(RowList::iterator rit,
242 int & x, bool & boundary) const;
244 /** returns a pointer to a specified row. y is set to the beginning
248 getRow(Paragraph * par, lyx::pos_type pos, int & y) const;
253 RowList const & rows() const {
258 Later this variable has to be removed. There should be now internal
259 cursor in a text (and thus not in a buffer). By keeping this it is
260 (I think) impossible to have several views with the same buffer, but
261 the cursor placed at different places.
263 Since the LyXText now has been moved from Buffer to BufferView
264 it should not be absolutely needed to move the cursor...
266 LyXCursor cursor; // actual cursor position
268 /** The structure that keeps track of the selections set. */
271 : set_(false), mark_(false)
285 LyXCursor cursor; // temporary cursor to hold a cursor position
286 // until setSelection is called!
287 LyXCursor start; // start of a REAL selection
288 LyXCursor end; // end of a REAL selection
290 bool set_; // former selection
291 bool mark_; // former mark_set
295 // this is used to handle XSelection events in the right manner
296 Selection xsel_cache;
298 /// needed for the toggling (cursor position on last selection made)
299 LyXCursor last_sel_cursor;
300 /// needed for toggling the selection in screen.C
301 LyXCursor toggle_cursor;
302 /// needed for toggling the selection in screen.C
303 LyXCursor toggle_end_cursor;
305 /// need the selection cursor:
308 void clearSelection();
310 string const selectionAsString(Buffer const *, bool label) const;
312 /// select the word we need depending on word_location
313 void getWord(LyXCursor & from, LyXCursor & to,
314 word_location const);
315 /// just selects the word the cursor is in
316 void selectWord(word_location const);
317 /// returns the inset at cursor (if it exists), 0 otherwise
318 Inset * getInset() const;
320 /// accept selected change
323 /// reject selected change
326 /** 'selects" the next word, where the cursor is not in
327 and returns this word as string. THe cursor will be moved
328 to the beginning of this word.
329 With SelectSelectedWord can this be highlighted really
331 WordLangTuple const selectNextWordToSpellcheck(float & value);
333 void selectSelectedWord();
334 /// returns true if par was empty and was removed
335 bool setCursor(Paragraph * par,
338 bool boundary = false);
340 void setCursor(LyXCursor &, Paragraph * par,
342 bool boundary = false);
344 void setCursorIntern(Paragraph * par,
347 bool boundary = false);
349 void setCurrentFont();
352 bool isBoundary(Buffer const *, Paragraph * par,
353 lyx::pos_type pos) const;
355 bool isBoundary(Buffer const *, Paragraph * par,
357 LyXFont const & font) const;
360 void setCursorFromCoordinates(int x, int y);
362 void setCursorFromCoordinates(LyXCursor &,
365 void cursorUp(bool selecting = false);
367 void cursorDown(bool selecting = false);
369 void cursorLeft(bool internal = true);
371 void cursorRight(bool internal = true);
373 void cursorLeftOneWord();
375 void cursorRightOneWord();
377 void cursorUpParagraph();
379 void cursorDownParagraph();
385 void cursorPrevious();
399 bool selectWordWhenUnderCursor(word_location);
405 text_capitalization = 1,
409 /// Change the case of the word at cursor position.
410 void changeCase(TextCase action);
412 void transposeChars();
417 void cutSelection(bool doclear = true, bool realcut = true);
419 void copySelection();
421 void pasteSelection();
423 void copyEnvironmentType();
425 void pasteEnvironmentType();
427 /** the DTP switches for paragraphs. LyX will store the top settings
428 always in the first physical paragraph, the bottom settings in the
429 last. When a paragraph is broken, the top settings rest, the bottom
430 settings are given to the new one. So I can make shure, they do not
431 duplicate themself (and you cannnot make dirty things with them! )
433 void setParagraph(bool line_top, bool line_bottom,
434 bool pagebreak_top, bool pagebreak_bottom,
435 VSpace const & space_top,
436 VSpace const & space_bottom,
437 Spacing const & spacing,
439 string const & labelwidthstring,
442 /* these things are for search and replace */
445 * Sets the selection from the current cursor position to length
446 * characters to the right. No safety checks.
448 void setSelectionRange(lyx::pos_type length);
450 /** simple replacing. The font of the first selected character
453 void replaceSelectionWithString(string const & str);
455 /// needed to insert the selection
456 void insertStringAsLines(string const & str);
457 /// needed to insert the selection
458 void insertStringAsParagraphs(string const & str);
460 /// Find next inset of some specified type.
461 bool gotoNextInset(std::vector<Inset::Code> const & codes,
462 string const & contents = string());
464 void gotoInset(std::vector<Inset::Code> const & codes,
467 void gotoInset(Inset::Code code, bool same_content);
470 /* for the greater insets */
472 /// returns false if inset wasn't found
473 bool updateInset(Inset *);
475 void checkParagraph(Paragraph * par, lyx::pos_type pos);
477 int workWidth() const;
479 int workWidth(Inset * inset) const;
482 void computeBidiTables(Buffer const *, Row const & row) const;
483 /// Maps positions in the visual string to positions in logical string.
484 lyx::pos_type log2vis(lyx::pos_type pos) const;
485 /// Maps positions in the logical string to positions in visual string.
486 lyx::pos_type vis2log(lyx::pos_type pos) const;
488 lyx::pos_type bidi_level(lyx::pos_type pos) const;
490 bool bidi_InRange(lyx::pos_type pos) const;
495 void cursorLeftOneWord(LyXCursor &);
498 float getCursorX(Row *, lyx::pos_type pos,
499 lyx::pos_type last, bool boundary) const;
500 /// used in setlayout
501 void makeFontEntriesLayoutSpecific(Buffer const &, Paragraph & par);
503 /** forces the redrawing of a paragraph. Needed when manipulating a
506 void redoDrawingOfParagraph(LyXCursor const & cursor);
508 /** Copybuffer for copy environment type.
509 Asger has learned that this should be a buffer-property instead
510 Lgb has learned that 'char' is a lousy type for non-characters
512 string copylayouttype;
514 /// removes the row and reset the touched counters
515 void removeRow(RowList::iterator rit);
517 /// remove all following rows of the paragraph of the specified row.
518 void removeParagraph(RowList::iterator rit);
520 /// insert the specified paragraph behind the specified row
521 void insertParagraph(Paragraph * par, RowList::iterator rowit);
523 /** appends the implizit specified paragraph behind the specified row,
524 * start at the implizit given position */
525 void appendParagraph(RowList::iterator rowit);
528 void breakAgain(RowList::iterator rit);
529 /// Calculate and set the height of the row
530 void setHeightOfRow(RowList::iterator rit);
532 // fix the cursor `cur' after a characters has been deleted at `where'
533 // position. Called by deleteEmptyParagraphMechanism
534 void fixCursorAfterDelete(LyXCursor & cur,
535 LyXCursor const & where);
537 /// delete double space (false) or empty paragraphs (true) around old_cursor
538 bool deleteEmptyParagraphMechanism(LyXCursor const & old_cursor);
541 /** Updates all counters starting BEHIND the row. Changed paragraphs
542 * with a dynamic left margin will be rebroken. */
543 void updateCounters();
547 * Returns an inset if inset was hit, or 0 if not.
548 * If hit, the coordinates are changed relative to the inset.
550 Inset * checkInsetHit(int & x, int & y);
553 int singleWidth(Paragraph * par,
554 lyx::pos_type pos) const;
556 int singleWidth(Paragraph * par,
557 lyx::pos_type pos, char c) const;
559 /// return the color of the canvas
560 LColor::color backgroundColor() const;
563 mutable bool bidi_same_direction;
565 unsigned char transformChar(unsigned char c, Paragraph * par,
566 lyx::pos_type pos) const;
569 * Returns the left beginning of the text.
570 * This information cannot be taken from the layout object, because
571 * in LaTeX the beginning of the text fits in some cases
572 * (for example sections) exactly the label-width.
574 int leftMargin(RowList::iterator rit) const;
576 int rightMargin(Buffer const &, Row const & row) const;
578 /** this calculates the specified parameters. needed when setting
579 * the cursor and when creating a visible row */
580 void prepareToPrint(RowList::iterator rit, float & x,
581 float & fill_separator,
583 float & fill_label_hfill,
584 bool bidi = true) const;
588 void setCounter(Buffer const *, Paragraph * par);
590 void deleteWordForward();
592 void deleteWordBackward();
594 void deleteLineForward();
597 * some low level functions
601 /// return the pos value *before* which a row should break.
602 /// for example, the pos at which IsNewLine(pos) == true
603 lyx::pos_type rowBreakPoint(Row const & row) const;
605 /// returns the minimum space a row needs on the screen in pixel
606 int fill(Row & row, int workwidth) const;
609 * returns the minimum space a manual label needs on the
612 int labelFill(Row const & row) const;
615 int labelEnd(Row const & row) const;
618 mutable std::vector<lyx::pos_type> log2vis_list;
620 mutable std::vector<lyx::pos_type> vis2log_list;
622 mutable std::vector<lyx::pos_type> bidi_levels;
624 mutable lyx::pos_type bidi_start;
626 mutable lyx::pos_type bidi_end;
632 // special owner functions
634 Paragraph * ownerParagraph() const;
636 void ownerParagraph(Paragraph *) const;
637 // set it searching first for the right owner using the paragraph id
638 void ownerParagraph(int id, Paragraph *) const;
640 /// return true if this is owned by an inset.
641 bool isInInset() const;
644 /// return the default height of a row in pixels, considering font zoom
645 extern int defaultRowHeight();