]> git.lyx.org Git - lyx.git/blob - src/lyxtext.h
get rid of LyXText::need_break_row
[lyx.git] / src / lyxtext.h
1 // -*- C++ -*-
2 /**
3  * \file lyxtext.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author unknown
8  * \author Lars Gullik Bjønnes
9  * \author John Levon
10  *
11  * Full author contact details are available in file CREDITS
12  */
13
14 #ifndef LYXTEXT_H
15 #define LYXTEXT_H
16
17 #include "lyxfont.h"
18 #include "lyxcursor.h"
19 #include "layout.h"
20 #include "LColor.h"
21 #include "insets/inset.h"
22 #include "RowList.h"
23 #include "bufferview_funcs.h"
24 #include "textcursor.h"
25
26 class Buffer;
27 class BufferParams;
28 class BufferView;
29 class InsetText;
30 class Paragraph;
31 class Row;
32 class Spacing;
33 class UpdatableInset;
34 class VSpace;
35 class WordLangTuple;
36 class ParagraphList;
37 class MetricsInfo;
38 class Dimension;
39
40
41 /**
42   This class holds the mapping between buffer paragraphs and screen rows.
43   */
44
45 // The inheritance from TextCursor should go. It's just there to ease
46 // transition...
47 class LyXText : public TextCursor {
48 public:
49         /// Constructor
50         LyXText(BufferView *);
51         /// sets inset as owner
52         LyXText(BufferView *, InsetText *);
53
54         void init(BufferView *);
55         ///
56         int height;
57         ///
58         unsigned int width;
59         /// the current font settings
60         LyXFont current_font;
61         /// the current font
62         LyXFont real_current_font;
63 private:
64         /** the 'anchor' row: the position of this row remains constant
65          *  with respect to the top of the screen
66          */
67         RowList::iterator anchor_row_;
68         /** the pixel offset with respect to this row of top_y
69          */
70         int anchor_row_offset_;
71 public:
72         /// update all cached row positions
73         void updateRowPositions();
74         /// get the y coord. of the top of the screen (relative to doc start)
75         int top_y() const;
76         /// set the y coord. of the top of the screen (relative to doc start)
77         void top_y(int newy);
78         /// set the anchoring row. top_y will be computed relative to this
79         void anchor_row(RowList::iterator rit);
80         ///
81         InsetText * inset_owner;
82         ///
83         UpdatableInset * the_locking_inset;
84
85         ///
86         int getRealCursorX() const;
87         ///
88         LyXFont const getFont(Buffer const *, ParagraphList::iterator pit,
89                 lyx::pos_type pos) const;
90         ///
91         LyXFont const getLayoutFont(Buffer const *,
92                                     ParagraphList::iterator pit) const;
93         ///
94         LyXFont const getLabelFont(Buffer const *,
95                                    ParagraphList::iterator pit) const;
96         ///
97         void setCharFont(Buffer const *, ParagraphList::iterator pit,
98                          lyx::pos_type pos, LyXFont const & font);
99         void setCharFont(ParagraphList::iterator pit,
100                          lyx::pos_type pos,
101                          LyXFont const & font, bool toggleall);
102
103         ///
104         void breakAgainOneRow(RowList::iterator rit);
105         /// what you expect when pressing <enter> at cursor position
106         void breakParagraph(ParagraphList & paragraphs, char keep_layout = 0);
107
108         /** set layout over selection and make a total rebreak of
109           those paragraphs
110           */
111         ParagraphList::iterator
112         setLayout(LyXCursor & actual_cursor,
113                   LyXCursor & selection_start,
114                   LyXCursor & selection_end,
115                   string const & layout);
116         ///
117         void setLayout(string const & layout);
118
119         /**
120          * Increase or decrease the nesting depth of the selected paragraph(s)
121          * if test_only, don't change any depths. Returns whether something
122          * (would have) changed
123          */
124         bool changeDepth(bv_funcs::DEPTH_CHANGE type, bool test_only);
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(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(LyXCursor const & cursor,
141                             ParagraphList::iterator endpit);
142         ///
143         void redoParagraph();
144
145         ///
146         void toggleFree(LyXFont const &, bool toggleall = false);
147
148         ///
149         string getStringToIndex();
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();
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(char c);
161         ///
162         void insertInset(InsetOld * inset);
163
164         /// Completes the insertion with a rebreak
165         void partialRebreak();
166         /// a full rebreak of the whole text
167         void fullRebreak();
168         /// compute text metrics
169         void metrics(MetricsInfo & mi, Dimension & dim);
170
171         /// clear any pending paints
172         void clearPaint();
173
174         /// submit repaint request
175         void postPaint();
176
177         ///
178         InsetOld::RESULT dispatch(FuncRequest const & cmd);
179
180         BufferView * bv();
181
182         BufferView * bv() const;
183
184         friend class LyXScreen;
185
186         /**
187          * Return the status. This represents what repaints are
188          * pending after some operation (e.g. inserting a char).
189          */
190         bool needRefresh() const;
191
192 private:
193         // do we need a refresh?
194         bool need_refresh_;
195
196 public:
197         /// only the top-level LyXText has this non-zero
198         BufferView * bv_owner;
199
200 private:
201         /// returns a pointer to a specified row.
202         RowList::iterator
203         getRow(ParagraphList::iterator pit, lyx::pos_type pos) const;
204 public:
205         /// returns a pointer cursor row
206         RowList::iterator getRow(LyXCursor const & cursor) const;
207         /// convenience
208         RowList::iterator cursorRow() const;
209         /**
210          * Return the next row, when cursor is at the end of the
211          * previous row, for insets that take a full row.
212          *
213          * FIXME: explain why we need this ? especially for y...
214          */
215         RowList::iterator cursorIRow() const;
216
217         /** returns a pointer to the row near the specified y-coordinate
218           (relative to the whole text). y is set to the real beginning
219           of this row
220           */
221         RowList::iterator getRowNearY(int & y) const;
222
223         /** returns the column near the specified x-coordinate of the row
224          x is set to the real beginning of this column
225          */
226         lyx::pos_type getColumnNearX(RowList::iterator rit,
227                                      int & x, bool & boundary) const;
228
229         /** returns a pointer to a specified row. y is set to the beginning
230          of the row
231          */
232         RowList::iterator
233         getRow(ParagraphList::iterator pit, lyx::pos_type pos, int & y) const;
234
235         RowList & rows() const {
236                 return rowlist_;
237         }
238
239
240         /// need the selection cursor:
241         void setSelection();
242         ///
243         void clearSelection();
244
245         /// select the word we need depending on word_location
246         void getWord(LyXCursor & from, LyXCursor & to, lyx::word_location const);
247         /// just selects the word the cursor is in
248         void selectWord(lyx::word_location loc);
249         /// returns the inset at cursor (if it exists), 0 otherwise
250         InsetOld * getInset() const;
251
252         /// accept selected change
253         void acceptChange();
254
255         /// reject selected change
256         void rejectChange();
257
258         /** 'selects" the next word, where the cursor is not in
259          and returns this word as string. THe cursor will be moved
260          to the beginning of this word.
261          With SelectSelectedWord can this be highlighted really
262          */
263         WordLangTuple const selectNextWordToSpellcheck(float & value);
264         ///
265         void selectSelectedWord();
266         /// returns true if par was empty and was removed
267         bool setCursor(ParagraphList::iterator pit,
268                        lyx::pos_type pos,
269                        bool setfont = true,
270                        bool boundary = false);
271         ///
272         void setCursor(LyXCursor &, ParagraphList::iterator pit,
273                        lyx::pos_type pos,
274                        bool boundary = false);
275         ///
276         void setCursorIntern(ParagraphList::iterator pit,
277                              lyx::pos_type pos,
278                              bool setfont = true,
279                              bool boundary = false);
280         ///
281         void setCurrentFont();
282
283         ///
284         bool isBoundary(Buffer const *, Paragraph const & par,
285                         lyx::pos_type pos) const;
286         ///
287         bool isBoundary(Buffer const *, Paragraph const & par,
288                          lyx::pos_type pos,
289                          LyXFont const & font) const;
290
291         ///
292         void setCursorFromCoordinates(int x, int y);
293         ///
294         void setCursorFromCoordinates(LyXCursor &,
295                                       int x, int y);
296         ///
297         void cursorUp(bool selecting = false);
298         ///
299         void cursorDown(bool selecting = false);
300         ///
301         void cursorLeft(bool internal = true);
302         ///
303         void cursorRight(bool internal = true);
304         ///
305         void cursorLeftOneWord();
306         ///
307         void cursorRightOneWord();
308         ///
309         void cursorUpParagraph();
310         ///
311         void cursorDownParagraph();
312         ///
313         void cursorHome();
314         ///
315         void cursorEnd();
316         ///
317         void cursorPrevious();
318         ///
319         void cursorNext();
320         ///
321         void cursorTop();
322         ///
323         void cursorBottom();
324         ///
325         void Delete();
326         ///
327         void backspace();
328         ///
329         bool selectWordWhenUnderCursor(lyx::word_location);
330         ///
331         enum TextCase {
332                 ///
333                 text_lowercase = 0,
334                 ///
335                 text_capitalization = 1,
336                 ///
337                 text_uppercase = 2
338         };
339         /// Change the case of the word at cursor position.
340         void changeCase(TextCase action);
341
342         ///
343         void toggleInset();
344         ///
345         void cutSelection(bool doclear = true, bool realcut = true);
346         ///
347         void copySelection();
348         ///
349         void pasteSelection(size_t sel_index = 0);
350
351         /** the DTP switches for paragraphs. LyX will store the top settings
352          always in the first physical paragraph, the bottom settings in the
353          last. When a paragraph is broken, the top settings rest, the bottom
354          settings are given to the new one. So I can make shure, they do not
355          duplicate themself (and you cannnot make dirty things with them! )
356          */
357         void setParagraph(bool line_top, bool line_bottom,
358                           bool pagebreak_top, bool pagebreak_bottom,
359                           VSpace const & space_top,
360                           VSpace const & space_bottom,
361                           Spacing const & spacing,
362                           LyXAlignment align,
363                           string const & labelwidthstring,
364                           bool noindent);
365
366         /* these things are for search and replace */
367
368         /**
369          * Sets the selection from the current cursor position to length
370          * characters to the right. No safety checks.
371          */
372         void setSelectionRange(lyx::pos_type length);
373
374         /** simple replacing. The font of the first selected character
375           is used
376           */
377         void replaceSelectionWithString(string const & str);
378
379         /// needed to insert the selection
380         void insertStringAsLines(string const & str);
381         /// needed to insert the selection
382         void insertStringAsParagraphs(string const & str);
383
384         /// Find next inset of some specified type.
385         bool gotoNextInset(std::vector<InsetOld::Code> const & codes,
386                            string const & contents = string());
387         ///
388         void gotoInset(std::vector<InsetOld::Code> const & codes,
389                        bool same_content);
390         ///
391         void gotoInset(InsetOld::Code code, bool same_content);
392         ///
393
394         /* for the greater insets */
395
396         /// returns false if inset wasn't found
397         bool updateInset(InsetOld *);
398         ///
399         void checkParagraph(ParagraphList::iterator pit, lyx::pos_type pos);
400         ///
401         int workWidth() const;
402         /// returns width of row containing inset
403         int workWidth(InsetOld const * inset) const;
404
405         ///
406         void computeBidiTables(Buffer const *, RowList::iterator row) const;
407         /// Maps positions in the visual string to positions in logical string.
408         lyx::pos_type log2vis(lyx::pos_type pos) const;
409         /// Maps positions in the logical string to positions in visual string.
410         lyx::pos_type vis2log(lyx::pos_type pos) const;
411         ///
412         lyx::pos_type bidi_level(lyx::pos_type pos) const;
413         ///
414         bool bidi_InRange(lyx::pos_type pos) const;
415 private:
416         ///
417         mutable RowList rowlist_;
418
419         ///
420         float getCursorX(RowList::iterator rit, lyx::pos_type pos,
421                          lyx::pos_type last, bool boundary) const;
422         /// used in setlayout
423         void makeFontEntriesLayoutSpecific(Buffer const &, Paragraph & par);
424
425         /** forces the redrawing of a paragraph. Needed when manipulating a
426             right address box
427             */
428         void redoDrawingOfParagraph(LyXCursor const & cursor);
429
430         /// removes the row and reset the touched counters
431         void removeRow(RowList::iterator rit);
432
433         /// remove all following rows of the paragraph of the specified row.
434         void removeParagraph(RowList::iterator rit);
435
436         /// insert the specified paragraph behind the specified row
437         void insertParagraph(ParagraphList::iterator pit,
438                              RowList::iterator rowit);
439
440         /** appends  the implizit specified paragraph behind the specified row,
441          * start at the implizit given position */
442         void appendParagraph(RowList::iterator rowit);
443
444         ///
445         void breakAgain(RowList::iterator rit);
446         /// Calculate and set the height of the row
447         void setHeightOfRow(RowList::iterator rit);
448
449         // fix the cursor `cur' after a characters has been deleted at `where'
450         // position. Called by deleteEmptyParagraphMechanism
451         void fixCursorAfterDelete(LyXCursor & cur,
452                                   LyXCursor const & where);
453
454         /// delete double space (false) or empty paragraphs (true) around old_cursor
455         bool deleteEmptyParagraphMechanism(LyXCursor const & old_cursor);
456
457 public:
458         /** Updates all counters starting BEHIND the row. Changed paragraphs
459          * with a dynamic left margin will be rebroken. */
460         void updateCounters();
461         ///
462         void update();
463         /**
464          * Returns an inset if inset was hit, or 0 if not.
465          * If hit, the coordinates are changed relative to the inset.
466          */
467         InsetOld * checkInsetHit(int & x, int & y);
468
469         ///
470         int singleWidth(ParagraphList::iterator pit, lyx::pos_type pos) const;
471         ///
472         int singleWidth(ParagraphList::iterator pit,
473                 lyx::pos_type pos, char c) const;
474         /// rebuild row cache
475         void rebuildRows(ParagraphList::iterator pit);
476
477         /// return the color of the canvas
478         LColor::color backgroundColor() const;
479
480         ///
481         mutable bool bidi_same_direction;
482
483         unsigned char transformChar(unsigned char c, Paragraph const & par,
484                                     lyx::pos_type pos) const;
485
486         /**
487          * Returns the left beginning of the text.
488          * This information cannot be taken from the layout object, because
489          * in LaTeX the beginning of the text fits in some cases
490          * (for example sections) exactly the label-width.
491          */
492         int leftMargin(Row const & row) const;
493         ///
494         int rightMargin(Buffer const &, Row const & row) const;
495
496         /** this calculates the specified parameters. needed when setting
497          * the cursor and when creating a visible row */
498         void prepareToPrint(RowList::iterator row, int & x,
499                             int & fill_separator,
500                             int & fill_hfill,
501                             int & fill_label_hfill,
502                             bool bidi = true) const;
503
504 private:
505         ///
506         void setCounter(Buffer const *, ParagraphList::iterator pit);
507         ///
508         void deleteWordForward();
509         ///
510         void deleteWordBackward();
511         ///
512         void deleteLineForward();
513
514         /*
515          * some low level functions
516          */
517
518
519         /// return the pos value *before* which a row should break.
520         /// for example, the pos at which IsNewLine(pos) == true
521         lyx::pos_type rowBreakPoint(Row const & row) const;
522
523         /// returns the minimum space a row needs on the screen in pixel
524         int fill(RowList::iterator row, int workwidth) const;
525
526         /**
527          * returns the minimum space a manual label needs on the
528          * screen in pixels
529          */
530         int labelFill(Row const & row) const;
531
532         /// FIXME
533         int labelEnd(Row const & row) const;
534
535         ///
536         mutable std::vector<lyx::pos_type> log2vis_list;
537         ///
538         mutable std::vector<lyx::pos_type> vis2log_list;
539         ///
540         mutable std::vector<lyx::pos_type> bidi_levels;
541         ///
542         mutable lyx::pos_type bidi_start;
543         ///
544         mutable lyx::pos_type bidi_end;
545
546         ///
547         void charInserted();
548 public:
549         //
550         // special owner functions
551         ///
552         ParagraphList & ownerParagraphs() const;
553
554         /// return true if this is owned by an inset.
555         bool isInInset() const;
556
557 private:
558         /** Cursor related data.
559           Later this variable has to be removed. There should be now internal
560           cursor in a text */
561         ///
562         ///TextCursor cursor_;
563 };
564
565 /// return the default height of a row in pixels, considering font zoom
566 extern int defaultRowHeight();
567
568 #endif // LYXTEXT_H