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