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