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