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