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