]> git.lyx.org Git - lyx.git/blob - src/Text.h
rev 19644: I forgot this.
[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 "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          This function will handle a multi-paragraph selection.
278          */
279         void setParagraphs(Cursor & cur, docstring arg, bool modify = false);
280         /// Sets parameters for current or selected paragraphs
281         void setParagraphs(Cursor & cur, ParagraphParameters const & p);
282
283         /* these things are for search and replace */
284
285         /// needed to insert the selection
286         /// FIXME: replace Cursor with DocIterator.
287         void insertStringAsLines(Cursor & cur, docstring const & str);
288         /// needed to insert the selection
289         /// FIXME: replace Cursor with DocIterator.
290         void insertStringAsParagraphs(Cursor & cur, docstring const & str);
291
292         /// Returns an inset if inset was hit, or 0 if not.
293         /// \warning This method is not recursive! It will return the
294         /// outermost inset within this Text.
295         /// \sa BufferView::getCoveringInset() to get the innermost inset.
296         Inset * checkInsetHit(BufferView &, int x, int y);
297
298         /// return the color of the canvas
299         Color_color backgroundColor() const;
300
301         /**
302          * Returns the left beginning of the text.
303          * This information cannot be taken from the layout object, because
304          * in LaTeX the beginning of the text fits in some cases
305          * (for example sections) exactly the label-width.
306          */
307         /// FIXME: move to TextMetrics.
308         int leftMargin(Buffer const &, int max_width, pit_type pit, pos_type pos) const;
309         int leftMargin(Buffer const &, int max_width, pit_type pit) const;
310
311         /// access to our paragraphs
312         ParagraphList const & paragraphs() const { return pars_; }
313         ParagraphList & paragraphs() { return pars_; }
314         /// return true if this is the main text
315         bool isMainText(Buffer const &) const;
316
317         /// is this row the last in the text?
318         /// FIXME: move to TextMetrics.
319         bool isLastRow(pit_type pit, Row const & row) const;
320         /// is this row the first in the text?
321         /// FIXME: move to TextMetrics.
322         bool isFirstRow(pit_type pit, Row const & row) const;
323
324         ///
325         double spacing(Buffer const & buffer, Paragraph const & par) const;
326         /// make a suggestion for a label
327         /// FIXME: replace Cursor with DocIterator.
328         docstring getPossibleLabel(Cursor & cur) const;
329         /// is this paragraph right-to-left?
330         bool isRTL(Buffer const &, Paragraph const & par) const;
331         /// is this position in the paragraph right-to-left?
332         bool isRTL(Buffer const & buffer, CursorSlice const & sl, bool boundary) const;
333         /// is between pos-1 and pos an RTL<->LTR boundary?
334         bool isRTLBoundary(Buffer const & buffer, Paragraph const & par,
335           pos_type pos) const;
336         /// would be a RTL<->LTR boundary between pos and the given font?
337         bool isRTLBoundary(Buffer const & buffer, Paragraph const & par,
338           pos_type pos, Font const & font) const;
339
340         ///
341         bool checkAndActivateInset(Cursor & cur, bool front);
342
343         ///
344         void write(Buffer const & buf, std::ostream & os) const;
345         /// returns whether we've seen our usual 'end' marker
346         bool read(Buffer const & buf, Lexer & lex, ErrorList & errorList);
347
348         ///
349         /// FIXME: move to TextMetrics.
350         int cursorX(BufferView const &, CursorSlice const & cursor,
351                 bool boundary) const;
352         ///
353         /// FIXME: move to TextMetrics.
354         int cursorY(BufferView const & bv, CursorSlice const & cursor,
355                 bool boundary) const;
356
357         /// delete double spaces, leading spaces, and empty paragraphs around old cursor.
358         /// \retval true if a change has happened and we need a redraw.
359         /// FIXME: replace Cursor with DocIterator. This is not possible right
360         /// now because recordUndo() is called which needs a Cursor.
361         static bool deleteEmptyParagraphMechanism(Cursor & cur,
362                 Cursor & old, bool & need_anchor_change);
363
364         /// delete double spaces, leading spaces, and empty paragraphs
365         /// from \first to \last paragraph
366         void deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool trackChanges);
367
368 public:
369         /// the current font settings
370         Font current_font;
371         /// the current font
372         Font real_current_font;
373         ///
374         int background_color_;
375
376         ///
377         ParagraphList pars_;
378
379         /// our 'outermost' font. This is handed down from the surrounding
380         // inset through the pi/mi parameter (pi.base.font)
381         Font font_;
382
383         ///
384         bool autoBreakRows_;
385 private:
386         /// return past-the-last paragraph influenced by a layout
387         /// change on pit
388         pit_type undoSpan(pit_type pit);
389
390         // fix the cursor `cur' after a characters has been deleted at `where'
391         // position. Called by deleteEmptyParagraphMechanism
392         static void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
393
394         // At cursor position 0, try to merge the paragraph with the one before it.
395         // Ignore change tracking, i.e., physically remove the end-of-par character
396         bool backspacePos0(Cursor & cur);
397         /// handle the case where bibitems were deleted
398         bool handleBibitems(Cursor & cur);
399
400         ///
401         void deleteWordForward(Cursor & cur);
402         ///
403         void deleteWordBackward(Cursor & cur);
404         ///
405         void deleteLineForward(Cursor & cur);
406         ///
407         void charInserted();
408         /// set 'number' font property
409         void number(Cursor & cur);
410         /// draw selection for a single row
411         void drawRowSelection(PainterInfo & pi, int x, Row const & row,
412                 DocIterator const & beg, DocIterator const & end, 
413                 bool drawOnBegMargin, bool drawOnEndMargin) const;
414
415         /// paste plain text at current cursor.
416         /// \param str string to paste
417         /// \param asParagraphs whether to paste as paragraphs or as lines
418         void pasteString(Cursor & cur, docstring const & str,
419                         bool asParagraphs);
420 };
421
422 } // namespace lyx
423
424 #endif // LYXTEXT_H