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