]> git.lyx.org Git - lyx.git/blob - src/Text.h
fc602fcf9775e325e54ea83aacb7c203a3c460c3
[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 Cursor;
27 class CursorSlice;
28 class DocIterator;
29 class ErrorList;
30 class Font;
31 class FontInfo;
32 class FuncRequest;
33 class FuncStatus;
34 class Inset;
35 class Lexer;
36 class PainterInfo;
37 class Spacing;
38
39 /// This class encapsulates the main text data and operations in LyX.
40 /// This is more or less the private implementation of InsetText.
41 class Text {
42 private:
43         /// Default constructor.
44         Text(InsetText * owner, bool use_default_layout);
45
46         /// Copy constructor.
47         Text(InsetText * owner, Text const & text);
48
49 public:
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         /// Access to owner InsetText.
55         InsetText const & inset() const;
56
57         ///
58         FontInfo layoutFont(pit_type pit) const;
59         ///
60         FontInfo labelFont(Paragraph const & par) const;
61         /** Set font of character at position \p pos in paragraph \p pit.
62          *  Must not be called if \p pos denotes an inset with text contents,
63          *  and the inset is not allowed inside a font change (see below).
64          */
65         void setCharFont(pit_type pit, pos_type pos,
66                 Font const & font, Font const & display_font);
67
68         /** Needed to propagate font changes to all text cells of insets
69          *  that are not allowed inside a font change (bugs 1973, 6919).
70          *  Must not be called if \p pos denotes an ordinary character or an
71          *  inset that is alowed inside a font change.
72          */
73         void setInsetFont(BufferView const & bv, pit_type pit, pos_type pos,
74                 Font const & font);
75
76         /// what you expect when pressing \<enter\> at cursor position
77         /// \param inverse_logic if false, the same layout is set for the
78         /// new paragraph if the layout is an environment; if true, the
79         /// same layout is set if it is not an environment
80         void breakParagraph(Cursor & cur, bool inverse_logic = false);
81
82         /// set layout over selection
83         void setLayout(pit_type start, pit_type end,
84                 docstring const & layout);
85         /// Set given layout to current cursor position or selection.
86         /// Handles undo.
87         void setLayout(Cursor & cur, docstring const & layout);
88
89         /// what type of depth change to make
90         enum DEPTH_CHANGE {
91                 INC_DEPTH,
92                 DEC_DEPTH
93         };
94         /// Increase or decrease the nesting depth of the selected paragraph(s)
95         /// FIXME: replace Cursor with DocIterator.
96         void changeDepth(Cursor & cur, DEPTH_CHANGE type);
97
98         /// Returns whether something would be changed by changeDepth
99         /// FIXME: replace Cursor with DocIterator.
100         bool changeDepthAllowed(Cursor & cur, DEPTH_CHANGE type) const;
101
102         /// Set font over selection paragraphs and rebreak.
103         /// FIXME: replace Cursor with DocIterator.
104         void setFont(Cursor & cur, Font const &, bool toggleall = false);
105         /// Set font from \p begin to \p end and rebreak.
106         void setFont(BufferView const & bv, CursorSlice const & begin,
107                 CursorSlice const & end, Font const &);
108
109         ///
110         void toggleFree(Cursor & cur, Font const &, bool toggleall = false);
111
112         /// ???
113         /// FIXME: replace Cursor with DocIterator.
114         docstring getStringToIndex(Cursor const & cur);
115
116         /// Convert the paragraphs to a string.
117         /// \param AsStringParameter options. This can contain any combination of
118         /// asStringParameter values. Valid examples:
119         ///             asString(AS_STR_LABEL)
120         ///             asString(AS_STR_LABEL | AS_STR_INSETS)
121         ///             asString(AS_STR_INSETS)
122         docstring asString(int options = AS_STR_NONE) const;
123         ///
124         docstring asString(pit_type beg, pit_type end,
125                 int options = AS_STR_NONE) const;
126
127         /// truncates str to maxlenwith an ellipsis and replaces the characters '\n'
128         /// and '\t' with spaces
129         static void shortenForOutliner(docstring & str, size_t const maxlen);
130
131         /// Appends a possibly abbreviated representation of our text to \param os,
132         /// where \param maxlen defines the maximum size of \param os. If \param
133         /// shorten is true, then os is shortened as above.
134         void forOutliner(docstring & os, size_t maxlen, bool shorten = true) const;
135         /// Appends a possibly abbreviated representation of our text, from
136         /// start to end, to \param os, where \param maxlen defines the
137         /// maximum size of \param os. Omits the label of the first paragraph.
138         void forOutliner(docstring & os, size_t maxlen, pit_type start, pit_type end,
139                          bool shorten = true) const;
140
141         /// insert a character at cursor position
142         /// FIXME: replace Cursor with DocIterator.
143         void insertChar(Cursor & cur, char_type c);
144         /// insert an inset at cursor position
145         /// FIXME: replace Cursor with DocIterator.
146         void insertInset(Cursor & cur, Inset * inset);
147
148         /// try to handle that request
149         /// FIXME: replace Cursor with DocIterator.
150         void dispatch(Cursor & cur, FuncRequest & cmd);
151         /// do we want to handle this event?
152         bool getStatus(Cursor & cur, FuncRequest const & cmd,
153                 FuncStatus & status) const;
154
155         /// read-only access to individual paragraph
156         Paragraph const & getPar(pit_type pit) const { return pars_[pit]; }
157         /// read-write access to individual paragraph
158         Paragraph & getPar(pit_type pit) { return pars_[pit]; }
159         // Returns the current font and depth as a message.
160         /// FIXME: replace Cursor with DocIterator.
161         docstring currentState(Cursor const & cur) const;
162
163         /** Find the word under \c from in the relative location
164          *  defined by \c word_location.
165          *  @param from return here the start of the word
166          *  @param to return here the end of the word
167          */
168         void getWord(CursorSlice & from, CursorSlice & to, word_location const) const;
169         /// just selects the word the cursor is in
170         void selectWord(Cursor & cur, word_location loc);
171         /// select all text
172         void selectAll(Cursor & cur);
173         /// convenience function get the previous word or an empty string
174         docstring previousWord(CursorSlice const & sl) const;
175
176         /// what type of change operation to make
177         enum ChangeOp {
178                 ACCEPT,
179                 REJECT
180         };
181         /// accept or reject the selected change
182         void acceptOrRejectChanges(Cursor & cur, ChangeOp op);
183         /// accept the changes within the complete Text
184         void acceptChanges();
185         /// reject the changes within the complete Text
186         void rejectChanges();
187
188         /// returns true if par was empty and was removed
189         bool setCursor(Cursor & cur, pit_type pit, pos_type pos,
190                        bool setfont = true, bool boundary = false);
191         ///
192         void setCursorIntern(Cursor & cur, pit_type pit,
193                  pos_type pos, bool setfont = true, bool boundary = false);
194
195         /// Move cursor one position backwards
196         /**
197          * Returns true if an update is needed after the move.
198          */
199         bool cursorBackward(Cursor & cur);
200         /// Move cursor visually one position to the left
201         /**
202          * \param skip_inset if true, don't enter insets
203          * Returns true if an update is needed after the move.
204          */
205         bool cursorVisLeft(Cursor & cur, bool skip_inset = false);
206         /// Move cursor one position forward
207         /**
208          * Returns true if an update is needed after the move.
209          */
210         bool cursorForward(Cursor & cur);
211         /// Move cursor visually one position to the right
212         /**
213          * \param skip_inset if true, don't enter insets
214          * Returns true if an update is needed after the move.
215          */
216         bool cursorVisRight(Cursor & cur, bool skip_inset = false);
217         ///
218         bool cursorBackwardOneWord(Cursor & cur);
219         ///
220         bool cursorForwardOneWord(Cursor & cur);
221         ///
222         bool cursorVisLeftOneWord(Cursor & cur);
223         ///
224         bool cursorVisRightOneWord(Cursor & cur);
225         /// Delete from cursor up to the end of the current or next word.
226         /// Use force to skip the confirmDeletion check.
227         void deleteWordForward(Cursor & cur, bool force = false);
228         /// Delete from cursor to start of current or prior word.
229         void deleteWordBackward(Cursor & cur, bool force = false);
230         ///
231         bool cursorUpParagraph(Cursor & cur);
232         ///
233         bool cursorDownParagraph(Cursor & cur);
234         ///
235         bool cursorTop(Cursor & cur);
236         ///
237         bool cursorBottom(Cursor & cur);
238         /// Erase character at cursor. Honour change tracking
239         /// FIXME: replace Cursor with DocIterator.
240         bool erase(Cursor & cur);
241         /// Delete character before cursor. Honour CT
242         /// FIXME: replace Cursor with DocIterator.
243         bool backspace(Cursor & cur);
244         // Dissolve the inset under cursor
245         /// FIXME: replace Cursor with DocIterator.
246         bool dissolveInset(Cursor & cur);
247         ///
248         bool selectWordWhenUnderCursor(Cursor & cur, word_location);
249         /// Change the case of the word at cursor position.
250         void changeCase(Cursor & cur, TextCase action, bool partial);
251         /// Transposes the character at the cursor with the one before it
252         void charsTranspose(Cursor & cur);
253
254         /** the DTP switches for paragraphs. LyX will store the top settings
255          always in the first physical paragraph, the bottom settings in the
256          last. When a paragraph is broken, the top settings rest, the bottom
257          settings are given to the new one.
258          This function will handle a multi-paragraph selection.
259          */
260         void setParagraphs(Cursor & cur, docstring arg, bool modify = false);
261         /// Sets parameters for current or selected paragraphs
262         void setParagraphs(Cursor & cur, ParagraphParameters const & p);
263
264         /* these things are for search and replace */
265
266         /// needed to insert the selection
267         void insertStringAsLines(Cursor & cur, docstring const & str,
268                 Font const & font);
269         /// needed to insert the selection
270         void insertStringAsParagraphs(Cursor & cur, docstring const & str,
271                 Font const & font);
272
273         /// access to our paragraphs
274         ParagraphList const & paragraphs() const { return pars_; }
275         ParagraphList & paragraphs() { return pars_; }
276         /// return true if this is the main text
277         bool isMainText() const;
278
279         ///
280         double spacing(Paragraph const & par) const;
281         /// make a suggestion for a label
282         /// FIXME: replace Cursor with DocIterator.
283         docstring getPossibleLabel(Cursor const & cur) const;
284         /// is this paragraph right-to-left?
285         bool isRTL(Paragraph const & par) const;
286
287         ///
288         bool checkAndActivateInset(Cursor & cur, bool front);
289         ///
290         bool checkAndActivateInsetVisual(Cursor & cur, bool movingForward, bool movingLeft);
291
292         ///
293         void write(std::ostream & os) const;
294         /// returns true if \end_document has not been read
295         /// insetPtr is the containing Inset
296         bool read(Lexer & lex, ErrorList & errorList,
297                   InsetText * insetPtr);
298
299         /// delete double spaces, leading spaces, and empty paragraphs around old cursor.
300         /// \retval true if a change has happened and we need a redraw.
301         /// FIXME: replace Cursor with DocIterator. This is not possible right
302         /// now because recordUndo() is called which needs a Cursor.
303         static bool deleteEmptyParagraphMechanism(Cursor & cur,
304                 Cursor & old, bool & need_anchor_change);
305
306         /// delete double spaces, leading spaces, and empty paragraphs
307         /// from \first to \last paragraph
308         void deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool trackChanges);
309
310         /// To resolve macros properly the texts get their DocIterator.
311         /// Every macro definition is stored with its DocIterator
312         /// as well. Only those macros with a smaller iterator become
313         /// visible in a paragraph.
314         DocIterator macrocontextPosition() const;
315         ///
316         void setMacrocontextPosition(DocIterator const & pos);
317
318         ///
319         bool completionSupported(Cursor const & cur) const;
320         ///
321         CompletionList const * createCompletionList(Cursor const & cur) const;
322         ///
323         bool insertCompletion(Cursor & cur, docstring const & s, bool /*finished*/);
324         ///
325         docstring completionPrefix(Cursor const & cur) const;
326         /// find a paragraph before \p par with the given \p depth, if such
327         /// a paragraph cannot be found, \p par is returned
328         pit_type depthHook(pit_type pit, depth_type depth) const;
329         /// find a paragraph before \p par with depth less than the
330         /// depth of \p par. If such paragraph cannot be found because
331         /// \p par already has depth 0, lastpar + 1 is returned. If
332         /// such paragraph cannot be found because there isn't a par
333         /// with less depth before this one, \p par is returned.
334         pit_type outerHook(pit_type pit) const;
335         /// Is it the first par with same depth and layout?
336         bool isFirstInSequence(pit_type pit) const;
337         /// Return the last paragraph with same depth and layout, or a strictly
338         /// greater depth
339         pit_type lastInSequence(pit_type pit) const;
340         /// Is this paragraph in the table of contents?
341         int getTocLevel(pit_type pit) const;
342         /// Get the font of the "environment" of paragraph \p par_offset in \p pars.
343         /// All font changes of the paragraph are relative to this font.
344         Font const outerFont(pit_type pit_offset) const;
345
346 private:
347         /// The InsetText owner shall have access to everything.
348         friend class InsetText;
349
350         // fix the cursor `cur' after a characters has been deleted at `where'
351         // position. Called by deleteEmptyParagraphMechanism
352         static void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
353
354         // At cursor position 0, try to merge the paragraph with the one before it.
355         // Ignore change tracking, i.e., physically remove the end-of-par character
356         bool backspacePos0(Cursor & cur);
357         /// handle the case where bibitems were deleted
358         bool handleBibitems(Cursor & cur);
359         /// are we in a list item (description etc.)?
360         bool inDescriptionItem(Cursor & cur) const;
361         ///
362         void charInserted(Cursor & cur);
363         /// set 'number' font property
364         void number(Cursor & cur);
365
366         /// paste plain text at current cursor.
367         /// \param str string to paste
368         /// \param asParagraphs whether to paste as paragraphs or as lines
369         void pasteString(Cursor & cur, docstring const & str,
370                         bool asParagraphs);
371         ///
372         void readParToken(Paragraph & par, Lexer & lex, std::string const & token,
373                 Font & font, Change & change, ErrorList & errorList);
374         ///
375         void readParagraph(Paragraph & par, Lexer & lex, ErrorList & errorList);
376         /// Set Label Width string to all paragraphs of the same layout
377         /// and depth in a sequence.
378         void setLabelWidthStringToSequence(Cursor const & cur, docstring const & s);
379
380         /// Owner Inset.
381         InsetText * owner_;
382         ///
383         ParagraphList pars_;
384         /// position of the text in the buffer.
385         DocIterator macrocontext_position_;
386 };
387
388
389 ///
390 void breakParagraphConservative(BufferParams const & bparams,
391                                 ParagraphList & paragraphs,
392                                 pit_type pit,
393                                 pos_type pos);
394
395 /**
396  * Append the next paragraph onto the tail of this one.
397  * Be careful, this doesent make any check at all.
398  */
399 void mergeParagraph(BufferParams const & bparams,
400         ParagraphList & paragraphs, pit_type pit);
401
402 /// accept the changes within the complete ParagraphList
403 void acceptChanges(ParagraphList & pars, BufferParams const & bparams);
404
405 /// reject the changes within the complete ParagraphList
406 void rejectChanges(ParagraphList & pars, BufferParams const & bparams);
407
408 } // namespace lyx
409
410 #endif // TEXT_H