]> git.lyx.org Git - features.git/blob - src/Text.h
General cleanup: Text is (or should be) nothing more than InsetText private implement...
[features.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(InsetText * owner)
45                 : autoBreakRows_(false), owner_(owner)
46         {}
47
48         /// \return true if there's no content at all.
49         /// \warning a non standard layout on an empty paragraph doesn't
50         // count as empty.
51         bool empty() const;
52
53         InsetText const * inset() { return owner_; }
54
55         ///
56         FontInfo layoutFont(Buffer const & buffer, pit_type pit) const;
57         ///
58         FontInfo labelFont(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 inverse_logic = 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         /// Convert the paragraphs to a string.
116         /// \param AsStringParameter options. This can contain any combination of
117         /// asStringParameter values. Valid examples:
118         ///             asString(AS_STR_LABEL)
119         ///             asString(AS_STR_LABEL | AS_STR_INSETS)
120         ///             asString(AS_STR_INSETS)
121         docstring asString(int options = AS_STR_NONE) const;
122         ///
123         docstring asString(pit_type beg, pit_type end,
124                 int options = AS_STR_NONE) const;
125
126         /// insert a character at cursor position
127         /// FIXME: replace Cursor with DocIterator.
128         void insertChar(Cursor & cur, char_type c);
129         /// insert an inset at cursor position
130         /// FIXME: replace Cursor with DocIterator.
131         void insertInset(Cursor & cur, Inset * inset);
132
133         /// try to handle that request
134         /// FIXME: replace Cursor with DocIterator.
135         void dispatch(Cursor & cur, FuncRequest & cmd);
136         /// do we want to handle this event?
137         bool getStatus(Cursor & cur, FuncRequest const & cmd,
138                 FuncStatus & status) const;
139
140         /// read-only access to individual paragraph
141         Paragraph const & getPar(pit_type pit) const { return pars_[pit]; }
142         /// read-write access to individual paragraph
143         Paragraph & getPar(pit_type pit) { return pars_[pit]; }
144         // Returns the current font and depth as a message.
145         /// FIXME: replace Cursor with DocIterator.
146         docstring currentState(Cursor const & cur) const;
147
148         /** Find the word under \c from in the relative location
149          *  defined by \c word_location.
150          *  @param from return here the start of the word
151          *  @param to return here the end of the word
152          */
153         void getWord(CursorSlice & from, CursorSlice & to, word_location const) const;
154         /// just selects the word the cursor is in
155         void selectWord(Cursor & cur, word_location loc);
156         /// select all text
157         void selectAll(Cursor & cur);
158         /// convenience function get the previous word or an empty string
159         docstring previousWord(CursorSlice const & sl) const;
160         
161         /// what type of change operation to make
162         enum ChangeOp {
163                 ACCEPT,
164                 REJECT
165         };
166         /// accept or reject the selected change
167         void acceptOrRejectChanges(Cursor & cur, ChangeOp op);
168         /// accept the changes within the complete Text
169         void acceptChanges(BufferParams const & bparams);
170         /// reject the changes within the complete Text
171         void rejectChanges(BufferParams const & bparams);
172
173         /// returns true if par was empty and was removed
174         bool setCursor(Cursor & cur, pit_type par, pos_type pos,
175                        bool setfont = true, bool boundary = false);
176         ///
177         void setCursor(CursorSlice &, pit_type par, pos_type pos);
178         ///
179         void setCursorIntern(Cursor & cur, pit_type par,
180                  pos_type pos, bool setfont = true, bool boundary = false);
181
182         ///
183         void recUndo(Cursor & cur, pit_type first, pit_type last) const;
184         ///
185         void recUndo(Cursor & cur, pit_type first) const;
186
187         /// Move cursor one position backwards
188         /**
189          * Returns true if an update is needed after the move.
190          */
191         bool cursorBackward(Cursor & cur);
192         /// Move cursor visually one position to the left
193         /**
194          * \param skip_inset if true, don't enter insets
195          * Returns true if an update is needed after the move.
196          */
197         bool cursorVisLeft(Cursor & cur, bool skip_inset = false);
198         /// Move cursor one position forward
199         /**
200          * Returns true if an update is needed after the move.
201          */
202         bool cursorForward(Cursor & cur);
203         /// Move cursor visually one position to the right
204         /**
205          * \param skip_inset if true, don't enter insets
206          * Returns true if an update is needed after the move.
207          */
208         bool cursorVisRight(Cursor & cur, bool skip_inset = false);
209         ///
210         bool cursorBackwardOneWord(Cursor & cur);
211         ///
212         bool cursorForwardOneWord(Cursor & cur);
213         ///
214         bool cursorVisLeftOneWord(Cursor & cur);
215         ///
216         bool cursorVisRightOneWord(Cursor & cur);
217         /// Delete from cursor up to the end of the current or next word.
218         void deleteWordForward(Cursor & cur);
219         /// Delete from cursor to start of current or prior word.
220         void deleteWordBackward(Cursor & cur);
221         ///
222         bool cursorUpParagraph(Cursor & cur);
223         ///
224         bool cursorDownParagraph(Cursor & cur);
225         ///
226         bool cursorTop(Cursor & cur);
227         ///
228         bool cursorBottom(Cursor & cur);
229         /// Erase character at cursor. Honour change tracking
230         /// FIXME: replace Cursor with DocIterator.
231         bool erase(Cursor & cur);
232         /// Delete character before cursor. Honour CT
233         /// FIXME: replace Cursor with DocIterator.
234         bool backspace(Cursor & cur);
235         // Dissolve the inset under cursor
236         /// FIXME: replace Cursor with DocIterator.
237         bool dissolveInset(Cursor & cur);
238         ///
239         bool selectWordWhenUnderCursor(Cursor & cur, word_location);
240         /// Change the case of the word at cursor position.
241         void changeCase(Cursor & cur, TextCase action);
242         /// Transposes the character at the cursor with the one before it
243         void charsTranspose(Cursor & cur);
244
245         /** the DTP switches for paragraphs. LyX will store the top settings
246          always in the first physical paragraph, the bottom settings in the
247          last. When a paragraph is broken, the top settings rest, the bottom
248          settings are given to the new one.
249          This function will handle a multi-paragraph selection.
250          */
251         void setParagraphs(Cursor & cur, docstring arg, bool modify = false);
252         /// Sets parameters for current or selected paragraphs
253         void setParagraphs(Cursor & cur, ParagraphParameters const & p);
254
255         /* these things are for search and replace */
256
257         /// needed to insert the selection
258         /// FIXME: replace Cursor with DocIterator.
259         void insertStringAsLines(Cursor & cur, docstring const & str);
260         /// needed to insert the selection
261         /// FIXME: replace Cursor with DocIterator.
262         void insertStringAsParagraphs(Cursor & cur, docstring const & str);
263
264         /// access to our paragraphs
265         ParagraphList const & paragraphs() const { return pars_; }
266         ParagraphList & paragraphs() { return pars_; }
267         /// return true if this is the main text
268         bool isMainText(Buffer const &) const;
269
270         ///
271         double spacing(Buffer const & buffer, Paragraph const & par) const;
272         /// make a suggestion for a label
273         /// FIXME: replace Cursor with DocIterator.
274         docstring getPossibleLabel(Cursor const & cur) const;
275         /// is this paragraph right-to-left?
276         bool isRTL(Buffer const &, Paragraph const & par) const;
277
278         ///
279         bool checkAndActivateInset(Cursor & cur, bool front);
280         ///
281         bool checkAndActivateInsetVisual(Cursor & cur, bool movingForward, bool movingLeft);
282
283         ///
284         void write(Buffer const & buf, std::ostream & os) const;
285         /// returns true if \end_document has not been read
286         /// insetPtr is the containing Inset
287         bool read(Buffer const & buf, Lexer & lex, ErrorList & errorList, 
288                   InsetText * insetPtr);
289
290         /// delete double spaces, leading spaces, and empty paragraphs around old cursor.
291         /// \retval true if a change has happened and we need a redraw.
292         /// FIXME: replace Cursor with DocIterator. This is not possible right
293         /// now because recordUndo() is called which needs a Cursor.
294         static bool deleteEmptyParagraphMechanism(Cursor & cur,
295                 Cursor & old, bool & need_anchor_change);
296
297         /// delete double spaces, leading spaces, and empty paragraphs
298         /// from \first to \last paragraph
299         void deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool trackChanges);
300
301         /// To resolve macros properly the texts get their DocIterator.
302         /// Every macro definition is stored with its DocIterator
303         /// as well. Only those macros with a smaller iterator become 
304         /// visible in a paragraph.
305         DocIterator macrocontextPosition() const;
306         ///
307         void setMacrocontextPosition(DocIterator const & pos);
308
309         ///
310         bool completionSupported(Cursor const & cur) const;
311         ///
312         CompletionList const * createCompletionList(Cursor const & cur) const;
313         ///
314         bool insertCompletion(Cursor & cur, docstring const & s, bool /*finished*/);
315         ///
316         docstring completionPrefix(Cursor const & cur) const;
317
318 public:
319         ///
320         ParagraphList pars_;
321
322         ///
323         bool autoBreakRows_;
324 private:
325         /// return past-the-last paragraph influenced by a layout
326         /// change on pit
327         pit_type undoSpan(pit_type pit);
328
329         // fix the cursor `cur' after a characters has been deleted at `where'
330         // position. Called by deleteEmptyParagraphMechanism
331         static void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
332
333         // At cursor position 0, try to merge the paragraph with the one before it.
334         // Ignore change tracking, i.e., physically remove the end-of-par character
335         bool backspacePos0(Cursor & cur);
336         /// handle the case where bibitems were deleted
337         bool handleBibitems(Cursor & cur);
338         /// are we in a list item (description etc.)?
339         bool inDescriptionItem(Cursor & cur) const;
340         ///
341         void charInserted(Cursor & cur);
342         /// set 'number' font property
343         void number(Cursor & cur);
344
345         /// paste plain text at current cursor.
346         /// \param str string to paste
347         /// \param asParagraphs whether to paste as paragraphs or as lines
348         void pasteString(Cursor & cur, docstring const & str,
349                         bool asParagraphs);
350
351         /// Owner Inset.
352         InsetText * owner_;
353
354         /// position of the text in the buffer.
355         DocIterator macrocontext_position_;
356 };
357
358 } // namespace lyx
359
360 #endif // TEXT_H