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 * ====================================================== */
21 #include "lyxcursor.h"
22 #include "lyxparagraph.h"
33 This class holds the mapping between buffer paragraphs and screen rows.
44 NEED_VERY_LITTLE_REFRESH,
50 LyXText(BufferView *);
57 void init(BufferView *);
59 mutable int number_of_rows;
63 mutable unsigned int width;
64 /// the current font settings
65 mutable LyXFont current_font;
67 mutable LyXFont real_current_font;
68 /// first visible pixel-row is set from LyXScreen!!!
69 // unsigned is wrong here for text-insets!
72 BufferView * bv_owner;
74 InsetText * inset_owner;
76 UpdatableInset * the_locking_inset;
79 int GetRealCursorX(BufferView *) const;
81 LyXFont const GetFont(Buffer const *, LyXParagraph * par,
82 LyXParagraph::size_type pos) const;
84 void SetCharFont(Buffer const *, LyXParagraph * par,
85 LyXParagraph::size_type pos, LyXFont const & font);
86 void SetCharFont(BufferView *, LyXParagraph * par,
87 LyXParagraph::size_type pos,
88 LyXFont const & font, bool toggleall);
89 /// returns a pointer to the very first LyXParagraph
90 LyXParagraph * FirstParagraph() const;
92 /// what you expect when pressing <enter> at cursor position
93 void BreakParagraph(BufferView *, char keep_layout = 0);
95 /** set layout over selection and make a total rebreak of
98 LyXParagraph * SetLayout(BufferView *, LyXCursor & actual_cursor,
99 LyXCursor & selection_start,
100 LyXCursor & selection_end,
101 LyXTextClass::size_type layout);
102 void SetLayout(BufferView *, LyXTextClass::size_type layout);
104 /// used in setlayout
105 void MakeFontEntriesLayoutSpecific(Buffer const *, LyXParagraph * par);
107 /** increment depth over selection and make a total rebreak of those
110 void IncDepth(BufferView *);
112 /** decrement depth over selection and make a total rebreak of those
114 void DecDepth(BufferView *);
116 /** Get the depth at current cursor position
118 int GetDepth() const { return cursor.par()->GetDepth(); }
120 /** set font over selection and make a total rebreak of those
122 toggleall defaults to false.
124 void SetFont(BufferView *, LyXFont const &, bool toggleall = false);
126 /** deletes and inserts again all paragaphs between the cursor
127 and the specified par. The Cursor is needed to set the refreshing
129 This function is needed after SetLayout and SetFont etc.
131 void RedoParagraphs(BufferView *, LyXCursor const & cursor,
132 LyXParagraph const * end_par) const;
134 void RedoParagraph(BufferView *) const;
137 void ToggleFree(BufferView *, LyXFont const &, bool toggleall = false);
139 /** recalculates the heights of all previous rows of the
140 specified paragraph. needed, if the last characters font
143 void RedoHeightOfParagraph(BufferView *, LyXCursor const & cursor);
145 /** forces the redrawing of a paragraph. Needed when manipulating a
148 void RedoDrawingOfParagraph(BufferView *, LyXCursor const & cursor);
150 /** insert a character, moves all the following breaks in the
151 same Paragraph one to the right and make a little rebreak
153 void InsertChar(BufferView *, char c);
155 void InsertInset(BufferView *, Inset * inset);
157 /** Completes the insertion with a full rebreak.
158 Returns true if something was broken. */
159 bool FullRebreak(BufferView *);
162 Row * need_break_row;
164 mutable int refresh_y;
172 mutable Row * refresh_row;
176 /** wether the screen needs a refresh,
177 starting with refresh_y
179 mutable text_status status;
181 /** returns a pointer to the row near the specified y-coordinate
182 (relative to the whole text). y is set to the real beginning
185 Row * GetRowNearY(int & y) const;
187 /** returns the column near the specified x-coordinate of the row
188 x is set to the real beginning of this column
190 int GetColumnNearX(BufferView *, Row * row,
191 int & x, bool & boundary) const;
193 /** returns a pointer to a specified row. y is set to the beginning
196 Row * GetRow(LyXParagraph * par,
197 LyXParagraph::size_type pos, int & y) const;
199 /** returns the height of a default row, needed for scrollbar
201 int DefaultHeight() const;
204 Later this variable has to be removed. There should be now internal
205 cursor in a text (and thus not in a buffer). By keeping this it is
206 (I think) impossible to have several views with the same buffer, but
207 the cursor placed at different places.
209 Since the LyXText now has been moved from Buffer to BufferView
210 it should not be absolutely needed to move the cursor...
212 mutable LyXCursor cursor;
215 /* the selection cursor */
217 mutable bool selection;
219 mutable bool mark_set;
222 mutable LyXCursor sel_cursor;
224 mutable LyXCursor sel_start_cursor;
226 mutable LyXCursor sel_end_cursor;
228 /** The structrue that keeps track of the selections set. */
246 bool set_; // former selection
247 bool mark_; // former mark_set
250 mutable Selection selection;
252 /// needed for the toggling
253 LyXCursor last_sel_cursor;
255 LyXCursor toggle_cursor;
257 LyXCursor toggle_end_cursor;
259 /// need the selection cursor:
260 void SetSelection(BufferView *);
262 void ClearSelection(BufferView *) const;
264 string const selectionAsString(Buffer const *) const;
266 /// just selects the word the cursor is in
267 void SelectWord(BufferView *);
269 /** 'selects" the next word, where the cursor is not in
270 and returns this word as string. THe cursor will be moved
271 to the beginning of this word.
272 With SelectSelectedWord can this be highlighted really
274 string const SelectNextWord(BufferView *, float & value) const;
276 void SelectSelectedWord(BufferView *);
278 void SetCursor(BufferView *, LyXParagraph * par,
279 LyXParagraph::size_type pos,
281 bool boundary = false) const;
283 void SetCursor(BufferView *, LyXCursor &, LyXParagraph * par,
284 LyXParagraph::size_type pos,
285 bool boundary = false) const;
287 void SetCursorIntern(BufferView *, LyXParagraph * par,
288 LyXParagraph::size_type pos,
290 bool boundary = false) const;
292 void SetCurrentFont(BufferView *) const;
295 bool IsBoundary(Buffer const *, LyXParagraph * par,
296 LyXParagraph::size_type pos) const;
298 bool IsBoundary(Buffer const *, LyXParagraph * par,
299 LyXParagraph::size_type pos,
300 LyXFont const & font) const;
303 void SetCursorFromCoordinates(BufferView *, int x, int y) const;
305 void SetCursorFromCoordinates(BufferView *, LyXCursor &,
308 void CursorUp(BufferView *) const;
310 void CursorDown(BufferView *) const;
312 void CursorLeft(BufferView *, bool internal = true) const;
314 void CursorRight(BufferView *, bool internal = true) const;
316 void CursorLeftOneWord(BufferView *) const;
318 void CursorRightOneWord(BufferView *) const;
320 void CursorUpParagraph(BufferView *) const;
322 void CursorDownParagraph(BufferView *) const;
324 void CursorHome(BufferView *) const;
326 void CursorEnd(BufferView *) const;
328 void CursorTab(BufferView *) const;
330 void CursorTop(BufferView *) const;
332 void CursorBottom(BufferView *) const;
334 void Delete(BufferView *);
336 void Backspace(BufferView *);
338 void DeleteWordForward(BufferView *);
340 void DeleteWordBackward(BufferView *);
342 void DeleteLineForward(BufferView *);
344 bool SelectWordWhenUnderCursor(BufferView *);
350 text_capitalization = 1,
354 /// Change the case of the word at cursor position.
355 void ChangeWordCase(BufferView *, TextCase action);
356 void TransposeChars(BufferView const &);
358 /** returns a printed row in a pixmap. The y value is needed to
359 decide, wether it is selected text or not. This is a strange
362 void GetVisibleRow(BufferView *, int y_offset, int x_offset,
363 Row * row_ptr, int y, bool cleared=false);
366 void CutSelection(BufferView *, bool = true);
368 void CopySelection(BufferView *);
370 void PasteSelection(BufferView *);
372 void copyEnvironmentType();
374 void pasteEnvironmentType(BufferView *);
376 void InsertFootnote();
378 void InsertMarginpar();
382 void InsertTabular();
384 /** the DTP switches for paragraphs. LyX will store the top settings
385 always in the first physical paragraph, the bottom settings in the
386 last. When a paragraph is broken, the top settings rest, the bottom
387 settings are given to the new one. So I can make shure, they do not
388 duplicate themself (and you cannnot make dirty things with them! )
390 void SetParagraph(BufferView *,
391 bool line_top, bool line_bottom,
392 bool pagebreak_top, bool pagebreak_bottom,
393 VSpace const & space_top,
394 VSpace const & space_bottom,
396 string labelwidthstring,
399 /* these things are for search and replace */
401 /** sets the selection over the number of characters of string,
404 void SetSelectionOverString(BufferView *, string const & str);
406 /** simple replacing. The font of the first selected character
409 void ReplaceSelectionWithString(BufferView *, string const & str);
411 /// needed to insert the selection
412 void InsertStringA(BufferView *, string const & str);
413 /// needed to insert the selection
414 void InsertStringB(BufferView *, string const & str);
416 /// Find next inset of some specified type.
417 bool GotoNextInset(BufferView *, std::vector<Inset::Code> const & codes,
418 string const & contents = string()) const;
421 /* for the greater insets */
423 /// returns false if inset wasn't found
424 bool UpdateInset(BufferView *, Inset *);
426 void CheckParagraph(BufferView *, LyXParagraph * par,
427 LyXParagraph::size_type pos);
429 int NumberOfCell(LyXParagraph * par,
430 LyXParagraph::size_type pos) const;
432 LyXParagraph * GetParFromID(int id);
435 /// returns false if no undo possible
436 bool TextUndo(BufferView *);
437 /// returns false if no redo possible
438 bool TextRedo(BufferView *);
439 /// used by TextUndo/TextRedo
440 bool TextHandleUndo(BufferView *, Undo * undo);
441 /// makes sure the next operation will be stored
443 /// this is dangerous and for internal use only
445 /// this is dangerous and for internal use only
447 /// the flag used by FinishUndo();
448 mutable bool undo_finished;
452 void SetUndo(Buffer *, Undo::undo_kind kind,
453 LyXParagraph const * before,
454 LyXParagraph const * end) const;
456 void SetRedo(Buffer *, Undo::undo_kind kind,
457 LyXParagraph const * before,
458 LyXParagraph const * end);
460 Undo * CreateUndo(Buffer *, Undo::undo_kind kind,
461 LyXParagraph const * before,
462 LyXParagraph const * end) const;
463 /// for external use in lyx_cb.C
464 void SetCursorParUndo(Buffer *);
466 void RemoveTableRow(LyXCursor & cursor) const;
468 bool IsEmptyTableCell() const;
470 void toggleAppendix(BufferView *);
472 int workWidth(BufferView *) const;
474 void ComputeBidiTables(Buffer const *, Row * row) const;
476 /// Maps positions in the visual string to positions in logical string.
478 LyXParagraph::size_type log2vis(LyXParagraph::size_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 LyXParagraph::size_type vis2log(LyXParagraph::size_type pos) const {
488 if (bidi_start == -1)
491 return vis2log_list[pos-bidi_start];
495 LyXParagraph::size_type bidi_level(LyXParagraph::size_type pos) const {
496 if (bidi_start == -1)
499 return bidi_levels[pos-bidi_start];
503 bool bidi_InRange(LyXParagraph::size_type pos) const {
504 return bidi_start == -1 ||
505 (bidi_start <= pos && pos <= bidi_end);
511 mutable Row * firstrow;
513 mutable Row * lastrow;
515 /** Copybuffer for copy environment type.
516 Asger has learned that this should be a buffer-property instead
517 Lgb has learned that 'char' is a lousy type for non-characters
519 LyXTextClass::size_type copylayouttype;
521 /** inserts a new row behind the specified row, increments
522 the touched counters */
523 void InsertRow(Row * row, LyXParagraph * par,
524 LyXParagraph::size_type pos) const;
525 /** removes the row and reset the touched counters */
526 void RemoveRow(Row * row) const;
528 /** remove all following rows of the paragraph of the specified row. */
529 void RemoveParagraph(Row * row) const;
531 /** insert the specified paragraph behind the specified row */
532 void InsertParagraph(BufferView *,
533 LyXParagraph * par, Row * row) const;
535 /** appends the implizit specified paragraph behind the specified row,
536 * start at the implizit given position */
537 void AppendParagraph(BufferView *, Row * row) const;
540 void BreakAgain(BufferView *, Row * row) const;
542 void BreakAgainOneRow(BufferView *, Row * row);
543 /// Calculate and set the height of the row
544 void SetHeightOfRow(BufferView *, Row * row_ptr) const;
546 /** this calculates the specified parameters. needed when setting
547 * the cursor and when creating a visible row */
548 void PrepareToPrint(BufferView *, Row * row, float & x,
549 float & fill_separator,
551 float & fill_label_hfill,
552 bool bidi = true) const;
555 void DeleteEmptyParagraphMechanism(BufferView *,
556 LyXCursor const & old_cursor) const;
558 /** Updates all counters starting BEHIND the row. Changed paragraphs
559 * with a dynamic left margin will be rebroken. */
560 void UpdateCounters(BufferView *, Row * row) const;
562 void SetCounter(Buffer const *, LyXParagraph * par) const;
565 * some low level functions
569 int SingleWidth(BufferView *, LyXParagraph * par,
570 LyXParagraph::size_type pos) const;
572 int SingleWidth(BufferView *, LyXParagraph * par,
573 LyXParagraph::size_type pos, char c) const;
575 void draw(BufferView *, Row const * row,
576 LyXParagraph::size_type & pos,
577 int offset, float & x, bool cleared);
579 /// get the next breakpoint in a given paragraph
580 LyXParagraph::size_type NextBreakPoint(BufferView *, Row const * row,
582 /// returns the minimum space a row needs on the screen in pixel
583 int Fill(BufferView *, Row * row, int workwidth) const;
585 /** returns the minimum space a manual label needs on the
587 int LabelFill(BufferView *, Row const * row) const;
590 LyXParagraph::size_type
591 BeginningOfMainBody(Buffer const *, LyXParagraph const * par) const;
593 /** Returns the left beginning of the text.
594 This information cannot be taken from the layouts-objekt, because
595 in LaTeX the beginning of the text fits in some cases
596 (for example sections) exactly the label-width.
598 int LeftMargin(BufferView *, Row const * row) const;
600 int RightMargin(Buffer const *, Row const * row) const;
602 int LabelEnd (BufferView *, Row const * row) const;
604 /** returns the number of separators in the specified row.
605 The separator on the very last column doesnt count
607 int NumberOfSeparators(Buffer const *, Row const * row) const;
609 /** returns the number of hfills in the specified row. The
610 LyX-Hfill is a LaTeX \hfill so that the hfills at the
611 beginning and at the end were ignored. This is {\em MUCH}
612 more usefull than not to ignore!
614 int NumberOfHfills(Buffer const *, Row const * row) const;
616 /// like NumberOfHfills, but only those in the manual label!
617 int NumberOfLabelHfills(Buffer const *, Row const * row) const;
618 /** returns true, if a expansion is needed. Rules are given by
621 bool HfillExpansion(Buffer const *, Row const * row_ptr,
622 LyXParagraph::size_type pos) const;
626 mutable std::vector<LyXParagraph::size_type> log2vis_list;
629 mutable std::vector<LyXParagraph::size_type> vis2log_list;
632 mutable std::vector<LyXParagraph::size_type> bidi_levels;
635 mutable LyXParagraph::size_type bidi_start;
638 mutable LyXParagraph::size_type bidi_end;
641 mutable bool bidi_same_direction;
644 unsigned char TransformChar(unsigned char c, LyXParagraph * par,
645 LyXParagraph::size_type pos) const;
647 /** returns the paragraph position of the last character in the
650 LyXParagraph::size_type RowLast(Row const * row) const;
652 LyXParagraph::size_type RowLastPrintable(Row const * row) const;
657 // special owner functions
659 LyXParagraph * OwnerParagraph() const;
661 LyXParagraph * OwnerParagraph(LyXParagraph *) const;
665 /* returns a pointer to the row near the specified y-coordinate
666 * (relative to the whole text). y is set to the real beginning
669 Row * LyXText::GetRowNearY(int & y) const
671 // If possible we should optimize this method. (Lgb)
672 Row * tmprow = firstrow;
675 while (tmprow->next() && tmpy + tmprow->height() <= y) {
676 tmpy += tmprow->height();
677 tmprow = tmprow->next();
680 y = tmpy; // return the real y