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