]> git.lyx.org Git - lyx.git/blob - src/Text.h
fix a visual cursor edge-case:
[lyx.git] / src / Text.h
1 // -*- C++ -*-
2 /**
3  * \file Text.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 TEXT_H
15 #define TEXT_H
16
17 #include "DocIterator.h"
18 #include "ParagraphList.h"
19
20 namespace lyx {
21
22 class Buffer;
23 class BufferParams;
24 class BufferView;
25 class CursorSlice;
26 class DocIterator;
27 class ErrorList;
28 class Font;
29 class FontInfo;
30 class FuncRequest;
31 class FuncStatus;
32 class Inset;
33 class Cursor;
34 class Lexer;
35 class PainterInfo;
36 class Spacing;
37
38
39 /// This class encapsulates the main text data and operations in LyX
40 class Text {
41 public:
42         /// constructor
43         explicit Text();
44
45         /// \return true if there's no content at all.
46         /// \warning a non standard layout on an empty paragraph doesn't
47         // count as empty.
48         bool empty() const;
49
50         ///
51         FontInfo getLayoutFont(Buffer const & buffer, pit_type pit) const;
52         ///
53         FontInfo getLabelFont(Buffer const & buffer,
54                 Paragraph const & par) const;
55         /** Set font of character at position \p pos in paragraph \p pit.
56          *  Must not be called if \p pos denotes an inset with text contents,
57          *  and the inset is not allowed inside a font change (see below).
58          */
59         void setCharFont(Buffer const & buffer, pit_type pit, pos_type pos,
60                 Font const & font, Font const & display_font);
61
62         /** Needed to propagate font changes to all text cells of insets
63          *  that are not allowed inside a font change (bug 1973).
64          *  Must not be called if \p pos denotes an ordinary character or an
65          *  inset that is alowed inside a font change.
66          *  FIXME: This should be removed, see documentation of noFontChange
67          *  in insetbase.h
68          */
69         void setInsetFont(BufferView const & bv, pit_type pit, pos_type pos,
70                 Font const & font, bool toggleall = false);
71
72         /// what you expect when pressing \<enter\> at cursor position
73         void breakParagraph(Cursor & cur, bool inverse_logic = false);
74
75         /// set layout over selection
76         void setLayout(Buffer const & buffer, pit_type start, pit_type end,
77                 docstring const & layout);
78         /// Set given layout to current cursor position.
79         /// FIXME: replace Cursor with DocIterator.
80         void setLayout(Cursor & cur, docstring const & layout);
81
82         /// what type of depth change to make
83         enum DEPTH_CHANGE {
84                 INC_DEPTH,
85                 DEC_DEPTH
86         };
87         /// Increase or decrease the nesting depth of the selected paragraph(s)
88         /// FIXME: replace Cursor with DocIterator.
89         void changeDepth(Cursor & cur, DEPTH_CHANGE type);
90
91         /// Returns whether something would be changed by changeDepth
92         /// FIXME: replace Cursor with DocIterator.
93         bool changeDepthAllowed(Cursor & cur, DEPTH_CHANGE type) const;
94
95         /// Set font over selection paragraphs and rebreak.
96         /// FIXME: replace Cursor with DocIterator.
97         void setFont(Cursor & cur, Font const &, bool toggleall = false);
98         /// Set font from \p begin to \p end and rebreak.
99         void setFont(BufferView const & bv, CursorSlice const & begin,
100                 CursorSlice const & end, Font const &,
101                 bool toggleall = false);
102
103         ///
104         void toggleFree(Cursor & cur, Font const &, bool toggleall = false);
105
106         /// ???
107         /// FIXME: replace Cursor with DocIterator.
108         docstring getStringToIndex(Cursor const & cur);
109
110         /// insert a character at cursor position
111         /// FIXME: replace Cursor with DocIterator.
112         void insertChar(Cursor & cur, char_type c);
113         /// insert an inset at cursor position
114         /// FIXME: replace Cursor with DocIterator.
115         void insertInset(Cursor & cur, Inset * inset);
116
117         /// try to handle that request
118         /// FIXME: replace Cursor with DocIterator.
119         void dispatch(Cursor & cur, FuncRequest & cmd);
120         /// do we want to handle this event?
121         bool getStatus(Cursor & cur, FuncRequest const & cmd,
122                 FuncStatus & status) const;
123
124         /// read-only access to individual paragraph
125         Paragraph const & getPar(pit_type pit) const { return pars_[pit]; }
126         /// read-write access to individual paragraph
127         Paragraph & getPar(pit_type pit) { return pars_[pit]; }
128         // Returns the current font and depth as a message.
129         /// FIXME: replace Cursor with DocIterator.
130         docstring currentState(Cursor & cur);
131
132         /** Find the word under \c from in the relative location
133          *  defined by \c word_location.
134          *  @param from return here the start of the word
135          *  @param to return here the end of the word
136          */
137         void getWord(CursorSlice & from, CursorSlice & to, word_location const);
138         /// just selects the word the cursor is in
139         void selectWord(Cursor & cur, word_location loc);
140
141         /// what type of change operation to make
142         enum ChangeOp {
143                 ACCEPT,
144                 REJECT
145         };
146         /// accept or reject the selected change
147         void acceptOrRejectChanges(Cursor & cur, ChangeOp op);
148         /// accept the changes within the complete Text
149         void acceptChanges(BufferParams const & bparams);
150         /// reject the changes within the complete Text
151         void rejectChanges(BufferParams const & bparams);
152
153         /// returns true if par was empty and was removed
154         bool setCursor(Cursor & cur, pit_type par, pos_type pos,
155                        bool setfont = true, bool boundary = false);
156         ///
157         void setCursor(CursorSlice &, pit_type par, pos_type pos);
158         ///
159         void setCursorIntern(Cursor & cur, pit_type par,
160                  pos_type pos, bool setfont = true, bool boundary = false);
161
162         ///
163         void recUndo(Cursor & cur, pit_type first, pit_type last) const;
164         ///
165         void recUndo(Cursor & cur, pit_type first) const;
166
167         /// Move cursor one position backwards
168         /**
169          * Returns true if an update is needed after the move.
170          */
171         bool cursorBackward(Cursor & cur);
172         /// Move cursor visually one position to the left
173         /**
174          * \param skip_inset if true, don't enter insets
175          * Returns true if an update is needed after the move.
176          */
177         bool cursorVisLeft(Cursor & cur, bool skip_inset = false);
178         /// Move cursor one position forward
179         /**
180          * Returns true if an update is needed after the move.
181          */
182         bool cursorForward(Cursor & cur);
183         /// Move cursor visually one position to the right
184         /**
185          * \param skip_inset if true, don't enter insets
186          * Returns true if an update is needed after the move.
187          */
188         bool cursorVisRight(Cursor & cur, bool skip_inset = false);
189         ///
190         bool cursorBackwardOneWord(Cursor & cur);
191         ///
192         bool cursorForwardOneWord(Cursor & cur);
193         /// Delete from cursor up to the end of the current or next word.
194         void deleteWordForward(Cursor & cur);
195         /// Delete from cursor to start of current or prior word.
196         void deleteWordBackward(Cursor & cur);
197         ///
198         bool cursorUpParagraph(Cursor & cur);
199         ///
200         bool cursorDownParagraph(Cursor & cur);
201         ///
202         bool cursorTop(Cursor & cur);
203         ///
204         bool cursorBottom(Cursor & cur);
205         /// Erase character at cursor. Honour change tracking
206         /// FIXME: replace Cursor with DocIterator.
207         bool erase(Cursor & cur);
208         /// Delete character before cursor. Honour CT
209         /// FIXME: replace Cursor with DocIterator.
210         bool backspace(Cursor & cur);
211         // Dissolve the inset under cursor
212         /// FIXME: replace Cursor with DocIterator.
213         bool dissolveInset(Cursor & cur);
214         ///
215         bool selectWordWhenUnderCursor(Cursor & cur, word_location);
216         /// Change the case of the word at cursor position.
217         void changeCase(Cursor & cur, TextCase action);
218         /// Transposes the character at the cursor with the one before it
219         void charsTranspose(Cursor & cur);
220
221         /** the DTP switches for paragraphs. LyX will store the top settings
222          always in the first physical paragraph, the bottom settings in the
223          last. When a paragraph is broken, the top settings rest, the bottom
224          settings are given to the new one.
225          This function will handle a multi-paragraph selection.
226          */
227         void setParagraphs(Cursor & cur, docstring arg, bool modify = false);
228         /// Sets parameters for current or selected paragraphs
229         void setParagraphs(Cursor & cur, ParagraphParameters const & p);
230
231         /* these things are for search and replace */
232
233         /// needed to insert the selection
234         /// FIXME: replace Cursor with DocIterator.
235         void insertStringAsLines(Cursor & cur, docstring const & str);
236         /// needed to insert the selection
237         /// FIXME: replace Cursor with DocIterator.
238         void insertStringAsParagraphs(Cursor & cur, docstring const & str);
239
240         /// access to our paragraphs
241         ParagraphList const & paragraphs() const { return pars_; }
242         ParagraphList & paragraphs() { return pars_; }
243         /// return true if this is the main text
244         bool isMainText(Buffer const &) const;
245
246         ///
247         double spacing(Buffer const & buffer, Paragraph const & par) const;
248         /// make a suggestion for a label
249         /// FIXME: replace Cursor with DocIterator.
250         docstring getPossibleLabel(Cursor & cur) const;
251         /// is this paragraph right-to-left?
252         bool isRTL(Buffer const &, Paragraph const & par) const;
253
254         ///
255         bool checkAndActivateInset(Cursor & cur, bool front);
256         ///
257         bool checkAndActivateInsetVisual(Cursor & cur, bool movingForward, bool movingLeft);
258
259         ///
260         void write(Buffer const & buf, std::ostream & os) const;
261         /// returns whether we've seen our usual 'end' marker
262         /// insetPtr is the containing Inset
263         bool read(Buffer const & buf, Lexer & lex, ErrorList & errorList, 
264                   InsetText * insetPtr);
265
266         /// delete double spaces, leading spaces, and empty paragraphs around old cursor.
267         /// \retval true if a change has happened and we need a redraw.
268         /// FIXME: replace Cursor with DocIterator. This is not possible right
269         /// now because recordUndo() is called which needs a Cursor.
270         static bool deleteEmptyParagraphMechanism(Cursor & cur,
271                 Cursor & old, bool & need_anchor_change);
272
273         /// delete double spaces, leading spaces, and empty paragraphs
274         /// from \first to \last paragraph
275         void deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool trackChanges);
276
277         /// To resolve macros properly the texts get their DocIterator.
278         /// Every macro definition is stored with its DocIterator
279         /// as well. Only those macros with a smaller iterator become 
280         /// visible in a paragraph.
281         DocIterator macrocontextPosition() const;
282         ///
283         void setMacrocontextPosition(DocIterator const & pos);
284
285 public:
286         ///
287         ParagraphList pars_;
288
289         ///
290         bool autoBreakRows_;
291 private:
292         /// return past-the-last paragraph influenced by a layout
293         /// change on pit
294         pit_type undoSpan(pit_type pit);
295
296         // fix the cursor `cur' after a characters has been deleted at `where'
297         // position. Called by deleteEmptyParagraphMechanism
298         static void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
299
300         // At cursor position 0, try to merge the paragraph with the one before it.
301         // Ignore change tracking, i.e., physically remove the end-of-par character
302         bool backspacePos0(Cursor & cur);
303         /// handle the case where bibitems were deleted
304         bool handleBibitems(Cursor & cur);
305         ///
306         void charInserted(Cursor & cur);
307         /// set 'number' font property
308         void number(Cursor & cur);
309
310         /// paste plain text at current cursor.
311         /// \param str string to paste
312         /// \param asParagraphs whether to paste as paragraphs or as lines
313         void pasteString(Cursor & cur, docstring const & str,
314                         bool asParagraphs);
315
316         /// position of the text in the buffer.
317         DocIterator macrocontext_position_;
318 };
319
320 } // namespace lyx
321
322 #endif // LYXTEXT_H