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