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