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