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