]> git.lyx.org Git - features.git/blob - src/Text.h
header cleanup.
[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 "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         void setCurrentFont(Cursor & cur);
173
174         ///
175         void recUndo(Cursor & cur, pit_type first, pit_type last) const;
176         ///
177         void recUndo(Cursor & cur, pit_type first) const;
178
179         /// Move cursor one position left
180         /**
181          * Returns true if an update is needed after the move.
182          */
183         bool cursorLeft(Cursor & cur);
184         /// Move cursor one position right
185         /**
186          * Returns true if an update is needed after the move.
187          */
188         bool cursorRight(Cursor & cur);
189         ///
190         bool cursorLeftOneWord(Cursor & cur);
191         ///
192         bool cursorRightOneWord(Cursor & cur);
193         /// Delete from cursor up to the end of the current or next word.
194         void deleteWordForward(Cursor & cur);
195         /// Delete from cursor to start of current or prior word.
196         void deleteWordBackward(Cursor & cur);
197         ///
198         bool cursorUpParagraph(Cursor & cur);
199         ///
200         bool cursorDownParagraph(Cursor & cur);
201         ///
202         bool cursorTop(Cursor & cur);
203         ///
204         bool cursorBottom(Cursor & cur);
205         /// Erase character at cursor. Honour change tracking
206         /// FIXME: replace Cursor with DocIterator.
207         bool erase(Cursor & cur);
208         /// Delete character before cursor. Honour CT
209         /// FIXME: replace Cursor with DocIterator.
210         bool backspace(Cursor & cur);
211         // Dissolve the inset under cursor
212         /// FIXME: replace Cursor with DocIterator.
213         bool dissolveInset(Cursor & cur);
214         ///
215         bool selectWordWhenUnderCursor(Cursor & cur, word_location);
216         ///
217         enum TextCase {
218                 ///
219                 text_lowercase = 0,
220                 ///
221                 text_capitalization = 1,
222                 ///
223                 text_uppercase = 2
224         };
225         /// Change the case of the word at cursor position.
226         void changeCase(Cursor & cur, TextCase action);
227         /// Transposes the character at the cursor with the one before it
228         void charsTranspose(Cursor & cur);
229
230         /** the DTP switches for paragraphs. LyX will store the top settings
231          always in the first physical paragraph, the bottom settings in the
232          last. When a paragraph is broken, the top settings rest, the bottom
233          settings are given to the new one.
234          This function will handle a multi-paragraph selection.
235          */
236         void setParagraphs(Cursor & cur, docstring arg, bool modify = false);
237         /// Sets parameters for current or selected paragraphs
238         void setParagraphs(Cursor & cur, ParagraphParameters const & p);
239
240         /* these things are for search and replace */
241
242         /// needed to insert the selection
243         /// FIXME: replace Cursor with DocIterator.
244         void insertStringAsLines(Cursor & cur, docstring const & str);
245         /// needed to insert the selection
246         /// FIXME: replace Cursor with DocIterator.
247         void insertStringAsParagraphs(Cursor & cur, docstring const & str);
248
249         /// return the color of the canvas
250         Color_color backgroundColor() const;
251
252         /// access to our paragraphs
253         ParagraphList const & paragraphs() const { return pars_; }
254         ParagraphList & paragraphs() { return pars_; }
255         /// return true if this is the main text
256         bool isMainText(Buffer const &) const;
257
258         ///
259         double spacing(Buffer const & buffer, Paragraph const & par) const;
260         /// make a suggestion for a label
261         /// FIXME: replace Cursor with DocIterator.
262         docstring getPossibleLabel(Cursor & cur) const;
263         /// is this paragraph right-to-left?
264         bool isRTL(Buffer const &, Paragraph const & par) const;
265         /// is this position in the paragraph right-to-left?
266         bool isRTL(Buffer const & buffer, CursorSlice const & sl, bool boundary) const;
267         /// is between pos-1 and pos an RTL<->LTR boundary?
268         bool isRTLBoundary(Buffer const & buffer, Paragraph const & par,
269           pos_type pos) const;
270         /// would be a RTL<->LTR boundary between pos and the given font?
271         bool isRTLBoundary(Buffer const & buffer, Paragraph const & par,
272           pos_type pos, Font const & font) const;
273
274         ///
275         bool checkAndActivateInset(Cursor & cur, bool front);
276
277         ///
278         void write(Buffer const & buf, std::ostream & os) const;
279         /// returns whether we've seen our usual 'end' marker
280         bool read(Buffer const & buf, Lexer & lex, ErrorList & errorList);
281
282         /// delete double spaces, leading spaces, and empty paragraphs around old cursor.
283         /// \retval true if a change has happened and we need a redraw.
284         /// FIXME: replace Cursor with DocIterator. This is not possible right
285         /// now because recordUndo() is called which needs a Cursor.
286         static bool deleteEmptyParagraphMechanism(Cursor & cur,
287                 Cursor & old, bool & need_anchor_change);
288
289         /// delete double spaces, leading spaces, and empty paragraphs
290         /// from \first to \last paragraph
291         void deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool trackChanges);
292
293 public:
294         /// the current font settings
295         Font current_font;
296         /// the current font
297         Font real_current_font;
298         ///
299         int background_color_;
300
301         ///
302         ParagraphList pars_;
303
304         /// our 'outermost' font. This is handed down from the surrounding
305         // inset through the pi/mi parameter (pi.base.font)
306         Font font_;
307
308         ///
309         bool autoBreakRows_;
310 private:
311         /// return past-the-last paragraph influenced by a layout
312         /// change on pit
313         pit_type undoSpan(pit_type pit);
314
315         // fix the cursor `cur' after a characters has been deleted at `where'
316         // position. Called by deleteEmptyParagraphMechanism
317         static void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
318
319         // At cursor position 0, try to merge the paragraph with the one before it.
320         // Ignore change tracking, i.e., physically remove the end-of-par character
321         bool backspacePos0(Cursor & cur);
322         /// handle the case where bibitems were deleted
323         bool handleBibitems(Cursor & cur);
324         ///
325         void charInserted();
326         /// set 'number' font property
327         void number(Cursor & cur);
328
329         /// paste plain text at current cursor.
330         /// \param str string to paste
331         /// \param asParagraphs whether to paste as paragraphs or as lines
332         void pasteString(Cursor & cur, docstring const & str,
333                         bool asParagraphs);
334 };
335
336 } // namespace lyx
337
338 #endif // LYXTEXT_H