]> git.lyx.org Git - lyx.git/blob - src/lyxtext.h
6a756e5c80ff3b05fc69f22f8a908429fd4e39d0
[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-2001 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 "lyxcursor.h"
21 #include "paragraph.h"
22 #include "layout.h"
23 #include "lyxrow.h"
24 #include "vspace.h"
25 #include "LColor.h"
26
27 class Buffer;
28 class BufferParams;
29 class BufferView;
30 class InsetText;
31
32
33 /**
34   This class holds the mapping between buffer paragraphs and screen rows.
35   */
36 class LyXText {
37 public:
38         ///
39         enum text_status {
40                 ///
41                 UNCHANGED = 0,
42                 ///
43                 CHANGED_IN_DRAW = 1,
44                 ///
45                 NEED_VERY_LITTLE_REFRESH = 2,
46                 ///
47                 NEED_MORE_REFRESH = 3
48         };
49         ///
50         enum word_location {
51                 // the word around the cursor, only if the cursor is
52                 //not at a boundary
53                 WHOLE_WORD_STRICT,
54                 // the word around the cursor
55                 WHOLE_WORD,
56                 /// the word begining from the cursor position
57                 PARTIAL_WORD,
58                 /// the word around the cursor or before the cursor
59                 PREVIOUS_WORD,
60                 /// the next word (not yet used)
61                 NEXT_WORD
62         };
63
64         /// Constructor
65         LyXText(BufferView *);
66         ///
67         LyXText(InsetText *);
68    
69         /// Destructor
70         ~LyXText();
71
72         void init(BufferView *, bool reinit = false);
73         ///
74         mutable int number_of_rows;
75         ///
76         mutable int height;
77         ///
78         mutable unsigned int width;
79         /// the current font settings
80         mutable LyXFont current_font;
81         /// the current font
82         mutable LyXFont real_current_font;
83         /// first visible pixel-row is set from LyXScreen!!!
84         // unsigned is wrong here for text-insets!
85         int first;
86         ///
87         BufferView * bv_owner;
88         ///
89         InsetText * inset_owner;
90         ///
91         UpdatableInset * the_locking_inset;
92
93         ///
94         int getRealCursorX(BufferView *) const;
95         ///
96         LyXFont const getFont(Buffer const *, Paragraph * par,
97                         Paragraph::size_type pos) const;
98         ///
99         void setCharFont(Buffer const *, Paragraph * par,
100                          Paragraph::size_type pos, LyXFont const & font);
101         void setCharFont(BufferView *, Paragraph * par,
102                          Paragraph::size_type pos,
103                          LyXFont const & font, bool toggleall);
104         /// returns a pointer to the very first Paragraph
105         Paragraph * firstParagraph() const;
106   
107         /// what you expect when pressing <enter> at cursor position
108         void breakParagraph(BufferView *, char keep_layout = 0);
109
110         /** set layout over selection and make a total rebreak of
111           those paragraphs
112           */
113         Paragraph * setLayout(BufferView *, LyXCursor & actual_cursor,
114                                  LyXCursor & selection_start,
115                                  LyXCursor & selection_end,
116                                  LyXTextClass::size_type layout);
117         void setLayout(BufferView *, LyXTextClass::size_type layout);
118         
119         /// used in setlayout
120         void makeFontEntriesLayoutSpecific(Buffer const *, Paragraph * par);
121         
122         /** increment depth over selection and make a total rebreak of those 
123           paragraphs
124           */
125         void incDepth(BufferView *);
126         
127         /** decrement depth over selection and make a total rebreak of those  
128           paragraphs */
129         void decDepth(BufferView *);
130
131         /** Get the depth at current cursor position
132          */
133         int getDepth() const { return cursor.par()->getDepth(); }
134         
135         /** set font over selection and make a total rebreak of those
136           paragraphs.
137           toggleall defaults to false.
138           */
139         void setFont(BufferView *, LyXFont const &, bool toggleall = false);
140         
141         /** deletes and inserts again all paragaphs between the cursor
142           and the specified par. The Cursor is needed to set the refreshing
143           parameters. 
144           This function is needed after SetLayout and SetFont etc.
145           */
146         void redoParagraphs(BufferView *, LyXCursor const & cursor,
147                             Paragraph const * end_par) const;
148         ///
149         void redoParagraph(BufferView *) const;
150         
151         ///
152         void toggleFree(BufferView *, LyXFont const &, bool toggleall = false);
153
154         ///
155         string getStringToIndex(BufferView *);
156         
157         /** recalculates the heights of all previous rows of the
158             specified paragraph.  needed, if the last characters font
159             has changed.  
160             */
161         void redoHeightOfParagraph(BufferView *, LyXCursor const & cursor);
162         
163         /** forces the redrawing of a paragraph. Needed when manipulating a 
164             right address box
165             */ 
166         void redoDrawingOfParagraph(BufferView *, LyXCursor const & cursor);
167         
168         /** insert a character, moves all the following breaks in the 
169           same Paragraph one to the right and make a little rebreak
170           */
171         void insertChar(BufferView *, char c);
172         ///
173         void insertInset(BufferView *, Inset * inset);
174    
175         /** Completes the insertion with a full rebreak.
176             Returns true if something was broken. */
177         bool fullRebreak(BufferView *);
178
179         ///
180         Row * need_break_row;
181         ///
182         mutable int refresh_y;
183         ///
184         int refresh_height;
185         ///
186         int refresh_width;
187         ///
188         int refresh_x;
189         ///
190         mutable Row * refresh_row;
191         ///
192         int refresh_pos;
193
194         /// give and set the LyXText status
195         text_status status() const;
196         void status(BufferView *, text_status) const;
197
198 private:        
199         /** wether the screen needs a refresh,
200            starting with refresh_y
201            */
202         mutable text_status status_;
203         
204 public:
205         /** returns a pointer to the row near the specified y-coordinate
206           (relative to the whole text). y is set to the real beginning
207           of this row
208           */ 
209         Row * getRowNearY(int & y) const;
210         
211         /** returns the column near the specified x-coordinate of the row 
212          x is set to the real beginning of this column
213          */ 
214         int getColumnNearX(BufferView *, Row * row,
215                            int & x, bool & boundary) const;
216         
217         /** returns a pointer to a specified row. y is set to the beginning
218          of the row
219          */
220         Row * getRow(Paragraph * par,
221                      Paragraph::size_type pos, int & y) const;
222
223         /** returns the height of a default row, needed  for scrollbar
224          */
225         int defaultHeight() const;
226    
227         /** The cursor.
228           Later this variable has to be removed. There should be now internal
229           cursor in a text (and thus not in a buffer). By keeping this it is
230           (I think) impossible to have several views with the same buffer, but
231           the cursor placed at different places.
232           [later]
233           Since the LyXText now has been moved from Buffer to BufferView
234           it should not be absolutely needed to move the cursor...
235           */
236         mutable LyXCursor cursor;
237
238         /** The structrue that keeps track of the selections set. */
239         struct Selection {
240                 Selection() 
241                         : set_(false), mark_(false)
242                         {}
243                 bool set() const {
244                         return set_;
245                 }
246                 void set(bool s) {
247                         set_ = s;
248                 }
249                 bool mark() const {
250                         return mark_;
251                 }
252                 void mark(bool m) {
253                         mark_ = m;
254                 }
255                 LyXCursor cursor;
256                 LyXCursor start;
257                 LyXCursor end;
258         private:
259                 bool set_; // former selection
260                 bool mark_; // former mark_set
261                 
262         };
263         mutable Selection selection;
264
265         /// needed for the toggling
266         LyXCursor last_sel_cursor;
267         ///
268         LyXCursor toggle_cursor;
269         ///
270         LyXCursor toggle_end_cursor;
271    
272         /// need the selection cursor:
273         void setSelection(BufferView *);
274         ///
275         void clearSelection(BufferView *) const;
276         ///
277         string const selectionAsString(Buffer const *) const;
278         
279         /// select the word we need depending on word_location
280         void getWord(LyXCursor & from, LyXCursor & to, 
281                      word_location const) const;
282         /// just selects the word the cursor is in
283         void selectWord(BufferView *, word_location const);
284         /// returns the inset at cursor (if it exists), 0 otherwise
285         Inset * getInset() const;
286
287         /** 'selects" the next word, where the cursor is not in 
288          and returns this word as string. THe cursor will be moved 
289          to the beginning of this word. 
290          With SelectSelectedWord can this be highlighted really
291          */ 
292         string const selectNextWord(BufferView *, float & value) const;
293         ///
294         void selectSelectedWord(BufferView *);
295         ///
296         void setCursor(BufferView *, Paragraph * par,
297                        Paragraph::size_type pos,
298                        bool setfont = true,
299                        bool boundary = false) const;
300         ///
301         void setCursor(BufferView *, LyXCursor &, Paragraph * par,
302                        Paragraph::size_type pos,
303                        bool boundary = false) const;
304         ///
305         void setCursorIntern(BufferView *, Paragraph * par,
306                              Paragraph::size_type pos,
307                              bool setfont = true,
308                              bool boundary = false) const;
309         ///
310         void setCurrentFont(BufferView *) const;
311
312         ///
313         bool isBoundary(Buffer const *, Paragraph * par,
314                         Paragraph::size_type pos) const;
315         ///
316         bool isBoundary(Buffer const *, Paragraph * par,
317                          Paragraph::size_type pos,
318                          LyXFont const & font) const;
319
320         ///
321         void setCursorFromCoordinates(BufferView *, int x, int y) const;
322         ///
323         void setCursorFromCoordinates(BufferView *, LyXCursor &,
324                                       int x, int y) const;
325         ///
326         void cursorUp(BufferView *) const;
327         ///
328         void cursorDown(BufferView *) const;
329         ///
330         void cursorLeft(BufferView *, bool internal = true) const;
331         ///
332         void cursorRight(BufferView *, bool internal = true) const;
333         ///
334         void cursorLeftOneWord(BufferView *) const;
335         ///
336         void cursorLeftOneWord(LyXCursor &) const;
337         ///
338         void cursorRightOneWord(BufferView *) const;
339         ///
340         void cursorUpParagraph(BufferView *) const;
341         ///
342         void cursorDownParagraph(BufferView *) const;
343         ///
344         void cursorHome(BufferView *) const;
345         ///
346         void cursorEnd(BufferView *) const;
347         ///
348         void cursorTab(BufferView *) const;
349         ///
350         void cursorTop(BufferView *) const;
351         ///
352         void cursorBottom(BufferView *) const;
353         ///
354         void Delete(BufferView *);
355         ///
356         void backspace(BufferView *);
357         ///
358         void deleteWordForward(BufferView *);
359         ///
360         void deleteWordBackward(BufferView *);
361         ///
362         void deleteLineForward(BufferView *);
363         ///
364         bool selectWordWhenUnderCursor(BufferView *, 
365                                        word_location const);
366         ///
367         enum TextCase {
368                 ///
369                 text_lowercase = 0,
370                 ///
371                 text_capitalization = 1,
372                 ///
373                 text_uppercase = 2
374         };
375         /// Change the case of the word at cursor position.
376         void changeCase(BufferView *, TextCase action);
377         ///
378         void changeRegionCase(BufferView * bview,
379                                        LyXCursor const & from,
380                                        LyXCursor const & to,
381                                        LyXText::TextCase action);
382         ///
383         void transposeChars(BufferView &);
384         
385         /** returns a printed row in a pixmap. The y value is needed to
386           decide, wether it is selected text or not. This is a strange
387           solution but faster.
388          */
389         void getVisibleRow(BufferView *, int y_offset, int x_offset,
390                            Row * row_ptr, int y, bool cleared=false);
391
392         /// 
393         void toggleInset(BufferView *);
394         ///
395         void cutSelection(BufferView *, bool = true);
396         ///
397         void copySelection(BufferView *);
398         ///
399         void pasteSelection(BufferView *);
400         ///
401         void copyEnvironmentType();
402         ///
403         void pasteEnvironmentType(BufferView *);
404
405         /** the DTP switches for paragraphs. LyX will store the top settings
406          always in the first physical paragraph, the bottom settings in the
407          last. When a paragraph is broken, the top settings rest, the bottom 
408          settings are given to the new one. So I can make shure, they do not
409          duplicate themself (and you cannnot make dirty things with them! )
410          */ 
411         void setParagraph(BufferView *,
412                           bool line_top, bool line_bottom,
413                           bool pagebreak_top, bool pagebreak_bottom,
414                           VSpace const & space_top,
415                           VSpace const & space_bottom,
416                           LyXAlignment align, 
417                           string labelwidthstring,
418                           bool noindent);
419
420         /* these things are for search and replace */
421
422         /** sets the selection over the number of characters of string,
423           no check!!
424           */
425         void setSelectionOverString(BufferView *, string const & str);
426
427         /** simple replacing. The font of the first selected character
428           is used
429           */
430         void replaceSelectionWithString(BufferView *, string const & str);
431
432         /// needed to insert the selection
433         void insertStringAsLines(BufferView *, string const & str);
434         /// needed to insert the selection
435         void insertStringAsParagraphs(BufferView *, string const & str);
436
437         /// Find next inset of some specified type.
438         bool gotoNextInset(BufferView *, std::vector<Inset::Code> const & codes,
439                            string const & contents = string()) const;
440         ///
441
442         /* for the greater insets */
443   
444         /// returns false if inset wasn't found
445         bool updateInset(BufferView *, Inset *);
446         ///
447         void checkParagraph(BufferView *, Paragraph * par,
448                             Paragraph::size_type pos);
449         ///
450         int numberOfCell(Paragraph * par,
451                          Paragraph::size_type pos) const;
452         ///
453         void removeTableRow(LyXCursor & cursor) const;
454         ///
455         bool isEmptyTableCell() const;
456         ///
457         void toggleAppendix(BufferView *);
458         ///
459         int workWidth(BufferView *) const;
460         ///
461         void computeBidiTables(Buffer const *, Row * row) const;
462
463         /// Maps positions in the visual string to positions in logical string.
464         inline
465         Paragraph::size_type log2vis(Paragraph::size_type pos) const {
466                 if (bidi_start == -1)
467                         return pos;
468                 else
469                         return log2vis_list[pos-bidi_start];
470         }
471
472         /// Maps positions in the logical string to positions in visual string.
473         inline
474         Paragraph::size_type vis2log(Paragraph::size_type pos) const {
475                 if (bidi_start == -1)
476                         return pos;
477                 else
478                         return vis2log_list[pos-bidi_start];
479         }
480         ///
481         inline
482         Paragraph::size_type bidi_level(Paragraph::size_type pos) const {
483                 if (bidi_start == -1)
484                         return 0;
485                 else
486                         return bidi_levels[pos-bidi_start];
487         }       
488         ///
489         inline
490         bool bidi_InRange(Paragraph::size_type pos) const {
491                 return bidi_start == -1 ||
492                         (bidi_start <= pos && pos <= bidi_end);
493         }
494 private:
495         ///
496         mutable Row * firstrow;
497         ///
498         mutable Row * lastrow;
499
500         /** Copybuffer for copy environment type.
501           Asger has learned that this should be a buffer-property instead
502           Lgb has learned that 'char' is a lousy type for non-characters
503           */
504         LyXTextClass::size_type copylayouttype;
505
506         /** inserts a new row behind the specified row, increments
507             the touched counters */
508         void insertRow(Row * row, Paragraph * par,
509                        Paragraph::size_type pos) const;
510         /** removes the row and reset the touched counters */
511         void removeRow(Row * row) const;
512
513         /** remove all following rows of the paragraph of the specified row. */
514         void removeParagraph(Row * row) const;
515
516         /** insert the specified paragraph behind the specified row */
517         void insertParagraph(BufferView *,
518                              Paragraph * par, Row * row) const;
519
520         /** appends  the implizit specified paragraph behind the specified row,
521          * start at the implizit given position */
522         void appendParagraph(BufferView *, Row * row) const;
523    
524         ///
525         void breakAgain(BufferView *, Row * row) const;
526         ///
527         void breakAgainOneRow(BufferView *, Row * row);
528         /// Calculate and set the height of the row
529         void setHeightOfRow(BufferView *, Row * row_ptr) const;
530
531         /** this calculates the specified parameters. needed when setting
532          * the cursor and when creating a visible row */ 
533         void prepareToPrint(BufferView *, Row * row, float & x,
534                             float & fill_separator, 
535                             float & fill_hfill,
536                             float & fill_label_hfill,
537                             bool bidi = true) const;
538
539         ///
540         void deleteEmptyParagraphMechanism(BufferView *,
541                                            LyXCursor const & old_cursor) const;
542
543 public:
544         /** Updates all counters starting BEHIND the row. Changed paragraphs
545          * with a dynamic left margin will be rebroken. */ 
546         void updateCounters(BufferView *, Row * row) const;
547 private:
548         ///
549         void setCounter(Buffer const *, Paragraph * par) const;
550    
551         /*
552          * some low level functions
553          */
554         
555         ///
556         int singleWidth(BufferView *, Paragraph * par,
557                         Paragraph::size_type pos) const;
558         ///
559         int singleWidth(BufferView *, Paragraph * par,
560                         Paragraph::size_type pos, char c) const;
561         ///
562         void draw(BufferView *, Row const * row,
563                   Paragraph::size_type & pos,
564                   int offset, float & x, bool cleared);
565
566         /// get the next breakpoint in a given paragraph
567         Paragraph::size_type nextBreakPoint(BufferView *, Row const * row,
568                                                int width) const;
569         /// returns the minimum space a row needs on the screen in pixel
570         int fill(BufferView *, Row * row, int workwidth) const;
571         
572         /** returns the minimum space a manual label needs on the
573           screen in pixel */ 
574         int labelFill(BufferView *, Row const * row) const;
575
576         ///
577         Paragraph::size_type
578         beginningOfMainBody(Buffer const *, Paragraph const * par) const;
579         
580         /** Returns the left beginning of the text.
581           This information cannot be taken from the layouts-objekt, because
582           in LaTeX the beginning of the text fits in some cases
583           (for example sections) exactly the label-width.
584           */
585         int leftMargin(BufferView *, Row const * row) const;
586         ///
587         int rightMargin(Buffer const *, Row const * row) const;
588         ///
589         int labelEnd (BufferView *, Row const * row) const;
590
591         /** returns the number of separators in the specified row.
592           The separator on the very last column doesnt count
593           */ 
594         int numberOfSeparators(Buffer const *, Row const * row) const;
595
596         /** returns the number of hfills in the specified row. The
597           LyX-Hfill is a LaTeX \hfill so that the hfills at the
598           beginning and at the end were ignored. This is {\em MUCH}
599           more usefull than not to ignore!
600           */
601         int numberOfHfills(Buffer const *, Row const * row) const;
602    
603         /// like NumberOfHfills, but only those in the manual label!
604         int numberOfLabelHfills(Buffer const *, Row const * row) const;
605         /** returns true, if a expansion is needed. Rules are given by 
606           LaTeX
607           */
608         bool hfillExpansion(Buffer const *, Row const * row_ptr,
609                             Paragraph::size_type pos) const;
610         /// 
611         LColor::color backgroundColor();
612
613
614         ///
615         mutable std::vector<Paragraph::size_type> log2vis_list;
616
617         ///
618         mutable std::vector<Paragraph::size_type> vis2log_list;
619
620         ///
621         mutable std::vector<Paragraph::size_type> bidi_levels;
622
623         ///
624         mutable Paragraph::size_type bidi_start;
625
626         ///
627         mutable Paragraph::size_type bidi_end;
628
629         ///
630         mutable bool bidi_same_direction;
631
632         ///
633         unsigned char transformChar(unsigned char c, Paragraph * par,
634                                     Paragraph::size_type pos) const;
635
636         /** returns the paragraph position of the last character in the 
637           specified row
638           */
639         Paragraph::size_type rowLast(Row const * row) const;
640         ///
641         Paragraph::size_type rowLastPrintable(Row const * row) const;
642
643         ///
644         void charInserted();
645 public:
646         //
647         // special owner functions
648         ///
649         Paragraph * ownerParagraph() const;
650         //
651         Paragraph * ownerParagraph(Paragraph *) const;
652         // set it searching first for the right owner using the paragraph id
653         Paragraph * ownerParagraph(int id, Paragraph *) const;
654 };
655
656
657 /* returns a pointer to the row near the specified y-coordinate
658  * (relative to the whole text). y is set to the real beginning
659  * of this row */
660 inline
661 Row * LyXText::getRowNearY(int & y) const
662 {
663         // If possible we should optimize this method. (Lgb)
664         Row * tmprow = firstrow;
665         int tmpy = 0;
666         
667         while (tmprow->next() && tmpy + tmprow->height() <= y) {
668                 tmpy += tmprow->height();
669                 tmprow = tmprow->next();
670         }
671         
672         y = tmpy;   // return the real y
673         return tmprow;
674 }
675 #endif