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