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