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"
46 /// This class encapsulates the main text data and operations in LyX
50 typedef lyx::pos_type pos_type;
52 typedef lyx::par_type par_type;
55 explicit LyXText(BufferView *);
57 void init(BufferView *);
59 /// update y coordinate cache of all paragraphs
60 void updateParPositions();
62 LyXFont getFont(Paragraph const & par, pos_type pos) const;
64 LyXFont getLayoutFont(par_type pit) const;
66 LyXFont getLabelFont(Paragraph const & par) const;
68 void setCharFont(par_type pit, pos_type pos, LyXFont const & font);
70 void setCharFont(par_type pit, pos_type pos, LyXFont const & font,
73 /// what you expect when pressing <enter> at cursor position
74 void breakParagraph(LCursor & cur, char keep_layout = 0);
76 /// set layout over selection
77 par_type setLayout(par_type start, par_type end,
78 std::string const & layout);
80 void setLayout(LCursor & cur, std::string const & layout);
82 /// what type of depth change to make
87 /// Increase or decrease the nesting depth of the selected paragraph(s)
88 void changeDepth(LCursor & cur, DEPTH_CHANGE type);
90 /// Returns whether something would be changed by changeDepth
91 bool changeDepthAllowed(LCursor & cur, DEPTH_CHANGE type) const;
93 /// Set font over selection paragraphs and rebreak.
94 void setFont(LCursor & cur, LyXFont const &, bool toggleall = false);
96 /// rebreaks all paragaphs between the given pars.
97 void redoParagraphs(par_type begin, par_type end);
98 /// rebreaks the given par
99 void redoParagraph(par_type pit);
100 /// rebreaks the cursor par
101 void redoParagraph(LCursor & cur);
103 /// returns pos in given par at given x coord
104 pos_type x2pos(par_type pit, int row, int x) const;
105 int pos2x(par_type pit, pos_type pos) const;
108 void toggleFree(LCursor & cur, LyXFont const &, bool toggleall = false);
111 std::string getStringToIndex(LCursor & cur);
113 /// insert a character at cursor position
114 void insertChar(LCursor & cur, char c);
115 /// insert an inset at cursor position
116 void insertInset(LCursor & cur, 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;
126 /// returns distance of this cell to the point given by x and y
127 // assumes valid position and size cache
128 int dist(int x, int y) const;
130 /// try to handle that request
131 void dispatch(LCursor & cur, FuncRequest & cmd);
132 /// do we want to handle this event?
133 bool getStatus(LCursor & cur, FuncRequest const & cmd,
134 FuncStatus & status) const;
136 /// access to out BufferView. This should go...
138 /// access to out BufferView. This should go...
139 BufferView * bv() const;
141 /// access to individual paragraphs
142 Paragraph & getPar(par_type par) const;
143 // Returns the current font and depth as a message.
144 std::string LyXText::currentState(LCursor & cur);
146 /** returns an iterator pointing to the row near the specified
147 * y-coordinate (relative to the whole text). y is set to the
148 * real beginning of this row
150 Row const & getRowNearY(int y, par_type & 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 pos_type getColumnNearX(par_type 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(LCursor & cur, lyx::word_location loc);
167 /// accept selected change
168 void acceptChange(LCursor & cur);
169 /// reject selected change
170 void rejectChange(LCursor & cur);
172 /// returns true if par was empty and was removed
173 bool setCursor(LCursor & cur, par_type par, pos_type pos,
174 bool setfont = true, bool boundary = false);
176 void setCursor(CursorSlice &, par_type par,
177 pos_type pos, bool boundary = false);
179 void setCursorIntern(LCursor & cur, par_type par,
180 pos_type pos, bool setfont = true, bool boundary = false);
182 void setCurrentFont(LCursor & cur);
185 void recUndo(par_type first, par_type last) const;
187 void recUndo(par_type first) const;
189 void setCursorFromCoordinates(LCursor & cur, int x, int y);
191 InsetBase * editXY(LCursor & cur, int x, int y) const;
193 void cursorUp(LCursor & cur);
195 void cursorDown(LCursor & cur);
197 void cursorLeft(LCursor & cur);
199 void cursorRight(LCursor & cur);
201 void cursorLeftOneWord(LCursor & cur);
203 void cursorRightOneWord(LCursor & cur);
205 void cursorUpParagraph(LCursor & cur);
207 void cursorDownParagraph(LCursor & cur);
209 void cursorHome(LCursor & cur);
211 void cursorEnd(LCursor & cur);
213 void cursorPrevious(LCursor & cur);
215 void cursorNext(LCursor & cur);
217 void cursorTop(LCursor & cur);
219 void cursorBottom(LCursor & cur);
221 void Delete(LCursor & cur);
223 void backspace(LCursor & cur);
225 bool selectWordWhenUnderCursor(LCursor & cur, lyx::word_location);
231 text_capitalization = 1,
235 /// Change the case of the word at cursor position.
236 void changeCase(LCursor & cur, TextCase action);
238 /** the DTP switches for paragraphs. LyX will store the top settings
239 always in the first physical paragraph, the bottom settings in the
240 last. When a paragraph is broken, the top settings rest, the bottom
241 settings are given to the new one.
243 void setParagraph(LCursor & cur,
244 Spacing const & spacing,
246 std::string const & labelwidthstring,
249 /* these things are for search and replace */
251 /// needed to insert the selection
252 void insertStringAsLines(LCursor & cur, std::string const & str);
253 /// needed to insert the selection
254 void insertStringAsParagraphs(LCursor & cur, std::string const & str);
256 /// Find next inset of some specified type.
257 bool gotoNextInset(LCursor & cur,
258 std::vector<InsetOld_code> const & codes,
259 std::string const & contents = std::string());
261 void gotoInset(LCursor & cur,
262 std::vector<InsetOld_code> const & codes, bool same_content);
264 void gotoInset(LCursor & cur, InsetOld_code code, bool same_content);
266 /// current text width
269 /// current text heigth
272 /// updates all counters
273 void updateCounters();
274 /// Returns an inset if inset was hit, or 0 if not.
275 InsetBase * checkInsetHit(int x, int y) const;
278 int singleWidth(Paragraph const & par, pos_type pos) const;
280 int singleWidth(Paragraph const & par,
281 pos_type pos, char c, LyXFont const & Font) const;
283 /// return the color of the canvas
284 LColor_color backgroundColor() const;
287 * Returns the left beginning of the text.
288 * This information cannot be taken from the layout object, because
289 * in LaTeX the beginning of the text fits in some cases
290 * (for example sections) exactly the label-width.
292 int leftMargin(par_type pit, pos_type pos) const;
293 int leftMargin(par_type pit) const;
295 int rightMargin(Paragraph const & par) const;
297 /** this calculates the specified parameters. needed when setting
298 * the cursor and when creating a visible row */
299 RowMetrics computeRowMetrics(par_type pit, Row const & row) const;
301 /// access to our paragraphs
302 ParagraphList & paragraphs() const;
303 /// return true if this is the main text
304 bool isMainText() const;
306 /// return first row of text
307 Row const & firstRow() const;
309 /// is this row the last in the text?
310 bool isLastRow(par_type pit, Row const & row) const;
311 /// is this row the first in the text?
312 bool isFirstRow(par_type pit, Row const & row) const;
315 double spacing(Paragraph const & par) const;
316 /// make a suggestion for a label
317 std::string getPossibleLabel(LCursor & cur) const;
318 /// is this paragraph right-to-left?
319 bool isRTL(Paragraph const & par) const;
321 bool checkAndActivateInset(LCursor & cur, bool front);
324 void write(Buffer const & buf, std::ostream & os) const;
325 /// returns whether we've seen our usual 'end' marker
326 bool read(Buffer const & buf, LyXLex & lex);
333 int cursorX(CursorSlice const & cursor) const;
335 int cursorY(CursorSlice const & cursor) const;
338 friend class LyXScreen;
347 /// the current font settings
348 LyXFont current_font;
350 LyXFont real_current_font;
351 /// our buffer's default layout font
352 LyXFont defaultfont_;
354 int background_color_;
356 /// only the top-level LyXText has this non-zero
357 BufferView * bv_owner;
364 /// absolute document pixel coordinates of this LyXText
368 /// our 'outermost' Font
372 /// return past-the-last paragraph influenced by a layout
374 par_type undoSpan(par_type pit);
376 /// rebreaks the given par
377 void redoParagraphInternal(par_type pit);
378 /// used in setlayout
379 void makeFontEntriesLayoutSpecific(BufferParams const &, Paragraph & par);
381 /// Calculate and set the height of the row
382 void setHeightOfRow(par_type, Row & row);
384 // fix the cursor `cur' after a characters has been deleted at `where'
385 // position. Called by deleteEmptyParagraphMechanism
386 void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
388 /// delete double space or empty paragraphs around old cursor
389 bool deleteEmptyParagraphMechanism(LCursor & cur, LCursor const & old);
392 void setCounter(Buffer const &, par_type pit);
394 void deleteWordForward(LCursor & cur);
396 void deleteWordBackward(LCursor & cur);
398 void deleteLineForward(LCursor & cur);
400 /// sets row.end to the pos value *after* which a row should break.
401 /// for example, the pos after which isNewLine(pos) == true
402 void rowBreakPoint(par_type pit, Row & row) const;
403 /// sets row.width to the minimum space a row needs on the screen in pixel
404 void setRowWidth(par_type pit, Row & row) const;
405 /// the minimum space a manual label needs on the screen in pixels
406 int labelFill(Paragraph const & par, Row const & row) const;
408 int labelEnd(par_type pit) const;
412 /// set 'number' font property
413 void number(LCursor & cur);
416 /// return the default height of a row in pixels, considering font zoom
417 int defaultRowHeight();
420 std::string expandLabel(LyXTextClass const & textclass,
421 LyXLayout_ptr const & layout, bool appendix);