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