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