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 /// what type of depth change to make
95 void changeDepth(LCursor & cur, DEPTH_CHANGE type);
97 /// Returns whether something would be changed by changeDepth
98 bool changeDepthAllowed(LCursor & cur, DEPTH_CHANGE type) const;
100 /// Set font over selection paragraphs and rebreak.
101 void setFont(LCursor & cur, LyXFont const &, bool toggleall = false);
103 /// rebreaks all paragaphs between the given pars.
104 void redoParagraphs(ParagraphList::iterator begin,
105 ParagraphList::iterator end);
106 /// rebreaks the given par
107 void redoParagraph(ParagraphList::iterator pit);
108 /// rebreaks the cursor par
109 void redoParagraph(LCursor & cur);
112 void toggleFree(LCursor & cur, LyXFont const &, bool toggleall = false);
115 std::string getStringToIndex(LCursor & cur);
117 /// insert a character at cursor position
118 void insertChar(LCursor & cur, char c);
119 /// insert an inset at cursor position
120 void insertInset(LCursor & cur, InsetBase * inset);
122 /// a full rebreak of the whole text
124 /// compute text metrics
125 void metrics(MetricsInfo & mi, Dimension & dim);
126 /// draw text (only used for insets)
127 void draw(PainterInfo & pi, int x, int y) const;
128 /// draw textselection
129 void drawSelection(PainterInfo & pi, int x, int y) const;
130 /// returns distance of this cell to the point given by x and y
131 // assumes valid position and size cache
132 int dist(int x, int y) const;
134 /// try to handle that request
135 void dispatch(LCursor & cur, FuncRequest & cmd);
136 /// do we want to handle this event?
137 bool getStatus(LCursor & cur, FuncRequest const & cmd,
138 FuncStatus & status) const;
140 /// access to out BufferView. This should go...
142 /// access to out BufferView. This should go...
143 BufferView * bv() const;
145 /// returns an iterator pointing to a cursor paragraph
146 ParagraphList::iterator getPar(CursorSlice const & cursor) const;
148 ParagraphList::iterator getPar(par_type par) const;
150 int parOffset(ParagraphList::iterator pit) const;
151 // Returns the current font and depth as a message.
152 std::string LyXText::currentState(LCursor & cur);
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 pos_type getColumnNearX(ParagraphList::iterator pit,
165 Row const & row, int & x, bool & boundary) const;
167 /** Find the word under \c from in the relative location
168 * defined by \c word_location.
169 * @param from return here the start of the word
170 * @param to return here the end of the word
172 void getWord(CursorSlice & from, CursorSlice & to, lyx::word_location const);
173 /// just selects the word the cursor is in
174 void selectWord(LCursor & cur, lyx::word_location loc);
176 /// accept selected change
177 void acceptChange(LCursor & cur);
178 /// reject selected change
179 void rejectChange(LCursor & cur);
181 /// returns true if par was empty and was removed
182 bool setCursor(LCursor & cur, par_type par, pos_type pos,
183 bool setfont = true, bool boundary = false);
185 void setCursor(CursorSlice &, par_type par,
186 pos_type pos, bool boundary = false);
188 void setCursorIntern(LCursor & cur, par_type par,
189 pos_type pos, bool setfont = true, bool boundary = false);
191 void setCurrentFont(LCursor & cur);
194 void recUndo(par_type first, par_type last) const;
196 void recUndo(par_type first) const;
198 void setCursorFromCoordinates(LCursor & cur, int x, int y);
200 InsetBase * editXY(LCursor & cur, int x, int y);
202 void cursorUp(LCursor & cur);
204 void cursorDown(LCursor & cur);
206 void cursorLeft(LCursor & cur);
208 void cursorRight(LCursor & cur);
210 void cursorLeftOneWord(LCursor & cur);
212 void cursorRightOneWord(LCursor & cur);
214 void cursorUpParagraph(LCursor & cur);
216 void cursorDownParagraph(LCursor & cur);
218 void cursorHome(LCursor & cur);
220 void cursorEnd(LCursor & cur);
222 void cursorPrevious(LCursor & cur);
224 void cursorNext(LCursor & cur);
226 void cursorTop(LCursor & cur);
228 void cursorBottom(LCursor & cur);
230 void Delete(LCursor & cur);
232 void 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);
248 bool toggleInset(LCursor & cur);
250 void cutSelection(LCursor & cur, bool doclear = true, bool realcut = true);
252 void copySelection(LCursor & cur);
254 void pasteSelection(LCursor & cur, size_t sel_index = 0);
256 /** the DTP switches for paragraphs. LyX will store the top settings
257 always in the first physical paragraph, the bottom settings in the
258 last. When a paragraph is broken, the top settings rest, the bottom
259 settings are given to the new one.
261 void setParagraph(LCursor & cur,
262 Spacing const & spacing,
264 std::string const & labelwidthstring,
267 /* these things are for search and replace */
270 * Sets the selection from the current cursor position to length
271 * characters to the right. No safety checks.
273 void setSelectionRange(LCursor & cur, pos_type length);
274 /// simply replace using the font of the first selected character
275 void replaceSelectionWithString(LCursor & cur, std::string const & str);
276 /// replace selection helper
277 void replaceSelection(LCursor & cur);
279 /// needed to insert the selection
280 void insertStringAsLines(LCursor & cur, std::string const & str);
281 /// needed to insert the selection
282 void insertStringAsParagraphs(LCursor & cur, std::string const & str);
284 /// Find next inset of some specified type.
285 bool gotoNextInset(LCursor & cur,
286 std::vector<InsetOld_code> const & codes,
287 std::string const & contents = std::string());
289 void gotoInset(LCursor & cur,
290 std::vector<InsetOld_code> const & codes, bool same_content);
292 void gotoInset(LCursor & cur, InsetOld_code code, bool same_content);
294 /// current text width
297 /// current text heigth
300 /// updates all counters
301 void updateCounters();
302 /// Returns an inset if inset was hit, or 0 if not.
303 InsetBase * checkInsetHit(int x, int y);
306 int singleWidth(ParagraphList::iterator pit, pos_type pos) const;
308 int singleWidth(ParagraphList::iterator pit,
309 pos_type pos, char c, LyXFont const & Font) const;
311 /// return the color of the canvas
312 LColor_color backgroundColor() const;
315 * Returns the left beginning of the text.
316 * This information cannot be taken from the layout object, because
317 * in LaTeX the beginning of the text fits in some cases
318 * (for example sections) exactly the label-width.
320 int leftMargin(ParagraphList::iterator pit, pos_type pos) const;
321 int leftMargin(ParagraphList::iterator pit) const;
323 int rightMargin(Paragraph const & par) const;
325 /** this calculates the specified parameters. needed when setting
326 * the cursor and when creating a visible row */
328 computeRowMetrics(ParagraphList::iterator pit, Row const & row) const;
330 /// access to our paragraphs
331 ParagraphList & paragraphs() const;
332 /// return true if this is the main text
333 bool isMainText() const;
335 /// return first row of text
336 RowList::iterator firstRow() const;
337 /// return last row of text
338 RowList::iterator lastRow() const;
339 /// return row "behind" last row of text
340 RowList::iterator endRow() const;
341 /// return next row crossing paragraph boundaries
342 void nextRow(ParagraphList::iterator & pit,
343 RowList::iterator & rit) const;
344 /// return previous row crossing paragraph boundaries
345 void previousRow(ParagraphList::iterator & pit,
346 RowList::iterator & rit) const;
348 /// is this row the last in the text?
349 bool isLastRow(ParagraphList::iterator pit, Row const & row) const;
350 /// is this row the first in the text?
351 bool isFirstRow(ParagraphList::iterator pit, Row const & row) const;
354 double spacing(Paragraph const & par) const;
355 /// make a suggestion for a label
356 std::string getPossibleLabel(LCursor & cur) const;
357 /// is this paragraph right-to-left?
358 bool isRTL(Paragraph const & par) const;
360 bool checkAndActivateInset(LCursor & cur, bool front);
363 void write(Buffer const & buf, std::ostream & os) const;
364 /// returns whether we've seen our usual 'end' marker
365 bool read(Buffer const & buf, LyXLex & lex);
372 int cursorX(CursorSlice const & cursor) const;
374 int cursorY(CursorSlice const & cursor) const;
377 friend class LyXScreen;
386 /// the current font settings
387 LyXFont current_font;
389 LyXFont real_current_font;
390 /// our buffer's default layout font
391 LyXFont defaultfont_;
393 int background_color_;
395 /// only the top-level LyXText has this non-zero
396 BufferView * bv_owner;
401 ParagraphList paragraphs_;
403 /// absolute document pixel coordinates of this LyXText
407 /// our 'outermost' Font
411 double fill_separator(Row const & row) const;
413 double fill_hfill(Row const & row) const;
416 fill_label_hfill(ParagraphList::iterator pit, Row const & row) const;
419 /// return past-the-last paragraph influenced by a layout
421 ParagraphList::iterator undoSpan(ParagraphList::iterator pit);
423 /// rebreaks the given par
424 void redoParagraphInternal(ParagraphList::iterator pit);
425 /// used in setlayout
426 void makeFontEntriesLayoutSpecific(BufferParams const &, Paragraph & par);
428 /// Calculate and set the height of the row
429 void setHeightOfRow(ParagraphList::iterator, Row & row);
431 // fix the cursor `cur' after a characters has been deleted at `where'
432 // position. Called by deleteEmptyParagraphMechanism
433 void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
435 /// delete double space (false) or empty paragraphs (true) around old_cursor
436 bool deleteEmptyParagraphMechanism(
438 CursorSlice const & old_cursor);
441 void setCounter(Buffer const &, ParagraphList::iterator pit);
443 void deleteWordForward(LCursor & cur);
445 void deleteWordBackward(LCursor & cur);
447 void deleteLineForward(LCursor & cur);
449 /// sets row.end to the pos value *after* which a row should break.
450 /// for example, the pos after which isNewLine(pos) == true
451 void rowBreakPoint(ParagraphList::iterator pit, Row & row) const;
452 /// sets row.width to the minimum space a row needs on the screen in pixel
453 void setRowWidth(ParagraphList::iterator pit, Row & row) const;
454 /// the minimum space a manual label needs on the screen in pixels
455 int labelFill(ParagraphList::iterator pit, Row const & row) const;
457 int labelEnd(ParagraphList::iterator pit) const;
461 /// set 'number' font property
462 void number(LCursor & cur);
465 /// return the default height of a row in pixels, considering font zoom
466 int defaultRowHeight();
469 std::string expandLabel(LyXTextClass const & textclass,
470 LyXLayout_ptr const & layout, bool appendix);