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