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