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