]> git.lyx.org Git - lyx.git/blob - src/lyxtext.h
e536a1aefb5eda73956050954270930813b9b787
[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 "bufferview_funcs.h"
18 #include "layout.h"
19 #include "lyxfont.h"
20 #include "ParagraphList_fwd.h"
21 #include "RowList_fwd.h"
22 #include "textcursor.h"
23
24 #include "insets/inset.h"
25
26 class Buffer;
27 class BufferParams;
28 class BufferView;
29 class Dimension;
30 class EnumLColor;
31 class InsetText;
32 class LyXCursor;
33 class MetricsInfo;
34 class Paragraph;
35 class Row;
36 class Spacing;
37 class UpdatableInset;
38 class VSpace;
39 class WordLangTuple;
40
41
42 /**
43   This class used to hold the mapping between buffer paragraphs and
44         screen rows. Nowadays, the Paragraphs take care of their rows
45   themselves and this contains just most of the code for manipulating
46   them and interaction with the Cursor.
47   */
48
49 // The inheritance from TextCursor should go. It's just there to ease
50 // transition...
51 class LyXText : public TextCursor {
52 public:
53         /// Constructor
54         LyXText(BufferView *, InsetText *, bool ininset,
55                 ParagraphList & paragraphs);
56
57         void init(BufferView *);
58         ///
59         int height;
60         ///
61         unsigned int width;
62         /// the current font settings
63         LyXFont current_font;
64         /// the current font
65         LyXFont real_current_font;
66         /// our buffer's default layout font
67         LyXFont defaultfont_;
68 private:
69         /// offset of dran area to document start.
70         int anchor_y_;
71 public:
72         /// update all cached row positions
73         void updateRowPositions();
74         ///
75         InsetText * inset_owner;
76         ///
77         UpdatableInset * the_locking_inset;
78
79         ///
80         int getRealCursorX() const;
81         ///
82         LyXFont getFont(ParagraphList::iterator pit, lyx::pos_type pos) const;
83         ///
84         LyXFont getLayoutFont(ParagraphList::iterator pit) const;
85         ///
86         LyXFont getLabelFont(ParagraphList::iterator pit) const;
87         ///
88         void setCharFont(ParagraphList::iterator pit,
89                          lyx::pos_type pos, LyXFont const & font);
90         void setCharFont(ParagraphList::iterator pit,
91                          lyx::pos_type pos,
92                          LyXFont const & font, bool toggleall);
93
94         /// what you expect when pressing <enter> at cursor position
95         void breakParagraph(ParagraphList & paragraphs, char keep_layout = 0);
96
97         /** set layout over selection and make a total rebreak of
98           those paragraphs
99           */
100         ParagraphList::iterator
101         setLayout(LyXCursor & actual_cursor,
102                   LyXCursor & selection_start,
103                   LyXCursor & selection_end,
104                   string const & layout);
105         ///
106         void setLayout(string const & layout);
107
108         /**
109          * Increase or decrease the nesting depth of the selected paragraph(s)
110          * if test_only, don't change any depths. Returns whether something
111          * (would have) changed
112          */
113         bool changeDepth(bv_funcs::DEPTH_CHANGE type, bool test_only);
114
115         /// get the depth at current cursor position
116         int getDepth() const;
117
118         /** set font over selection and make a total rebreak of those
119           paragraphs.
120           toggleall defaults to false.
121           */
122         void setFont(LyXFont const &, bool toggleall = false);
123
124         /// rebreaks all paragaphs between the given pars.
125         int redoParagraphs(ParagraphList::iterator begin,
126                             ParagraphList::iterator end);
127         /// rebreaks the given par
128         void redoParagraph(ParagraphList::iterator pit);
129
130         /// rebreaks the cursor par
131         void redoParagraph();
132 private:
133         /// rebreaks the given par, return max row width
134         int redoParagraphInternal(ParagraphList::iterator pit);
135 public:
136
137         ///
138         void toggleFree(LyXFont const &, bool toggleall = false);
139
140         ///
141         string getStringToIndex();
142
143         /** insert a character, moves all the following breaks in the
144           same Paragraph one to the right and make a little rebreak
145           */
146         void insertChar(char c);
147         ///
148         void insertInset(InsetOld * inset);
149
150         /// a full rebreak of the whole text
151         void fullRebreak();
152         /// compute text metrics
153         void metrics(MetricsInfo & mi, Dimension & dim);
154
155         ///
156         InsetOld::RESULT dispatch(FuncRequest const & cmd);
157
158         BufferView * bv();
159
160         BufferView * bv() const;
161
162         friend class LyXScreen;
163
164 public:
165         /// only the top-level LyXText has this non-zero
166         BufferView * bv_owner;
167
168 private:
169         /// returns a pointer to a specified row.
170         RowList::iterator
171         getRow(ParagraphList::iterator pit, lyx::pos_type pos) const;
172 public:
173         /// returns a pointer cursor row
174         RowList::iterator getRow(LyXCursor const & cursor) const;
175         /// convenience
176         RowList::iterator cursorRow() const;
177         /**
178          * Return the next row, when cursor is at the end of the
179          * previous row, for insets that take a full row.
180          *
181          * FIXME: explain why we need this ? especially for y...
182          */
183         RowList::iterator cursorIRow() const;
184
185         /** returns a pointer to the row near the specified y-coordinate
186           (relative to the whole text). y is set to the real beginning
187           of this row
188           */
189         RowList::iterator getRowNearY(int y,
190                 ParagraphList::iterator & pit) const;
191
192         /** returns the column near the specified x-coordinate of the row
193          x is set to the real beginning of this column
194          */
195         lyx::pos_type getColumnNearX(ParagraphList::iterator pit,
196                 RowList::iterator rit, int & x, bool & boundary) const;
197
198         /// need the selection cursor:
199         void setSelection();
200         ///
201         void clearSelection();
202
203         /// select the word we need depending on word_location
204         void getWord(LyXCursor & from, LyXCursor & to, lyx::word_location const);
205         /// just selects the word the cursor is in
206         void selectWord(lyx::word_location loc);
207         /// returns the inset at cursor (if it exists), 0 otherwise
208         InsetOld * getInset() const;
209
210         /// accept selected change
211         void acceptChange();
212
213         /// reject selected change
214         void rejectChange();
215
216         /** 'selects" the next word, where the cursor is not in
217          and returns this word as string. THe cursor will be moved
218          to the beginning of this word.
219          With SelectSelectedWord can this be highlighted really
220          */
221         WordLangTuple const selectNextWordToSpellcheck(float & value);
222         ///
223         void selectSelectedWord();
224         /// re-computes the cached coordinates in the cursor
225         void redoCursor();
226         /// returns true if par was empty and was removed
227         bool setCursor(ParagraphList::iterator pit,
228                        lyx::pos_type pos,
229                        bool setfont = true,
230                        bool boundary = false);
231         ///
232         void setCursor(LyXCursor &, ParagraphList::iterator pit,
233                        lyx::pos_type pos,
234                        bool boundary = false);
235         ///
236         void setCursorIntern(ParagraphList::iterator pit,
237                              lyx::pos_type pos,
238                              bool setfont = true,
239                              bool boundary = false);
240         ///
241         void setCurrentFont();
242
243         ///
244         bool isBoundary(Buffer const &, Paragraph const & par,
245                         lyx::pos_type pos) const;
246         ///
247         bool isBoundary(Buffer const &, Paragraph const & par,
248                          lyx::pos_type pos,
249                          LyXFont const & font) const;
250
251         ///
252         void setCursorFromCoordinates(int x, int y);
253         ///
254         void setCursorFromCoordinates(LyXCursor &,
255                                       int x, int y);
256         ///
257         void cursorUp(bool selecting = false);
258         ///
259         void cursorDown(bool selecting = false);
260         ///
261         void cursorLeft(bool internal = true);
262         ///
263         void cursorRight(bool internal = true);
264         ///
265         void cursorLeftOneWord();
266         ///
267         void cursorRightOneWord();
268         ///
269         void cursorUpParagraph();
270         ///
271         void cursorDownParagraph();
272         ///
273         void cursorHome();
274         ///
275         void cursorEnd();
276         ///
277         void cursorPrevious();
278         ///
279         void cursorNext();
280         ///
281         void cursorTop();
282         ///
283         void cursorBottom();
284         ///
285         void Delete();
286         ///
287         void backspace();
288         ///
289         bool selectWordWhenUnderCursor(lyx::word_location);
290         ///
291         enum TextCase {
292                 ///
293                 text_lowercase = 0,
294                 ///
295                 text_capitalization = 1,
296                 ///
297                 text_uppercase = 2
298         };
299         /// Change the case of the word at cursor position.
300         void changeCase(TextCase action);
301
302         ///
303         void toggleInset();
304         ///
305         void cutSelection(bool doclear = true, bool realcut = true);
306         ///
307         void copySelection();
308         ///
309         void pasteSelection(size_t sel_index = 0);
310
311         /** the DTP switches for paragraphs. LyX will store the top settings
312          always in the first physical paragraph, the bottom settings in the
313          last. When a paragraph is broken, the top settings rest, the bottom
314          settings are given to the new one. So I can make shure, they do not
315          duplicate themself (and you cannnot make dirty things with them! )
316          */
317         void setParagraph(bool line_top, bool line_bottom,
318                           bool pagebreak_top, bool pagebreak_bottom,
319                           VSpace const & space_top,
320                           VSpace const & space_bottom,
321                           Spacing const & spacing,
322                           LyXAlignment align,
323                           string const & labelwidthstring,
324                           bool noindent);
325
326         /* these things are for search and replace */
327
328         /**
329          * Sets the selection from the current cursor position to length
330          * characters to the right. No safety checks.
331          */
332         void setSelectionRange(lyx::pos_type length);
333
334         /** simple replacing. The font of the first selected character
335           is used
336           */
337         void replaceSelectionWithString(string const & str);
338
339         /// needed to insert the selection
340         void insertStringAsLines(string const & str);
341         /// needed to insert the selection
342         void insertStringAsParagraphs(string const & str);
343
344         /// Find next inset of some specified type.
345         bool gotoNextInset(std::vector<InsetOld::Code> const & codes,
346                            string const & contents = string());
347         ///
348         void gotoInset(std::vector<InsetOld::Code> const & codes,
349                        bool same_content);
350         ///
351         void gotoInset(InsetOld::Code code, bool same_content);
352
353         ///
354         int workWidth() const;
355
356         ///
357         void computeBidiTables(ParagraphList::iterator pit,
358                 Buffer const &, RowList::iterator row) const;
359         /// Maps positions in the visual string to positions in logical string.
360         lyx::pos_type log2vis(lyx::pos_type pos) const;
361         /// Maps positions in the logical string to positions in visual string.
362         lyx::pos_type vis2log(lyx::pos_type pos) const;
363         ///
364         lyx::pos_type bidi_level(lyx::pos_type pos) const;
365         ///
366         bool bidi_InRange(lyx::pos_type pos) const;
367 private:
368         ///
369         float getCursorX(ParagraphList::iterator pit,
370              RowList::iterator rit, lyx::pos_type pos,
371                          lyx::pos_type last, bool boundary) const;
372         /// used in setlayout
373         void makeFontEntriesLayoutSpecific(BufferParams const &, Paragraph & par);
374
375         /// Calculate and set the height of the row
376         void setHeightOfRow(ParagraphList::iterator, RowList::iterator rit);
377
378         // fix the cursor `cur' after a characters has been deleted at `where'
379         // position. Called by deleteEmptyParagraphMechanism
380         void fixCursorAfterDelete(LyXCursor & cur, LyXCursor const & where);
381
382         /// delete double space (false) or empty paragraphs (true) around old_cursor
383         bool deleteEmptyParagraphMechanism(LyXCursor const & old_cursor);
384
385 public:
386         /** Updates all counters starting BEHIND the row. Changed paragraphs
387          * with a dynamic left margin will be rebroken. */
388         void updateCounters();
389         /**
390          * Returns an inset if inset was hit, or 0 if not.
391          * If hit, the coordinates are changed relative to the inset.
392          */
393         InsetOld * checkInsetHit(int & x, int & y);
394
395         ///
396         int singleWidth(ParagraphList::iterator pit, lyx::pos_type pos) const;
397         ///
398         int singleWidth(ParagraphList::iterator pit,
399                 lyx::pos_type pos, char c, LyXFont const & Font) const;
400
401         /// return the color of the canvas
402         EnumLColor backgroundColor() const;
403
404         ///
405         mutable bool bidi_same_direction;
406
407         unsigned char transformChar(unsigned char c, Paragraph const & par,
408                                     lyx::pos_type pos) const;
409
410         /**
411          * Returns the left beginning of the text.
412          * This information cannot be taken from the layout object, because
413          * in LaTeX the beginning of the text fits in some cases
414          * (for example sections) exactly the label-width.
415          */
416         int leftMargin(ParagraphList::iterator pit, Row const & row) const;
417         ///
418         int rightMargin(ParagraphList::iterator pit, Buffer const &, Row const & row) const;
419
420         /** this calculates the specified parameters. needed when setting
421          * the cursor and when creating a visible row */
422         void prepareToPrint(ParagraphList::iterator pit,
423                 RowList::iterator row) const;
424
425 private:
426         ///
427         void setCounter(Buffer const &, ParagraphList::iterator pit);
428         ///
429         void deleteWordForward();
430         ///
431         void deleteWordBackward();
432         ///
433         void deleteLineForward();
434
435         /*
436          * some low level functions
437          */
438
439
440         /// sets row.end to the pos value *after* which a row should break.
441         /// for example, the pos after which isNewLine(pos) == true
442         lyx::pos_type rowBreakPoint(ParagraphList::iterator pit,
443                 Row const & row) const;
444
445         /// returns the minimum space a row needs on the screen in pixel
446         int fill(ParagraphList::iterator pit,
447                 RowList::iterator row, int workwidth) const;
448
449         /**
450          * returns the minimum space a manual label needs on the
451          * screen in pixels
452          */
453         int labelFill(ParagraphList::iterator pit, Row const & row) const;
454
455         /// FIXME
456         int labelEnd(ParagraphList::iterator pit, Row const & row) const;
457
458         ///
459         mutable std::vector<lyx::pos_type> log2vis_list;
460         ///
461         mutable std::vector<lyx::pos_type> vis2log_list;
462         ///
463         mutable std::vector<lyx::pos_type> bidi_levels;
464         ///
465         mutable lyx::pos_type bidi_start;
466         ///
467         mutable lyx::pos_type bidi_end;
468
469         ///
470         const bool in_inset_;
471         ///
472         ParagraphList & paragraphs_;
473
474         ///
475         void charInserted();
476 public:
477         //
478         // special owner functions
479         ///
480         ParagraphList & ownerParagraphs() const;
481
482         /// return true if this is owned by an inset.
483         bool isInInset() const;
484
485         /// return first row of text
486         RowList::iterator firstRow() const;
487         /// return last row of text
488         RowList::iterator lastRow() const;
489         /// return row "behind" last row of text
490         RowList::iterator endRow() const;
491         /// return next row crossing paragraph boundaries
492         void nextRow(ParagraphList::iterator & pit,
493                 RowList::iterator & rit) const;
494         /// return previous row crossing paragraph boundaries
495         void previousRow(ParagraphList::iterator & pit,
496                 RowList::iterator & rit) const;
497
498         ///
499         bool noRows() const;
500
501 private:
502         /** Cursor related data.
503           Later this variable has to be removed. There should be now internal
504           cursor in a text */
505         ///
506         ///TextCursor cursor_;
507 };
508
509 /// return the default height of a row in pixels, considering font zoom
510 extern int defaultRowHeight();
511
512 #endif // LYXTEXT_H