4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
8 * \author Lars Gullik Bjønnes
11 * Full author contact details are available in file CREDITS.
18 #include "DispatchResult.h"
21 #include "lyxlayout_ptr_fwd.h"
22 #include "ParagraphList.h"
46 /// This class encapsulates the main text data and operations in LyX
52 /// \return true if there's no content at all.
53 /// \warning a non standard layout on an empty paragraph doesn't
58 Font getFont(Buffer const & buffer, Paragraph const & par,
61 void applyOuterFont(Buffer const & buffer, Font &) const;
63 Font getLayoutFont(Buffer const & buffer, pit_type pit) const;
65 Font getLabelFont(Buffer const & buffer,
66 Paragraph const & par) const;
67 /** Set font of character at position \p pos in paragraph \p pit.
68 * Must not be called if \p pos denotes an inset with text contents,
69 * and the inset is not allowed inside a font change (see below).
71 void setCharFont(Buffer const & buffer, pit_type pit, pos_type pos,
74 /** Needed to propagate font changes to all text cells of insets
75 * that are not allowed inside a font change (bug 1973).
76 * Must not be called if \p pos denotes an ordinary character or an
77 * inset that is alowed inside a font change.
78 * FIXME: This should be removed, see documentation of noFontChange
81 void setInsetFont(Buffer const & buffer, pit_type pit, pos_type pos,
82 Font const & font, bool toggleall = false);
84 /// what you expect when pressing \<enter\> at cursor position
85 void breakParagraph(Cursor & cur, bool keep_layout = false);
87 /// set layout over selection
88 void setLayout(Buffer const & buffer, pit_type start, pit_type end,
89 docstring const & layout);
90 /// Set given layout to current cursor position.
91 /// FIXME: replace Cursor with DocIterator.
92 void setLayout(Cursor & cur, docstring const & layout);
94 /// what type of depth change to make
99 /// Increase or decrease the nesting depth of the selected paragraph(s)
100 /// FIXME: replace Cursor with DocIterator.
101 void changeDepth(Cursor & cur, DEPTH_CHANGE type);
103 /// Returns whether something would be changed by changeDepth
104 /// FIXME: replace Cursor with DocIterator.
105 bool changeDepthAllowed(Cursor & cur, DEPTH_CHANGE type) const;
107 /// Set font over selection paragraphs and rebreak.
108 /// FIXME: replace Cursor with DocIterator.
109 void setFont(Cursor & cur, Font const &, bool toggleall = false);
110 /// Set font from \p begin to \p end and rebreak.
111 void setFont(Buffer const & buffer, CursorSlice const & begin,
112 CursorSlice const & end, Font const &,
113 bool toggleall = false);
116 void toggleFree(Cursor & cur, Font const &, bool toggleall = false);
119 /// FIXME: replace Cursor with DocIterator.
120 docstring getStringToIndex(Cursor const & cur);
122 /// insert a character at cursor position
123 /// FIXME: replace Cursor with DocIterator.
124 void insertChar(Cursor & cur, char_type c);
125 /// insert an inset at cursor position
126 /// FIXME: replace Cursor with DocIterator.
127 void insertInset(Cursor & cur, Inset * inset);
129 /// draw text (only used for insets)
130 void draw(PainterInfo & pi, int x, int y) const;
131 /// draw textselection
132 void drawSelection(PainterInfo & pi, int x, int y) const;
134 /// try to handle that request
135 /// FIXME: replace Cursor with DocIterator.
136 void dispatch(Cursor & cur, FuncRequest & cmd);
137 /// do we want to handle this event?
138 bool getStatus(Cursor & cur, FuncRequest const & cmd,
139 FuncStatus & status) const;
141 /// read-only access to individual paragraph
142 Paragraph const & getPar(pit_type pit) const { return pars_[pit]; }
143 /// read-write access to individual paragraph
144 Paragraph & getPar(pit_type pit) { return pars_[pit]; }
145 // Returns the current font and depth as a message.
146 /// FIXME: replace Cursor with DocIterator.
147 docstring currentState(Cursor & cur);
149 /** returns row near the specified
150 * y-coordinate in given paragraph (relative to the screen).
152 /// FIXME: move to TextMetrics.
153 Row const & getRowNearY(BufferView const & bv, int y,
156 /// returns the paragraph number closest to screen y-coordinate.
157 /// This method uses the BufferView CoordCache to locate the
158 /// paragraph. The y-coodinate is allowed to be off-screen and
159 /// the CoordCache will be automatically updated if needed. This is
160 /// the reason why we need a non const BufferView.
161 /// FIXME: move to TextMetrics.
162 pit_type getPitNearY(BufferView & bv, int y) const;
164 /** Find the word under \c from in the relative location
165 * defined by \c word_location.
166 * @param from return here the start of the word
167 * @param to return here the end of the word
169 void getWord(CursorSlice & from, CursorSlice & to, word_location const);
170 /// just selects the word the cursor is in
171 void selectWord(Cursor & cur, word_location loc);
173 /// what type of change operation to make
178 /// accept or reject the selected change
179 void acceptOrRejectChanges(Cursor & cur, ChangeOp op);
180 /// accept the changes within the complete Text
181 void acceptChanges(BufferParams const & bparams);
182 /// reject the changes within the complete Text
183 void rejectChanges(BufferParams const & bparams);
185 /// returns true if par was empty and was removed
186 bool setCursor(Cursor & cur, pit_type par, pos_type pos,
187 bool setfont = true, bool boundary = false);
189 void setCursor(CursorSlice &, pit_type par, pos_type pos);
191 void setCursorIntern(Cursor & cur, pit_type par,
192 pos_type pos, bool setfont = true, bool boundary = false);
194 void setCurrentFont(Cursor & cur);
197 void recUndo(Cursor & cur, pit_type first, pit_type last) const;
199 void recUndo(Cursor & cur, pit_type first) const;
201 /// sets cursor only within this Text.
202 /// x,y are screen coordinates
203 void setCursorFromCoordinates(Cursor & cur, int x, int y);
205 /// sets cursor recursively descending into nested editable insets
207 \return the inset pointer if x,y is covering that inset
208 \param x,y are absolute screen coordinates.
209 \retval inset is non-null if the cursor is positionned inside
211 /// FIXME: move to TextMetrics.
212 /// FIXME: cleanup to use BufferView::getCoveringInset() and
213 /// setCursorFromCoordinates() instead of checkInsetHit().
214 Inset * editXY(Cursor & cur, int x, int y);
216 /// Move cursor one position left
218 * Returns true if an update is needed after the move.
220 bool cursorLeft(Cursor & cur);
221 /// Move cursor one position right
223 * Returns true if an update is needed after the move.
225 bool cursorRight(Cursor & cur);
227 bool cursorLeftOneWord(Cursor & cur);
229 bool cursorRightOneWord(Cursor & cur);
231 bool cursorUpParagraph(Cursor & cur);
233 bool cursorDownParagraph(Cursor & cur);
235 /// FIXME: move to TextMetrics.
236 bool cursorHome(Cursor & cur);
238 /// FIXME: move to TextMetrics.
239 bool cursorEnd(Cursor & cur);
241 void cursorPrevious(Cursor & cur);
243 void cursorNext(Cursor & cur);
245 bool cursorTop(Cursor & cur);
247 bool cursorBottom(Cursor & cur);
248 /// Erase character at cursor. Honour change tracking
249 /// FIXME: replace Cursor with DocIterator.
250 bool erase(Cursor & cur);
251 /// Delete character before cursor. Honour CT
252 /// FIXME: replace Cursor with DocIterator.
253 bool backspace(Cursor & cur);
254 // Dissolve the inset under cursor
255 /// FIXME: replace Cursor with DocIterator.
256 bool dissolveInset(Cursor & cur);
258 bool selectWordWhenUnderCursor(Cursor & cur, word_location);
264 text_capitalization = 1,
268 /// Change the case of the word at cursor position.
269 void changeCase(Cursor & cur, TextCase action);
270 /// Transposes the character at the cursor with the one before it
271 void charsTranspose(Cursor & cur);
273 /** the DTP switches for paragraphs. LyX will store the top settings
274 always in the first physical paragraph, the bottom settings in the
275 last. When a paragraph is broken, the top settings rest, the bottom
276 settings are given to the new one.
278 void setParagraph(Cursor & cur,
279 Spacing const & spacing,
281 docstring const & labelwidthstring,
284 /* these things are for search and replace */
286 /// needed to insert the selection
287 /// FIXME: replace Cursor with DocIterator.
288 void insertStringAsLines(Cursor & cur, docstring const & str);
289 /// needed to insert the selection
290 /// FIXME: replace Cursor with DocIterator.
291 void insertStringAsParagraphs(Cursor & cur, docstring const & str);
293 /// Returns an inset if inset was hit, or 0 if not.
294 /// \warning This method is not recursive! It will return the
295 /// outermost inset within this Text.
296 /// \sa BufferView::getCoveringInset() to get the innermost inset.
297 Inset * checkInsetHit(BufferView &, int x, int y);
300 /// FIXME: move to TextMetrics.
301 int singleWidth(Buffer const &, Paragraph const & par,
304 /// FIXME: move to TextMetrics.
305 int singleWidth(Paragraph const & par, pos_type pos, char_type c,
306 Font const & Font) const;
308 /// return the color of the canvas
309 Color_color backgroundColor() const;
312 * Returns the left beginning of the text.
313 * This information cannot be taken from the layout object, because
314 * in LaTeX the beginning of the text fits in some cases
315 * (for example sections) exactly the label-width.
317 /// FIXME: move to TextMetrics.
318 int leftMargin(Buffer const &, int max_width, pit_type pit, pos_type pos) const;
319 int leftMargin(Buffer const &, int max_width, pit_type pit) const;
321 /// access to our paragraphs
322 ParagraphList const & paragraphs() const { return pars_; }
323 ParagraphList & paragraphs() { return pars_; }
324 /// return true if this is the main text
325 bool isMainText(Buffer const &) const;
327 /// is this row the last in the text?
328 /// FIXME: move to TextMetrics.
329 bool isLastRow(pit_type pit, Row const & row) const;
330 /// is this row the first in the text?
331 /// FIXME: move to TextMetrics.
332 bool isFirstRow(pit_type pit, Row const & row) const;
335 double spacing(Buffer const & buffer, Paragraph const & par) const;
336 /// make a suggestion for a label
337 /// FIXME: replace Cursor with DocIterator.
338 docstring getPossibleLabel(Cursor & cur) const;
339 /// is this paragraph right-to-left?
340 bool isRTL(Buffer const &, Paragraph const & par) const;
341 /// is this position in the paragraph right-to-left?
342 bool isRTL(Buffer const & buffer, CursorSlice const & sl, bool boundary) const;
343 /// is between pos-1 and pos an RTL<->LTR boundary?
344 bool isRTLBoundary(Buffer const & buffer, Paragraph const & par,
346 /// would be a RTL<->LTR boundary between pos and the given font?
347 bool isRTLBoundary(Buffer const & buffer, Paragraph const & par,
348 pos_type pos, Font const & font) const;
351 bool checkAndActivateInset(Cursor & cur, bool front);
354 void write(Buffer const & buf, std::ostream & os) const;
355 /// returns whether we've seen our usual 'end' marker
356 bool read(Buffer const & buf, Lexer & lex, ErrorList & errorList);
359 /// FIXME: move to TextMetrics.
360 int cursorX(BufferView const &, CursorSlice const & cursor,
361 bool boundary) const;
363 /// FIXME: move to TextMetrics.
364 int cursorY(BufferView const & bv, CursorSlice const & cursor,
365 bool boundary) const;
367 /// delete double spaces, leading spaces, and empty paragraphs around old cursor.
368 /// \retval true if a change has happened and we need a redraw.
369 /// FIXME: replace Cursor with DocIterator. This is not possible right
370 /// now because recordUndo() is called which needs a Cursor.
371 static bool deleteEmptyParagraphMechanism(Cursor & cur,
372 Cursor & old, bool & need_anchor_change);
374 /// delete double spaces, leading spaces, and empty paragraphs
375 /// from \first to \last paragraph
376 void deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool trackChanges);
379 /// the current font settings
382 Font real_current_font;
384 int background_color_;
389 /// our 'outermost' font. This is handed down from the surrounding
390 // inset through the pi/mi parameter (pi.base.font)
396 /// return past-the-last paragraph influenced by a layout
398 pit_type undoSpan(pit_type pit);
400 // fix the cursor `cur' after a characters has been deleted at `where'
401 // position. Called by deleteEmptyParagraphMechanism
402 static void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
404 // At cursor position 0, try to merge the paragraph with the one before it.
405 // Ignore change tracking, i.e., physically remove the end-of-par character
406 bool backspacePos0(Cursor & cur);
407 /// handle the case where bibitems were deleted
408 bool handleBibitems(Cursor & cur);
411 void deleteWordForward(Cursor & cur);
413 void deleteWordBackward(Cursor & cur);
415 void deleteLineForward(Cursor & cur);
418 /// set 'number' font property
419 void number(Cursor & cur);
420 /// draw selection for a single row
421 void drawRowSelection(PainterInfo & pi, int x, Row const & row,
422 DocIterator const & beg, DocIterator const & end,
423 bool drawOnBegMargin, bool drawOnEndMargin) const;
425 /// paste plain text at current cursor.
426 /// \param str string to paste
427 /// \param asParagraphs whether to paste as paragraphs or as lines
428 void pasteString(Cursor & cur, docstring const & str,