]> git.lyx.org Git - features.git/blob - src/Text.h
delete unused definition.
[features.git] / src / Text.h
1 // -*- C++ -*-
2 /**
3  * \file Text.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 TEXT_H
15 #define TEXT_H
16
17 #include "Bidi.h"
18 #include "DispatchResult.h"
19 #include "Font.h"
20 #include "Layout.h"
21 #include "lyxlayout_ptr_fwd.h"
22 #include "ParagraphList.h"
23
24 #include <iosfwd>
25
26
27 namespace lyx {
28
29 class Buffer;
30 class BufferParams;
31 class BufferView;
32 class CursorSlice;
33 class DocIterator;
34 class ErrorList;
35 class FuncRequest;
36 class FuncStatus;
37 class Inset;
38 class Color_color;
39 class Cursor;
40 class PainterInfo;
41 class Row;
42 class Spacing;
43
44
45 /// This class encapsulates the main text data and operations in LyX
46 class Text {
47 public:
48         /// constructor
49         explicit Text();
50
51         /// \return true if there's no content at all.
52         /// \warning a non standard layout on an empty paragraph doesn't
53         // count as empty.
54         bool empty() const;
55
56         ///
57         Font getFont(Buffer const & buffer, Paragraph const & par,
58                 pos_type pos) const;
59         ///
60         void applyOuterFont(Buffer const & buffer, Font &) const;
61         ///
62         Font getLayoutFont(Buffer const & buffer, pit_type pit) const;
63         ///
64         Font getLabelFont(Buffer const & buffer,
65                 Paragraph const & par) const;
66         /** Set font of character at position \p pos in paragraph \p pit.
67          *  Must not be called if \p pos denotes an inset with text contents,
68          *  and the inset is not allowed inside a font change (see below).
69          */
70         void setCharFont(Buffer const & buffer, pit_type pit, pos_type pos,
71                 Font const & font);
72
73         /** Needed to propagate font changes to all text cells of insets
74          *  that are not allowed inside a font change (bug 1973).
75          *  Must not be called if \p pos denotes an ordinary character or an
76          *  inset that is alowed inside a font change.
77          *  FIXME: This should be removed, see documentation of noFontChange
78          *  in insetbase.h
79          */
80         void setInsetFont(Buffer const & buffer, pit_type pit, pos_type pos,
81                 Font const & font, bool toggleall = false);
82
83         /// what you expect when pressing \<enter\> at cursor position
84         void breakParagraph(Cursor & cur, bool keep_layout = false);
85
86         /// set layout over selection
87         void setLayout(Buffer const & buffer, pit_type start, pit_type end,
88                 docstring const & layout);
89         /// Set given layout to current cursor position.
90         /// FIXME: replace Cursor with DocIterator.
91         void setLayout(Cursor & cur, docstring const & layout);
92
93         /// what type of depth change to make
94         enum DEPTH_CHANGE {
95                 INC_DEPTH,
96                 DEC_DEPTH
97         };
98         /// Increase or decrease the nesting depth of the selected paragraph(s)
99         /// FIXME: replace Cursor with DocIterator.
100         void changeDepth(Cursor & cur, DEPTH_CHANGE type);
101
102         /// Returns whether something would be changed by changeDepth
103         /// FIXME: replace Cursor with DocIterator.
104         bool changeDepthAllowed(Cursor & cur, DEPTH_CHANGE type) const;
105
106         /// Set font over selection paragraphs and rebreak.
107         /// FIXME: replace Cursor with DocIterator.
108         void setFont(Cursor & cur, Font const &, bool toggleall = false);
109         /// Set font from \p begin to \p end and rebreak.
110         void setFont(Buffer const & buffer, CursorSlice const & begin,
111                 CursorSlice const & end, Font const &,
112                 bool toggleall = false);
113
114         ///
115         void toggleFree(Cursor & cur, Font const &, bool toggleall = false);
116
117         /// ???
118         /// FIXME: replace Cursor with DocIterator.
119         docstring getStringToIndex(Cursor const & cur);
120
121         /// insert a character at cursor position
122         /// FIXME: replace Cursor with DocIterator.
123         void insertChar(Cursor & cur, char_type c);
124         /// insert an inset at cursor position
125         /// FIXME: replace Cursor with DocIterator.
126         void insertInset(Cursor & cur, Inset * inset);
127
128         /// try to handle that request
129         /// FIXME: replace Cursor with DocIterator.
130         void dispatch(Cursor & cur, FuncRequest & cmd);
131         /// do we want to handle this event?
132         bool getStatus(Cursor & cur, FuncRequest const & cmd,
133                 FuncStatus & status) const;
134
135         /// read-only access to individual paragraph
136         Paragraph const & getPar(pit_type pit) const { return pars_[pit]; }
137         /// read-write access to individual paragraph
138         Paragraph & getPar(pit_type pit) { return pars_[pit]; }
139         // Returns the current font and depth as a message.
140         /// FIXME: replace Cursor with DocIterator.
141         docstring currentState(Cursor & cur);
142
143         /** returns row near the specified
144           * y-coordinate in given paragraph (relative to the screen).
145           */
146         /// FIXME: move to TextMetrics.
147         Row const & getRowNearY(BufferView const & bv, int y,
148                 pit_type pit) const;
149
150         /// returns the paragraph number closest to screen y-coordinate.
151         /// This method uses the BufferView CoordCache to locate the
152         /// paragraph. The y-coodinate is allowed to be off-screen and
153         /// the CoordCache will be automatically updated if needed. This is
154         /// the reason why we need a non const BufferView.
155         /// FIXME: move to TextMetrics.
156         pit_type getPitNearY(BufferView & bv, int y) const;
157
158         /** Find the word under \c from in the relative location
159          *  defined by \c word_location.
160          *  @param from return here the start of the word
161          *  @param to return here the end of the word
162          */
163         void getWord(CursorSlice & from, CursorSlice & to, word_location const);
164         /// just selects the word the cursor is in
165         void selectWord(Cursor & cur, word_location loc);
166
167         /// what type of change operation to make
168         enum ChangeOp {
169                 ACCEPT,
170                 REJECT
171         };
172         /// accept or reject the selected change
173         void acceptOrRejectChanges(Cursor & cur, ChangeOp op);
174         /// accept the changes within the complete Text
175         void acceptChanges(BufferParams const & bparams);
176         /// reject the changes within the complete Text
177         void rejectChanges(BufferParams const & bparams);
178
179         /// returns true if par was empty and was removed
180         bool setCursor(Cursor & cur, pit_type par, pos_type pos,
181                        bool setfont = true, bool boundary = false);
182         ///
183         void setCursor(CursorSlice &, pit_type par, pos_type pos);
184         ///
185         void setCursorIntern(Cursor & cur, pit_type par,
186                  pos_type pos, bool setfont = true, bool boundary = false);
187         ///
188         void setCurrentFont(Cursor & cur);
189
190         ///
191         void recUndo(Cursor & cur, pit_type first, pit_type last) const;
192         ///
193         void recUndo(Cursor & cur, pit_type first) const;
194
195         /// sets cursor only within this Text.
196         /// x,y are screen coordinates
197         void setCursorFromCoordinates(Cursor & cur, int x, int y);
198
199         /// sets cursor recursively descending into nested editable insets
200         /**
201         \return the inset pointer if x,y is covering that inset
202         \param x,y are absolute screen coordinates.
203         \retval inset is non-null if the cursor is positionned inside
204         */
205         /// FIXME: move to TextMetrics.
206         /// FIXME: cleanup to use BufferView::getCoveringInset() and
207         /// setCursorFromCoordinates() instead of checkInsetHit().
208         Inset * editXY(Cursor & cur, int x, int y);
209
210         /// Move cursor one position left
211         /**
212          * Returns true if an update is needed after the move.
213          */
214         bool cursorLeft(Cursor & cur);
215         /// Move cursor one position right
216         /**
217          * Returns true if an update is needed after the move.
218          */
219         bool cursorRight(Cursor & cur);
220         ///
221         bool cursorLeftOneWord(Cursor & cur);
222         ///
223         bool cursorRightOneWord(Cursor & cur);
224         ///
225         bool cursorUpParagraph(Cursor & cur);
226         ///
227         bool cursorDownParagraph(Cursor & cur);
228         ///
229         /// FIXME: move to TextMetrics.
230         bool cursorHome(Cursor & cur);
231         ///
232         /// FIXME: move to TextMetrics.
233         bool cursorEnd(Cursor & cur);
234         ///
235         void cursorPrevious(Cursor & cur);
236         ///
237         void cursorNext(Cursor & cur);
238         ///
239         bool cursorTop(Cursor & cur);
240         ///
241         bool cursorBottom(Cursor & cur);
242         /// Erase character at cursor. Honour change tracking
243         /// FIXME: replace Cursor with DocIterator.
244         bool erase(Cursor & cur);
245         /// Delete character before cursor. Honour CT
246         /// FIXME: replace Cursor with DocIterator.
247         bool backspace(Cursor & cur);
248         // Dissolve the inset under cursor
249         /// FIXME: replace Cursor with DocIterator.
250         bool dissolveInset(Cursor & cur);
251         ///
252         bool selectWordWhenUnderCursor(Cursor & cur, word_location);
253         ///
254         enum TextCase {
255                 ///
256                 text_lowercase = 0,
257                 ///
258                 text_capitalization = 1,
259                 ///
260                 text_uppercase = 2
261         };
262         /// Change the case of the word at cursor position.
263         void changeCase(Cursor & cur, TextCase action);
264         /// Transposes the character at the cursor with the one before it
265         void charsTranspose(Cursor & cur);
266
267         /** the DTP switches for paragraphs. LyX will store the top settings
268          always in the first physical paragraph, the bottom settings in the
269          last. When a paragraph is broken, the top settings rest, the bottom
270          settings are given to the new one.
271          This function will handle a multi-paragraph selection.
272          */
273         void setParagraphs(Cursor & cur, docstring arg, bool modify = false);
274         /// Sets parameters for current or selected paragraphs
275         void setParagraphs(Cursor & cur, ParagraphParameters const & p);
276
277         /* these things are for search and replace */
278
279         /// needed to insert the selection
280         /// FIXME: replace Cursor with DocIterator.
281         void insertStringAsLines(Cursor & cur, docstring const & str);
282         /// needed to insert the selection
283         /// FIXME: replace Cursor with DocIterator.
284         void insertStringAsParagraphs(Cursor & cur, docstring const & str);
285
286         /// Returns an inset if inset was hit, or 0 if not.
287         /// \warning This method is not recursive! It will return the
288         /// outermost inset within this Text.
289         /// \sa BufferView::getCoveringInset() to get the innermost inset.
290         Inset * checkInsetHit(BufferView &, int x, int y);
291
292         /// return the color of the canvas
293         Color_color backgroundColor() const;
294
295         /**
296          * Returns the left beginning of the text.
297          * This information cannot be taken from the layout object, because
298          * in LaTeX the beginning of the text fits in some cases
299          * (for example sections) exactly the label-width.
300          */
301         /// FIXME: move to TextMetrics.
302         int leftMargin(Buffer const &, int max_width, pit_type pit, pos_type pos) const;
303         int leftMargin(Buffer const &, int max_width, pit_type pit) const;
304
305         /// access to our paragraphs
306         ParagraphList const & paragraphs() const { return pars_; }
307         ParagraphList & paragraphs() { return pars_; }
308         /// return true if this is the main text
309         bool isMainText(Buffer const &) const;
310
311         /// is this row the last in the text?
312         /// FIXME: move to TextMetrics.
313         bool isLastRow(pit_type pit, Row const & row) const;
314         /// is this row the first in the text?
315         /// FIXME: move to TextMetrics.
316         bool isFirstRow(pit_type pit, Row const & row) const;
317
318         ///
319         double spacing(Buffer const & buffer, Paragraph const & par) const;
320         /// make a suggestion for a label
321         /// FIXME: replace Cursor with DocIterator.
322         docstring getPossibleLabel(Cursor & cur) const;
323         /// is this paragraph right-to-left?
324         bool isRTL(Buffer const &, Paragraph const & par) const;
325         /// is this position in the paragraph right-to-left?
326         bool isRTL(Buffer const & buffer, CursorSlice const & sl, bool boundary) const;
327         /// is between pos-1 and pos an RTL<->LTR boundary?
328         bool isRTLBoundary(Buffer const & buffer, Paragraph const & par,
329           pos_type pos) const;
330         /// would be a RTL<->LTR boundary between pos and the given font?
331         bool isRTLBoundary(Buffer const & buffer, Paragraph const & par,
332           pos_type pos, Font const & font) const;
333
334         ///
335         bool checkAndActivateInset(Cursor & cur, bool front);
336
337         ///
338         void write(Buffer const & buf, std::ostream & os) const;
339         /// returns whether we've seen our usual 'end' marker
340         bool read(Buffer const & buf, Lexer & lex, ErrorList & errorList);
341
342         ///
343         /// FIXME: move to TextMetrics.
344         int cursorX(BufferView const &, CursorSlice const & cursor,
345                 bool boundary) const;
346         ///
347         /// FIXME: move to TextMetrics.
348         int cursorY(BufferView const & bv, CursorSlice const & cursor,
349                 bool boundary) const;
350
351         /// delete double spaces, leading spaces, and empty paragraphs around old cursor.
352         /// \retval true if a change has happened and we need a redraw.
353         /// FIXME: replace Cursor with DocIterator. This is not possible right
354         /// now because recordUndo() is called which needs a Cursor.
355         static bool deleteEmptyParagraphMechanism(Cursor & cur,
356                 Cursor & old, bool & need_anchor_change);
357
358         /// delete double spaces, leading spaces, and empty paragraphs
359         /// from \first to \last paragraph
360         void deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool trackChanges);
361
362 public:
363         /// the current font settings
364         Font current_font;
365         /// the current font
366         Font real_current_font;
367         ///
368         int background_color_;
369
370         ///
371         ParagraphList pars_;
372
373         /// our 'outermost' font. This is handed down from the surrounding
374         // inset through the pi/mi parameter (pi.base.font)
375         Font font_;
376
377         ///
378         bool autoBreakRows_;
379 private:
380         /// return past-the-last paragraph influenced by a layout
381         /// change on pit
382         pit_type undoSpan(pit_type pit);
383
384         // fix the cursor `cur' after a characters has been deleted at `where'
385         // position. Called by deleteEmptyParagraphMechanism
386         static void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
387
388         // At cursor position 0, try to merge the paragraph with the one before it.
389         // Ignore change tracking, i.e., physically remove the end-of-par character
390         bool backspacePos0(Cursor & cur);
391         /// handle the case where bibitems were deleted
392         bool handleBibitems(Cursor & cur);
393
394         ///
395         void deleteWordForward(Cursor & cur);
396         ///
397         void deleteWordBackward(Cursor & cur);
398         ///
399         void deleteLineForward(Cursor & cur);
400         ///
401         void charInserted();
402         /// set 'number' font property
403         void number(Cursor & cur);
404
405         /// paste plain text at current cursor.
406         /// \param str string to paste
407         /// \param asParagraphs whether to paste as paragraphs or as lines
408         void pasteString(Cursor & cur, docstring const & str,
409                         bool asParagraphs);
410 };
411
412 } // namespace lyx
413
414 #endif // LYXTEXT_H