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