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 LyXText(BufferView *, bool ininset);
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 /// get the depth at current cursor position
98 /// Set font over selection paragraphs and rebreak.
99 void setFont(LCursor & cur, LyXFont const &, bool toggleall = false);
101 /// rebreaks all paragaphs between the given pars.
102 void redoParagraphs(ParagraphList::iterator begin,
103 ParagraphList::iterator end);
104 /// rebreaks the given par
105 void redoParagraph(ParagraphList::iterator pit);
106 /// rebreaks the cursor par
107 void redoParagraph(LCursor & cur);
110 void toggleFree(LCursor & cur, LyXFont const &, bool toggleall = false);
113 std::string getStringToIndex(LCursor & cur);
115 /// insert a character at cursor position
116 void insertChar(LCursor & cur, char c);
117 /// insert an inset at cursor position
118 void insertInset(LCursor & cur, InsetBase * inset);
120 /// a full rebreak of the whole text
122 /// compute text metrics
123 void metrics(MetricsInfo & mi, Dimension & dim);
124 /// draw text (only used for insets)
125 void draw(PainterInfo & pi, int x, int y) const;
126 /// draw textselection
127 void drawSelection(PainterInfo & pi, int x, int y) const;
128 /// returns distance of this cell to the point given by x and y
129 // assumes valid position and size cache
130 int dist(int x, int y) const;
132 /// try to handle that request
133 void dispatch(LCursor & cur, FuncRequest const & cmd);
134 /// do we want to handle this event?
135 bool getStatus(LCursor & cur, FuncRequest const & cmd, FuncStatus & status);
137 /// access to out BufferView. This should go...
139 /// access to out BufferView. This should go...
140 BufferView * bv() const;
142 /// returns an iterator pointing to a cursor paragraph
143 ParagraphList::iterator getPar(CursorSlice const & cursor) const;
145 ParagraphList::iterator getPar(par_type par) const;
147 int parOffset(ParagraphList::iterator pit) const;
148 /// # FIXME: should not be used
149 ParagraphList::iterator cursorPar() const;
150 // Returns the current font and depth as a message.
151 std::string LyXText::currentState(LCursor & cur);
153 /** returns an iterator pointing to the row near the specified
154 * y-coordinate (relative to the whole text). y is set to the
155 * real beginning of this row
157 RowList::iterator getRowNearY(int y,
158 ParagraphList::iterator & pit) const;
160 /** returns the column near the specified x-coordinate of the row
161 x is set to the real beginning of this column
163 pos_type getColumnNearX(ParagraphList::iterator pit,
164 Row const & row, int & x, bool & boundary) const;
166 /** Find the word under \c from in the relative location
167 * defined by \c word_location.
168 * @param from return here the start of the word
169 * @param to return here the end of the word
171 void getWord(CursorSlice & from, CursorSlice & to, lyx::word_location const);
172 /// just selects the word the cursor is in
173 void selectWord(LCursor & cur, lyx::word_location loc);
175 /// accept selected change
176 void acceptChange(LCursor & cur);
177 /// reject selected change
178 void rejectChange(LCursor & cur);
180 /// returns true if par was empty and was removed
181 bool setCursor(LCursor & cur, par_type par, pos_type pos,
182 bool setfont = true, bool boundary = false);
184 void setCursor(CursorSlice &, par_type par,
185 pos_type pos, bool boundary = false);
187 void setCursorIntern(LCursor & cur, par_type par,
188 pos_type pos, bool setfont = true, bool boundary = false);
190 void setCurrentFont(LCursor & cur);
193 void recUndo(par_type first, par_type last) const;
195 void recUndo(par_type first) const;
197 void setCursorFromCoordinates(LCursor & cur, int x, int y);
199 InsetBase * editXY(LCursor & cur, int x, int y);
201 void cursorUp(LCursor & cur);
203 void cursorDown(LCursor & cur);
205 void cursorLeft(LCursor & cur);
207 void cursorRight(LCursor & cur);
209 void cursorLeftOneWord(LCursor & cur);
211 void cursorRightOneWord(LCursor & cur);
213 void cursorUpParagraph(LCursor & cur);
215 void cursorDownParagraph(LCursor & cur);
217 void cursorHome(LCursor & cur);
219 void cursorEnd(LCursor & cur);
221 void cursorPrevious(LCursor & cur);
223 void cursorNext(LCursor & cur);
225 void cursorTop(LCursor & cur);
227 void cursorBottom(LCursor & cur);
229 void Delete(LCursor & cur);
231 void backspace(LCursor & cur);
233 bool selectWordWhenUnderCursor(LCursor & cur, lyx::word_location);
239 text_capitalization = 1,
243 /// Change the case of the word at cursor position.
244 void changeCase(LCursor & cur, TextCase action);
247 bool toggleInset(LCursor & cur);
249 void cutSelection(LCursor & cur, bool doclear = true, bool realcut = true);
251 void copySelection(LCursor & cur);
253 void pasteSelection(LCursor & cur, size_t sel_index = 0);
255 /** the DTP switches for paragraphs. LyX will store the top settings
256 always in the first physical paragraph, the bottom settings in the
257 last. When a paragraph is broken, the top settings rest, the bottom
258 settings are given to the new one.
260 void setParagraph(LCursor & cur,
261 Spacing const & spacing,
263 std::string const & labelwidthstring,
266 /* these things are for search and replace */
269 * Sets the selection from the current cursor position to length
270 * characters to the right. No safety checks.
272 void setSelectionRange(LCursor & cur, pos_type length);
273 /// simply replace using the font of the first selected character
274 void replaceSelectionWithString(LCursor & cur, std::string const & str);
275 /// replace selection helper
276 void replaceSelection(LCursor & cur);
278 /// needed to insert the selection
279 void insertStringAsLines(LCursor & cur, std::string const & str);
280 /// needed to insert the selection
281 void insertStringAsParagraphs(LCursor & cur, std::string const & str);
283 /// Find next inset of some specified type.
284 bool gotoNextInset(LCursor & cur,
285 std::vector<InsetOld_code> const & codes,
286 std::string const & contents = std::string());
288 void gotoInset(LCursor & cur,
289 std::vector<InsetOld_code> const & codes, bool same_content);
291 void gotoInset(LCursor & cur, InsetOld_code code, bool same_content);
293 /// current max text width
294 int textWidth() const;
296 /// updates all counters
297 void updateCounters();
298 /// Returns an inset if inset was hit, or 0 if not.
299 InsetBase * checkInsetHit(int x, int y);
302 int singleWidth(ParagraphList::iterator pit, pos_type pos) const;
304 int singleWidth(ParagraphList::iterator pit,
305 pos_type pos, char c, LyXFont const & Font) const;
307 /// return the color of the canvas
308 LColor_color backgroundColor() const;
311 * Returns the left beginning of the text.
312 * This information cannot be taken from the layout object, because
313 * in LaTeX the beginning of the text fits in some cases
314 * (for example sections) exactly the label-width.
316 int leftMargin(ParagraphList::iterator pit, pos_type pos) const;
317 int leftMargin(ParagraphList::iterator pit) const;
319 int rightMargin(Paragraph const & par) const;
321 /** this calculates the specified parameters. needed when setting
322 * the cursor and when creating a visible row */
323 void prepareToPrint(ParagraphList::iterator pit, Row & row) const;
325 /// access to our paragraphs
326 ParagraphList & paragraphs() const;
327 /// return true if this is owned by an inset.
328 bool isInInset() const;
330 /// return first row of text
331 RowList::iterator firstRow() const;
332 /// return last row of text
333 RowList::iterator lastRow() const;
334 /// return row "behind" last row of text
335 RowList::iterator endRow() const;
336 /// return next row crossing paragraph boundaries
337 void nextRow(ParagraphList::iterator & pit,
338 RowList::iterator & rit) const;
339 /// return previous row crossing paragraph boundaries
340 void previousRow(ParagraphList::iterator & pit,
341 RowList::iterator & rit) const;
343 /// is this row the last in the text?
344 bool isLastRow(ParagraphList::iterator pit, Row const & row) const;
345 /// is this row the first in the text?
346 bool isFirstRow(ParagraphList::iterator pit, Row const & row) const;
349 double spacing(Paragraph const & par) const;
350 /// make a suggestion for a label
351 std::string getPossibleLabel(LCursor & cur) const;
352 /// is this paragraph right-to-left?
353 bool isRTL(Paragraph const & par) const;
355 bool checkAndActivateInset(LCursor & cur, bool front);
358 void write(Buffer const & buf, std::ostream & os) const;
359 /// returns whether we've seen our usual 'end' marker
360 bool read(Buffer const & buf, LyXLex & lex);
367 int cursorX(CursorSlice const & cursor) const;
369 int cursorY(CursorSlice const & cursor) const;
371 /// the current cursor slice
372 CursorSlice & cursor();
373 /// the current cursor slice
374 CursorSlice const & cursor() const;
376 friend class LyXScreen;
385 /// the current font settings
386 LyXFont current_font;
388 LyXFont real_current_font;
389 /// our buffer's default layout font
390 LyXFont defaultfont_;
392 int background_color_;
394 /// only the top-level LyXText has this non-zero
395 BufferView * bv_owner;
402 ParagraphList paragraphs_;
404 /// absolute document pixel coordinates of this LyXText
408 /// our 'outermost' Font
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.witdh to the minimum space a row needs on the screen in pixel
447 void fill(ParagraphList::iterator pit, Row & row, int workwidth) 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);