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::pit_type pit_type;
57 explicit LyXText(BufferView *);
59 void init(BufferView *);
62 LyXFont getFont(Paragraph const & par, pos_type pos) const;
64 void applyOuterFont(LyXFont &) const;
66 LyXFont getLayoutFont(pit_type pit) const;
68 LyXFont getLabelFont(Paragraph const & par) const;
70 void setCharFont(pit_type pit, pos_type pos, LyXFont const & font);
72 void setCharFont(pit_type pit, pos_type pos, LyXFont const & font,
75 /// what you expect when pressing <enter> at cursor position
76 void breakParagraph(LCursor & cur, bool keep_layout = false);
78 /// set layout over selection
79 void setLayout(pit_type start, pit_type end,
80 std::string const & layout);
82 void setLayout(LCursor & cur, std::string const & layout);
84 /// what type of depth change to make
89 /// Increase or decrease the nesting depth of the selected paragraph(s)
90 void changeDepth(LCursor & cur, DEPTH_CHANGE type);
92 /// Returns whether something would be changed by changeDepth
93 bool changeDepthAllowed(LCursor & cur, DEPTH_CHANGE type) const;
95 /// Set font over selection paragraphs and rebreak.
96 void setFont(LCursor & cur, LyXFont const &, bool toggleall = false);
98 /// rebreaks the given par
99 bool redoParagraph(pit_type pit);
101 /// returns pos in given par at given x coord
102 pos_type x2pos(pit_type pit, int row, int x) const;
103 int pos2x(pit_type pit, pos_type pos) const;
106 void toggleFree(LCursor & cur, LyXFont const &, bool toggleall = false);
109 std::string getStringToIndex(LCursor const & cur);
111 /// insert a character at cursor position
112 void insertChar(LCursor & cur, lyx::char_type c);
113 /// insert an inset at cursor position
114 void insertInset(LCursor & cur, InsetBase * inset);
116 /// compute text metrics
117 void metrics(MetricsInfo & mi, Dimension & dim);
118 /// draw text (only used for insets)
119 void draw(PainterInfo & pi, int x, int y) const;
120 /// draw textselection
121 void drawSelection(PainterInfo & pi, int x, int y) const;
123 /// try to handle that request
124 void dispatch(LCursor & cur, FuncRequest & cmd);
125 /// do we want to handle this event?
126 bool getStatus(LCursor & cur, FuncRequest const & cmd,
127 FuncStatus & status) const;
129 /// access to out BufferView. This should go...
130 BufferView * bv() const;
132 /// read-only access to individual paragraph
133 Paragraph const & getPar(pit_type pit) const { return pars_[pit]; }
134 /// read-write access to individual paragraph
135 Paragraph & getPar(pit_type pit) { return pars_[pit]; }
136 // Returns the current font and depth as a message.
137 std::string currentState(LCursor & cur);
139 /** returns row near the specified
140 * y-coordinate in given paragraph (relative to the screen).
142 Row const & getRowNearY(int y, pit_type pit) const;
143 pit_type getPitNearY(int y) const;
145 /** returns the column near the specified x-coordinate of the row
146 x is set to the real beginning of this column
148 pos_type getColumnNearX(pit_type pit,
149 Row const & row, int & x, bool & boundary) const;
151 /** Find the word under \c from in the relative location
152 * defined by \c word_location.
153 * @param from return here the start of the word
154 * @param to return here the end of the word
156 void getWord(CursorSlice & from, CursorSlice & to, lyx::word_location const);
157 /// just selects the word the cursor is in
158 void selectWord(LCursor & cur, lyx::word_location loc);
160 /// accept selected change
161 void acceptChange(LCursor & cur);
162 /// reject selected change
163 void rejectChange(LCursor & cur);
165 /// returns true if par was empty and was removed
166 bool setCursor(LCursor & cur, pit_type par, pos_type pos,
167 bool setfont = true, bool boundary = false);
169 void setCursor(CursorSlice &, pit_type par, pos_type pos);
171 void setCursorIntern(LCursor & cur, pit_type par,
172 pos_type pos, bool setfont = true, bool boundary = false);
174 void setCurrentFont(LCursor & cur);
177 void recUndo(pit_type first, pit_type last) const;
179 void recUndo(pit_type first) const;
180 /// returns true if par was empty and was removed
181 bool setCursorFromCoordinates(LCursor & cur, int x, int y);
183 InsetBase * editXY(LCursor & cur, int x, int y);
184 /// Move cursor one line up.
186 * Returns true if an update is needed after the move.
188 bool cursorUp(LCursor & cur);
189 /// Move cursor one line down.
191 * Returns true if an update is needed after the move.
193 bool cursorDown(LCursor & cur);
194 /// Move cursor one position left
196 * Returns true if an update is needed after the move.
198 bool cursorLeft(LCursor & cur);
199 /// Move cursor one position right
201 * Returns true if an update is needed after the move.
203 bool cursorRight(LCursor & cur);
205 bool cursorLeftOneWord(LCursor & cur);
207 bool cursorRightOneWord(LCursor & cur);
209 bool cursorUpParagraph(LCursor & cur);
211 bool cursorDownParagraph(LCursor & cur);
213 bool cursorHome(LCursor & cur);
215 bool cursorEnd(LCursor & cur);
217 bool cursorPrevious(LCursor & cur);
219 bool cursorNext(LCursor & cur);
221 bool cursorTop(LCursor & cur);
223 bool cursorBottom(LCursor & cur);
224 /// Erase character at cursor. Honour change tracking
225 bool erase(LCursor & cur);
226 /** At cursor position 0, merge paragraph with the one before it.
227 * Ignore CT (this is used in \c acceptChange, \c rejectChange for
228 * physical deletion of paragraph break)
230 bool backspacePos0(LCursor & cur);
231 /// Delete character before cursor. Honour CT
232 bool backspace(LCursor & cur);
234 bool selectWordWhenUnderCursor(LCursor & cur, lyx::word_location);
240 text_capitalization = 1,
244 /// Change the case of the word at cursor position.
245 void changeCase(LCursor & cur, TextCase action);
247 /** the DTP switches for paragraphs. LyX will store the top settings
248 always in the first physical paragraph, the bottom settings in the
249 last. When a paragraph is broken, the top settings rest, the bottom
250 settings are given to the new one.
252 void setParagraph(LCursor & cur,
253 Spacing const & spacing,
255 std::string const & labelwidthstring,
258 /* these things are for search and replace */
260 /// needed to insert the selection
261 void insertStringAsLines(LCursor & cur, std::string const & str);
262 /// needed to insert the selection
263 void insertStringAsParagraphs(LCursor & cur, std::string const & str);
265 /// current text width
268 /// current text heigth
271 /// Returns an inset if inset was hit, or 0 if not.
272 InsetBase * checkInsetHit(int x, int y) const;
275 int singleWidth(Paragraph const & par, pos_type pos) const;
277 int singleWidth(Paragraph const & par,
278 pos_type pos, char c, LyXFont const & Font) const;
280 /// return the color of the canvas
281 LColor_color backgroundColor() const;
284 * Returns the left beginning of the text.
285 * This information cannot be taken from the layout object, because
286 * in LaTeX the beginning of the text fits in some cases
287 * (for example sections) exactly the label-width.
289 int leftMargin(pit_type pit, pos_type pos) const;
290 int leftMargin(pit_type pit) const;
292 int rightMargin(Paragraph const & par) const;
294 /** this calculates the specified parameters. needed when setting
295 * the cursor and when creating a visible row */
296 RowMetrics computeRowMetrics(pit_type pit, Row const & row) const;
298 /// access to our paragraphs
299 ParagraphList const & paragraphs() const { return pars_; }
300 ParagraphList & paragraphs() { return pars_; }
301 /// return true if this is the main text
302 bool isMainText() const;
304 /// return first row of text
305 Row const & firstRow() const;
307 /// is this row the last in the text?
308 bool isLastRow(pit_type pit, Row const & row) const;
309 /// is this row the first in the text?
310 bool isFirstRow(pit_type pit, Row const & row) const;
313 double spacing(Paragraph const & par) const;
314 /// make a suggestion for a label
315 std::string getPossibleLabel(LCursor & cur) const;
316 /// is this paragraph right-to-left?
317 bool isRTL(Paragraph const & par) const;
319 bool checkAndActivateInset(LCursor & cur, bool front);
322 void write(Buffer const & buf, std::ostream & os) const;
323 /// returns whether we've seen our usual 'end' marker
324 bool read(Buffer const & buf, LyXLex & lex, ErrorList & errorList);
331 int cursorX(CursorSlice const & cursor, bool boundary) const;
333 int cursorY(CursorSlice const & cursor, bool boundary) const;
335 /// delete double space or empty paragraphs around old cursor
336 bool deleteEmptyParagraphMechanism(LCursor & cur, LCursor & old);
343 /// the current font settings
344 LyXFont current_font;
346 LyXFont real_current_font;
348 int background_color_;
350 /// only the top-level LyXText has this non-zero
351 BufferView * bv_owner;
358 /// our 'outermost' font. This is handed down from the surrounding
359 // inset through the pi/mi parameter (pi.base.font)
365 /// return past-the-last paragraph influenced by a layout
367 pit_type undoSpan(pit_type pit);
369 /// Calculate and set the height of the row
370 void setHeightOfRow(pit_type, Row & row);
372 // fix the cursor `cur' after a characters has been deleted at `where'
373 // position. Called by deleteEmptyParagraphMechanism
374 void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
377 void deleteWordForward(LCursor & cur);
379 void deleteWordBackward(LCursor & cur);
381 void deleteLineForward(LCursor & cur);
383 /// sets row.end to the pos value *after* which a row should break.
384 /// for example, the pos after which isNewLine(pos) == true
385 void rowBreakPoint(pit_type pit, Row & row) const;
386 /// sets row.width to the minimum space a row needs on the screen in pixel
387 void setRowWidth(pit_type pit, Row & row) const;
388 /// the minimum space a manual label needs on the screen in pixels
389 int labelFill(Paragraph const & par, Row const & row) const;
391 int labelEnd(pit_type pit) const;
395 /// set 'number' font property
396 void number(LCursor & cur);
399 /// return the default height of a row in pixels, considering font zoom
400 int defaultRowHeight();