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