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