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