]> git.lyx.org Git - lyx.git/blob - src/tabular.h
ed06496481499a31f121844ccebc0f027bc999e6
[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 Buffer;
28 class LyXLex;
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                 ALIGN_BLOCK,
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(BufferParams const &,
181                    InsetTabular *, int columns_arg, int rows_arg);
182         ///
183         LyXTabular(BufferParams const &,
184                    InsetTabular *, LyXTabular const &, bool same_id = false);
185         ///
186         explicit
187         LyXTabular(Buffer const *, InsetTabular *, LyXLex & lex);
188         ///
189         LyXTabular & operator=(LyXTabular const &);
190         ///
191         LyXTabular * clone(BufferParams const &,
192                            InsetTabular *, bool same_id = false);
193
194         /// Returns true if there is a topline, returns false if not
195         bool TopLine(int cell, bool onlycolumn = false) const;
196         /// Returns true if there is a topline, returns false if not
197         bool BottomLine(int cell, bool onlycolumn = false) const;
198         /// Returns true if there is a topline, returns false if not
199         bool LeftLine(int cell, bool onlycolumn = false) const;
200         /// Returns true if there is a topline, returns false if not
201         bool RightLine(int cell, bool onlycolumn = false) const;
202
203         ///
204         bool TopAlreadyDrawed(int cell) const;
205         ///
206         bool LeftAlreadyDrawed(int cell) const;
207         ///
208         bool IsLastRow(int cell) const;
209
210         ///
211         int GetAdditionalHeight(int row) const;
212         ///
213         int GetAdditionalWidth(int cell) const;
214
215         /* returns the maximum over all rows */
216         ///
217         int GetWidthOfColumn(int cell) const;
218         ///
219         int GetWidthOfTabular() const;
220         ///
221         int GetAscentOfRow(int row) const;
222         ///
223         int GetDescentOfRow(int row) const;
224         ///
225         int GetHeightOfTabular() const;
226         /// Returns true if a complete update is necessary, otherwise false
227         bool SetAscentOfRow(int row, int height);
228         /// Returns true if a complete update is necessary, otherwise false
229         bool SetDescentOfRow(int row, int height);
230         /// Returns true if a complete update is necessary, otherwise false
231         bool SetWidthOfCell(int cell, int new_width);
232         /// Returns true if a complete update is necessary, otherwise false
233         bool SetAllLines(int cell, bool line);
234         /// Returns true if a complete update is necessary, otherwise false
235         bool SetTopLine(int cell, bool line, bool onlycolumn = false);
236         /// Returns true if a complete update is necessary, otherwise false
237         bool SetBottomLine(int cell, bool line, bool onlycolumn = false);
238         /// Returns true if a complete update is necessary, otherwise false
239         bool SetLeftLine(int cell, bool line, bool onlycolumn = false);
240         /// Returns true if a complete update is necessary, otherwise false
241         bool SetRightLine(int cell, bool line, bool onlycolumn = false);
242         /// Returns true if a complete update is necessary, otherwise false
243         bool SetAlignment(int cell, LyXAlignment align,
244                           bool onlycolumn = false);
245         /// Returns true if a complete update is necessary, otherwise false
246         bool SetVAlignment(int cell, VAlignment align,
247                            bool onlycolumn = false);
248         ///
249         bool SetColumnPWidth(int cell, LyXLength const & width);
250         ///
251         bool SetMColumnPWidth(int cell, LyXLength const & width);
252         ///
253         bool SetAlignSpecial(int cell, string const & special, Feature what);
254         ///
255         LyXAlignment GetAlignment(int cell, bool onlycolumn = false) const;
256         ///
257         VAlignment GetVAlignment(int cell, bool onlycolumn = false) const;
258         ///
259         LyXLength const GetPWidth(int cell) const;
260         ///
261         LyXLength const GetColumnPWidth(int cell) const;
262         ///
263         LyXLength const GetMColumnPWidth(int cell) const;
264         ///
265         string const GetAlignSpecial(int cell, int what) const;
266         ///
267         int GetWidthOfCell(int cell) const;
268         ///
269         int GetBeginningOfTextInCell(int cell) const;
270         ///
271         void AppendRow(BufferParams const &, int cell);
272         ///
273         void DeleteRow(int row);
274         ///
275         void AppendColumn(BufferParams const &, int cell);
276         ///
277         void DeleteColumn(int column);
278         ///
279         bool IsFirstCellInRow(int cell) const;
280         ///
281         int GetFirstCellInRow(int row) const;
282         ///
283         bool IsLastCellInRow(int cell) const;
284         ///
285         int GetLastCellInRow(int row) const;
286         ///
287         int GetNumberOfCells() const;
288         ///
289         int NumberOfCellsInRow(int cell) const;
290         ///
291         void Write(Buffer const *, std::ostream &) const;
292         ///
293         void Read(Buffer const *, LyXLex &);
294         ///
295         void OldFormatRead(BufferParams const &, LyXLex &, string const &);
296         ///
297         int latex(Buffer const *, std::ostream &, bool, bool) const;
298         ///
299         int docbook(Buffer const * buf, std::ostream & os, bool mixcont) const;
300         ///
301         int ascii(Buffer const *, std::ostream &, int const depth,
302                   bool onlydata, unsigned char delim) const;
303         ///
304         bool IsMultiColumn(int cell, bool real = false) const;
305         ///
306         void SetMultiColumn(Buffer const *, int cell, int number);
307         ///
308         int UnsetMultiColumn(int cell); // returns number of new cells
309         ///
310         bool IsPartOfMultiColumn(int row, int column) const;
311         ///
312         int row_of_cell(int cell) const;
313         ///
314         int column_of_cell(int cell) const;
315         ///
316         int right_column_of_cell(int cell) const;
317         ///
318         void SetLongTabular(bool);
319         ///
320         bool IsLongTabular() const;
321         ///
322         void SetRotateTabular(bool);
323         ///
324         bool GetRotateTabular() const;
325         ///
326         void SetRotateCell(int cell, bool);
327         ///
328         bool GetRotateCell(int cell) const;
329         ///
330         bool NeedRotating() const;
331         ///
332         bool IsLastCell(int cell) const;
333         ///
334         int GetCellAbove(int cell) const;
335         ///
336         int GetCellBelow(int cell) const;
337         ///
338         int GetLastCellAbove(int cell) const;
339         ///
340         int GetLastCellBelow(int cell) const;
341         ///
342         int GetCellNumber(int row, int column) const;
343         ///
344         void SetUsebox(int cell, BoxType);
345         ///
346         BoxType GetUsebox(int cell) const;
347         //
348         // Long Tabular Options support functions
349         ///
350         bool checkLTType(int row, ltType const &) const;
351         ///
352         void SetLTHead(int row, bool flag, ltType const &, bool first);
353         ///
354         bool GetRowOfLTHead(int row, ltType &) const;
355         ///
356         bool GetRowOfLTFirstHead(int row, ltType &) const;
357         ///
358         void SetLTFoot(int row, bool flag, ltType const &, bool last);
359         ///
360         bool GetRowOfLTFoot(int row, ltType &) const;
361         ///
362         bool GetRowOfLTLastFoot(int row, ltType &) const;
363         ///
364         void SetLTNewPage(int row, bool what);
365         ///
366         bool GetLTNewPage(int row) const;
367         ///
368         bool haveLTHead() const;
369         ///
370         bool haveLTFirstHead() const;
371         ///
372         bool haveLTFoot() const;
373         ///
374         bool haveLTLastFoot() const;
375         ///
376         // end longtable support
377         ///
378         InsetText * GetCellInset(int cell) const;
379         ///
380         InsetText * GetCellInset(int row, int column) const;
381         /// Search for \param inset in the tabular, with the
382         /// additional hint that it could be at \param maybe_cell
383         int GetCellFromInset(Inset const * inset, int maybe_cell = -1) const;
384         ///
385         int rows() const { return rows_; }
386         ///
387         int columns() const { return columns_;}
388         ///
389         InsetTabular * owner() const { return owner_; }
390         ///
391         void Validate(LaTeXFeatures &) const;
392         ///
393         std::vector<string> const getLabelList() const;
394         ///
395         /// recalculate the widths/heights only!
396         void reinit();
397         ///
398         mutable int cur_cell;
399 private:
400         ///
401         struct cellstruct {
402                 ///
403                 cellstruct(BufferParams const &);
404                 ///
405                 int cellno;
406                 ///
407                 int width_of_cell;
408                 ///
409                 int multicolumn;
410                 ///
411                 LyXAlignment alignment;
412                 ///
413                 VAlignment valignment;
414                 ///
415                 bool top_line;
416                 ///
417                 bool bottom_line;
418                 ///
419                 bool left_line;
420                 ///
421                 bool right_line;
422                 ///
423                 BoxType usebox;
424                 ///
425                 bool rotate;
426                 ///
427                 string align_special;
428                 ///
429                 LyXLength p_width; // this is only set for multicolumn!!!
430                 ///
431                 InsetText inset;
432         };
433         ///
434         typedef std::vector<cellstruct> cell_vector;
435         ///
436         typedef std::vector<cell_vector> cell_vvector;
437
438         ///
439         struct rowstruct {
440                 ///
441                 rowstruct();
442                 ///
443                 int ascent_of_row;
444                 ///
445                 int descent_of_row;
446                 ///
447                 bool top_line;
448                 ///
449                 bool bottom_line;
450                 /// This are for longtabulars only
451                 /// a row of endhead
452                 bool endhead;
453                 /// a row of endfirsthead
454                 bool endfirsthead;
455                 /// a row of endfoot
456                 bool endfoot;
457                 /// row of endlastfoot
458                 bool endlastfoot;
459                 /// row for a pagebreak
460                 bool newpage;
461         };
462         ///
463         typedef std::vector<rowstruct> row_vector;
464
465         ///
466         struct columnstruct {
467                 ///
468                 columnstruct();
469                 ///
470                 LyXAlignment alignment;
471                 ///
472                 VAlignment valignment;
473                 ///
474                 bool left_line;
475                 ///
476                 bool right_line;
477                 ///
478                 int  width_of_column;
479                 ///
480                 LyXLength p_width;
481                 ///
482                 string align_special;
483         };
484         ///
485         typedef std::vector<columnstruct> column_vector;
486
487         ///
488         void ReadNew(Buffer const * buf, std::istream & is,
489                                  LyXLex & lex, string const & l, int const version);
490         ///
491         void ReadOld(Buffer const * buf, std::istream & is,
492                                  LyXLex & lex, string const & l);
493         ///
494         int rows_;
495         ///
496         int columns_;
497         ///
498         int numberofcells;
499         ///
500         std::vector<int> rowofcell;
501         ///
502         std::vector<int> columnofcell;
503         ///
504         row_vector row_info;
505         ///
506         column_vector column_info;
507         ///
508         mutable cell_vvector cell_info;
509         ///
510         int width_of_tabular;
511         ///
512         bool rotate;
513         //
514         // for long tabulars
515         //
516         bool is_long_tabular;
517         /// endhead data
518         ltType endhead;
519         /// endfirsthead data
520         ltType endfirsthead;
521         /// endfoot data
522         ltType endfoot;
523         /// endlastfoot data
524         ltType endlastfoot;
525         //
526         ///
527         InsetTabular * owner_;
528
529         ///
530         void Init(BufferParams const &,
531                   int columns_arg, int rows_arg, LyXTabular const * lt = 0);
532         ///
533         void Reinit(bool reset_widths = true);
534         ///
535         void set_row_column_number_info(bool oldformat = false);
536         /// Returns true if a complete update is necessary, otherwise false
537         bool SetWidthOfMulticolCell(int cell, int new_width);
538         ///
539         void recalculateMulticolumnsOfColumn(int column);
540         /// Returns true if change
541         bool calculate_width_of_column(int column);
542         ///
543         bool calculate_width_of_column_NMC(int column); // no multi cells
544         ///
545         void calculate_width_of_tabular();
546         ///
547         cellstruct * cellinfo_of_cell(int cell) const;
548         ///
549         void delete_column(int column);
550         ///
551         int cells_in_multicolumn(int cell) const;
552         ///
553         BoxType UseParbox(int cell) const;
554         ///
555         void setHeaderFooterRows(int header, int fheader, int footer, int lfooter);
556         ///
557         // helper function for Latex returns number of newlines
558         ///
559         int TeXTopHLine(std::ostream &, int row) const;
560         ///
561         int TeXBottomHLine(std::ostream &, int row) const;
562         ///
563         int TeXCellPreamble(std::ostream &, int cell) const;
564         ///
565         int TeXCellPostamble(std::ostream &, int cell) const;
566         ///
567         int TeXLongtableHeaderFooter(std::ostream &, Buffer const * buf,
568                                      bool fragile, bool fp) const;
569         ///
570         bool isValidRow(int const row) const;
571         ///
572         int TeXRow(std::ostream &, int const row, Buffer const * buf,
573                    bool fragile, bool fp) const;
574         ///
575         // helper function for ASCII returns number of newlines
576         ///
577         int asciiTopHLine(std::ostream &, int row,
578                           std::vector<unsigned int> const &) const;
579         ///
580         int asciiBottomHLine(std::ostream &, int row,
581                              std::vector<unsigned int> const &) const;
582         ///
583         int asciiPrintCell(Buffer const *, std::ostream &,
584                            int cell, int row, int column,
585                            std::vector<unsigned int> const &,
586                                            bool onlydata) const;
587         /// auxiliary function for docbook
588         int docbookRow(Buffer const * buf, std::ostream & os, int row) const;
589 };
590
591 #endif