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