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