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