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