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"
38 This class holds the mapping between buffer paragraphs and screen rows.
49 NEED_VERY_LITTLE_REFRESH = 2,
55 // the word around the cursor, only if the cursor is
58 // the word around the cursor
60 /// the word begining from the cursor position
62 /// the word around the cursor or before the cursor
64 /// the next word (not yet used)
69 LyXText(BufferView *);
70 /// sets inset as owner
76 void init(BufferView *, bool reinit = false);
80 mutable unsigned int width;
81 /// the current font settings
82 mutable LyXFont current_font;
84 mutable LyXFont real_current_font;
85 /// first visible pixel-row is set from LyXScreen!!!
86 // unsigned is wrong here for text-insets!
89 BufferView * bv_owner;
91 InsetText * inset_owner;
93 UpdatableInset * the_locking_inset;
96 int getRealCursorX(BufferView *) const;
98 LyXFont const getFont(Buffer const *, Paragraph * par,
99 lyx::pos_type pos) const;
101 LyXFont const getLayoutFont(Buffer const *, Paragraph * par) const;
103 LyXFont const getLabelFont(Buffer const *, Paragraph * par) const;
105 void setCharFont(Buffer const *, Paragraph * par,
106 lyx::pos_type pos, LyXFont const & font);
107 void setCharFont(BufferView *, Paragraph * par,
108 lyx::pos_type pos, LyXFont const & font, bool toggleall);
110 /// what you expect when pressing <enter> at cursor position
111 void breakParagraph(BufferView *, char keep_layout = 0);
113 /** set layout over selection and make a total rebreak of
116 Paragraph * setLayout(BufferView *, LyXCursor & actual_cursor,
117 LyXCursor & selection_start,
118 LyXCursor & selection_end,
119 string const & layout);
121 void setLayout(BufferView *, string const & layout);
123 /** increment depth over selection and make a total rebreak of those
126 void incDepth(BufferView *);
128 /** decrement depth over selection and make a total rebreak of those
130 void decDepth(BufferView *);
132 /// get the depth at current cursor position
133 int getDepth() const;
135 /** set font over selection and make a total rebreak of those
137 toggleall defaults to false.
139 void setFont(BufferView *, LyXFont const &, bool toggleall = false);
141 /** deletes and inserts again all paragaphs between the cursor
142 and the specified par. The Cursor is needed to set the refreshing
144 This function is needed after SetLayout and SetFont etc.
146 void redoParagraphs(BufferView *, LyXCursor const & cursor,
147 Paragraph const * end_par) const;
149 void redoParagraph(BufferView *) const;
152 void toggleFree(BufferView *, LyXFont const &, bool toggleall = false);
155 string getStringToIndex(BufferView *);
157 /** recalculates the heights of all previous rows of the
158 specified paragraph. needed, if the last characters font
161 void redoHeightOfParagraph(BufferView *, LyXCursor const & cursor);
163 /** insert a character, moves all the following breaks in the
164 same Paragraph one to the right and make a little rebreak
166 void insertChar(BufferView *, char c);
168 void insertInset(BufferView *, Inset * inset);
170 /// Completes the insertion with a full rebreak
171 void fullRebreak(BufferView *);
174 mutable Row * need_break_row;
176 mutable int refresh_y;
182 mutable Row * refresh_row;
184 lyx::pos_type refresh_pos;
186 /// give and set the LyXText status
187 text_status status() const;
189 void status(BufferView *, text_status) const;
192 Inset::RESULT dispatch(FuncRequest const & cmd);
195 /** wether the screen needs a refresh,
196 starting with refresh_y
198 mutable text_status status_;
201 /** returns a pointer to the row near the specified y-coordinate
202 (relative to the whole text). y is set to the real beginning
205 Row * getRowNearY(int & y) const;
207 /** returns the column near the specified x-coordinate of the row
208 x is set to the real beginning of this column
210 lyx::pos_type getColumnNearX(BufferView *, Row * row,
211 int & x, bool & boundary) const;
213 /** returns a pointer to a specified row. y is set to the beginning
216 Row * getRow(Paragraph * par, lyx::pos_type pos, int & y) const;
217 /** returns the firstrow, this could be done with the above too but
218 IMO it's stupid to have to allocate a dummy y all the time I need
221 Row * firstRow() { return firstrow; }
223 /** returns the height of a default row, needed for scrollbar
225 int defaultHeight() const;
228 Later this variable has to be removed. There should be now internal
229 cursor in a text (and thus not in a buffer). By keeping this it is
230 (I think) impossible to have several views with the same buffer, but
231 the cursor placed at different places.
233 Since the LyXText now has been moved from Buffer to BufferView
234 it should not be absolutely needed to move the cursor...
236 mutable LyXCursor cursor; // actual cursor position
238 /** The structure that keeps track of the selections set. */
241 : set_(false), mark_(false)
255 LyXCursor cursor; // temporary cursor to hold a cursor position
256 // until setSelection is called!
257 LyXCursor start; // start of a REAL selection
258 LyXCursor end; // end of a REAL selection
260 bool set_; // former selection
261 bool mark_; // former mark_set
264 mutable Selection selection;
265 // this is used to handle XSelection events in the right manner
266 mutable Selection xsel_cache;
268 /// needed for the toggling (cursor position on last selection made)
269 mutable LyXCursor last_sel_cursor;
270 /// needed for toggling the selection in screen.C
271 mutable LyXCursor toggle_cursor;
272 /// needed for toggling the selection in screen.C
273 mutable LyXCursor toggle_end_cursor;
275 /// need the selection cursor:
276 void setSelection(BufferView *);
278 void clearSelection() const;
280 string const selectionAsString(Buffer const *, bool label) const;
282 /// select the word we need depending on word_location
283 void getWord(LyXCursor & from, LyXCursor & to,
284 word_location const) const;
285 /// just selects the word the cursor is in
286 void selectWord(BufferView *, word_location const);
287 /// returns the inset at cursor (if it exists), 0 otherwise
288 Inset * getInset() const;
290 /** 'selects" the next word, where the cursor is not in
291 and returns this word as string. THe cursor will be moved
292 to the beginning of this word.
293 With SelectSelectedWord can this be highlighted really
295 WordLangTuple const selectNextWordToSpellcheck(BufferView *, float & value) const;
297 void selectSelectedWord(BufferView *);
298 /// returns true if par was empty and was removed
299 bool setCursor(BufferView *, Paragraph * par,
302 bool boundary = false) const;
304 void setCursor(BufferView *, LyXCursor &, Paragraph * par,
306 bool boundary = false) const;
308 void setCursorIntern(BufferView *, Paragraph * par,
311 bool boundary = false) const;
313 void setCurrentFont(BufferView *) const;
316 bool isBoundary(Buffer const *, Paragraph * par,
317 lyx::pos_type pos) const;
319 bool isBoundary(Buffer const *, Paragraph * par,
321 LyXFont const & font) const;
324 void setCursorFromCoordinates(BufferView *, int x, int y) const;
326 void setCursorFromCoordinates(BufferView *, LyXCursor &,
329 void cursorUp(BufferView *, bool selecting = false) const;
331 void cursorDown(BufferView *, bool selecting = false) const;
333 void cursorLeft(BufferView *, bool internal = true) const;
335 void cursorRight(BufferView *, bool internal = true) const;
337 void cursorLeftOneWord(BufferView *) const;
339 void cursorRightOneWord(BufferView *) const;
341 void cursorUpParagraph(BufferView *) const;
343 void cursorDownParagraph(BufferView *) const;
345 void cursorHome(BufferView *) const;
347 void cursorEnd(BufferView *) const;
349 void cursorTab(BufferView *) const;
351 void cursorTop(BufferView *) const;
353 void cursorBottom(BufferView *) const;
355 void Delete(BufferView *);
357 void backspace(BufferView *);
359 bool selectWordWhenUnderCursor(BufferView *,
360 word_location const);
366 text_capitalization = 1,
370 /// Change the case of the word at cursor position.
371 void changeCase(BufferView *, TextCase action);
373 void transposeChars(BufferView &);
375 /** returns a printed row in a pixmap. The y value is needed to
376 decide, wether it is selected text or not. This is a strange
379 void getVisibleRow(BufferView *, int y_offset, int x_offset,
380 Row * row_ptr, int y, bool cleared=false);
383 void toggleInset(BufferView *);
385 void cutSelection(BufferView *, bool doclear = true, bool realcut = true);
387 void copySelection(BufferView *);
389 void pasteSelection(BufferView *);
391 void copyEnvironmentType();
393 void pasteEnvironmentType(BufferView *);
395 /** the DTP switches for paragraphs. LyX will store the top settings
396 always in the first physical paragraph, the bottom settings in the
397 last. When a paragraph is broken, the top settings rest, the bottom
398 settings are given to the new one. So I can make shure, they do not
399 duplicate themself (and you cannnot make dirty things with them! )
401 void setParagraph(BufferView *,
402 bool line_top, bool line_bottom,
403 bool pagebreak_top, bool pagebreak_bottom,
404 VSpace const & space_top,
405 VSpace const & space_bottom,
406 Spacing const & spacing,
408 string labelwidthstring,
411 /* these things are for search and replace */
413 /** sets the selection over the number of characters of string,
416 void setSelectionOverString(BufferView *, string const & str);
418 /** simple replacing. The font of the first selected character
421 void replaceSelectionWithString(BufferView *, string const & str);
423 /// needed to insert the selection
424 void insertStringAsLines(BufferView *, string const & str);
425 /// needed to insert the selection
426 void insertStringAsParagraphs(BufferView *, string const & str);
428 /// Find next inset of some specified type.
429 bool gotoNextInset(BufferView *, std::vector<Inset::Code> const & codes,
430 string const & contents = string()) const;
432 void gotoInset(BufferView * bv, std::vector<Inset::Code> const & codes,
435 void gotoInset(BufferView * bv, Inset::Code code, bool same_content);
438 /* for the greater insets */
440 /// returns false if inset wasn't found
441 bool updateInset(BufferView *, Inset *);
443 void checkParagraph(BufferView *, Paragraph * par, lyx::pos_type pos);
445 int workWidth(BufferView *) const;
447 int workWidth(BufferView *, Inset * inset) const;
449 void computeBidiTables(Buffer const *, Row * row) const;
451 /// Maps positions in the visual string to positions in logical string.
453 lyx::pos_type log2vis(lyx::pos_type pos) const {
454 if (bidi_start == -1)
457 return log2vis_list[pos-bidi_start];
460 /// Maps positions in the logical string to positions in visual string.
462 lyx::pos_type vis2log(lyx::pos_type pos) const {
463 if (bidi_start == -1)
466 return vis2log_list[pos-bidi_start];
470 lyx::pos_type bidi_level(lyx::pos_type pos) const {
471 if (bidi_start == -1)
474 return bidi_levels[pos-bidi_start];
478 bool bidi_InRange(lyx::pos_type pos) const {
479 return bidi_start == -1 ||
480 (bidi_start <= pos && pos <= bidi_end);
484 mutable Row * firstrow;
486 mutable Row * lastrow;
489 void cursorLeftOneWord(LyXCursor &) const;
491 void cursorPrevious(BufferView * bv);
493 void cursorNext(BufferView * bv);
496 float getCursorX(BufferView *, Row *, lyx::pos_type pos,
497 lyx::pos_type last, bool boundary) const;
499 void changeRegionCase(BufferView * bv,
500 LyXCursor const & from,
501 LyXCursor const & to,
502 LyXText::TextCase action);
503 /// used in setlayout
504 void makeFontEntriesLayoutSpecific(Buffer const *, Paragraph * par);
506 /** forces the redrawing of a paragraph. Needed when manipulating a
509 void redoDrawingOfParagraph(BufferView *, LyXCursor const & cursor);
511 /** Copybuffer for copy environment type.
512 Asger has learned that this should be a buffer-property instead
513 Lgb has learned that 'char' is a lousy type for non-characters
515 string copylayouttype;
517 /** inserts a new row behind the specified row, increments
518 the touched counters */
519 void insertRow(Row * row, Paragraph * par, lyx::pos_type pos) const;
520 /// removes the row and reset the touched counters
521 void removeRow(Row * row) const;
523 /// remove all following rows of the paragraph of the specified row.
524 void removeParagraph(Row * row) const;
526 /// insert the specified paragraph behind the specified row
527 void insertParagraph(BufferView *,
528 Paragraph * par, Row * row) const;
530 /** appends the implizit specified paragraph behind the specified row,
531 * start at the implizit given position */
532 void appendParagraph(BufferView *, Row * row) const;
535 void breakAgain(BufferView *, Row * row) const;
537 void breakAgainOneRow(BufferView *, Row * row);
538 /// Calculate and set the height of the row
539 void setHeightOfRow(BufferView *, Row * row_ptr) const;
541 /** this calculates the specified parameters. needed when setting
542 * the cursor and when creating a visible row */
543 void prepareToPrint(BufferView *, Row * row, float & x,
544 float & fill_separator,
546 float & fill_label_hfill,
547 bool bidi = true) const;
549 /// A struct used for drawing routines
550 struct DrawRowParams {
555 // the painter to use
557 // has the background been cleared
559 /// x offset (e.g. for insets)
561 /// y offset (e.g. for insets)
567 /// the inset/view full width
573 /// fill separator size
577 /// paint the background
578 bool paintRowBackground(DrawRowParams & p);
580 /// paint the selection background
581 void paintRowSelection(DrawRowParams & p);
583 /// paint appendix marker
584 void paintRowAppendix(DrawRowParams & p);
586 /// paint page break marker. Returns its height.
587 int paintPageBreak(string const & label, int y, DrawRowParams & p);
589 /// paint env depth bar
590 void paintRowDepthBar(DrawRowParams & p);
592 /// get the on-screen size of the length marker
593 int getLengthMarkerHeight(BufferView * bv, VSpace const & vsp) const;
595 /// paint an added space marker
596 int drawLengthMarker(DrawRowParams & p, string const & str,
597 VSpace const & vsp, int start);
599 /// paint a first row in a paragraph
600 void paintFirstRow(DrawRowParams & p);
602 /// paint a last row in a paragraph
603 void paintLastRow(DrawRowParams & p);
606 void paintRowText(DrawRowParams & p);
608 // fix the cursor `cur' after a characters has been deleted at `where'
609 // position. Called by deleteEmptyParagraphMechanism
610 void fixCursorAfterDelete(BufferView * bv,
612 LyXCursor const & where) const;
614 /// delete double space (false) or empty paragraphs (true) around old_cursor
615 bool deleteEmptyParagraphMechanism(BufferView *,
616 LyXCursor const & old_cursor) const;
619 /** Updates all counters starting BEHIND the row. Changed paragraphs
620 * with a dynamic left margin will be rebroken. */
621 void updateCounters(BufferView *) const;
623 void update(BufferView * bv, bool changed = true);
625 * Returns an inset if inset was hit, or 0 if not.
626 * If hit, the coordinates are changed relative to the inset.
628 Inset * checkInsetHit(BufferView * bv, int & x, int & y) const;
632 void setCounter(Buffer const *, Paragraph * par) const;
634 void deleteWordForward(BufferView *);
636 void deleteWordBackward(BufferView *);
638 void deleteLineForward(BufferView *);
641 * some low level functions
645 int singleWidth(BufferView *, Paragraph * par,
646 lyx::pos_type pos) const;
648 int singleWidth(BufferView *, Paragraph * par,
649 lyx::pos_type pos, char c) const;
652 /// draw normal chars
653 void drawChars(DrawRowParams & p, lyx::pos_type & vpos,
654 bool hebrew, bool arabic);
655 /// draw from arabic composed char
656 void drawArabicComposeChar(DrawRowParams & p, lyx::pos_type & vpos);
657 /// draw from hebrew composed char
658 void drawHebrewComposeChar(DrawRowParams & p, lyx::pos_type & vpos);
659 /// draw a mark for foreign language, starting from orig_x
660 void drawForeignMark(DrawRowParams & p, float const orig_x, LyXFont const & orig_font);
662 bool drawInset(DrawRowParams & p, lyx::pos_type const pos);
663 /// draw new line marker
664 void drawNewline(DrawRowParams & p, lyx::pos_type const pos);
666 bool draw(DrawRowParams & p, lyx::pos_type & vpos);
668 /// get the next breakpoint in a given paragraph
669 lyx::pos_type nextBreakPoint(BufferView *, Row const * row, int width) const;
670 /// returns the minimum space a row needs on the screen in pixel
671 int fill(BufferView *, Row * row, int workwidth) const;
673 /** returns the minimum space a manual label needs on the
675 int labelFill(BufferView *, Row const * row) const;
678 lyx::pos_type beginningOfMainBody(Buffer const *, Paragraph const * par) const;
681 * Returns the left beginning of the text.
682 * This information cannot be taken from the layout object, because
683 * in LaTeX the beginning of the text fits in some cases
684 * (for example sections) exactly the label-width.
686 int leftMargin(BufferView *, Row const * row) const;
688 int rightMargin(Buffer const *, Row const * row) const;
690 int labelEnd (BufferView *, Row const * row) const;
692 /** returns the number of separators in the specified row.
693 The separator on the very last column doesnt count
695 int numberOfSeparators(Buffer const *, Row const * row) const;
697 /** returns the number of hfills in the specified row. The
698 LyX-Hfill is a LaTeX \hfill so that the hfills at the
699 beginning and at the end were ignored. This is {\em MUCH}
700 more usefull than not to ignore!
702 int numberOfHfills(Buffer const *, Row const * row) const;
704 /// like NumberOfHfills, but only those in the manual label!
705 int numberOfLabelHfills(Buffer const *, Row const * row) const;
706 /** returns true, if a expansion is needed. Rules are given by
709 bool hfillExpansion(Buffer const *, Row const * row_ptr,
710 lyx::pos_type pos) const;
712 LColor::color backgroundColor();
716 mutable std::vector<lyx::pos_type> log2vis_list;
719 mutable std::vector<lyx::pos_type> vis2log_list;
722 mutable std::vector<lyx::pos_type> bidi_levels;
725 mutable lyx::pos_type bidi_start;
728 mutable lyx::pos_type bidi_end;
731 mutable bool bidi_same_direction;
734 unsigned char transformChar(unsigned char c, Paragraph * par,
735 lyx::pos_type pos) const;
737 /** returns the paragraph position of the last character in the
740 lyx::pos_type rowLast(Row const * row) const;
742 lyx::pos_type rowLastPrintable(Row const * row) const;
748 // special owner functions
750 Paragraph * ownerParagraph() const;
752 void ownerParagraph(Paragraph *) const;
753 // set it searching first for the right owner using the paragraph id
754 void ownerParagraph(int id, Paragraph *) const;