]> git.lyx.org Git - features.git/blob - src/lyxtext.h
1c3ec4d6fc8483bb2701b7e912cf0dac2d63602a
[features.git] / src / lyxtext.h
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  * 
5  *           LyX, The Document Processor
6  *       
7  *           Copyright 1995 Matthias Ettrich
8  *           Copyright 1995-2000 The LyX Team.
9  *
10  * ====================================================== */
11
12 #ifndef LYXTEXT_H
13 #define LYXTEXT_H
14
15 #ifdef __GNUG__
16 #pragma interface
17 #endif
18
19 #include "lyxfont.h"
20 #include "undo.h"
21 #include "lyxcursor.h"
22 #include "lyxparagraph.h"
23 #include "layout.h"
24 #include "lyxrow.h"
25
26 class Buffer;
27 class BufferParams;
28 class BufferView;
29 class InsetText;
30
31
32 /**
33   This class holds the mapping between buffer paragraphs and screen rows.
34   */
35 class LyXText {
36 public:
37         ///
38         enum text_status {
39                 ///
40                 UNCHANGED,
41                 ///
42                 NEED_MORE_REFRESH,
43                 ///
44                 NEED_VERY_LITTLE_REFRESH,
45                 ///
46                 CHANGED_IN_DRAW
47         };
48
49         /// Constructor
50         LyXText(BufferView *);
51         ///
52         LyXText(InsetText *);
53    
54         /// Destructor
55         ~LyXText();
56
57         void init(BufferView *);
58         ///
59         mutable int number_of_rows;
60         ///
61         mutable int height;
62         ///
63         mutable unsigned int width;
64         /// the current font settings
65         mutable LyXFont current_font;
66         /// the 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!
70         int first;
71         ///
72         BufferView * bv_owner;
73         ///
74         InsetText * inset_owner;
75         ///
76         UpdatableInset * the_locking_inset;
77
78         ///
79         int GetRealCursorX(BufferView *) const;
80         ///
81         LyXFont const GetFont(Buffer const *, LyXParagraph * par,
82                         LyXParagraph::size_type pos) const;
83         ///
84         void SetCharFont(Buffer const *, LyXParagraph * par,
85                          LyXParagraph::size_type pos,
86                          LyXFont const & font);
87         /// returns a pointer to the very first LyXParagraph
88         LyXParagraph * FirstParagraph() const;
89   
90         /// what you expect when pressing <enter> at cursor position
91         void BreakParagraph(BufferView *, char keep_layout = 0);
92
93         /** set layout over selection and make a total rebreak of
94           those paragraphs
95           */
96         LyXParagraph * SetLayout(BufferView *, LyXCursor & actual_cursor,
97                                  LyXCursor & selection_start,
98                                  LyXCursor & selection_end,
99                                  LyXTextClass::size_type layout);
100         void SetLayout(BufferView *, LyXTextClass::size_type layout);
101         
102         /// used in setlayout
103         void MakeFontEntriesLayoutSpecific(Buffer const *, LyXParagraph * par);
104         
105         /** increment depth over selection and make a total rebreak of those 
106           paragraphs
107           */
108         void IncDepth(BufferView *);
109         
110         /** decrement depth over selection and make a total rebreak of those  
111           paragraphs */
112         void DecDepth(BufferView *);
113
114         /** Get the depth at current cursor position
115          */
116         int GetDepth() const { return cursor.par()->GetDepth(); }
117         
118         /** set font over selection and make a total rebreak of those
119           paragraphs.
120           toggleall defaults to false.
121           */
122         void SetFont(BufferView *, LyXFont const &, bool toggleall = false);
123         
124         /** deletes and inserts again all paragaphs between the cursor
125           and the specified par. The Cursor is needed to set the refreshing
126           parameters. 
127           This function is needed after SetLayout and SetFont etc.
128           */
129         void RedoParagraphs(BufferView *, LyXCursor const & cursor,
130                             LyXParagraph const * end_par) const;
131         ///
132         void RedoParagraph(BufferView *) const;
133         
134         ///
135         void ToggleFree(BufferView *, LyXFont const &, bool toggleall = false);
136         
137         /** recalculates the heights of all previous rows of the
138             specified paragraph.  needed, if the last characters font
139             has changed.  
140             */
141         void RedoHeightOfParagraph(BufferView *, LyXCursor const & cursor);
142         
143         /** forces the redrawing of a paragraph. Needed when manipulating a 
144             right address box
145             */ 
146         void RedoDrawingOfParagraph(BufferView *, LyXCursor const & cursor);
147         
148         /** insert a character, moves all the following breaks in the 
149           same Paragraph one to the right and make a little rebreak
150           */
151         void InsertChar(BufferView *, char c);
152         ///
153         void InsertInset(BufferView *, Inset * inset);
154    
155         /** Completes the insertion with a full rebreak.
156             Returns true if something was broken. */
157         bool FullRebreak(BufferView *);
158
159 #ifndef NEW_INSETS
160         ///
161         LyXParagraph::footnote_flag GetFootnoteFlag(int row);
162 #endif
163         ///
164         Row * need_break_row;
165         ///
166         mutable int refresh_y;
167         ///
168         int refresh_height;
169         ///
170         int refresh_width;
171         ///
172         int refresh_x;
173         ///
174         mutable Row * refresh_row;
175         ///
176         int refresh_pos;
177         
178         /** wether the screen needs a refresh,
179            starting with refresh_y
180            */
181         mutable text_status status;
182         
183         /** returns a pointer to the row near the specified y-coordinate
184           (relative to the whole text). y is set to the real beginning
185           of this row
186           */ 
187         Row * GetRowNearY(int & y) const;
188         
189         /** returns the column near the specified x-coordinate of the row 
190          x is set to the real beginning of this column
191          */ 
192         int GetColumnNearX(BufferView *, Row * row,
193                            int & x, bool & boundary) const;
194         
195         /** returns a pointer to a specified row. y is set to the beginning
196          of the row
197          */
198         Row * GetRow(LyXParagraph * par,
199                      LyXParagraph::size_type pos, int & y) const;
200
201         /** returns the height of a default row, needed  for scrollbar
202          */
203         int DefaultHeight() const;
204    
205         /** The cursor.
206           Later this variable has to be removed. There should be now internal
207           cursor in a text (and thus not in a buffer). By keeping this it is
208           (I think) impossible to have several views with the same buffer, but
209           the cursor placed at different places.
210           [later]
211           Since the LyXText now has been moved from Buffer to BufferView
212           it should not be absolutely needed to move the cursor...
213           */
214         mutable LyXCursor cursor;
215    
216         /* the selection cursor */
217         /// 
218         mutable bool selection;
219         ///
220         mutable bool mark_set;
221
222         ///
223         mutable LyXCursor sel_cursor;
224         ///
225         LyXCursor sel_start_cursor;
226         ///
227         mutable LyXCursor sel_end_cursor;
228         /// needed for the toggling
229         LyXCursor last_sel_cursor;
230         ///
231         LyXCursor toggle_cursor;
232         ///
233         LyXCursor toggle_end_cursor;
234    
235         /// need the selection cursor:
236         void SetSelection(BufferView *);
237         ///
238         void ClearSelection(BufferView *) const;
239         ///
240         string const selectionAsString(Buffer const *) const;
241         
242         /// just selects the word the cursor is in
243         void SelectWord(BufferView *);
244
245         /** 'selects" the next word, where the cursor is not in 
246          and returns this word as string. THe cursor will be moved 
247          to the beginning of this word. 
248          With SelectSelectedWord can this be highlighted really
249          */ 
250         string const SelectNextWord(BufferView *, float & value) const;
251         ///
252         void SelectSelectedWord(BufferView *);
253         ///
254         void SetCursor(BufferView *, LyXParagraph * par,
255                        LyXParagraph::size_type pos,
256                        bool setfont = true,
257                        bool boundary = false) const;
258         ///
259         void SetCursor(BufferView *, LyXCursor &, LyXParagraph * par,
260                        LyXParagraph::size_type pos,
261                        bool boundary = false) const;
262         ///
263         void SetCursorIntern(BufferView *, LyXParagraph * par,
264                              LyXParagraph::size_type pos,
265                              bool setfont = true,
266                              bool boundary = false) const;
267         ///
268         void SetCurrentFont(BufferView *) const;
269
270         ///
271         bool IsBoundary(Buffer const *, LyXParagraph * par,
272                         LyXParagraph::size_type pos) const;
273         ///
274         bool IsBoundary(Buffer const *, LyXParagraph * par,
275                          LyXParagraph::size_type pos,
276                          LyXFont const & font) const;
277
278         ///
279         void SetCursorFromCoordinates(BufferView *, int x, int y) const;
280         ///
281         void SetCursorFromCoordinates(BufferView *, LyXCursor &,
282                                       int x, int y) const;
283         ///
284         void CursorUp(BufferView *) const;
285         ///
286         void CursorDown(BufferView *) const;
287         ///
288         void CursorLeft(BufferView *, bool internal = true) const;
289         ///
290         void CursorRight(BufferView *, bool internal = true) const;
291         ///
292         void CursorLeftOneWord(BufferView *) const;
293         ///
294         void CursorRightOneWord(BufferView *) const;
295         ///
296         void CursorUpParagraph(BufferView *) const;
297         ///
298         void CursorDownParagraph(BufferView *) const;
299         ///
300         void CursorHome(BufferView *) const;
301         ///
302         void CursorEnd(BufferView *) const;
303         ///
304         void CursorTab(BufferView *) const;
305         ///
306         void CursorTop(BufferView *) const;
307         ///
308         void CursorBottom(BufferView *) const;
309         ///
310         void Delete(BufferView *);
311         ///
312         void Backspace(BufferView *);
313         ///
314         void DeleteWordForward(BufferView *);
315         ///
316         void DeleteWordBackward(BufferView *);
317         ///
318         void DeleteLineForward(BufferView *);
319         ///
320         bool SelectWordWhenUnderCursor(BufferView *);
321         ///
322         enum TextCase {
323                 ///
324                 text_lowercase = 0,
325                 ///
326                 text_capitalization = 1,
327                 ///
328                 text_uppercase = 2
329         };
330         /// Change the case of the word at cursor position.
331         void ChangeWordCase(BufferView *, TextCase action);
332         void TransposeChars(BufferView const &);
333         
334         /** returns a printed row in a pixmap. The y value is needed to
335           decide, wether it is selected text or not. This is a strange
336           solution but faster.
337          */
338         void GetVisibleRow(BufferView *, int y_offset, int x_offset,
339                            Row * row_ptr, int y, bool cleared=false);
340
341 #ifndef NEW_INSETS
342         /* footnotes: */
343         ///
344         void ToggleFootnote(BufferView *);
345         ///
346         void OpenStuff(BufferView *);
347         ///
348         void OpenFootnotes();
349         ///
350         void OpenFootnote(BufferView *);
351         ///
352         void CloseFootnotes();
353         ///
354         void CloseFootnote(BufferView *);
355
356         /** turn the selection into a new environment. If there is no
357           selection, create an empty environment
358          */ 
359         void InsertFootnoteEnvironment(BufferView *,
360                                        LyXParagraph::footnote_kind kind);
361         ///
362         void MeltFootnoteEnvironment(BufferView *);
363 #endif
364         ///
365         void CutSelection(BufferView *, bool = true);
366         ///
367         void CopySelection(BufferView *);
368         ///
369         void PasteSelection(BufferView *);
370         ///
371         void copyEnvironmentType();
372         ///
373         void pasteEnvironmentType(BufferView *);
374         ///
375         void InsertFootnote();
376         ///
377         void InsertMarginpar();
378         ///
379         void InsertFigure();
380         ///
381         void InsertTabular();
382
383         /** the DTP switches for paragraphs. LyX will store the top settings
384          always in the first physical paragraph, the bottom settings in the
385          last. When a paragraph is broken, the top settings rest, the bottom 
386          settings are given to the new one. So I can make shure, they do not
387          duplicate themself (and you cannnot make dirty things with them! )
388          */ 
389         void SetParagraph(BufferView *,
390                           bool line_top, bool line_bottom,
391                           bool pagebreak_top, bool pagebreak_bottom,
392                           VSpace const & space_top,
393                           VSpace const & space_bottom,
394                           LyXAlignment align, 
395                           string labelwidthstring,
396                           bool noindent);
397 #ifndef NO_PEXTRA
398         ///
399         void SetParagraphExtraOpt(BufferView *, int type,
400                                   string const & width,
401                                   string const & widthp,
402                                   int alignment, bool hfill,
403                                   bool start_minipage);
404 #endif
405         /* these things are for search and replace */
406
407         /** sets the selection over the number of characters of string,
408           no check!!
409           */
410         void SetSelectionOverString(BufferView *, string const & str);
411
412         /** simple replacing. The font of the first selected character
413           is used
414           */
415         void ReplaceSelectionWithString(BufferView *, string const & str);
416
417         /// needed to insert the selection
418         void InsertStringA(BufferView *, string const & str);
419         /// needed to insert the selection
420         void InsertStringB(BufferView *, string const & str);
421
422         /// Find next inset of some specified type.
423         bool GotoNextInset(BufferView *, std::vector<Inset::Code> const & codes,
424                            string const & contents = string()) const;
425         ///
426
427         /* for the greater insets */
428   
429         /// returns false if inset wasn't found
430         bool UpdateInset(BufferView *, Inset *);
431         ///
432         void CheckParagraph(BufferView *, LyXParagraph * par,
433                             LyXParagraph::size_type pos);
434         ///
435         int NumberOfCell(LyXParagraph * par,
436                          LyXParagraph::size_type pos) const;
437         ///
438         LyXParagraph * GetParFromID(int id);
439
440         // undo functions
441         /// returns false if no undo possible
442         bool TextUndo(BufferView *);
443         /// returns false if no redo possible
444         bool TextRedo(BufferView *);
445         /// used by TextUndo/TextRedo
446         bool TextHandleUndo(BufferView *, Undo * undo);
447         /// makes sure the next operation will be stored
448         void FinishUndo();
449         /// this is dangerous and for internal use only
450         void FreezeUndo();
451         /// this is dangerous and for internal use only
452         void UnFreezeUndo();
453         /// the flag used by FinishUndo();
454         mutable bool undo_finished;
455         /// a flag
456         bool undo_frozen;
457         ///
458         void SetUndo(Buffer *, Undo::undo_kind kind,
459                      LyXParagraph const * before,
460                      LyXParagraph const * end) const;
461         ///
462         void SetRedo(Buffer *, Undo::undo_kind kind,
463                      LyXParagraph const * before,
464                      LyXParagraph const * end);
465         ///
466         Undo * CreateUndo(Buffer *, Undo::undo_kind kind,
467                           LyXParagraph const * before,
468                           LyXParagraph const * end) const;
469         /// for external use in lyx_cb.C
470         void SetCursorParUndo(Buffer *);
471         ///
472         void RemoveTableRow(LyXCursor & cursor) const;
473         ///
474         bool IsEmptyTableCell() const;
475         ///
476         void toggleAppendix(BufferView *);
477         ///
478         int workWidth(BufferView *) const;
479         ///
480         void ComputeBidiTables(Buffer const *, Row * row) const;
481
482         /// Maps positions in the visual string to positions in logical string.
483         inline
484         LyXParagraph::size_type log2vis(LyXParagraph::size_type pos) const {
485                 if (bidi_start == -1)
486                         return pos;
487                 else
488                         return log2vis_list[pos-bidi_start];
489         }
490
491         /// Maps positions in the logical string to positions in visual string.
492         inline
493         LyXParagraph::size_type vis2log(LyXParagraph::size_type pos) const {
494                 if (bidi_start == -1)
495                         return pos;
496                 else
497                         return vis2log_list[pos-bidi_start];
498         }
499         ///
500         inline
501         LyXParagraph::size_type bidi_level(LyXParagraph::size_type pos) const {
502                 if (bidi_start == -1)
503                         return 0;
504                 else
505                         return bidi_levels[pos-bidi_start];
506         }       
507         ///
508         inline
509         bool bidi_InRange(LyXParagraph::size_type pos) const {
510                 return bidi_start == -1 ||
511                         (bidi_start <= pos && pos <= bidi_end);
512         }
513 private:
514         ///
515         void init();
516         ///
517         mutable Row * firstrow;
518         ///
519         mutable Row * lastrow;
520
521         /** Copybuffer for copy environment type.
522           Asger has learned that this should be a buffer-property instead
523           Lgb has learned that 'char' is a lousy type for non-characters
524           */
525         LyXTextClass::size_type copylayouttype;
526
527         /** inserts a new row behind the specified row, increments
528             the touched counters */
529         void InsertRow(Row * row, LyXParagraph * par,
530                        LyXParagraph::size_type pos) const;
531         /** removes the row and reset the touched counters */
532         void RemoveRow(Row * row) const;
533
534         /** remove all following rows of the paragraph of the specified row. */
535         void RemoveParagraph(Row * row) const;
536
537         /** insert the specified paragraph behind the specified row */
538         void InsertParagraph(BufferView *,
539                              LyXParagraph * par, Row * row) const;
540
541         /** appends  the implizit specified paragraph behind the specified row,
542          * start at the implizit given position */
543         void AppendParagraph(BufferView *, Row * row) const;
544    
545         ///
546         void BreakAgain(BufferView *, Row * row) const;
547         ///
548         void BreakAgainOneRow(BufferView *, Row * row);
549         /// Calculate and set the height of the row
550         void SetHeightOfRow(BufferView *, Row * row_ptr) const;
551
552         /** this calculates the specified parameters. needed when setting
553          * the cursor and when creating a visible row */ 
554         void PrepareToPrint(BufferView *, Row * row, float & x,
555                             float & fill_separator, 
556                             float & fill_hfill,
557                             float & fill_label_hfill,
558                             bool bidi = true) const;
559
560         ///
561         void DeleteEmptyParagraphMechanism(BufferView *,
562                                            LyXCursor const & old_cursor) const;
563
564         /** Updates all counters starting BEHIND the row. Changed paragraphs
565          * with a dynamic left margin will be rebroken. */ 
566         void UpdateCounters(BufferView *, Row * row) const;
567         ///
568         void SetCounter(Buffer const *, LyXParagraph * par) const;
569    
570         /*
571          * some low level functions
572          */
573         
574         ///
575         int SingleWidth(BufferView *, LyXParagraph * par,
576                         LyXParagraph::size_type pos) const;
577         ///
578         int SingleWidth(BufferView *, LyXParagraph * par,
579                         LyXParagraph::size_type pos, char c) const;
580         ///
581         void draw(BufferView *, Row const * row,
582                   LyXParagraph::size_type & pos,
583                   int offset, float & x, bool cleared);
584
585         /// get the next breakpoint in a given paragraph
586         LyXParagraph::size_type NextBreakPoint(BufferView *, Row const * row,
587                                                int width) const;
588         /// returns the minimum space a row needs on the screen in pixel
589         int Fill(BufferView *, Row * row, int workwidth) const;
590         
591         /** returns the minimum space a manual label needs on the
592           screen in pixel */ 
593         int LabelFill(BufferView *, Row const * row) const;
594
595         ///
596         LyXParagraph::size_type
597         BeginningOfMainBody(Buffer const *, LyXParagraph const * par) const;
598         
599         /** Returns the left beginning of the text.
600           This information cannot be taken from the layouts-objekt, because
601           in LaTeX the beginning of the text fits in some cases
602           (for example sections) exactly the label-width.
603           */
604         int LeftMargin(BufferView *, Row const * row) const;
605         ///
606         int RightMargin(Buffer const *, Row const * row) const;
607         ///
608         int LabelEnd (BufferView *, Row const * row) const;
609
610         /** returns the number of separators in the specified row.
611           The separator on the very last column doesnt count
612           */ 
613         int NumberOfSeparators(Buffer const *, Row const * row) const;
614
615         /** returns the number of hfills in the specified row. The
616           LyX-Hfill is a LaTeX \hfill so that the hfills at the
617           beginning and at the end were ignored. This is {\em MUCH}
618           more usefull than not to ignore!
619           */
620         int NumberOfHfills(Buffer const *, Row const * row) const;
621    
622         /// like NumberOfHfills, but only those in the manual label!
623         int NumberOfLabelHfills(Buffer const *, Row const * row) const;
624         /** returns true, if a expansion is needed. Rules are given by 
625           LaTeX
626           */
627         bool HfillExpansion(Buffer const *, Row const * row_ptr,
628                             LyXParagraph::size_type pos) const;
629
630
631         ///
632         mutable std::vector<LyXParagraph::size_type> log2vis_list;
633
634         ///
635         mutable std::vector<LyXParagraph::size_type> vis2log_list;
636
637         ///
638         mutable std::vector<LyXParagraph::size_type> bidi_levels;
639
640         ///
641         mutable LyXParagraph::size_type bidi_start;
642
643         ///
644         mutable LyXParagraph::size_type bidi_end;
645
646         ///
647         mutable bool bidi_same_direction;
648
649         ///
650         unsigned char TransformChar(unsigned char c, LyXParagraph * par,
651                                 LyXParagraph::size_type pos) const;
652
653         /** returns the paragraph position of the last character in the 
654           specified row
655           */
656         LyXParagraph::size_type RowLast(Row const * row) const;
657         ///
658         LyXParagraph::size_type RowLastPrintable(Row const * row) const;
659
660         ///
661         void charInserted();
662         //
663         // special owner functions
664         ///
665         LyXParagraph * OwnerParagraph() const;
666         //
667         LyXParagraph * OwnerParagraph(LyXParagraph *) const;
668 };
669
670
671 /* returns a pointer to the row near the specified y-coordinate
672  * (relative to the whole text). y is set to the real beginning
673  * of this row */
674 inline
675 Row * LyXText::GetRowNearY(int & y) const
676 {
677         // If possible we should optimize this method. (Lgb)
678         Row * tmprow = firstrow;
679         int tmpy = 0;
680         
681         while (tmprow->next() && tmpy + tmprow->height() <= y) {
682                 tmpy += tmprow->height();
683                 tmprow = tmprow->next();
684         }
685         
686         y = tmpy;   // return the real y
687         return tmprow;
688 }
689 #endif