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
43 /// the refresh_row needs repainting
45 /// everything from refresh_y downwards needs repainting
51 // the word around the cursor, only if the cursor is
54 // the word around the cursor
56 /// the word begining from the cursor position
58 /// the word around the cursor or before the cursor
60 /// the next word (not yet used)
65 LyXText(BufferView *);
66 /// sets inset as owner
67 LyXText(BufferView *, InsetText *);
72 void init(BufferView *, bool reinit = false);
77 /// the current font settings
80 LyXFont real_current_font;
82 /** the 'anchor' row: the position of this row remains constant
83 * with respect to the top of the screen
86 /** the pixel offset with respect to this row of top_y
88 int anchor_row_offset_;
90 /// get the y coord. of the top of the screen (relative to doc start)
92 /// set the y coord. of the top of the screen (relative to doc start)
94 /// set the anchoring row. top_y will be computed relative to this
95 void anchor_row(Row * row);
97 InsetText * inset_owner;
99 UpdatableInset * the_locking_inset;
102 int getRealCursorX() 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(Paragraph * par,
114 lyx::pos_type pos, LyXFont const & font, bool toggleall);
117 void breakAgainOneRow(Row * row);
118 /// what you expect when pressing <enter> at cursor position
119 void breakParagraph(ParagraphList & paragraphs, char keep_layout = 0);
121 /** set layout over selection and make a total rebreak of
124 Paragraph * setLayout(LyXCursor & actual_cursor,
125 LyXCursor & selection_start,
126 LyXCursor & selection_end,
127 string const & layout);
129 void setLayout(string const & layout);
131 /** increment depth over selection and make a total rebreak of those
136 /** decrement depth over selection and make a total rebreak of those
140 /// get the depth at current cursor position
141 int getDepth() const;
143 /** set font over selection and make a total rebreak of those
145 toggleall defaults to false.
147 void setFont(LyXFont const &, bool toggleall = false);
149 /** deletes and inserts again all paragaphs between the cursor
150 and the specified par. The Cursor is needed to set the refreshing
152 This function is needed after SetLayout and SetFont etc.
154 void redoParagraphs(LyXCursor const & cursor,
155 Paragraph const * end_par);
157 void redoParagraph();
160 void toggleFree(LyXFont const &, bool toggleall = false);
163 string getStringToIndex();
165 /** recalculates the heights of all previous rows of the
166 specified paragraph. needed, if the last characters font
169 void redoHeightOfParagraph();
171 /** insert a character, moves all the following breaks in the
172 same Paragraph one to the right and make a little rebreak
174 void insertChar(char c);
176 void insertInset(Inset * inset);
178 /// Completes the insertion with a full rebreak
182 Row * need_break_row;
184 /// clear any pending paints
188 * Mark position y as the starting point for a repaint
190 void postPaint(int start_y);
193 * Mark the given row at position y as needing a repaint.
195 void postRowPaint(Row * row, int start_y);
198 Inset::RESULT dispatch(FuncRequest const & cmd);
202 BufferView * bv() const;
204 friend class LyXScreen;
207 * Return the status. This represents what repaints are
208 * pending after some operation (e.g. inserting a char).
210 refresh_status refreshStatus() const;
214 * The pixel y position from which to repaint the screen.
215 * The position is absolute along the height of outermost
216 * lyxtext (I think). REFRESH_AREA and REFRESH_ROW
217 * repaints both use this as a starting point (if it's within
218 * the viewable portion of the lyxtext).
222 * The row from which to repaint the screen, used by screen.c.
223 * This must be set if the pending update is REFRESH_ROW.
224 * It doesn't make any difference for REFRESH_AREA.
228 refresh_status refresh_status_;
230 /// only the top-level LyXText has this non-zero
231 BufferView * bv_owner;
234 /** returns a pointer to the row near the specified y-coordinate
235 (relative to the whole text). y is set to the real beginning
238 Row * getRowNearY(int & y) const;
240 /** returns the column near the specified x-coordinate of the row
241 x is set to the real beginning of this column
243 lyx::pos_type getColumnNearX(Row * row,
244 int & x, bool & boundary) const;
246 /** returns a pointer to a specified row. y is set to the beginning
249 Row * getRow(Paragraph * par, lyx::pos_type pos, int & y) const;
250 /** returns the firstrow, this could be done with the above too but
251 IMO it's stupid to have to allocate a dummy y all the time I need
254 Row * firstRow() const { return firstrow; }
257 Later this variable has to be removed. There should be now internal
258 cursor in a text (and thus not in a buffer). By keeping this it is
259 (I think) impossible to have several views with the same buffer, but
260 the cursor placed at different places.
262 Since the LyXText now has been moved from Buffer to BufferView
263 it should not be absolutely needed to move the cursor...
265 LyXCursor cursor; // actual cursor position
267 /** The structure that keeps track of the selections set. */
270 : set_(false), mark_(false)
284 LyXCursor cursor; // temporary cursor to hold a cursor position
285 // until setSelection is called!
286 LyXCursor start; // start of a REAL selection
287 LyXCursor end; // end of a REAL selection
289 bool set_; // former selection
290 bool mark_; // former mark_set
294 // this is used to handle XSelection events in the right manner
295 Selection xsel_cache;
297 /// needed for the toggling (cursor position on last selection made)
298 LyXCursor last_sel_cursor;
299 /// needed for toggling the selection in screen.C
300 LyXCursor toggle_cursor;
301 /// needed for toggling the selection in screen.C
302 LyXCursor toggle_end_cursor;
304 /// need the selection cursor:
307 void clearSelection();
309 string const selectionAsString(Buffer const *, bool label) const;
311 /// select the word we need depending on word_location
312 void getWord(LyXCursor & from, LyXCursor & to,
313 word_location const);
314 /// just selects the word the cursor is in
315 void selectWord(word_location const);
316 /// returns the inset at cursor (if it exists), 0 otherwise
317 Inset * getInset() const;
319 /// accept selected change
322 /// reject selected change
325 /** 'selects" the next word, where the cursor is not in
326 and returns this word as string. THe cursor will be moved
327 to the beginning of this word.
328 With SelectSelectedWord can this be highlighted really
330 WordLangTuple const selectNextWordToSpellcheck(float & value);
332 void selectSelectedWord();
333 /// returns true if par was empty and was removed
334 bool setCursor(Paragraph * par,
337 bool boundary = false);
339 void setCursor(LyXCursor &, Paragraph * par,
341 bool boundary = false);
343 void setCursorIntern(Paragraph * par,
346 bool boundary = false);
348 void setCurrentFont();
351 bool isBoundary(Buffer const *, Paragraph * par,
352 lyx::pos_type pos) const;
354 bool isBoundary(Buffer const *, Paragraph * par,
356 LyXFont const & font) const;
359 void setCursorFromCoordinates(int x, int y);
361 void setCursorFromCoordinates(LyXCursor &,
364 void cursorUp(bool selecting = false);
366 void cursorDown(bool selecting = false);
368 void cursorLeft(bool internal = true);
370 void cursorRight(bool internal = true);
372 void cursorLeftOneWord();
374 void cursorRightOneWord();
376 void cursorUpParagraph();
378 void cursorDownParagraph();
384 void cursorPrevious();
398 bool selectWordWhenUnderCursor(word_location);
404 text_capitalization = 1,
408 /// Change the case of the word at cursor position.
409 void changeCase(TextCase action);
411 void transposeChars();
416 void cutSelection(bool doclear = true, bool realcut = true);
418 void copySelection();
420 void pasteSelection();
422 void copyEnvironmentType();
424 void pasteEnvironmentType();
426 /** the DTP switches for paragraphs. LyX will store the top settings
427 always in the first physical paragraph, the bottom settings in the
428 last. When a paragraph is broken, the top settings rest, the bottom
429 settings are given to the new one. So I can make shure, they do not
430 duplicate themself (and you cannnot make dirty things with them! )
432 void setParagraph(bool line_top, bool line_bottom,
433 bool pagebreak_top, bool pagebreak_bottom,
434 VSpace const & space_top,
435 VSpace const & space_bottom,
436 Spacing const & spacing,
438 string const & labelwidthstring,
441 /* these things are for search and replace */
444 * Sets the selection from the current cursor position to length
445 * characters to the right. No safety checks.
447 void setSelectionRange(lyx::pos_type length);
449 /** simple replacing. The font of the first selected character
452 void replaceSelectionWithString(string const & str);
454 /// needed to insert the selection
455 void insertStringAsLines(string const & str);
456 /// needed to insert the selection
457 void insertStringAsParagraphs(string const & str);
459 /// Find next inset of some specified type.
460 bool gotoNextInset(std::vector<Inset::Code> const & codes,
461 string const & contents = string());
463 void gotoInset(std::vector<Inset::Code> const & codes,
466 void gotoInset(Inset::Code code, bool same_content);
469 /* for the greater insets */
471 /// returns false if inset wasn't found
472 bool updateInset(Inset *);
474 void checkParagraph(Paragraph * par, lyx::pos_type pos);
476 int workWidth() const;
478 int workWidth(Inset * inset) const;
481 void computeBidiTables(Buffer const *, Row * row) const;
482 /// Maps positions in the visual string to positions in logical string.
483 lyx::pos_type log2vis(lyx::pos_type pos) const;
484 /// Maps positions in the logical string to positions in visual string.
485 lyx::pos_type vis2log(lyx::pos_type pos) const;
487 lyx::pos_type bidi_level(lyx::pos_type pos) const;
489 bool bidi_InRange(lyx::pos_type pos) const;
497 void cursorLeftOneWord(LyXCursor &);
500 float getCursorX(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(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);
519 /// removes the row and reset the touched counters
520 void removeRow(Row * row);
522 /// remove all following rows of the paragraph of the specified row.
523 void removeParagraph(Row * row);
525 /// insert the specified paragraph behind the specified row
526 void insertParagraph(Paragraph * par, Row * row);
528 /** appends the implizit specified paragraph behind the specified row,
529 * start at the implizit given position */
530 void appendParagraph(Row * row);
533 void breakAgain(Row * row);
534 /// Calculate and set the height of the row
535 void setHeightOfRow(Row * row_ptr);
537 // fix the cursor `cur' after a characters has been deleted at `where'
538 // position. Called by deleteEmptyParagraphMechanism
539 void fixCursorAfterDelete(LyXCursor & cur,
540 LyXCursor const & where);
542 /// delete double space (false) or empty paragraphs (true) around old_cursor
543 bool deleteEmptyParagraphMechanism(LyXCursor const & old_cursor);
546 /** Updates all counters starting BEHIND the row. Changed paragraphs
547 * with a dynamic left margin will be rebroken. */
548 void updateCounters();
552 * Returns an inset if inset was hit, or 0 if not.
553 * If hit, the coordinates are changed relative to the inset.
555 Inset * checkInsetHit(int & x, int & y);
558 int singleWidth(Paragraph * par,
559 lyx::pos_type pos) const;
561 int singleWidth(Paragraph * par,
562 lyx::pos_type pos, char c) const;
564 /// return the color of the canvas
565 LColor::color backgroundColor() const;
568 mutable bool bidi_same_direction;
570 unsigned char transformChar(unsigned char c, Paragraph * par,
571 lyx::pos_type pos) const;
574 * Returns the left beginning of the text.
575 * This information cannot be taken from the layout object, because
576 * in LaTeX the beginning of the text fits in some cases
577 * (for example sections) exactly the label-width.
579 int leftMargin(Row const * row) const;
581 int rightMargin(Buffer const &, Row const & row) const;
583 /** this calculates the specified parameters. needed when setting
584 * the cursor and when creating a visible row */
585 void prepareToPrint(Row * row, float & x,
586 float & fill_separator,
588 float & fill_label_hfill,
589 bool bidi = true) const;
593 void setCounter(Buffer const *, Paragraph * par);
595 void deleteWordForward();
597 void deleteWordBackward();
599 void deleteLineForward();
602 * some low level functions
606 /// return the pos value *before* which a row should break.
607 /// for example, the pos at which IsNewLine(pos) == true
608 lyx::pos_type rowBreakPoint(Row const & row) const;
610 /// returns the minimum space a row needs on the screen in pixel
611 int fill(Row & row, int workwidth) const;
614 * returns the minimum space a manual label needs on the
617 int labelFill(Row const & row) const;
620 int labelEnd(Row const & row) const;
623 mutable std::vector<lyx::pos_type> log2vis_list;
625 mutable std::vector<lyx::pos_type> vis2log_list;
627 mutable std::vector<lyx::pos_type> bidi_levels;
629 mutable lyx::pos_type bidi_start;
631 mutable lyx::pos_type bidi_end;
637 // special owner functions
639 Paragraph * ownerParagraph() const;
641 void ownerParagraph(Paragraph *) const;
642 // set it searching first for the right owner using the paragraph id
643 void ownerParagraph(int id, Paragraph *) const;
645 /// return true if this is owned by an inset.
646 bool isInInset() const;
649 /// return the default height of a row in pixels, considering font zoom
650 extern int defaultRowHeight();