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