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