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