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"
25 #include "insets/inset.h"
43 /// This class encapsulates the main text data and operations in LyX
47 LyXText(BufferView *, bool ininset);
49 void init(BufferView *);
52 /// update y coordinate cache of all paragraphs
53 void updateParPositions();
55 LyXFont getFont(ParagraphList::iterator pit, lyx::pos_type pos) const;
57 LyXFont getLayoutFont(ParagraphList::iterator pit) const;
59 LyXFont getLabelFont(ParagraphList::iterator pit) const;
61 void setCharFont(ParagraphList::iterator pit,
62 lyx::pos_type pos, LyXFont const & font);
63 void setCharFont(ParagraphList::iterator pit,
65 LyXFont const & font, bool toggleall);
67 /// what you expect when pressing <enter> at cursor position
68 void breakParagraph(ParagraphList & paragraphs, char keep_layout = 0);
70 /** set layout over selection and make a total rebreak of
73 ParagraphList::iterator
74 setLayout(ParagraphList::iterator start,
75 ParagraphList::iterator end,
76 std::string const & layout);
78 void setLayout(std::string const & layout);
80 /// Increase or decrease the nesting depth of the selected paragraph(s)
81 void changeDepth(bv_funcs::DEPTH_CHANGE type);
83 /// Returns whether something would be changed by changeDepth
84 bool changeDepthAllowed(bv_funcs::DEPTH_CHANGE type);
86 /// get the depth at current cursor position
89 /** set font over selection and make a total rebreak of those
91 toggleall defaults to false.
93 void setFont(LyXFont const &, bool toggleall = false);
95 /// rebreaks all paragaphs between the given pars.
96 void redoParagraphs(ParagraphList::iterator begin,
97 ParagraphList::iterator end);
98 /// rebreaks the given par
99 void redoParagraph(ParagraphList::iterator pit);
101 /// rebreaks the cursor par
102 void redoParagraph();
105 void toggleFree(LyXFont const &, bool toggleall = false);
108 std::string getStringToIndex();
110 /** insert a character, moves all the following breaks in the
111 same Paragraph one to the right and make a little rebreak
113 void insertChar(char c);
115 void insertInset(InsetOld * 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;
124 /// try to handle that request
125 DispatchResult dispatch(LCursor & cur, FuncRequest const & cmd);
129 BufferView * bv() const;
131 friend class LyXScreen;
133 /// returns an iterator pointing to a cursor paragraph
134 ParagraphList::iterator getPar(CursorSlice const & cursor) const;
136 ParagraphList::iterator getPar(lyx::paroffset_type par) const;
138 int parOffset(ParagraphList::iterator pit) const;
140 ParagraphList::iterator cursorPar() const;
142 RowList::iterator cursorRow() const;
144 /** returns an iterator pointing to the row near the specified
145 * y-coordinate (relative to the whole text). y is set to the
146 * real beginning of this row
148 RowList::iterator getRowNearY(int y,
149 ParagraphList::iterator & pit) const;
151 /** returns the column near the specified x-coordinate of the row
152 x is set to the real beginning of this column
154 lyx::pos_type getColumnNearX(ParagraphList::iterator pit,
155 Row const & row, int & x, bool & boundary) const;
157 /** Find the word under \c from in the relative location
158 * defined by \c word_location.
159 * @param from return here the start of the word
160 * @param to return here the end of the word
162 void getWord(CursorSlice & from, CursorSlice & to, lyx::word_location const);
163 /// just selects the word the cursor is in
164 void selectWord(lyx::word_location loc);
165 /// returns the inset at cursor (if it exists), 0 otherwise
166 InsetOld * getInset() const;
168 /// accept selected change
171 /// reject selected change
175 void setCursor(ParagraphList::iterator pit, lyx::pos_type pos);
176 /// returns true if par was empty and was removed
177 bool setCursor(lyx::paroffset_type par, lyx::pos_type pos,
178 bool setfont = true, bool boundary = false);
180 void setCursor(CursorSlice &, lyx::paroffset_type par,
181 lyx::pos_type pos, bool boundary = false);
183 void setCursorIntern(lyx::paroffset_type par, lyx::pos_type pos,
184 bool setfont = true, bool boundary = false);
186 void setCurrentFont();
189 void recUndo(lyx::paroffset_type first, lyx::paroffset_type last) const;
191 void recUndo(lyx::paroffset_type first) const;
193 void setCursorFromCoordinates(int x, int y);
195 void setCursorFromCoordinates(CursorSlice &, int x, int y);
197 void cursorUp(bool selecting = false);
199 void cursorDown(bool selecting = false);
201 bool cursorLeft(bool internal = true);
203 bool cursorRight(bool internal = true);
205 void cursorLeftOneWord();
207 void cursorRightOneWord();
209 void cursorUpParagraph();
211 void cursorDownParagraph();
217 void cursorPrevious();
229 bool selectWordWhenUnderCursor(lyx::word_location);
235 text_capitalization = 1,
239 /// Change the case of the word at cursor position.
240 void changeCase(TextCase action);
245 void cutSelection(bool doclear = true, bool realcut = true);
247 void copySelection();
249 void pasteSelection(size_t sel_index = 0);
251 /** the DTP switches for paragraphs. LyX will store the top settings
252 always in the first physical paragraph, the bottom settings in the
253 last. When a paragraph is broken, the top settings rest, the bottom
254 settings are given to the new one.
257 Spacing const & spacing,
259 std::string const & labelwidthstring,
262 /* these things are for search and replace */
265 * Sets the selection from the current cursor position to length
266 * characters to the right. No safety checks.
268 void setSelectionRange(lyx::pos_type length);
270 /** simple replacing. The font of the first selected character
273 void replaceSelectionWithString(std::string const & str);
275 /// needed to insert the selection
276 void insertStringAsLines(std::string const & str);
277 /// needed to insert the selection
278 void insertStringAsParagraphs(std::string const & str);
280 /// Find next inset of some specified type.
281 bool gotoNextInset(std::vector<InsetOld::Code> const & codes,
282 std::string const & contents = std::string());
284 void gotoInset(std::vector<InsetOld::Code> const & codes,
287 void gotoInset(InsetOld::Code code, bool same_content);
289 /// current max text width
290 int textWidth() const;
292 /// updates all counters
293 void updateCounters();
294 /// Returns an inset if inset was hit, or 0 if not.
295 InsetOld * checkInsetHit(int x, int y);
298 int singleWidth(ParagraphList::iterator pit, lyx::pos_type pos) const;
300 int singleWidth(ParagraphList::iterator pit,
301 lyx::pos_type pos, char c, LyXFont const & Font) const;
303 /// return the color of the canvas
304 LColor_color backgroundColor() const;
307 * Returns the left beginning of the text.
308 * This information cannot be taken from the layout object, because
309 * in LaTeX the beginning of the text fits in some cases
310 * (for example sections) exactly the label-width.
312 int leftMargin(ParagraphList::iterator pit, lyx::pos_type pos) const;
313 int leftMargin(ParagraphList::iterator pit) const;
315 int rightMargin(Paragraph const & par) const;
317 /** this calculates the specified parameters. needed when setting
318 * the cursor and when creating a visible row */
319 void prepareToPrint(ParagraphList::iterator pit, Row & row) const;
322 // special owner functions
324 ParagraphList & paragraphs() const;
326 /// return true if this is owned by an inset.
327 bool isInInset() const;
330 ParagraphList::iterator firstPar() const;
332 ParagraphList::iterator lastPar() const;
334 ParagraphList::iterator endPar() const;
336 /// return first row of text
337 RowList::iterator firstRow() const;
338 /// return last row of text
339 RowList::iterator lastRow() const;
340 /// return row "behind" last row of text
341 RowList::iterator endRow() const;
342 /// return next row crossing paragraph boundaries
343 void nextRow(ParagraphList::iterator & pit,
344 RowList::iterator & rit) const;
345 /// return previous row crossing paragraph boundaries
346 void previousRow(ParagraphList::iterator & pit,
347 RowList::iterator & rit) const;
349 /// is this row the last in the text?
350 bool isLastRow(ParagraphList::iterator pit, Row const & row) const;
351 /// is this row the first in the text?
352 bool isFirstRow(ParagraphList::iterator pit, Row const & row) const;
355 std::string selectionAsString(Buffer const & buffer, bool label) const;
357 double spacing(Paragraph const &) const;
359 void cursorLeftOneWord(CursorSlice &);
361 void cursorRightOneWord(CursorSlice &);
364 DispatchResult moveRight();
366 DispatchResult moveLeft();
368 DispatchResult moveRightIntern(bool front,
369 bool activate_inset, bool selecting);
371 DispatchResult moveLeftIntern(bool front,
372 bool activate_inset, bool selecting);
374 DispatchResult moveUp();
376 DispatchResult moveDown();
378 bool checkAndActivateInset(bool front);
381 void write(Buffer const & buf, std::ostream & os) const;
382 /// returns whether we've seen our usual 'end' marker
383 bool read(Buffer const & buf, LyXLex & lex);
394 int cursorX(CursorSlice const & cursor) const;
396 int cursorY(CursorSlice const & cursor) const;
398 /// the topmost cursor slice
399 CursorSlice & cursor();
400 /// the topmost cursor slice
401 CursorSlice const & cursor() const;
402 /// access to the selection anchor
403 CursorSlice & anchor();
404 /// access to the selection anchor
405 CursorSlice const & anchor() const;
414 /// the current font settings
415 LyXFont current_font;
417 LyXFont real_current_font;
418 /// our buffer's default layout font
419 LyXFont defaultfont_;
421 int background_color_;
423 /// only the top-level LyXText has this non-zero
424 BufferView * bv_owner;
431 ParagraphList paragraphs_;
433 /// absolute document pixel coordinates of this LyXText
437 /// our 'outermost' Font
442 /// return past-the-last paragraph influenced by a layout
444 ParagraphList::iterator undoSpan(ParagraphList::iterator pit);
446 /// rebreaks the given par
447 void redoParagraphInternal(ParagraphList::iterator pit);
448 /// used in setlayout
449 void makeFontEntriesLayoutSpecific(BufferParams const &, Paragraph & par);
451 /// Calculate and set the height of the row
452 void setHeightOfRow(ParagraphList::iterator, Row & row);
454 // fix the cursor `cur' after a characters has been deleted at `where'
455 // position. Called by deleteEmptyParagraphMechanism
456 void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
458 /// delete double space (false) or empty paragraphs (true) around old_cursor
459 bool deleteEmptyParagraphMechanism(CursorSlice const & old_cursor);
462 void setCounter(Buffer const &, ParagraphList::iterator pit);
464 void deleteWordForward();
466 void deleteWordBackward();
468 void deleteLineForward();
470 /// sets row.end to the pos value *after* which a row should break.
471 /// for example, the pos after which isNewLine(pos) == true
472 void rowBreakPoint(ParagraphList::iterator pit, Row & row) const;
474 /// sets row.witdh to the minimum space a row needs on the screen in pixel
475 void fill(ParagraphList::iterator pit, Row & row, int workwidth) const;
478 * returns the minimum space a manual label needs on the
481 int labelFill(ParagraphList::iterator pit, Row const & row) const;
484 int labelEnd(ParagraphList::iterator pit) const;
488 /// set 'number' font property
490 /// is the cursor paragraph right-to-left?
494 /// return the default height of a row in pixels, considering font zoom
495 extern int defaultRowHeight();
498 std::string expandLabel(LyXTextClass const & textclass,
499 LyXLayout_ptr const & layout, bool appendix);