]> git.lyx.org Git - lyx.git/blob - src/lyxtext.h
601f7960f1a8562e8f1446ea89e0e5dba1ad04d5
[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 public:
505         ///
506         mutable Row * firstrow;
507 private:
508         ///
509         mutable Row * lastrow;
510
511         /** Copybuffer for copy environment type.
512           Asger has learned that this should be a buffer-property instead
513           Lgb has learned that 'char' is a lousy type for non-characters
514           */
515         lyx::layout_type copylayouttype;
516
517         /** inserts a new row behind the specified row, increments
518             the touched counters */
519         void insertRow(Row * row, Paragraph * par, lyx::pos_type pos) const;
520         /** removes the row and reset the touched counters */
521         void removeRow(Row * row) const;
522
523         /** remove all following rows of the paragraph of the specified row. */
524         void removeParagraph(Row * row) const;
525
526         /** insert the specified paragraph behind the specified row */
527         void insertParagraph(BufferView *,
528                              Paragraph * par, Row * row) const;
529
530         /** appends  the implizit specified paragraph behind the specified row,
531          * start at the implizit given position */
532         void appendParagraph(BufferView *, Row * row) const;
533    
534         ///
535         void breakAgain(BufferView *, Row * row) const;
536         ///
537         void breakAgainOneRow(BufferView *, Row * row);
538         /// Calculate and set the height of the row
539         void setHeightOfRow(BufferView *, Row * row_ptr) const;
540
541         /** this calculates the specified parameters. needed when setting
542          * the cursor and when creating a visible row */ 
543         void prepareToPrint(BufferView *, Row * row, float & x,
544                             float & fill_separator, 
545                             float & fill_hfill,
546                             float & fill_label_hfill,
547                             bool bidi = true) const;
548
549         /// A struct used for drawing routines
550         struct DrawRowParams {
551                 // the bufferview
552                 BufferView * bv; 
553                 // the row
554                 Row * row;
555                 // the painter to use
556                 Painter * pain; 
557                 // has the background been cleared
558                 bool cleared;
559                 /// x offset
560                 int xo;
561                 /// y offset 
562                 int yo;
563                 /// FIXME
564                 float x;
565                 /// FIXME
566                 int y;
567                 /// the inset/view full width
568                 int width;
569                 /// hfill size
570                 float hfill;
571                 /// label hfill size
572                 float label_hfill;
573                 /// fill separator size
574                 float separator;
575         };
576                  
577         /// paint the background
578         bool paintRowBackground(DrawRowParams & p);
579  
580         /// paint the selection background 
581         void paintRowSelection(DrawRowParams & p);
582
583         /// paint appendix marker
584         void paintRowAppendix(DrawRowParams & p);
585          
586         /// paint env depth bar 
587         void paintRowDepthBar(DrawRowParams & p);
588  
589         /// paint a first row in a paragraph
590         void paintFirstRow(DrawRowParams & p);
591  
592         /// paint a last row in a paragraph
593         void paintLastRow(DrawRowParams & p);
594  
595         /// paint text
596         void paintRowText(DrawRowParams & p);
597  
598         // fix the cursor `cur' after a characters has been deleted at `where'
599         // position. Called by deleteEmptyParagraphMechanism
600         void fixCursorAfterDelete(BufferView * bview,
601                                   LyXCursor & cur,
602                                   LyXCursor const & where) const;
603         
604         ///
605         void deleteEmptyParagraphMechanism(BufferView *,
606                                            LyXCursor const & old_cursor) const;
607
608 public:
609         /** Updates all counters starting BEHIND the row. Changed paragraphs
610          * with a dynamic left margin will be rebroken. */ 
611         void updateCounters(BufferView *, Row * row) const;
612 private:
613         ///
614         void setCounter(Buffer const *, Paragraph * par) const;
615    
616         /*
617          * some low level functions
618          */
619         
620         ///
621         int singleWidth(BufferView *, Paragraph * par,
622                 lyx::pos_type pos) const;
623         ///
624         int singleWidth(BufferView *, Paragraph * par,
625                 lyx::pos_type pos, char c) const;
626         ///
627         void draw(BufferView *, Row const * row,
628                   lyx::pos_type & pos, int offset, float & x, bool cleared);
629
630         /// get the next breakpoint in a given paragraph
631         lyx::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         lyx::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                             lyx::pos_type pos) const;
672         /// 
673         LColor::color backgroundColor();
674
675
676         ///
677         mutable std::vector<lyx::pos_type> log2vis_list;
678
679         ///
680         mutable std::vector<lyx::pos_type> vis2log_list;
681
682         ///
683         mutable std::vector<lyx::pos_type> bidi_levels;
684
685         ///
686         mutable lyx::pos_type bidi_start;
687
688         ///
689         mutable lyx::pos_type bidi_end;
690
691         ///
692         mutable bool bidi_same_direction;
693
694         ///
695         unsigned char transformChar(unsigned char c, Paragraph * par,
696                                     lyx::pos_type pos) const;
697
698         /** returns the paragraph position of the last character in the 
699           specified row
700           */
701         lyx::pos_type rowLast(Row const * row) const;
702         ///
703         lyx::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