]> git.lyx.org Git - lyx.git/blob - src/lyxtext.h
* Painter.h:
[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
29 namespace lyx {
30
31 class Buffer;
32 class BufferParams;
33 class BufferView;
34 class CursorSlice;
35 class Dimension;
36 class ErrorList;
37 class InsetBase;
38 class InsetBase_code;
39 class FuncRequest;
40 class FuncStatus;
41 class LColor_color;
42 class LCursor;
43 class LyXTextClass;
44 class MetricsInfo;
45 class PainterInfo;
46 class Row;
47 class RowMetrics;
48 class Spacing;
49
50
51 /// This class encapsulates the main text data and operations in LyX
52 class LyXText {
53 public:
54         /// constructor
55         explicit LyXText(BufferView *);
56         ///
57         void init(BufferView *);
58
59         ///
60         LyXFont getFont(Paragraph const & par, pos_type pos) const;
61         ///
62         void applyOuterFont(LyXFont &) const;
63         ///
64         LyXFont getLayoutFont(pit_type pit) const;
65         ///
66         LyXFont getLabelFont(Paragraph const & par) const;
67         ///
68         void setCharFont(pit_type pit, pos_type pos, LyXFont const & font);
69         ///
70         void setCharFont(pit_type pit, pos_type pos, LyXFont const & font,
71                 bool toggleall);
72
73         /// what you expect when pressing <enter> at cursor position
74         void breakParagraph(LCursor & cur, bool keep_layout = false);
75
76         /// set layout over selection
77         void setLayout(pit_type start, pit_type end,
78                 std::string const & layout);
79         ///
80         void setLayout(LCursor & cur, std::string const & layout);
81
82         /// what type of depth change to make
83         enum DEPTH_CHANGE {
84                 INC_DEPTH,
85                 DEC_DEPTH
86         };
87         /// Increase or decrease the nesting depth of the selected paragraph(s)
88         void changeDepth(LCursor & cur, DEPTH_CHANGE type);
89
90         /// Returns whether something would be changed by changeDepth
91         bool changeDepthAllowed(LCursor & cur, DEPTH_CHANGE type) const;
92
93         /// Set font over selection paragraphs and rebreak.
94         void setFont(LCursor & cur, LyXFont const &, bool toggleall = false);
95
96         /// rebreaks the given par
97         bool redoParagraph(pit_type pit);
98
99         /// returns pos in given par at given x coord
100         pos_type x2pos(pit_type pit, int row, int x) const;
101         int pos2x(pit_type pit, pos_type pos) const;
102
103         ///
104         void toggleFree(LCursor & cur, LyXFont const &, bool toggleall = false);
105
106         ///
107         std::string getStringToIndex(LCursor const & cur);
108
109         /// insert a character at cursor position
110         void insertChar(LCursor & cur, char_type c);
111         /// insert an inset at cursor position
112         void insertInset(LCursor & cur, InsetBase * inset);
113
114         /// compute text metrics
115         void metrics(MetricsInfo & mi, Dimension & dim);
116         /// draw text (only used for insets)
117         void draw(PainterInfo & pi, int x, int y) const;
118         /// draw textselection
119         void drawSelection(PainterInfo & pi, int x, int y) const;
120
121         /// try to handle that request
122         void dispatch(LCursor & cur, FuncRequest & cmd);
123         /// do we want to handle this event?
124         bool getStatus(LCursor & cur, FuncRequest const & cmd,
125                 FuncStatus & status) const;
126
127         /// access to out BufferView. This should go...
128         BufferView * bv() const;
129
130         /// read-only access to individual paragraph
131         Paragraph const & getPar(pit_type pit) const { return pars_[pit]; }
132         /// read-write access to individual paragraph
133         Paragraph & getPar(pit_type pit) { return pars_[pit]; }
134         // Returns the current font and depth as a message.
135         std::string currentState(LCursor & cur);
136
137         /** returns row near the specified
138           * y-coordinate in given paragraph (relative to the screen).
139           */
140         Row const & getRowNearY(int y, pit_type pit) const;
141         pit_type getPitNearY(int y) const;
142
143         /** returns the column near the specified x-coordinate of the row
144          x is set to the real beginning of this column
145          */
146         pos_type getColumnNearX(pit_type pit,
147                 Row const & row, int & x, bool & boundary) const;
148
149         /** Find the word under \c from in the relative location
150          *  defined by \c word_location.
151          *  @param from return here the start of the word
152          *  @param to return here the end of the word
153          */
154         void getWord(CursorSlice & from, CursorSlice & to, word_location const);
155         /// just selects the word the cursor is in
156         void selectWord(LCursor & cur, word_location loc);
157
158         /// accept selected change
159         void acceptChange(LCursor & cur);
160         /// reject selected change
161         void rejectChange(LCursor & cur);
162
163         /// returns true if par was empty and was removed
164         bool setCursor(LCursor & 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(LCursor & cur, pit_type par,
170                  pos_type pos, bool setfont = true, bool boundary = false);
171         ///
172         void setCurrentFont(LCursor & cur);
173
174         ///
175         void recUndo(pit_type first, pit_type last) const;
176         ///
177         void recUndo(pit_type first) const;
178         /// returns true if par was empty and was removed
179         bool setCursorFromCoordinates(LCursor & cur, int x, int y);
180         ///
181         InsetBase * editXY(LCursor & cur, int x, int y);
182         /// Move cursor one line up.
183         /**
184          * Returns true if an update is needed after the move.
185          */
186         bool cursorUp(LCursor & cur);
187         /// Move cursor one line down.
188         /**
189          * Returns true if an update is needed after the move.
190          */
191         bool cursorDown(LCursor & cur);
192         /// Move cursor one position left
193         /**
194          * Returns true if an update is needed after the move.
195          */
196         bool cursorLeft(LCursor & cur);
197         /// Move cursor one position right
198         /**
199          * Returns true if an update is needed after the move.
200          */
201         bool cursorRight(LCursor & cur);
202         ///
203         bool cursorLeftOneWord(LCursor & cur);
204         ///
205         bool cursorRightOneWord(LCursor & cur);
206         ///
207         bool cursorUpParagraph(LCursor & cur);
208         ///
209         bool cursorDownParagraph(LCursor & cur);
210         ///
211         bool cursorHome(LCursor & cur);
212         ///
213         bool cursorEnd(LCursor & cur);
214         ///
215         bool cursorPrevious(LCursor & cur);
216         ///
217         bool cursorNext(LCursor & cur);
218         ///
219         bool cursorTop(LCursor & cur);
220         ///
221         bool cursorBottom(LCursor & cur);
222         /// Erase character at cursor. Honour change tracking
223         bool erase(LCursor & cur);
224         /** At cursor position 0, merge paragraph with the one before it.
225          * Ignore CT (this is used in \c acceptChange, \c rejectChange for
226          * physical deletion of paragraph break)
227          */
228         bool backspacePos0(LCursor & cur);
229         /// Delete character before cursor. Honour CT
230         bool backspace(LCursor & cur);
231         // Dissolve the inset under cursor
232         bool dissolveInset(LCursor & cur);
233         ///
234         bool selectWordWhenUnderCursor(LCursor & cur, 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, docstring const & str);
262         /// needed to insert the selection
263         void insertStringAsParagraphs(LCursor & cur, docstring 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_type 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
403 } // namespace lyx
404
405 #endif // LYXTEXT_H