]> git.lyx.org Git - lyx.git/blob - src/tabular.h
namespace grfx -> lyx::graphics
[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         /// constructor
178         LyXTabular(BufferParams const &,
179                    InsetTabular *, int columns_arg, int rows_arg);
180         /// sort of copy constructor
181         LyXTabular(BufferParams const &,
182                    InsetTabular *, LyXTabular const &);
183         ///
184         explicit
185         LyXTabular(Buffer const *, InsetTabular *, LyXLex & lex);
186         ///
187         LyXTabular * clone(BufferParams const &, InsetTabular *);
188
189         /// Returns true if there is a topline, returns false if not
190         bool topLine(int cell, bool onlycolumn = false) const;
191         /// Returns true if there is a topline, returns false if not
192         bool bottomLine(int cell, bool onlycolumn = false) const;
193         /// Returns true if there is a topline, returns false if not
194         bool leftLine(int cell, bool onlycolumn = false) const;
195         /// Returns true if there is a topline, returns false if not
196         bool rightLine(int cell, bool onlycolumn = false) const;
197
198         ///
199         bool topAlreadyDrawn(int cell) const;
200         ///
201         bool leftAlreadyDrawn(int cell) const;
202         ///
203         bool isLastRow(int cell) const;
204
205         ///
206         int getAdditionalHeight(int row) const;
207         ///
208         int getAdditionalWidth(int cell) const;
209
210         /* returns the maximum over all rows */
211         ///
212         int getWidthOfColumn(int cell) const;
213         ///
214         int getWidthOfTabular() const;
215         ///
216         int getAscentOfRow(int row) const;
217         ///
218         int getDescentOfRow(int row) const;
219         ///
220         int getHeightOfTabular() const;
221         /// Returns true if a complete update is necessary, otherwise false
222         bool setAscentOfRow(int row, int height);
223         /// Returns true if a complete update is necessary, otherwise false
224         bool setDescentOfRow(int row, int height);
225         /// Returns true if a complete update is necessary, otherwise false
226         bool setWidthOfCell(int cell, int new_width);
227         /// Returns true if a complete update is necessary, otherwise false
228         bool setAllLines(int cell, bool line);
229         /// Returns true if a complete update is necessary, otherwise false
230         bool setTopLine(int cell, bool line, bool onlycolumn = false);
231         /// Returns true if a complete update is necessary, otherwise false
232         bool setBottomLine(int cell, bool line, bool onlycolumn = false);
233         /// Returns true if a complete update is necessary, otherwise false
234         bool setLeftLine(int cell, bool line, bool onlycolumn = false);
235         /// Returns true if a complete update is necessary, otherwise false
236         bool setRightLine(int cell, bool line, bool onlycolumn = false);
237         /// Returns true if a complete update is necessary, otherwise false
238         bool setAlignment(int cell, LyXAlignment align,
239                           bool onlycolumn = false);
240         /// Returns true if a complete update is necessary, otherwise false
241         bool setVAlignment(int cell, VAlignment align,
242                            bool onlycolumn = false);
243         ///
244         bool setColumnPWidth(int cell, LyXLength const & width);
245         ///
246         bool setMColumnPWidth(int cell, LyXLength const & width);
247         ///
248         bool setAlignSpecial(int cell, string const & special, Feature what);
249         ///
250         LyXAlignment getAlignment(int cell, bool onlycolumn = false) const;
251         ///
252         VAlignment getVAlignment(int cell, bool onlycolumn = false) const;
253         ///
254         LyXLength const getPWidth(int cell) const;
255         ///
256         LyXLength const getColumnPWidth(int cell) const;
257         ///
258         LyXLength const getMColumnPWidth(int cell) const;
259         ///
260         string const getAlignSpecial(int cell, int what) const;
261         ///
262         int getWidthOfCell(int cell) const;
263         ///
264         int getBeginningOfTextInCell(int cell) const;
265         ///
266         void appendRow(BufferParams const &, int cell);
267         ///
268         void deleteRow(int row);
269         ///
270         void appendColumn(BufferParams const &, int cell);
271         ///
272         void deleteColumn(int column);
273         ///
274         bool isFirstCellInRow(int cell) const;
275         ///
276         int getFirstCellInRow(int row) const;
277         ///
278         bool isLastCellInRow(int cell) const;
279         ///
280         int getLastCellInRow(int row) const;
281         ///
282         int getNumberOfCells() const;
283         ///
284         int numberOfCellsInRow(int cell) const;
285         ///
286         void write(Buffer const *, std::ostream &) const;
287         ///
288         void read(Buffer const *, LyXLex &);
289         ///
290         int latex(Buffer const *, std::ostream &,
291                   LatexRunParams const &) const;
292         ///
293         int docbook(Buffer const * buf, std::ostream & os, bool mixcont) const;
294         ///
295         int ascii(Buffer const *, std::ostream &, int const depth,
296                   bool onlydata, unsigned char delim) const;
297         ///
298         bool isMultiColumn(int cell, bool real = false) const;
299         ///
300         void setMultiColumn(Buffer *, int cell, int number);
301         ///
302         int unsetMultiColumn(int cell); // returns number of new cells
303         ///
304         bool isPartOfMultiColumn(int row, int column) const;
305         ///
306         int row_of_cell(int cell) const;
307         ///
308         int column_of_cell(int cell) const;
309         ///
310         int right_column_of_cell(int cell) const;
311         ///
312         void setLongTabular(bool);
313         ///
314         bool isLongTabular() const;
315         ///
316         void setRotateTabular(bool);
317         ///
318         bool getRotateTabular() const;
319         ///
320         void setRotateCell(int cell, bool);
321         ///
322         bool getRotateCell(int cell) const;
323         ///
324         bool needRotating() const;
325         ///
326         bool isLastCell(int cell) const;
327         ///
328         int getCellAbove(int cell) const;
329         ///
330         int getCellBelow(int cell) const;
331         ///
332         int getLastCellAbove(int cell) const;
333         ///
334         int getLastCellBelow(int cell) const;
335         ///
336         int getCellNumber(int row, int column) const;
337         ///
338         void setUsebox(int cell, BoxType);
339         ///
340         BoxType getUsebox(int cell) const;
341         //
342         // Long Tabular Options support functions
343         ///
344         bool checkLTType(int row, ltType const &) const;
345         ///
346         void setLTHead(int row, bool flag, ltType const &, bool first);
347         ///
348         bool getRowOfLTHead(int row, ltType &) const;
349         ///
350         bool getRowOfLTFirstHead(int row, ltType &) const;
351         ///
352         void setLTFoot(int row, bool flag, ltType const &, bool last);
353         ///
354         bool getRowOfLTFoot(int row, ltType &) const;
355         ///
356         bool getRowOfLTLastFoot(int row, ltType &) const;
357         ///
358         void setLTNewPage(int row, bool what);
359         ///
360         bool getLTNewPage(int row) const;
361         ///
362         bool haveLTHead() const;
363         ///
364         bool haveLTFirstHead() const;
365         ///
366         bool haveLTFoot() const;
367         ///
368         bool haveLTLastFoot() const;
369         ///
370         // end longtable support
371         ///
372         InsetText * getCellInset(int cell) const;
373         ///
374         InsetText * getCellInset(int row, int column) const;
375         /// Search for \param inset in the tabular, with the
376         /// additional hint that it could be at \param maybe_cell
377         int getCellFromInset(Inset const * inset, int maybe_cell = -1) const;
378         ///
379         int rows() const { return rows_; }
380         ///
381         int columns() const { return columns_;}
382         ///
383         void validate(LaTeXFeatures &) const;
384         ///
385         void getLabelList(std::vector<string> &) const;
386         ///
387         /// recalculate the widths/heights only!
388         void reinit();
389 private:
390         ///
391         mutable int cur_cell;
392         ///
393         struct cellstruct {
394                 ///
395                 cellstruct(BufferParams const &);
396                 ///
397                 int cellno;
398                 ///
399                 int width_of_cell;
400                 ///
401                 int multicolumn;
402                 ///
403                 LyXAlignment alignment;
404                 ///
405                 VAlignment valignment;
406                 ///
407                 bool top_line;
408                 ///
409                 bool bottom_line;
410                 ///
411                 bool left_line;
412                 ///
413                 bool right_line;
414                 ///
415                 BoxType usebox;
416                 ///
417                 bool rotate;
418                 ///
419                 string align_special;
420                 ///
421                 LyXLength p_width; // this is only set for multicolumn!!!
422                 ///
423                 InsetText inset;
424         };
425         ///
426         typedef std::vector<cellstruct> cell_vector;
427         ///
428         typedef std::vector<cell_vector> cell_vvector;
429
430         ///
431         struct rowstruct {
432                 ///
433                 rowstruct();
434                 ///
435                 int ascent_of_row;
436                 ///
437                 int descent_of_row;
438                 ///
439                 bool top_line;
440                 ///
441                 bool bottom_line;
442                 /// This are for longtabulars only
443                 /// a row of endhead
444                 bool endhead;
445                 /// a row of endfirsthead
446                 bool endfirsthead;
447                 /// a row of endfoot
448                 bool endfoot;
449                 /// row of endlastfoot
450                 bool endlastfoot;
451                 /// row for a pagebreak
452                 bool newpage;
453         };
454         ///
455         typedef std::vector<rowstruct> row_vector;
456
457         ///
458         struct columnstruct {
459                 ///
460                 columnstruct();
461                 ///
462                 LyXAlignment alignment;
463                 ///
464                 VAlignment valignment;
465                 ///
466                 bool left_line;
467                 ///
468                 bool right_line;
469                 ///
470                 int  width_of_column;
471                 ///
472                 LyXLength p_width;
473                 ///
474                 string align_special;
475         };
476         ///
477         typedef std::vector<columnstruct> column_vector;
478
479         ///
480         int rows_;
481         ///
482         int columns_;
483         ///
484         int numberofcells;
485         ///
486         std::vector<int> rowofcell;
487         ///
488         std::vector<int> columnofcell;
489         ///
490         row_vector row_info;
491         ///
492         column_vector column_info;
493         ///
494         mutable cell_vvector cell_info;
495         ///
496         int width_of_tabular;
497         ///
498         bool rotate;
499         //
500         // for long tabulars
501         //
502         bool is_long_tabular;
503         /// endhead data
504         ltType endhead;
505         /// endfirsthead data
506         ltType endfirsthead;
507         /// endfoot data
508         ltType endfoot;
509         /// endlastfoot data
510         ltType endlastfoot;
511         //
512         ///
513         InsetTabular * owner_;
514
515         ///
516         void init(BufferParams const &,
517                   int columns_arg, int rows_arg, LyXTabular const * lt = 0);
518         ///
519         void Reinit(bool reset_widths = true);
520         ///
521         LyXTabular & operator=(LyXTabular const &);
522         ///
523         void set_row_column_number_info(bool oldformat = false);
524         /// Returns true if a complete update is necessary, otherwise false
525         bool setWidthOfMulticolCell(int cell, int new_width);
526         ///
527         void recalculateMulticolumnsOfColumn(int column);
528         /// Returns true if change
529         bool calculate_width_of_column(int column);
530         ///
531         bool calculate_width_of_column_NMC(int column); // no multi cells
532         ///
533         void calculate_width_of_tabular();
534         ///
535         cellstruct * cellinfo_of_cell(int cell) const;
536         ///
537         void delete_column(int column);
538         ///
539         int cells_in_multicolumn(int cell) const;
540         ///
541         BoxType useParbox(int cell) const;
542         ///
543         void setHeaderFooterRows(int header, int fheader, int footer, int lfooter);
544         ///
545         // helper function for Latex returns number of newlines
546         ///
547         int TeXTopHLine(std::ostream &, int row) const;
548         ///
549         int TeXBottomHLine(std::ostream &, int row) const;
550         ///
551         int TeXCellPreamble(std::ostream &, int cell) const;
552         ///
553         int TeXCellPostamble(std::ostream &, int cell) const;
554         ///
555         int TeXLongtableHeaderFooter(std::ostream &, Buffer const * buf,
556                                      LatexRunParams const &) const;
557         ///
558         bool isValidRow(int const row) const;
559         ///
560         int TeXRow(std::ostream &, int const row, Buffer const * buf,
561                    LatexRunParams const &) const;
562         ///
563         // helper function for ASCII returns number of newlines
564         ///
565         int asciiTopHLine(std::ostream &, int row,
566                           std::vector<unsigned int> const &) const;
567         ///
568         int asciiBottomHLine(std::ostream &, int row,
569                              std::vector<unsigned int> const &) const;
570         ///
571         int asciiPrintCell(Buffer const *, std::ostream &,
572                            int cell, int row, int column,
573                            std::vector<unsigned int> const &,
574                                            bool onlydata) const;
575         /// auxiliary function for docbook
576         int docbookRow(Buffer const * buf, std::ostream & os, int row) const;
577 };
578
579 #endif