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