]> git.lyx.org Git - lyx.git/blob - src/tabular.h
Look for mathed xpms. Doesn't do anything yet due to lack of workable XPMs
[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 #ifdef __GNUG__
16 #pragma interface
17 #endif
18
19 #include "layout.h"
20 #include "LString.h"
21 #include "lyxlength.h"
22
23 #include "insets/insettext.h"
24
25 #include <iosfwd>
26 #include <vector>
27
28 class InsetTabular;
29 class BufferParams;
30 class LaTeXFeatures;
31 class Buffer;
32 class LyXLex;
33
34 /* The features the text class offers for tables */
35
36 ///
37 class LyXTabular  {
38 public:
39         ///
40         enum Feature {
41                 ///
42                 APPEND_ROW = 0,
43                 ///
44                 APPEND_COLUMN,
45                 ///
46                 DELETE_ROW,
47                 ///
48                 DELETE_COLUMN,
49                 ///
50                 TOGGLE_LINE_TOP,
51                 ///
52                 TOGGLE_LINE_BOTTOM,
53                 ///
54                 TOGGLE_LINE_LEFT,
55                 ///
56                 TOGGLE_LINE_RIGHT,
57                 ///
58                 ALIGN_LEFT,
59                 ///
60                 ALIGN_RIGHT,
61                 ///
62                 ALIGN_CENTER,
63                 ///
64                 VALIGN_TOP,
65                 ///
66                 VALIGN_BOTTOM,
67                 ///
68                 VALIGN_CENTER,
69                 ///
70                 M_TOGGLE_LINE_TOP,
71                 ///
72                 M_TOGGLE_LINE_BOTTOM,
73                 ///
74                 M_TOGGLE_LINE_LEFT,
75                 ///
76                 M_TOGGLE_LINE_RIGHT,
77                 ///
78                 M_ALIGN_LEFT,
79                 ///
80                 M_ALIGN_RIGHT,
81                 ///
82                 M_ALIGN_CENTER,
83                 ///
84                 M_VALIGN_TOP,
85                 ///
86                 M_VALIGN_BOTTOM,
87                 ///
88                 M_VALIGN_CENTER,
89                 ///
90                 MULTICOLUMN,
91                 ///
92                 SET_ALL_LINES,
93                 ///
94                 UNSET_ALL_LINES,
95                 ///
96                 SET_LONGTABULAR,
97                 ///
98                 UNSET_LONGTABULAR,
99                 ///
100                 SET_PWIDTH,
101                 ///
102                 SET_MPWIDTH,
103                 ///
104                 SET_ROTATE_TABULAR,
105                 ///
106                 UNSET_ROTATE_TABULAR,
107                 ///
108                 SET_ROTATE_CELL,
109                 ///
110                 UNSET_ROTATE_CELL,
111                 ///
112                 SET_USEBOX,
113                 ///
114                 SET_LTHEAD,
115                 UNSET_LTHEAD,
116                 ///
117                 SET_LTFIRSTHEAD,
118                 UNSET_LTFIRSTHEAD,
119                 ///
120                 SET_LTFOOT,
121                 UNSET_LTFOOT,
122                 ///
123                 SET_LTLASTFOOT,
124                 UNSET_LTLASTFOOT,
125                 ///
126                 SET_LTNEWPAGE,
127                 ///
128                 SET_SPECIAL_COLUMN,
129                 ///
130                 SET_SPECIAL_MULTI,
131                 ///
132                 LAST_ACTION
133         };
134         ///
135         enum {
136                 ///
137                 CELL_NORMAL = 0,
138                 ///
139                 CELL_BEGIN_OF_MULTICOLUMN,
140                 ///
141                 CELL_PART_OF_MULTICOLUMN
142         };
143
144         ///
145         enum VAlignment {
146                 ///
147                 LYX_VALIGN_TOP = 0,
148                 ///
149                 LYX_VALIGN_BOTTOM = 1,
150                 ///
151                 LYX_VALIGN_CENTER = 2
152         };
153
154         enum BoxType {
155                 ///
156                 BOX_NONE = 0,
157                 ///
158                 BOX_PARBOX = 1,
159                 ///
160                 BOX_MINIPAGE = 2
161         };
162
163         struct lttype {
164                 // constructor
165                 lttype();
166                 // we have this header type (is set in the GetLT... functions)
167                 bool set;
168                 // double borders on top
169                 bool topDL;
170                 // double borders on bottom
171                 bool bottomDL;
172                 // used for FirstHeader & LastFooter and if this is true
173                 // all the rows marked as FirstHeader or LastFooter are
174                 // ignored in the output and it is set to be empty!
175                 bool empty;
176         };
177         ///
178         typedef struct lttype ltType;
179
180         /* konstruktor */
181         ///
182         LyXTabular(BufferParams const &,
183                    InsetTabular *, int columns_arg, int rows_arg);
184         ///
185         LyXTabular(BufferParams const &,
186                    InsetTabular *, LyXTabular const &, bool same_id = false);
187         ///
188         explicit
189         LyXTabular(Buffer const *, InsetTabular *, LyXLex & lex);
190         ///
191         LyXTabular & operator=(LyXTabular const &);
192         ///
193         LyXTabular * clone(BufferParams const &,
194                            InsetTabular *, bool same_id = false);
195
196         /// Returns true if there is a topline, returns false if not
197         bool TopLine(int cell, bool onlycolumn = false) const;
198         /// Returns true if there is a topline, returns false if not
199         bool BottomLine(int cell, bool onlycolumn = false) const;
200         /// Returns true if there is a topline, returns false if not
201         bool LeftLine(int cell, bool onlycolumn = false) const;
202         /// Returns true if there is a topline, returns false if not
203         bool RightLine(int cell, bool onlycolumn = false) const;
204
205         ///
206         bool TopAlreadyDrawed(int cell) const;
207         ///
208         bool LeftAlreadyDrawed(int cell) const;
209         ///
210         bool IsLastRow(int cell) const;
211
212         ///
213         int GetAdditionalHeight(int row) const;
214         ///
215         int GetAdditionalWidth(int cell) const;
216
217         /* returns the maximum over all rows */
218         ///
219         int GetWidthOfColumn(int cell) const;
220         ///
221         int GetWidthOfTabular() const;
222         ///
223         int GetAscentOfRow(int row) const;
224         ///
225         int GetDescentOfRow(int row) const;
226         ///
227         int GetHeightOfTabular() const;
228         /// Returns true if a complete update is necessary, otherwise false
229         bool SetAscentOfRow(int row, int height);
230         /// Returns true if a complete update is necessary, otherwise false
231         bool SetDescentOfRow(int row, int height);
232         /// Returns true if a complete update is necessary, otherwise false
233         bool SetWidthOfCell(int cell, int new_width);
234         /// Returns true if a complete update is necessary, otherwise false
235         bool SetAllLines(int cell, bool line);
236         /// Returns true if a complete update is necessary, otherwise false
237         bool SetTopLine(int cell, bool line, bool onlycolumn = false);
238         /// Returns true if a complete update is necessary, otherwise false
239         bool SetBottomLine(int cell, bool line, bool onlycolumn = false);
240         /// Returns true if a complete update is necessary, otherwise false
241         bool SetLeftLine(int cell, bool line, bool onlycolumn = false);
242         /// Returns true if a complete update is necessary, otherwise false
243         bool SetRightLine(int cell, bool line, bool onlycolumn = false);
244         /// Returns true if a complete update is necessary, otherwise false
245         bool SetAlignment(int cell, LyXAlignment align,
246                           bool onlycolumn = false);
247         /// Returns true if a complete update is necessary, otherwise false
248         bool SetVAlignment(int cell, VAlignment align,
249                            bool onlycolumn = false);
250         ///
251         bool SetColumnPWidth(int cell, LyXLength const & width);
252         ///
253         bool SetMColumnPWidth(int cell, LyXLength const & width);
254         ///
255         bool SetAlignSpecial(int cell, string const & special, Feature what);
256         ///
257         LyXAlignment GetAlignment(int cell, bool onlycolumn = false) const;
258         ///
259         VAlignment GetVAlignment(int cell, bool onlycolumn = false) const;
260         ///
261         LyXLength const GetPWidth(int cell) const;
262         ///
263         LyXLength const GetColumnPWidth(int cell) const;
264         ///
265         LyXLength const GetMColumnPWidth(int cell) const;
266         ///
267         string const GetAlignSpecial(int cell, int what) const;
268         ///
269         int GetWidthOfCell(int cell) const;
270         ///
271         int GetBeginningOfTextInCell(int cell) const;
272         ///
273         void AppendRow(BufferParams const &, int cell);
274         ///
275         void DeleteRow(int row);
276         ///
277         void AppendColumn(BufferParams const &, int cell);
278         ///
279         void DeleteColumn(int column);
280         ///
281         bool IsFirstCellInRow(int cell) const;
282         ///
283         int GetFirstCellInRow(int row) const;
284         ///
285         bool IsLastCellInRow(int cell) const;
286         ///
287         int GetLastCellInRow(int row) const;
288         ///
289         int GetNumberOfCells() const;
290         ///
291         int NumberOfCellsInRow(int cell) const;
292         ///
293         void Write(Buffer const *, std::ostream &) const;
294         ///
295         void Read(Buffer const *, LyXLex &);
296         ///
297         void OldFormatRead(BufferParams const &, LyXLex &, string const &);
298         ///
299         int latex(Buffer const *, std::ostream &, bool, bool) const;
300         ///
301         int docbook(Buffer const * buf, std::ostream & os, bool mixcont) const;
302         ///
303         int ascii(Buffer const *, std::ostream &, int const depth,
304                   bool onlydata, unsigned char delim) const;
305         ///
306         bool IsMultiColumn(int cell, bool real = false) const;
307         ///
308         void SetMultiColumn(Buffer const *, int cell, int number);
309         ///
310         int UnsetMultiColumn(int cell); // returns number of new cells
311         ///
312         bool IsPartOfMultiColumn(int row, int column) const;
313         ///
314         int row_of_cell(int cell) const;
315         ///
316         int column_of_cell(int cell) const;
317         ///
318         int right_column_of_cell(int cell) const;
319         ///
320         void SetLongTabular(bool);
321         ///
322         bool IsLongTabular() const;
323         ///
324         void SetRotateTabular(bool);
325         ///
326         bool GetRotateTabular() const;
327         ///
328         void SetRotateCell(int cell, bool);
329         ///
330         bool GetRotateCell(int cell) const;
331         ///
332         bool NeedRotating() const;
333         ///
334         bool IsLastCell(int cell) const;
335         ///
336         int GetCellAbove(int cell) const;
337         ///
338         int GetCellBelow(int cell) const;
339         ///
340         int GetLastCellAbove(int cell) const;
341         ///
342         int GetLastCellBelow(int cell) const;
343         ///
344         int GetCellNumber(int row, int column) const;
345         ///
346         void SetUsebox(int cell, BoxType);
347         ///
348         BoxType GetUsebox(int cell) const;
349         //
350         // Long Tabular Options support functions
351         ///
352         bool checkLTType(int row, ltType const &) const;
353         ///
354         void SetLTHead(int row, bool flag, ltType const &, bool first);
355         ///
356         bool GetRowOfLTHead(int row, ltType &) const;
357         ///
358         bool GetRowOfLTFirstHead(int row, ltType &) const;
359         ///
360         void SetLTFoot(int row, bool flag, ltType const &, bool last);
361         ///
362         bool GetRowOfLTFoot(int row, ltType &) const;
363         ///
364         bool GetRowOfLTLastFoot(int row, ltType &) const;
365         ///
366         void SetLTNewPage(int row, bool what);
367         ///
368         bool GetLTNewPage(int row) const;
369         ///
370         bool haveLTHead() const;
371         ///
372         bool haveLTFirstHead() const;
373         ///
374         bool haveLTFoot() const;
375         ///
376         bool haveLTLastFoot() const;
377         ///
378         // end longtable support
379         ///
380         InsetText * GetCellInset(int cell) const;
381         ///
382         InsetText * GetCellInset(int row, int column) const;
383         /// Search for \param inset in the tabular, with the
384         /// additional hint that it could be at \param maybe_cell
385         int GetCellFromInset(Inset const * inset, int maybe_cell = -1) const;
386         ///
387         int rows() const { return rows_; }
388         ///
389         int columns() const { return columns_;}
390         ///
391         InsetTabular * owner() const { return owner_; }
392         ///
393         void Validate(LaTeXFeatures &) const;
394         ///
395         std::vector<string> const getLabelList() const;
396         ///
397         /// recalculate the widths/heights only!
398         void reinit();
399         ///
400         mutable int cur_cell;
401 private:
402         ///
403         struct cellstruct {
404                 ///
405                 cellstruct(BufferParams const &);
406                 ///
407                 int cellno;
408                 ///
409                 int width_of_cell;
410                 ///
411                 int multicolumn;
412                 ///
413                 LyXAlignment alignment;
414                 ///
415                 VAlignment valignment;
416                 ///
417                 bool top_line;
418                 ///
419                 bool bottom_line;
420                 ///
421                 bool left_line;
422                 ///
423                 bool right_line;
424                 ///
425                 BoxType usebox;
426                 ///
427                 bool rotate;
428                 ///
429                 string align_special;
430                 ///
431                 LyXLength p_width; // this is only set for multicolumn!!!
432                 ///
433                 InsetText inset;
434         };
435         ///
436         typedef std::vector<cellstruct> cell_vector;
437         ///
438         typedef std::vector<cell_vector> cell_vvector;
439
440         ///
441         struct rowstruct {
442                 ///
443                 rowstruct();
444                 ///
445                 int ascent_of_row;
446                 ///
447                 int descent_of_row;
448                 ///
449                 bool top_line;
450                 ///
451                 bool bottom_line;
452                 /// This are for longtabulars only
453                 /// a row of endhead
454                 bool endhead;
455                 /// a row of endfirsthead
456                 bool endfirsthead;
457                 /// a row of endfoot
458                 bool endfoot;
459                 /// row of endlastfoot
460                 bool endlastfoot;
461                 /// row for a pagebreak
462                 bool newpage;
463         };
464         ///
465         typedef std::vector<rowstruct> row_vector;
466
467         ///
468         struct columnstruct {
469                 ///
470                 columnstruct();
471                 ///
472                 LyXAlignment alignment;
473                 ///
474                 VAlignment valignment;
475                 ///
476                 bool left_line;
477                 ///
478                 bool right_line;
479                 ///
480                 int  width_of_column;
481                 ///
482                 LyXLength p_width;
483                 ///
484                 string align_special;
485         };
486         ///
487         typedef std::vector<columnstruct> column_vector;
488
489         ///
490         void ReadNew(Buffer const * buf, std::istream & is,
491                                  LyXLex & lex, string const & l, int const version);
492         ///
493         void ReadOld(Buffer const * buf, std::istream & is,
494                                  LyXLex & lex, string const & l);
495         ///
496         int rows_;
497         ///
498         int columns_;
499         ///
500         int numberofcells;
501         ///
502         std::vector<int> rowofcell;
503         ///
504         std::vector<int> columnofcell;
505         ///
506         row_vector row_info;
507         ///
508         column_vector column_info;
509         ///
510         mutable cell_vvector cell_info;
511         ///
512         int width_of_tabular;
513         ///
514         bool rotate;
515         //
516         // for long tabulars
517         //
518         bool is_long_tabular;
519         /// endhead data
520         ltType endhead;
521         /// endfirsthead data
522         ltType endfirsthead;
523         /// endfoot data
524         ltType endfoot;
525         /// endlastfoot data
526         ltType endlastfoot;
527         //
528         ///
529         InsetTabular * owner_;
530
531         ///
532         void Init(BufferParams const &,
533                   int columns_arg, int rows_arg, LyXTabular const * lt = 0);
534         ///
535         void Reinit(bool reset_widths = true);
536         ///
537         void set_row_column_number_info(bool oldformat = false);
538         /// Returns true if a complete update is necessary, otherwise false
539         bool SetWidthOfMulticolCell(int cell, int new_width);
540         ///
541         void recalculateMulticolumnsOfColumn(int column);
542         /// Returns true if change
543         bool calculate_width_of_column(int column);
544         ///
545         bool calculate_width_of_column_NMC(int column); // no multi cells
546         ///
547         void calculate_width_of_tabular();
548         ///
549         cellstruct * cellinfo_of_cell(int cell) const;
550         ///
551         void delete_column(int column);
552         ///
553         int cells_in_multicolumn(int cell) const;
554         ///
555         BoxType UseParbox(int cell) const;
556         ///
557         void setHeaderFooterRows(int header, int fheader, int footer, int lfooter);
558         ///
559         // helper function for Latex returns number of newlines
560         ///
561         int TeXTopHLine(std::ostream &, int row) const;
562         ///
563         int TeXBottomHLine(std::ostream &, int row) const;
564         ///
565         int TeXCellPreamble(std::ostream &, int cell) const;
566         ///
567         int TeXCellPostamble(std::ostream &, int cell) const;
568         ///
569         int TeXLongtableHeaderFooter(std::ostream &, Buffer const * buf,
570                                      bool fragile, bool fp) const;
571         ///
572         bool isValidRow(int const row) const;
573         ///
574         int TeXRow(std::ostream &, int const row, Buffer const * buf,
575                    bool fragile, bool fp) const;
576         ///
577         // helper function for ASCII returns number of newlines
578         ///
579         int asciiTopHLine(std::ostream &, int row,
580                           std::vector<unsigned int> const &) const;
581         ///
582         int asciiBottomHLine(std::ostream &, int row,
583                              std::vector<unsigned int> const &) const;
584         ///
585         int asciiPrintCell(Buffer const *, std::ostream &,
586                            int cell, int row, int column,
587                            std::vector<unsigned int> const &,
588                                            bool onlydata) const;
589         /// auxiliary function for docbook
590         int docbookRow(Buffer const * buf, std::ostream & os, int row) const;
591 };
592
593 #endif