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