]> git.lyx.org Git - lyx.git/blob - src/insets/InsetTabular.h
ee5cffcd5ed8db8e7eb707cf5aff1e5c14bffc31
[lyx.git] / src / insets / InsetTabular.h
1 // -*- C++ -*-
2 /**
3  * \file InsetTabular.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Matthias Ettrich
9  * \author André Pönitz
10  * \author Jürgen Vigna
11  * \author Edwin Leuven
12  * \author Uwe Stöhr
13  * \author Scott Kostyshak
14  *
15  * Full author contact details are available in file CREDITS.
16  */
17
18 // Things to think of when designing the new tabular support:
19 // - color support (colortbl, color)
20 // - decimal alignment (dcloumn)
21 // - custom lines (hhline)
22 // - column styles
23
24 #ifndef INSET_TABULAR_H
25 #define INSET_TABULAR_H
26
27 #include "Inset.h"
28 #include "InsetText.h"
29 #include "Length.h"
30
31 #include <climits>
32 #include <iosfwd>
33 #include <memory>
34 #include <vector>
35
36
37 using std::shared_ptr;
38
39 namespace lyx {
40
41 class Buffer;
42 class BufferView;
43 class CompletionList;
44 class Cursor;
45 class CursorSlice;
46 class FuncStatus;
47 class Lexer;
48 class OutputParams;
49 class Paragraph;
50 class XHTMLStream;
51
52
53 ///
54 class InsetTableCell : public InsetText
55 {
56 public:
57         ///
58         InsetTableCell(Buffer * buf);
59         ///
60         InsetCode lyxCode() const { return CELL_CODE; }
61         ///
62         Inset * clone() const { return new InsetTableCell(*this); }
63         ///
64         bool getStatus(Cursor & cur, FuncRequest const & cmd,
65                 FuncStatus & status) const;
66         ///
67         void toggleFixedWidth(bool fw) { isFixedWidth = fw; }
68         ///
69         void setContentAlignment(LyXAlignment al) {contentAlign = al; }
70         /// writes the contents of the cell as a string, optionally
71         /// descending into insets
72         docstring asString(bool intoInsets = true);
73         ///
74         docstring xhtml(XHTMLStream &, OutputParams const &) const;
75         ///
76         void addToToc(DocIterator const & di, bool output_active,
77                                   UpdateType utype) const;
78 private:
79         /// unimplemented
80         InsetTableCell();
81         /// unimplemented
82         void operator=(InsetTableCell const &);
83         // FIXME
84         // This boolean is supposed to track whether the cell has had its
85         // width explicitly set. We need to know this to determine whether
86         // layout changes and paragraph customization are allowed---that is,
87         // we need it in forcePlainLayout() and allowParagraphCustomization().
88         // Unfortunately, that information is not readily available in
89         // InsetTableCell. In the case of multicolumn cells, it is present
90         // in CellData, and so would be available here if CellData were to
91         // become a member of InsetTableCell. But in the other case, it isn't
92         // even available there, but is held in Tabular::ColumnData.
93         // So, the present solution uses this boolean to track the information
94         // we need to track, and tries to keep it updated. This is not ideal,
95         // but the other solutions are no better. These are:
96         // (i)  Keep a pointer in InsetTableCell to the table;
97         // (ii) Find the table by iterating over the Buffer's insets.
98         // Solution (i) raises the problem of updating the pointer when an
99         // InsetTableCell is copied, and we'd therefore need a copy constructor
100         // in InsetTabular and then in Tabular, which seems messy, given how
101         // complicated those classes are. Solution (ii) involves a lot of
102         // iterating, since this information is needed quite often, and so may
103         // be quite slow.
104         // So, well, if someone can do better, please do!
105         // --rgh
106         ///
107         bool isFixedWidth;
108         // FIXME: Here the thoughts from the comment above also apply.
109         ///
110         LyXAlignment contentAlign;
111         /// should paragraph indendation be omitted in any case?
112         bool neverIndent() const { return true; }
113         ///
114         LyXAlignment contentAlignment() const { return contentAlign; }
115         ///
116         virtual bool usePlainLayout() const { return true; }
117         ///
118         virtual bool forcePlainLayout(idx_type = 0) const;
119         ///
120         virtual bool allowParagraphCustomization(idx_type = 0) const;
121         ///
122         virtual bool forceLocalFontSwitch() const;
123         /// Is the width forced to some value?
124         bool hasFixedWidth() const { return isFixedWidth; }
125         /// Can the cell contain several paragraphs?
126         /** FIXME this is wrong for multirows, that are limited to one
127          * paragraph. However, we cannot test for this (see the big
128          * comment above).
129          */
130         bool allowMultiPar() const { return isFixedWidth; }
131 };
132
133
134 //
135 // A helper struct for tables
136 //
137 class Tabular {
138 public:
139         ///
140         enum Feature {
141                 ///
142                 APPEND_ROW = 0,
143                 ///
144                 APPEND_COLUMN,
145                 ///
146                 DELETE_ROW,
147                 ///
148                 DELETE_COLUMN,
149                 ///
150                 COPY_ROW,
151                 ///
152                 COPY_COLUMN,
153                 ///
154                 MOVE_COLUMN_RIGHT,
155                 ///
156                 MOVE_COLUMN_LEFT,
157                 ///
158                 MOVE_ROW_DOWN,
159                 ///
160                 MOVE_ROW_UP,
161                 ///
162                 SET_LINE_TOP,
163                 ///
164                 SET_LINE_BOTTOM,
165                 ///
166                 SET_LINE_LEFT,
167                 ///
168                 SET_LINE_RIGHT,
169                 ///FIXME: remove
170                 TOGGLE_LINE_TOP,
171                 ///FIXME: remove
172                 TOGGLE_LINE_BOTTOM,
173                 ///FIXME: remove
174                 TOGGLE_LINE_LEFT,
175                 ///FIXME: remove
176                 TOGGLE_LINE_RIGHT,
177                 ///
178                 ALIGN_LEFT,
179                 ///
180                 ALIGN_RIGHT,
181                 ///
182                 ALIGN_CENTER,
183                 ///
184                 ALIGN_BLOCK,
185                 ///
186                 ALIGN_DECIMAL,
187                 ///
188                 VALIGN_TOP,
189                 ///
190                 VALIGN_BOTTOM,
191                 ///
192                 VALIGN_MIDDLE,
193                 ///
194                 M_ALIGN_LEFT,
195                 ///
196                 M_ALIGN_RIGHT,
197                 ///
198                 M_ALIGN_CENTER,
199                 ///
200                 M_VALIGN_TOP,
201                 ///
202                 M_VALIGN_BOTTOM,
203                 ///
204                 M_VALIGN_MIDDLE,
205                 ///
206                 MULTICOLUMN,
207                 ///
208                 SET_MULTICOLUMN,
209                 ///
210                 UNSET_MULTICOLUMN,
211                 ///
212                 MULTIROW,
213                 ///
214                 SET_MULTIROW,
215                 ///
216                 UNSET_MULTIROW,
217                 ///
218                 SET_MROFFSET,
219                 ///
220                 SET_ALL_LINES,
221                 ///
222                 UNSET_ALL_LINES,
223                 ///
224                 TOGGLE_LONGTABULAR,
225                 ///
226                 SET_LONGTABULAR,
227                 ///
228                 UNSET_LONGTABULAR,
229                 ///
230                 SET_PWIDTH,
231                 ///
232                 SET_MPWIDTH,
233                 ///
234                 SET_ROTATE_TABULAR,
235                 ///
236                 UNSET_ROTATE_TABULAR,
237                 ///
238                 TOGGLE_ROTATE_TABULAR,
239                 ///
240                 SET_ROTATE_CELL,
241                 ///
242                 UNSET_ROTATE_CELL,
243                 ///
244                 TOGGLE_ROTATE_CELL,
245                 ///
246                 SET_USEBOX,
247                 ///
248                 SET_LTHEAD,
249                 UNSET_LTHEAD,
250                 ///
251                 SET_LTFIRSTHEAD,
252                 UNSET_LTFIRSTHEAD,
253                 ///
254                 SET_LTFOOT,
255                 UNSET_LTFOOT,
256                 ///
257                 SET_LTLASTFOOT,
258                 UNSET_LTLASTFOOT,
259                 ///
260                 SET_LTNEWPAGE,
261                 UNSET_LTNEWPAGE,
262                 ///
263                 TOGGLE_LTCAPTION,
264                 ///
265                 SET_LTCAPTION,
266                 ///
267                 UNSET_LTCAPTION,
268                 ///
269                 SET_SPECIAL_COLUMN,
270                 ///
271                 SET_SPECIAL_MULTICOLUMN,
272                 ///
273                 TOGGLE_BOOKTABS,
274                 ///
275                 SET_BOOKTABS,
276                 ///
277                 UNSET_BOOKTABS,
278                 ///
279                 SET_TOP_SPACE,
280                 ///
281                 SET_BOTTOM_SPACE,
282                 ///
283                 SET_INTERLINE_SPACE,
284                 ///
285                 SET_BORDER_LINES,
286                 ///
287                 TABULAR_VALIGN_TOP,
288                 ///
289                 TABULAR_VALIGN_MIDDLE,
290                 ///
291                 TABULAR_VALIGN_BOTTOM,
292                 ///
293                 LONGTABULAR_ALIGN_LEFT,
294                 ///
295                 LONGTABULAR_ALIGN_CENTER,
296                 ///
297                 LONGTABULAR_ALIGN_RIGHT,
298                 ///
299                 SET_DECIMAL_POINT,
300                 ///
301                 SET_TABULAR_WIDTH,
302                 ///
303                 LAST_ACTION
304         };
305         ///
306         enum {
307                 ///
308                 CELL_NORMAL = 0,
309                 ///
310                 CELL_BEGIN_OF_MULTICOLUMN,
311                 ///
312                 CELL_PART_OF_MULTICOLUMN,
313                 ///
314                 CELL_BEGIN_OF_MULTIROW,
315                 ///
316                 CELL_PART_OF_MULTIROW
317         };
318
319         ///
320         enum VAlignment {
321                 ///
322                 LYX_VALIGN_TOP = 0,
323                 ///
324                 LYX_VALIGN_MIDDLE = 1,
325                 ///
326                 LYX_VALIGN_BOTTOM = 2
327
328         };
329         ///
330         enum HAlignment {
331                 ///
332                 LYX_LONGTABULAR_ALIGN_LEFT = 0,
333                 ///
334                 LYX_LONGTABULAR_ALIGN_CENTER = 1,
335                 ///
336                 LYX_LONGTABULAR_ALIGN_RIGHT = 2
337         };
338
339         enum BoxType {
340                 ///
341                 BOX_NONE = 0,
342                 ///
343                 BOX_PARBOX = 1,
344                 ///
345                 BOX_MINIPAGE = 2
346         };
347
348         enum CaptionType {
349                 ///
350                 CAPTION_FIRSTHEAD,
351                 ///
352                 CAPTION_HEAD,
353                 ///
354                 CAPTION_FOOT,
355                 ///
356                 CAPTION_LASTFOOT,
357                 ///
358                 CAPTION_ANY
359         };
360
361         enum RowDirection {
362                 UP,
363                 DOWN
364         };
365
366         enum ColDirection {
367                 RIGHT,
368                 LEFT
369         };
370
371         class ltType {
372         public:
373                 // constructor
374                 ltType();
375                 // we have this header type (is set in the getLT... functions)
376                 bool set;
377                 // double borders on top
378                 bool topDL;
379                 // double borders on bottom
380                 bool bottomDL;
381                 // used for FirstHeader & LastFooter and if this is true
382                 // all the rows marked as FirstHeader or LastFooter are
383                 // ignored in the output and it is set to be empty!
384                 bool empty;
385         };
386
387         /// type for row numbers
388         typedef size_t row_type;
389         /// type for column numbers
390         typedef size_t col_type;
391         /// type for cell indices
392         typedef size_t idx_type;
393         /// index indicating an invalid position
394         static const idx_type npos = static_cast<idx_type>(-1);
395
396         /// constructor
397         Tabular(Buffer * buf, col_type columns_arg, row_type rows_arg);
398
399         /// Returns true if there is a topline, returns false if not
400         bool topLine(idx_type cell) const;
401         /// Returns true if there is a topline, returns false if not
402         bool bottomLine(idx_type cell) const;
403         /// Returns true if there is a topline, returns false if not
404         bool leftLine(idx_type cell) const;
405         /// Returns true if there is a topline, returns false if not
406         bool rightLine(idx_type cell) const;
407
408         /// return space occupied by the second horizontal line and
409         /// interline space above row \p row in pixels
410         int interRowSpace(row_type row) const;
411         ///
412         int interColumnSpace(idx_type cell) const;
413
414         /* returns the maximum over all rows */
415         ///
416         int cellWidth(idx_type cell) const;
417         ///
418         int cellHeight(idx_type cell) const;
419         ///
420         int width() const;
421         ///
422         int height() const;
423         ///
424         row_type nrows() const {return row_info.size();}
425         ///
426         col_type ncols() const {return column_info.size();}
427         ///
428         int rowAscent(row_type row) const;
429         ///
430         int rowDescent(row_type row) const;
431         ///
432         void setRowAscent(row_type row, int height);
433         ///
434         void setRowDescent(row_type row, int height);
435         ///
436         void setTopLine(idx_type cell, bool line);
437         ///
438         void setBottomLine(idx_type cell, bool line);
439         ///
440         void setLeftLine(idx_type cell, bool line);
441         ///
442         void setRightLine(idx_type cell, bool line);
443         ///
444         bool rowTopLine(row_type row) const;
445         ///
446         bool rowBottomLine(row_type row) const;
447         ///
448         bool columnLeftLine(col_type column) const;
449         ///
450         bool columnRightLine(col_type column) const;
451
452         void setAlignment(idx_type cell, LyXAlignment align,
453                           bool onlycolumn = false);
454         ///
455         void setVAlignment(idx_type cell, VAlignment align,
456                            bool onlycolumn = false);
457         ///
458         void setTabularWidth(Length const & l) { tabular_width = l; }
459         ///
460         Length tabularWidth() const { return tabular_width; }
461         ///
462         void setColumnPWidth(Cursor &, idx_type, Length const &);
463         ///
464         bool setMColumnPWidth(Cursor &, idx_type, Length const &);
465         ///
466         bool setMROffset(Cursor &, idx_type, Length const &);
467         ///
468         void setAlignSpecial(idx_type cell, docstring const & special,
469                              Feature what);
470         ///
471         LyXAlignment getAlignment(idx_type cell,
472                                   bool onlycolumn = false) const;
473         ///
474         VAlignment getVAlignment(idx_type cell,
475                                  bool onlycolumn = false) const;
476         ///
477         Length const getPWidth(idx_type cell) const;
478         ///
479         Length const getMROffset(idx_type cell) const;
480         ///
481         int textHOffset(idx_type cell) const;
482         ///
483         int textVOffset(idx_type cell) const;
484         ///
485         void appendRow(row_type row);
486         ///
487         void deleteRow(row_type row);
488         ///
489         void copyRow(row_type row);
490         ///
491         void insertRow(row_type row, bool copy);
492         ///
493         void moveColumn(col_type col, ColDirection direction);
494         ///
495         void moveRow(row_type row, RowDirection direction);
496         ///
497         void appendColumn(col_type column);
498         ///
499         void deleteColumn(col_type column);
500         ///
501         void copyColumn(col_type column);
502         ///
503         void insertColumn(col_type column, bool copy);
504         ///
505         idx_type getFirstCellInRow(row_type row) const;
506         ///
507         idx_type getLastCellInRow(row_type row) const;
508         ///
509         idx_type numberOfCellsInRow(row_type row) const;
510         ///
511         void write(std::ostream &) const;
512         ///
513         void read(Lexer &);
514         ///
515         void latex(otexstream &, OutputParams const &) const;
516         ///
517         int docbook(odocstream & os, OutputParams const &) const;
518         ///
519         docstring xhtml(XHTMLStream & os, OutputParams const &) const;
520         ///
521         void plaintext(odocstringstream &,
522                        OutputParams const & runparams, int const depth,
523                        bool onlydata, char_type delim, size_t max_length = INT_MAX) const;
524         ///
525         bool isMultiColumn(idx_type cell) const;
526         ///
527         bool hasMultiColumn(col_type cell) const;
528         ///
529         idx_type setMultiColumn(idx_type cell, idx_type number,
530                              bool const right_border);
531         ///
532         void unsetMultiColumn(idx_type cell);
533         ///
534         bool isPartOfMultiColumn(row_type row, col_type column) const;
535         ///
536         bool isPartOfMultiRow(row_type row, col_type column) const;
537         ///
538         bool isMultiRow(idx_type cell) const;
539         ///
540         bool hasMultiRow(row_type r) const;
541         ///
542         idx_type setMultiRow(idx_type cell, idx_type number,
543                              bool const bottom_border,
544                              LyXAlignment const halign);
545         ///
546         void unsetMultiRow(idx_type cell);
547         ///
548         row_type cellRow(idx_type cell) const;
549         ///
550         col_type cellColumn(idx_type cell) const;
551         ///
552         void setRotateCell(idx_type cell, int);
553         ///
554         int getRotateCell(idx_type cell) const;
555         ///
556         bool needRotating() const;
557         ///
558         bool isLastCell(idx_type cell) const;
559         ///
560         idx_type cellAbove(idx_type cell) const;
561         ///
562         idx_type cellBelow(idx_type cell) const;
563         /// \return the index of the VISIBLE cell at row, column
564         /// this will be the same as the cell in the previous row,
565         /// e.g., if the cell is part of a multirow
566         idx_type cellIndex(row_type row, col_type column) const;
567         ///
568         void setUsebox(idx_type cell, BoxType);
569         ///
570         BoxType getUsebox(idx_type cell) const;
571         //
572         // Long Tabular Options support functions
573         ///
574         void setLTHead(row_type row, bool flag, ltType const &, bool first);
575         ///
576         bool getRowOfLTHead(row_type row, ltType &) const;
577         ///
578         bool getRowOfLTFirstHead(row_type row, ltType &) const;
579         ///
580         void setLTFoot(row_type row, bool flag, ltType const &, bool last);
581         ///
582         bool getRowOfLTFoot(row_type row, ltType &) const;
583         ///
584         bool getRowOfLTLastFoot(row_type row, ltType &) const;
585         ///
586         void setLTNewPage(row_type row, bool what);
587         ///
588         bool getLTNewPage(row_type row) const;
589         ///
590         idx_type setLTCaption(row_type row, bool what);
591         ///
592         bool ltCaption(row_type row) const;
593         ///
594         bool haveLTHead(bool withcaptions = true) const;
595         ///
596         bool haveLTFirstHead(bool withcaptions = true) const;
597         ///
598         bool haveLTFoot(bool withcaptions = true) const;
599         ///
600         bool haveLTLastFoot(bool withcaptions = true) const;
601         ///
602         bool haveLTCaption(CaptionType captiontype = CAPTION_ANY) const;
603         ///
604         // end longtable support
605
606         //@{
607         /// there is a subtle difference between these two methods.
608         ///   cellInset(r,c);
609         /// and
610         ///   cellInset(cellIndex(r,c));
611         /// can return different things. this is because cellIndex(r,c)
612         /// returns the VISIBLE cell at r,c, which may be the same as the
613         /// cell at the previous row or column, if we're dealing with some
614         /// multirow or multicell.
615         shared_ptr<InsetTableCell> cellInset(idx_type cell);
616         shared_ptr<InsetTableCell> cellInset(row_type row, col_type column);
617         InsetTableCell const * cellInset(idx_type cell) const;
618         //@}
619         ///
620         void setCellInset(row_type row, col_type column,
621                           shared_ptr<InsetTableCell>);
622         /// Search for \param inset in the tabular, with the
623         ///
624         void validate(LaTeXFeatures &) const;
625
626         //private:
627         // FIXME Now that cells have an InsetTableCell as their insets, rather
628         // than an InsetText, it'd be possible to reverse the relationship here,
629         // so that cell_vector was a vector<InsetTableCell> rather than a
630         // vector<CellData>, and an InsetTableCell had a CellData as a member,
631         // or perhaps just had its members as members.
632         ///
633         class CellData {
634         public:
635                 ///
636                 CellData(Buffer *);
637                 ///
638                 CellData(CellData const &);
639                 ///
640                 CellData & operator=(CellData const &);
641                 ///
642                 idx_type cellno;
643                 ///
644                 int width;
645                 ///
646                 int multicolumn;
647                 ///
648                 int multirow;
649                 ///
650                 Length mroffset;
651                 ///
652                 LyXAlignment alignment;
653                 ///
654                 VAlignment valignment;
655                 /// width of the part before the decimal
656                 int decimal_hoffset;
657                 /// width of the decimal part
658                 int decimal_width;
659                 ///
660                 int voffset;
661                 ///
662                 bool top_line;
663                 ///
664                 bool bottom_line;
665                 ///
666                 bool left_line;
667                 ///
668                 bool right_line;
669                 ///
670                 BoxType usebox;
671                 ///
672                 int rotate;
673                 ///
674                 docstring align_special;
675                 ///
676                 Length p_width; // this is only set for multicolumn!!!
677                 ///
678                 shared_ptr<InsetTableCell> inset;
679         };
680         CellData & cellInfo(idx_type cell) const;
681         ///
682         typedef std::vector<CellData> cell_vector;
683         ///
684         typedef std::vector<cell_vector> cell_vvector;
685
686         ///
687         class RowData {
688         public:
689                 ///
690                 RowData();
691                 ///
692                 int ascent;
693                 ///
694                 int descent;
695                 /// Extra space between the top line and this row
696                 Length top_space;
697                 /// Ignore top_space if true and use the default top space
698                 bool top_space_default;
699                 /// Extra space between this row and the bottom line
700                 Length bottom_space;
701                 /// Ignore bottom_space if true and use the default bottom space
702                 bool bottom_space_default;
703                 /// Extra space between the bottom line and the next top line
704                 Length interline_space;
705                 /// Ignore interline_space if true and use the default interline space
706                 bool interline_space_default;
707                 /// This are for longtabulars only
708                 /// a row of endhead
709                 bool endhead;
710                 /// a row of endfirsthead
711                 bool endfirsthead;
712                 /// a row of endfoot
713                 bool endfoot;
714                 /// row of endlastfoot
715                 bool endlastfoot;
716                 /// row for a newpage
717                 bool newpage;
718                 /// caption
719                 bool caption;
720         };
721         ///
722         typedef std::vector<RowData> row_vector;
723
724         ///
725         class ColumnData {
726                 public:
727                 ///
728                 ColumnData();
729                 ///
730                 LyXAlignment alignment;
731                 ///
732                 VAlignment valignment;
733                 ///
734                 int width;
735                 ///
736                 Length p_width;
737                 ///
738                 docstring align_special;
739                 ///
740                 docstring decimal_point;
741         };
742         ///
743         typedef std::vector<ColumnData> column_vector;
744
745         ///
746         idx_type numberofcells;
747         ///
748         std::vector<row_type> rowofcell;
749         ///
750         std::vector<col_type> columnofcell;
751         ///
752         row_vector row_info;
753         ///
754         column_vector column_info;
755         ///
756         mutable cell_vvector cell_info;
757         ///
758         Length tabular_width;
759         ///
760         bool use_booktabs;
761         ///
762         int rotate;
763         ///
764         VAlignment tabular_valignment;
765         //
766         // for long tabulars
767         ///
768         HAlignment longtabular_alignment;
769         //
770         bool is_long_tabular;
771         /// endhead data
772         ltType endhead;
773         /// endfirsthead data
774         ltType endfirsthead;
775         /// endfoot data
776         ltType endfoot;
777         /// endlastfoot data
778         ltType endlastfoot;
779
780         ///
781         void init(Buffer *, row_type rows_arg,
782                   col_type columns_arg);
783         ///
784         void updateIndexes();
785         ///
786         bool setFixedWidth(row_type r, col_type c);
787         /// return true of update is needed
788         bool updateColumnWidths();
789         ///
790         idx_type columnSpan(idx_type cell) const;
791         ///
792         idx_type rowSpan(idx_type cell) const;
793         ///
794         BoxType useParbox(idx_type cell) const;
795         ///
796         // helper function for Latex
797         ///
798         void TeXTopHLine(otexstream &, row_type row, std::string const & lang) const;
799         ///
800         void TeXBottomHLine(otexstream &, row_type row, std::string const & lang) const;
801         ///
802         void TeXCellPreamble(otexstream &, idx_type cell, bool & ismulticol, bool & ismultirow) const;
803         ///
804         void TeXCellPostamble(otexstream &, idx_type cell, bool ismulticol, bool ismultirow) const;
805         ///
806         void TeXLongtableHeaderFooter(otexstream &, OutputParams const &) const;
807         ///
808         bool isValidRow(row_type const row) const;
809         ///
810         void TeXRow(otexstream &, row_type const row,
811                     OutputParams const &) const;
812         ///
813         // helper functions for plain text
814         ///
815         bool plaintextTopHLine(odocstringstream &, row_type row,
816                                std::vector<unsigned int> const &) const;
817         ///
818         bool plaintextBottomHLine(odocstringstream &, row_type row,
819                                   std::vector<unsigned int> const &) const;
820         ///
821         void plaintextPrintCell(odocstringstream &,
822                                 OutputParams const &,
823                                 idx_type cell, row_type row, col_type column,
824                                 std::vector<unsigned int> const &,
825                                 bool onlydata, size_t max_length) const;
826         /// auxiliary function for docbook
827         int docbookRow(odocstream & os, row_type, OutputParams const &) const;
828         ///
829         docstring xhtmlRow(XHTMLStream & xs, row_type, OutputParams const &,
830                            bool header = false) const;
831
832         /// change associated Buffer
833         void setBuffer(Buffer & buffer);
834         /// retrieve associated Buffer
835         Buffer & buffer() const { return *buffer_; }
836
837 private:
838         Buffer * buffer_;
839
840 }; // Tabular
841
842
843 class InsetTabular : public Inset
844 {
845 public:
846         ///
847         InsetTabular(Buffer *, row_type rows = 1,
848                      col_type columns = 1);
849         ///
850         ~InsetTabular();
851         ///
852         void setBuffer(Buffer & buffer);
853
854         ///
855         static void string2params(std::string const &, InsetTabular &);
856         ///
857         static std::string params2string(InsetTabular const &);
858         ///
859         void read(Lexer &);
860         ///
861         void write(std::ostream &) const;
862         ///
863         void metrics(MetricsInfo &, Dimension &) const;
864         ///
865         void draw(PainterInfo & pi, int x, int y) const;
866         ///
867         void drawSelection(PainterInfo & pi, int x, int y) const;
868         ///
869         void drawBackground(PainterInfo & pi, int x, int y) const;
870         ///
871         bool editable() const { return true; }
872         ///
873         bool hasSettings() const { return true; }
874         ///
875         bool insetAllowed(InsetCode code) const;
876         ///
877         bool allowSpellCheck() const { return true; }
878         ///
879         bool canTrackChanges() const { return true; }
880         ///
881         bool canPaintChange(BufferView const &) const { return true; }
882         /** returns false if, when outputing LaTeX, font changes should
883             be closed before generating this inset. This is needed for
884             insets that may contain several paragraphs */
885         bool inheritFont() const { return false; }
886         ///
887         bool allowsCaptionVariation(std::string const &) const;
888         //
889         bool isTable() const { return true; }
890         ///
891         DisplayType display() const;
892         ///
893         void latex(otexstream &, OutputParams const &) const;
894         ///
895         int plaintext(odocstringstream & ods, OutputParams const & op,
896                       size_t max_length = INT_MAX) const;
897         ///
898         int docbook(odocstream &, OutputParams const &) const;
899         ///
900         docstring xhtml(XHTMLStream &, OutputParams const &) const;
901         ///
902         void validate(LaTeXFeatures & features) const;
903         ///
904         InsetCode lyxCode() const { return TABULAR_CODE; }
905         ///
906         std::string contextMenu(BufferView const &, int, int) const;
907         ///
908         std::string contextMenuName() const;
909         /// get offset of this cursor slice relative to our upper left corner
910         void cursorPos(BufferView const & bv, CursorSlice const & sl,
911                 bool boundary, int & x, int & y) const;
912         /// Executes a space-separated sequence of tabular-features requests
913         void tabularFeatures(Cursor & cur, std::string const & what);
914         /// Change a single tabular feature; does not handle undo.
915         void tabularFeatures(Cursor & cur, Tabular::Feature feature,
916                              std::string const & val = std::string());
917         /// number of cells
918         size_t nargs() const { return tabular.numberofcells; }
919         ///
920         shared_ptr<InsetTableCell const> cell(idx_type) const;
921         ///
922         shared_ptr<InsetTableCell> cell(idx_type);
923         ///
924         Text * getText(int) const;
925
926         /// set the change for the entire inset
927         void setChange(Change const & change);
928         /// accept the changes within the inset
929         void acceptChanges();
930         /// reject the changes within the inset
931         void rejectChanges();
932
933         // this should return true if we have a "normal" cell, otherwise false.
934         // "normal" means without width set!
935         /// should all paragraphs be output with "Standard" layout?
936         virtual bool allowParagraphCustomization(idx_type cell = 0) const;
937         ///
938         virtual bool forcePlainLayout(idx_type cell = 0) const;
939         ///
940         void addPreview(DocIterator const & inset_pos,
941                 graphics::PreviewLoader &) const;
942
943         /// lock cell with given index
944         void edit(Cursor & cur, bool front, EntryDirection entry_from);
945         /// get table row from x coordinate
946         int rowFromY(Cursor & cur, int y) const;
947         /// get table column from y coordinate
948         int columnFromX(Cursor & cur, int x) const;
949         ///
950         Inset * editXY(Cursor & cur, int x, int y);
951         /// can we go further down on mouse click?
952         bool descendable(BufferView const &) const { return true; }
953         /// Update the counters of this inset and of its contents
954         void updateBuffer(ParIterator const &, UpdateType);
955         ///
956         void addToToc(DocIterator const & di, bool output_active,
957                                   UpdateType utype) const;
958
959         ///
960         bool completionSupported(Cursor const &) const;
961         ///
962         bool inlineCompletionSupported(Cursor const & cur) const;
963         ///
964         bool automaticInlineCompletion() const;
965         ///
966         bool automaticPopupCompletion() const;
967         ///
968         bool showCompletionCursor() const;
969         ///
970         CompletionList const * createCompletionList(Cursor const & cur) const;
971         ///
972         docstring completionPrefix(Cursor const & cur) const;
973         ///
974         bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
975         ///
976         void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
977         ///
978         virtual bool usePlainLayout() const { return true; }
979
980         ///
981         InsetTabular * asInsetTabular() { return this; }
982         ///
983         InsetTabular const * asInsetTabular() const { return this; }
984         ///
985         bool isRightToLeft(Cursor & cur) const;
986         /// writes the cells between stidx and enidx as a string, optionally
987         /// descending into the insets
988         docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
989
990         /// Returns whether the cell in the specified row and column is selected.
991         bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
992         ///
993         void setLayoutForHiddenCells(DocumentClass const & dc);
994         //
995         // Public structures and variables
996         ///
997         mutable Tabular tabular;
998
999 private:
1000         ///
1001         InsetTabular(InsetTabular const &);
1002         ///
1003         void doDispatch(Cursor & cur, FuncRequest & cmd);
1004         ///
1005         bool getFeatureStatus(Cursor & cur, std::string const & s,
1006                          std::string const & argument, FuncStatus & status) const;
1007         ///
1008         bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
1009         ///
1010         Inset * clone() const { return new InsetTabular(*this); }
1011
1012         ///
1013         bool hitSelectRow(BufferView const & bv, int x) const;
1014         ///
1015         bool hitSelectColumn(BufferView const & bv, int y) const;
1016         /// Returns true if coordinates are on row/column selection zones
1017         bool clickable(BufferView const &, int x, int y) const;
1018
1019         ///
1020         void drawCellLines(PainterInfo &, int x, int y, row_type row,
1021                            idx_type cell) const;
1022         ///
1023         void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
1024
1025         ///
1026         void moveNextCell(Cursor & cur,
1027                                 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
1028         ///
1029         void movePrevCell(Cursor & cur,
1030                                 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
1031         ///
1032         int cellXPos(idx_type cell) const;
1033         ///
1034         int cellYPos(idx_type cell) const;
1035         ///
1036         bool copySelection(Cursor & cur);
1037         ///
1038         bool pasteClipboard(Cursor & cur);
1039         ///
1040         void cutSelection(Cursor & cur);
1041         ///
1042         void getSelection(Cursor & cur, row_type & rs, row_type & re,
1043                           col_type & cs, col_type & ce) const;
1044         ///
1045         bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
1046
1047         /// return the "Manhattan distance" to nearest corner
1048         int dist(BufferView &, idx_type cell, int x, int y) const;
1049         /// return the cell nearest to x, y
1050         idx_type getNearestCell(BufferView &, int x, int y) const;
1051
1052         /// test the rotation state of the give cell range.
1053         bool oneCellHasRotationState(bool rotated,
1054                                 row_type row_start, row_type row_end,
1055                                 col_type col_start, col_type col_end) const;
1056         ///
1057         mutable idx_type first_visible_cell_;
1058         /// The vertical offset of the table due to the vertical
1059         /// alignment with respect to the baseline.
1060         mutable int offset_valign_;
1061         /// true when selecting rows with the mouse
1062         bool rowselect_;
1063         /// true when selecting columns with the mouse
1064         bool colselect_;
1065 };
1066
1067 std::string const featureAsString(Tabular::Feature feature);
1068
1069 /// Split cell on decimal symbol
1070 InsetTableCell splitCell(InsetTableCell & head, docstring const & decimal_sym, bool & hassep);
1071
1072 } // namespace lyx
1073
1074 #endif // INSET_TABULAR_H