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