]> git.lyx.org Git - lyx.git/blob - src/tabular.h
fix
[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 <iosfwd>
20 #include <vector>
21
22 #include "lyxlex.h"
23 #include "layout.h"
24 #include "LString.h"
25 #include "insets/insettext.h"
26 #include "lyxlength.h"
27
28 class InsetTabular;
29 class LaTeXFeatures;
30 class Buffer;
31
32 /* The features the text class offers for tables */ 
33
34 ///
35 class LyXTabular  {
36 public:
37         ///
38         enum Feature {
39                 ///
40                 APPEND_ROW = 0,
41                 ///
42                 APPEND_COLUMN,
43                 ///
44                 DELETE_ROW,
45                 ///
46                 DELETE_COLUMN,
47                 ///
48                 TOGGLE_LINE_TOP,
49                 ///
50                 TOGGLE_LINE_BOTTOM,
51                 ///
52                 TOGGLE_LINE_LEFT,
53                 ///
54                 TOGGLE_LINE_RIGHT,
55                 ///
56                 ALIGN_LEFT,
57                 ///
58                 ALIGN_RIGHT,
59                 ///
60                 ALIGN_CENTER,
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         typedef struct lttype ltType;
177         
178         /* konstruktor */
179         ///
180         LyXTabular(InsetTabular *, int columns_arg, int rows_arg);
181         ///
182         LyXTabular(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(InsetTabular *, bool same_id = false);
190         
191         /// Returns true if there is a topline, returns false if not
192         bool TopLine(int cell, bool onlycolumn = false) const;
193         /// Returns true if there is a topline, returns false if not
194         bool BottomLine(int cell, bool onlycolumn = false) const;
195         /// Returns true if there is a topline, returns false if not
196         bool LeftLine(int cell, bool onlycolumn = false) const;
197         /// Returns true if there is a topline, returns false if not
198         bool RightLine(int cell, bool onlycolumn = false) const;
199         
200         ///
201         bool TopAlreadyDrawed(int cell) const;
202         ///
203         bool LeftAlreadyDrawed(int cell) const;
204         ///
205         bool IsLastRow(int cell) const;
206
207         ///
208         int GetAdditionalHeight(int row) const;
209         ///
210         int GetAdditionalWidth(int cell) const;
211         
212         /* returns the maximum over all rows */
213         ///
214         int GetWidthOfColumn(int cell) const;
215         ///
216         int GetWidthOfTabular() const;
217         ///
218         int GetAscentOfRow(int row) const;
219         ///
220         int GetDescentOfRow(int row) const;
221         ///
222         int GetHeightOfTabular() const;
223         /// Returns true if a complete update is necessary, otherwise false
224         bool SetAscentOfRow(int row, int height);
225         /// Returns true if a complete update is necessary, otherwise false
226         bool SetDescentOfRow(int row, int height);
227         /// Returns true if a complete update is necessary, otherwise false
228         bool SetWidthOfCell(int cell, int new_width);
229         /// Returns true if a complete update is necessary, otherwise false
230         bool SetAllLines(int cell, bool line);
231         /// Returns true if a complete update is necessary, otherwise false
232         bool SetTopLine(int cell, bool line, bool onlycolumn = false);
233         /// Returns true if a complete update is necessary, otherwise false
234         bool SetBottomLine(int cell, bool line, bool onlycolumn = false);
235         /// Returns true if a complete update is necessary, otherwise false
236         bool SetLeftLine(int cell, bool line, bool onlycolumn = false);
237         /// Returns true if a complete update is necessary, otherwise false
238         bool SetRightLine(int cell, bool line, bool onlycolumn = false);
239         /// Returns true if a complete update is necessary, otherwise false
240         bool SetAlignment(int cell, LyXAlignment align,
241                           bool onlycolumn = false);
242         /// Returns true if a complete update is necessary, otherwise false
243         bool SetVAlignment(int cell, VAlignment align,
244                            bool onlycolumn = false);
245         ///
246         bool SetColumnPWidth(int cell, LyXLength const & width);
247         ///
248         bool SetMColumnPWidth(int cell, LyXLength const & width);
249         ///
250         bool SetAlignSpecial(int cell, string const & special, Feature what);
251         ///
252         LyXAlignment GetAlignment(int cell, bool onlycolumn = false) const;
253         ///
254         VAlignment GetVAlignment(int cell, bool onlycolumn = false) const;
255         ///
256         LyXLength const GetPWidth(int cell) const;
257         ///
258         LyXLength const GetColumnPWidth(int cell) const;
259         ///
260         LyXLength const GetMColumnPWidth(int cell) const;
261         ///
262         string const GetAlignSpecial(int cell, int what) const;
263         ///
264         int GetWidthOfCell(int cell) const;
265         ///
266         int GetBeginningOfTextInCell(int cell) const;
267         ///
268         void AppendRow(int cell);
269         ///
270         void DeleteRow(int row);
271         ///
272         void AppendColumn(int cell);
273         ///
274         void DeleteColumn(int column);
275         ///
276         bool IsFirstCellInRow(int cell) const;
277         ///
278         int GetFirstCellInRow(int row) const;
279         ///
280         bool IsLastCellInRow(int cell) const;
281         ///
282         int GetLastCellInRow(int row) const;
283         ///
284         int GetNumberOfCells() const;
285         ///
286         int NumberOfCellsInRow(int cell) const;
287         ///
288         void Write(Buffer const *, std::ostream &) const;
289         ///
290         void Read(Buffer const *, LyXLex &);
291         ///
292         void OldFormatRead(LyXLex &, string const &);
293         //
294         // helper function for Latex returns number of newlines
295         ///
296         int TeXTopHLine(std::ostream &, int row) const;
297         ///
298         int TeXBottomHLine(std::ostream &, int row) const;
299         ///
300         int TeXCellPreamble(std::ostream &, int cell) const;
301         ///
302         int TeXCellPostamble(std::ostream &, int cell) const;
303         ///
304         int TeXLongtableHeaderFooter(std::ostream &, Buffer const * buf,
305                                      bool fragile, bool fp) const;
306         ///
307         bool isValidRow(int const row) const;
308         ///
309         int TeXRow(std::ostream &, int const row, Buffer const * buf,
310                    bool fragile, bool fp) const;
311         ///
312         int Latex(Buffer const *, std::ostream &, bool, bool) const;
313         /// auxiliary function for docbook rows
314         int docbookRow(Buffer const * buf, std::ostream & os, int row) const;
315         ///
316         int DocBook(Buffer const * buf, std::ostream & os) const;
317         ///
318         // helper function for Latex returns number of newlines
319         ///
320         int AsciiTopHLine(std::ostream &, int row,
321                           std::vector<unsigned int> const &) const;
322         ///
323         int AsciiBottomHLine(std::ostream &, int row,
324                              std::vector<unsigned int> const &) const;
325         ///
326         int AsciiPrintCell(Buffer const *, std::ostream &,
327                            int cell, int row, int column,
328                            std::vector<unsigned int> const &) const;
329         ///
330         int Ascii(Buffer const *, std::ostream &) const;
331         ///
332         bool IsMultiColumn(int cell, bool real = false) const;
333         ///
334         void SetMultiColumn(int cell, int number);
335         ///
336         int UnsetMultiColumn(int cell); // returns number of new cells
337         ///
338         bool IsPartOfMultiColumn(int row, int column) const;
339         ///
340         int row_of_cell(int cell) const;
341         ///
342         int column_of_cell(int cell) const;
343         ///
344         int right_column_of_cell(int cell) const;
345         ///
346         void SetLongTabular(bool);
347         ///
348         bool IsLongTabular() const;
349         ///
350         void SetRotateTabular(bool);
351         ///
352         bool GetRotateTabular() const;
353         ///
354         void SetRotateCell(int cell, bool);
355         ///
356         bool GetRotateCell(int cell) const;
357         ///
358         bool NeedRotating() const;
359         ///
360         bool IsLastCell(int cell) const;
361         ///
362         int GetCellAbove(int cell) const;
363         ///
364         int GetCellBelow(int cell) const;
365         ///
366         int GetLastCellAbove(int cell) const;
367         ///
368         int GetLastCellBelow(int cell) const;
369         ///
370         int GetCellNumber(int row, int column) const;
371         ///
372         void SetUsebox(int cell, BoxType);
373         ///
374         BoxType GetUsebox(int cell) const;
375         //
376         // Long Tabular Options support functions
377         ///
378         bool checkLTType(int row, ltType const &) const;
379         ///
380         void SetLTHead(int row, bool flag, ltType const &, bool first);
381         ///
382         bool GetRowOfLTHead(int row, ltType &) const;
383         ///
384         bool GetRowOfLTFirstHead(int row, ltType &) const;
385         ///
386         void SetLTFoot(int row, bool flag, ltType const &, bool last);
387         ///
388         bool GetRowOfLTFoot(int row, ltType &) const;
389         ///
390         bool GetRowOfLTLastFoot(int row, ltType &) const;
391         ///
392         void SetLTNewPage(int row, bool what);
393         ///
394         bool GetLTNewPage(int row) const;
395         ///
396         bool haveLTHead() const;
397         ///
398         bool haveLTFirstHead() const;
399         ///
400         bool haveLTFoot() const;
401         ///
402         bool haveLTLastFoot() const;
403         ///
404         // end longtable support
405         ///
406         InsetText * GetCellInset(int cell) const;
407         ///
408         InsetText * GetCellInset(int row, int column) const;
409         ///
410         int rows() const { return rows_; }
411         ///
412         int columns() const { return columns_;}
413         ///
414         InsetTabular * owner() const { return owner_; }
415         ///
416         void Validate(LaTeXFeatures &) const;
417         ///
418         std::vector<string> const getLabelList() const;
419         ///
420         /// recalculate the widths/heights only!
421         void reinit();
422         ///
423         mutable int cur_cell;
424 private:
425         ///
426         struct cellstruct {
427                 ///
428                 cellstruct();
429                 ///
430                 int cellno;
431                 ///
432                 int width_of_cell;
433                 ///
434                 int multicolumn;
435                 ///
436                 LyXAlignment alignment;
437                 ///
438                 VAlignment valignment;
439                 ///
440                 bool top_line;
441                 ///
442                 bool bottom_line;
443                 ///
444                 bool left_line;
445                 ///
446                 bool right_line;
447                 ///
448                 BoxType usebox;
449                 ///
450                 bool rotate;
451                 ///
452                 string align_special;
453                 ///
454                 LyXLength p_width; // this is only set for multicolumn!!!
455                 ///
456                 InsetText inset;
457         };
458         ///
459         typedef std::vector<cellstruct> cell_vector;
460         ///
461         typedef std::vector<cell_vector> cell_vvector;
462
463         ///
464         struct rowstruct {
465                 ///
466                 rowstruct();
467                 ///
468                 int ascent_of_row;
469                 ///
470                 int descent_of_row;
471                 ///
472                 bool top_line;
473                 ///
474                 bool bottom_line;
475                 /// This are for longtabulars only
476                 /// a row of endhead
477                 bool endhead;
478                 /// a row of endfirsthead
479                 bool endfirsthead;
480                 /// a row of endfoot
481                 bool endfoot;
482                 /// row of endlastfoot
483                 bool endlastfoot;
484                 /// row for a pagebreak
485                 bool newpage;
486         };
487         ///
488         typedef std::vector<rowstruct> row_vector;
489
490         ///
491         struct columnstruct {
492                 ///
493                 columnstruct();
494                 ///
495                 LyXAlignment alignment;
496                 ///
497                 VAlignment valignment;
498                 ///
499                 bool left_line;
500                 ///
501                 bool right_line;
502                 ///
503                 int  width_of_column;
504                 ///
505                 LyXLength p_width;
506                 ///
507                 string align_special;
508         };
509         ///
510         typedef std::vector<columnstruct> column_vector;
511
512         ///
513         void ReadNew(Buffer const * buf, std::istream & is,
514                                  LyXLex & lex, string const & l, int const version);
515         ///
516         void ReadOld(Buffer const * buf, std::istream & is,
517                                  LyXLex & lex, string const & l);
518         ///
519         int rows_;
520         ///
521         int columns_;
522         ///
523         int numberofcells;
524         ///
525         std::vector<int> rowofcell;
526         ///
527         std::vector<int> columnofcell;
528         ///
529         row_vector row_info;
530         ///
531         column_vector column_info;
532         ///
533         mutable cell_vvector cell_info;
534         ///
535         int width_of_tabular;
536         ///
537         bool rotate;
538         //
539         // for long tabulars
540         //
541         bool is_long_tabular;
542         /// endhead data
543         ltType endhead;
544         /// endfirsthead data
545         ltType endfirsthead;
546         /// endfoot data
547         ltType endfoot;
548         /// endlastfoot data
549         ltType endlastfoot;
550         //
551         ///
552         InsetTabular * owner_;
553
554         ///
555         void Init(int columns_arg, int rows_arg, LyXTabular const * lt = 0);
556         ///
557         void Reinit(bool reset_widths = true);
558         ///
559         void set_row_column_number_info(bool oldformat = false);
560         /// Returns true if a complete update is necessary, otherwise false
561         bool SetWidthOfMulticolCell(int cell, int new_width);
562         ///
563         void recalculateMulticolCells(int cell, int new_width);
564         /// Returns true if change
565         bool calculate_width_of_column(int column);
566         ///
567         bool calculate_width_of_column_NMC(int column); // no multi cells
568         ///
569         void calculate_width_of_tabular();
570         ///
571         cellstruct * cellinfo_of_cell(int cell) const;
572         ///
573         void delete_column(int column);
574         ///
575         int cells_in_multicolumn(int cell) const;
576         ///
577         BoxType UseParbox(int cell) const;
578         ///
579         void setHeaderFooterRows(int header, int fheader, int footer, int lfooter);
580 };
581
582 #endif