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