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