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