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