]> git.lyx.org Git - lyx.git/blob - src/lyxtext.h
c61c94a617cc2b44274c565d9719f60a6e57bf4d
[lyx.git] / src / lyxtext.h
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  * 
5  *           LyX, The Document Processor
6  *       
7  *           Copyright 1995 Matthias Ettrich
8  *           Copyright 1995-2001 The LyX Team.
9  *
10  * ====================================================== */
11
12 #ifndef LYXTEXT_H
13 #define LYXTEXT_H
14
15 #ifdef __GNUG__
16 #pragma interface
17 #endif
18
19 #include "lyxfont.h"
20 #include "lyxcursor.h"
21 #include "paragraph.h"
22 #include "layout.h"
23 #include "lyxrow.h"
24 #include "vspace.h"
25 #include "Spacing.h"
26 #include "LColor.h"
27
28 class Buffer;
29 class BufferParams;
30 class BufferView;
31 class InsetText;
32
33
34 /**
35   This class holds the mapping between buffer paragraphs and screen rows.
36   */
37 class LyXText {
38 public:
39         ///
40         enum text_status {
41                 ///
42                 UNCHANGED = 0,
43                 ///
44                 CHANGED_IN_DRAW = 1,
45                 ///
46                 NEED_VERY_LITTLE_REFRESH = 2,
47                 ///
48                 NEED_MORE_REFRESH = 3
49         };
50         ///
51         enum word_location {
52                 // the word around the cursor, only if the cursor is
53                 //not at a boundary
54                 WHOLE_WORD_STRICT,
55                 // the word around the cursor
56                 WHOLE_WORD,
57                 /// the word begining from the cursor position
58                 PARTIAL_WORD,
59                 /// the word around the cursor or before the cursor
60                 PREVIOUS_WORD,
61                 /// the next word (not yet used)
62                 NEXT_WORD
63         };
64
65         /// Constructor
66         LyXText(BufferView *);
67         ///
68         LyXText(InsetText *);
69    
70         /// Destructor
71         ~LyXText();
72
73         void init(BufferView *, bool reinit = false);
74         ///
75         mutable int number_of_rows;
76         ///
77         mutable int height;
78         ///
79         mutable unsigned int width;
80         /// the current font settings
81         mutable LyXFont current_font;
82         /// the current font
83         mutable LyXFont real_current_font;
84         /// first visible pixel-row is set from LyXScreen!!!
85         // unsigned is wrong here for text-insets!
86         int first;
87         ///
88         BufferView * bv_owner;
89         ///
90         InsetText * inset_owner;
91         ///
92         UpdatableInset * the_locking_inset;
93
94         ///
95         int getRealCursorX(BufferView *) const;
96         ///
97         LyXFont const getFont(Buffer const *, Paragraph * par,
98                         Paragraph::size_type pos) const;
99         ///
100         LyXFont const getLayoutFont(Buffer const *, Paragraph * par) const;
101         ///
102         LyXFont const getLabelFont(Buffer const *, Paragraph * par) const;
103         ///
104         void setCharFont(Buffer const *, Paragraph * par,
105                          Paragraph::size_type pos, LyXFont const & font);
106         void setCharFont(BufferView *, Paragraph * par,
107                          Paragraph::size_type pos,
108                          LyXFont const & font, bool toggleall);
109         /// returns a pointer to the very first Paragraph
110         Paragraph * firstParagraph() const;
111   
112         /// what you expect when pressing <enter> at cursor position
113         void breakParagraph(BufferView *, char keep_layout = 0);
114
115         /** set layout over selection and make a total rebreak of
116           those paragraphs
117           */
118         Paragraph * setLayout(BufferView *, LyXCursor & actual_cursor,
119                                  LyXCursor & selection_start,
120                                  LyXCursor & selection_end,
121                                  LyXTextClass::size_type layout);
122         void setLayout(BufferView *, LyXTextClass::size_type layout);
123         
124         /// used in setlayout
125         void makeFontEntriesLayoutSpecific(Buffer const *, Paragraph * par);
126         
127         /** increment depth over selection and make a total rebreak of those 
128           paragraphs
129           */
130         void incDepth(BufferView *);
131         
132         /** decrement depth over selection and make a total rebreak of those  
133           paragraphs */
134         void decDepth(BufferView *);
135
136         /** Get the depth at current cursor position
137          */
138         int getDepth() const { return cursor.par()->getDepth(); }
139         
140         /** set font over selection and make a total rebreak of those
141           paragraphs.
142           toggleall defaults to false.
143           */
144         void setFont(BufferView *, LyXFont const &, bool toggleall = false);
145         
146         /** deletes and inserts again all paragaphs between the cursor
147           and the specified par. The Cursor is needed to set the refreshing
148           parameters. 
149           This function is needed after SetLayout and SetFont etc.
150           */
151         void redoParagraphs(BufferView *, LyXCursor const & cursor,
152                             Paragraph const * end_par) const;
153         ///
154         void redoParagraph(BufferView *) const;
155         
156         ///
157         void toggleFree(BufferView *, LyXFont const &, bool toggleall = false);
158
159         ///
160         string getStringToIndex(BufferView *);
161         
162         /** recalculates the heights of all previous rows of the
163             specified paragraph.  needed, if the last characters font
164             has changed.  
165             */
166         void redoHeightOfParagraph(BufferView *, LyXCursor const & cursor);
167         
168         /** forces the redrawing of a paragraph. Needed when manipulating a 
169             right address box
170             */ 
171         void redoDrawingOfParagraph(BufferView *, LyXCursor const & cursor);
172         
173         /** insert a character, moves all the following breaks in the 
174           same Paragraph one to the right and make a little rebreak
175           */
176         void insertChar(BufferView *, char c);
177         ///
178         void insertInset(BufferView *, Inset * inset);
179    
180         /** Completes the insertion with a full rebreak.
181             Returns true if something was broken. */
182         bool fullRebreak(BufferView *);
183
184         ///
185         mutable Row * need_break_row;
186         ///
187         mutable int refresh_y;
188         ///
189         int refresh_height;
190         ///
191         int refresh_width;
192         ///
193         int refresh_x;
194         ///
195         mutable Row * refresh_row;
196         ///
197         Paragraph::size_type refresh_pos;
198
199         /// give and set the LyXText status
200         text_status status() const;
201         ///
202         void status(BufferView *, text_status) const;
203
204 private:        
205         /** wether the screen needs a refresh,
206            starting with refresh_y
207            */
208         mutable text_status status_;
209         
210 public:
211         /** returns a pointer to the row near the specified y-coordinate
212           (relative to the whole text). y is set to the real beginning
213           of this row
214           */ 
215         Row * getRowNearY(int & y) const;
216         
217         /** returns the column near the specified x-coordinate of the row 
218          x is set to the real beginning of this column
219          */ 
220         Paragraph::size_type getColumnNearX(BufferView *, Row * row,
221                                             int & x, bool & boundary) const;
222         
223         /** returns a pointer to a specified row. y is set to the beginning
224          of the row
225          */
226         Row * getRow(Paragraph * par,
227                      Paragraph::size_type pos, int & y) const;
228
229         /** returns the height of a default row, needed  for scrollbar
230          */
231         int defaultHeight() const;
232    
233         /** The cursor.
234           Later this variable has to be removed. There should be now internal
235           cursor in a text (and thus not in a buffer). By keeping this it is
236           (I think) impossible to have several views with the same buffer, but
237           the cursor placed at different places.
238           [later]
239           Since the LyXText now has been moved from Buffer to BufferView
240           it should not be absolutely needed to move the cursor...
241           */
242         mutable LyXCursor cursor; // actual cursor position
243
244         /** The structure that keeps track of the selections set. */
245         struct Selection {
246                 Selection() 
247                         : set_(false), mark_(false)
248                         {}
249                 bool set() const {
250                         return set_;
251                 }
252                 void set(bool s) {
253                         set_ = s;
254                 }
255                 bool mark() const {
256                         return mark_;
257                 }
258                 void mark(bool m) {
259                         mark_ = m;
260                 }
261                 LyXCursor cursor; // temporary cursor to hold a cursor position
262                                   // until setSelection is called!
263                 LyXCursor start;  // start of a REAL selection
264                 LyXCursor end;    // end of a REAL selection
265         private:
266                 bool set_; // former selection
267                 bool mark_; // former mark_set
268                 
269         };
270         mutable Selection selection;
271
272         /// needed for the toggling (cursor position on last selection made)
273         mutable LyXCursor last_sel_cursor; 
274         /// needed for toggling the selection in screen.C
275         mutable LyXCursor toggle_cursor;
276         /// needed for toggling the selection in screen.C
277         mutable LyXCursor toggle_end_cursor;
278    
279         /// need the selection cursor:
280         void setSelection(BufferView *);
281         ///
282         void clearSelection() const;
283         ///
284         string const selectionAsString(Buffer const *, bool label) const;
285         
286         /// select the word we need depending on word_location
287         void getWord(LyXCursor & from, LyXCursor & to, 
288                      word_location const) const;
289         /// just selects the word the cursor is in
290         void selectWord(BufferView *, word_location const);
291         /// returns the inset at cursor (if it exists), 0 otherwise
292         Inset * getInset() const;
293
294         /** 'selects" the next word, where the cursor is not in 
295          and returns this word as string. THe cursor will be moved 
296          to the beginning of this word. 
297          With SelectSelectedWord can this be highlighted really
298          */ 
299         string const selectNextWordToSpellcheck(BufferView *, float & value) const;
300         ///
301         void selectSelectedWord(BufferView *);
302         ///
303         void setCursor(BufferView *, Paragraph * par,
304                        Paragraph::size_type pos,
305                        bool setfont = true,
306                        bool boundary = false) const;
307         ///
308         void setCursor(BufferView *, LyXCursor &, Paragraph * par,
309                        Paragraph::size_type pos,
310                        bool boundary = false) const;
311         ///
312         void setCursorIntern(BufferView *, Paragraph * par,
313                              Paragraph::size_type pos,
314                              bool setfont = true,
315                              bool boundary = false) const;
316         ///
317         void setCurrentFont(BufferView *) const;
318
319         ///
320         bool isBoundary(Buffer const *, Paragraph * par,
321                         Paragraph::size_type pos) const;
322         ///
323         bool isBoundary(Buffer const *, Paragraph * par,
324                          Paragraph::size_type pos,
325                          LyXFont const & font) const;
326
327         ///
328         void setCursorFromCoordinates(BufferView *, int x, int y) const;
329         ///
330         void setCursorFromCoordinates(BufferView *, LyXCursor &,
331                                       int x, int y) const;
332         ///
333         void cursorUp(BufferView *) const;
334         ///
335         void cursorDown(BufferView *) const;
336         ///
337         void cursorLeft(BufferView *, bool internal = true) const;
338         ///
339         void cursorRight(BufferView *, bool internal = true) const;
340         ///
341         void cursorLeftOneWord(BufferView *) const;
342         ///
343         void cursorLeftOneWord(LyXCursor &) const;
344         ///
345         void cursorRightOneWord(BufferView *) const;
346         ///
347         void cursorUpParagraph(BufferView *) const;
348         ///
349         void cursorDownParagraph(BufferView *) const;
350         ///
351         void cursorHome(BufferView *) const;
352         ///
353         void cursorEnd(BufferView *) const;
354         ///
355         void cursorTab(BufferView *) const;
356         ///
357         void cursorTop(BufferView *) const;
358         ///
359         void cursorBottom(BufferView *) const;
360         ///
361         void Delete(BufferView *);
362         ///
363         void backspace(BufferView *);
364         ///
365         void deleteWordForward(BufferView *);
366         ///
367         void deleteWordBackward(BufferView *);
368         ///
369         void deleteLineForward(BufferView *);
370         ///
371         bool selectWordWhenUnderCursor(BufferView *, 
372                                        word_location const);
373         ///
374         enum TextCase {
375                 ///
376                 text_lowercase = 0,
377                 ///
378                 text_capitalization = 1,
379                 ///
380                 text_uppercase = 2
381         };
382         /// Change the case of the word at cursor position.
383         void changeCase(BufferView *, TextCase action);
384         ///
385         void changeRegionCase(BufferView * bview,
386                                        LyXCursor const & from,
387                                        LyXCursor const & to,
388                                        LyXText::TextCase action);
389         ///
390         void transposeChars(BufferView &);
391         
392         /** returns a printed row in a pixmap. The y value is needed to
393           decide, wether it is selected text or not. This is a strange
394           solution but faster.
395          */
396         void getVisibleRow(BufferView *, int y_offset, int x_offset,
397                            Row * row_ptr, int y, bool cleared=false);
398
399         /// 
400         void toggleInset(BufferView *);
401         ///
402         void cutSelection(BufferView *, bool doclear = true, bool realcut = true);
403         ///
404         void copySelection(BufferView *);
405         ///
406         void pasteSelection(BufferView *);
407         ///
408         void copyEnvironmentType();
409         ///
410         void pasteEnvironmentType(BufferView *);
411
412         /** the DTP switches for paragraphs. LyX will store the top settings
413          always in the first physical paragraph, the bottom settings in the
414          last. When a paragraph is broken, the top settings rest, the bottom 
415          settings are given to the new one. So I can make shure, they do not
416          duplicate themself (and you cannnot make dirty things with them! )
417          */ 
418         void setParagraph(BufferView *,
419                           bool line_top, bool line_bottom,
420                           bool pagebreak_top, bool pagebreak_bottom,
421                           VSpace const & space_top,
422                           VSpace const & space_bottom,
423                           Spacing const & spacing,
424                           LyXAlignment align, 
425                           string labelwidthstring,
426                           bool noindent);
427
428         /* these things are for search and replace */
429
430         /** sets the selection over the number of characters of string,
431           no check!!
432           */
433         void setSelectionOverString(BufferView *, string const & str);
434
435         /** simple replacing. The font of the first selected character
436           is used
437           */
438         void replaceSelectionWithString(BufferView *, string const & str);
439
440         /// needed to insert the selection
441         void insertStringAsLines(BufferView *, string const & str);
442         /// needed to insert the selection
443         void insertStringAsParagraphs(BufferView *, string const & str);
444
445         /// Find next inset of some specified type.
446         bool gotoNextInset(BufferView *, std::vector<Inset::Code> const & codes,
447                            string const & contents = string()) const;
448         ///
449
450         /* for the greater insets */
451   
452         /// returns false if inset wasn't found
453         bool updateInset(BufferView *, Inset *);
454         ///
455         void checkParagraph(BufferView *, Paragraph * par,
456                             Paragraph::size_type pos);
457         ///
458         int numberOfCell(Paragraph * par,
459                          Paragraph::size_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         Paragraph::size_type log2vis(Paragraph::size_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         Paragraph::size_type vis2log(Paragraph::size_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         Paragraph::size_type bidi_level(Paragraph::size_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(Paragraph::size_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         LyXTextClass::size_type copylayouttype;
515
516         /** inserts a new row behind the specified row, increments
517             the touched counters */
518         void insertRow(Row * row, Paragraph * par,
519                        Paragraph::size_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                         Paragraph::size_type pos) const;
623         ///
624         int singleWidth(BufferView *, Paragraph * par,
625                         Paragraph::size_type pos, char c) const;
626         ///
627         void draw(BufferView *, Row const * row,
628                   Paragraph::size_type & pos,
629                   int offset, float & x, bool cleared);
630
631         /// get the next breakpoint in a given paragraph
632         Paragraph::size_type nextBreakPoint(BufferView *, Row const * row,
633                                                int width) const;
634         /// returns the minimum space a row needs on the screen in pixel
635         int fill(BufferView *, Row * row, int workwidth) const;
636         
637         /** returns the minimum space a manual label needs on the
638           screen in pixel */ 
639         int labelFill(BufferView *, Row const * row) const;
640
641         ///
642         Paragraph::size_type
643         beginningOfMainBody(Buffer const *, Paragraph const * par) const;
644         
645         /** Returns the left beginning of the text.
646           This information cannot be taken from the layouts-objekt, because
647           in LaTeX the beginning of the text fits in some cases
648           (for example sections) exactly the label-width.
649           */
650         int leftMargin(BufferView *, Row const * row) const;
651         ///
652         int rightMargin(Buffer const *, Row const * row) const;
653         ///
654         int labelEnd (BufferView *, Row const * row) const;
655
656         /** returns the number of separators in the specified row.
657           The separator on the very last column doesnt count
658           */ 
659         int numberOfSeparators(Buffer const *, Row const * row) const;
660
661         /** returns the number of hfills in the specified row. The
662           LyX-Hfill is a LaTeX \hfill so that the hfills at the
663           beginning and at the end were ignored. This is {\em MUCH}
664           more usefull than not to ignore!
665           */
666         int numberOfHfills(Buffer const *, Row const * row) const;
667    
668         /// like NumberOfHfills, but only those in the manual label!
669         int numberOfLabelHfills(Buffer const *, Row const * row) const;
670         /** returns true, if a expansion is needed. Rules are given by 
671           LaTeX
672           */
673         bool hfillExpansion(Buffer const *, Row const * row_ptr,
674                             Paragraph::size_type pos) const;
675         /// 
676         LColor::color backgroundColor();
677
678
679         ///
680         mutable std::vector<Paragraph::size_type> log2vis_list;
681
682         ///
683         mutable std::vector<Paragraph::size_type> vis2log_list;
684
685         ///
686         mutable std::vector<Paragraph::size_type> bidi_levels;
687
688         ///
689         mutable Paragraph::size_type bidi_start;
690
691         ///
692         mutable Paragraph::size_type bidi_end;
693
694         ///
695         mutable bool bidi_same_direction;
696
697         ///
698         unsigned char transformChar(unsigned char c, Paragraph * par,
699                                     Paragraph::size_type pos) const;
700
701         /** returns the paragraph position of the last character in the 
702           specified row
703           */
704         Paragraph::size_type rowLast(Row const * row) const;
705         ///
706         Paragraph::size_type rowLastPrintable(Row const * row) const;
707
708         ///
709         void charInserted();
710 public:
711         //
712         // special owner functions
713         ///
714         Paragraph * ownerParagraph() const;
715         //
716         void ownerParagraph(Paragraph *) const;
717         // set it searching first for the right owner using the paragraph id
718         void ownerParagraph(int id, Paragraph *) const;
719 };
720
721
722 /* returns a pointer to the row near the specified y-coordinate
723  * (relative to the whole text). y is set to the real beginning
724  * of this row */
725 inline
726 Row * LyXText::getRowNearY(int & y) const
727 {
728         // If possible we should optimize this method. (Lgb)
729         Row * tmprow = firstrow;
730         int tmpy = 0;
731         
732         while (tmprow->next() && tmpy + tmprow->height() <= y) {
733                 tmpy += tmprow->height();
734                 tmprow = tmprow->next();
735         }
736         
737         y = tmpy;   // return the real y
738         return tmprow;
739 }
740 #endif