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!!!
71 BufferView * bv_owner;
73 InsetText * inset_owner;
76 LyXFont const GetFont(Buffer const *, LyXParagraph * par,
77 LyXParagraph::size_type pos) const;
79 void SetCharFont(Buffer const *, LyXParagraph * par,
80 LyXParagraph::size_type pos,
81 LyXFont const & font);
82 /// returns a pointer to the very first LyXParagraph
83 LyXParagraph * FirstParagraph() const;
85 /// what you expect when pressing <enter> at cursor position
86 void BreakParagraph(BufferView *, char keep_layout = 0);
88 /** set layout over selection and make a total rebreak of
91 LyXParagraph * SetLayout(BufferView *, LyXCursor & actual_cursor,
92 LyXCursor & selection_start,
93 LyXCursor & selection_end,
94 LyXTextClass::size_type layout);
95 void SetLayout(BufferView *, LyXTextClass::size_type layout);
98 void MakeFontEntriesLayoutSpecific(Buffer const *, LyXParagraph * par);
100 /** increment depth over selection and make a total rebreak of those
103 void IncDepth(BufferView *);
105 /** decrement depth over selection and make a total rebreak of those
107 void DecDepth(BufferView *);
109 /** Get the depth at current cursor position
111 int GetDepth() const { return cursor.par()->GetDepth(); }
113 /** set font over selection and make a total rebreak of those
115 toggleall defaults to false.
117 void SetFont(BufferView *, LyXFont const &, bool toggleall = false);
119 /** deletes and inserts again all paragaphs between the cursor
120 and the specified par. The Cursor is needed to set the refreshing
122 This function is needed after SetLayout and SetFont etc.
124 void RedoParagraphs(BufferView *, LyXCursor const & cursor,
125 LyXParagraph const * end_par) const;
127 void RedoParagraph(BufferView *) const;
130 void ToggleFree(BufferView *, LyXFont const &, bool toggleall = false);
132 /** recalculates the heights of all previous rows of the
133 specified paragraph. needed, if the last characters font
136 void RedoHeightOfParagraph(BufferView *, LyXCursor const & cursor);
138 /** forces the redrawing of a paragraph. Needed when manipulating a
141 void RedoDrawingOfParagraph(BufferView *, LyXCursor const & cursor);
143 /** insert a character, moves all the following breaks in the
144 same Paragraph one to the right and make a little rebreak
146 void InsertChar(BufferView *, char c);
148 void InsertInset(BufferView *, Inset * inset);
150 /** Completes the insertion with a full rebreak.
151 Returns true if something was broken. */
152 bool FullRebreak(BufferView *);
156 LyXParagraph::footnote_flag GetFootnoteFlag(int row);
159 Row * need_break_row;
161 mutable long refresh_y;
169 mutable Row * refresh_row;
173 /** wether the screen needs a refresh,
174 starting with refresh_y
176 mutable text_status status;
178 /** returns a pointer to the row near the specified y-coordinate
179 (relative to the whole text). y is set to the real beginning
182 Row * GetRowNearY(long & y) const;
184 /** returns the column near the specified x-coordinate of the row
185 x is set to the real beginning of this column
187 int GetColumnNearX(BufferView *, Row * row,
188 int & x, bool & boundary) const;
190 /** returns a pointer to a specified row. y is set to the beginning
193 Row * GetRow(LyXParagraph * par,
194 LyXParagraph::size_type pos, long & y) const;
196 /** returns the height of a default row, needed for scrollbar
198 int DefaultHeight() const;
201 Later this variable has to be removed. There should be now internal
202 cursor in a text (and thus not in a buffer). By keeping this it is
203 (I think) impossible to have several views with the same buffer, but
204 the cursor placed at different places.
206 Since the LyXText now has been moved from Buffer to BufferView
207 it should not be absolutely needed to move the cursor...
209 mutable LyXCursor cursor;
211 /* the selection cursor */
213 mutable bool selection;
215 mutable bool mark_set;
218 mutable LyXCursor sel_cursor;
220 LyXCursor sel_start_cursor;
222 mutable LyXCursor sel_end_cursor;
223 /// needed for the toggling
224 LyXCursor last_sel_cursor;
226 LyXCursor toggle_cursor;
228 LyXCursor toggle_end_cursor;
230 /// need the selection cursor:
233 void ClearSelection() const;
235 string const selectionAsString(Buffer const *) const;
237 /// just selects the word the cursor is in
238 void SelectWord(BufferView *);
240 /** 'selects" the next word, where the cursor is not in
241 and returns this word as string. THe cursor will be moved
242 to the beginning of this word.
243 With SelectSelectedWord can this be highlighted really
245 string const SelectNextWord(BufferView *, float & value) const;
247 void SelectSelectedWord(BufferView *);
249 void SetCursor(BufferView *, LyXParagraph * par,
250 LyXParagraph::size_type pos,
252 bool boundary = false) const;
254 void SetCursor(BufferView *, LyXCursor &, LyXParagraph * par,
255 LyXParagraph::size_type pos,
256 bool boundary = false) const;
258 void SetCursorIntern(BufferView *, LyXParagraph * par,
259 LyXParagraph::size_type pos,
261 bool boundary = false) const;
263 void SetCurrentFont(BufferView *) const;
266 bool IsBoundary(Buffer const *, LyXParagraph * par,
267 LyXParagraph::size_type pos) const;
269 bool IsBoundary(Buffer const *, LyXParagraph * par,
270 LyXParagraph::size_type pos,
271 LyXFont const & font) const;
274 void SetCursorFromCoordinates(BufferView *, int x, long y) const;
276 void SetCursorFromCoordinates(BufferView *, LyXCursor &,
277 int x, long y) const;
279 void CursorUp(BufferView *) const;
281 void CursorDown(BufferView *) const;
283 void CursorLeft(BufferView *, bool internal = true) const;
285 void CursorRight(BufferView *, bool internal = true) const;
287 void CursorLeftIntern(BufferView *, bool internal = true) const;
289 void CursorRightIntern(BufferView *, bool internal = true) const;
291 void CursorLeftOneWord(BufferView *) const;
293 void CursorRightOneWord(BufferView *) const;
295 void CursorUpParagraph(BufferView *) const;
297 void CursorDownParagraph(BufferView *) const;
299 void CursorHome(BufferView *) const;
301 void CursorEnd(BufferView *) const;
303 void CursorTab(BufferView *) const;
305 void CursorTop(BufferView *) const;
307 void CursorBottom(BufferView *) const;
309 void Delete(BufferView *);
311 void Backspace(BufferView *);
313 void DeleteWordForward(BufferView *);
315 void DeleteWordBackward(BufferView *);
317 void DeleteLineForward(BufferView *);
319 bool SelectWordWhenUnderCursor(BufferView *);
325 text_capitalization = 1,
329 /// Change the case of the word at cursor position.
330 void ChangeWordCase(BufferView *, TextCase action);
332 /** returns a printed row in a pixmap. The y value is needed to
333 decide, wether it is selected text or not. This is a strange
336 void GetVisibleRow(BufferView *, int y_offset, int x_offset,
337 Row * row_ptr, long y, bool cleared=false);
342 void ToggleFootnote(BufferView *);
344 void OpenStuff(BufferView *);
346 void OpenFootnotes();
348 void OpenFootnote(BufferView *);
350 void CloseFootnotes();
352 void CloseFootnote(BufferView *);
354 /** turn the selection into a new environment. If there is no
355 selection, create an empty environment
357 void InsertFootnoteEnvironment(BufferView *,
358 LyXParagraph::footnote_kind kind);
360 void MeltFootnoteEnvironment(BufferView *);
363 void CutSelection(BufferView *, bool = true);
365 void CopySelection(BufferView *);
367 void PasteSelection(BufferView *);
369 void copyEnvironmentType();
371 void pasteEnvironmentType(BufferView *);
373 void InsertFootnote();
375 void InsertMarginpar();
379 void InsertTabular();
381 /** the DTP switches for paragraphs. LyX will store the top settings
382 always in the first physical paragraph, the bottom settings in the
383 last. When a paragraph is broken, the top settings rest, the bottom
384 settings are given to the new one. So I can make shure, they do not
385 duplicate themself (and you cannnot make dirty things with them! )
387 void SetParagraph(BufferView *,
388 bool line_top, bool line_bottom,
389 bool pagebreak_top, bool pagebreak_bottom,
390 VSpace const & space_top,
391 VSpace const & space_bottom,
393 string labelwidthstring,
396 void SetParagraphExtraOpt(BufferView *, int type,
397 string const & width,
398 string const & widthp,
399 int alignment, bool hfill,
400 bool start_minipage);
402 /* these things are for search and replace */
404 /** returns true if the specified string is at the specified
407 bool IsStringInText(LyXParagraph * par,
408 LyXParagraph::size_type pos,
409 string const & str) const;
410 /** sets the selection over the number of characters of string,
413 void SetSelectionOverString(BufferView *, string const & str);
415 /** simple replacing. The font of the first selected character
418 void ReplaceSelectionWithString(BufferView *, string const & str);
420 /** if the string can be found: return true and set the cursor to
422 bool SearchForward(BufferView *, string const & str) const;
424 bool SearchBackward(BufferView *, string const & str) const;
426 /// needed to insert the selection
427 void InsertStringA(BufferView *, string const & str);
428 /// needed to insert the selection
429 void InsertStringB(BufferView *, string const & str);
431 /// usefull when texing from within LyX
432 bool GotoNextError(BufferView *) const;
434 /// just another feature :)
435 bool GotoNextNote(BufferView *) const;
437 /* for the greater insets */
439 /// returns false if inset wasn't found
440 bool UpdateInset(BufferView *, Inset *);
442 void CheckParagraph(BufferView *, LyXParagraph * par,
443 LyXParagraph::size_type pos);
445 int NumberOfCell(LyXParagraph * par,
446 LyXParagraph::size_type pos) const;
448 LyXParagraph * GetParFromID(int id);
451 /// returns false if no undo possible
452 bool TextUndo(BufferView *);
453 /// returns false if no redo possible
454 bool TextRedo(BufferView *);
455 /// used by TextUndo/TextRedo
456 bool TextHandleUndo(BufferView *, Undo * undo);
457 /// makes sure the next operation will be stored
459 /// this is dangerous and for internal use only
461 /// this is dangerous and for internal use only
463 /// the flag used by FinishUndo();
464 mutable bool undo_finished;
468 void SetUndo(Buffer *, Undo::undo_kind kind,
469 LyXParagraph const * before,
470 LyXParagraph const * end) const;
472 void SetRedo(Buffer *, Undo::undo_kind kind,
473 LyXParagraph const * before,
474 LyXParagraph const * end);
476 Undo * CreateUndo(Buffer *, Undo::undo_kind kind,
477 LyXParagraph const * before,
478 LyXParagraph const * end) const;
479 /// for external use in lyx_cb.C
480 void SetCursorParUndo(Buffer *);
482 void RemoveTableRow(LyXCursor & cursor) const;
484 bool IsEmptyTableCell() const;
486 void toggleAppendix(BufferView *);
488 int workWidth(BufferView *) const;
490 void ComputeBidiTables(Buffer const *, Row * row) const;
492 /// Maps positions in the visual string to positions in logical string.
494 LyXParagraph::size_type log2vis(LyXParagraph::size_type pos) const {
495 if (bidi_start == -1)
498 return log2vis_list[pos-bidi_start];
501 /// Maps positions in the logical string to positions in visual string.
503 LyXParagraph::size_type vis2log(LyXParagraph::size_type pos) const {
504 if (bidi_start == -1)
507 return vis2log_list[pos-bidi_start];
511 int bidi_level(LyXParagraph::size_type pos) const {
512 if (bidi_start == -1)
515 return bidi_levels[pos-bidi_start];
519 bool bidi_InRange(LyXParagraph::size_type pos) const {
520 return bidi_start == -1 ||
521 (bidi_start <= pos && pos <= bidi_end);
527 mutable Row * firstrow;
529 mutable Row * lastrow;
531 /** Copybuffer for copy environment type.
532 Asger has learned that this should be a buffer-property instead
533 Lgb has learned that 'char' is a lousy type for non-characters
535 LyXTextClass::size_type copylayouttype;
537 /** inserts a new row behind the specified row, increments
538 the touched counters */
539 void InsertRow(Row * row, LyXParagraph * par,
540 LyXParagraph::size_type pos) const;
541 /** removes the row and reset the touched counters */
542 void RemoveRow(Row * row) const;
544 /** remove all following rows of the paragraph of the specified row. */
545 void RemoveParagraph(Row * row) const;
547 /** insert the specified paragraph behind the specified row */
548 void InsertParagraph(BufferView *,
549 LyXParagraph * par, Row * row) const;
551 /** appends the implizit specified paragraph behind the specified row,
552 * start at the implizit given position */
553 void AppendParagraph(BufferView *, Row * row) const;
556 void BreakAgain(BufferView *, Row * row) const;
558 void BreakAgainOneRow(BufferView *, Row * row);
559 /// Calculate and set the height of the row
560 void SetHeightOfRow(BufferView *, Row * row_ptr) const;
562 /** this calculates the specified parameters. needed when setting
563 * the cursor and when creating a visible row */
564 void PrepareToPrint(BufferView *, Row * row, float & x,
565 float & fill_separator,
567 float & fill_label_hfill,
568 bool bidi = true) const;
571 void DeleteEmptyParagraphMechanism(BufferView *,
572 LyXCursor const & old_cursor) const;
574 /** Updates all counters starting BEHIND the row. Changed paragraphs
575 * with a dynamic left margin will be rebroken. */
576 void UpdateCounters(BufferView *, Row * row) const;
578 void SetCounter(Buffer const *, LyXParagraph * par) const;
581 * some low level functions
585 int SingleWidth(BufferView *, LyXParagraph * par,
586 LyXParagraph::size_type pos) const;
588 int SingleWidth(BufferView *, LyXParagraph * par,
589 LyXParagraph::size_type pos, char c) const;
591 void draw(BufferView *, Row const * row,
592 LyXParagraph::size_type & pos,
593 int offset, float & x, bool cleared);
595 /// get the next breakpoint in a given paragraph
596 LyXParagraph::size_type NextBreakPoint(BufferView *, Row const * row,
598 /// returns the minimum space a row needs on the screen in pixel
599 int Fill(BufferView *, Row * row, int workwidth) const;
601 /** returns the minimum space a manual label needs on the
603 int LabelFill(BufferView *, Row const * row) const;
606 LyXParagraph::size_type
607 BeginningOfMainBody(Buffer const *, LyXParagraph const * par) const;
609 /** Returns the left beginning of the text.
610 This information cannot be taken from the layouts-objekt, because
611 in LaTeX the beginning of the text fits in some cases
612 (for example sections) exactly the label-width.
614 int LeftMargin(BufferView *, Row const * row) const;
616 int RightMargin(Buffer const *, Row const * row) const;
618 int LabelEnd (BufferView *, Row const * row) const;
620 /** returns the number of separators in the specified row.
621 The separator on the very last column doesnt count
623 int NumberOfSeparators(Buffer const *, Row const * row) const;
625 /** returns the number of hfills in the specified row. The
626 LyX-Hfill is a LaTeX \hfill so that the hfills at the
627 beginning and at the end were ignored. This is {\em MUCH}
628 more usefull than not to ignore!
630 int NumberOfHfills(Buffer const *, Row const * row) const;
632 /// like NumberOfHfills, but only those in the manual label!
633 int NumberOfLabelHfills(Buffer const *, Row const * row) const;
634 /** returns true, if a expansion is needed. Rules are given by
637 bool HfillExpansion(Buffer const *, Row const * row_ptr,
638 LyXParagraph::size_type pos) const;
642 mutable std::vector<LyXParagraph::size_type> log2vis_list;
645 mutable std::vector<LyXParagraph::size_type> vis2log_list;
648 mutable std::vector<LyXParagraph::size_type> bidi_levels;
651 mutable LyXParagraph::size_type bidi_start;
654 mutable LyXParagraph::size_type bidi_end;
657 mutable bool bidi_same_direction;
660 unsigned char TransformChar(unsigned char c, LyXParagraph * par,
661 LyXParagraph::size_type pos) const;
663 /** returns the paragraph position of the last character in the
666 LyXParagraph::size_type RowLast(Row const * row) const;
668 LyXParagraph::size_type RowLastPrintable(Row const * row) const;
673 // special owner functions
675 LyXParagraph * OwnerParagraph() const;
677 LyXParagraph * OwnerParagraph(LyXParagraph *) const;