]> git.lyx.org Git - lyx.git/blob - src/lyxtext.h
cleanup after svn hang-up, #undef CursorShape. Should be compilable ganin now.
[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         // Dissolve the inset under cursor
236         bool dissolveInset(LCursor & cur);
237         ///
238         bool selectWordWhenUnderCursor(LCursor & cur, lyx::word_location);
239         ///
240         enum TextCase {
241                 ///
242                 text_lowercase = 0,
243                 ///
244                 text_capitalization = 1,
245                 ///
246                 text_uppercase = 2
247         };
248         /// Change the case of the word at cursor position.
249         void changeCase(LCursor & cur, TextCase action);
250
251         /** the DTP switches for paragraphs. LyX will store the top settings
252          always in the first physical paragraph, the bottom settings in the
253          last. When a paragraph is broken, the top settings rest, the bottom
254          settings are given to the new one.
255          */
256         void setParagraph(LCursor & cur,
257                           Spacing const & spacing,
258                           LyXAlignment align,
259                           std::string const & labelwidthstring,
260                           bool noindent);
261
262         /* these things are for search and replace */
263
264         /// needed to insert the selection
265         void insertStringAsLines(LCursor & cur, lyx::docstring const & str);
266         /// needed to insert the selection
267         void insertStringAsParagraphs(LCursor & cur, lyx::docstring const & str);
268
269         /// current text width
270         int width() const;
271
272         /// current text heigth
273         int height() const;
274
275         /// Returns an inset if inset was hit, or 0 if not.
276         InsetBase * checkInsetHit(int x, int y) const;
277
278         ///
279         int singleWidth(Paragraph const & par, pos_type pos) const;
280         ///
281         int singleWidth(Paragraph const & par,
282                 pos_type pos, char_type c, LyXFont const & Font) const;
283
284         /// return the color of the canvas
285         LColor_color backgroundColor() const;
286
287         /**
288          * Returns the left beginning of the text.
289          * This information cannot be taken from the layout object, because
290          * in LaTeX the beginning of the text fits in some cases
291          * (for example sections) exactly the label-width.
292          */
293         int leftMargin(pit_type pit, pos_type pos) const;
294         int leftMargin(pit_type pit) const;
295         ///
296         int rightMargin(Paragraph const & par) const;
297
298         /** this calculates the specified parameters. needed when setting
299          * the cursor and when creating a visible row */
300         RowMetrics computeRowMetrics(pit_type pit, Row const & row) 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() const;
307
308         /// return first row of text
309         Row const & firstRow() const;
310
311         /// is this row the last in the text?
312         bool isLastRow(pit_type pit, Row const & row) const;
313         /// is this row the first in the text?
314         bool isFirstRow(pit_type pit, Row const & row) const;
315
316         ///
317         double spacing(Paragraph const & par) const;
318         /// make a suggestion for a label
319         std::string getPossibleLabel(LCursor & cur) const;
320         /// is this paragraph right-to-left?
321         bool isRTL(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         int ascent() const;
332         ///
333         int descent() const;
334         ///
335         int cursorX(CursorSlice const & cursor, bool boundary) const;
336         ///
337         int cursorY(CursorSlice const & cursor, bool boundary) const;
338
339         /// delete double space or empty paragraphs around old cursor
340         bool deleteEmptyParagraphMechanism(LCursor & cur, LCursor & old);
341
342 public:
343         ///
344         Dimension dim_;
345         ///
346         int maxwidth_;
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         /// only the top-level LyXText has this non-zero
355         BufferView * bv_owner;
356
357         ///
358         mutable Bidi bidi;
359         ///
360         ParagraphList pars_;
361
362         /// our 'outermost' font. This is handed down from the surrounding
363         // inset through the pi/mi parameter (pi.base.font)
364         LyXFont font_;
365
366         ///
367         bool autoBreakRows_;
368 private:
369         /// return past-the-last paragraph influenced by a layout
370         /// change on pit
371         pit_type undoSpan(pit_type pit);
372
373         /// Calculate and set the height of the row
374         void setHeightOfRow(pit_type, Row & row);
375
376         // fix the cursor `cur' after a characters has been deleted at `where'
377         // position. Called by deleteEmptyParagraphMechanism
378         void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
379
380         ///
381         void deleteWordForward(LCursor & cur);
382         ///
383         void deleteWordBackward(LCursor & cur);
384         ///
385         void deleteLineForward(LCursor & cur);
386
387         /// sets row.end to the pos value *after* which a row should break.
388         /// for example, the pos after which isNewLine(pos) == true
389         void rowBreakPoint(pit_type pit, Row & row) const;
390         /// sets row.width to the minimum space a row needs on the screen in pixel
391         void setRowWidth(pit_type pit, Row & row) const;
392         /// the minimum space a manual label needs on the screen in pixels
393         int labelFill(Paragraph const & par, Row const & row) const;
394         /// FIXME
395         int labelEnd(pit_type pit) const;
396
397         ///
398         void charInserted();
399         /// set 'number' font property
400         void number(LCursor & cur);
401 };
402
403 /// return the default height of a row in pixels, considering font zoom
404 int defaultRowHeight();
405
406 #endif // LYXTEXT_H