]> git.lyx.org Git - lyx.git/blob - src/lyxtext.h
Added function firstRow() to LyXText as this function is needed in
[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         /** returns the firstrow, this could be done with the above too but
231             IMO it's stupid to have to allocate a dummy y all the time I need
232             the first row
233         */
234         Row * firstRow() { return firstrow; }
235                         
236         /** returns the height of a default row, needed  for scrollbar
237          */
238         int defaultHeight() const;
239    
240         /** The cursor.
241           Later this variable has to be removed. There should be now internal
242           cursor in a text (and thus not in a buffer). By keeping this it is
243           (I think) impossible to have several views with the same buffer, but
244           the cursor placed at different places.
245           [later]
246           Since the LyXText now has been moved from Buffer to BufferView
247           it should not be absolutely needed to move the cursor...
248           */
249         mutable LyXCursor cursor; // actual cursor position
250
251         /** The structure that keeps track of the selections set. */
252         struct Selection {
253                 Selection() 
254                         : set_(false), mark_(false)
255                         {}
256                 bool set() const {
257                         return set_;
258                 }
259                 void set(bool s) {
260                         set_ = s;
261                 }
262                 bool mark() const {
263                         return mark_;
264                 }
265                 void mark(bool m) {
266                         mark_ = m;
267                 }
268                 LyXCursor cursor; // temporary cursor to hold a cursor position
269                                   // until setSelection is called!
270                 LyXCursor start;  // start of a REAL selection
271                 LyXCursor end;    // end of a REAL selection
272         private:
273                 bool set_; // former selection
274                 bool mark_; // former mark_set
275                 
276         };
277         mutable Selection selection;
278
279         /// needed for the toggling (cursor position on last selection made)
280         mutable LyXCursor last_sel_cursor; 
281         /// needed for toggling the selection in screen.C
282         mutable LyXCursor toggle_cursor;
283         /// needed for toggling the selection in screen.C
284         mutable LyXCursor toggle_end_cursor;
285    
286         /// need the selection cursor:
287         void setSelection(BufferView *);
288         ///
289         void clearSelection() const;
290         ///
291         string const selectionAsString(Buffer const *, bool label) const;
292         
293         /// select the word we need depending on word_location
294         void getWord(LyXCursor & from, LyXCursor & to, 
295                      word_location const) const;
296         /// just selects the word the cursor is in
297         void selectWord(BufferView *, word_location const);
298         /// returns the inset at cursor (if it exists), 0 otherwise
299         Inset * getInset() const;
300
301         /** 'selects" the next word, where the cursor is not in 
302          and returns this word as string. THe cursor will be moved 
303          to the beginning of this word. 
304          With SelectSelectedWord can this be highlighted really
305          */ 
306         string const selectNextWordToSpellcheck(BufferView *, float & value) const;
307         ///
308         void selectSelectedWord(BufferView *);
309         ///
310         void setCursor(BufferView *, Paragraph * par,
311                        lyx::pos_type pos,
312                        bool setfont = true,
313                        bool boundary = false) const;
314         ///
315         void setCursor(BufferView *, LyXCursor &, Paragraph * par,
316                        lyx::pos_type pos,
317                        bool boundary = false) const;
318         ///
319         void setCursorIntern(BufferView *, Paragraph * par,
320                              lyx::pos_type pos,
321                              bool setfont = true,
322                              bool boundary = false) const;
323         ///
324         void setCurrentFont(BufferView *) const;
325
326         ///
327         bool isBoundary(Buffer const *, Paragraph * par,
328                         lyx::pos_type pos) const;
329         ///
330         bool isBoundary(Buffer const *, Paragraph * par,
331                          lyx::pos_type pos,
332                          LyXFont const & font) const;
333
334         ///
335         void setCursorFromCoordinates(BufferView *, int x, int y) const;
336         ///
337         void setCursorFromCoordinates(BufferView *, LyXCursor &,
338                                       int x, int y) const;
339         ///
340         void cursorUp(BufferView *) const;
341         ///
342         void cursorDown(BufferView *) const;
343         ///
344         void cursorLeft(BufferView *, bool internal = true) const;
345         ///
346         void cursorRight(BufferView *, bool internal = true) const;
347         ///
348         void cursorLeftOneWord(BufferView *) const;
349         ///
350         void cursorLeftOneWord(LyXCursor &) const;
351         ///
352         void cursorRightOneWord(BufferView *) const;
353         ///
354         void cursorUpParagraph(BufferView *) const;
355         ///
356         void cursorDownParagraph(BufferView *) const;
357         ///
358         void cursorHome(BufferView *) const;
359         ///
360         void cursorEnd(BufferView *) const;
361         ///
362         void cursorTab(BufferView *) const;
363         ///
364         void cursorTop(BufferView *) const;
365         ///
366         void cursorBottom(BufferView *) const;
367         ///
368         void Delete(BufferView *);
369         ///
370         void backspace(BufferView *);
371         ///
372         void deleteWordForward(BufferView *);
373         ///
374         void deleteWordBackward(BufferView *);
375         ///
376         void deleteLineForward(BufferView *);
377         ///
378         bool selectWordWhenUnderCursor(BufferView *, 
379                                        word_location const);
380         ///
381         enum TextCase {
382                 ///
383                 text_lowercase = 0,
384                 ///
385                 text_capitalization = 1,
386                 ///
387                 text_uppercase = 2
388         };
389         /// Change the case of the word at cursor position.
390         void changeCase(BufferView *, TextCase action);
391         ///
392         void changeRegionCase(BufferView * bview,
393                                        LyXCursor const & from,
394                                        LyXCursor const & to,
395                                        LyXText::TextCase action);
396         ///
397         void transposeChars(BufferView &);
398         
399         /** returns a printed row in a pixmap. The y value is needed to
400           decide, wether it is selected text or not. This is a strange
401           solution but faster.
402          */
403         void getVisibleRow(BufferView *, int y_offset, int x_offset,
404                            Row * row_ptr, int y, bool cleared=false);
405
406         /// 
407         void toggleInset(BufferView *);
408         ///
409         void cutSelection(BufferView *, bool doclear = true, bool realcut = true);
410         ///
411         void copySelection(BufferView *);
412         ///
413         void pasteSelection(BufferView *);
414         ///
415         void copyEnvironmentType();
416         ///
417         void pasteEnvironmentType(BufferView *);
418
419         /** the DTP switches for paragraphs. LyX will store the top settings
420          always in the first physical paragraph, the bottom settings in the
421          last. When a paragraph is broken, the top settings rest, the bottom 
422          settings are given to the new one. So I can make shure, they do not
423          duplicate themself (and you cannnot make dirty things with them! )
424          */ 
425         void setParagraph(BufferView *,
426                           bool line_top, bool line_bottom,
427                           bool pagebreak_top, bool pagebreak_bottom,
428                           VSpace const & space_top,
429                           VSpace const & space_bottom,
430                           Spacing const & spacing,
431                           LyXAlignment align, 
432                           string labelwidthstring,
433                           bool noindent);
434
435         /* these things are for search and replace */
436
437         /** sets the selection over the number of characters of string,
438           no check!!
439           */
440         void setSelectionOverString(BufferView *, string const & str);
441
442         /** simple replacing. The font of the first selected character
443           is used
444           */
445         void replaceSelectionWithString(BufferView *, string const & str);
446
447         /// needed to insert the selection
448         void insertStringAsLines(BufferView *, string const & str);
449         /// needed to insert the selection
450         void insertStringAsParagraphs(BufferView *, string const & str);
451
452         /// Find next inset of some specified type.
453         bool gotoNextInset(BufferView *, std::vector<Inset::Code> const & codes,
454                            string const & contents = string()) const;
455         ///
456
457         /* for the greater insets */
458   
459         /// returns false if inset wasn't found
460         bool updateInset(BufferView *, Inset *);
461         ///
462         void checkParagraph(BufferView *, Paragraph * par, lyx::pos_type pos);
463         ///
464         int numberOfCell(Paragraph * par, lyx::pos_type pos) const;
465         ///
466         void removeTableRow(LyXCursor & cursor) const;
467         ///
468         bool isEmptyTableCell() const;
469         ///
470         void toggleAppendix(BufferView *);
471         ///
472         int workWidth(BufferView *) const;
473         ///
474         int workWidth(BufferView *, Inset * inset) const;
475         ///
476         void computeBidiTables(Buffer const *, Row * row) const;
477
478         /// Maps positions in the visual string to positions in logical string.
479         inline
480         lyx::pos_type log2vis(lyx::pos_type pos) const {
481                 if (bidi_start == -1)
482                         return pos;
483                 else
484                         return log2vis_list[pos-bidi_start];
485         }
486
487         /// Maps positions in the logical string to positions in visual string.
488         inline
489         lyx::pos_type vis2log(lyx::pos_type pos) const {
490                 if (bidi_start == -1)
491                         return pos;
492                 else
493                         return vis2log_list[pos-bidi_start];
494         }
495         ///
496         inline
497         lyx::pos_type bidi_level(lyx::pos_type pos) const {
498                 if (bidi_start == -1)
499                         return 0;
500                 else
501                         return bidi_levels[pos-bidi_start];
502         }       
503         ///
504         inline
505         bool bidi_InRange(lyx::pos_type pos) const {
506                 return bidi_start == -1 ||
507                         (bidi_start <= pos && pos <= bidi_end);
508         }
509 private:
510         ///
511         mutable Row * firstrow;
512         ///
513         mutable Row * lastrow;
514
515         /** Copybuffer for copy environment type.
516           Asger has learned that this should be a buffer-property instead
517           Lgb has learned that 'char' is a lousy type for non-characters
518           */
519         lyx::layout_type copylayouttype;
520
521         /** inserts a new row behind the specified row, increments
522             the touched counters */
523         void insertRow(Row * row, Paragraph * par, lyx::pos_type pos) const;
524         /** removes the row and reset the touched counters */
525         void removeRow(Row * row) const;
526
527         /** remove all following rows of the paragraph of the specified row. */
528         void removeParagraph(Row * row) const;
529
530         /** insert the specified paragraph behind the specified row */
531         void insertParagraph(BufferView *,
532                              Paragraph * par, Row * row) const;
533
534         /** appends  the implizit specified paragraph behind the specified row,
535          * start at the implizit given position */
536         void appendParagraph(BufferView *, Row * row) const;
537    
538         ///
539         void breakAgain(BufferView *, Row * row) const;
540         ///
541         void breakAgainOneRow(BufferView *, Row * row);
542         /// Calculate and set the height of the row
543         void setHeightOfRow(BufferView *, Row * row_ptr) const;
544
545         /** this calculates the specified parameters. needed when setting
546          * the cursor and when creating a visible row */ 
547         void prepareToPrint(BufferView *, Row * row, float & x,
548                             float & fill_separator, 
549                             float & fill_hfill,
550                             float & fill_label_hfill,
551                             bool bidi = true) const;
552
553         /// A struct used for drawing routines
554         struct DrawRowParams {
555                 // the bufferview
556                 BufferView * bv; 
557                 // the row
558                 Row * row;
559                 // the painter to use
560                 Painter * pain; 
561                 // has the background been cleared
562                 bool cleared;
563                 /// x offset
564                 int xo;
565                 /// y offset 
566                 int yo;
567                 /// FIXME
568                 float x;
569                 /// FIXME
570                 int y;
571                 /// the inset/view full width
572                 int width;
573                 /// hfill size
574                 float hfill;
575                 /// label hfill size
576                 float label_hfill;
577                 /// fill separator size
578                 float separator;
579         };
580                  
581         /// paint the background
582         bool paintRowBackground(DrawRowParams & p);
583  
584         /// paint the selection background 
585         void paintRowSelection(DrawRowParams & p);
586
587         /// paint appendix marker
588         void paintRowAppendix(DrawRowParams & p);
589          
590         /// paint env depth bar 
591         void paintRowDepthBar(DrawRowParams & p);
592  
593         /// paint a first row in a paragraph
594         void paintFirstRow(DrawRowParams & p);
595  
596         /// paint a last row in a paragraph
597         void paintLastRow(DrawRowParams & p);
598  
599         /// paint text
600         void paintRowText(DrawRowParams & p);
601  
602         // fix the cursor `cur' after a characters has been deleted at `where'
603         // position. Called by deleteEmptyParagraphMechanism
604         void fixCursorAfterDelete(BufferView * bview,
605                                   LyXCursor & cur,
606                                   LyXCursor const & where) const;
607         
608         ///
609         void deleteEmptyParagraphMechanism(BufferView *,
610                                            LyXCursor const & old_cursor) const;
611
612 public:
613         /** Updates all counters starting BEHIND the row. Changed paragraphs
614          * with a dynamic left margin will be rebroken. */ 
615         void updateCounters(BufferView *, Row * row) const;
616 private:
617         ///
618         void setCounter(Buffer const *, Paragraph * par) const;
619    
620         /*
621          * some low level functions
622          */
623         
624         ///
625         int singleWidth(BufferView *, Paragraph * par,
626                 lyx::pos_type pos) const;
627         ///
628         int singleWidth(BufferView *, Paragraph * par,
629                 lyx::pos_type pos, char c) const;
630         ///
631         void draw(BufferView *, Row const * row,
632                   lyx::pos_type & pos, int offset, float & x, bool cleared);
633
634         /// get the next breakpoint in a given paragraph
635         lyx::pos_type nextBreakPoint(BufferView *, Row const * row, int width) const;
636         /// returns the minimum space a row needs on the screen in pixel
637         int fill(BufferView *, Row * row, int workwidth) const;
638         
639         /** returns the minimum space a manual label needs on the
640           screen in pixel */ 
641         int labelFill(BufferView *, Row const * row) const;
642
643         ///
644         lyx::pos_type beginningOfMainBody(Buffer const *, Paragraph const * par) const;
645         
646         /** Returns the left beginning of the text.
647           This information cannot be taken from the layouts-objekt, because
648           in LaTeX the beginning of the text fits in some cases
649           (for example sections) exactly the label-width.
650           */
651         int leftMargin(BufferView *, Row const * row) const;
652         ///
653         int rightMargin(Buffer const *, Row const * row) const;
654         ///
655         int labelEnd (BufferView *, Row const * row) const;
656
657         /** returns the number of separators in the specified row.
658           The separator on the very last column doesnt count
659           */ 
660         int numberOfSeparators(Buffer const *, Row const * row) const;
661
662         /** returns the number of hfills in the specified row. The
663           LyX-Hfill is a LaTeX \hfill so that the hfills at the
664           beginning and at the end were ignored. This is {\em MUCH}
665           more usefull than not to ignore!
666           */
667         int numberOfHfills(Buffer const *, Row const * row) const;
668    
669         /// like NumberOfHfills, but only those in the manual label!
670         int numberOfLabelHfills(Buffer const *, Row const * row) const;
671         /** returns true, if a expansion is needed. Rules are given by 
672           LaTeX
673           */
674         bool hfillExpansion(Buffer const *, Row const * row_ptr,
675                             lyx::pos_type pos) const;
676         /// 
677         LColor::color backgroundColor();
678
679
680         ///
681         mutable std::vector<lyx::pos_type> log2vis_list;
682
683         ///
684         mutable std::vector<lyx::pos_type> vis2log_list;
685
686         ///
687         mutable std::vector<lyx::pos_type> bidi_levels;
688
689         ///
690         mutable lyx::pos_type bidi_start;
691
692         ///
693         mutable lyx::pos_type bidi_end;
694
695         ///
696         mutable bool bidi_same_direction;
697
698         ///
699         unsigned char transformChar(unsigned char c, Paragraph * par,
700                                     lyx::pos_type pos) const;
701
702         /** returns the paragraph position of the last character in the 
703           specified row
704           */
705         lyx::pos_type rowLast(Row const * row) const;
706         ///
707         lyx::pos_type rowLastPrintable(Row const * row) const;
708
709         ///
710         void charInserted();
711 public:
712         //
713         // special owner functions
714         ///
715         Paragraph * ownerParagraph() const;
716         //
717         void ownerParagraph(Paragraph *) const;
718         // set it searching first for the right owner using the paragraph id
719         void ownerParagraph(int id, Paragraph *) const;
720 };
721
722 #endif