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