]> git.lyx.org Git - lyx.git/blob - src/lyxtext.h
white-space changes, removed definitions.h several enum changes because of this,...
[lyx.git] / src / lyxtext.h
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  * 
5  *           LyX, The Document Processor
6  *       
7  *           Copyright 1995 Matthias Ettrich
8  *           Copyright 1995-1999 The LyX Team.
9  *
10  * ====================================================== */
11
12 #ifndef LYXTEXT_H
13 #define LYXTEXT_H
14
15 #ifdef __GNUG__
16 #pragma interface
17 #endif
18
19 #include "lyxfont.h"
20 #include "lyxrow.h"
21 #include "undo.h"
22 #include "lyxcursor.h"
23 #include "lyxparagraph.h"
24
25 class Buffer;
26 class BufferParams;
27 class LyXScreen;
28
29 /**
30   This class holds the mapping between buffer paragraphs and screen rows.
31   */
32 class LyXText {
33 public:
34         ///
35         enum text_status {
36                 ///
37                 UNCHANGED,
38                 ///
39                 NEED_LITTLE_REFRESH,
40                 ///
41                 NEED_MORE_REFRESH,
42                 ///
43                 NEED_VERY_LITTLE_REFRESH
44         };
45
46         /// points to Buffer.params
47         BufferParams * parameters;
48         /// points to Buffer
49         Buffer * params;
50         ///
51         int number_of_rows;
52         ///
53         long height;
54         /// the current font settings
55         LyXFont current_font;
56         /// the current font
57         LyXFont real_current_font;
58
59         /// Constructor
60         LyXText(int paperwidth, Buffer *);
61    
62         /// Destructor
63         ~LyXText();
64    
65         ///
66         LyXFont GetFont(LyXParagraph * par,
67                         LyXParagraph::size_type pos);
68         ///
69         void SetCharFont(LyXParagraph * par,
70                          LyXParagraph::size_type pos,
71                          LyXFont font);
72         /// returns a pointer to the very first LyXParagraph
73         LyXParagraph * FirstParagraph();
74   
75         /// what you expect when pressing <enter> at cursor position
76         void BreakParagraph(char keep_layout = 0);
77
78         /** set layout over selection and make a total rebreak of
79           those paragraphs
80           */
81         void SetLayout(char layout);
82         
83         /// used in setlayout
84         void MakeFontEntriesLayoutSpecific(LyXParagraph * par);
85         
86         /** increment depth over selection and make a total rebreak of those 
87           paragraphs
88           */
89         void IncDepth();
90         
91         /** decrement depth over selection and make a total rebreak of those  
92           paragraphs */
93         void DecDepth();
94
95         /** Get the depth at current cursor position
96          */
97         int GetDepth() { return cursor.par->GetDepth(); }
98         
99         /** set font over selection and make a total rebreak of those
100           paragraphs.
101           toggleall defaults to false.
102           */
103         void SetFont(LyXFont font, bool toggleall = false);
104         
105         /** deletes and inserts again all paragaphs between the cursor
106           and the specified par .The Cursor is needed to set the refreshing
107           parameters. 
108           This function is needed after SetLayout and SetFont etc.
109           */
110         void RedoParagraphs(LyXCursor cursor, LyXParagraph * end_par);
111         ///
112         void RedoParagraph();
113         
114         ///
115         void ToggleFree(LyXFont font, bool toggleall = false);
116         
117         /** recalculates the heights of all previous rows of the
118             specified paragraph.  needed, if the last characters font
119             has changed.  
120             */
121         void RedoHeightOfParagraph(LyXCursor cursor);
122         
123         /** forces the redrawing of a paragraph. Needed when manipulating a 
124             right address box
125             */ 
126         void RedoDrawingOfParagraph(LyXCursor cursor);
127         
128         /** insert a character, moves all the following breaks in the 
129           same Paragraph one to the right and make a little rebreak
130           */
131         void InsertChar(char c);
132         ///
133         void InsertInset(Inset * inset);
134    
135         /// completes the insertion with a full rebreak
136         int FullRebreak();
137    
138         /// may be important for the menu
139         char * GetLayout(int row);
140         ///
141         LyXParagraph::footnote_flag GetFootnoteFlag(int row);
142         ///
143         Row * need_break_row;
144         ///
145         long refresh_y;
146         ///
147         int refresh_height;
148         ///
149         int refresh_width;
150         ///
151         int refresh_x;
152         ///
153         Row * refresh_row;
154         ///
155         int refresh_pos;
156         
157         /** wether the screen needs a refresh,
158            starting with refresh_y
159            */
160         text_status status;
161         
162         /** returns a pointer to the row near the specified y-coordinate
163           (relative to the whole text). y is set to the real beginning
164           of this row
165           */ 
166         Row * GetRowNearY(long & y);
167         
168         /** returns the column near the specified x-coordinate of the row 
169          x is set to the real beginning of this column
170          */ 
171         int GetColumnNearX(Row * row, int & x);
172         
173         /** returns a pointer to a specified row. y is set to the beginning
174          of the row
175          */
176         Row * GetRow(LyXParagraph * par,
177                      LyXParagraph::size_type pos, long & y);
178         /** returns the height of a default row, needed  for scrollbar
179          */
180         int DefaultHeight();
181    
182         /** The cursor.
183           Later this variable has to be removed. There should be now internal
184           cursor in a text (and thus not in a buffer). By keeping this it is
185           (I think) impossible to have several views with the same buffer, but
186           the cursor placed at different places.
187           */
188         LyXCursor cursor;
189    
190         /* the selection cursor */
191         /// 
192         bool selection;
193         ///
194         bool mark_set;
195
196         ///
197         LyXCursor sel_cursor;
198         ///
199         LyXCursor sel_start_cursor;
200         ///
201         LyXCursor sel_end_cursor;
202         /// needed for the toggling
203         LyXCursor last_sel_cursor;
204         ///
205         LyXCursor toggle_cursor;
206         ///
207         LyXCursor toggle_end_cursor;
208    
209         /// need the selection cursor:
210         void SetSelection();
211         ///
212         void ClearSelection();
213
214         /// just selects the word the cursor is in
215         void SelectWord();
216
217         /** 'selects" the next word, where the cursor is not in 
218          and returns this word as string. THe cursor will be moved 
219          to the beginning of this word. 
220          With SelectSelectedWord can this be highlighted really
221          */ 
222         char * SelectNextWord(float & value);
223         ///
224         void SelectSelectedWord();
225         ///
226         void SetCursor(LyXParagraph * par,
227                        LyXParagraph::size_type pos);
228         ///
229         void SetCursorIntern(LyXParagraph * par,
230                              LyXParagraph::size_type pos);
231         ///
232         void SetCursorFromCoordinates(int x, long y);
233         ///
234         void CursorUp();
235         ///
236         void CursorDown();
237         ///
238         void CursorLeft();
239         ///
240         void CursorRight();
241         ///
242         void CursorLeftOneWord();
243         ///
244         void CursorRightOneWord();
245         ///
246         void CursorUpParagraph();
247         ///
248         void CursorDownParagraph();
249         ///
250         void CursorHome();
251         ///
252         void CursorEnd();
253         ///
254         void CursorTab();
255         ///
256         void CursorTop();
257         ///
258         void CursorBottom();
259         ///
260         void Delete();
261         ///
262         void Backspace();
263         ///
264         void DeleteWordForward();
265         ///
266         void DeleteWordBackward();
267         ///
268         void DeleteLineForward();
269         ///
270         int SelectWordWhenUnderCursor();
271         /// Change the case of the word at cursor position
272         /** Change the case of the word at cursor position.
273             action is 0 for lower case, 1 for capitalization and 2 for
274             uppercase. 
275          */
276         enum TextCase {
277                 text_lowercase = 0,
278                 text_capitalization = 1,
279                 text_uppercase = 2
280         };
281         void ChangeWordCase(TextCase action);
282
283         /** returns a printed row in a pixmap. The y value is needed to
284           decide, wether it is selected text or not. This is a strange
285           solution but faster.
286          */ 
287         void GetVisibleRow(LyXScreen & scr, int offset, 
288                            Row * row_ptr, long y);
289                                            
290         /* footnotes: */
291         ///
292         void ToggleFootnote();
293         ///
294         void OpenStuff();
295         ///
296         void OpenFootnotes();
297         ///
298         void OpenFootnote();
299         ///
300         void CloseFootnotes();
301         ///
302         void CloseFootnote();
303
304         /** turn the selection into a new environment. If there is no
305           selection, create an empty environment
306          */ 
307         void InsertFootnoteEnvironment(LyXParagraph::footnote_kind kind);
308         ///
309         void MeltFootnoteEnvironment();
310         ///
311         void CutSelection(bool = true);
312         ///
313         void CopySelection();
314         ///
315         void PasteSelection();
316         ///
317         void copyEnvironmentType();
318         ///
319         void pasteEnvironmentType();
320         ///
321         void InsertFootnote();
322         ///
323         void InsertMarginpar();
324         ///
325         void InsertFigure();
326         ///
327         void InsertTabular();
328
329         /** the DTP switches for paragraphs. LyX will store the top settings
330          always in the first physical paragraph, the bottom settings in the
331          last. When a paragraph is broken, the top settings rest, the bottom 
332          settings are given to the new one. So I can make shure, they do not
333          duplicate themself (and you cannnot make dirty things with them! )
334          */ 
335         void SetParagraph(bool line_top, bool line_bottom,
336                           bool pagebreak_top, bool pagebreak_bottom,
337                           VSpace space_top, VSpace space_bottom,
338                           LyXAlignment align, 
339                           string labelwidthstring,
340                           bool noindent);
341         void SetParagraphExtraOpt(int type,
342                                   char const * width,
343                                   char const * widthp,
344                                   int alignment, bool hfill,
345                                   bool start_minipage);
346
347         /* these things are for search and replace */
348
349         /** returns true if the specified string is at the specified
350           position
351           */
352         bool IsStringInText(LyXParagraph * par,
353                             LyXParagraph::size_type pos,
354                             char const * str);
355         /** sets the selection over the number of characters of string,
356           no check!!
357           */
358         void SetSelectionOverString(char const * str);
359
360         /** simple replacing. The font of the first selected character
361           is used
362           */
363         void ReplaceSelectionWithString(char const * string);
364
365         /** if the string can be found: return true and set the cursor to
366           the new position */
367         bool SearchForward(char const * string);
368         bool SearchBackward(char const * string);
369
370         /// needed to insert the selection
371         void InsertStringA(LyXParagraph::TextContainer const & text);
372         /// needed to insert the selection
373         void InsertStringB(LyXParagraph::TextContainer const & text);
374         /// needed to insert the selection
375         void InsertStringA(char const * string);
376         /// needed to insert the selection
377         void InsertStringB(char const * string);
378
379         /// usefull when texing from within LyX
380         bool GotoNextError();
381
382         /// just another feature :)
383         bool GotoNextNote();
384
385         /** needed to switch between different classes this works
386           for a list of paragraphs beginning with the specified par 
387           return value is the number of wrong conversions
388           */ 
389         int SwitchLayoutsBetweenClasses(char class1, char class2,
390                                         LyXParagraph * par);
391
392         /* for the greater insets */
393   
394         /// returns 0 if inset wasn't found
395         int UpdateInset(Inset * inset);
396         ///
397         void CheckParagraph(LyXParagraph * par,
398                             LyXParagraph::size_type pos);
399         ///
400         int NumberOfCell(LyXParagraph * par,
401                          LyXParagraph::size_type pos);
402         /* table stuff -- begin*/
403
404         /** all table features of the text-class are put together in
405           this function. Possible values of feature are defined in table.h
406           */
407         void TableFeatures(int feature, string val);
408         ///
409         void TableFeatures(int feature);
410
411         /** pos points at the beginning of the next cell (if there is one)
412          */
413         int WidthOfCell(LyXParagraph * par, LyXParagraph::size_type & pos);
414         ///
415         void CheckParagraphInTable(LyXParagraph * par,
416                                    LyXParagraph::size_type pos);
417         ///
418         void InsertCharInTable(char c);
419         ///
420         void BackspaceInTable();
421         ///
422         char HitInTable(Row * row, int x);
423         ///
424         bool MouseHitInTable(int x, long y);
425         /* table stuff -- end*/
426         ///
427         LyXParagraph * GetParFromID(int id);
428
429         // undo functions
430         /// returns false if no undo possible
431         bool  TextUndo();
432         /// returns false if no redo possible
433         bool  TextRedo();
434         /// used by TextUndo/TextRedo
435         bool TextHandleUndo(Undo * undo);
436         /// makes sure the next operation will be stored
437         void FinishUndo();
438         /// this is dangerous and for internal use only
439         void FreezeUndo();
440         /// this is dangerous and for internal use only
441         void UnFreezeUndo();
442         /// the flag used by FinishUndo();
443         bool undo_finished;
444         /// a flag
445         bool undo_frozen;
446         ///
447         void SetUndo(Undo::undo_kind kind, LyXParagraph * before,
448                      LyXParagraph * end);
449         ///
450         void SetRedo(Undo::undo_kind kind, LyXParagraph * before,
451                      LyXParagraph * end);
452         ///
453         Undo * CreateUndo(Undo::undo_kind kind, LyXParagraph * before,
454                           LyXParagraph * end);
455         /// for external use in lyx_cb.C
456         void SetCursorParUndo();
457         ///
458         void CursorLeftIntern();
459         ///
460         void CursorRightIntern();
461         ///
462         void RemoveTableRow(LyXCursor * cursor);
463         ///
464         bool IsEmptyTableRow(LyXCursor * cursor);
465         ///
466         bool IsEmptyTableCell();
467         ///
468         void toggleAppendix();
469
470 private:
471         ///
472         Row * firstrow;
473         ///
474         Row * lastrow;
475
476         /** Copybuffer for copy environment type
477           Asger has learned that this should be a buffer-property instead
478           Lgb has learned that 'char' is a lousy type for non-characters
479           */
480         char copylayouttype;
481
482         /// the currentrow is needed to access rows faster*/ 
483         Row * currentrow;               /* pointer to the current row  */
484         /// position in the text 
485         long  currentrow_y;
486         /// width of the paper
487         unsigned short  paperwidth;
488    
489         /** inserts a new row behind the specified row, increments
490          * the touched counters */
491         void InsertRow(Row * row, LyXParagraph * par,
492                        LyXParagraph::size_type pos );
493         /** removes the row and reset the touched counters */
494         void RemoveRow(Row * row);
495
496         /** remove all following rows of the paragraph of the specified row. */
497         void RemoveParagraph(Row * row);
498
499         /** insert the specified paragraph behind the specified row */
500         void InsertParagraph(LyXParagraph * par, Row * row);
501
502         /** appends  the implizit specified paragraph behind the specified row,
503          * start at the implizit given position */
504         void AppendParagraph(Row * row);
505    
506         ///
507         void BreakAgain(Row * row);
508         ///
509         void BreakAgainOneRow(Row * row);
510         ///
511         void SetHeightOfRow(Row * row_ptr); /* calculate and set the height 
512                                             * of the row */
513
514         /** this calculates the specified parameters. needed when setting
515          * the cursor and when creating a visible row */ 
516         void PrepareToPrint(Row * row, float & x, float & fill_separator, 
517                             float & fill_hfill, float & fill_label_hfill);
518         ///
519         void DeleteEmptyParagraphMechanism(LyXCursor old_cursor);
520
521         /** Updates all counters starting BEHIND the row. Changed paragraphs
522          * with a dynamic left margin will be rebroken. */ 
523         void UpdateCounters(Row * row);
524         ///
525         void SetCounter(LyXParagraph * par);
526    
527         /*
528          * some low level functions
529          */
530         
531         ///
532         int SingleWidth(LyXParagraph * par,
533                         LyXParagraph::size_type pos);
534         ///
535         int SingleWidth(LyXParagraph * par,
536                         LyXParagraph::size_type pos, char c);
537         ///
538         void Draw(Row * row, LyXParagraph::size_type & pos,
539                   LyXScreen & scr,
540                   int offset, float & x);
541         /// get the next breakpoint in a given paragraph
542         LyXParagraph::size_type NextBreakPoint(Row * row,
543                                                int width);
544         /// returns the minimum space a row needs on the screen in pixel
545         int Fill(Row * row, int paperwidth);
546         
547         /** returns the minimum space a manual label needs on the
548           screen in pixel */ 
549         int LabelFill(Row * row);
550
551         ///
552         LyXParagraph::size_type BeginningOfMainBody(LyXParagraph * par);
553         /** Returns the left beginning of the text.
554           This information cannot be taken from the layouts-objekt, because
555           in LaTeX the beginning of the text fits in some cases
556           (for example sections) exactly the label-width.
557           */
558         int LeftMargin(Row * row);
559         ///
560         int RightMargin(Row * row);
561         ///
562         int LabelEnd (Row * row);
563
564         /** returns the number of separators in the specified row.
565           The separator on the very last column doesnt count
566           */ 
567         int NumberOfSeparators(Row * row);
568
569         /** returns the number of hfills in the specified row. The
570           LyX-Hfill is a LaTeX \hfill so that the hfills at the
571           beginning and at the end were ignored. This is {\em MUCH}
572           more usefull than not to ignore!
573           */
574         int NumberOfHfills(Row * row);
575    
576         /// like NumberOfHfills, but only those in the manual label!
577         int NumberOfLabelHfills(Row * row);
578
579         /** returns true, if a expansion is needed. Rules are given by 
580           LaTeX
581           */
582         bool HfillExpansion(Row * row_ptr,
583                             LyXParagraph::size_type pos);
584         /** returns the paragraph position of the last character in the 
585           specified row
586           */
587         LyXParagraph::size_type RowLast(Row * row);
588 };
589
590 #endif