]> git.lyx.org Git - lyx.git/blob - src/tabular.h
write \mathrm{x}\mathrm{y} as \mathrm{xy} again
[lyx.git] / src / tabular.h
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  * 
5  *           LyX, The Document Processor
6  *       
7  *           Copyright 2000-2001 The LyX Team.
8  *
9  *           @author: Jürgen Vigna
10  *
11  * ====================================================== */
12 #ifndef TABULAR_H
13 #define TABULAR_H
14
15 #ifdef __GNUG__
16 #pragma interface
17 #endif
18
19 #include <iosfwd>
20 #include <vector>
21
22 #include "lyxlex.h"
23 #include "LString.h"
24 #include "insets/insettext.h"
25
26 class InsetTabular;
27 class LaTeXFeatures;
28 class Buffer;
29
30 /* The features the text class offers for tables */ 
31
32 ///
33 class LyXTabular  {
34 public:
35         ///
36         enum Feature {
37                 ///
38                 APPEND_ROW = 0,
39                 ///
40                 APPEND_COLUMN,
41                 ///
42                 DELETE_ROW,
43                 ///
44                 DELETE_COLUMN,
45                 ///
46                 TOGGLE_LINE_TOP,
47                 ///
48                 TOGGLE_LINE_BOTTOM,
49                 ///
50                 TOGGLE_LINE_LEFT,
51                 ///
52                 TOGGLE_LINE_RIGHT,
53                 ///
54                 ALIGN_LEFT,
55                 ///
56                 ALIGN_RIGHT,
57                 ///
58                 ALIGN_CENTER,
59                 ///
60                 VALIGN_TOP,
61                 ///
62                 VALIGN_BOTTOM,
63                 ///
64                 VALIGN_CENTER,
65                 ///
66                 M_TOGGLE_LINE_TOP,
67                 ///
68                 M_TOGGLE_LINE_BOTTOM,
69                 ///
70                 M_TOGGLE_LINE_LEFT,
71                 ///
72                 M_TOGGLE_LINE_RIGHT,
73                 ///
74                 M_ALIGN_LEFT,
75                 ///
76                 M_ALIGN_RIGHT,
77                 ///
78                 M_ALIGN_CENTER,
79                 ///
80                 M_VALIGN_TOP,
81                 ///
82                 M_VALIGN_BOTTOM,
83                 ///
84                 M_VALIGN_CENTER,
85                 ///
86                 MULTICOLUMN,
87                 ///
88                 SET_ALL_LINES,
89                 ///
90                 UNSET_ALL_LINES,
91                 ///
92                 SET_LONGTABULAR,
93                 ///
94                 UNSET_LONGTABULAR,
95                 ///
96                 SET_PWIDTH,
97                 ///
98                 SET_MPWIDTH,
99                 ///
100                 SET_ROTATE_TABULAR,
101                 ///
102                 UNSET_ROTATE_TABULAR,
103                 ///
104                 SET_ROTATE_CELL,
105                 ///
106                 UNSET_ROTATE_CELL,
107                 ///
108                 SET_USEBOX,
109                 ///
110                 SET_LTHEAD,
111                 UNSET_LTHEAD,
112                 ///
113                 SET_LTFIRSTHEAD,
114                 UNSET_LTFIRSTHEAD,
115                 ///
116                 SET_LTFOOT,
117                 UNSET_LTFOOT,
118                 ///
119                 SET_LTLASTFOOT,
120                 UNSET_LTLASTFOOT,
121                 ///
122                 SET_LTNEWPAGE,
123                 ///
124                 SET_SPECIAL_COLUMN,
125                 ///
126                 SET_SPECIAL_MULTI,
127                 ///
128                 LAST_ACTION
129         };
130         ///
131         enum {
132                 ///
133                 CELL_NORMAL = 0,
134                 ///
135                 CELL_BEGIN_OF_MULTICOLUMN,
136                 ///
137                 CELL_PART_OF_MULTICOLUMN
138         };
139
140         ///
141         enum VAlignment {
142                 ///
143                 LYX_VALIGN_TOP = 0,
144                 ///
145                 LYX_VALIGN_BOTTOM = 1,
146                 ///
147                 LYX_VALIGN_CENTER = 2
148         };
149
150         enum BoxType {
151                 ///
152                 BOX_NONE = 0,
153                 ///
154                 BOX_PARBOX = 1,
155                 ///
156                 BOX_MINIPAGE = 2
157         };
158
159         struct lttype {
160                 // constructor
161                 lttype();
162                 // row of the header/footer type end definition
163                 int row;
164                 // double borders on top
165                 bool topDL;
166                 // double borders on bottom
167                 bool bottomDL;
168         };
169         ///
170         typedef struct lttype ltType;
171         
172         /* konstruktor */
173         ///
174         LyXTabular(InsetTabular *, int columns_arg, int rows_arg);
175         ///
176         LyXTabular(InsetTabular *, LyXTabular const &);
177         ///
178         explicit
179         LyXTabular(Buffer const *, InsetTabular *, LyXLex & lex);
180         ///
181         LyXTabular & operator=(LyXTabular const &);
182         ///
183         LyXTabular * clone(InsetTabular *);
184         
185         /// Returns true if there is a topline, returns false if not
186         bool TopLine(int cell, bool onlycolumn = false) const;
187         /// Returns true if there is a topline, returns false if not
188         bool BottomLine(int cell, bool onlycolumn = false) const;
189         /// Returns true if there is a topline, returns false if not
190         bool LeftLine(int cell, bool onlycolumn = false) const;
191         /// Returns true if there is a topline, returns false if not
192         bool RightLine(int cell, bool onlycolumn = false) const;
193         
194         ///
195         bool TopAlreadyDrawed(int cell) const;
196         ///
197         bool LeftAlreadyDrawed(int cell) const;
198         ///
199         bool IsLastRow(int cell) const;
200
201         ///
202         int GetAdditionalHeight(int row) const;
203         ///
204         int GetAdditionalWidth(int cell) const;
205         
206         /* returns the maximum over all rows */
207         ///
208         int GetWidthOfColumn(int cell) const;
209         ///
210         int GetWidthOfTabular() const;
211         ///
212         int GetAscentOfRow(int row) const;
213         ///
214         int GetDescentOfRow(int row) const;
215         ///
216         int GetHeightOfTabular() const;
217         /// Returns true if a complete update is necessary, otherwise false
218         bool SetAscentOfRow(int row, int height);
219         /// Returns true if a complete update is necessary, otherwise false
220         bool SetDescentOfRow(int row, int height);
221         /// Returns true if a complete update is necessary, otherwise false
222         bool SetWidthOfCell(int cell, int new_width);
223         /// Returns true if a complete update is necessary, otherwise false
224         bool SetAllLines(int cell, bool line);
225         /// Returns true if a complete update is necessary, otherwise false
226         bool SetTopLine(int cell, bool line, bool onlycolumn = false);
227         /// Returns true if a complete update is necessary, otherwise false
228         bool SetBottomLine(int cell, bool line, bool onlycolumn = false);
229         /// Returns true if a complete update is necessary, otherwise false
230         bool SetLeftLine(int cell, bool line, bool onlycolumn = false);
231         /// Returns true if a complete update is necessary, otherwise false
232         bool SetRightLine(int cell, bool line, bool onlycolumn = false);
233         /// Returns true if a complete update is necessary, otherwise false
234         bool SetAlignment(int cell, LyXAlignment align,
235                           bool onlycolumn = false);
236         /// Returns true if a complete update is necessary, otherwise false
237         bool SetVAlignment(int cell, VAlignment align,
238                            bool onlycolumn = false);
239         ///
240         bool SetColumnPWidth(int cell, string const & width);
241         ///
242         bool SetMColumnPWidth(int cell, string const & width);
243         ///
244         bool SetAlignSpecial(int cell, string const & special, Feature what);
245         ///
246         LyXAlignment GetAlignment(int cell, bool onlycolumn = false) const;
247         ///
248         VAlignment GetVAlignment(int cell, bool onlycolumn = false) const;
249         ///
250         string const GetPWidth(int cell) const;
251         ///
252         string const GetColumnPWidth(int cell) const;
253         ///
254         string const GetMColumnPWidth(int cell) const;
255         ///
256         string const GetAlignSpecial(int cell, int what) const;
257         ///
258         int GetWidthOfCell(int cell) const;
259         ///
260         int GetBeginningOfTextInCell(int cell) const;
261         ///
262         void AppendRow(int cell);
263         ///
264         void DeleteRow(int row);
265         ///
266         void AppendColumn(int cell);
267         ///
268         void DeleteColumn(int column);
269         ///
270         bool IsFirstCellInRow(int cell) const;
271         ///
272         int GetFirstCellInRow(int row) const;
273         ///
274         bool IsLastCellInRow(int cell) const;
275         ///
276         int GetLastCellInRow(int row) const;
277         ///
278         int GetNumberOfCells() const;
279         ///
280         int NumberOfCellsInRow(int cell) const;
281         ///
282         void Write(Buffer const *, std::ostream &) const;
283         ///
284         void Read(Buffer const *, LyXLex &);
285         ///
286         void OldFormatRead(LyXLex &, string const &);
287         //
288         // helper function for Latex returns number of newlines
289         ///
290         int TeXTopHLine(std::ostream &, int row) const;
291         ///
292         int TeXBottomHLine(std::ostream &, int row) const;
293         ///
294         int TeXCellPreamble(std::ostream &, int cell) const;
295         ///
296         int TeXCellPostamble(std::ostream &, int cell) const;
297         ///
298         int Latex(Buffer const *, std::ostream &, bool, bool) const;
299         /// auxiliary function for docbook rows
300         int docbookRow(Buffer const * buf, std::ostream & os, int row) const;
301         ///
302         int DocBook(Buffer const * buf, std::ostream & os) const;
303         ///
304         // helper function for Latex returns number of newlines
305         ///
306         int AsciiTopHLine(std::ostream &, int row,
307                           std::vector<unsigned int> const &) const;
308         ///
309         int AsciiBottomHLine(std::ostream &, int row,
310                              std::vector<unsigned int> const &) const;
311         ///
312         int AsciiPrintCell(Buffer const *, std::ostream &,
313                            int cell, int row, int column,
314                            std::vector<unsigned int> const &) const;
315         ///
316         int Ascii(Buffer const *, std::ostream &) const;
317         ///
318         bool IsMultiColumn(int cell, bool real = false) const;
319         ///
320         void SetMultiColumn(int cell, int number);
321         ///
322         int UnsetMultiColumn(int cell); // returns number of new cells
323         ///
324         bool IsPartOfMultiColumn(int row, int column) const;
325         ///
326         int row_of_cell(int cell) const;
327         ///
328         int column_of_cell(int cell) const;
329         ///
330         int right_column_of_cell(int cell) const;
331         ///
332         void SetLongTabular(bool);
333         ///
334         bool IsLongTabular() const;
335         ///
336         void SetRotateTabular(bool);
337         ///
338         bool GetRotateTabular() const;
339         ///
340         void SetRotateCell(int cell, bool);
341         ///
342         bool GetRotateCell(int cell) const;
343         ///
344         bool NeedRotating() const;
345         ///
346         bool IsLastCell(int cell) const;
347         ///
348         int GetCellAbove(int cell) const;
349         ///
350         int GetCellBelow(int cell) const;
351         ///
352         int GetLastCellAbove(int cell) const;
353         ///
354         int GetLastCellBelow(int cell) const;
355         ///
356         int GetCellNumber(int row, int column) const;
357         ///
358         void SetUsebox(int cell, BoxType);
359         ///
360         BoxType GetUsebox(int cell) const;
361         //
362         // Long Tabular Options
363         ///
364         bool checkLTType(int row, ltType const &) const;
365         ///
366         void SetLTHead(ltType const &, bool first);
367         ///
368         bool GetRowOfLTHead(int row, ltType &) const;
369         ///
370         bool GetRowOfLTFirstHead(int row, ltType &) const;
371         ///
372         void SetLTFoot(ltType const &, bool last);
373         ///
374         bool GetRowOfLTFoot(int row, ltType &) const;
375         ///
376         bool GetRowOfLTLastFoot(int row, ltType &) const;
377         ///
378         void SetLTNewPage(int cell, bool what);
379         ///
380         bool GetLTNewPage(int cell) const;
381         ///
382         InsetText * GetCellInset(int cell) const;
383         ///
384         InsetText * GetCellInset(int row, int column) const;
385         ///
386         int rows() const { return rows_; }
387         ///
388         int columns() const { return columns_;}
389         ///
390         InsetTabular * owner() const { return owner_; }
391         ///
392         void Validate(LaTeXFeatures &) const;
393         ///
394         std::vector<string> const getLabelList() const;
395         ///
396         mutable int cur_cell;
397 private:
398         ///
399         struct cellstruct {
400                 ///
401                 cellstruct();
402                 ///
403                 int cellno;
404                 ///
405                 int width_of_cell;
406                 ///
407                 int multicolumn;
408                 ///
409                 LyXAlignment alignment;
410                 ///
411                 VAlignment valignment;
412                 ///
413                 bool top_line;
414                 ///
415                 bool bottom_line;
416                 ///
417                 bool left_line;
418                 ///
419                 bool right_line;
420                 ///
421                 BoxType usebox;
422                 ///
423                 bool rotate;
424                 ///
425                 string align_special;
426                 ///
427                 string p_width; // this is only set for multicolumn!!!
428                 ///
429                 InsetText inset;
430         };
431         ///
432         typedef std::vector<cellstruct> cell_vector;
433         ///
434         typedef std::vector<cell_vector> cell_vvector;
435
436         ///
437         struct rowstruct {
438                 ///
439                 rowstruct();
440                 ///
441                 bool top_line;
442                 ///
443                 bool bottom_line;
444                 ///
445                 int ascent_of_row;
446                 ///
447                 int descent_of_row;
448                 /// This are for longtabulars only
449                 bool newpage;
450         };
451         ///
452         typedef std::vector<rowstruct> row_vector;
453
454         ///
455         struct columnstruct {
456                 ///
457                 columnstruct();
458                 ///
459                 LyXAlignment alignment;
460                 ///
461                 VAlignment valignment;
462                 ///
463                 bool left_line;
464                 ///
465                 bool right_line;
466                 ///
467                 int  width_of_column;
468                 ///
469                 string p_width;
470                 ///
471                 string align_special;
472         };
473         ///
474         typedef std::vector<columnstruct> column_vector;
475
476         ///
477         void ReadNew(Buffer const * buf, std::istream & is,
478                                  LyXLex & lex, string const & l);
479         ///
480         void ReadOld(Buffer const * buf, std::istream & is,
481                                  LyXLex & lex, string const & l);
482         ///
483         int rows_;
484         ///
485         int columns_;
486         ///
487         int numberofcells;
488         ///
489         std::vector<int> rowofcell;
490         ///
491         std::vector<int> columnofcell;
492         ///
493         row_vector row_info;
494         ///
495         column_vector column_info;
496         ///
497         mutable cell_vvector cell_info;
498         ///
499         int width_of_tabular;
500         ///
501         bool rotate;
502         //
503         // for long tabulars
504         ///
505         bool is_long_tabular;
506         /// row of endhead
507         ltType endhead;
508         /// row of endfirsthead
509         ltType endfirsthead;
510         /// row of endfoot
511         ltType endfoot;
512         /// row of endlastfoot
513         ltType endlastfoot;
514         ///
515         InsetTabular * owner_;
516
517         ///
518         void Init(int columns_arg, int rows_arg, LyXTabular const * lt = 0);
519         ///
520         void Reinit();
521         ///
522         void set_row_column_number_info(bool oldformat = false);
523         /// Returns true if a complete update is necessary, otherwise false
524         bool SetWidthOfMulticolCell(int cell, int new_width);
525         ///
526         void recalculateMulticolCells(int cell, int new_width);
527         /// Returns true if change
528         bool calculate_width_of_column(int column);
529         ///
530         bool calculate_width_of_column_NMC(int column); // no multi cells
531         ///
532         void calculate_width_of_tabular();
533         ///
534         cellstruct * cellinfo_of_cell(int cell) const;
535         ///
536         void delete_column(int column);
537         ///
538         int cells_in_multicolumn(int cell) const;
539         ///
540         BoxType UseParbox(int cell) const;
541 };
542
543 #endif