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