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 * ====================================================== */
20 #include "lyxcursor.h"
23 #include "insets/inset.h"
37 This class holds the mapping between buffer paragraphs and screen rows.
48 NEED_VERY_LITTLE_REFRESH = 2,
54 // the word around the cursor, only if the cursor is
57 // the word around the cursor
59 /// the word begining from the cursor position
61 /// the word around the cursor or before the cursor
63 /// the next word (not yet used)
68 LyXText(BufferView *);
75 void init(BufferView *, bool reinit = false);
77 mutable int number_of_rows;
81 mutable unsigned int width;
82 /// the current font settings
83 mutable LyXFont current_font;
85 mutable LyXFont real_current_font;
86 /// first visible pixel-row is set from LyXScreen!!!
87 // unsigned is wrong here for text-insets!
90 BufferView * bv_owner;
92 InsetText * inset_owner;
94 UpdatableInset * the_locking_inset;
97 int getRealCursorX(BufferView *) const;
99 LyXFont const getFont(Buffer const *, Paragraph * par,
100 lyx::pos_type pos) const;
102 LyXFont const getLayoutFont(Buffer const *, Paragraph * par) const;
104 LyXFont const getLabelFont(Buffer const *, Paragraph * par) const;
106 void setCharFont(Buffer const *, Paragraph * par,
107 lyx::pos_type pos, LyXFont const & font);
108 void setCharFont(BufferView *, Paragraph * par,
109 lyx::pos_type pos, LyXFont const & font, bool toggleall);
111 /// what you expect when pressing <enter> at cursor position
112 void breakParagraph(BufferView *, char keep_layout = 0);
114 /** set layout over selection and make a total rebreak of
117 Paragraph * setLayout(BufferView *, LyXCursor & actual_cursor,
118 LyXCursor & selection_start,
119 LyXCursor & selection_end,
120 lyx::layout_type layout);
122 void setLayout(BufferView *, lyx::layout_type layout);
124 /// used in setlayout
125 void makeFontEntriesLayoutSpecific(Buffer const *, Paragraph * par);
127 /** increment depth over selection and make a total rebreak of those
130 void incDepth(BufferView *);
132 /** decrement depth over selection and make a total rebreak of those
134 void decDepth(BufferView *);
136 /** Get the depth at current cursor position
138 int getDepth() const;
140 /** set font over selection and make a total rebreak of those
142 toggleall defaults to false.
144 void setFont(BufferView *, LyXFont const &, bool toggleall = false);
146 /** deletes and inserts again all paragaphs between the cursor
147 and the specified par. The Cursor is needed to set the refreshing
149 This function is needed after SetLayout and SetFont etc.
151 void redoParagraphs(BufferView *, LyXCursor const & cursor,
152 Paragraph const * end_par) const;
154 void redoParagraph(BufferView *) const;
157 void toggleFree(BufferView *, LyXFont const &, bool toggleall = false);
160 string getStringToIndex(BufferView *);
162 /** recalculates the heights of all previous rows of the
163 specified paragraph. needed, if the last characters font
166 void redoHeightOfParagraph(BufferView *, LyXCursor const & cursor);
168 /** forces the redrawing of a paragraph. Needed when manipulating a
171 void redoDrawingOfParagraph(BufferView *, LyXCursor const & cursor);
173 /** insert a character, moves all the following breaks in the
174 same Paragraph one to the right and make a little rebreak
176 void insertChar(BufferView *, char c);
178 void insertInset(BufferView *, Inset * inset);
180 /** Completes the insertion with a full rebreak.
181 Returns true if something was broken. */
182 bool fullRebreak(BufferView *);
185 mutable Row * need_break_row;
187 mutable int refresh_y;
195 mutable Row * refresh_row;
197 lyx::pos_type refresh_pos;
199 /// give and set the LyXText status
200 text_status status() const;
202 void status(BufferView *, text_status) const;
205 /** wether the screen needs a refresh,
206 starting with refresh_y
208 mutable text_status status_;
211 /** returns a pointer to the row near the specified y-coordinate
212 (relative to the whole text). y is set to the real beginning
215 Row * getRowNearY(int & y) const;
217 /** returns the column near the specified x-coordinate of the row
218 x is set to the real beginning of this column
220 lyx::pos_type getColumnNearX(BufferView *, Row * row,
221 int & x, bool & boundary) const;
223 /** returns a pointer to a specified row. y is set to the beginning
226 Row * getRow(Paragraph * par,
227 lyx::pos_type pos, int & y) const;
228 /** returns the firstrow, this could be done with the above too but
229 IMO it's stupid to have to allocate a dummy y all the time I need
232 Row * firstRow() { return firstrow; }
234 /** returns the height of a default row, needed for scrollbar
236 int defaultHeight() const;
239 Later this variable has to be removed. There should be now internal
240 cursor in a text (and thus not in a buffer). By keeping this it is
241 (I think) impossible to have several views with the same buffer, but
242 the cursor placed at different places.
244 Since the LyXText now has been moved from Buffer to BufferView
245 it should not be absolutely needed to move the cursor...
247 mutable LyXCursor cursor; // actual cursor position
249 /** The structure that keeps track of the selections set. */
252 : set_(false), mark_(false)
266 LyXCursor cursor; // temporary cursor to hold a cursor position
267 // until setSelection is called!
268 LyXCursor start; // start of a REAL selection
269 LyXCursor end; // end of a REAL selection
271 bool set_; // former selection
272 bool mark_; // former mark_set
275 mutable Selection selection;
277 /// needed for the toggling (cursor position on last selection made)
278 mutable LyXCursor last_sel_cursor;
279 /// needed for toggling the selection in screen.C
280 mutable LyXCursor toggle_cursor;
281 /// needed for toggling the selection in screen.C
282 mutable LyXCursor toggle_end_cursor;
284 /// need the selection cursor:
285 void setSelection(BufferView *);
287 void clearSelection() const;
289 string const selectionAsString(Buffer const *, bool label) const;
291 /// select the word we need depending on word_location
292 void getWord(LyXCursor & from, LyXCursor & to,
293 word_location const) const;
294 /// just selects the word the cursor is in
295 void selectWord(BufferView *, word_location const);
296 /// returns the inset at cursor (if it exists), 0 otherwise
297 Inset * getInset() const;
299 /** 'selects" the next word, where the cursor is not in
300 and returns this word as string. THe cursor will be moved
301 to the beginning of this word.
302 With SelectSelectedWord can this be highlighted really
304 string const selectNextWordToSpellcheck(BufferView *, float & value) const;
306 void selectSelectedWord(BufferView *);
307 /// returns true if par was empty and was removed
308 bool setCursor(BufferView *, Paragraph * par,
311 bool boundary = false) const;
313 void setCursor(BufferView *, LyXCursor &, Paragraph * par,
315 bool boundary = false) const;
317 void setCursorIntern(BufferView *, Paragraph * par,
320 bool boundary = false) const;
322 void setCurrentFont(BufferView *) const;
325 bool isBoundary(Buffer const *, Paragraph * par,
326 lyx::pos_type pos) const;
328 bool isBoundary(Buffer const *, Paragraph * par,
330 LyXFont const & font) const;
333 void setCursorFromCoordinates(BufferView *, int x, int y) const;
335 void setCursorFromCoordinates(BufferView *, LyXCursor &,
338 void cursorUp(BufferView *) const;
340 void cursorDown(BufferView *) const;
342 void cursorLeft(BufferView *, bool internal = true) const;
344 void cursorRight(BufferView *, bool internal = true) const;
346 void cursorLeftOneWord(BufferView *) const;
348 void cursorLeftOneWord(LyXCursor &) const;
350 void cursorRightOneWord(BufferView *) const;
352 void cursorUpParagraph(BufferView *) const;
354 void cursorDownParagraph(BufferView *) const;
356 void cursorHome(BufferView *) const;
358 void cursorEnd(BufferView *) const;
360 void cursorTab(BufferView *) const;
362 void cursorTop(BufferView *) const;
364 void cursorBottom(BufferView *) const;
366 void Delete(BufferView *);
368 void backspace(BufferView *);
370 void deleteWordForward(BufferView *);
372 void deleteWordBackward(BufferView *);
374 void deleteLineForward(BufferView *);
376 bool selectWordWhenUnderCursor(BufferView *,
377 word_location const);
383 text_capitalization = 1,
387 /// Change the case of the word at cursor position.
388 void changeCase(BufferView *, TextCase action);
390 void changeRegionCase(BufferView * bview,
391 LyXCursor const & from,
392 LyXCursor const & to,
393 LyXText::TextCase action);
395 void transposeChars(BufferView &);
397 /** returns a printed row in a pixmap. The y value is needed to
398 decide, wether it is selected text or not. This is a strange
401 void getVisibleRow(BufferView *, int y_offset, int x_offset,
402 Row * row_ptr, int y, bool cleared=false);
405 void toggleInset(BufferView *);
407 void cutSelection(BufferView *, bool doclear = true, bool realcut = true);
409 void copySelection(BufferView *);
411 void pasteSelection(BufferView *);
413 void copyEnvironmentType();
415 void pasteEnvironmentType(BufferView *);
417 /** the DTP switches for paragraphs. LyX will store the top settings
418 always in the first physical paragraph, the bottom settings in the
419 last. When a paragraph is broken, the top settings rest, the bottom
420 settings are given to the new one. So I can make shure, they do not
421 duplicate themself (and you cannnot make dirty things with them! )
423 void setParagraph(BufferView *,
424 bool line_top, bool line_bottom,
425 bool pagebreak_top, bool pagebreak_bottom,
426 VSpace const & space_top,
427 VSpace const & space_bottom,
428 Spacing const & spacing,
430 string labelwidthstring,
433 /* these things are for search and replace */
435 /** sets the selection over the number of characters of string,
438 void setSelectionOverString(BufferView *, string const & str);
440 /** simple replacing. The font of the first selected character
443 void replaceSelectionWithString(BufferView *, string const & str);
445 /// needed to insert the selection
446 void insertStringAsLines(BufferView *, string const & str);
447 /// needed to insert the selection
448 void insertStringAsParagraphs(BufferView *, string const & str);
450 /// Find next inset of some specified type.
451 bool gotoNextInset(BufferView *, std::vector<Inset::Code> const & codes,
452 string const & contents = string()) const;
455 /* for the greater insets */
457 /// returns false if inset wasn't found
458 bool updateInset(BufferView *, Inset *);
460 void checkParagraph(BufferView *, Paragraph * par, lyx::pos_type pos);
462 int numberOfCell(Paragraph * par, lyx::pos_type pos) const;
464 void removeTableRow(LyXCursor & cursor) const;
466 bool isEmptyTableCell() const;
468 void toggleAppendix(BufferView *);
470 int workWidth(BufferView *) const;
472 int workWidth(BufferView *, Inset * inset) const;
474 void computeBidiTables(Buffer const *, Row * row) const;
476 /// Maps positions in the visual string to positions in logical string.
478 lyx::pos_type log2vis(lyx::pos_type pos) const {
479 if (bidi_start == -1)
482 return log2vis_list[pos-bidi_start];
485 /// Maps positions in the logical string to positions in visual string.
487 lyx::pos_type vis2log(lyx::pos_type pos) const {
488 if (bidi_start == -1)
491 return vis2log_list[pos-bidi_start];
495 lyx::pos_type bidi_level(lyx::pos_type pos) const {
496 if (bidi_start == -1)
499 return bidi_levels[pos-bidi_start];
503 bool bidi_InRange(lyx::pos_type pos) const {
504 return bidi_start == -1 ||
505 (bidi_start <= pos && pos <= bidi_end);
509 mutable Row * firstrow;
511 mutable Row * lastrow;
513 /** Copybuffer for copy environment type.
514 Asger has learned that this should be a buffer-property instead
515 Lgb has learned that 'char' is a lousy type for non-characters
517 lyx::layout_type copylayouttype;
519 /** inserts a new row behind the specified row, increments
520 the touched counters */
521 void insertRow(Row * row, Paragraph * par, lyx::pos_type pos) const;
522 /** removes the row and reset the touched counters */
523 void removeRow(Row * row) const;
525 /** remove all following rows of the paragraph of the specified row. */
526 void removeParagraph(Row * row) const;
528 /** insert the specified paragraph behind the specified row */
529 void insertParagraph(BufferView *,
530 Paragraph * par, Row * row) const;
532 /** appends the implizit specified paragraph behind the specified row,
533 * start at the implizit given position */
534 void appendParagraph(BufferView *, Row * row) const;
537 void breakAgain(BufferView *, Row * row) const;
539 void breakAgainOneRow(BufferView *, Row * row);
540 /// Calculate and set the height of the row
541 void setHeightOfRow(BufferView *, Row * row_ptr) const;
543 /** this calculates the specified parameters. needed when setting
544 * the cursor and when creating a visible row */
545 void prepareToPrint(BufferView *, Row * row, float & x,
546 float & fill_separator,
548 float & fill_label_hfill,
549 bool bidi = true) const;
551 /// A struct used for drawing routines
552 struct DrawRowParams {
557 // the painter to use
559 // has the background been cleared
561 /// x offset (e.g. for insets)
563 /// y offset (e.g. for insets)
569 /// the inset/view full width
575 /// fill separator size
579 /// paint the background
580 bool paintRowBackground(DrawRowParams & p);
582 /// paint the selection background
583 void paintRowSelection(DrawRowParams & p);
585 /// paint appendix marker
586 void paintRowAppendix(DrawRowParams & p);
588 /// paint env depth bar
589 void paintRowDepthBar(DrawRowParams & p);
591 /// get the on-screen size of the length marker
592 int getLengthMarkerHeight(BufferView * bv, VSpace const & vsp) const;
594 /// paint an added space marker
595 int drawLengthMarker(DrawRowParams & p, string const & str,
596 VSpace const & vsp, int start);
598 /// paint a first row in a paragraph
599 void paintFirstRow(DrawRowParams & p);
601 /// paint a last row in a paragraph
602 void paintLastRow(DrawRowParams & p);
605 void paintRowText(DrawRowParams & p);
607 // fix the cursor `cur' after a characters has been deleted at `where'
608 // position. Called by deleteEmptyParagraphMechanism
609 void fixCursorAfterDelete(BufferView * bview,
611 LyXCursor const & where) const;
613 /// delete double space (false) or empty paragraphs (true) around old_cursor
614 bool deleteEmptyParagraphMechanism(BufferView *,
615 LyXCursor const & old_cursor) const;
618 /** Updates all counters starting BEHIND the row. Changed paragraphs
619 * with a dynamic left margin will be rebroken. */
620 void updateCounters(BufferView *, Row * row) const;
623 void setCounter(Buffer const *, Paragraph * par) const;
626 * some low level functions
630 int singleWidth(BufferView *, Paragraph * par,
631 lyx::pos_type pos) const;
633 int singleWidth(BufferView *, Paragraph * par,
634 lyx::pos_type pos, char c) const;
637 /// draw normal chars
638 void drawChars(DrawRowParams & p, lyx::pos_type & vpos,
639 bool hebrew, bool arabic);
640 /// draw from arabic composed char
641 void drawArabicComposeChar(DrawRowParams & p, lyx::pos_type & vpos);
642 /// draw from hebrew composed char
643 void drawHebrewComposeChar(DrawRowParams & p, lyx::pos_type & vpos);
644 /// draw a mark for foreign language, starting from orig_x
645 void drawForeignMark(DrawRowParams & p, float const orig_x, LyXFont const & orig_font);
647 void drawInset(DrawRowParams & p, lyx::pos_type const pos);
648 /// draw new line marker
649 void drawNewline(DrawRowParams & p, lyx::pos_type const pos);
651 void draw(DrawRowParams & p, lyx::pos_type & vpos);
653 /// get the next breakpoint in a given paragraph
654 lyx::pos_type nextBreakPoint(BufferView *, Row const * row, int width) const;
655 /// returns the minimum space a row needs on the screen in pixel
656 int fill(BufferView *, Row * row, int workwidth) const;
658 /** returns the minimum space a manual label needs on the
660 int labelFill(BufferView *, Row const * row) const;
663 lyx::pos_type beginningOfMainBody(Buffer const *, Paragraph const * par) const;
665 /** Returns the left beginning of the text.
666 This information cannot be taken from the layouts-objekt, because
667 in LaTeX the beginning of the text fits in some cases
668 (for example sections) exactly the label-width.
670 int leftMargin(BufferView *, Row const * row) const;
672 int rightMargin(Buffer const *, Row const * row) const;
674 int labelEnd (BufferView *, Row const * row) const;
676 /** returns the number of separators in the specified row.
677 The separator on the very last column doesnt count
679 int numberOfSeparators(Buffer const *, Row const * row) const;
681 /** returns the number of hfills in the specified row. The
682 LyX-Hfill is a LaTeX \hfill so that the hfills at the
683 beginning and at the end were ignored. This is {\em MUCH}
684 more usefull than not to ignore!
686 int numberOfHfills(Buffer const *, Row const * row) const;
688 /// like NumberOfHfills, but only those in the manual label!
689 int numberOfLabelHfills(Buffer const *, Row const * row) const;
690 /** returns true, if a expansion is needed. Rules are given by
693 bool hfillExpansion(Buffer const *, Row const * row_ptr,
694 lyx::pos_type pos) const;
696 LColor::color backgroundColor();
700 mutable std::vector<lyx::pos_type> log2vis_list;
703 mutable std::vector<lyx::pos_type> vis2log_list;
706 mutable std::vector<lyx::pos_type> bidi_levels;
709 mutable lyx::pos_type bidi_start;
712 mutable lyx::pos_type bidi_end;
715 mutable bool bidi_same_direction;
718 unsigned char transformChar(unsigned char c, Paragraph * par,
719 lyx::pos_type pos) const;
721 /** returns the paragraph position of the last character in the
724 lyx::pos_type rowLast(Row const * row) const;
726 lyx::pos_type rowLastPrintable(Row const * row) const;
732 // special owner functions
734 Paragraph * ownerParagraph() const;
736 void ownerParagraph(Paragraph *) const;
737 // set it searching first for the right owner using the paragraph id
738 void ownerParagraph(int id, Paragraph *) const;