]> git.lyx.org Git - lyx.git/blob - src/lyxtext.h
two-phase-drawing for InsetText & InsetTabular
[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
38
39 /**
40   This class holds the mapping between buffer paragraphs and screen rows.
41   */
42
43 // The inheritance from TextCursor should go. It's just there to ease
44 // transition...
45 class LyXText : public TextCursor {
46 public:
47         /// Constructor
48         LyXText(BufferView *);
49         /// sets inset as owner
50         LyXText(BufferView *, InsetText *);
51
52         void init(BufferView *);
53         ///
54         int height;
55         ///
56         unsigned int width;
57         /// the current font settings
58         LyXFont current_font;
59         /// the current font
60         LyXFont real_current_font;
61 private:
62         /** the 'anchor' row: the position of this row remains constant
63          *  with respect to the top of the screen
64          */
65         RowList::iterator anchor_row_;
66         /** the pixel offset with respect to this row of top_y
67          */
68         int anchor_row_offset_;
69 public:
70         /// update all cached row positions
71         void updateRowPositions();
72         /// get the y coord. of the top of the screen (relative to doc start)
73         int top_y() const;
74         /// set the y coord. of the top of the screen (relative to doc start)
75         void top_y(int newy);
76         /// set the anchoring row. top_y will be computed relative to this
77         void anchor_row(RowList::iterator rit);
78         ///
79         InsetText * inset_owner;
80         ///
81         UpdatableInset * the_locking_inset;
82
83         ///
84         int getRealCursorX() const;
85         ///
86         LyXFont const getFont(Buffer const *, ParagraphList::iterator pit,
87                 lyx::pos_type pos) const;
88         ///
89         LyXFont const getLayoutFont(Buffer const *,
90                                     ParagraphList::iterator pit) const;
91         ///
92         LyXFont const getLabelFont(Buffer const *,
93                                    ParagraphList::iterator pit) const;
94         ///
95         void setCharFont(Buffer const *, ParagraphList::iterator pit,
96                          lyx::pos_type pos, LyXFont const & font);
97         void setCharFont(ParagraphList::iterator pit,
98                          lyx::pos_type pos,
99                          LyXFont const & font, bool toggleall);
100
101         ///
102         void breakAgainOneRow(RowList::iterator rit);
103         /// what you expect when pressing <enter> at cursor position
104         void breakParagraph(ParagraphList & paragraphs, char keep_layout = 0);
105
106         /** set layout over selection and make a total rebreak of
107           those paragraphs
108           */
109         ParagraphList::iterator
110         setLayout(LyXCursor & actual_cursor,
111                   LyXCursor & selection_start,
112                   LyXCursor & selection_end,
113                   string const & layout);
114         ///
115         void setLayout(string const & layout);
116
117         /**
118          * Increase or decrease the nesting depth of the selected paragraph(s)
119          * if test_only, don't change any depths. Returns whether something
120          * (would have) changed
121          */
122         bool changeDepth(bv_funcs::DEPTH_CHANGE type, bool test_only);
123
124         /// get the depth at current cursor position
125         int getDepth() const;
126
127         /** set font over selection and make a total rebreak of those
128           paragraphs.
129           toggleall defaults to false.
130           */
131         void setFont(LyXFont const &, bool toggleall = false);
132
133         /** deletes and inserts again all paragaphs between the cursor
134           and the specified par. The Cursor is needed to set the refreshing
135           parameters.
136           This function is needed after SetLayout and SetFont etc.
137           */
138         void redoParagraphs(LyXCursor const & cursor,
139                             ParagraphList::iterator endpit);
140         ///
141         void redoParagraph();
142
143         ///
144         void toggleFree(LyXFont const &, bool toggleall = false);
145
146         ///
147         string getStringToIndex();
148
149         /** recalculates the heights of all previous rows of the
150             specified paragraph.  needed, if the last characters font
151             has changed.
152             */
153         void redoHeightOfParagraph();
154
155         /** insert a character, moves all the following breaks in the
156           same Paragraph one to the right and make a little rebreak
157           */
158         void insertChar(char c);
159         ///
160         void insertInset(Inset * inset);
161
162         /// Completes the insertion with a rebreak from 'need_break_row' on
163         void partialRebreak();
164         /// a full rebreak of the whole text
165         void fullRebreak();
166         /// rebuild RowList cache
167         void rebuild(int maxwidth);
168
169         ///
170         RowList::iterator need_break_row;
171
172         /// clear any pending paints
173         void clearPaint();
174
175         /// submit repaint request
176         void postPaint();
177
178         ///
179         Inset::RESULT dispatch(FuncRequest const & cmd);
180
181         BufferView * bv();
182
183         BufferView * bv() const;
184
185         friend class LyXScreen;
186
187         /**
188          * Return the status. This represents what repaints are
189          * pending after some operation (e.g. inserting a char).
190          */
191         bool needRefresh() const;
192
193 private:
194         // do we need a refresh?
195         bool need_refresh_;
196
197 public:
198         /// only the top-level LyXText has this non-zero
199         BufferView * bv_owner;
200
201 private:
202         /// returns a pointer to a specified row.
203         RowList::iterator
204         getRow(ParagraphList::iterator pit, lyx::pos_type pos) const;
205 public:
206         /// returns a pointer cursor row
207         RowList::iterator getRow(LyXCursor const & cursor) const;
208         /// convenience
209         RowList::iterator cursorRow() const;
210         /**
211          * Return the next row, when cursor is at the end of the
212          * previous row, for insets that take a full row.
213          *
214          * FIXME: explain why we need this ? especially for y...
215          */
216         RowList::iterator cursorIRow() const;
217
218         /** returns a pointer to the row near the specified y-coordinate
219           (relative to the whole text). y is set to the real beginning
220           of this row
221           */
222         RowList::iterator getRowNearY(int & y) const;
223
224         /** returns the column near the specified x-coordinate of the row
225          x is set to the real beginning of this column
226          */
227         lyx::pos_type getColumnNearX(RowList::iterator rit,
228                                      int & x, bool & boundary) const;
229
230         /** returns a pointer to a specified row. y is set to the beginning
231          of the row
232          */
233         RowList::iterator
234         getRow(ParagraphList::iterator pit, lyx::pos_type pos, int & y) const;
235
236         RowList & rows() const {
237                 return rowlist_;
238         }
239
240
241         /// need the selection cursor:
242         void setSelection();
243         ///
244         void clearSelection();
245
246         /// select the word we need depending on word_location
247         void getWord(LyXCursor & from, LyXCursor & to, lyx::word_location const);
248         /// just selects the word the cursor is in
249         void selectWord(lyx::word_location loc);
250         /// returns the inset at cursor (if it exists), 0 otherwise
251         Inset * getInset() const;
252
253         /// accept selected change
254         void acceptChange();
255
256         /// reject selected change
257         void rejectChange();
258
259         /** 'selects" the next word, where the cursor is not in
260          and returns this word as string. THe cursor will be moved
261          to the beginning of this word.
262          With SelectSelectedWord can this be highlighted really
263          */
264         WordLangTuple const selectNextWordToSpellcheck(float & value);
265         ///
266         void selectSelectedWord();
267         /// returns true if par was empty and was removed
268         bool setCursor(ParagraphList::iterator pit,
269                        lyx::pos_type pos,
270                        bool setfont = true,
271                        bool boundary = false);
272         ///
273         void setCursor(LyXCursor &, ParagraphList::iterator pit,
274                        lyx::pos_type pos,
275                        bool boundary = false);
276         ///
277         void setCursorIntern(ParagraphList::iterator pit,
278                              lyx::pos_type pos,
279                              bool setfont = true,
280                              bool boundary = false);
281         ///
282         void setCurrentFont();
283
284         ///
285         bool isBoundary(Buffer const *, Paragraph const & par,
286                         lyx::pos_type pos) const;
287         ///
288         bool isBoundary(Buffer const *, Paragraph const & par,
289                          lyx::pos_type pos,
290                          LyXFont const & font) const;
291
292         ///
293         void setCursorFromCoordinates(int x, int y);
294         ///
295         void setCursorFromCoordinates(LyXCursor &,
296                                       int x, int y);
297         ///
298         void cursorUp(bool selecting = false);
299         ///
300         void cursorDown(bool selecting = false);
301         ///
302         void cursorLeft(bool internal = true);
303         ///
304         void cursorRight(bool internal = true);
305         ///
306         void cursorLeftOneWord();
307         ///
308         void cursorRightOneWord();
309         ///
310         void cursorUpParagraph();
311         ///
312         void cursorDownParagraph();
313         ///
314         void cursorHome();
315         ///
316         void cursorEnd();
317         ///
318         void cursorPrevious();
319         ///
320         void cursorNext();
321         ///
322         void cursorTop();
323         ///
324         void cursorBottom();
325         ///
326         void Delete();
327         ///
328         void backspace();
329         ///
330         bool selectWordWhenUnderCursor(lyx::word_location);
331         ///
332         enum TextCase {
333                 ///
334                 text_lowercase = 0,
335                 ///
336                 text_capitalization = 1,
337                 ///
338                 text_uppercase = 2
339         };
340         /// Change the case of the word at cursor position.
341         void changeCase(TextCase action);
342
343         ///
344         void toggleInset();
345         ///
346         void cutSelection(bool doclear = true, bool realcut = true);
347         ///
348         void copySelection();
349         ///
350         void pasteSelection(size_t sel_index = 0);
351
352         /** the DTP switches for paragraphs. LyX will store the top settings
353          always in the first physical paragraph, the bottom settings in the
354          last. When a paragraph is broken, the top settings rest, the bottom
355          settings are given to the new one. So I can make shure, they do not
356          duplicate themself (and you cannnot make dirty things with them! )
357          */
358         void setParagraph(bool line_top, bool line_bottom,
359                           bool pagebreak_top, bool pagebreak_bottom,
360                           VSpace const & space_top,
361                           VSpace const & space_bottom,
362                           Spacing const & spacing,
363                           LyXAlignment align,
364                           string const & labelwidthstring,
365                           bool noindent);
366
367         /* these things are for search and replace */
368
369         /**
370          * Sets the selection from the current cursor position to length
371          * characters to the right. No safety checks.
372          */
373         void setSelectionRange(lyx::pos_type length);
374
375         /** simple replacing. The font of the first selected character
376           is used
377           */
378         void replaceSelectionWithString(string const & str);
379
380         /// needed to insert the selection
381         void insertStringAsLines(string const & str);
382         /// needed to insert the selection
383         void insertStringAsParagraphs(string const & str);
384
385         /// Find next inset of some specified type.
386         bool gotoNextInset(std::vector<Inset::Code> const & codes,
387                            string const & contents = string());
388         ///
389         void gotoInset(std::vector<Inset::Code> const & codes,
390                        bool same_content);
391         ///
392         void gotoInset(Inset::Code code, bool same_content);
393         ///
394
395         /* for the greater insets */
396
397         /// returns false if inset wasn't found
398         bool updateInset(Inset *);
399         ///
400         void checkParagraph(ParagraphList::iterator pit, lyx::pos_type pos);
401         ///
402         int workWidth() const;
403         /// returns width of row containing inset
404         int workWidth(Inset const * inset) const;
405
406         ///
407         void computeBidiTables(Buffer const *, RowList::iterator row) const;
408         /// Maps positions in the visual string to positions in logical string.
409         lyx::pos_type log2vis(lyx::pos_type pos) const;
410         /// Maps positions in the logical string to positions in visual string.
411         lyx::pos_type vis2log(lyx::pos_type pos) const;
412         ///
413         lyx::pos_type bidi_level(lyx::pos_type pos) const;
414         ///
415         bool bidi_InRange(lyx::pos_type pos) const;
416 private:
417         ///
418         mutable RowList rowlist_;
419
420         ///
421         float getCursorX(RowList::iterator rit, lyx::pos_type pos,
422                          lyx::pos_type last, bool boundary) const;
423         /// used in setlayout
424         void makeFontEntriesLayoutSpecific(Buffer const &, Paragraph & par);
425
426         /** forces the redrawing of a paragraph. Needed when manipulating a
427             right address box
428             */
429         void redoDrawingOfParagraph(LyXCursor const & cursor);
430
431         /// removes the row and reset the touched counters
432         void removeRow(RowList::iterator rit);
433
434         /// remove all following rows of the paragraph of the specified row.
435         void removeParagraph(RowList::iterator rit);
436
437         /// insert the specified paragraph behind the specified row
438         void insertParagraph(ParagraphList::iterator pit,
439                              RowList::iterator rowit);
440
441         /** appends  the implizit specified paragraph behind the specified row,
442          * start at the implizit given position */
443         void appendParagraph(RowList::iterator rowit);
444
445         ///
446         void breakAgain(RowList::iterator rit);
447         /// Calculate and set the height of the row
448         void setHeightOfRow(RowList::iterator rit);
449
450         // fix the cursor `cur' after a characters has been deleted at `where'
451         // position. Called by deleteEmptyParagraphMechanism
452         void fixCursorAfterDelete(LyXCursor & cur,
453                                   LyXCursor const & where);
454
455         /// delete double space (false) or empty paragraphs (true) around old_cursor
456         bool deleteEmptyParagraphMechanism(LyXCursor const & old_cursor);
457
458 public:
459         /** Updates all counters starting BEHIND the row. Changed paragraphs
460          * with a dynamic left margin will be rebroken. */
461         void updateCounters();
462         ///
463         void update();
464         /**
465          * Returns an inset if inset was hit, or 0 if not.
466          * If hit, the coordinates are changed relative to the inset.
467          */
468         Inset * checkInsetHit(int & x, int & y);
469
470         ///
471         int singleWidth(ParagraphList::iterator pit,
472                 lyx::pos_type pos) const;
473         ///
474         int singleWidth(ParagraphList::iterator pit,
475                 lyx::pos_type pos, char c) const;
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