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 *);
76 void init(BufferView *, bool reinit = false);
78 mutable int number_of_rows;
82 mutable unsigned int width;
83 /// the current font settings
84 mutable LyXFont current_font;
86 mutable LyXFont real_current_font;
87 /// first visible pixel-row is set from LyXScreen!!!
88 // unsigned is wrong here for text-insets!
91 BufferView * bv_owner;
93 InsetText * inset_owner;
95 UpdatableInset * the_locking_inset;
98 int getRealCursorX(BufferView *) const;
100 LyXFont const getFont(Buffer const *, Paragraph * par,
101 lyx::pos_type pos) const;
103 LyXFont const getLayoutFont(Buffer const *, Paragraph * par) const;
105 LyXFont const getLabelFont(Buffer const *, Paragraph * par) const;
107 void setCharFont(Buffer const *, Paragraph * par,
108 lyx::pos_type pos, LyXFont const & font);
109 void setCharFont(BufferView *, Paragraph * par,
110 lyx::pos_type pos, LyXFont const & font, bool toggleall);
112 /// what you expect when pressing <enter> at cursor position
113 void breakParagraph(BufferView *, char keep_layout = 0);
115 /** set layout over selection and make a total rebreak of
118 Paragraph * setLayout(BufferView *, LyXCursor & actual_cursor,
119 LyXCursor & selection_start,
120 LyXCursor & selection_end,
121 string const & layout);
123 void setLayout(BufferView *, string const & layout);
125 /** increment depth over selection and make a total rebreak of those
128 void incDepth(BufferView *);
130 /** decrement depth over selection and make a total rebreak of those
132 void decDepth(BufferView *);
134 /// get the depth at current cursor position
135 int getDepth() const;
137 /** set font over selection and make a total rebreak of those
139 toggleall defaults to false.
141 void setFont(BufferView *, LyXFont const &, bool toggleall = false);
143 /** deletes and inserts again all paragaphs between the cursor
144 and the specified par. The Cursor is needed to set the refreshing
146 This function is needed after SetLayout and SetFont etc.
148 void redoParagraphs(BufferView *, LyXCursor const & cursor,
149 Paragraph const * end_par) const;
151 void redoParagraph(BufferView *) const;
154 void toggleFree(BufferView *, LyXFont const &, bool toggleall = false);
157 string getStringToIndex(BufferView *);
159 /** recalculates the heights of all previous rows of the
160 specified paragraph. needed, if the last characters font
163 void redoHeightOfParagraph(BufferView *, LyXCursor const & cursor);
165 /** insert a character, moves all the following breaks in the
166 same Paragraph one to the right and make a little rebreak
168 void insertChar(BufferView *, char c);
170 void insertInset(BufferView *, Inset * inset);
172 /// Completes the insertion with a full rebreak
173 void fullRebreak(BufferView *);
176 mutable Row * need_break_row;
178 mutable int refresh_y;
184 mutable Row * refresh_row;
186 lyx::pos_type refresh_pos;
188 /// give and set the LyXText status
189 text_status status() const;
191 void status(BufferView *, text_status) const;
194 Inset::RESULT dispatch(FuncRequest const & cmd);
197 /** wether the screen needs a refresh,
198 starting with refresh_y
200 mutable text_status status_;
203 /** returns a pointer to the row near the specified y-coordinate
204 (relative to the whole text). y is set to the real beginning
207 Row * getRowNearY(int & y) const;
209 /** returns the column near the specified x-coordinate of the row
210 x is set to the real beginning of this column
212 lyx::pos_type getColumnNearX(BufferView *, Row * row,
213 int & x, bool & boundary) const;
215 /** returns a pointer to a specified row. y is set to the beginning
218 Row * getRow(Paragraph * par,
219 lyx::pos_type pos, int & y) const;
220 /** returns the firstrow, this could be done with the above too but
221 IMO it's stupid to have to allocate a dummy y all the time I need
224 Row * firstRow() { return firstrow; }
226 /** returns the height of a default row, needed for scrollbar
228 int defaultHeight() const;
231 Later this variable has to be removed. There should be now internal
232 cursor in a text (and thus not in a buffer). By keeping this it is
233 (I think) impossible to have several views with the same buffer, but
234 the cursor placed at different places.
236 Since the LyXText now has been moved from Buffer to BufferView
237 it should not be absolutely needed to move the cursor...
239 mutable LyXCursor cursor; // actual cursor position
241 /** The structure that keeps track of the selections set. */
244 : set_(false), mark_(false)
258 LyXCursor cursor; // temporary cursor to hold a cursor position
259 // until setSelection is called!
260 LyXCursor start; // start of a REAL selection
261 LyXCursor end; // end of a REAL selection
263 bool set_; // former selection
264 bool mark_; // former mark_set
267 mutable Selection selection;
268 // this is used to handle XSelection events in the right manner
269 mutable Selection xsel_cache;
271 /// needed for the toggling (cursor position on last selection made)
272 mutable LyXCursor last_sel_cursor;
273 /// needed for toggling the selection in screen.C
274 mutable LyXCursor toggle_cursor;
275 /// needed for toggling the selection in screen.C
276 mutable LyXCursor toggle_end_cursor;
278 /// need the selection cursor:
279 void setSelection(BufferView *);
281 void clearSelection() const;
283 string const selectionAsString(Buffer const *, bool label) const;
285 /// select the word we need depending on word_location
286 void getWord(LyXCursor & from, LyXCursor & to,
287 word_location const) const;
288 /// just selects the word the cursor is in
289 void selectWord(BufferView *, word_location const);
290 /// returns the inset at cursor (if it exists), 0 otherwise
291 Inset * getInset() const;
293 /** 'selects" the next word, where the cursor is not in
294 and returns this word as string. THe cursor will be moved
295 to the beginning of this word.
296 With SelectSelectedWord can this be highlighted really
298 WordLangTuple const selectNextWordToSpellcheck(BufferView *, float & value) const;
300 void selectSelectedWord(BufferView *);
301 /// returns true if par was empty and was removed
302 bool setCursor(BufferView *, Paragraph * par,
305 bool boundary = false) const;
307 void setCursor(BufferView *, LyXCursor &, Paragraph * par,
309 bool boundary = false) const;
311 void setCursorIntern(BufferView *, Paragraph * par,
314 bool boundary = false) const;
316 void setCurrentFont(BufferView *) const;
319 bool isBoundary(Buffer const *, Paragraph * par,
320 lyx::pos_type pos) const;
322 bool isBoundary(Buffer const *, Paragraph * par,
324 LyXFont const & font) const;
327 void setCursorFromCoordinates(BufferView *, int x, int y) const;
329 void setCursorFromCoordinates(BufferView *, LyXCursor &,
332 void cursorUp(BufferView *, bool selecting = false) const;
334 void cursorDown(BufferView *, bool selecting = false) const;
336 void cursorLeft(BufferView *, bool internal = true) const;
338 void cursorRight(BufferView *, bool internal = true) const;
340 void cursorLeftOneWord(BufferView *) const;
342 void cursorRightOneWord(BufferView *) const;
344 void cursorUpParagraph(BufferView *) const;
346 void cursorDownParagraph(BufferView *) const;
348 void cursorHome(BufferView *) const;
350 void cursorEnd(BufferView *) const;
352 void cursorTab(BufferView *) const;
354 void cursorTop(BufferView *) const;
356 void cursorBottom(BufferView *) const;
358 void Delete(BufferView *);
360 void backspace(BufferView *);
362 void deleteWordForward(BufferView *);
364 void deleteWordBackward(BufferView *);
366 void deleteLineForward(BufferView *);
368 bool selectWordWhenUnderCursor(BufferView *,
369 word_location const);
375 text_capitalization = 1,
379 /// Change the case of the word at cursor position.
380 void changeCase(BufferView *, TextCase action);
382 void transposeChars(BufferView &);
384 /** returns a printed row in a pixmap. The y value is needed to
385 decide, wether it is selected text or not. This is a strange
388 void getVisibleRow(BufferView *, int y_offset, int x_offset,
389 Row * row_ptr, int y, bool cleared=false);
392 void toggleInset(BufferView *);
394 void cutSelection(BufferView *, bool doclear = true, bool realcut = true);
396 void copySelection(BufferView *);
398 void pasteSelection(BufferView *);
400 void copyEnvironmentType();
402 void pasteEnvironmentType(BufferView *);
404 /** the DTP switches for paragraphs. LyX will store the top settings
405 always in the first physical paragraph, the bottom settings in the
406 last. When a paragraph is broken, the top settings rest, the bottom
407 settings are given to the new one. So I can make shure, they do not
408 duplicate themself (and you cannnot make dirty things with them! )
410 void setParagraph(BufferView *,
411 bool line_top, bool line_bottom,
412 bool pagebreak_top, bool pagebreak_bottom,
413 VSpace const & space_top,
414 VSpace const & space_bottom,
415 Spacing const & spacing,
417 string labelwidthstring,
420 /* these things are for search and replace */
422 /** sets the selection over the number of characters of string,
425 void setSelectionOverString(BufferView *, string const & str);
427 /** simple replacing. The font of the first selected character
430 void replaceSelectionWithString(BufferView *, string const & str);
432 /// needed to insert the selection
433 void insertStringAsLines(BufferView *, string const & str);
434 /// needed to insert the selection
435 void insertStringAsParagraphs(BufferView *, string const & str);
437 /// Find next inset of some specified type.
438 bool gotoNextInset(BufferView *, std::vector<Inset::Code> const & codes,
439 string const & contents = string()) const;
442 /* for the greater insets */
444 /// returns false if inset wasn't found
445 bool updateInset(BufferView *, Inset *);
447 void checkParagraph(BufferView *, Paragraph * par, lyx::pos_type pos);
449 int workWidth(BufferView *) const;
451 int workWidth(BufferView *, Inset * inset) const;
453 void computeBidiTables(Buffer const *, Row * row) const;
455 /// Maps positions in the visual string to positions in logical string.
457 lyx::pos_type log2vis(lyx::pos_type pos) const {
458 if (bidi_start == -1)
461 return log2vis_list[pos-bidi_start];
464 /// Maps positions in the logical string to positions in visual string.
466 lyx::pos_type vis2log(lyx::pos_type pos) const {
467 if (bidi_start == -1)
470 return vis2log_list[pos-bidi_start];
474 lyx::pos_type bidi_level(lyx::pos_type pos) const {
475 if (bidi_start == -1)
478 return bidi_levels[pos-bidi_start];
482 bool bidi_InRange(lyx::pos_type pos) const {
483 return bidi_start == -1 ||
484 (bidi_start <= pos && pos <= bidi_end);
488 mutable Row * firstrow;
491 mutable Row * lastrow;
494 void cursorLeftOneWord(LyXCursor &) const;
497 float getCursorX(BufferView *, Row *, lyx::pos_type pos,
498 lyx::pos_type last, bool boundary) const;
500 void changeRegionCase(BufferView * bview,
501 LyXCursor const & from,
502 LyXCursor const & to,
503 LyXText::TextCase action);
504 /// used in setlayout
505 void makeFontEntriesLayoutSpecific(Buffer const *, Paragraph * par);
507 /** forces the redrawing of a paragraph. Needed when manipulating a
510 void redoDrawingOfParagraph(BufferView *, LyXCursor const & cursor);
512 /** Copybuffer for copy environment type.
513 Asger has learned that this should be a buffer-property instead
514 Lgb has learned that 'char' is a lousy type for non-characters
516 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 * bview,
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;
624 void setCounter(Buffer const *, Paragraph * par) const;
626 Inset::RESULT lfunAppendix(FuncRequest const & cmd);
629 * some low level functions
633 int singleWidth(BufferView *, Paragraph * par,
634 lyx::pos_type pos) const;
636 int singleWidth(BufferView *, Paragraph * par,
637 lyx::pos_type pos, char c) const;
640 /// draw normal chars
641 void drawChars(DrawRowParams & p, lyx::pos_type & vpos,
642 bool hebrew, bool arabic);
643 /// draw from arabic composed char
644 void drawArabicComposeChar(DrawRowParams & p, lyx::pos_type & vpos);
645 /// draw from hebrew composed char
646 void drawHebrewComposeChar(DrawRowParams & p, lyx::pos_type & vpos);
647 /// draw a mark for foreign language, starting from orig_x
648 void drawForeignMark(DrawRowParams & p, float const orig_x, LyXFont const & orig_font);
650 bool drawInset(DrawRowParams & p, lyx::pos_type const pos);
651 /// draw new line marker
652 void drawNewline(DrawRowParams & p, lyx::pos_type const pos);
654 bool draw(DrawRowParams & p, lyx::pos_type & vpos);
656 /// get the next breakpoint in a given paragraph
657 lyx::pos_type nextBreakPoint(BufferView *, Row const * row, int width) const;
658 /// returns the minimum space a row needs on the screen in pixel
659 int fill(BufferView *, Row * row, int workwidth) const;
661 /** returns the minimum space a manual label needs on the
663 int labelFill(BufferView *, Row const * row) const;
666 lyx::pos_type beginningOfMainBody(Buffer const *, Paragraph const * par) const;
669 * Returns the left beginning of the text.
670 * This information cannot be taken from the layout object, because
671 * in LaTeX the beginning of the text fits in some cases
672 * (for example sections) exactly the label-width.
674 int leftMargin(BufferView *, Row const * row) const;
676 int rightMargin(Buffer const *, Row const * row) const;
678 int labelEnd (BufferView *, Row const * row) const;
680 /** returns the number of separators in the specified row.
681 The separator on the very last column doesnt count
683 int numberOfSeparators(Buffer const *, Row const * row) const;
685 /** returns the number of hfills in the specified row. The
686 LyX-Hfill is a LaTeX \hfill so that the hfills at the
687 beginning and at the end were ignored. This is {\em MUCH}
688 more usefull than not to ignore!
690 int numberOfHfills(Buffer const *, Row const * row) const;
692 /// like NumberOfHfills, but only those in the manual label!
693 int numberOfLabelHfills(Buffer const *, Row const * row) const;
694 /** returns true, if a expansion is needed. Rules are given by
697 bool hfillExpansion(Buffer const *, Row const * row_ptr,
698 lyx::pos_type pos) const;
700 LColor::color backgroundColor();
704 mutable std::vector<lyx::pos_type> log2vis_list;
707 mutable std::vector<lyx::pos_type> vis2log_list;
710 mutable std::vector<lyx::pos_type> bidi_levels;
713 mutable lyx::pos_type bidi_start;
716 mutable lyx::pos_type bidi_end;
719 mutable bool bidi_same_direction;
722 unsigned char transformChar(unsigned char c, Paragraph * par,
723 lyx::pos_type pos) const;
725 /** returns the paragraph position of the last character in the
728 lyx::pos_type rowLast(Row const * row) const;
730 lyx::pos_type rowLastPrintable(Row const * row) const;
736 // special owner functions
738 Paragraph * ownerParagraph() const;
740 void ownerParagraph(Paragraph *) const;
741 // set it searching first for the right owner using the paragraph id
742 void ownerParagraph(int id, Paragraph *) const;