2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-2000 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 LyXFont const GetFont(Buffer const *, LyXParagraph * par,
80 LyXParagraph::size_type pos) const;
82 void SetCharFont(Buffer const *, LyXParagraph * par,
83 LyXParagraph::size_type pos,
84 LyXFont const & font);
85 /// returns a pointer to the very first LyXParagraph
86 LyXParagraph * FirstParagraph() const;
88 /// what you expect when pressing <enter> at cursor position
89 void BreakParagraph(BufferView *, char keep_layout = 0);
91 /** set layout over selection and make a total rebreak of
94 LyXParagraph * SetLayout(BufferView *, LyXCursor & actual_cursor,
95 LyXCursor & selection_start,
96 LyXCursor & selection_end,
97 LyXTextClass::size_type layout);
98 void SetLayout(BufferView *, LyXTextClass::size_type layout);
100 /// used in setlayout
101 void MakeFontEntriesLayoutSpecific(Buffer const *, LyXParagraph * par);
103 /** increment depth over selection and make a total rebreak of those
106 void IncDepth(BufferView *);
108 /** decrement depth over selection and make a total rebreak of those
110 void DecDepth(BufferView *);
112 /** Get the depth at current cursor position
114 int GetDepth() const { return cursor.par()->GetDepth(); }
116 /** set font over selection and make a total rebreak of those
118 toggleall defaults to false.
120 void SetFont(BufferView *, LyXFont const &, bool toggleall = false);
122 /** deletes and inserts again all paragaphs between the cursor
123 and the specified par. The Cursor is needed to set the refreshing
125 This function is needed after SetLayout and SetFont etc.
127 void RedoParagraphs(BufferView *, LyXCursor const & cursor,
128 LyXParagraph const * end_par) const;
130 void RedoParagraph(BufferView *) const;
133 void ToggleFree(BufferView *, LyXFont const &, bool toggleall = false);
135 /** recalculates the heights of all previous rows of the
136 specified paragraph. needed, if the last characters font
139 void RedoHeightOfParagraph(BufferView *, LyXCursor const & cursor);
141 /** forces the redrawing of a paragraph. Needed when manipulating a
144 void RedoDrawingOfParagraph(BufferView *, LyXCursor const & cursor);
146 /** insert a character, moves all the following breaks in the
147 same Paragraph one to the right and make a little rebreak
149 void InsertChar(BufferView *, char c);
151 void InsertInset(BufferView *, Inset * inset);
153 /** Completes the insertion with a full rebreak.
154 Returns true if something was broken. */
155 bool FullRebreak(BufferView *);
159 LyXParagraph::footnote_flag GetFootnoteFlag(int row);
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;
214 /* the selection cursor */
216 mutable bool selection;
218 mutable bool mark_set;
221 mutable LyXCursor sel_cursor;
223 LyXCursor sel_start_cursor;
225 mutable LyXCursor sel_end_cursor;
226 /// needed for the toggling
227 LyXCursor last_sel_cursor;
229 LyXCursor toggle_cursor;
231 LyXCursor toggle_end_cursor;
233 /// need the selection cursor:
236 void ClearSelection() const;
238 string const selectionAsString(Buffer const *) const;
240 /// just selects the word the cursor is in
241 void SelectWord(BufferView *);
243 /** 'selects" the next word, where the cursor is not in
244 and returns this word as string. THe cursor will be moved
245 to the beginning of this word.
246 With SelectSelectedWord can this be highlighted really
248 string const SelectNextWord(BufferView *, float & value) const;
250 void SelectSelectedWord(BufferView *);
252 void SetCursor(BufferView *, LyXParagraph * par,
253 LyXParagraph::size_type pos,
255 bool boundary = false) const;
257 void SetCursor(BufferView *, LyXCursor &, LyXParagraph * par,
258 LyXParagraph::size_type pos,
259 bool boundary = false) const;
261 void SetCursorIntern(BufferView *, LyXParagraph * par,
262 LyXParagraph::size_type pos,
264 bool boundary = false) const;
266 void SetCurrentFont(BufferView *) const;
269 bool IsBoundary(Buffer const *, LyXParagraph * par,
270 LyXParagraph::size_type pos) const;
272 bool IsBoundary(Buffer const *, LyXParagraph * par,
273 LyXParagraph::size_type pos,
274 LyXFont const & font) const;
277 void SetCursorFromCoordinates(BufferView *, int x, int y) const;
279 void SetCursorFromCoordinates(BufferView *, LyXCursor &,
282 void CursorUp(BufferView *) const;
284 void CursorDown(BufferView *) const;
286 void CursorLeft(BufferView *, bool internal = true) const;
288 void CursorRight(BufferView *, bool internal = true) const;
290 void CursorLeftOneWord(BufferView *) const;
292 void CursorRightOneWord(BufferView *) const;
294 void CursorUpParagraph(BufferView *) const;
296 void CursorDownParagraph(BufferView *) const;
298 void CursorHome(BufferView *) const;
300 void CursorEnd(BufferView *) const;
302 void CursorTab(BufferView *) const;
304 void CursorTop(BufferView *) const;
306 void CursorBottom(BufferView *) const;
308 void Delete(BufferView *);
310 void Backspace(BufferView *);
312 void DeleteWordForward(BufferView *);
314 void DeleteWordBackward(BufferView *);
316 void DeleteLineForward(BufferView *);
318 bool SelectWordWhenUnderCursor(BufferView *);
324 text_capitalization = 1,
328 /// Change the case of the word at cursor position.
329 void ChangeWordCase(BufferView *, TextCase action);
331 /** returns a printed row in a pixmap. The y value is needed to
332 decide, wether it is selected text or not. This is a strange
335 void GetVisibleRow(BufferView *, int y_offset, int x_offset,
336 Row * row_ptr, int y, bool cleared=false);
341 void ToggleFootnote(BufferView *);
343 void OpenStuff(BufferView *);
345 void OpenFootnotes();
347 void OpenFootnote(BufferView *);
349 void CloseFootnotes();
351 void CloseFootnote(BufferView *);
353 /** turn the selection into a new environment. If there is no
354 selection, create an empty environment
356 void InsertFootnoteEnvironment(BufferView *,
357 LyXParagraph::footnote_kind kind);
359 void MeltFootnoteEnvironment(BufferView *);
362 void CutSelection(BufferView *, bool = true);
364 void CopySelection(BufferView *);
366 void PasteSelection(BufferView *);
368 void copyEnvironmentType();
370 void pasteEnvironmentType(BufferView *);
372 void InsertFootnote();
374 void InsertMarginpar();
378 void InsertTabular();
380 /** the DTP switches for paragraphs. LyX will store the top settings
381 always in the first physical paragraph, the bottom settings in the
382 last. When a paragraph is broken, the top settings rest, the bottom
383 settings are given to the new one. So I can make shure, they do not
384 duplicate themself (and you cannnot make dirty things with them! )
386 void SetParagraph(BufferView *,
387 bool line_top, bool line_bottom,
388 bool pagebreak_top, bool pagebreak_bottom,
389 VSpace const & space_top,
390 VSpace const & space_bottom,
392 string labelwidthstring,
395 void SetParagraphExtraOpt(BufferView *, int type,
396 string const & width,
397 string const & widthp,
398 int alignment, bool hfill,
399 bool start_minipage);
401 /* these things are for search and replace */
403 /** returns true if the specified string is at the specified
406 bool IsStringInText(LyXParagraph * par,
407 LyXParagraph::size_type pos,
408 string const & str) const;
409 /** sets the selection over the number of characters of string,
412 void SetSelectionOverString(BufferView *, string const & str);
414 /** simple replacing. The font of the first selected character
417 void ReplaceSelectionWithString(BufferView *, string const & str);
419 /** if the string can be found: return true and set the cursor to
421 bool SearchForward(BufferView *, string const & str) const;
423 bool SearchBackward(BufferView *, string const & str) const;
425 /// needed to insert the selection
426 void InsertStringA(BufferView *, string const & str);
427 /// needed to insert the selection
428 void InsertStringB(BufferView *, string const & str);
430 /// Find next inset of some specified type.
431 bool GotoNextInset(BufferView *, std::vector<Inset::Code> const & codes,
432 string const & contents = string()) const;
435 /* for the greater insets */
437 /// returns false if inset wasn't found
438 bool UpdateInset(BufferView *, Inset *);
440 void CheckParagraph(BufferView *, LyXParagraph * par,
441 LyXParagraph::size_type pos);
443 int NumberOfCell(LyXParagraph * par,
444 LyXParagraph::size_type pos) const;
446 LyXParagraph * GetParFromID(int id);
449 /// returns false if no undo possible
450 bool TextUndo(BufferView *);
451 /// returns false if no redo possible
452 bool TextRedo(BufferView *);
453 /// used by TextUndo/TextRedo
454 bool TextHandleUndo(BufferView *, Undo * undo);
455 /// makes sure the next operation will be stored
457 /// this is dangerous and for internal use only
459 /// this is dangerous and for internal use only
461 /// the flag used by FinishUndo();
462 mutable bool undo_finished;
466 void SetUndo(Buffer *, Undo::undo_kind kind,
467 LyXParagraph const * before,
468 LyXParagraph const * end) const;
470 void SetRedo(Buffer *, Undo::undo_kind kind,
471 LyXParagraph const * before,
472 LyXParagraph const * end);
474 Undo * CreateUndo(Buffer *, Undo::undo_kind kind,
475 LyXParagraph const * before,
476 LyXParagraph const * end) const;
477 /// for external use in lyx_cb.C
478 void SetCursorParUndo(Buffer *);
480 void RemoveTableRow(LyXCursor & cursor) const;
482 bool IsEmptyTableCell() const;
484 void toggleAppendix(BufferView *);
486 int workWidth(BufferView *) const;
488 void ComputeBidiTables(Buffer const *, Row * row) const;
490 /// Maps positions in the visual string to positions in logical string.
492 LyXParagraph::size_type log2vis(LyXParagraph::size_type pos) const {
493 if (bidi_start == -1)
496 return log2vis_list[pos-bidi_start];
499 /// Maps positions in the logical string to positions in visual string.
501 LyXParagraph::size_type vis2log(LyXParagraph::size_type pos) const {
502 if (bidi_start == -1)
505 return vis2log_list[pos-bidi_start];
509 LyXParagraph::size_type bidi_level(LyXParagraph::size_type pos) const {
510 if (bidi_start == -1)
513 return bidi_levels[pos-bidi_start];
517 bool bidi_InRange(LyXParagraph::size_type pos) const {
518 return bidi_start == -1 ||
519 (bidi_start <= pos && pos <= bidi_end);
525 mutable Row * firstrow;
527 mutable Row * lastrow;
529 /** Copybuffer for copy environment type.
530 Asger has learned that this should be a buffer-property instead
531 Lgb has learned that 'char' is a lousy type for non-characters
533 LyXTextClass::size_type copylayouttype;
535 /** inserts a new row behind the specified row, increments
536 the touched counters */
537 void InsertRow(Row * row, LyXParagraph * par,
538 LyXParagraph::size_type pos) const;
539 /** removes the row and reset the touched counters */
540 void RemoveRow(Row * row) const;
542 /** remove all following rows of the paragraph of the specified row. */
543 void RemoveParagraph(Row * row) const;
545 /** insert the specified paragraph behind the specified row */
546 void InsertParagraph(BufferView *,
547 LyXParagraph * par, Row * row) const;
549 /** appends the implizit specified paragraph behind the specified row,
550 * start at the implizit given position */
551 void AppendParagraph(BufferView *, Row * row) const;
554 void BreakAgain(BufferView *, Row * row) const;
556 void BreakAgainOneRow(BufferView *, Row * row);
557 /// Calculate and set the height of the row
558 void SetHeightOfRow(BufferView *, Row * row_ptr) const;
560 /** this calculates the specified parameters. needed when setting
561 * the cursor and when creating a visible row */
562 void PrepareToPrint(BufferView *, Row * row, float & x,
563 float & fill_separator,
565 float & fill_label_hfill,
566 bool bidi = true) const;
569 void DeleteEmptyParagraphMechanism(BufferView *,
570 LyXCursor const & old_cursor) const;
572 /** Updates all counters starting BEHIND the row. Changed paragraphs
573 * with a dynamic left margin will be rebroken. */
574 void UpdateCounters(BufferView *, Row * row) const;
576 void SetCounter(Buffer const *, LyXParagraph * par) const;
579 * some low level functions
583 int SingleWidth(BufferView *, LyXParagraph * par,
584 LyXParagraph::size_type pos) const;
586 int SingleWidth(BufferView *, LyXParagraph * par,
587 LyXParagraph::size_type pos, char c) const;
589 void draw(BufferView *, Row const * row,
590 LyXParagraph::size_type & pos,
591 int offset, float & x, bool cleared);
593 /// get the next breakpoint in a given paragraph
594 LyXParagraph::size_type NextBreakPoint(BufferView *, Row const * row,
596 /// returns the minimum space a row needs on the screen in pixel
597 int Fill(BufferView *, Row * row, int workwidth) const;
599 /** returns the minimum space a manual label needs on the
601 int LabelFill(BufferView *, Row const * row) const;
604 LyXParagraph::size_type
605 BeginningOfMainBody(Buffer const *, LyXParagraph const * par) const;
607 /** Returns the left beginning of the text.
608 This information cannot be taken from the layouts-objekt, because
609 in LaTeX the beginning of the text fits in some cases
610 (for example sections) exactly the label-width.
612 int LeftMargin(BufferView *, Row const * row) const;
614 int RightMargin(Buffer const *, Row const * row) const;
616 int LabelEnd (BufferView *, Row const * row) const;
618 /** returns the number of separators in the specified row.
619 The separator on the very last column doesnt count
621 int NumberOfSeparators(Buffer const *, Row const * row) const;
623 /** returns the number of hfills in the specified row. The
624 LyX-Hfill is a LaTeX \hfill so that the hfills at the
625 beginning and at the end were ignored. This is {\em MUCH}
626 more usefull than not to ignore!
628 int NumberOfHfills(Buffer const *, Row const * row) const;
630 /// like NumberOfHfills, but only those in the manual label!
631 int NumberOfLabelHfills(Buffer const *, Row const * row) const;
632 /** returns true, if a expansion is needed. Rules are given by
635 bool HfillExpansion(Buffer const *, Row const * row_ptr,
636 LyXParagraph::size_type pos) const;
640 mutable std::vector<LyXParagraph::size_type> log2vis_list;
643 mutable std::vector<LyXParagraph::size_type> vis2log_list;
646 mutable std::vector<LyXParagraph::size_type> bidi_levels;
649 mutable LyXParagraph::size_type bidi_start;
652 mutable LyXParagraph::size_type bidi_end;
655 mutable bool bidi_same_direction;
658 unsigned char TransformChar(unsigned char c, LyXParagraph * par,
659 LyXParagraph::size_type pos) const;
661 /** returns the paragraph position of the last character in the
664 LyXParagraph::size_type RowLast(Row const * row) const;
666 LyXParagraph::size_type RowLastPrintable(Row const * row) const;
671 // special owner functions
673 LyXParagraph * OwnerParagraph() const;
675 LyXParagraph * OwnerParagraph(LyXParagraph *) const;