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