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"
21 #include "lyxtextclass.h"
22 #include "ParagraphList_fwd.h"
23 #include "RowList_fwd.h"
24 #include "textcursor.h"
26 #include "insets/inset.h"
44 This class used to hold the mapping between buffer paragraphs and
45 screen rows. Nowadays, the Paragraphs take care of their rows
46 themselves and this contains just most of the code for manipulating
47 them and interaction with the Cursor.
50 // The inheritance from TextCursor should go. It's just there to ease
52 class LyXText : public TextCursor {
56 LyXText(BufferView *, InsetText *, bool ininset, ParagraphList & plist);
58 void init(BufferView *);
61 /// update y coordinate cache of all paragraphs
62 void updateParPositions();
64 LyXFont getFont(ParagraphList::iterator pit, lyx::pos_type pos) const;
66 LyXFont getLayoutFont(ParagraphList::iterator pit) const;
68 LyXFont getLabelFont(ParagraphList::iterator pit) const;
70 void setCharFont(ParagraphList::iterator pit,
71 lyx::pos_type pos, LyXFont const & font);
72 void setCharFont(ParagraphList::iterator pit,
74 LyXFont const & font, bool toggleall);
76 /// what you expect when pressing <enter> at cursor position
77 void breakParagraph(ParagraphList & paragraphs, char keep_layout = 0);
79 /** set layout over selection and make a total rebreak of
82 ParagraphList::iterator
83 setLayout(LyXCursor & actual_cursor,
84 LyXCursor & selection_start,
85 LyXCursor & selection_end,
86 std::string const & layout);
88 void setLayout(std::string const & layout);
90 /// Increase or decrease the nesting depth of the selected paragraph(s)
91 void changeDepth(bv_funcs::DEPTH_CHANGE type);
93 /// Returns whether something would be changed by changeDepth
94 bool changeDepthAllowed(bv_funcs::DEPTH_CHANGE type);
96 /// get the depth at current cursor position
99 /** set font over selection and make a total rebreak of those
101 toggleall defaults to false.
103 void setFont(LyXFont const &, bool toggleall = false);
105 /// rebreaks all paragaphs between the given pars.
106 void redoParagraphs(ParagraphList::iterator begin,
107 ParagraphList::iterator end);
108 /// rebreaks the given par
109 void redoParagraph(ParagraphList::iterator pit);
111 /// rebreaks the cursor par
112 void redoParagraph();
115 void toggleFree(LyXFont const &, bool toggleall = false);
118 std::string getStringToIndex();
120 /** insert a character, moves all the following breaks in the
121 same Paragraph one to the right and make a little rebreak
123 void insertChar(char c);
125 void insertInset(InsetOld * inset);
127 /// a full rebreak of the whole text
129 /// compute text metrics
130 void metrics(MetricsInfo & mi, Dimension & dim);
131 /// draw text (only used for insets)
132 void draw(PainterInfo & pi, int x, int y) const;
135 DispatchResult dispatch(FuncRequest const & cmd);
139 BufferView * bv() const;
141 friend class LyXScreen;
143 /// returns an iterator pointing to a cursor paragraph
144 ParagraphList::iterator getPar(LyXCursor const & cursor) const;
146 ParagraphList::iterator getPar(lyx::paroffset_type par) const;
148 int parOffset(ParagraphList::iterator pit) const;
150 ParagraphList::iterator cursorPar() const;
152 RowList::iterator cursorRow() const;
154 /** returns an iterator pointing to the row near the specified
155 * y-coordinate (relative to the whole text). y is set to the
156 * real beginning of this row
158 RowList::iterator getRowNearY(int y,
159 ParagraphList::iterator & pit) const;
161 /** returns the column near the specified x-coordinate of the row
162 x is set to the real beginning of this column
164 lyx::pos_type getColumnNearX(ParagraphList::iterator pit,
165 Row const & row, int & x, bool & boundary) const;
167 /// need the selection cursor:
170 void clearSelection();
172 /// select the word we need depending on word_location
173 void getWord(LyXCursor & from, LyXCursor & to, lyx::word_location const);
174 /// just selects the word the cursor is in
175 void selectWord(lyx::word_location loc);
176 /// returns the inset at cursor (if it exists), 0 otherwise
177 InsetOld * getInset() const;
179 /// accept selected change
182 /// reject selected change
185 /// re-computes the cached coordinates in the cursor
188 void setCursor(ParagraphList::iterator pit, lyx::pos_type pos);
189 /// returns true if par was empty and was removed
190 bool setCursor(lyx::paroffset_type par, lyx::pos_type pos,
191 bool setfont = true, bool boundary = false);
193 void setCursor(LyXCursor &, lyx::paroffset_type par,
194 lyx::pos_type pos, bool boundary = false);
196 void setCursorIntern(lyx::paroffset_type par, lyx::pos_type pos,
197 bool setfont = true, bool boundary = false);
199 void setCurrentFont();
202 void recUndo(lyx::paroffset_type first, lyx::paroffset_type last) const;
204 void recUndo(lyx::paroffset_type first) const;
206 void setCursorFromCoordinates(int x, int y);
208 void setCursorFromCoordinates(LyXCursor &, int x, int y);
210 void cursorUp(bool selecting = false);
212 void cursorDown(bool selecting = false);
214 bool cursorLeft(bool internal = true);
216 bool cursorRight(bool internal = true);
218 void cursorLeftOneWord();
220 void cursorRightOneWord();
222 void cursorUpParagraph();
224 void cursorDownParagraph();
230 void cursorPrevious();
242 bool selectWordWhenUnderCursor(lyx::word_location);
248 text_capitalization = 1,
252 /// Change the case of the word at cursor position.
253 void changeCase(TextCase action);
258 void cutSelection(bool doclear = true, bool realcut = true);
260 void copySelection();
262 void pasteSelection(size_t sel_index = 0);
264 /** the DTP switches for paragraphs. LyX will store the top settings
265 always in the first physical paragraph, the bottom settings in the
266 last. When a paragraph is broken, the top settings rest, the bottom
267 settings are given to the new one.
270 Spacing const & spacing,
272 std::string const & labelwidthstring,
275 /* these things are for search and replace */
278 * Sets the selection from the current cursor position to length
279 * characters to the right. No safety checks.
281 void setSelectionRange(lyx::pos_type length);
283 /** simple replacing. The font of the first selected character
286 void replaceSelectionWithString(std::string const & str);
288 /// needed to insert the selection
289 void insertStringAsLines(std::string const & str);
290 /// needed to insert the selection
291 void insertStringAsParagraphs(std::string const & str);
293 /// Find next inset of some specified type.
294 bool gotoNextInset(std::vector<InsetOld::Code> const & codes,
295 std::string const & contents = std::string());
297 void gotoInset(std::vector<InsetOld::Code> const & codes,
300 void gotoInset(InsetOld::Code code, bool same_content);
302 /// current max text width
303 int textWidth() const;
305 /// updates all counters
306 void updateCounters();
307 /// Returns an inset if inset was hit, or 0 if not.
308 InsetOld * checkInsetHit(int x, int y);
311 int singleWidth(ParagraphList::iterator pit, lyx::pos_type pos) const;
313 int singleWidth(ParagraphList::iterator pit,
314 lyx::pos_type pos, char c, LyXFont const & Font) const;
316 /// return the color of the canvas
317 LColor_color backgroundColor() const;
320 * Returns the left beginning of the text.
321 * This information cannot be taken from the layout object, because
322 * in LaTeX the beginning of the text fits in some cases
323 * (for example sections) exactly the label-width.
325 int leftMargin(ParagraphList::iterator pit, lyx::pos_type pos) const;
326 int leftMargin(ParagraphList::iterator pit) const;
328 int rightMargin(Paragraph const & par) const;
330 /** this calculates the specified parameters. needed when setting
331 * the cursor and when creating a visible row */
332 void prepareToPrint(ParagraphList::iterator pit, Row & row) const;
335 // special owner functions
337 ParagraphList & ownerParagraphs() const;
339 /// return true if this is owned by an inset.
340 bool isInInset() const;
343 ParagraphList::iterator firstPar() const;
345 ParagraphList::iterator lastPar() const;
347 ParagraphList::iterator endPar() const;
349 /// return first row of text
350 RowList::iterator firstRow() const;
351 /// return last row of text
352 RowList::iterator lastRow() const;
353 /// return row "behind" last row of text
354 RowList::iterator endRow() const;
355 /// return next row crossing paragraph boundaries
356 void nextRow(ParagraphList::iterator & pit,
357 RowList::iterator & rit) const;
358 /// return previous row crossing paragraph boundaries
359 void previousRow(ParagraphList::iterator & pit,
360 RowList::iterator & rit) const;
362 /// is this row the last in the text?
363 bool isLastRow(ParagraphList::iterator pit, Row const & row) const;
364 /// is this row the first in the text?
365 bool isFirstRow(ParagraphList::iterator pit, Row const & row) const;
368 std::string selectionAsString(Buffer const & buffer, bool label) const;
370 double spacing(Paragraph const &) const;
372 void cursorLeftOneWord(LyXCursor &);
374 void cursorRightOneWord(LyXCursor &);
377 DispatchResult moveRight();
379 DispatchResult moveLeft();
381 DispatchResult moveRightIntern(bool front,
382 bool activate_inset, bool selecting);
384 DispatchResult moveLeftIntern(bool front,
385 bool activate_inset, bool selecting);
387 DispatchResult moveUp();
389 DispatchResult moveDown();
391 bool checkAndActivateInset(bool front);
401 /// the current font settings
402 LyXFont current_font;
404 LyXFont real_current_font;
405 /// our buffer's default layout font
406 LyXFont defaultfont_;
408 InsetText * inset_owner;
410 /// only the top-level LyXText has this non-zero
411 BufferView * bv_owner;
418 ParagraphList * paragraphs_;
420 /// absolute document pixel coordinates of this LyXText
426 /// rebreaks the given par
427 void redoParagraphInternal(ParagraphList::iterator pit);
430 float getCursorX(ParagraphList::iterator pit,
431 Row const & row, lyx::pos_type pos, bool boundary) const;
432 /// used in setlayout
433 void makeFontEntriesLayoutSpecific(BufferParams const &, Paragraph & par);
435 /// Calculate and set the height of the row
436 void setHeightOfRow(ParagraphList::iterator, Row & row);
438 // fix the cursor `cur' after a characters has been deleted at `where'
439 // position. Called by deleteEmptyParagraphMechanism
440 void fixCursorAfterDelete(LyXCursor & cur, LyXCursor const & where);
442 /// delete double space (false) or empty paragraphs (true) around old_cursor
443 bool deleteEmptyParagraphMechanism(LyXCursor const & old_cursor);
446 void setCounter(Buffer const &, ParagraphList::iterator pit);
448 void deleteWordForward();
450 void deleteWordBackward();
452 void deleteLineForward();
454 /// sets row.end to the pos value *after* which a row should break.
455 /// for example, the pos after which isNewLine(pos) == true
456 void rowBreakPoint(ParagraphList::iterator pit, Row & row) const;
458 /// sets row.witdh to the minimum space a row needs on the screen in pixel
459 void fill(ParagraphList::iterator pit, Row & row, int workwidth) const;
462 * returns the minimum space a manual label needs on the
465 int labelFill(ParagraphList::iterator pit, Row const & row) const;
468 int labelEnd(ParagraphList::iterator pit) const;
472 /// set 'number' font property
474 /// is the cursor paragraph right-to-left?
478 /// return the default height of a row in pixels, considering font zoom
479 extern int defaultRowHeight();
482 std::string expandLabel(LyXTextClass const & textclass,
483 LyXLayout_ptr const & layout, bool appendix);