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