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