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