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