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