]> git.lyx.org Git - lyx.git/blob - src/lyxtext.h
fix some C++ parsing bugs
[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         /** returns the firstrow, this could be done with the above too but
249             IMO it's stupid to have to allocate a dummy y all the time I need
250             the first row
251         */
252         Row * firstRow() const { return &*rowlist_.begin(); }
253         Row * lastRow() const { return &const_cast<LyXText*>(this)->rowlist_.back(); }
254         /** The cursor.
255           Later this variable has to be removed. There should be now internal
256           cursor in a text (and thus not in a buffer). By keeping this it is
257           (I think) impossible to have several views with the same buffer, but
258           the cursor placed at different places.
259           [later]
260           Since the LyXText now has been moved from Buffer to BufferView
261           it should not be absolutely needed to move the cursor...
262           */
263         LyXCursor cursor; // actual cursor position
264
265         /** The structure that keeps track of the selections set. */
266         struct Selection {
267                 Selection()
268                         : set_(false), mark_(false)
269                         {}
270                 bool set() const {
271                         return set_;
272                 }
273                 void set(bool s) {
274                         set_ = s;
275                 }
276                 bool mark() const {
277                         return mark_;
278                 }
279                 void mark(bool m) {
280                         mark_ = m;
281                 }
282                 LyXCursor cursor; // temporary cursor to hold a cursor position
283                                   // until setSelection is called!
284                 LyXCursor start;  // start of a REAL selection
285                 LyXCursor end;    // end of a REAL selection
286         private:
287                 bool set_; // former selection
288                 bool mark_; // former mark_set
289
290         };
291         Selection selection;
292         // this is used to handle XSelection events in the right manner
293         Selection xsel_cache;
294
295         /// needed for the toggling (cursor position on last selection made)
296         LyXCursor last_sel_cursor;
297         /// needed for toggling the selection in screen.C
298         LyXCursor toggle_cursor;
299         /// needed for toggling the selection in screen.C
300         LyXCursor toggle_end_cursor;
301
302         /// need the selection cursor:
303         void setSelection();
304         ///
305         void clearSelection();
306         ///
307         string const selectionAsString(Buffer const *, bool label) const;
308
309         /// select the word we need depending on word_location
310         void getWord(LyXCursor & from, LyXCursor & to,
311                      word_location const);
312         /// just selects the word the cursor is in
313         void selectWord(word_location const);
314         /// returns the inset at cursor (if it exists), 0 otherwise
315         Inset * getInset() const;
316
317         /// accept selected change
318         void acceptChange();
319
320         /// reject selected change
321         void rejectChange();
322
323         /** 'selects" the next word, where the cursor is not in
324          and returns this word as string. THe cursor will be moved
325          to the beginning of this word.
326          With SelectSelectedWord can this be highlighted really
327          */
328         WordLangTuple const selectNextWordToSpellcheck(float & value);
329         ///
330         void selectSelectedWord();
331         /// returns true if par was empty and was removed
332         bool setCursor(Paragraph * par,
333                        lyx::pos_type pos,
334                        bool setfont = true,
335                        bool boundary = false);
336         ///
337         void setCursor(LyXCursor &, Paragraph * par,
338                        lyx::pos_type pos,
339                        bool boundary = false);
340         ///
341         void setCursorIntern(Paragraph * par,
342                              lyx::pos_type pos,
343                              bool setfont = true,
344                              bool boundary = false);
345         ///
346         void setCurrentFont();
347
348         ///
349         bool isBoundary(Buffer const *, Paragraph * par,
350                         lyx::pos_type pos) const;
351         ///
352         bool isBoundary(Buffer const *, Paragraph * par,
353                          lyx::pos_type pos,
354                          LyXFont const & font) const;
355
356         ///
357         void setCursorFromCoordinates(int x, int y);
358         ///
359         void setCursorFromCoordinates(LyXCursor &,
360                                       int x, int y);
361         ///
362         void cursorUp(bool selecting = false);
363         ///
364         void cursorDown(bool selecting = false);
365         ///
366         void cursorLeft(bool internal = true);
367         ///
368         void cursorRight(bool internal = true);
369         ///
370         void cursorLeftOneWord();
371         ///
372         void cursorRightOneWord();
373         ///
374         void cursorUpParagraph();
375         ///
376         void cursorDownParagraph();
377         ///
378         void cursorHome();
379         ///
380         void cursorEnd();
381         ///
382         void cursorPrevious();
383         ///
384         void cursorNext();
385         ///
386         void cursorTab();
387         ///
388         void cursorTop();
389         ///
390         void cursorBottom();
391         ///
392         void Delete();
393         ///
394         void backspace();
395         ///
396         bool selectWordWhenUnderCursor(word_location);
397         ///
398         enum TextCase {
399                 ///
400                 text_lowercase = 0,
401                 ///
402                 text_capitalization = 1,
403                 ///
404                 text_uppercase = 2
405         };
406         /// Change the case of the word at cursor position.
407         void changeCase(TextCase action);
408         ///
409         void transposeChars();
410
411         ///
412         void toggleInset();
413         ///
414         void cutSelection(bool doclear = true, bool realcut = true);
415         ///
416         void copySelection();
417         ///
418         void pasteSelection();
419         ///
420         void copyEnvironmentType();
421         ///
422         void pasteEnvironmentType();
423
424         /** the DTP switches for paragraphs. LyX will store the top settings
425          always in the first physical paragraph, the bottom settings in the
426          last. When a paragraph is broken, the top settings rest, the bottom
427          settings are given to the new one. So I can make shure, they do not
428          duplicate themself (and you cannnot make dirty things with them! )
429          */
430         void setParagraph(bool line_top, bool line_bottom,
431                           bool pagebreak_top, bool pagebreak_bottom,
432                           VSpace const & space_top,
433                           VSpace const & space_bottom,
434                           Spacing const & spacing,
435                           LyXAlignment align,
436                           string const & labelwidthstring,
437                           bool noindent);
438
439         /* these things are for search and replace */
440
441         /**
442          * Sets the selection from the current cursor position to length
443          * characters to the right. No safety checks.
444          */
445         void setSelectionRange(lyx::pos_type length);
446
447         /** simple replacing. The font of the first selected character
448           is used
449           */
450         void replaceSelectionWithString(string const & str);
451
452         /// needed to insert the selection
453         void insertStringAsLines(string const & str);
454         /// needed to insert the selection
455         void insertStringAsParagraphs(string const & str);
456
457         /// Find next inset of some specified type.
458         bool gotoNextInset(std::vector<Inset::Code> const & codes,
459                            string const & contents = string());
460         ///
461         void gotoInset(std::vector<Inset::Code> const & codes,
462                        bool same_content);
463         ///
464         void gotoInset(Inset::Code code, bool same_content);
465         ///
466
467         /* for the greater insets */
468
469         /// returns false if inset wasn't found
470         bool updateInset(Inset *);
471         ///
472         void checkParagraph(Paragraph * par, lyx::pos_type pos);
473         ///
474         int workWidth() const;
475         ///
476         int workWidth(Inset * inset) const;
477
478         ///
479         void computeBidiTables(Buffer const *, Row * row) const;
480         /// Maps positions in the visual string to positions in logical string.
481         lyx::pos_type log2vis(lyx::pos_type pos) const;
482         /// Maps positions in the logical string to positions in visual string.
483         lyx::pos_type vis2log(lyx::pos_type pos) const;
484         ///
485         lyx::pos_type bidi_level(lyx::pos_type pos) const;
486         ///
487         bool bidi_InRange(lyx::pos_type pos) const;
488 private:
489         ///
490         RowList rowlist_;
491         ///
492         void cursorLeftOneWord(LyXCursor &);
493
494         ///
495         float getCursorX(Row *, lyx::pos_type pos,
496                          lyx::pos_type last, bool boundary) const;
497         /// used in setlayout
498         void makeFontEntriesLayoutSpecific(Buffer const &, Paragraph & par);
499
500         /** forces the redrawing of a paragraph. Needed when manipulating a
501             right address box
502             */
503         void redoDrawingOfParagraph(LyXCursor const & cursor);
504
505         /** Copybuffer for copy environment type.
506           Asger has learned that this should be a buffer-property instead
507           Lgb has learned that 'char' is a lousy type for non-characters
508           */
509         string copylayouttype;
510
511         /** inserts a new row behind the specified row, increments
512             the touched counters */
513         void insertRow(Row * row, Paragraph * par, lyx::pos_type pos);
514
515         /// removes the row and reset the touched counters
516         void removeRow(Row * row);
517
518         /// remove all following rows of the paragraph of the specified row.
519         void removeParagraph(Row * row);
520
521         /// insert the specified paragraph behind the specified row
522         void insertParagraph(Paragraph * par, Row * row);
523
524         /** appends  the implizit specified paragraph behind the specified row,
525          * start at the implizit given position */
526         void appendParagraph(Row * row);
527
528         ///
529         void breakAgain(Row * row);
530         /// Calculate and set the height of the row
531         void setHeightOfRow(Row * row_ptr);
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(Paragraph * par,
555                 lyx::pos_type pos) const;
556         ///
557         int singleWidth(Paragraph * par,
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 * 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(Row * 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 *, Paragraph * par);
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(Row & 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         Paragraph * ownerParagraph() const;
636         //
637         void ownerParagraph(Paragraph *) const;
638         // set it searching first for the right owner using the paragraph id
639         void ownerParagraph(int id, Paragraph *) const;
640
641         /// return true if this is owned by an inset.
642         bool isInInset() const;
643 };
644
645 /// return the default height of a row in pixels, considering font zoom
646 extern int defaultRowHeight();
647
648 #endif // LYXTEXT_H