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