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