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