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
55 LyXText(BufferView *);
61 void init(BufferView *);
63 mutable int number_of_rows;
66 mutable unsigned int width;
67 /// the current font settings
68 mutable LyXFont current_font;
70 mutable LyXFont real_current_font;
71 /// first visible pixel-row is set from LyXScreen!!!
74 BufferView * bv_owner;
76 InsetText * inset_owner;
79 // void owner(BufferView *);
82 LyXFont GetFont(Buffer const *, LyXParagraph * par,
83 LyXParagraph::size_type pos) const;
85 void SetCharFont(Buffer const *, LyXParagraph * par,
86 LyXParagraph::size_type pos,
87 LyXFont const & font);
88 /// returns a pointer to the very first LyXParagraph
89 LyXParagraph * FirstParagraph() const;
91 /// what you expect when pressing <enter> at cursor position
92 void BreakParagraph(BufferView *, char keep_layout = 0);
94 /** set layout over selection and make a total rebreak of
97 LyXParagraph * SetLayout(BufferView *, LyXCursor & actual_cursor,
98 LyXCursor & selection_start,
99 LyXCursor & selection_end,
100 LyXTextClass::size_type layout);
101 void SetLayout(BufferView *, LyXTextClass::size_type layout);
103 /// used in setlayout
104 void MakeFontEntriesLayoutSpecific(Buffer const *, LyXParagraph * par);
106 /** increment depth over selection and make a total rebreak of those
109 void IncDepth(BufferView *);
111 /** decrement depth over selection and make a total rebreak of those
113 void DecDepth(BufferView *);
115 /** Get the depth at current cursor position
117 int GetDepth() const { return cursor.par()->GetDepth(); }
119 /** set font over selection and make a total rebreak of those
121 toggleall defaults to false.
123 void SetFont(BufferView *, LyXFont const &, bool toggleall = false);
125 /** deletes and inserts again all paragaphs between the cursor
126 and the specified par. The Cursor is needed to set the refreshing
128 This function is needed after SetLayout and SetFont etc.
130 void RedoParagraphs(BufferView *, LyXCursor const & cursor,
131 LyXParagraph const * end_par) const;
133 void RedoParagraph(BufferView *) const;
136 void ToggleFree(BufferView *, LyXFont const &, bool toggleall = false);
138 /** recalculates the heights of all previous rows of the
139 specified paragraph. needed, if the last characters font
142 void RedoHeightOfParagraph(BufferView *, LyXCursor const & cursor);
144 /** forces the redrawing of a paragraph. Needed when manipulating a
147 void RedoDrawingOfParagraph(BufferView *, LyXCursor const & cursor);
149 /** insert a character, moves all the following breaks in the
150 same Paragraph one to the right and make a little rebreak
152 void InsertChar(BufferView *, char c);
154 void InsertInset(BufferView *, Inset * inset);
156 /** Completes the insertion with a full rebreak.
157 Returns true if something was broken. */
158 bool FullRebreak(BufferView *);
161 LyXParagraph::footnote_flag GetFootnoteFlag(int row);
163 Row * need_break_row;
165 mutable long refresh_y;
173 mutable Row * refresh_row;
177 /** wether the screen needs a refresh,
178 starting with refresh_y
180 mutable text_status status;
182 /** returns a pointer to the row near the specified y-coordinate
183 (relative to the whole text). y is set to the real beginning
186 Row * GetRowNearY(long & y) const;
188 /** returns the column near the specified x-coordinate of the row
189 x is set to the real beginning of this column
191 int GetColumnNearX(BufferView *, Row * row, 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, long & 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 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 char * SelectNextWord(BufferView *, float & value);
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, long y) const;
278 void SetCursorFromCoordinates(BufferView *, LyXCursor &, int x, long y) const;
280 void CursorUp(BufferView *) const;
282 void CursorDown(BufferView *) const;
284 void CursorLeft(BufferView *, bool internal = true) const;
286 void CursorRight(BufferView *, bool internal = true) const;
288 void CursorLeftIntern(BufferView *, bool internal = true) const;
290 void CursorRightIntern(BufferView *, bool internal = true) const;
292 void CursorLeftOneWord(BufferView *) const;
294 void CursorRightOneWord(BufferView *) const;
296 void CursorUpParagraph(BufferView *) const;
298 void CursorDownParagraph(BufferView *) const;
300 void CursorHome(BufferView *) const;
302 void CursorEnd(BufferView *) const;
304 void CursorTab(BufferView *) const;
306 void CursorTop(BufferView *) const;
308 void CursorBottom(BufferView *) const;
310 void Delete(BufferView *);
312 void Backspace(BufferView *);
314 void DeleteWordForward(BufferView *);
316 void DeleteWordBackward(BufferView *);
318 void DeleteLineForward(BufferView *);
320 bool SelectWordWhenUnderCursor(BufferView *);
324 text_capitalization = 1,
327 /// Change the case of the word at cursor position.
328 void ChangeWordCase(BufferView *, TextCase action);
330 /** returns a printed row in a pixmap. The y value is needed to
331 decide, wether it is selected text or not. This is a strange
334 void GetVisibleRow(BufferView *, int y_offset, int x_offset,
335 Row * row_ptr, long y, bool cleared=false);
339 void ToggleFootnote(BufferView *);
341 void OpenStuff(BufferView *);
343 void OpenFootnotes();
345 void OpenFootnote(BufferView *);
347 void CloseFootnotes();
349 void CloseFootnote(BufferView *);
351 /** turn the selection into a new environment. If there is no
352 selection, create an empty environment
354 void InsertFootnoteEnvironment(BufferView *,
355 LyXParagraph::footnote_kind kind);
357 void MeltFootnoteEnvironment(BufferView *);
359 void CutSelection(BufferView *, bool = true);
361 void CopySelection(BufferView *);
363 void PasteSelection(BufferView *);
365 void copyEnvironmentType();
367 void pasteEnvironmentType(BufferView *);
369 void InsertFootnote();
371 void InsertMarginpar();
375 void InsertTabular();
377 /** the DTP switches for paragraphs. LyX will store the top settings
378 always in the first physical paragraph, the bottom settings in the
379 last. When a paragraph is broken, the top settings rest, the bottom
380 settings are given to the new one. So I can make shure, they do not
381 duplicate themself (and you cannnot make dirty things with them! )
383 void SetParagraph(BufferView *,
384 bool line_top, bool line_bottom,
385 bool pagebreak_top, bool pagebreak_bottom,
386 VSpace const & space_top,
387 VSpace const & space_bottom,
389 string labelwidthstring,
391 void SetParagraphExtraOpt(BufferView *, int type,
394 int alignment, bool hfill,
395 bool start_minipage);
397 /* these things are for search and replace */
399 /** returns true if the specified string is at the specified
402 bool IsStringInText(LyXParagraph * par,
403 LyXParagraph::size_type pos,
404 char const * str) const;
405 /** sets the selection over the number of characters of string,
408 void SetSelectionOverString(BufferView *, char const * str);
410 /** simple replacing. The font of the first selected character
413 void ReplaceSelectionWithString(BufferView *, char const * str);
415 /** if the string can be found: return true and set the cursor to
417 bool SearchForward(BufferView *, char const * str) const;
418 bool SearchBackward(BufferView *, char const * str) const;
420 /// needed to insert the selection
421 void InsertStringA(BufferView *, string const & str);
422 /// needed to insert the selection
423 void InsertStringB(BufferView *, string const & str);
425 /// usefull when texing from within LyX
426 bool GotoNextError(BufferView *) const;
428 /// just another feature :)
429 bool GotoNextNote(BufferView *) const;
431 /* for the greater insets */
433 /// returns false if inset wasn't found
434 bool UpdateInset(BufferView *, Inset *);
436 void CheckParagraph(BufferView *, LyXParagraph * par,
437 LyXParagraph::size_type pos);
439 int NumberOfCell(LyXParagraph * par,
440 LyXParagraph::size_type pos) const;
441 /* table stuff -- begin*/
443 /** all table features of the text-class are put together in
444 this function. Possible values of feature are defined in table.h
446 void TableFeatures(BufferView *, int feature, string const & val) const;
448 void TableFeatures(BufferView *, int feature) const;
450 /** pos points at the beginning of the next cell (if there is one)
452 int WidthOfCell(BufferView *, LyXParagraph * par,
453 LyXParagraph::size_type & pos) const;
455 void CheckParagraphInTable(BufferView *, LyXParagraph * par,
456 LyXParagraph::size_type pos);
458 void InsertCharInTable(BufferView *, char c);
460 void BackspaceInTable(BufferView *);
462 bool HitInTable(BufferView *, Row * row, int x) const;
464 bool MouseHitInTable(BufferView *, int x, long y) const;
465 /* table stuff -- end*/
467 LyXParagraph * GetParFromID(int id);
470 /// returns false if no undo possible
471 bool TextUndo(BufferView *);
472 /// returns false if no redo possible
473 bool TextRedo(BufferView *);
474 /// used by TextUndo/TextRedo
475 bool TextHandleUndo(BufferView *, Undo * undo);
476 /// makes sure the next operation will be stored
478 /// this is dangerous and for internal use only
480 /// this is dangerous and for internal use only
482 /// the flag used by FinishUndo();
483 mutable bool undo_finished;
487 void SetUndo(Buffer *, Undo::undo_kind kind,
488 LyXParagraph const * before,
489 LyXParagraph const * end) const;
491 void SetRedo(Buffer *, Undo::undo_kind kind,
492 LyXParagraph const * before,
493 LyXParagraph const * end);
495 Undo * CreateUndo(Buffer *, Undo::undo_kind kind,
496 LyXParagraph const * before,
497 LyXParagraph const * end) const;
498 /// for external use in lyx_cb.C
499 void SetCursorParUndo(Buffer *);
501 void RemoveTableRow(LyXCursor & cursor) const;
503 bool IsEmptyTableCell() const;
505 void toggleAppendix(BufferView *);
507 int workWidth(BufferView *) const;
509 // Buffer * buffer() const;
511 void ComputeBidiTables(Buffer const *, Row * row) const;
513 /// Maps positions in the visual string to positions in logical string.
515 LyXParagraph::size_type log2vis(LyXParagraph::size_type pos) const {
516 if (bidi_start == -1)
519 return log2vis_list[pos-bidi_start];
522 /// Maps positions in the logical string to positions in visual string.
524 LyXParagraph::size_type vis2log(LyXParagraph::size_type pos) const {
525 if (bidi_start == -1)
528 return vis2log_list[pos-bidi_start];
532 int bidi_level(LyXParagraph::size_type pos) const {
533 if (bidi_start == -1)
536 return bidi_levels[pos-bidi_start];
540 bool bidi_InRange(LyXParagraph::size_type pos) const {
541 return bidi_start == -1 ||
542 (bidi_start <= pos && pos <= bidi_end);
548 mutable Row * firstrow;
550 mutable Row * lastrow;
552 /** Copybuffer for copy environment type
553 Asger has learned that this should be a buffer-property instead
554 Lgb has learned that 'char' is a lousy type for non-characters
556 LyXTextClass::size_type copylayouttype;
558 /** inserts a new row behind the specified row, increments
559 * the touched counters */
560 void InsertRow(Row * row, LyXParagraph * par,
561 LyXParagraph::size_type pos) const;
562 /** removes the row and reset the touched counters */
563 void RemoveRow(Row * row) const;
565 /** remove all following rows of the paragraph of the specified row. */
566 void RemoveParagraph(Row * row) const;
568 /** insert the specified paragraph behind the specified row */
569 void InsertParagraph(BufferView *, LyXParagraph * par, Row * row) const;
571 /** appends the implizit specified paragraph behind the specified row,
572 * start at the implizit given position */
573 void AppendParagraph(BufferView *, Row * row) const;
576 void BreakAgain(BufferView *, Row * row) const;
578 void BreakAgainOneRow(BufferView *, Row * row);
579 /// Calculate and set the height of the row
580 void SetHeightOfRow(BufferView *, Row * row_ptr) const;
582 /** this calculates the specified parameters. needed when setting
583 * the cursor and when creating a visible row */
584 void PrepareToPrint(BufferView *, Row * row, float & x,
585 float & fill_separator,
587 float & fill_label_hfill,
588 bool bidi = true) const;
591 void DeleteEmptyParagraphMechanism(BufferView *,
592 LyXCursor const & old_cursor) const;
594 /** Updates all counters starting BEHIND the row. Changed paragraphs
595 * with a dynamic left margin will be rebroken. */
596 void UpdateCounters(BufferView *, Row * row) const;
598 void SetCounter(Buffer const *, LyXParagraph * par) const;
601 * some low level functions
605 int SingleWidth(BufferView *, LyXParagraph * par,
606 LyXParagraph::size_type pos) const;
608 int SingleWidth(BufferView *, LyXParagraph * par,
609 LyXParagraph::size_type pos, char c) const;
611 void draw(BufferView *, Row const * row,
612 LyXParagraph::size_type & pos,
613 int offset, float & x, bool cleared);
615 /// get the next breakpoint in a given paragraph
616 LyXParagraph::size_type NextBreakPoint(BufferView *, Row const * row,
618 /// returns the minimum space a row needs on the screen in pixel
619 int Fill(BufferView *, Row * row, int workwidth) const;
621 /** returns the minimum space a manual label needs on the
623 int LabelFill(BufferView *, Row const * row) const;
626 LyXParagraph::size_type
627 BeginningOfMainBody(Buffer const *, LyXParagraph const * par) const;
629 /** Returns the left beginning of the text.
630 This information cannot be taken from the layouts-objekt, because
631 in LaTeX the beginning of the text fits in some cases
632 (for example sections) exactly the label-width.
634 int LeftMargin(BufferView *, Row const * row) const;
636 int RightMargin(Buffer const *, Row const * row) const;
638 int LabelEnd (BufferView *, Row const * row) const;
640 /** returns the number of separators in the specified row.
641 The separator on the very last column doesnt count
643 int NumberOfSeparators(Buffer const *, Row const * row) const;
645 /** returns the number of hfills in the specified row. The
646 LyX-Hfill is a LaTeX \hfill so that the hfills at the
647 beginning and at the end were ignored. This is {\em MUCH}
648 more usefull than not to ignore!
650 int NumberOfHfills(Buffer const *, Row const * row) const;
652 /// like NumberOfHfills, but only those in the manual label!
653 int NumberOfLabelHfills(Buffer const *, Row const * row) const;
654 /** returns true, if a expansion is needed. Rules are given by
657 bool HfillExpansion(Buffer const *, Row const * row_ptr,
658 LyXParagraph::size_type pos) const;
662 mutable std::vector<LyXParagraph::size_type> log2vis_list;
665 mutable std::vector<LyXParagraph::size_type> vis2log_list;
668 mutable std::vector<LyXParagraph::size_type> bidi_levels;
671 mutable LyXParagraph::size_type bidi_start;
674 mutable LyXParagraph::size_type bidi_end;
677 mutable bool bidi_same_direction;
680 unsigned char TransformChar(unsigned char c, Letter_Form form) const;
683 unsigned char TransformChar(unsigned char c, LyXParagraph * par,
684 LyXParagraph::size_type pos) const;
686 /** returns the paragraph position of the last character in the
689 LyXParagraph::size_type RowLast(Row const * row) const;
691 LyXParagraph::size_type RowLastPrintable(Row const * row) const;
696 /// special owner functions
698 LyXParagraph * OwnerParagraph() const;
700 LyXParagraph * OwnerParagraph(LyXParagraph *) const;