]> git.lyx.org Git - features.git/blob - src/Text.h
Fix crash when the Outline dialog is opened on an empty document. This was caused...
[features.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 "Bidi.h"
18 #include "DispatchResult.h"
19 #include "Font.h"
20 #include "Layout.h"
21 #include "lyxlayout_ptr_fwd.h"
22 #include "ParagraphList.h"
23
24 #include <iosfwd>
25
26
27 namespace lyx {
28
29 class Buffer;
30 class BufferParams;
31 class BufferView;
32 class CursorSlice;
33 class DocIterator;
34 class ErrorList;
35 class FuncRequest;
36 class FuncStatus;
37 class Inset;
38 class Color_color;
39 class Cursor;
40 class PainterInfo;
41 class Row;
42 class RowMetrics;
43 class Spacing;
44
45
46 /// This class encapsulates the main text data and operations in LyX
47 class Text {
48 public:
49         /// constructor
50         explicit Text();
51
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
57         ///
58         Font getFont(Buffer const & buffer, Paragraph const & par,
59                 pos_type pos) const;
60         ///
61         void applyOuterFont(Buffer const & buffer, Font &) const;
62         ///
63         Font getLayoutFont(Buffer const & buffer, pit_type pit) const;
64         ///
65         Font getLabelFont(Buffer const & buffer,
66                 Paragraph const & par) const;
67         /** Set font of character at position \p pos in paragraph \p pit.
68          *  Must not be called if \p pos denotes an inset with text contents,
69          *  and the inset is not allowed inside a font change (see below).
70          */
71         void setCharFont(Buffer const & buffer, pit_type pit, pos_type pos,
72                 Font const & font);
73
74         /** Needed to propagate font changes to all text cells of insets
75          *  that are not allowed inside a font change (bug 1973).
76          *  Must not be called if \p pos denotes an ordinary character or an
77          *  inset that is alowed inside a font change.
78          *  FIXME: This should be removed, see documentation of noFontChange
79          *  in insetbase.h
80          */
81         void setInsetFont(Buffer const & buffer, pit_type pit, pos_type pos,
82                 Font const & font, bool toggleall = false);
83
84         /// what you expect when pressing \<enter\> at cursor position
85         void breakParagraph(Cursor & cur, bool keep_layout = false);
86
87         /// set layout over selection
88         void setLayout(Buffer const & buffer, pit_type start, pit_type end,
89                 docstring const & layout);
90         /// Set given layout to current cursor position.
91         /// FIXME: replace Cursor with DocIterator.
92         void setLayout(Cursor & cur, docstring const & layout);
93
94         /// what type of depth change to make
95         enum DEPTH_CHANGE {
96                 INC_DEPTH,
97                 DEC_DEPTH
98         };
99         /// Increase or decrease the nesting depth of the selected paragraph(s)
100         /// FIXME: replace Cursor with DocIterator.
101         void changeDepth(Cursor & cur, DEPTH_CHANGE type);
102
103         /// Returns whether something would be changed by changeDepth
104         /// FIXME: replace Cursor with DocIterator.
105         bool changeDepthAllowed(Cursor & cur, DEPTH_CHANGE type) const;
106
107         /// Set font over selection paragraphs and rebreak.
108         /// FIXME: replace Cursor with DocIterator.
109         void setFont(Cursor & cur, Font const &, bool toggleall = false);
110         /// Set font from \p begin to \p end and rebreak.
111         void setFont(Buffer const & buffer, CursorSlice const & begin,
112                 CursorSlice const & end, Font const &,
113                 bool toggleall = false);
114
115         ///
116         void toggleFree(Cursor & cur, Font const &, bool toggleall = false);
117
118         /// ???
119         /// FIXME: replace Cursor with DocIterator.
120         docstring getStringToIndex(Cursor const & cur);
121
122         /// insert a character at cursor position
123         /// FIXME: replace Cursor with DocIterator.
124         void insertChar(Cursor & cur, char_type c);
125         /// insert an inset at cursor position
126         /// FIXME: replace Cursor with DocIterator.
127         void insertInset(Cursor & cur, Inset * inset);
128
129         /// draw text (only used for insets)
130         void draw(PainterInfo & pi, int x, int y) const;
131         /// draw textselection
132         void drawSelection(PainterInfo & pi, int x, int y) const;
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 & cur);
148
149         /** returns row near the specified
150           * y-coordinate in given paragraph (relative to the screen).
151           */
152         /// FIXME: move to TextMetrics.
153         Row const & getRowNearY(BufferView const & bv, int y,
154                 pit_type pit) const;
155
156         /// returns the paragraph number closest to screen y-coordinate.
157         /// This method uses the BufferView CoordCache to locate the
158         /// paragraph. The y-coodinate is allowed to be off-screen and
159         /// the CoordCache will be automatically updated if needed. This is
160         /// the reason why we need a non const BufferView.
161         /// FIXME: move to TextMetrics.
162         pit_type getPitNearY(BufferView & bv, int y) const;
163
164         /** Find the word under \c from in the relative location
165          *  defined by \c word_location.
166          *  @param from return here the start of the word
167          *  @param to return here the end of the word
168          */
169         void getWord(CursorSlice & from, CursorSlice & to, word_location const);
170         /// just selects the word the cursor is in
171         void selectWord(Cursor & cur, word_location loc);
172
173         /// what type of change operation to make
174         enum ChangeOp {
175                 ACCEPT,
176                 REJECT
177         };
178         /// accept or reject the selected change
179         void acceptOrRejectChanges(Cursor & cur, ChangeOp op);
180         /// accept the changes within the complete Text
181         void acceptChanges(BufferParams const & bparams);
182         /// reject the changes within the complete Text
183         void rejectChanges(BufferParams const & bparams);
184
185         /// returns true if par was empty and was removed
186         bool setCursor(Cursor & cur, pit_type par, pos_type pos,
187                        bool setfont = true, bool boundary = false);
188         ///
189         void setCursor(CursorSlice &, pit_type par, pos_type pos);
190         ///
191         void setCursorIntern(Cursor & cur, pit_type par,
192                  pos_type pos, bool setfont = true, bool boundary = false);
193         ///
194         void setCurrentFont(Cursor & cur);
195
196         ///
197         void recUndo(Cursor & cur, pit_type first, pit_type last) const;
198         ///
199         void recUndo(Cursor & cur, pit_type first) const;
200
201         /// sets cursor only within this Text.
202         /// x,y are screen coordinates
203         void setCursorFromCoordinates(Cursor & cur, int x, int y);
204
205         /// sets cursor recursively descending into nested editable insets
206         /**
207         \return the inset pointer if x,y is covering that inset
208         \param x,y are absolute screen coordinates.
209         \retval inset is non-null if the cursor is positionned inside
210         */
211         /// FIXME: move to TextMetrics.
212         /// FIXME: cleanup to use BufferView::getCoveringInset() and
213         /// setCursorFromCoordinates() instead of checkInsetHit().
214         Inset * editXY(Cursor & cur, int x, int y);
215
216         /// Move cursor one position left
217         /**
218          * Returns true if an update is needed after the move.
219          */
220         bool cursorLeft(Cursor & cur);
221         /// Move cursor one position right
222         /**
223          * Returns true if an update is needed after the move.
224          */
225         bool cursorRight(Cursor & cur);
226         ///
227         bool cursorLeftOneWord(Cursor & cur);
228         ///
229         bool cursorRightOneWord(Cursor & cur);
230         ///
231         bool cursorUpParagraph(Cursor & cur);
232         ///
233         bool cursorDownParagraph(Cursor & cur);
234         ///
235         /// FIXME: move to TextMetrics.
236         bool cursorHome(Cursor & cur);
237         ///
238         /// FIXME: move to TextMetrics.
239         bool cursorEnd(Cursor & cur);
240         ///
241         void cursorPrevious(Cursor & cur);
242         ///
243         void cursorNext(Cursor & cur);
244         ///
245         bool cursorTop(Cursor & cur);
246         ///
247         bool cursorBottom(Cursor & cur);
248         /// Erase character at cursor. Honour change tracking
249         /// FIXME: replace Cursor with DocIterator.
250         bool erase(Cursor & cur);
251         /// Delete character before cursor. Honour CT
252         /// FIXME: replace Cursor with DocIterator.
253         bool backspace(Cursor & cur);
254         // Dissolve the inset under cursor
255         /// FIXME: replace Cursor with DocIterator.
256         bool dissolveInset(Cursor & cur);
257         ///
258         bool selectWordWhenUnderCursor(Cursor & cur, word_location);
259         ///
260         enum TextCase {
261                 ///
262                 text_lowercase = 0,
263                 ///
264                 text_capitalization = 1,
265                 ///
266                 text_uppercase = 2
267         };
268         /// Change the case of the word at cursor position.
269         void changeCase(Cursor & cur, TextCase action);
270         /// Transposes the character at the cursor with the one before it
271         void charsTranspose(Cursor & cur);
272
273         /** the DTP switches for paragraphs. LyX will store the top settings
274          always in the first physical paragraph, the bottom settings in the
275          last. When a paragraph is broken, the top settings rest, the bottom
276          settings are given to the new one.
277          */
278         void setParagraph(Cursor & cur,
279                           Spacing const & spacing,
280                           LyXAlignment align,
281                           docstring const & labelwidthstring,
282                           bool noindent);
283
284         /* these things are for search and replace */
285
286         /// needed to insert the selection
287         /// FIXME: replace Cursor with DocIterator.
288         void insertStringAsLines(Cursor & cur, docstring const & str);
289         /// needed to insert the selection
290         /// FIXME: replace Cursor with DocIterator.
291         void insertStringAsParagraphs(Cursor & cur, docstring const & str);
292
293         /// Returns an inset if inset was hit, or 0 if not.
294         /// \warning This method is not recursive! It will return the
295         /// outermost inset within this Text.
296         /// \sa BufferView::getCoveringInset() to get the innermost inset.
297         Inset * checkInsetHit(BufferView &, int x, int y);
298
299         ///
300         /// FIXME: move to TextMetrics.
301         int singleWidth(Buffer const &, Paragraph const & par,
302                 pos_type pos) const;
303         ///
304         /// FIXME: move to TextMetrics.
305         int singleWidth(Paragraph const & par, pos_type pos, char_type c,
306                 Font const & Font) const;
307
308         /// return the color of the canvas
309         Color_color backgroundColor() const;
310
311         /**
312          * Returns the left beginning of the text.
313          * This information cannot be taken from the layout object, because
314          * in LaTeX the beginning of the text fits in some cases
315          * (for example sections) exactly the label-width.
316          */
317         /// FIXME: move to TextMetrics.
318         int leftMargin(Buffer const &, int max_width, pit_type pit, pos_type pos) const;
319         int leftMargin(Buffer const &, int max_width, pit_type pit) const;
320
321         /// access to our paragraphs
322         ParagraphList const & paragraphs() const { return pars_; }
323         ParagraphList & paragraphs() { return pars_; }
324         /// return true if this is the main text
325         bool isMainText(Buffer const &) const;
326
327         /// is this row the last in the text?
328         /// FIXME: move to TextMetrics.
329         bool isLastRow(pit_type pit, Row const & row) const;
330         /// is this row the first in the text?
331         /// FIXME: move to TextMetrics.
332         bool isFirstRow(pit_type pit, Row const & row) const;
333
334         ///
335         double spacing(Buffer const & buffer, Paragraph const & par) const;
336         /// make a suggestion for a label
337         /// FIXME: replace Cursor with DocIterator.
338         docstring getPossibleLabel(Cursor & cur) const;
339         /// is this paragraph right-to-left?
340         bool isRTL(Buffer const &, Paragraph const & par) const;
341         /// is this position in the paragraph right-to-left?
342         bool isRTL(Buffer const & buffer, CursorSlice const & sl, bool boundary) const;
343         /// is between pos-1 and pos an RTL<->LTR boundary?
344         bool isRTLBoundary(Buffer const & buffer, Paragraph const & par,
345           pos_type pos) const;
346         /// would be a RTL<->LTR boundary between pos and the given font?
347         bool isRTLBoundary(Buffer const & buffer, Paragraph const & par,
348           pos_type pos, Font const & font) const;
349
350         ///
351         bool checkAndActivateInset(Cursor & cur, bool front);
352
353         ///
354         void write(Buffer const & buf, std::ostream & os) const;
355         /// returns whether we've seen our usual 'end' marker
356         bool read(Buffer const & buf, Lexer & lex, ErrorList & errorList);
357
358         ///
359         /// FIXME: move to TextMetrics.
360         int cursorX(BufferView const &, CursorSlice const & cursor,
361                 bool boundary) const;
362         ///
363         /// FIXME: move to TextMetrics.
364         int cursorY(BufferView const & bv, CursorSlice const & cursor,
365                 bool boundary) const;
366
367         /// delete double spaces, leading spaces, and empty paragraphs around old cursor.
368         /// \retval true if a change has happened and we need a redraw.
369         /// FIXME: replace Cursor with DocIterator. This is not possible right
370         /// now because recordUndo() is called which needs a Cursor.
371         static bool deleteEmptyParagraphMechanism(Cursor & cur,
372                 Cursor & old, bool & need_anchor_change);
373
374         /// delete double spaces, leading spaces, and empty paragraphs
375         /// from \first to \last paragraph
376         void deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool trackChanges);
377
378 public:
379         /// the current font settings
380         Font current_font;
381         /// the current font
382         Font real_current_font;
383         ///
384         int background_color_;
385
386         ///
387         ParagraphList pars_;
388
389         /// our 'outermost' font. This is handed down from the surrounding
390         // inset through the pi/mi parameter (pi.base.font)
391         Font font_;
392
393         ///
394         bool autoBreakRows_;
395 private:
396         /// return past-the-last paragraph influenced by a layout
397         /// change on pit
398         pit_type undoSpan(pit_type pit);
399
400         // fix the cursor `cur' after a characters has been deleted at `where'
401         // position. Called by deleteEmptyParagraphMechanism
402         static void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
403
404         // At cursor position 0, try to merge the paragraph with the one before it.
405         // Ignore change tracking, i.e., physically remove the end-of-par character
406         bool backspacePos0(Cursor & cur);
407         /// handle the case where bibitems were deleted
408         bool handleBibitems(Cursor & cur);
409
410         ///
411         void deleteWordForward(Cursor & cur);
412         ///
413         void deleteWordBackward(Cursor & cur);
414         ///
415         void deleteLineForward(Cursor & cur);
416         ///
417         void charInserted();
418         /// set 'number' font property
419         void number(Cursor & cur);
420         /// draw selection for a single row
421         void drawRowSelection(PainterInfo & pi, int x, Row const & row,
422                 DocIterator const & beg, DocIterator const & end, 
423                 bool drawOnBegMargin, bool drawOnEndMargin) const;
424
425         /// paste plain text at current cursor.
426         /// \param str string to paste
427         /// \param asParagraphs whether to paste as paragraphs or as lines
428         void pasteString(Cursor & cur, docstring const & str,
429                         bool asParagraphs);
430 };
431
432 } // namespace lyx
433
434 #endif // LYXTEXT_H