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