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