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