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