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