]> git.lyx.org Git - lyx.git/blob - src/lyxtext.h
another fix
[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
25 class Buffer;
26 class BufferParams;
27 class BufferView;
28 class InsetText;
29 class Paragraph;
30 class Row;
31 class Spacing;
32 class UpdatableInset;
33 class VSpace;
34 class WordLangTuple;
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         /// sets inset as owner
71         LyXText(InsetText *);
72
73         /// Destructor
74         ~LyXText();
75
76         void init(BufferView *, bool reinit = false);
77         ///
78         mutable int height;
79         ///
80         mutable unsigned int width;
81         /// the current font settings
82         mutable LyXFont current_font;
83         /// the current font
84         mutable LyXFont real_current_font;
85         /// first visible pixel-row is set from LyXScreen!!!
86         // unsigned is wrong here for text-insets!
87         int first_y;
88         ///
89         BufferView * bv_owner;
90         ///
91         InsetText * inset_owner;
92         ///
93         UpdatableInset * the_locking_inset;
94
95         ///
96         int getRealCursorX(BufferView *) const;
97         ///
98         LyXFont const getFont(Buffer const *, Paragraph * par,
99                 lyx::pos_type pos) const;
100         ///
101         LyXFont const getLayoutFont(Buffer const *, Paragraph * par) const;
102         ///
103         LyXFont const getLabelFont(Buffer const *, Paragraph * par) const;
104         ///
105         void setCharFont(Buffer const *, Paragraph * par,
106                          lyx::pos_type pos, LyXFont const & font);
107         void setCharFont(BufferView *, Paragraph * par,
108                          lyx::pos_type pos, LyXFont const & font, bool toggleall);
109
110         /// what you expect when pressing <enter> at cursor position
111         void breakParagraph(BufferView *, char keep_layout = 0);
112
113         /** set layout over selection and make a total rebreak of
114           those paragraphs
115           */
116         Paragraph * setLayout(BufferView *, LyXCursor & actual_cursor,
117                               LyXCursor & selection_start,
118                               LyXCursor & selection_end,
119                               string const & layout);
120         ///
121         void setLayout(BufferView *, string const & layout);
122
123         /** increment depth over selection and make a total rebreak of those
124           paragraphs
125           */
126         void incDepth(BufferView *);
127
128         /** decrement depth over selection and make a total rebreak of those
129           paragraphs */
130         void decDepth(BufferView *);
131
132         /// get the depth at current cursor position
133         int getDepth() const;
134
135         /** set font over selection and make a total rebreak of those
136           paragraphs.
137           toggleall defaults to false.
138           */
139         void setFont(BufferView *, LyXFont const &, bool toggleall = false);
140
141         /** deletes and inserts again all paragaphs between the cursor
142           and the specified par. The Cursor is needed to set the refreshing
143           parameters.
144           This function is needed after SetLayout and SetFont etc.
145           */
146         void redoParagraphs(BufferView *, LyXCursor const & cursor,
147                             Paragraph const * end_par) const;
148         ///
149         void redoParagraph(BufferView *) const;
150
151         ///
152         void toggleFree(BufferView *, LyXFont const &, bool toggleall = false);
153
154         ///
155         string getStringToIndex(BufferView *);
156
157         /** recalculates the heights of all previous rows of the
158             specified paragraph.  needed, if the last characters font
159             has changed.
160             */
161         void redoHeightOfParagraph(BufferView *, LyXCursor const & cursor);
162
163         /** insert a character, moves all the following breaks in the
164           same Paragraph one to the right and make a little rebreak
165           */
166         void insertChar(BufferView *, char c);
167         ///
168         void insertInset(BufferView *, Inset * inset);
169
170         /// Completes the insertion with a full rebreak
171         void fullRebreak(BufferView *);
172
173         ///
174         mutable Row * need_break_row;
175         ///
176         mutable int refresh_y;
177         ///
178         int refresh_width;
179         ///
180         int refresh_x;
181         ///
182         mutable Row * refresh_row;
183         ///
184         lyx::pos_type refresh_pos;
185
186         /// give and set the LyXText status
187         text_status status() const;
188         ///
189         void status(BufferView *, text_status) const;
190
191         ///
192         Inset::RESULT dispatch(FuncRequest const & cmd);
193
194 private:
195         /** wether the screen needs a refresh,
196            starting with refresh_y
197            */
198         mutable text_status status_;
199
200 public:
201         /** returns a pointer to the row near the specified y-coordinate
202           (relative to the whole text). y is set to the real beginning
203           of this row
204           */
205         Row * getRowNearY(int & y) const;
206
207         /** returns the column near the specified x-coordinate of the row
208          x is set to the real beginning of this column
209          */
210         lyx::pos_type getColumnNearX(BufferView *, Row * row,
211                                             int & x, bool & boundary) const;
212
213         /** returns a pointer to a specified row. y is set to the beginning
214          of the row
215          */
216         Row * getRow(Paragraph * par, lyx::pos_type pos, int & y) const;
217         /** returns the firstrow, this could be done with the above too but
218             IMO it's stupid to have to allocate a dummy y all the time I need
219             the first row
220         */
221         Row * firstRow() { return firstrow; }
222
223         /** returns the height of a default row, needed  for scrollbar
224          */
225         int defaultHeight() const;
226
227         /** The cursor.
228           Later this variable has to be removed. There should be now internal
229           cursor in a text (and thus not in a buffer). By keeping this it is
230           (I think) impossible to have several views with the same buffer, but
231           the cursor placed at different places.
232           [later]
233           Since the LyXText now has been moved from Buffer to BufferView
234           it should not be absolutely needed to move the cursor...
235           */
236         mutable LyXCursor cursor; // actual cursor position
237
238         /** The structure that keeps track of the selections set. */
239         struct Selection {
240                 Selection()
241                         : set_(false), mark_(false)
242                         {}
243                 bool set() const {
244                         return set_;
245                 }
246                 void set(bool s) {
247                         set_ = s;
248                 }
249                 bool mark() const {
250                         return mark_;
251                 }
252                 void mark(bool m) {
253                         mark_ = m;
254                 }
255                 LyXCursor cursor; // temporary cursor to hold a cursor position
256                                   // until setSelection is called!
257                 LyXCursor start;  // start of a REAL selection
258                 LyXCursor end;    // end of a REAL selection
259         private:
260                 bool set_; // former selection
261                 bool mark_; // former mark_set
262
263         };
264         mutable Selection selection;
265         // this is used to handle XSelection events in the right manner
266         mutable Selection xsel_cache;
267
268         /// needed for the toggling (cursor position on last selection made)
269         mutable LyXCursor last_sel_cursor;
270         /// needed for toggling the selection in screen.C
271         mutable LyXCursor toggle_cursor;
272         /// needed for toggling the selection in screen.C
273         mutable LyXCursor toggle_end_cursor;
274
275         /// need the selection cursor:
276         void setSelection(BufferView *);
277         ///
278         void clearSelection() const;
279         ///
280         string const selectionAsString(Buffer const *, bool label) const;
281
282         /// select the word we need depending on word_location
283         void getWord(LyXCursor & from, LyXCursor & to,
284                      word_location const) const;
285         /// just selects the word the cursor is in
286         void selectWord(BufferView *, word_location const);
287         /// returns the inset at cursor (if it exists), 0 otherwise
288         Inset * getInset() const;
289
290         /** 'selects" the next word, where the cursor is not in
291          and returns this word as string. THe cursor will be moved
292          to the beginning of this word.
293          With SelectSelectedWord can this be highlighted really
294          */
295         WordLangTuple const selectNextWordToSpellcheck(BufferView *, float & value) const;
296         ///
297         void selectSelectedWord(BufferView *);
298         /// returns true if par was empty and was removed
299         bool setCursor(BufferView *, Paragraph * par,
300                        lyx::pos_type pos,
301                        bool setfont = true,
302                        bool boundary = false) const;
303         ///
304         void setCursor(BufferView *, LyXCursor &, Paragraph * par,
305                        lyx::pos_type pos,
306                        bool boundary = false) const;
307         ///
308         void setCursorIntern(BufferView *, Paragraph * par,
309                              lyx::pos_type pos,
310                              bool setfont = true,
311                              bool boundary = false) const;
312         ///
313         void setCurrentFont(BufferView *) const;
314
315         ///
316         bool isBoundary(Buffer const *, Paragraph * par,
317                         lyx::pos_type pos) const;
318         ///
319         bool isBoundary(Buffer const *, Paragraph * par,
320                          lyx::pos_type pos,
321                          LyXFont const & font) const;
322
323         ///
324         void setCursorFromCoordinates(BufferView *, int x, int y) const;
325         ///
326         void setCursorFromCoordinates(BufferView *, LyXCursor &,
327                                       int x, int y) const;
328         ///
329         void cursorUp(BufferView *, bool selecting = false) const;
330         ///
331         void cursorDown(BufferView *, bool selecting = false) const;
332         ///
333         void cursorLeft(BufferView *, bool internal = true) const;
334         ///
335         void cursorRight(BufferView *, bool internal = true) const;
336         ///
337         void cursorLeftOneWord(BufferView *) const;
338         ///
339         void cursorRightOneWord(BufferView *) const;
340         ///
341         void cursorUpParagraph(BufferView *) const;
342         ///
343         void cursorDownParagraph(BufferView *) const;
344         ///
345         void cursorHome(BufferView *) const;
346         ///
347         void cursorEnd(BufferView *) const;
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         /** sets the selection over the number of characters of string,
414           no check!!
415           */
416         void setSelectionOverString(BufferView *, string const & str);
417
418         /** simple replacing. The font of the first selected character
419           is used
420           */
421         void replaceSelectionWithString(BufferView *, string const & str);
422
423         /// needed to insert the selection
424         void insertStringAsLines(BufferView *, string const & str);
425         /// needed to insert the selection
426         void insertStringAsParagraphs(BufferView *, string const & str);
427
428         /// Find next inset of some specified type.
429         bool gotoNextInset(BufferView *, std::vector<Inset::Code> const & codes,
430                            string const & contents = string()) const;
431         ///
432         void gotoInset(BufferView * bv, std::vector<Inset::Code> const & codes,
433                                                 bool same_content);
434         ///
435         void gotoInset(BufferView * bv, Inset::Code code, bool same_content);
436         ///
437
438         /* for the greater insets */
439
440         /// returns false if inset wasn't found
441         bool updateInset(BufferView *, Inset *);
442         ///
443         void checkParagraph(BufferView *, Paragraph * par, lyx::pos_type pos);
444         ///
445         int workWidth(BufferView *) const;
446         ///
447         int workWidth(BufferView *, Inset * inset) const;
448         ///
449         void computeBidiTables(Buffer const *, Row * row) const;
450
451         /// Maps positions in the visual string to positions in logical string.
452         inline
453         lyx::pos_type log2vis(lyx::pos_type pos) const {
454                 if (bidi_start == -1)
455                         return pos;
456                 else
457                         return log2vis_list[pos-bidi_start];
458         }
459
460         /// Maps positions in the logical string to positions in visual string.
461         inline
462         lyx::pos_type vis2log(lyx::pos_type pos) const {
463                 if (bidi_start == -1)
464                         return pos;
465                 else
466                         return vis2log_list[pos-bidi_start];
467         }
468         ///
469         inline
470         lyx::pos_type bidi_level(lyx::pos_type pos) const {
471                 if (bidi_start == -1)
472                         return 0;
473                 else
474                         return bidi_levels[pos-bidi_start];
475         }
476         ///
477         inline
478         bool bidi_InRange(lyx::pos_type pos) const {
479                 return bidi_start == -1 ||
480                         (bidi_start <= pos && pos <= bidi_end);
481         }
482 private:
483         ///
484         mutable Row * firstrow;
485         ///
486         mutable Row * lastrow;
487
488         ///
489         void cursorLeftOneWord(LyXCursor &) const;
490         ///
491         void cursorPrevious(BufferView * bv);
492         ///
493         void cursorNext(BufferView * bv);
494  
495         ///
496         float getCursorX(BufferView *, Row *, lyx::pos_type pos,
497                                          lyx::pos_type last, bool boundary) const;
498         ///
499         void changeRegionCase(BufferView * bv,
500                                        LyXCursor const & from,
501                                        LyXCursor const & to,
502                                        LyXText::TextCase action);
503         /// used in setlayout
504         void makeFontEntriesLayoutSpecific(Buffer const *, Paragraph * par);
505
506         /** forces the redrawing of a paragraph. Needed when manipulating a
507             right address box
508             */
509         void redoDrawingOfParagraph(BufferView *, LyXCursor const & cursor);
510
511         /** Copybuffer for copy environment type.
512           Asger has learned that this should be a buffer-property instead
513           Lgb has learned that 'char' is a lousy type for non-characters
514           */
515         string copylayouttype;
516
517         /** inserts a new row behind the specified row, increments
518             the touched counters */
519         void insertRow(Row * row, Paragraph * par, lyx::pos_type pos) const;
520         /// removes the row and reset the touched counters 
521         void removeRow(Row * row) const;
522
523         /// remove all following rows of the paragraph of the specified row.
524         void removeParagraph(Row * row) const;
525
526         /// insert the specified paragraph behind the specified row 
527         void insertParagraph(BufferView *,
528                              Paragraph * par, Row * row) const;
529
530         /** appends  the implizit specified paragraph behind the specified row,
531          * start at the implizit given position */
532         void appendParagraph(BufferView *, Row * row) const;
533
534         ///
535         void breakAgain(BufferView *, Row * row) const;
536         ///
537         void breakAgainOneRow(BufferView *, Row * row);
538         /// Calculate and set the height of the row
539         void setHeightOfRow(BufferView *, Row * row_ptr) const;
540
541         /** this calculates the specified parameters. needed when setting
542          * the cursor and when creating a visible row */
543         void prepareToPrint(BufferView *, Row * row, float & x,
544                             float & fill_separator,
545                             float & fill_hfill,
546                             float & fill_label_hfill,
547                             bool bidi = true) const;
548
549         /// A struct used for drawing routines
550         struct DrawRowParams {
551                 // the bufferview
552                 BufferView * bv;
553                 // the row
554                 Row * row;
555                 // the painter to use
556                 Painter * pain;
557                 // has the background been cleared
558                 bool cleared;
559                 /// x offset (e.g. for insets)
560                 int xo;
561                 /// y offset (e.g. for insets)
562                 int yo;
563                 /// FIXME
564                 float x;
565                 /// FIXME
566                 int y;
567                 /// the inset/view full width
568                 int width;
569                 /// hfill size
570                 float hfill;
571                 /// label hfill size
572                 float label_hfill;
573                 /// fill separator size
574                 float separator;
575         };
576
577         /// paint the background
578         bool paintRowBackground(DrawRowParams & p);
579
580         /// paint the selection background
581         void paintRowSelection(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