]> git.lyx.org Git - lyx.git/blob - src/lyxtext.h
'full redraw' related stuff that's innocent for the upward-selection-in-tables bug.
[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
167         ///
168         RowList::iterator need_break_row;
169
170         /// clear any pending paints
171         void clearPaint();
172
173         /// submit repaint request
174         void postPaint();
175
176         ///
177         Inset::RESULT dispatch(FuncRequest const & cmd);
178
179         BufferView * bv();
180
181         BufferView * bv() const;
182
183         friend class LyXScreen;
184
185         /**
186          * Return the status. This represents what repaints are
187          * pending after some operation (e.g. inserting a char).
188          */
189         bool needRefresh() const;
190
191 private:
192         // do we need a refresh?
193         bool need_refresh_;
194
195 public:
196         /// only the top-level LyXText has this non-zero
197         BufferView * bv_owner;
198
199 private:
200         /// returns a pointer to a specified row.
201         RowList::iterator
202         getRow(ParagraphList::iterator pit, lyx::pos_type pos) const;
203 public:
204         /// returns a pointer cursor row
205         RowList::iterator getRow(LyXCursor const & cursor) const;
206         /// convenience
207         RowList::iterator cursorRow() const;
208
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         RowList::iterator 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(RowList::iterator rit,
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         RowList::iterator
225         getRow(ParagraphList::iterator pit, lyx::pos_type pos, int & y) const;
226
227         RowList & rows() const {
228                 return rowlist_;
229         }
230
231
232         /// need the selection cursor:
233         void setSelection();
234         ///
235         void clearSelection();
236
237         /// select the word we need depending on word_location
238         void getWord(LyXCursor & from, LyXCursor & to, lyx::word_location const);
239         /// just selects the word the cursor is in
240         void selectWord(lyx::word_location loc);
241         /// returns the inset at cursor (if it exists), 0 otherwise
242         Inset * getInset() const;
243
244         /// accept selected change
245         void acceptChange();
246
247         /// reject selected change
248         void rejectChange();
249
250         /** 'selects" the next word, where the cursor is not in
251          and returns this word as string. THe cursor will be moved
252          to the beginning of this word.
253          With SelectSelectedWord can this be highlighted really
254          */
255         WordLangTuple const selectNextWordToSpellcheck(float & value);
256         ///
257         void selectSelectedWord();
258         /// returns true if par was empty and was removed
259         bool setCursor(ParagraphList::iterator pit,
260                        lyx::pos_type pos,
261                        bool setfont = true,
262                        bool boundary = false);
263         ///
264         void setCursor(LyXCursor &, ParagraphList::iterator pit,
265                        lyx::pos_type pos,
266                        bool boundary = false);
267         ///
268         void setCursorIntern(ParagraphList::iterator pit,
269                              lyx::pos_type pos,
270                              bool setfont = true,
271                              bool boundary = false);
272         ///
273         void setCurrentFont();
274
275         ///
276         bool isBoundary(Buffer const *, Paragraph const & par,
277                         lyx::pos_type pos) const;
278         ///
279         bool isBoundary(Buffer const *, Paragraph const & par,
280                          lyx::pos_type pos,
281                          LyXFont const & font) const;
282
283         ///
284         void setCursorFromCoordinates(int x, int y);
285         ///
286         void setCursorFromCoordinates(LyXCursor &,
287                                       int x, int y);
288         ///
289         void cursorUp(bool selecting = false);
290         ///
291         void cursorDown(bool selecting = false);
292         ///
293         void cursorLeft(bool internal = true);
294         ///
295         void cursorRight(bool internal = true);
296         ///
297         void cursorLeftOneWord();
298         ///
299         void cursorRightOneWord();
300         ///
301         void cursorUpParagraph();
302         ///
303         void cursorDownParagraph();
304         ///
305         void cursorHome();
306         ///
307         void cursorEnd();
308         ///
309         void cursorPrevious();
310         ///
311         void cursorNext();
312         ///
313         void cursorTop();
314         ///
315         void cursorBottom();
316         ///
317         void Delete();
318         ///
319         void backspace();
320         ///
321         bool selectWordWhenUnderCursor(lyx::word_location);
322         ///
323         enum TextCase {
324                 ///
325                 text_lowercase = 0,
326                 ///
327                 text_capitalization = 1,
328                 ///
329                 text_uppercase = 2
330         };
331         /// Change the case of the word at cursor position.
332         void changeCase(TextCase action);
333
334         ///
335         void toggleInset();
336         ///
337         void cutSelection(bool doclear = true, bool realcut = true);
338         ///
339         void copySelection();
340         ///
341         void pasteSelection(size_t sel_index = 0);
342
343         /** the DTP switches for paragraphs. LyX will store the top settings
344          always in the first physical paragraph, the bottom settings in the
345          last. When a paragraph is broken, the top settings rest, the bottom
346          settings are given to the new one. So I can make shure, they do not
347          duplicate themself (and you cannnot make dirty things with them! )
348          */
349         void setParagraph(bool line_top, bool line_bottom,
350                           bool pagebreak_top, bool pagebreak_bottom,
351                           VSpace const & space_top,
352                           VSpace const & space_bottom,
353                           Spacing const & spacing,
354                           LyXAlignment align,
355                           string const & labelwidthstring,
356                           bool noindent);
357
358         /* these things are for search and replace */
359
360         /**
361          * Sets the selection from the current cursor position to length
362          * characters to the right. No safety checks.
363          */
364         void setSelectionRange(lyx::pos_type length);
365
366         /** simple replacing. The font of the first selected character
367           is used
368           */
369         void replaceSelectionWithString(string const & str);
370
371         /// needed to insert the selection
372         void insertStringAsLines(string const & str);
373         /// needed to insert the selection
374         void insertStringAsParagraphs(string const & str);
375
376         /// Find next inset of some specified type.
377         bool gotoNextInset(std::vector<Inset::Code> const & codes,
378                            string const & contents = string());
379         ///
380         void gotoInset(std::vector<Inset::Code> const & codes,
381                        bool same_content);
382         ///
383         void gotoInset(Inset::Code code, bool same_content);
384         ///
385
386         /* for the greater insets */
387
388         /// returns false if inset wasn't found
389         bool updateInset(Inset *);
390         ///
391         void checkParagraph(ParagraphList::iterator pit, lyx::pos_type pos);
392         ///
393         int workWidth() const;
394         /// returns width of row containing inset
395         int workWidth(Inset const * inset) const;
396
397         ///
398         void computeBidiTables(Buffer const *, RowList::iterator row) const;
399         /// Maps positions in the visual string to positions in logical string.
400         lyx::pos_type log2vis(lyx::pos_type pos) const;
401         /// Maps positions in the logical string to positions in visual string.
402         lyx::pos_type vis2log(lyx::pos_type pos) const;
403         ///
404         lyx::pos_type bidi_level(lyx::pos_type pos) const;
405         ///
406         bool bidi_InRange(lyx::pos_type pos) const;
407 private:
408         ///
409         mutable RowList rowlist_;
410
411         ///
412         float getCursorX(RowList::iterator rit, lyx::pos_type pos,
413                          lyx::pos_type last, bool boundary) const;
414         /// used in setlayout
415         void makeFontEntriesLayoutSpecific(Buffer const &, Paragraph & par);
416
417         /** forces the redrawing of a paragraph. Needed when manipulating a
418             right address box
419             */
420         void redoDrawingOfParagraph(LyXCursor const & cursor);
421
422         /// removes the row and reset the touched counters
423         void removeRow(RowList::iterator rit);
424
425         /// remove all following rows of the paragraph of the specified row.
426         void removeParagraph(RowList::iterator rit);
427
428         /// insert the specified paragraph behind the specified row
429         void insertParagraph(ParagraphList::iterator pit,
430                              RowList::iterator rowit);
431
432         /** appends  the implizit specified paragraph behind the specified row,
433          * start at the implizit given position */
434         void appendParagraph(RowList::iterator rowit);
435
436         ///
437         void breakAgain(RowList::iterator rit);
438         /// Calculate and set the height of the row
439         void setHeightOfRow(RowList::iterator rit);
440
441         // fix the cursor `cur' after a characters has been deleted at `where'
442         // position. Called by deleteEmptyParagraphMechanism
443         void fixCursorAfterDelete(LyXCursor & cur,
444                                   LyXCursor const & where);
445
446         /// delete double space (false) or empty paragraphs (true) around old_cursor
447         bool deleteEmptyParagraphMechanism(LyXCursor const & old_cursor);
448
449 public:
450         /** Updates all counters starting BEHIND the row. Changed paragraphs
451          * with a dynamic left margin will be rebroken. */
452         void updateCounters();
453         ///
454         void update();
455         /**
456          * Returns an inset if inset was hit, or 0 if not.
457          * If hit, the coordinates are changed relative to the inset.
458          */
459         Inset * checkInsetHit(int & x, int & y);
460
461         ///
462         int singleWidth(ParagraphList::iterator pit,
463                 lyx::pos_type pos) const;
464         ///
465         int singleWidth(ParagraphList::iterator pit,
466                 lyx::pos_type pos, char c) const;
467
468         /// return the color of the canvas
469         LColor::color backgroundColor() const;
470
471         ///
472         mutable bool bidi_same_direction;
473
474         unsigned char transformChar(unsigned char c, Paragraph const & par,
475                                     lyx::pos_type pos) const;
476
477         /**
478          * Returns the left beginning of the text.
479          * This information cannot be taken from the layout object, because
480          * in LaTeX the beginning of the text fits in some cases
481          * (for example sections) exactly the label-width.
482          */
483         int leftMargin(Row const & row) const;
484         ///
485         int rightMargin(Buffer const &, Row const & row) const;
486
487         /** this calculates the specified parameters. needed when setting
488          * the cursor and when creating a visible row */
489         void prepareToPrint(RowList::iterator row, float & x,
490                             float & fill_separator,
491                             float & fill_hfill,
492                             float & fill_label_hfill,
493                             bool bidi = true) const;
494
495 private:
496         ///
497         void setCounter(Buffer const *, ParagraphList::iterator pit);
498         ///
499         void deleteWordForward();
500         ///
501         void deleteWordBackward();
502         ///
503         void deleteLineForward();
504
505         /*
506          * some low level functions
507          */
508
509
510         /// return the pos value *before* which a row should break.
511         /// for example, the pos at which IsNewLine(pos) == true
512         lyx::pos_type rowBreakPoint(Row const & row) const;
513
514         /// returns the minimum space a row needs on the screen in pixel
515         int fill(RowList::iterator row, int workwidth) const;
516
517         /**
518          * returns the minimum space a manual label needs on the
519          * screen in pixels
520          */
521         int labelFill(Row const & row) const;
522
523         /// FIXME
524         int labelEnd(Row const & row) const;
525
526         ///
527         mutable std::vector<lyx::pos_type> log2vis_list;
528         ///
529         mutable std::vector<lyx::pos_type> vis2log_list;
530         ///
531         mutable std::vector<lyx::pos_type> bidi_levels;
532         ///
533         mutable lyx::pos_type bidi_start;
534         ///
535         mutable lyx::pos_type bidi_end;
536
537         ///
538         void charInserted();
539 public:
540         //
541         // special owner functions
542         ///
543         ParagraphList & ownerParagraphs() const;
544
545         /// return true if this is owned by an inset.
546         bool isInInset() const;
547
548 private:
549         /** Cursor related data.
550           Later this variable has to be removed. There should be now internal
551           cursor in a text */
552         ///
553         ///TextCursor cursor_;
554 };
555
556 /// return the default height of a row in pixels, considering font zoom
557 extern int defaultRowHeight();
558
559 #endif // LYXTEXT_H