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);
236 bool selectWordWhenUnderCursor(LCursor & cur, lyx::word_location);
242 text_capitalization = 1,
246 /// Change the case of the word at cursor position.
247 void changeCase(LCursor & cur, TextCase action);
249 /** the DTP switches for paragraphs. LyX will store the top settings
250 always in the first physical paragraph, the bottom settings in the
251 last. When a paragraph is broken, the top settings rest, the bottom
252 settings are given to the new one.
254 void setParagraph(LCursor & cur,
255 Spacing const & spacing,
257 std::string const & labelwidthstring,
260 /* these things are for search and replace */
262 /// needed to insert the selection
263 void insertStringAsLines(LCursor & cur, std::string const & str);
264 /// needed to insert the selection
265 void insertStringAsParagraphs(LCursor & cur, std::string const & str);
267 /// current text width
270 /// current text heigth
273 /// Returns an inset if inset was hit, or 0 if not.
274 InsetBase * checkInsetHit(int x, int y) const;
277 int singleWidth(Paragraph const & par, pos_type pos) const;
279 int singleWidth(Paragraph const & par,
280 pos_type pos, char_type c, LyXFont const & Font) const;
282 /// return the color of the canvas
283 LColor_color backgroundColor() const;
286 * Returns the left beginning of the text.
287 * This information cannot be taken from the layout object, because
288 * in LaTeX the beginning of the text fits in some cases
289 * (for example sections) exactly the label-width.
291 int leftMargin(pit_type pit, pos_type pos) const;
292 int leftMargin(pit_type pit) const;
294 int rightMargin(Paragraph const & par) const;
296 /** this calculates the specified parameters. needed when setting
297 * the cursor and when creating a visible row */
298 RowMetrics computeRowMetrics(pit_type pit, Row const & row) const;
300 /// access to our paragraphs
301 ParagraphList const & paragraphs() const { return pars_; }
302 ParagraphList & paragraphs() { return pars_; }
303 /// return true if this is the main text
304 bool isMainText() const;
306 /// return first row of text
307 Row const & firstRow() const;
309 /// is this row the last in the text?
310 bool isLastRow(pit_type pit, Row const & row) const;
311 /// is this row the first in the text?
312 bool isFirstRow(pit_type pit, Row const & row) const;
315 double spacing(Paragraph const & par) const;
316 /// make a suggestion for a label
317 std::string getPossibleLabel(LCursor & cur) const;
318 /// is this paragraph right-to-left?
319 bool isRTL(Paragraph const & par) const;
321 bool checkAndActivateInset(LCursor & cur, bool front);
324 void write(Buffer const & buf, std::ostream & os) const;
325 /// returns whether we've seen our usual 'end' marker
326 bool read(Buffer const & buf, LyXLex & lex, ErrorList & errorList);
333 int cursorX(CursorSlice const & cursor, bool boundary) const;
335 int cursorY(CursorSlice const & cursor, bool boundary) const;
337 /// delete double space or empty paragraphs around old cursor
338 bool deleteEmptyParagraphMechanism(LCursor & cur, LCursor & old);
345 /// the current font settings
346 LyXFont current_font;
348 LyXFont real_current_font;
350 int background_color_;
352 /// only the top-level LyXText has this non-zero
353 BufferView * bv_owner;
360 /// our 'outermost' font. This is handed down from the surrounding
361 // inset through the pi/mi parameter (pi.base.font)
367 /// return past-the-last paragraph influenced by a layout
369 pit_type undoSpan(pit_type pit);
371 /// Calculate and set the height of the row
372 void setHeightOfRow(pit_type, Row & row);
374 // fix the cursor `cur' after a characters has been deleted at `where'
375 // position. Called by deleteEmptyParagraphMechanism
376 void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
379 void deleteWordForward(LCursor & cur);
381 void deleteWordBackward(LCursor & cur);
383 void deleteLineForward(LCursor & cur);
385 /// sets row.end to the pos value *after* which a row should break.
386 /// for example, the pos after which isNewLine(pos) == true
387 void rowBreakPoint(pit_type pit, Row & row) const;
388 /// sets row.width to the minimum space a row needs on the screen in pixel
389 void setRowWidth(pit_type pit, Row & row) const;
390 /// the minimum space a manual label needs on the screen in pixels
391 int labelFill(Paragraph const & par, Row const & row) const;
393 int labelEnd(pit_type pit) const;
397 /// set 'number' font property
398 void number(LCursor & cur);
401 /// return the default height of a row in pixels, considering font zoom
402 int defaultRowHeight();