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