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