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