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