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