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