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.
17 #include "bufferview_funcs.h"
19 #include "dispatchresult.h"
20 #include "dimension.h"
23 #include "lyxlayout_ptr_fwd.h"
24 #include "ParagraphList.h"
48 /// This class encapsulates the main text data and operations in LyX
52 typedef lyx::pos_type pos_type;
54 typedef lyx::char_type char_type;
56 typedef lyx::pit_type pit_type;
59 explicit LyXText(BufferView *);
61 void init(BufferView *);
64 LyXFont getFont(Paragraph const & par, pos_type pos) const;
66 void applyOuterFont(LyXFont &) const;
68 LyXFont getLayoutFont(pit_type pit) const;
70 LyXFont getLabelFont(Paragraph const & par) const;
72 void setCharFont(pit_type pit, pos_type pos, LyXFont const & font);
74 void setCharFont(pit_type pit, pos_type pos, LyXFont const & font,
77 /// what you expect when pressing <enter> at cursor position
78 void breakParagraph(LCursor & cur, bool keep_layout = false);
80 /// set layout over selection
81 void setLayout(pit_type start, pit_type end,
82 std::string const & layout);
84 void setLayout(LCursor & cur, std::string const & layout);
86 /// what type of depth change to make
91 /// Increase or decrease the nesting depth of the selected paragraph(s)
92 void changeDepth(LCursor & cur, DEPTH_CHANGE type);
94 /// Returns whether something would be changed by changeDepth
95 bool changeDepthAllowed(LCursor & cur, DEPTH_CHANGE type) const;
97 /// Set font over selection paragraphs and rebreak.
98 void setFont(LCursor & cur, LyXFont const &, bool toggleall = false);
100 /// rebreaks the given par
101 bool redoParagraph(pit_type pit);
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;
108 void toggleFree(LCursor & cur, LyXFont const &, bool toggleall = false);
111 std::string getStringToIndex(LCursor const & cur);
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);
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;
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;
131 /// access to out BufferView. This should go...
132 BufferView * bv() const;
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);
141 /** returns row near the specified
142 * y-coordinate in given paragraph (relative to the screen).
144 Row const & getRowNearY(int y, pit_type pit) const;
145 pit_type getPitNearY(int y) const;
147 /** returns the column near the specified x-coordinate of the row
148 x is set to the real beginning of this column
150 pos_type getColumnNearX(pit_type pit,
151 Row const & row, int & x, bool & boundary) const;
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
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);
162 /// accept selected change
163 void acceptChange(LCursor & cur);
164 /// reject selected change
165 void rejectChange(LCursor & cur);
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);
171 void setCursor(CursorSlice &, pit_type par, pos_type pos);
173 void setCursorIntern(LCursor & cur, pit_type par,
174 pos_type pos, bool setfont = true, bool boundary = false);
176 void setCurrentFont(LCursor & cur);
179 void recUndo(pit_type first, pit_type last) const;
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);
185 InsetBase * editXY(LCursor & cur, int x, int y);
186 /// Move cursor one line up.
188 * Returns true if an update is needed after the move.
190 bool cursorUp(LCursor & cur);
191 /// Move cursor one line down.
193 * Returns true if an update is needed after the move.
195 bool cursorDown(LCursor & cur);
196 /// Move cursor one position left
198 * Returns true if an update is needed after the move.
200 bool cursorLeft(LCursor & cur);
201 /// Move cursor one position right
203 * Returns true if an update is needed after the move.
205 bool cursorRight(LCursor & cur);
207 bool cursorLeftOneWord(LCursor & cur);
209 bool cursorRightOneWord(LCursor & cur);
211 bool cursorUpParagraph(LCursor & cur);
213 bool cursorDownParagraph(LCursor & cur);
215 bool cursorHome(LCursor & cur);
217 bool cursorEnd(LCursor & cur);
219 bool cursorPrevious(LCursor & cur);
221 bool cursorNext(LCursor & cur);
223 bool cursorTop(LCursor & cur);
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)
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);
238 bool selectWordWhenUnderCursor(LCursor & cur, lyx::word_location);
244 text_capitalization = 1,
248 /// Change the case of the word at cursor position.
249 void changeCase(LCursor & cur, TextCase action);
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.
256 void setParagraph(LCursor & cur,
257 Spacing const & spacing,
259 std::string const & labelwidthstring,
262 /* these things are for search and replace */
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);
269 /// current text width
272 /// current text heigth
275 /// Returns an inset if inset was hit, or 0 if not.
276 InsetBase * checkInsetHit(int x, int y) const;
279 int singleWidth(Paragraph const & par, pos_type pos) const;
281 int singleWidth(Paragraph const & par,
282 pos_type pos, char_type c, LyXFont const & Font) const;
284 /// return the color of the canvas
285 LColor_color backgroundColor() const;
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.
293 int leftMargin(pit_type pit, pos_type pos) const;
294 int leftMargin(pit_type pit) const;
296 int rightMargin(Paragraph const & par) const;
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;
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;
308 /// return first row of text
309 Row const & firstRow() const;
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;
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;
323 bool checkAndActivateInset(LCursor & cur, bool front);
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);
335 int cursorX(CursorSlice const & cursor, bool boundary) const;
337 int cursorY(CursorSlice const & cursor, bool boundary) const;
339 /// delete double space or empty paragraphs around old cursor
340 bool deleteEmptyParagraphMechanism(LCursor & cur, LCursor & old);
347 /// the current font settings
348 LyXFont current_font;
350 LyXFont real_current_font;
352 int background_color_;
354 /// only the top-level LyXText has this non-zero
355 BufferView * bv_owner;
362 /// our 'outermost' font. This is handed down from the surrounding
363 // inset through the pi/mi parameter (pi.base.font)
369 /// return past-the-last paragraph influenced by a layout
371 pit_type undoSpan(pit_type pit);
373 /// Calculate and set the height of the row
374 void setHeightOfRow(pit_type, Row & row);
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);
381 void deleteWordForward(LCursor & cur);
383 void deleteWordBackward(LCursor & cur);
385 void deleteLineForward(LCursor & cur);
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;
395 int labelEnd(pit_type pit) const;
399 /// set 'number' font property
400 void number(LCursor & cur);
403 /// return the default height of a row in pixels, considering font zoom
404 int defaultRowHeight();