]> git.lyx.org Git - lyx.git/blob - src/lyxtext.h
rowlist2
[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 #include "RowList.h"
21
22 class Buffer;
23 class BufferParams;
24 class BufferView;
25 class InsetText;
26 class Paragraph;
27 class Row;
28 class Spacing;
29 class UpdatableInset;
30 class VSpace;
31 class WordLangTuple;
32 class ParagraphList;
33
34
35 /**
36   This class holds the mapping between buffer paragraphs and screen rows.
37   */
38 class LyXText {
39 public:
40         /// what repainting is needed
41         enum refresh_status {
42                 /// no repaint is needed
43                 REFRESH_NONE = 0,
44                 /// the refresh_row needs repainting
45                 REFRESH_ROW = 1,
46                 /// everything from refresh_y downwards needs repainting
47                 REFRESH_AREA = 2
48         };
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(BufferView *, InsetText *);
69
70         void init(BufferView *, bool reinit = false);
71         ///
72         int height;
73         ///
74         unsigned int width;
75         /// the current font settings
76         LyXFont current_font;
77         /// the current font
78         LyXFont real_current_font;
79 private:
80         /** the 'anchor' row: the position of this row remains constant
81          *  with respect to the top of the screen
82          */
83         Row * anchor_row_;
84         /** the pixel offset with respect to this row of top_y
85          */
86         int anchor_row_offset_;
87 public:
88         /// get the y coord. of the top of the screen (relative to doc start)
89         int top_y() const;
90         /// set the y coord. of the top of the screen (relative to doc start)
91         void top_y(int newy);
92         /// set the anchoring row. top_y will be computed relative to this
93         void anchor_row(Row * row);
94         ///
95         InsetText * inset_owner;
96         ///
97         UpdatableInset * the_locking_inset;
98
99         ///
100         int getRealCursorX() const;
101         ///
102         LyXFont const getFont(Buffer const *, Paragraph * par,
103                 lyx::pos_type pos) const;
104         ///
105         LyXFont const getLayoutFont(Buffer const *, Paragraph * par) const;
106         ///
107         LyXFont const getLabelFont(Buffer const *, Paragraph * par) const;
108         ///
109         void setCharFont(Buffer const *, Paragraph * par,
110                          lyx::pos_type pos, LyXFont const & font);
111         void setCharFont(Paragraph * par,
112                          lyx::pos_type pos, LyXFont const & font, bool toggleall);
113
114         ///
115         void breakAgainOneRow(Row * row);
116         /// what you expect when pressing <enter> at cursor position
117         void breakParagraph(ParagraphList & paragraphs, char keep_layout = 0);
118
119         /** set layout over selection and make a total rebreak of
120           those paragraphs
121           */
122         Paragraph * setLayout(LyXCursor & actual_cursor,
123                               LyXCursor & selection_start,
124                               LyXCursor & selection_end,
125                               string const & layout);
126         ///
127         void setLayout(string const & layout);
128
129         /** increment depth over selection and make a total rebreak of those
130           paragraphs
131           */
132         void incDepth();
133
134         /** decrement depth over selection and make a total rebreak of those
135           paragraphs */
136         void decDepth();
137
138         /// get the depth at current cursor position
139         int getDepth() const;
140
141         /** set font over selection and make a total rebreak of those
142           paragraphs.
143           toggleall defaults to false.
144           */
145         void setFont(LyXFont const &, bool toggleall = false);
146
147         /** deletes and inserts again all paragaphs between the cursor
148           and the specified par. The Cursor is needed to set the refreshing
149           parameters.
150           This function is needed after SetLayout and SetFont etc.
151           */
152         void redoParagraphs(LyXCursor const & cursor,
153                             Paragraph const * end_par);
154         ///
155         void redoParagraph();
156
157         ///
158         void toggleFree(LyXFont const &, bool toggleall = false);
159
160         ///
161         string getStringToIndex();
162
163         /** recalculates the heights of all previous rows of the
164             specified paragraph.  needed, if the last characters font
165             has changed.
166             */
167         void redoHeightOfParagraph();
168
169         /** insert a character, moves all the following breaks in the
170           same Paragraph one to the right and make a little rebreak
171           */
172         void insertChar(char c);
173         ///
174         void insertInset(Inset * inset);
175
176         /// Completes the insertion with a full rebreak
177         void fullRebreak();
178
179         ///
180         Row * need_break_row;
181
182         /// clear any pending paints
183         void clearPaint();
184
185         /**
186          * Mark position y as the starting point for a repaint
187          */
188         void postPaint(int start_y);
189
190         /**
191          * Mark the given row at position y as needing a repaint.
192          */
193         void postRowPaint(Row * row, int start_y);
194
195         ///
196         Inset::RESULT dispatch(FuncRequest const & cmd);
197
198         BufferView * bv();
199
200         BufferView * bv() const;
201
202         friend class LyXScreen;
203
204         /**
205          * Return the status. This represents what repaints are
206          * pending after some operation (e.g. inserting a char).
207          */
208         refresh_status refreshStatus() const;
209
210 private:
211         /**
212          * The pixel y position from which to repaint the screen.
213          * The position is absolute along the height of outermost
214          * lyxtext (I think). REFRESH_AREA and REFRESH_ROW
215          * repaints both use this as a starting point (if it's within
216          * the viewable portion of the lyxtext).
217          */
218         int refresh_y;
219         /**
220          * The row from which to repaint the screen, used by screen.c.
221          * This must be set if the pending update is REFRESH_ROW.
222          * It doesn't make any difference for REFRESH_AREA.
223          */
224         Row * refresh_row;
225
226         refresh_status refresh_status_;
227
228         /// only the top-level LyXText has this non-zero
229         BufferView * bv_owner;
230
231 public:
232         /** returns a pointer to the row near the specified y-coordinate
233           (relative to the whole text). y is set to the real beginning
234           of this row
235           */
236         Row * getRowNearY(int & y) const;
237
238         /** returns the column near the specified x-coordinate of the row
239          x is set to the real beginning of this column
240          */
241         lyx::pos_type getColumnNearX(Row * row,
242                                             int & x, bool & boundary) const;
243
244         /** returns a pointer to a specified row. y is set to the beginning
245          of the row
246          */
247         Row * getRow(Paragraph * par, lyx::pos_type pos, int & y) const;
248
249         RowList & rows() {
250                 return rowlist_;
251         }
252         RowList const & rows() const {
253                 return rowlist_;
254         }
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         RowList rowlist_;
493         ///
494         void cursorLeftOneWord(LyXCursor &);
495
496         ///
497         float getCursorX(Row *, lyx::pos_type pos,
498                          lyx::pos_type last, bool boundary) const;
499         /// used in setlayout
500         void makeFontEntriesLayoutSpecific(Buffer const &, Paragraph & par);
501
502         /** forces the redrawing of a paragraph. Needed when manipulating a
503             right address box
504             */
505         void redoDrawingOfParagraph(LyXCursor const & cursor);
506
507         /** Copybuffer for copy environment type.
508           Asger has learned that this should be a buffer-property instead
509           Lgb has learned that 'char' is a lousy type for non-characters
510           */
511         string copylayouttype;
512
513         /// removes the row and reset the touched counters
514         void removeRow(Row * row);
515
516         /// remove all following rows of the paragraph of the specified row.
517         void removeParagraph(Row * row);
518
519         /// insert the specified paragraph behind the specified row
520         void insertParagraph(Paragraph * par, RowList::iterator rowit);
521
522         /** appends  the implizit specified paragraph behind the specified row,
523          * start at the implizit given position */
524         void appendParagraph(RowList::iterator rowit);
525
526         ///
527         void breakAgain(Row * row);
528         /// Calculate and set the height of the row
529         void setHeightOfRow(Row * row_ptr);
530
531         // fix the cursor `cur' after a characters has been deleted at `where'
532         // position. Called by deleteEmptyParagraphMechanism
533         void fixCursorAfterDelete(LyXCursor & cur,
534                                   LyXCursor const & where);
535
536         /// delete double space (false) or empty paragraphs (true) around old_cursor
537         bool deleteEmptyParagraphMechanism(LyXCursor const & old_cursor);
538
539 public:
540         /** Updates all counters starting BEHIND the row. Changed paragraphs
541          * with a dynamic left margin will be rebroken. */
542         void updateCounters();
543         ///
544         void update();
545         /**
546          * Returns an inset if inset was hit, or 0 if not.
547          * If hit, the coordinates are changed relative to the inset.
548          */
549         Inset * checkInsetHit(int & x, int & y);
550
551         ///
552         int singleWidth(Paragraph * par,
553                 lyx::pos_type pos) const;
554         ///
555         int singleWidth(Paragraph * par,
556                 lyx::pos_type pos, char c) const;
557
558         /// return the color of the canvas
559         LColor::color backgroundColor() const;
560
561         ///
562         mutable bool bidi_same_direction;
563
564         unsigned char transformChar(unsigned char c, Paragraph * par,
565                                     lyx::pos_type pos) const;
566
567         /**
568          * Returns the left beginning of the text.
569          * This information cannot be taken from the layout object, because
570          * in LaTeX the beginning of the text fits in some cases
571          * (for example sections) exactly the label-width.
572          */
573         int leftMargin(Row const * row) const;
574         ///
575         int rightMargin(Buffer const &, Row const & row) const;
576
577         /** this calculates the specified parameters. needed when setting
578          * the cursor and when creating a visible row */
579         void prepareToPrint(Row * row, float & x,
580                             float & fill_separator,
581                             float & fill_hfill,
582                             float & fill_label_hfill,
583                             bool bidi = true) const;
584
585 private:
586         ///
587         void setCounter(Buffer const *, Paragraph * par);
588         ///
589         void deleteWordForward();
590         ///
591         void deleteWordBackward();
592         ///
593         void deleteLineForward();
594
595         /*
596          * some low level functions
597          */
598
599
600         /// return the pos value *before* which a row should break.
601         /// for example, the pos at which IsNewLine(pos) == true
602         lyx::pos_type rowBreakPoint(Row const & row) const;
603
604         /// returns the minimum space a row needs on the screen in pixel
605         int fill(Row & row, int workwidth) const;
606
607         /**
608          * returns the minimum space a manual label needs on the
609          * screen in pixels
610          */
611         int labelFill(Row const & row) const;
612
613         /// FIXME
614         int labelEnd(Row const & row) const;
615
616         ///
617         mutable std::vector<lyx::pos_type> log2vis_list;
618         ///
619         mutable std::vector<lyx::pos_type> vis2log_list;
620         ///
621         mutable std::vector<lyx::pos_type> bidi_levels;
622         ///
623         mutable lyx::pos_type bidi_start;
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 owned by an inset.
640         bool isInInset() const;
641 };
642
643 /// return the default height of a row in pixels, considering font zoom
644 extern int defaultRowHeight();
645
646 #endif // LYXTEXT_H