]> git.lyx.org Git - lyx.git/blob - src/Text.h
0ebc1eeace21fc92b40aa046facff4d2075a9106
[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 one position forward
173         /**
174          * Returns true if an update is needed after the move.
175          */
176         bool cursorForward(Cursor & cur);
177         ///
178         bool cursorBackwardOneWord(Cursor & cur);
179         ///
180         bool cursorForwardOneWord(Cursor & cur);
181         /// Delete from cursor up to the end of the current or next word.
182         void deleteWordForward(Cursor & cur);
183         /// Delete from cursor to start of current or prior word.
184         void deleteWordBackward(Cursor & cur);
185         ///
186         bool cursorUpParagraph(Cursor & cur);
187         ///
188         bool cursorDownParagraph(Cursor & cur);
189         ///
190         bool cursorTop(Cursor & cur);
191         ///
192         bool cursorBottom(Cursor & cur);
193         /// Erase character at cursor. Honour change tracking
194         /// FIXME: replace Cursor with DocIterator.
195         bool erase(Cursor & cur);
196         /// Delete character before cursor. Honour CT
197         /// FIXME: replace Cursor with DocIterator.
198         bool backspace(Cursor & cur);
199         // Dissolve the inset under cursor
200         /// FIXME: replace Cursor with DocIterator.
201         bool dissolveInset(Cursor & cur);
202         ///
203         bool selectWordWhenUnderCursor(Cursor & cur, word_location);
204         /// Change the case of the word at cursor position.
205         void changeCase(Cursor & cur, TextCase action);
206         /// Transposes the character at the cursor with the one before it
207         void charsTranspose(Cursor & cur);
208
209         /** the DTP switches for paragraphs. LyX will store the top settings
210          always in the first physical paragraph, the bottom settings in the
211          last. When a paragraph is broken, the top settings rest, the bottom
212          settings are given to the new one.
213          This function will handle a multi-paragraph selection.
214          */
215         void setParagraphs(Cursor & cur, docstring arg, bool modify = false);
216         /// Sets parameters for current or selected paragraphs
217         void setParagraphs(Cursor & cur, ParagraphParameters const & p);
218
219         /* these things are for search and replace */
220
221         /// needed to insert the selection
222         /// FIXME: replace Cursor with DocIterator.
223         void insertStringAsLines(Cursor & cur, docstring const & str);
224         /// needed to insert the selection
225         /// FIXME: replace Cursor with DocIterator.
226         void insertStringAsParagraphs(Cursor & cur, docstring const & str);
227
228         /// access to our paragraphs
229         ParagraphList const & paragraphs() const { return pars_; }
230         ParagraphList & paragraphs() { return pars_; }
231         /// return true if this is the main text
232         bool isMainText(Buffer const &) const;
233
234         ///
235         double spacing(Buffer const & buffer, Paragraph const & par) const;
236         /// make a suggestion for a label
237         /// FIXME: replace Cursor with DocIterator.
238         docstring getPossibleLabel(Cursor & cur) const;
239         /// is this paragraph right-to-left?
240         bool isRTL(Buffer const &, Paragraph const & par) const;
241
242         ///
243         bool checkAndActivateInset(Cursor & cur, bool front);
244
245         ///
246         void write(Buffer const & buf, std::ostream & os) const;
247         /// returns whether we've seen our usual 'end' marker
248         bool read(Buffer const & buf, Lexer & lex, ErrorList & errorList);
249
250         /// delete double spaces, leading spaces, and empty paragraphs around old cursor.
251         /// \retval true if a change has happened and we need a redraw.
252         /// FIXME: replace Cursor with DocIterator. This is not possible right
253         /// now because recordUndo() is called which needs a Cursor.
254         static bool deleteEmptyParagraphMechanism(Cursor & cur,
255                 Cursor & old, bool & need_anchor_change);
256
257         /// delete double spaces, leading spaces, and empty paragraphs
258         /// from \first to \last paragraph
259         void deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool trackChanges);
260
261         /// To resolve macros properly the texts get their DocIterator.
262         /// Every macro definition is stored with its DocIterator
263         /// as well. Only those macros with a smaller iterator become 
264         /// visible in a paragraph.
265         DocIterator macrocontextPosition() const;
266         ///
267         void setMacrocontextPosition(DocIterator const & pos);
268
269 public:
270         ///
271         ParagraphList pars_;
272
273         ///
274         bool autoBreakRows_;
275 private:
276         /// return past-the-last paragraph influenced by a layout
277         /// change on pit
278         pit_type undoSpan(pit_type pit);
279
280         // fix the cursor `cur' after a characters has been deleted at `where'
281         // position. Called by deleteEmptyParagraphMechanism
282         static void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
283
284         // At cursor position 0, try to merge the paragraph with the one before it.
285         // Ignore change tracking, i.e., physically remove the end-of-par character
286         bool backspacePos0(Cursor & cur);
287         /// handle the case where bibitems were deleted
288         bool handleBibitems(Cursor & cur);
289         ///
290         void charInserted(Cursor & cur);
291         /// set 'number' font property
292         void number(Cursor & cur);
293
294         /// paste plain text at current cursor.
295         /// \param str string to paste
296         /// \param asParagraphs whether to paste as paragraphs or as lines
297         void pasteString(Cursor & cur, docstring const & str,
298                         bool asParagraphs);
299
300         /// position of the text in the buffer.
301         DocIterator macrocontext_position_;
302 };
303
304 } // namespace lyx
305
306 #endif // LYXTEXT_H