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