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