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"
22 #include "lyxlayout_ptr_fwd.h"
23 #include "ParagraphList_fwd.h"
24 #include "RowList_fwd.h"
49 /// This class encapsulates the main text data and operations in LyX
53 typedef lyx::pos_type pos_type;
55 typedef lyx::paroffset_type par_type;
58 explicit LyXText(BufferView *);
60 void init(BufferView *);
62 /// update y coordinate cache of all paragraphs
63 void updateParPositions();
65 LyXFont getFont(ParagraphList::iterator pit, pos_type pos) const;
67 LyXFont getLayoutFont(ParagraphList::iterator pit) const;
69 LyXFont getLabelFont(ParagraphList::iterator pit) const;
71 void setCharFont(ParagraphList::iterator pit,
72 pos_type pos, LyXFont const & font);
73 void setCharFont(ParagraphList::iterator pit,
74 pos_type pos, LyXFont const & font, bool toggleall);
76 /// what you expect when pressing <enter> at cursor position
77 void breakParagraph(LCursor & cur, char keep_layout = 0);
79 /** set layout over selection and make a total rebreak of
82 ParagraphList::iterator
83 setLayout(ParagraphList::iterator start,
84 ParagraphList::iterator end,
85 std::string const & layout);
87 void setLayout(LCursor & cur, std::string const & layout);
89 /// Increase or decrease the nesting depth of the selected paragraph(s)
90 void changeDepth(LCursor & cur, bv_funcs::DEPTH_CHANGE type);
92 /// Returns whether something would be changed by changeDepth
93 bool changeDepthAllowed(LCursor & cur, bv_funcs::DEPTH_CHANGE type);
95 /// Set font over selection paragraphs and rebreak.
96 void setFont(LCursor & cur, LyXFont const &, bool toggleall = false);
98 /// rebreaks all paragaphs between the given pars.
99 void redoParagraphs(ParagraphList::iterator begin,
100 ParagraphList::iterator end);
101 /// rebreaks the given par
102 void redoParagraph(ParagraphList::iterator pit);
103 /// rebreaks the cursor par
104 void redoParagraph(LCursor & cur);
107 void toggleFree(LCursor & cur, LyXFont const &, bool toggleall = false);
110 std::string getStringToIndex(LCursor & cur);
112 /// insert a character at cursor position
113 void insertChar(LCursor & cur, char c);
114 /// insert an inset at cursor position
115 void insertInset(LCursor & cur, InsetBase * inset);
117 /// a full rebreak of the whole text
119 /// compute text metrics
120 void metrics(MetricsInfo & mi, Dimension & dim);
121 /// draw text (only used for insets)
122 void draw(PainterInfo & pi, int x, int y) const;
123 /// draw textselection
124 void drawSelection(PainterInfo & pi, int x, int y) const;
125 /// returns distance of this cell to the point given by x and y
126 // assumes valid position and size cache
127 int dist(int x, int y) const;
129 /// try to handle that request
130 void dispatch(LCursor & cur, FuncRequest & cmd);
131 /// do we want to handle this event?
132 bool getStatus(LCursor & cur, FuncRequest const & cmd, FuncStatus & status);
134 /// access to out BufferView. This should go...
136 /// access to out BufferView. This should go...
137 BufferView * bv() const;
139 /// returns an iterator pointing to a cursor paragraph
140 ParagraphList::iterator getPar(CursorSlice const & cursor) const;
142 ParagraphList::iterator getPar(par_type par) const;
144 int parOffset(ParagraphList::iterator pit) const;
145 // Returns the current font and depth as a message.
146 std::string LyXText::currentState(LCursor & cur);
148 /** returns an iterator pointing to the row near the specified
149 * y-coordinate (relative to the whole text). y is set to the
150 * real beginning of this row
152 RowList::iterator getRowNearY(int y,
153 ParagraphList::iterator & pit) const;
155 /** returns the column near the specified x-coordinate of the row
156 x is set to the real beginning of this column
158 pos_type getColumnNearX(ParagraphList::iterator pit,
159 Row const & row, int & x, bool & boundary) const;
161 /** Find the word under \c from in the relative location
162 * defined by \c word_location.
163 * @param from return here the start of the word
164 * @param to return here the end of the word
166 void getWord(CursorSlice & from, CursorSlice & to, lyx::word_location const);
167 /// just selects the word the cursor is in
168 void selectWord(LCursor & cur, lyx::word_location loc);
170 /// accept selected change
171 void acceptChange(LCursor & cur);
172 /// reject selected change
173 void rejectChange(LCursor & cur);
175 /// returns true if par was empty and was removed
176 bool setCursor(LCursor & cur, par_type par, pos_type pos,
177 bool setfont = true, bool boundary = false);
179 void setCursor(CursorSlice &, par_type par,
180 pos_type pos, bool boundary = false);
182 void setCursorIntern(LCursor & cur, par_type par,
183 pos_type pos, bool setfont = true, bool boundary = false);
185 void setCurrentFont(LCursor & cur);
188 void recUndo(par_type first, par_type last) const;
190 void recUndo(par_type first) const;
192 void setCursorFromCoordinates(LCursor & cur, int x, int y);
194 InsetBase * editXY(LCursor & cur, int x, int y);
196 void cursorUp(LCursor & cur);
198 void cursorDown(LCursor & cur);
200 void cursorLeft(LCursor & cur);
202 void cursorRight(LCursor & cur);
204 void cursorLeftOneWord(LCursor & cur);
206 void cursorRightOneWord(LCursor & cur);
208 void cursorUpParagraph(LCursor & cur);
210 void cursorDownParagraph(LCursor & cur);
212 void cursorHome(LCursor & cur);
214 void cursorEnd(LCursor & cur);
216 void cursorPrevious(LCursor & cur);
218 void cursorNext(LCursor & cur);
220 void cursorTop(LCursor & cur);
222 void cursorBottom(LCursor & cur);
224 void Delete(LCursor & cur);
226 void backspace(LCursor & cur);
228 bool selectWordWhenUnderCursor(LCursor & cur, lyx::word_location);
234 text_capitalization = 1,
238 /// Change the case of the word at cursor position.
239 void changeCase(LCursor & cur, TextCase action);
242 bool toggleInset(LCursor & cur);
244 void cutSelection(LCursor & cur, bool doclear = true, bool realcut = true);
246 void copySelection(LCursor & cur);
248 void pasteSelection(LCursor & cur, size_t sel_index = 0);
250 /** the DTP switches for paragraphs. LyX will store the top settings
251 always in the first physical paragraph, the bottom settings in the
252 last. When a paragraph is broken, the top settings rest, the bottom
253 settings are given to the new one.
255 void setParagraph(LCursor & cur,
256 Spacing const & spacing,
258 std::string const & labelwidthstring,
261 /* these things are for search and replace */
264 * Sets the selection from the current cursor position to length
265 * characters to the right. No safety checks.
267 void setSelectionRange(LCursor & cur, pos_type length);
268 /// simply replace using the font of the first selected character
269 void replaceSelectionWithString(LCursor & cur, std::string const & str);
270 /// replace selection helper
271 void replaceSelection(LCursor & cur);
273 /// needed to insert the selection
274 void insertStringAsLines(LCursor & cur, std::string const & str);
275 /// needed to insert the selection
276 void insertStringAsParagraphs(LCursor & cur, std::string const & str);
278 /// Find next inset of some specified type.
279 bool gotoNextInset(LCursor & cur,
280 std::vector<InsetOld_code> const & codes,
281 std::string const & contents = std::string());
283 void gotoInset(LCursor & cur,
284 std::vector<InsetOld_code> const & codes, bool same_content);
286 void gotoInset(LCursor & cur, InsetOld_code code, bool same_content);
288 /// current text width
291 /// current text heigth
294 /// updates all counters
295 void updateCounters();
296 /// Returns an inset if inset was hit, or 0 if not.
297 InsetBase * checkInsetHit(int x, int y);
300 int singleWidth(ParagraphList::iterator pit, pos_type pos) const;
302 int singleWidth(ParagraphList::iterator pit,
303 pos_type pos, char c, LyXFont const & Font) const;
305 /// return the color of the canvas
306 LColor_color backgroundColor() const;
309 * Returns the left beginning of the text.
310 * This information cannot be taken from the layout object, because
311 * in LaTeX the beginning of the text fits in some cases
312 * (for example sections) exactly the label-width.
314 int leftMargin(ParagraphList::iterator pit, pos_type pos) const;
315 int leftMargin(ParagraphList::iterator pit) const;
317 int rightMargin(Paragraph const & par) const;
319 /** this calculates the specified parameters. needed when setting
320 * the cursor and when creating a visible row */
322 computeRowMetrics(ParagraphList::iterator pit, Row const & row) const;
324 /// access to our paragraphs
325 ParagraphList & paragraphs() const;
326 /// return true if this is the main text
327 bool isMainText() const;
329 /// return first row of text
330 RowList::iterator firstRow() const;
331 /// return last row of text
332 RowList::iterator lastRow() const;
333 /// return row "behind" last row of text
334 RowList::iterator endRow() const;
335 /// return next row crossing paragraph boundaries
336 void nextRow(ParagraphList::iterator & pit,
337 RowList::iterator & rit) const;
338 /// return previous row crossing paragraph boundaries
339 void previousRow(ParagraphList::iterator & pit,
340 RowList::iterator & rit) const;
342 /// is this row the last in the text?
343 bool isLastRow(ParagraphList::iterator pit, Row const & row) const;
344 /// is this row the first in the text?
345 bool isFirstRow(ParagraphList::iterator pit, Row const & row) const;
348 double spacing(Paragraph const & par) const;
349 /// make a suggestion for a label
350 std::string getPossibleLabel(LCursor & cur) const;
351 /// is this paragraph right-to-left?
352 bool isRTL(Paragraph const & par) const;
354 bool checkAndActivateInset(LCursor & cur, bool front);
357 void write(Buffer const & buf, std::ostream & os) const;
358 /// returns whether we've seen our usual 'end' marker
359 bool read(Buffer const & buf, LyXLex & lex);
366 int cursorX(CursorSlice const & cursor) const;
368 int cursorY(CursorSlice const & cursor) const;
371 friend class LyXScreen;
380 /// the current font settings
381 LyXFont current_font;
383 LyXFont real_current_font;
384 /// our buffer's default layout font
385 LyXFont defaultfont_;
387 int background_color_;
389 /// only the top-level LyXText has this non-zero
390 BufferView * bv_owner;
395 ParagraphList paragraphs_;
397 /// absolute document pixel coordinates of this LyXText
401 /// our 'outermost' Font
405 double fill_separator(Row const & row) const;
407 double fill_hfill(Row const & row) const;
410 fill_label_hfill(ParagraphList::iterator pit, Row const & row) const;
413 /// return past-the-last paragraph influenced by a layout
415 ParagraphList::iterator undoSpan(ParagraphList::iterator pit);
417 /// rebreaks the given par
418 void redoParagraphInternal(ParagraphList::iterator pit);
419 /// used in setlayout
420 void makeFontEntriesLayoutSpecific(BufferParams const &, Paragraph & par);
422 /// Calculate and set the height of the row
423 void setHeightOfRow(ParagraphList::iterator, Row & row);
425 // fix the cursor `cur' after a characters has been deleted at `where'
426 // position. Called by deleteEmptyParagraphMechanism
427 void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
429 /// delete double space (false) or empty paragraphs (true) around old_cursor
430 bool deleteEmptyParagraphMechanism(
432 CursorSlice const & old_cursor);
435 void setCounter(Buffer const &, ParagraphList::iterator pit);
437 void deleteWordForward(LCursor & cur);
439 void deleteWordBackward(LCursor & cur);
441 void deleteLineForward(LCursor & cur);
443 /// sets row.end to the pos value *after* which a row should break.
444 /// for example, the pos after which isNewLine(pos) == true
445 void rowBreakPoint(ParagraphList::iterator pit, Row & row) const;
446 /// sets row.width to the minimum space a row needs on the screen in pixel
447 void setRowWidth(ParagraphList::iterator pit, Row & row) const;
448 /// the minimum space a manual label needs on the screen in pixels
449 int labelFill(ParagraphList::iterator pit, Row const & row) const;
451 int labelEnd(ParagraphList::iterator pit) const;
455 /// set 'number' font property
456 void number(LCursor & cur);
459 /// return the default height of a row in pixels, considering font zoom
460 int defaultRowHeight();
463 std::string expandLabel(LyXTextClass const & textclass,
464 LyXLayout_ptr const & layout, bool appendix);