]> git.lyx.org Git - lyx.git/blob - src/lyxtext.h
parlist-a-1.diff
[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 cursorTab();
387         ///
388         void cursorTop();
389         ///
390         void cursorBottom();
391         ///
392         void Delete();
393         ///
394         void backspace();
395         ///
396         bool selectWordWhenUnderCursor(word_location);
397         ///
398         enum TextCase {
399                 ///
400                 text_lowercase = 0,
401                 ///
402                 text_capitalization = 1,
403                 ///
404                 text_uppercase = 2
405         };
406         /// Change the case of the word at cursor position.
407         void changeCase(TextCase action);
408
409         ///
410         void toggleInset();
411         ///
412         void cutSelection(bool doclear = true, bool realcut = true);
413         ///
414         void copySelection();
415         ///
416         void pasteSelection();
417         ///
418         void copyEnvironmentType();
419         ///
420         void pasteEnvironmentType();
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(bool line_top, bool line_bottom,
429                           bool pagebreak_top, bool pagebreak_bottom,
430                           VSpace const & space_top,
431                           VSpace const & space_bottom,
432                           Spacing const & spacing,
433                           LyXAlignment align,
434                           string const & labelwidthstring,
435                           bool noindent);
436
437         /* these things are for search and replace */
438
439         /**
440          * Sets the selection from the current cursor position to length
441          * characters to the right. No safety checks.
442          */
443         void setSelectionRange(lyx::pos_type length);
444
445         /** simple replacing. The font of the first selected character
446           is used
447           */
448         void replaceSelectionWithString(string const & str);
449
450         /// needed to insert the selection
451         void insertStringAsLines(string const & str);
452         /// needed to insert the selection
453         void insertStringAsParagraphs(string const & str);
454
455         /// Find next inset of some specified type.
456         bool gotoNextInset(std::vector<Inset::Code> const & codes,
457                            string const & contents = string());
458         ///
459         void gotoInset(std::vector<Inset::Code> const & codes,
460                        bool same_content);
461         ///
462         void gotoInset(Inset::Code code, bool same_content);
463         ///
464
465         /* for the greater insets */
466
467         /// returns false if inset wasn't found
468         bool updateInset(Inset *);
469         ///
470         void checkParagraph(Paragraph * par, lyx::pos_type pos);
471         ///
472         int workWidth() const;
473         ///
474         int workWidth(Inset * inset) const;
475
476         ///
477         void computeBidiTables(Buffer const *, RowList::iterator row) const;
478         /// Maps positions in the visual string to positions in logical string.
479         lyx::pos_type log2vis(lyx::pos_type pos) const;
480         /// Maps positions in the logical string to positions in visual string.
481         lyx::pos_type vis2log(lyx::pos_type pos) const;
482         ///
483         lyx::pos_type bidi_level(lyx::pos_type pos) const;
484         ///
485         bool bidi_InRange(lyx::pos_type pos) const;
486 private:
487         ///
488         mutable RowList rowlist_;
489         ///
490         void cursorLeftOneWord(LyXCursor &);
491
492         ///
493         float getCursorX(RowList::iterator rit, lyx::pos_type pos,
494                          lyx::pos_type last, bool boundary) const;
495         /// used in setlayout
496         void makeFontEntriesLayoutSpecific(Buffer const &, Paragraph & par);
497
498         /** forces the redrawing of a paragraph. Needed when manipulating a
499             right address box
500             */
501         void redoDrawingOfParagraph(LyXCursor const & cursor);
502
503         /** Copybuffer for copy environment type.
504           Asger has learned that this should be a buffer-property instead
505           Lgb has learned that 'char' is a lousy type for non-characters
506           */
507         string copylayouttype;
508
509         /// removes the row and reset the touched counters
510         void removeRow(RowList::iterator rit);
511
512         /// remove all following rows of the paragraph of the specified row.
513         void removeParagraph(RowList::iterator rit);
514
515         /// insert the specified paragraph behind the specified row
516         void insertParagraph(Paragraph * par, RowList::iterator rowit);
517
518         /** appends  the implizit specified paragraph behind the specified row,
519          * start at the implizit given position */
520         void appendParagraph(RowList::iterator rowit);
521
522         ///
523         void breakAgain(RowList::iterator rit);
524         /// Calculate and set the height of the row
525         void setHeightOfRow(RowList::iterator rit);
526
527         // fix the cursor `cur' after a characters has been deleted at `where'
528         // position. Called by deleteEmptyParagraphMechanism
529         void fixCursorAfterDelete(LyXCursor & cur,
530                                   LyXCursor const & where);
531
532         /// delete double space (false) or empty paragraphs (true) around old_cursor
533         bool deleteEmptyParagraphMechanism(LyXCursor const & old_cursor);
534
535 public:
536         /** Updates all counters starting BEHIND the row. Changed paragraphs
537          * with a dynamic left margin will be rebroken. */
538         void updateCounters();
539         ///
540         void update();
541         /**
542          * Returns an inset if inset was hit, or 0 if not.
543          * If hit, the coordinates are changed relative to the inset.
544          */
545         Inset * checkInsetHit(int & x, int & y);
546
547         ///
548         int singleWidth(Paragraph * par,
549                 lyx::pos_type pos) const;
550         ///
551         int singleWidth(Paragraph * par,
552                 lyx::pos_type pos, char c) const;
553
554         /// return the color of the canvas
555         LColor::color backgroundColor() const;
556
557         ///
558         mutable bool bidi_same_direction;
559
560         unsigned char transformChar(unsigned char c, Paragraph * par,
561                                     lyx::pos_type pos) const;
562
563         /**
564          * Returns the left beginning of the text.
565          * This information cannot be taken from the layout object, because
566          * in LaTeX the beginning of the text fits in some cases
567          * (for example sections) exactly the label-width.
568          */
569         int leftMargin(Row const & row) const;
570         ///
571         int rightMargin(Buffer const &, Row const & row) const;
572
573         /** this calculates the specified parameters. needed when setting
574          * the cursor and when creating a visible row */
575         void prepareToPrint(RowList::iterator row, float & x,
576                             float & fill_separator,
577                             float & fill_hfill,
578                             float & fill_label_hfill,
579                             bool bidi = true) const;
580
581 private:
582         ///
583         void setCounter(Buffer const *, Paragraph * par);
584         ///
585         void deleteWordForward();
586         ///
587         void deleteWordBackward();
588         ///
589         void deleteLineForward();
590
591         /*
592          * some low level functions
593          */
594
595
596         /// return the pos value *before* which a row should break.
597         /// for example, the pos at which IsNewLine(pos) == true
598         lyx::pos_type rowBreakPoint(Row const & row) const;
599
600         /// returns the minimum space a row needs on the screen in pixel
601         int fill(RowList::iterator row, int workwidth) const;
602
603         /**
604          * returns the minimum space a manual label needs on the
605          * screen in pixels
606          */
607         int labelFill(Row const & row) const;
608
609         /// FIXME
610         int labelEnd(Row const & row) const;
611
612         ///
613         mutable std::vector<lyx::pos_type> log2vis_list;
614         ///
615         mutable std::vector<lyx::pos_type> vis2log_list;
616         ///
617         mutable std::vector<lyx::pos_type> bidi_levels;
618         ///
619         mutable lyx::pos_type bidi_start;
620         ///
621         mutable lyx::pos_type bidi_end;
622
623         ///
624         void charInserted();
625 public:
626         //
627         // special owner functions
628         ///
629         ParagraphList & ownerParagraphs() const;
630         //
631         void ownerParagraph(Paragraph *) const;
632         // set it searching first for the right owner using the paragraph id
633         void ownerParagraph(int id, Paragraph *) const;
634
635         /// return true if this is owned by an inset.
636         bool isInInset() const;
637 };
638
639 /// return the default height of a row in pixels, considering font zoom
640 extern int defaultRowHeight();
641
642 #endif // LYXTEXT_H