]> git.lyx.org Git - lyx.git/blob - src/insets/InsetTabular.h
605815aa034b16a3885fd9c8df0039191d2ac833
[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, TocBackend & backend) 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                 BOX_VARWIDTH = 3
345         };
346
347         enum CaptionType {
348                 ///
349                 CAPTION_FIRSTHEAD,
350                 ///
351                 CAPTION_HEAD,
352                 ///
353                 CAPTION_FOOT,
354                 ///
355                 CAPTION_LASTFOOT,
356                 ///
357                 CAPTION_ANY
358         };
359
360         enum RowDirection {
361                 UP,
362                 DOWN
363         };
364
365         enum ColDirection {
366                 RIGHT,
367                 LEFT
368         };
369
370         class ltType {
371         public:
372                 // constructor
373                 ltType();
374                 // we have this header type (is set in the getLT... functions)
375                 bool set;
376                 // double borders on top
377                 bool topDL;
378                 // double borders on bottom
379                 bool bottomDL;
380                 // used for FirstHeader & LastFooter and if this is true
381                 // all the rows marked as FirstHeader or LastFooter are
382                 // ignored in the output and it is set to be empty!
383                 bool empty;
384         };
385
386         /// type for row numbers
387         typedef size_t row_type;
388         /// type for column numbers
389         typedef size_t col_type;
390         /// type for cell indices
391         typedef size_t idx_type;
392         /// index indicating an invalid position
393         static const idx_type npos = static_cast<idx_type>(-1);
394
395         /// constructor
396         Tabular(Buffer * buf, col_type columns_arg, row_type rows_arg);
397
398         /// Returns true if there is a topline, returns false if not
399         bool topLine(idx_type cell) const;
400         /// Returns true if there is a topline, returns false if not
401         bool bottomLine(idx_type cell) const;
402         /// Returns true if there is a topline, returns false if not
403         bool leftLine(idx_type cell) const;
404         /// Returns true if there is a topline, returns false if not
405         bool rightLine(idx_type cell) const;
406
407         /// return space occupied by the second horizontal line and
408         /// interline space above row \p row in pixels
409         int interRowSpace(row_type row) const;
410         ///
411         int interColumnSpace(idx_type cell) const;
412
413         /* returns the maximum over all rows */
414         ///
415         int cellWidth(idx_type cell) const;
416         ///
417         int cellHeight(idx_type cell) const;
418         ///
419         int width() const;
420         ///
421         int height() const;
422         ///
423         row_type nrows() const {return row_info.size();}
424         ///
425         col_type ncols() const {return column_info.size();}
426         ///
427         int rowAscent(row_type row) const;
428         ///
429         int rowDescent(row_type row) const;
430         ///
431         void setRowAscent(row_type row, int height);
432         ///
433         void setRowDescent(row_type row, int height);
434         ///
435         void setTopLine(idx_type cell, bool line);
436         ///
437         void setBottomLine(idx_type cell, bool line);
438         ///
439         void setLeftLine(idx_type cell, bool line);
440         ///
441         void setRightLine(idx_type cell, bool line);
442         ///
443         bool rowTopLine(row_type row) const;
444         ///
445         bool rowBottomLine(row_type row) const;
446         ///
447         bool columnLeftLine(col_type column) const;
448         ///
449         bool columnRightLine(col_type column) const;
450
451         void setAlignment(idx_type cell, LyXAlignment align,
452                           bool onlycolumn = false);
453         ///
454         void setVAlignment(idx_type cell, VAlignment align,
455                            bool onlycolumn = false);
456         ///
457         void setTabularWidth(Length const & l) { tabular_width = l; }
458         ///
459         Length tabularWidth() const { return tabular_width; }
460         ///
461         void setColumnPWidth(Cursor &, idx_type, Length const &);
462         ///
463         bool setMColumnPWidth(Cursor &, idx_type, Length const &);
464         ///
465         bool setMROffset(Cursor &, idx_type, Length const &);
466         ///
467         void setAlignSpecial(idx_type cell, docstring const & special,
468                              Feature what);
469         ///
470         LyXAlignment getAlignment(idx_type cell,
471                                   bool onlycolumn = false) const;
472         ///
473         VAlignment getVAlignment(idx_type cell,
474                                  bool onlycolumn = false) const;
475         /// The vertical offset of the table due to the vertical
476         /// alignment with respect to the baseline.
477         int offsetVAlignment() const;
478         ///
479         Length const getPWidth(idx_type cell) const;
480         ///
481         Length const getMROffset(idx_type cell) const;
482         ///
483         int textHOffset(idx_type cell) const;
484         ///
485         int textVOffset(idx_type cell) const;
486         ///
487         void appendRow(row_type row);
488         ///
489         void deleteRow(row_type row);
490         ///
491         void copyRow(row_type row);
492         ///
493         void insertRow(row_type row, bool copy);
494         ///
495         void moveColumn(col_type col, ColDirection direction);
496         ///
497         void moveRow(row_type row, RowDirection direction);
498         ///
499         void appendColumn(col_type column);
500         ///
501         void deleteColumn(col_type column);
502         ///
503         void copyColumn(col_type column);
504         ///
505         void insertColumn(col_type column, bool copy);
506         ///
507         idx_type getFirstCellInRow(row_type row) const;
508         ///
509         idx_type getLastCellInRow(row_type row) const;
510         ///
511         idx_type numberOfCellsInRow(row_type row) const;
512         ///
513         void write(std::ostream &) const;
514         ///
515         void read(Lexer &);
516         ///
517         void latex(otexstream &, OutputParams const &) const;
518         ///
519         int docbook(odocstream & os, OutputParams const &) const;
520         ///
521         docstring xhtml(XHTMLStream & os, OutputParams const &) const;
522         ///
523         void plaintext(odocstringstream &,
524                        OutputParams const & runparams, int const depth,
525                        bool onlydata, char_type delim, size_t max_length = INT_MAX) const;
526         ///
527         bool isMultiColumn(idx_type cell) const;
528         ///
529         bool hasMultiColumn(col_type cell) const;
530         ///
531         idx_type setMultiColumn(idx_type cell, idx_type number,
532                              bool const right_border);
533         ///
534         void unsetMultiColumn(idx_type cell);
535         ///
536         bool isPartOfMultiColumn(row_type row, col_type column) const;
537         ///
538         bool isPartOfMultiRow(row_type row, col_type column) const;
539         ///
540         bool isMultiRow(idx_type cell) const;
541         ///
542         bool hasMultiRow(row_type r) const;
543         ///
544         idx_type setMultiRow(idx_type cell, idx_type number,
545                              bool const bottom_border,
546                              LyXAlignment const halign);
547         ///
548         void unsetMultiRow(idx_type cell);
549         ///
550         row_type cellRow(idx_type cell) const;
551         ///
552         col_type cellColumn(idx_type cell) const;
553         ///
554         void setRotateCell(idx_type cell, int);
555         ///
556         int getRotateCell(idx_type cell) const;
557         ///
558         bool needRotating() const;
559         ///
560         bool isLastCell(idx_type cell) const;
561         ///
562         idx_type cellAbove(idx_type cell) const;
563         ///
564         idx_type cellBelow(idx_type cell) const;
565         /// \return the index of the VISIBLE cell at row, column
566         /// this will be the same as the cell in the previous row,
567         /// e.g., if the cell is part of a multirow
568         idx_type cellIndex(row_type row, col_type column) const;
569         ///
570         void setUsebox(idx_type cell, BoxType);
571         ///
572         BoxType getUsebox(idx_type cell) const;
573         //
574         // Long Tabular Options support functions
575         ///
576         void setLTHead(row_type row, bool flag, ltType const &, bool first);
577         ///
578         bool getRowOfLTHead(row_type row, ltType &) const;
579         ///
580         bool getRowOfLTFirstHead(row_type row, ltType &) const;
581         ///
582         void setLTFoot(row_type row, bool flag, ltType const &, bool last);
583         ///
584         bool getRowOfLTFoot(row_type row, ltType &) const;
585         ///
586         bool getRowOfLTLastFoot(row_type row, ltType &) const;
587         ///
588         void setLTNewPage(row_type row, bool what);
589         ///
590         bool getLTNewPage(row_type row) const;
591         ///
592         idx_type setLTCaption(row_type row, bool what);
593         ///
594         bool ltCaption(row_type row) const;
595         ///
596         bool haveLTHead(bool withcaptions = true) const;
597         ///
598         bool haveLTFirstHead(bool withcaptions = true) const;
599         ///
600         bool haveLTFoot(bool withcaptions = true) const;
601         ///
602         bool haveLTLastFoot(bool withcaptions = true) const;
603         ///
604         bool haveLTCaption(CaptionType captiontype = CAPTION_ANY) const;
605         ///
606         // end longtable support
607
608         //@{
609         /// there is a subtle difference between these two methods.
610         ///   cellInset(r,c);
611         /// and
612         ///   cellInset(cellIndex(r,c));
613         /// can return different things. this is because cellIndex(r,c)
614         /// returns the VISIBLE cell at r,c, which may be the same as the
615         /// cell at the previous row or column, if we're dealing with some
616         /// multirow or multicell.
617         std::shared_ptr<InsetTableCell> cellInset(idx_type cell);
618         std::shared_ptr<InsetTableCell> cellInset(row_type row, col_type column);
619         InsetTableCell const * cellInset(idx_type cell) const;
620         //@}
621         ///
622         void setCellInset(row_type row, col_type column,
623                           std::shared_ptr<InsetTableCell>);
624         /// Search for \param inset in the tabular, with the
625         ///
626         void validate(LaTeXFeatures &) const;
627
628         //private:
629         // FIXME Now that cells have an InsetTableCell as their insets, rather
630         // than an InsetText, it'd be possible to reverse the relationship here,
631         // so that cell_vector was a vector<InsetTableCell> rather than a
632         // vector<CellData>, and an InsetTableCell had a CellData as a member,
633         // or perhaps just had its members as members.
634         ///
635         class CellData {
636         public:
637                 ///
638                 CellData(Buffer *);
639                 ///
640                 CellData(CellData const &);
641                 ///
642                 CellData & operator=(CellData const &);
643                 ///
644                 idx_type cellno;
645                 ///
646                 int width;
647                 ///
648                 int multicolumn;
649                 ///
650                 int multirow;
651                 ///
652                 Length mroffset;
653                 ///
654                 LyXAlignment alignment;
655                 ///
656                 VAlignment valignment;
657                 /// width of the part before the decimal
658                 int decimal_hoffset;
659                 /// width of the decimal part
660                 int decimal_width;
661                 ///
662                 int voffset;
663                 ///
664                 bool top_line;
665                 ///
666                 bool bottom_line;
667                 ///
668                 bool left_line;
669                 ///
670                 bool right_line;
671                 ///
672                 BoxType usebox;
673                 ///
674                 int rotate;
675                 ///
676                 docstring align_special;
677                 ///
678                 Length p_width; // this is only set for multicolumn!!!
679                 ///
680                 std::shared_ptr<InsetTableCell> inset;
681         };
682         ///
683         CellData const & cellInfo(idx_type cell) const;
684         ///
685         CellData & cellInfo(idx_type cell);
686         ///
687         typedef std::vector<CellData> cell_vector;
688         ///
689         typedef std::vector<cell_vector> cell_vvector;
690
691         ///
692         class RowData {
693         public:
694                 ///
695                 RowData();
696                 ///
697                 int ascent;
698                 ///
699                 int descent;
700                 /// Extra space between the top line and this row
701                 Length top_space;
702                 /// Ignore top_space if true and use the default top space
703                 bool top_space_default;
704                 /// Extra space between this row and the bottom line
705                 Length bottom_space;
706                 /// Ignore bottom_space if true and use the default bottom space
707                 bool bottom_space_default;
708                 /// Extra space between the bottom line and the next top line
709                 Length interline_space;
710                 /// Ignore interline_space if true and use the default interline space
711                 bool interline_space_default;
712                 /// This are for longtabulars only
713                 /// a row of endhead
714                 bool endhead;
715                 /// a row of endfirsthead
716                 bool endfirsthead;
717                 /// a row of endfoot
718                 bool endfoot;
719                 /// row of endlastfoot
720                 bool endlastfoot;
721                 /// row for a newpage
722                 bool newpage;
723                 /// caption
724                 bool caption;
725         };
726         ///
727         typedef std::vector<RowData> row_vector;
728
729         ///
730         class ColumnData {
731                 public:
732                 ///
733                 ColumnData();
734                 ///
735                 LyXAlignment alignment;
736                 ///
737                 VAlignment valignment;
738                 ///
739                 int width;
740                 ///
741                 Length p_width;
742                 ///
743                 docstring align_special;
744                 ///
745                 docstring decimal_point;
746         };
747         ///
748         typedef std::vector<ColumnData> column_vector;
749
750         ///
751         idx_type numberofcells;
752         ///
753         std::vector<row_type> rowofcell;
754         ///
755         std::vector<col_type> columnofcell;
756         ///
757         row_vector row_info;
758         ///
759         column_vector column_info;
760         ///
761         cell_vvector cell_info;
762         ///
763         Length tabular_width;
764         ///
765         bool use_booktabs;
766         ///
767         int rotate;
768         ///
769         VAlignment tabular_valignment;
770         //
771         // for long tabulars
772         ///
773         HAlignment longtabular_alignment;
774         //
775         bool is_long_tabular;
776         /// endhead data
777         ltType endhead;
778         /// endfirsthead data
779         ltType endfirsthead;
780         /// endfoot data
781         ltType endfoot;
782         /// endlastfoot data
783         ltType endlastfoot;
784
785         ///
786         void init(Buffer *, row_type rows_arg,
787                   col_type columns_arg);
788         ///
789         void updateIndexes();
790         ///
791         bool setFixedWidth(row_type r, col_type c);
792         /// return true of update is needed
793         bool updateColumnWidths();
794         ///
795         idx_type columnSpan(idx_type cell) const;
796         ///
797         idx_type rowSpan(idx_type cell) const;
798         ///
799         BoxType useParbox(idx_type cell) const;
800         ///
801         // helper function for Latex
802         ///
803         void TeXTopHLine(otexstream &, row_type row, std::string const & lang) const;
804         ///
805         void TeXBottomHLine(otexstream &, row_type row, std::string const & lang) const;
806         ///
807         void TeXCellPreamble(otexstream &, idx_type cell, bool & ismulticol, bool & ismultirow) const;
808         ///
809         void TeXCellPostamble(otexstream &, idx_type cell, bool ismulticol, bool ismultirow) const;
810         ///
811         void TeXLongtableHeaderFooter(otexstream &, OutputParams const &) const;
812         ///
813         bool isValidRow(row_type const row) const;
814         ///
815         void TeXRow(otexstream &, row_type const row,
816                     OutputParams const &) const;
817         ///
818         // helper functions for plain text
819         ///
820         bool plaintextTopHLine(odocstringstream &, row_type row,
821                                std::vector<unsigned int> const &) const;
822         ///
823         bool plaintextBottomHLine(odocstringstream &, row_type row,
824                                   std::vector<unsigned int> const &) const;
825         ///
826         void plaintextPrintCell(odocstringstream &,
827                                 OutputParams const &,
828                                 idx_type cell, row_type row, col_type column,
829                                 std::vector<unsigned int> const &,
830                                 bool onlydata, size_t max_length) const;
831         /// auxiliary function for docbook
832         int docbookRow(odocstream & os, row_type, OutputParams const &) const;
833         ///
834         docstring xhtmlRow(XHTMLStream & xs, row_type, OutputParams const &,
835                            bool header = false) const;
836
837         /// change associated Buffer
838         void setBuffer(Buffer & buffer);
839         /// retrieve associated Buffer
840         Buffer const & buffer() const { return *buffer_; }
841         /// retrieve associated Buffer
842         Buffer & buffer() { return *buffer_; }
843
844 private:
845         Buffer * buffer_;
846
847 }; // Tabular
848
849
850 class InsetTabular : public Inset
851 {
852 public:
853         ///
854         InsetTabular(Buffer *, row_type rows = 1,
855                      col_type columns = 1);
856         ///
857         ~InsetTabular();
858         ///
859         void setBuffer(Buffer & buffer);
860
861         ///
862         static void string2params(std::string const &, InsetTabular &);
863         ///
864         static std::string params2string(InsetTabular const &);
865         ///
866         void read(Lexer &);
867         ///
868         void write(std::ostream &) const;
869         ///
870         void metrics(MetricsInfo &, Dimension &) const;
871         ///
872         void draw(PainterInfo & pi, int x, int y) const;
873         ///
874         void drawSelection(PainterInfo & pi, int x, int y) const;
875         ///
876         void drawBackground(PainterInfo & pi, int x, int y) const;
877         ///
878         bool editable() const { return true; }
879         ///
880         bool hasSettings() const { return true; }
881         ///
882         bool insetAllowed(InsetCode code) const;
883         ///
884         bool allowSpellCheck() const { return true; }
885         ///
886         bool canTrackChanges() const { return true; }
887         ///
888         bool canPaintChange(BufferView const &) const { return true; }
889         /** returns false if, when outputing LaTeX, font changes should
890             be closed before generating this inset. This is needed for
891             insets that may contain several paragraphs */
892         bool inheritFont() const { return false; }
893         ///
894         bool allowsCaptionVariation(std::string const &) const;
895         //
896         bool isTable() const { return true; }
897         ///
898         DisplayType display() const;
899         ///
900         void latex(otexstream &, OutputParams const &) const;
901         ///
902         int plaintext(odocstringstream & ods, OutputParams const & op,
903                       size_t max_length = INT_MAX) const;
904         ///
905         int docbook(odocstream &, OutputParams const &) const;
906         ///
907         docstring xhtml(XHTMLStream &, OutputParams const &) const;
908         ///
909         void validate(LaTeXFeatures & features) const;
910         ///
911         InsetCode lyxCode() const { return TABULAR_CODE; }
912         ///
913         std::string contextMenu(BufferView const &, int, int) const;
914         ///
915         std::string contextMenuName() const;
916         /// get offset of this cursor slice relative to our upper left corner
917         void cursorPos(BufferView const & bv, CursorSlice const & sl,
918                 bool boundary, int & x, int & y) const;
919         /// Executes a space-separated sequence of tabular-features requests
920         void tabularFeatures(Cursor & cur, std::string const & what);
921         /// Change a single tabular feature; does not handle undo.
922         void tabularFeatures(Cursor & cur, Tabular::Feature feature,
923                              std::string const & val = std::string());
924         /// number of cells
925         size_t nargs() const { return tabular.numberofcells; }
926         ///
927         std::shared_ptr<InsetTableCell const> cell(idx_type) const;
928         ///
929         std::shared_ptr<InsetTableCell> cell(idx_type);
930         ///
931         Text * getText(int) const;
932
933         /// set the change for the entire inset
934         void setChange(Change const & change);
935         /// accept the changes within the inset
936         void acceptChanges();
937         /// reject the changes within the inset
938         void rejectChanges();
939
940         // this should return true if we have a "normal" cell, otherwise false.
941         // "normal" means without width set!
942         /// should all paragraphs be output with "Standard" layout?
943         virtual bool allowParagraphCustomization(idx_type cell = 0) const;
944         ///
945         virtual bool forcePlainLayout(idx_type cell = 0) const;
946         ///
947         void addPreview(DocIterator const & inset_pos,
948                 graphics::PreviewLoader &) const;
949
950         /// lock cell with given index
951         void edit(Cursor & cur, bool front, EntryDirection entry_from);
952         /// get table row from x coordinate
953         int rowFromY(Cursor & cur, int y) const;
954         /// get table column from y coordinate
955         int columnFromX(Cursor & cur, int x) const;
956         ///
957         Inset * editXY(Cursor & cur, int x, int y);
958         /// can we go further down on mouse click?
959         bool descendable(BufferView const &) const { return true; }
960         /// Update the counters of this inset and of its contents
961         void updateBuffer(ParIterator const &, UpdateType);
962         ///
963         void addToToc(DocIterator const & di, bool output_active,
964                                   UpdateType utype, TocBackend & backend) const;
965
966         ///
967         bool completionSupported(Cursor const &) const;
968         ///
969         bool inlineCompletionSupported(Cursor const & cur) const;
970         ///
971         bool automaticInlineCompletion() const;
972         ///
973         bool automaticPopupCompletion() const;
974         ///
975         bool showCompletionCursor() const;
976         ///
977         CompletionList const * createCompletionList(Cursor const & cur) const;
978         ///
979         docstring completionPrefix(Cursor const & cur) const;
980         ///
981         bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
982         ///
983         void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
984         ///
985         virtual bool usePlainLayout() const { return true; }
986         ///
987         docstring layoutName() const { return from_ascii("Tabular"); }
988
989
990         ///
991         InsetTabular * asInsetTabular() { return this; }
992         ///
993         InsetTabular const * asInsetTabular() const { return this; }
994         ///
995         bool isRightToLeft(Cursor & cur) const;
996         /// writes the cells between stidx and enidx as a string, optionally
997         /// descending into the insets
998         docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
999
1000         /// Returns whether the cell in the specified row and column is selected.
1001         bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
1002         ///
1003         void setLayoutForHiddenCells(DocumentClass const & dc);
1004         //
1005         // Public structures and variables
1006         ///
1007         mutable Tabular tabular;
1008
1009 private:
1010         ///
1011         InsetTabular(InsetTabular const &);
1012         ///
1013         void doDispatch(Cursor & cur, FuncRequest & cmd);
1014         ///
1015         bool getFeatureStatus(Cursor & cur, std::string const & s,
1016                          std::string const & argument, FuncStatus & status) const;
1017         ///
1018         bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
1019         ///
1020         Inset * clone() const { return new InsetTabular(*this); }
1021
1022         ///
1023         bool hitSelectRow(BufferView const & bv, int x) const;
1024         ///
1025         bool hitSelectColumn(BufferView const & bv, int y) const;
1026         /// Returns true if coordinates are on row/column selection zones
1027         bool clickable(BufferView const &, int x, int y) const;
1028
1029         ///
1030         void drawCellLines(PainterInfo &, int x, int y, row_type row,
1031                            idx_type cell) const;
1032         ///
1033         void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
1034
1035         ///
1036         void moveNextCell(Cursor & cur,
1037                                 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
1038         ///
1039         void movePrevCell(Cursor & cur,
1040                                 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
1041         ///
1042         int cellXPos(idx_type cell) const;
1043         ///
1044         int cellYPos(idx_type cell) const;
1045         ///
1046         bool copySelection(Cursor & cur);
1047         ///
1048         bool pasteClipboard(Cursor & cur);
1049         ///
1050         void cutSelection(Cursor & cur);
1051         ///
1052         void getSelection(Cursor & cur, row_type & rs, row_type & re,
1053                           col_type & cs, col_type & ce) const;
1054         ///
1055         bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
1056
1057         /// return the "Manhattan distance" to nearest corner
1058         int dist(BufferView &, idx_type cell, int x, int y) const;
1059         /// return the cell nearest to x, y
1060         idx_type getNearestCell(BufferView &, int x, int y) const;
1061
1062         /// test the rotation state of the given cell range.
1063         bool oneCellHasRotationState(bool rotated,
1064                                 row_type row_start, row_type row_end,
1065                                 col_type col_start, col_type col_end) const;
1066
1067         /// true when selecting rows with the mouse
1068         bool rowselect_;
1069         /// true when selecting columns with the mouse
1070         bool colselect_;
1071 };
1072
1073 std::string const featureAsString(Tabular::Feature feature);
1074
1075 /// Split cell on decimal symbol
1076 InsetTableCell splitCell(InsetTableCell & head, docstring const & decimal_sym, bool & hassep);
1077
1078 } // namespace lyx
1079
1080 #endif // INSET_TABULAR_H