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