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