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