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