]> git.lyx.org Git - lyx.git/blob - src/lyxtext.h
IU stuff + shuffling things from BV::dispatch to LyXText::dispatch
[lyx.git] / src / lyxtext.h
1 // -*- C++ -*-
2 /**
3  * \file lyxtext.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author unknown
8  * \author Lars Gullik Bjønnes
9  * \author John Levon
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef LYXTEXT_H
15 #define LYXTEXT_H
16
17 #include "bufferview_funcs.h"
18 #include "Bidi.h"
19 #include "dispatchresult.h"
20 #include "lyxfont.h"
21 #include "layout.h"
22 #include "lyxlayout_ptr_fwd.h"
23 #include "ParagraphList_fwd.h"
24 #include "RowList_fwd.h"
25
26 #include <iosfwd>
27
28 class Buffer;
29 class BufferParams;
30 class BufferView;
31 class CursorSlice;
32 class Dimension;
33 class InsetBase;
34 class InsetOld_code;
35 class FuncRequest;
36 class LColor_color;
37 class LCursor;
38 class LyXTextClass;
39 class MetricsInfo;
40 class PainterInfo;
41 class Paragraph;
42 class Row;
43 class Spacing;
44 class UpdatableInset;
45 class VSpace;
46
47
48 /// This class encapsulates the main text data and operations in LyX
49 class LyXText {
50 public:
51         /// constructor
52         LyXText(BufferView *, bool ininset);
53         ///
54         void init(BufferView *);
55
56         /// update y coordinate cache of all paragraphs
57         void updateParPositions();
58         ///
59         LyXFont getFont(ParagraphList::iterator pit, lyx::pos_type pos) const;
60         ///
61         LyXFont getLayoutFont(ParagraphList::iterator pit) const;
62         ///
63         LyXFont getLabelFont(ParagraphList::iterator pit) const;
64         ///
65         void setCharFont(ParagraphList::iterator pit,
66                          lyx::pos_type pos, LyXFont const & font);
67         void setCharFont(ParagraphList::iterator pit,
68                          lyx::pos_type pos,
69                          LyXFont const & font, bool toggleall);
70
71         /// what you expect when pressing <enter> at cursor position
72         void breakParagraph(LCursor & cur, char keep_layout = 0);
73
74         /** set layout over selection and make a total rebreak of
75           those paragraphs
76           */
77         ParagraphList::iterator
78         setLayout(ParagraphList::iterator start,
79                   ParagraphList::iterator end,
80                   std::string const & layout);
81         ///
82         void setLayout(LCursor & cur, std::string const & layout);
83
84         /// Increase or decrease the nesting depth of the selected paragraph(s)
85         void changeDepth(LCursor & cur, bv_funcs::DEPTH_CHANGE type);
86
87         /// Returns whether something would be changed by changeDepth
88         bool changeDepthAllowed(LCursor & cur, bv_funcs::DEPTH_CHANGE type);
89
90         /// get the depth at current cursor position
91         int getDepth() const;
92
93         /// Set font over selection paragraphs and rebreak.
94         void setFont(LCursor & cur, LyXFont const &, bool toggleall = false);
95
96         /// rebreaks all paragaphs between the given pars.
97         void redoParagraphs(ParagraphList::iterator begin,
98                             ParagraphList::iterator end);
99         /// rebreaks the given par
100         void redoParagraph(ParagraphList::iterator pit);
101         /// rebreaks the cursor par
102         void redoParagraph(LCursor & cur);
103
104         ///
105         void toggleFree(LCursor & cur, LyXFont const &, bool toggleall = false);
106
107         ///
108         std::string getStringToIndex(LCursor & cur);
109
110         /// insert a character at cursor position
111         void insertChar(LCursor & cur, char c);
112         /// insert an inset at cursor position
113         void insertInset(LCursor & cur, InsetBase * inset);
114
115         /// a full rebreak of the whole text
116         void fullRebreak();
117         /// compute text metrics
118         void metrics(MetricsInfo & mi, Dimension & dim);
119         /// draw text (only used for insets)
120         void draw(PainterInfo & pi, int x, int y) const;
121         /// draw textselection
122         void drawSelection(PainterInfo & pi, int x, int y) const;
123
124         /// try to handle that request
125         DispatchResult dispatch(LCursor & cur, FuncRequest const & cmd);
126         /// access to out BufferView. This should go...
127         BufferView * bv();
128         /// access to out BufferView. This should go...
129         BufferView * bv() const;
130
131         /// returns an iterator pointing to a cursor paragraph
132         ParagraphList::iterator getPar(CursorSlice const & cursor) const;
133         ///
134         ParagraphList::iterator getPar(lyx::paroffset_type par) const;
135         ///
136         int parOffset(ParagraphList::iterator pit) const;
137         /// # FIXME: should not be used
138         ParagraphList::iterator cursorPar() const;
139         // Returns the current font and depth as a message.
140         std::string LyXText::currentState(LCursor & cur);
141
142         /** returns an iterator pointing to the row near the specified
143           * y-coordinate (relative to the whole text). y is set to the
144           * real beginning of this row
145           */
146         RowList::iterator getRowNearY(int y,
147                 ParagraphList::iterator & pit) const;
148
149         /** returns the column near the specified x-coordinate of the row
150          x is set to the real beginning of this column
151          */
152         lyx::pos_type getColumnNearX(ParagraphList::iterator pit,
153                 Row const & row, int & x, bool & boundary) const;
154
155         /** Find the word under \c from in the relative location
156          *  defined by \c word_location.
157          *  @param from return here the start of the word
158          *  @param to return here the end of the word
159          */
160         void getWord(CursorSlice & from, CursorSlice & to, lyx::word_location const);
161         /// just selects the word the cursor is in
162         void selectWord(LCursor & cur, lyx::word_location loc);
163
164         /// accept selected change
165         void acceptChange(LCursor & cur);
166         /// reject selected change
167         void rejectChange(LCursor & cur);
168
169         /// returns true if par was empty and was removed
170         bool setCursor(lyx::paroffset_type par, lyx::pos_type pos,
171                        bool setfont = true, bool boundary = false);
172         ///
173         void setCursor(CursorSlice &, lyx::paroffset_type par,
174                        lyx::pos_type pos, bool boundary = false);
175         ///
176         void setCursorIntern(lyx::paroffset_type par, lyx::pos_type pos,
177                              bool setfont = true, bool boundary = false);
178         ///
179         void setCurrentFont();
180
181         ///
182         void recUndo(lyx::paroffset_type first, lyx::paroffset_type last) const;
183         ///
184         void recUndo(lyx::paroffset_type first) const;
185         ///
186         void setCursorFromCoordinates(int x, int y);
187         ///
188         void setCursorFromCoordinates(CursorSlice &, int x, int y);
189         ///
190         void edit(LCursor & cur, int x, int y);
191         ///
192         void cursorUp(LCursor & cur, bool selecting = false);
193         ///
194         void cursorDown(LCursor & cur, bool selecting = false);
195         ///
196         bool cursorLeft(LCursor & cur, bool internal = true);
197         ///
198         bool cursorRight(LCursor & cur, bool internal = true);
199         ///
200         void cursorLeftOneWord(LCursor & cur);
201         ///
202         void cursorRightOneWord(LCursor & cur);
203         ///
204         void cursorUpParagraph(LCursor & cur);
205         ///
206         void cursorDownParagraph(LCursor & cur);
207         ///
208         void cursorHome(LCursor & cur);
209         ///
210         void cursorEnd(LCursor & cur);
211         ///
212         void cursorPrevious(LCursor & cur);
213         ///
214         void cursorNext(LCursor & cur);
215         ///
216         void cursorTop(LCursor & cur);
217         ///
218         void cursorBottom(LCursor & cur);
219         ///
220         void Delete(LCursor & cur);
221         ///
222         void backspace(LCursor & cur);
223         ///
224         bool selectWordWhenUnderCursor(LCursor & cur, lyx::word_location);
225         ///
226         enum TextCase {
227                 ///
228                 text_lowercase = 0,
229                 ///
230                 text_capitalization = 1,
231                 ///
232                 text_uppercase = 2
233         };
234         /// Change the case of the word at cursor position.
235         void changeCase(LCursor & cur, TextCase action);
236
237         /// returns success
238         bool toggleInset(LCursor & cur);
239         ///
240         void cutSelection(bool doclear = true, bool realcut = true);
241         ///
242         void copySelection();
243         ///
244         void pasteSelection(LCursor & cur, size_t sel_index = 0);
245
246         /** the DTP switches for paragraphs. LyX will store the top settings
247          always in the first physical paragraph, the bottom settings in the
248          last. When a paragraph is broken, the top settings rest, the bottom
249          settings are given to the new one.
250          */
251         void setParagraph(
252                           Spacing const & spacing,
253                           LyXAlignment align,
254                           std::string const & labelwidthstring,
255                           bool noindent);
256
257         /* these things are for search and replace */
258
259         /**
260          * Sets the selection from the current cursor position to length
261          * characters to the right. No safety checks.
262          */
263         void setSelectionRange(LCursor & cur, lyx::pos_type length);
264         /// simply replace using the font of the first selected character
265         void replaceSelectionWithString(LCursor & cur, std::string const & str);
266         /// replace selection helper
267         void replaceSelection(LCursor & cur);
268
269         /// needed to insert the selection
270         void insertStringAsLines(LCursor & cur, std::string const & str);
271         /// needed to insert the selection
272         void insertStringAsParagraphs(LCursor & cur, std::string const & str);
273
274         /// Find next inset of some specified type.
275         bool gotoNextInset(std::vector<InsetOld_code> const & codes,
276                            std::string const & contents = std::string());
277         ///
278         void gotoInset(std::vector<InsetOld_code> const & codes,
279                        bool same_content);
280         ///
281         void gotoInset(InsetOld_code code, bool same_content);
282
283         /// current max text width
284         int textWidth() const;
285
286         /// updates all counters
287         void updateCounters();
288         /// Returns an inset if inset was hit, or 0 if not.
289         InsetBase * checkInsetHit(int x, int y);
290
291         ///
292         int singleWidth(ParagraphList::iterator pit, lyx::pos_type pos) const;
293         ///
294         int singleWidth(ParagraphList::iterator pit,
295                 lyx::pos_type pos, char c, LyXFont const & Font) const;
296
297         /// return the color of the canvas
298         LColor_color backgroundColor() const;
299
300         /**
301          * Returns the left beginning of the text.
302          * This information cannot be taken from the layout object, because
303          * in LaTeX the beginning of the text fits in some cases
304          * (for example sections) exactly the label-width.
305          */
306         int leftMargin(ParagraphList::iterator pit, lyx::pos_type pos) const;
307         int leftMargin(ParagraphList::iterator pit) const;
308         ///
309         int rightMargin(Paragraph const & par) const;
310
311         /** this calculates the specified parameters. needed when setting
312          * the cursor and when creating a visible row */
313         void prepareToPrint(ParagraphList::iterator pit, Row & row) const;
314
315         /// access to our paragraphs
316         ParagraphList & paragraphs() const;
317         /// return true if this is owned by an inset.
318         bool isInInset() const;
319         
320         /// return first row of text
321         RowList::iterator firstRow() const;
322         /// return last row of text
323         RowList::iterator lastRow() const;
324         /// return row "behind" last row of text
325         RowList::iterator endRow() const;
326         /// return next row crossing paragraph boundaries
327         void nextRow(ParagraphList::iterator & pit,
328                 RowList::iterator & rit) const;
329         /// return previous row crossing paragraph boundaries
330         void previousRow(ParagraphList::iterator & pit,
331                 RowList::iterator & rit) const;
332
333         /// is this row the last in the text?
334         bool isLastRow(ParagraphList::iterator pit, Row const & row) const;
335         /// is this row the first in the text?
336         bool isFirstRow(ParagraphList::iterator pit, Row const & row) const;
337
338         ///
339         double spacing(Paragraph const &) const;
340
341         ///
342         DispatchResult moveRight(LCursor & cur);
343         ///
344         DispatchResult moveLeft(LCursor & cur);
345         ///
346         DispatchResult moveRightIntern(LCursor & cur, bool front,
347                 bool activate_inset, bool selecting);
348         ///
349         DispatchResult moveLeftIntern(LCursor & cur, bool front,
350                 bool activate_inset, bool selecting);
351         ///
352         DispatchResult moveUp(LCursor & cur);
353         ///
354         DispatchResult moveDown(LCursor & cur);
355         ///
356         bool checkAndActivateInset(LCursor & cur, bool front);
357
358         ///
359         void write(Buffer const & buf, std::ostream & os) const;
360         /// returns whether we've seen our usual 'end' marker
361         bool read(Buffer const & buf, LyXLex & lex);
362
363         ///
364         int ascent() const;
365         ///
366         int descent() const;
367         ///
368         int cursorX(CursorSlice const & cursor) const;
369         ///
370         int cursorY(CursorSlice const & cursor) const;
371
372         /// the current cursor slice
373         CursorSlice & cursor();
374         /// the current cursor slice
375         CursorSlice const & cursor() const;
376
377         friend class LyXScreen;
378
379 public:
380         ///
381         int height;
382         ///
383         unsigned int width;
384         ///
385         int textwidth_;
386         /// the current font settings
387         LyXFont current_font;
388         /// the current font
389         LyXFont real_current_font;
390         /// our buffer's default layout font
391         LyXFont defaultfont_;
392         ///
393         int background_color_;
394
395         /// only the top-level LyXText has this non-zero
396         BufferView * bv_owner;
397
398         ///
399         mutable Bidi bidi;
400         ///
401         bool in_inset_;
402         ///
403         ParagraphList paragraphs_;
404
405         /// absolute document pixel coordinates of this LyXText
406         mutable int xo_;
407         mutable int yo_;
408
409         /// our 'outermost' Font
410         LyXFont font_;
411
412
413 private:
414         /// return past-the-last paragraph influenced by a layout
415         /// change on pit
416         ParagraphList::iterator undoSpan(ParagraphList::iterator pit);
417         
418         /// rebreaks the given par
419         void redoParagraphInternal(ParagraphList::iterator pit);
420         /// used in setlayout
421         void makeFontEntriesLayoutSpecific(BufferParams const &, Paragraph & par);
422
423         /// Calculate and set the height of the row
424         void setHeightOfRow(ParagraphList::iterator, Row & row);
425
426         // fix the cursor `cur' after a characters has been deleted at `where'
427         // position. Called by deleteEmptyParagraphMechanism
428         void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
429
430         /// delete double space (false) or empty paragraphs (true) around old_cursor
431         bool deleteEmptyParagraphMechanism(CursorSlice const & old_cursor);
432
433         ///
434         void setCounter(Buffer const &, ParagraphList::iterator pit);
435         ///
436         void deleteWordForward(LCursor & cur);
437         ///
438         void deleteWordBackward(LCursor & cur);
439         ///
440         void deleteLineForward(LCursor & cur);
441
442         /// sets row.end to the pos value *after* which a row should break.
443         /// for example, the pos after which isNewLine(pos) == true
444         void rowBreakPoint(ParagraphList::iterator pit, Row & row) const;
445         /// sets row.witdh to the minimum space a row needs on the screen in pixel
446         void fill(ParagraphList::iterator pit, Row & row, int workwidth) const;
447         /// the minimum space a manual label needs on the screen in pixels
448         int labelFill(ParagraphList::iterator pit, Row const & row) const;
449         /// FIXME
450         int labelEnd(ParagraphList::iterator pit) const;
451
452         ///
453         void charInserted();
454         /// set 'number' font property
455         void number();
456         /// is the cursor paragraph right-to-left?
457         bool rtl() const;
458 };
459
460 /// return the default height of a row in pixels, considering font zoom
461 int defaultRowHeight();
462
463 ///
464 std::string expandLabel(LyXTextClass const & textclass,
465                 LyXLayout_ptr const & layout, bool appendix);
466
467 #endif // LYXTEXT_H