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 typedef lyx::pos_type pos_type;
54 typedef lyx::paroffset_type par_type;
57 LyXText(BufferView *, bool ininset);
59 void init(BufferView *);
61 /// update y coordinate cache of all paragraphs
62 void updateParPositions();
64 LyXFont getFont(ParagraphList::iterator pit, 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 pos_type pos, LyXFont const & font);
72 void setCharFont(ParagraphList::iterator pit,
73 pos_type pos, LyXFont const & font, bool toggleall);
75 /// what you expect when pressing <enter> at cursor position
76 void breakParagraph(LCursor & cur, char keep_layout = 0);
78 /** set layout over selection and make a total rebreak of
81 ParagraphList::iterator
82 setLayout(ParagraphList::iterator start,
83 ParagraphList::iterator end,
84 std::string const & layout);
86 void setLayout(LCursor & cur, std::string const & layout);
88 /// Increase or decrease the nesting depth of the selected paragraph(s)
89 void changeDepth(LCursor & cur, bv_funcs::DEPTH_CHANGE type);
91 /// Returns whether something would be changed by changeDepth
92 bool changeDepthAllowed(LCursor & cur, bv_funcs::DEPTH_CHANGE type);
94 /// get the depth at current cursor position
97 /// Set font over selection paragraphs and rebreak.
98 void setFont(LCursor & cur, LyXFont const &, bool toggleall = false);
100 /// rebreaks all paragaphs between the given pars.
101 void redoParagraphs(ParagraphList::iterator begin,
102 ParagraphList::iterator end);
103 /// rebreaks the given par
104 void redoParagraph(ParagraphList::iterator pit);
105 /// rebreaks the cursor par
106 void redoParagraph(LCursor & cur);
109 void toggleFree(LCursor & cur, LyXFont const &, bool toggleall = false);
112 std::string getStringToIndex(LCursor & cur);
114 /// insert a character at cursor position
115 void insertChar(LCursor & cur, char c);
116 /// insert an inset at cursor position
117 void insertInset(LCursor & cur, InsetBase * inset);
119 /// a full rebreak of the whole text
121 /// compute text metrics
122 void metrics(MetricsInfo & mi, Dimension & dim);
123 /// draw text (only used for insets)
124 void draw(PainterInfo & pi, int x, int y) const;
125 /// draw textselection
126 void drawSelection(PainterInfo & pi, int x, int y) const;
127 /// returns distance of this cell to the point given by x and y
128 // assumes valid position and size cache
129 int dist(int x, int y) const;
131 /// try to handle that request
132 DispatchResult dispatch(LCursor & cur, FuncRequest const & cmd);
133 /// access to out BufferView. This should go...
135 /// access to out BufferView. This should go...
136 BufferView * bv() const;
138 /// returns an iterator pointing to a cursor paragraph
139 ParagraphList::iterator getPar(CursorSlice const & cursor) const;
141 ParagraphList::iterator getPar(par_type par) const;
143 int parOffset(ParagraphList::iterator pit) const;
144 /// # FIXME: should not be used
145 ParagraphList::iterator cursorPar() const;
146 // Returns the current font and depth as a message.
147 std::string LyXText::currentState(LCursor & cur);
149 /** returns an iterator pointing to the row near the specified
150 * y-coordinate (relative to the whole text). y is set to the
151 * real beginning of this row
153 RowList::iterator getRowNearY(int y,
154 ParagraphList::iterator & pit) const;
156 /** returns the column near the specified x-coordinate of the row
157 x is set to the real beginning of this column
159 pos_type getColumnNearX(ParagraphList::iterator pit,
160 Row const & row, int & x, bool & boundary) const;
162 /** Find the word under \c from in the relative location
163 * defined by \c word_location.
164 * @param from return here the start of the word
165 * @param to return here the end of the word
167 void getWord(CursorSlice & from, CursorSlice & to, lyx::word_location const);
168 /// just selects the word the cursor is in
169 void selectWord(LCursor & cur, lyx::word_location loc);
171 /// accept selected change
172 void acceptChange(LCursor & cur);
173 /// reject selected change
174 void rejectChange(LCursor & cur);
176 /// returns true if par was empty and was removed
177 bool setCursor(LCursor & cur, par_type par, pos_type pos,
178 bool setfont = true, bool boundary = false);
180 void setCursor(CursorSlice &, par_type par,
181 pos_type pos, bool boundary = false);
183 void setCursorIntern(LCursor & cur, par_type par,
184 pos_type pos, bool setfont = true, bool boundary = false);
186 void setCurrentFont(LCursor & cur);
189 void recUndo(par_type first, par_type last) const;
191 void recUndo(par_type first) const;
193 void setCursorFromCoordinates(LCursor & cur, int x, int y);
195 InsetBase * editXY(LCursor & cur, int x, int y);
197 void cursorUp(LCursor & cur, bool selecting = false);
199 void cursorDown(LCursor & cur, bool selecting = false);
201 bool cursorLeft(LCursor & cur, bool internal = true);
203 bool cursorRight(LCursor & cur, bool internal = true);
205 void cursorLeftOneWord(LCursor & cur);
207 void cursorRightOneWord(LCursor & cur);
209 void cursorUpParagraph(LCursor & cur);
211 void cursorDownParagraph(LCursor & cur);
213 void cursorHome(LCursor & cur);
215 void cursorEnd(LCursor & cur);
217 void cursorPrevious(LCursor & cur);
219 void cursorNext(LCursor & cur);
221 void cursorTop(LCursor & cur);
223 void cursorBottom(LCursor & cur);
225 void Delete(LCursor & cur);
227 void backspace(LCursor & cur);
229 bool selectWordWhenUnderCursor(LCursor & cur, lyx::word_location);
235 text_capitalization = 1,
239 /// Change the case of the word at cursor position.
240 void changeCase(LCursor & cur, TextCase action);
243 bool toggleInset(LCursor & cur);
245 void cutSelection(LCursor & cur, bool doclear = true, bool realcut = true);
247 void copySelection(LCursor & cur);
249 void pasteSelection(LCursor & cur, 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.
256 void setParagraph(LCursor & cur,
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(LCursor & cur, pos_type length);
269 /// simply replace using the font of the first selected character
270 void replaceSelectionWithString(LCursor & cur, std::string const & str);
271 /// replace selection helper
272 void replaceSelection(LCursor & cur);
274 /// needed to insert the selection
275 void insertStringAsLines(LCursor & cur, std::string const & str);
276 /// needed to insert the selection
277 void insertStringAsParagraphs(LCursor & cur, std::string const & str);
279 /// Find next inset of some specified type.
280 bool gotoNextInset(LCursor & cur,
281 std::vector<InsetOld_code> const & codes,
282 std::string const & contents = std::string());
284 void gotoInset(LCursor & cur,
285 std::vector<InsetOld_code> const & codes, bool same_content);
287 void gotoInset(LCursor & cur, 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 InsetBase * checkInsetHit(int x, int y);
298 int singleWidth(ParagraphList::iterator pit, pos_type pos) const;
300 int singleWidth(ParagraphList::iterator pit,
301 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, 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;
321 /// access to our paragraphs
322 ParagraphList & paragraphs() const;
323 /// return true if this is owned by an inset.
324 bool isInInset() const;
326 /// return first row of text
327 RowList::iterator firstRow() const;
328 /// return last row of text
329 RowList::iterator lastRow() const;
330 /// return row "behind" last row of text
331 RowList::iterator endRow() const;
332 /// return next row crossing paragraph boundaries
333 void nextRow(ParagraphList::iterator & pit,
334 RowList::iterator & rit) const;
335 /// return previous row crossing paragraph boundaries
336 void previousRow(ParagraphList::iterator & pit,
337 RowList::iterator & rit) const;
339 /// is this row the last in the text?
340 bool isLastRow(ParagraphList::iterator pit, Row const & row) const;
341 /// is this row the first in the text?
342 bool isFirstRow(ParagraphList::iterator pit, Row const & row) const;
345 double spacing(Paragraph const & par) const;
346 /// make a suggestion for a label
347 std::string getPossibleLabel(LCursor & cur) const;
348 /// is this paragraph right-to-left?
349 bool isRTL(Paragraph const & par) const;
352 DispatchResult moveRight(LCursor & cur);
354 DispatchResult moveLeft(LCursor & cur);
356 DispatchResult moveRightIntern(LCursor & cur, bool front,
357 bool activate_inset, bool selecting);
359 DispatchResult moveLeftIntern(LCursor & cur, bool front,
360 bool activate_inset, bool selecting);
362 DispatchResult moveUp(LCursor & cur);
364 DispatchResult moveDown(LCursor & cur);
366 bool checkAndActivateInset(LCursor & cur, bool front);
369 void write(Buffer const & buf, std::ostream & os) const;
370 /// returns whether we've seen our usual 'end' marker
371 bool read(Buffer const & buf, LyXLex & lex);
378 int cursorX(CursorSlice const & cursor) const;
380 int cursorY(CursorSlice const & cursor) const;
382 /// the current cursor slice
383 CursorSlice & cursor();
384 /// the current cursor slice
385 CursorSlice const & cursor() const;
387 friend class LyXScreen;
396 /// the current font settings
397 LyXFont current_font;
399 LyXFont real_current_font;
400 /// our buffer's default layout font
401 LyXFont defaultfont_;
403 int background_color_;
405 /// only the top-level LyXText has this non-zero
406 BufferView * bv_owner;
413 ParagraphList paragraphs_;
415 /// absolute document pixel coordinates of this LyXText
419 /// our 'outermost' Font
424 /// return past-the-last paragraph influenced by a layout
426 ParagraphList::iterator undoSpan(ParagraphList::iterator pit);
428 /// rebreaks the given par
429 void redoParagraphInternal(ParagraphList::iterator pit);
430 /// used in setlayout
431 void makeFontEntriesLayoutSpecific(BufferParams const &, Paragraph & par);
433 /// Calculate and set the height of the row
434 void setHeightOfRow(ParagraphList::iterator, Row & row);
436 // fix the cursor `cur' after a characters has been deleted at `where'
437 // position. Called by deleteEmptyParagraphMechanism
438 void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
440 /// delete double space (false) or empty paragraphs (true) around old_cursor
441 bool deleteEmptyParagraphMechanism(
443 CursorSlice const & old_cursor);
446 void setCounter(Buffer const &, ParagraphList::iterator pit);
448 void deleteWordForward(LCursor & cur);
450 void deleteWordBackward(LCursor & cur);
452 void deleteLineForward(LCursor & cur);
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;
457 /// sets row.witdh to the minimum space a row needs on the screen in pixel
458 void fill(ParagraphList::iterator pit, Row & row, int workwidth) const;
459 /// the minimum space a manual label needs on the screen in pixels
460 int labelFill(ParagraphList::iterator pit, Row const & row) const;
462 int labelEnd(ParagraphList::iterator pit) const;
466 /// set 'number' font property
467 void number(LCursor & cur);
470 /// return the default height of a row in pixels, considering font zoom
471 int defaultRowHeight();
474 std::string expandLabel(LyXTextClass const & textclass,
475 LyXLayout_ptr const & layout, bool appendix);