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