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