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"
48 /// This class encapsulates the main text data and operations in LyX
52 LyXText(BufferView *, bool ininset);
54 void init(BufferView *);
56 /// update y coordinate cache of all paragraphs
57 void updateParPositions();
59 LyXFont getFont(ParagraphList::iterator pit, lyx::pos_type pos) const;
61 LyXFont getLayoutFont(ParagraphList::iterator pit) const;
63 LyXFont getLabelFont(ParagraphList::iterator pit) const;
65 void setCharFont(ParagraphList::iterator pit,
66 lyx::pos_type pos, LyXFont const & font);
67 void setCharFont(ParagraphList::iterator pit,
69 LyXFont const & font, bool toggleall);
71 /// what you expect when pressing <enter> at cursor position
72 void breakParagraph(LCursor & cur, char keep_layout = 0);
74 /** set layout over selection and make a total rebreak of
77 ParagraphList::iterator
78 setLayout(ParagraphList::iterator start,
79 ParagraphList::iterator end,
80 std::string const & layout);
82 void setLayout(std::string const & layout);
84 /// Increase or decrease the nesting depth of the selected paragraph(s)
85 void changeDepth(bv_funcs::DEPTH_CHANGE type);
87 /// Returns whether something would be changed by changeDepth
88 bool changeDepthAllowed(bv_funcs::DEPTH_CHANGE type);
90 /// get the depth at current cursor position
93 /** set font over selection and make a total rebreak of those
95 toggleall defaults to false.
97 void setFont(LyXFont const &, bool toggleall = false);
99 /// rebreaks all paragaphs between the given pars.
100 void redoParagraphs(ParagraphList::iterator begin,
101 ParagraphList::iterator end);
102 /// rebreaks the given par
103 void redoParagraph(ParagraphList::iterator pit);
104 /// rebreaks the cursor par
105 void redoParagraph();
108 void toggleFree(LyXFont const &, bool toggleall = false);
111 std::string getStringToIndex();
113 /// insert a character at cursor position
114 void insertChar(char c);
115 /// insert an inset at cursor position
116 void insertInset(InsetBase * inset);
118 /// a full rebreak of the whole text
120 /// compute text metrics
121 void metrics(MetricsInfo & mi, Dimension & dim);
122 /// draw text (only used for insets)
123 void draw(PainterInfo & pi, int x, int y) const;
124 /// draw textselection
125 void drawSelection(PainterInfo & pi, int x, int y) const;
127 /// try to handle that request
128 DispatchResult dispatch(LCursor & cur, FuncRequest const & cmd);
129 /// access to out BufferView. This should go...
131 /// access to out BufferView. This should go...
132 BufferView * bv() const;
134 /// returns an iterator pointing to a cursor paragraph
135 ParagraphList::iterator getPar(CursorSlice const & cursor) const;
137 ParagraphList::iterator getPar(lyx::paroffset_type par) const;
139 int parOffset(ParagraphList::iterator pit) const;
141 ParagraphList::iterator cursorPar() const;
143 RowList::iterator cursorRow() const;
145 /** returns an iterator pointing to the row near the specified
146 * y-coordinate (relative to the whole text). y is set to the
147 * real beginning of this row
149 RowList::iterator getRowNearY(int y,
150 ParagraphList::iterator & pit) const;
152 /** returns the column near the specified x-coordinate of the row
153 x is set to the real beginning of this column
155 lyx::pos_type getColumnNearX(ParagraphList::iterator pit,
156 Row const & row, int & x, bool & boundary) const;
158 /** Find the word under \c from in the relative location
159 * defined by \c word_location.
160 * @param from return here the start of the word
161 * @param to return here the end of the word
163 void getWord(CursorSlice & from, CursorSlice & to, lyx::word_location const);
164 /// just selects the word the cursor is in
165 void selectWord(lyx::word_location loc);
167 /// accept selected change
169 /// reject selected change
172 /// returns true if par was empty and was removed
173 bool setCursor(lyx::paroffset_type par, lyx::pos_type pos,
174 bool setfont = true, bool boundary = false);
176 void setCursor(CursorSlice &, lyx::paroffset_type par,
177 lyx::pos_type pos, bool boundary = false);
179 void setCursorIntern(lyx::paroffset_type par, lyx::pos_type pos,
180 bool setfont = true, bool boundary = false);
182 void setCurrentFont();
185 void recUndo(lyx::paroffset_type first, lyx::paroffset_type last) const;
187 void recUndo(lyx::paroffset_type first) const;
189 void setCursorFromCoordinates(int x, int y);
191 void setCursorFromCoordinates(CursorSlice &, int x, int y);
193 void edit(LCursor & cur, int x, int y);
195 void cursorUp(LCursor & cur, bool selecting = false);
197 void cursorDown(LCursor & cur, bool selecting = false);
199 bool cursorLeft(LCursor & cur, bool internal = true);
201 bool cursorRight(LCursor & cur, bool internal = true);
203 void cursorLeftOneWord(LCursor & cur);
205 void cursorRightOneWord(LCursor & cur);
207 void cursorUpParagraph(LCursor & cur);
209 void cursorDownParagraph(LCursor & cur);
211 void cursorHome(LCursor & cur);
213 void cursorEnd(LCursor & cur);
215 void cursorPrevious(LCursor & cur);
217 void cursorNext(LCursor & cur);
219 void cursorTop(LCursor & cur);
221 void cursorBottom(LCursor & cur);
223 void Delete(LCursor & cur);
225 void backspace(LCursor & cur);
227 bool selectWordWhenUnderCursor(lyx::word_location);
233 text_capitalization = 1,
237 /// Change the case of the word at cursor position.
238 void changeCase(TextCase action);
243 void cutSelection(bool doclear = true, bool realcut = true);
245 void copySelection();
247 void pasteSelection(size_t sel_index = 0);
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.
255 Spacing const & spacing,
257 std::string const & labelwidthstring,
260 /* these things are for search and replace */
263 * Sets the selection from the current cursor position to length
264 * characters to the right. No safety checks.
266 void setSelectionRange(lyx::pos_type length);
267 /** simple replacing. The font of the first selected character
270 void replaceSelectionWithString(std::string const & str);
272 /// needed to insert the selection
273 void insertStringAsLines(std::string const & str);
274 /// needed to insert the selection
275 void insertStringAsParagraphs(std::string const & str);
277 /// Find next inset of some specified type.
278 bool gotoNextInset(std::vector<InsetOld_code> const & codes,
279 std::string const & contents = std::string());
281 void gotoInset(std::vector<InsetOld_code> const & codes,
284 void gotoInset(InsetOld_code code, bool same_content);
286 /// current max text width
287 int textWidth() const;
289 /// updates all counters
290 void updateCounters();
291 /// Returns an inset if inset was hit, or 0 if not.
292 InsetBase * checkInsetHit(int x, int y);
295 int singleWidth(ParagraphList::iterator pit, lyx::pos_type pos) const;
297 int singleWidth(ParagraphList::iterator pit,
298 lyx::pos_type pos, char c, LyXFont const & Font) const;
300 /// return the color of the canvas
301 LColor_color backgroundColor() const;
304 * Returns the left beginning of the text.
305 * This information cannot be taken from the layout object, because
306 * in LaTeX the beginning of the text fits in some cases
307 * (for example sections) exactly the label-width.
309 int leftMargin(ParagraphList::iterator pit, lyx::pos_type pos) const;
310 int leftMargin(ParagraphList::iterator pit) const;
312 int rightMargin(Paragraph const & par) const;
314 /** this calculates the specified parameters. needed when setting
315 * the cursor and when creating a visible row */
316 void prepareToPrint(ParagraphList::iterator pit, Row & row) const;
318 /// access to our paragraphs
319 ParagraphList & paragraphs() const;
320 /// return true if this is owned by an inset.
321 bool isInInset() const;
323 /// return first row of text
324 RowList::iterator firstRow() const;
325 /// return last row of text
326 RowList::iterator lastRow() const;
327 /// return row "behind" last row of text
328 RowList::iterator endRow() const;
329 /// return next row crossing paragraph boundaries
330 void nextRow(ParagraphList::iterator & pit,
331 RowList::iterator & rit) const;
332 /// return previous row crossing paragraph boundaries
333 void previousRow(ParagraphList::iterator & pit,
334 RowList::iterator & rit) const;
336 /// is this row the last in the text?
337 bool isLastRow(ParagraphList::iterator pit, Row const & row) const;
338 /// is this row the first in the text?
339 bool isFirstRow(ParagraphList::iterator pit, Row const & row) const;
342 double spacing(Paragraph const &) const;
345 DispatchResult moveRight(LCursor & cur);
347 DispatchResult moveLeft(LCursor & cur);
349 DispatchResult moveRightIntern(LCursor & cur, bool front,
350 bool activate_inset, bool selecting);
352 DispatchResult moveLeftIntern(LCursor & cur, bool front,
353 bool activate_inset, bool selecting);
355 DispatchResult moveUp(LCursor & cur);
357 DispatchResult moveDown(LCursor & cur);
359 bool checkAndActivateInset(LCursor & cur, bool front);
362 void write(Buffer const & buf, std::ostream & os) const;
363 /// returns whether we've seen our usual 'end' marker
364 bool read(Buffer const & buf, LyXLex & lex);
371 int cursorX(CursorSlice const & cursor) const;
373 int cursorY(CursorSlice const & cursor) const;
375 /// the current cursor slice
376 CursorSlice & cursor();
377 /// the current cursor slice
378 CursorSlice const & cursor() const;
380 friend class LyXScreen;
389 /// the current font settings
390 LyXFont current_font;
392 LyXFont real_current_font;
393 /// our buffer's default layout font
394 LyXFont defaultfont_;
396 int background_color_;
398 /// only the top-level LyXText has this non-zero
399 BufferView * bv_owner;
406 ParagraphList paragraphs_;
408 /// absolute document pixel coordinates of this LyXText
412 /// our 'outermost' Font
417 /// return past-the-last paragraph influenced by a layout
419 ParagraphList::iterator undoSpan(ParagraphList::iterator pit);
421 /// rebreaks the given par
422 void redoParagraphInternal(ParagraphList::iterator pit);
423 /// used in setlayout
424 void makeFontEntriesLayoutSpecific(BufferParams const &, Paragraph & par);
426 /// Calculate and set the height of the row
427 void setHeightOfRow(ParagraphList::iterator, Row & row);
429 // fix the cursor `cur' after a characters has been deleted at `where'
430 // position. Called by deleteEmptyParagraphMechanism
431 void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
433 /// delete double space (false) or empty paragraphs (true) around old_cursor
434 bool deleteEmptyParagraphMechanism(CursorSlice const & old_cursor);
437 void setCounter(Buffer const &, ParagraphList::iterator pit);
439 void deleteWordForward(LCursor & cur);
441 void deleteWordBackward(LCursor & cur);
443 void deleteLineForward(LCursor & cur);
445 /// sets row.end to the pos value *after* which a row should break.
446 /// for example, the pos after which isNewLine(pos) == true
447 void rowBreakPoint(ParagraphList::iterator pit, Row & row) const;
448 /// sets row.witdh to the minimum space a row needs on the screen in pixel
449 void fill(ParagraphList::iterator pit, Row & row, int workwidth) const;
450 /// the minimum space a manual label needs on the screen in pixels
451 int labelFill(ParagraphList::iterator pit, Row const & row) const;
453 int labelEnd(ParagraphList::iterator pit) const;
457 /// set 'number' font property
459 /// is the cursor paragraph right-to-left?
463 /// return the default height of a row in pixels, considering font zoom
464 int defaultRowHeight();
467 std::string expandLabel(LyXTextClass const & textclass,
468 LyXLayout_ptr const & layout, bool appendix);