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