]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetTabular.cpp
fix the drawing for cells with a specified vertical alignment
[lyx.git] / src / insets / InsetTabular.cpp
index 3a9d73b1768336e7787f4e62187441da56827c33..a46c0a2c0c9b78d616ed8916436321aa710c4384 100644 (file)
@@ -514,13 +514,11 @@ string const featureAsString(Tabular::Feature action)
 Tabular::CellData::CellData(Buffer * buf)
        : cellno(0),
          width(0),
-         height(0),
-         ascent(0),
-         descent(0),
          multicolumn(Tabular::CELL_NORMAL),
          multirow(Tabular::CELL_NORMAL),
          alignment(LYX_ALIGN_CENTER),
          valignment(LYX_VALIGN_TOP),
+         voffset(0),
          top_line(false),
          bottom_line(false),
          left_line(false),
@@ -589,9 +587,7 @@ Tabular::RowData::RowData()
          endfoot(false),
          endlastfoot(false),
          newpage(false),
-         caption(false),
-         valignment(LYX_VALIGN_TOP),
-         maxheight(0)
+         caption(false)
 {}
 
 
@@ -1226,20 +1222,7 @@ int Tabular::textHOffset(idx_type cell) const
 
 int Tabular::textVOffset(idx_type cell) const
 {
-       row_type const r = cellRow(cell);
-       int y = cellHeight(cell) - rowDescent(r) - rowAscent(r);
-       switch (getVAlignment(cell)) {
-          case LYX_VALIGN_TOP:
-                  y = 0;
-                  break;
-          case LYX_VALIGN_MIDDLE:
-                  y = y/2;
-                  break;
-          case LYX_VALIGN_BOTTOM:
-                  break;
-       }
-
-       return y;
+       return cellInfo(cell).voffset;
 }
 
 
@@ -3240,14 +3223,26 @@ void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
                        if (!p_width.zero())
                                dim.wid = m.base.textwidth;
                        tabular.setCellWidth(cell, dim.wid);
-                       maxAsc  = max(maxAsc, dim.asc);
-                       maxDesc = max(maxDesc, dim.des);
-
-                       // store the height for every cell
-                       // this is later needed in InsetTabular::draw to determine the valignment
-                       tabular.cell_info[r][c].height = dim.asc + dim.des;
-                       tabular.cell_info[r][c].ascent = dim.asc;
-                       tabular.cell_info[r][c].descent = dim.des;
+
+                       // FIXME?: do we need a second metrics call
+                       // to get the descent of the text in last par?
+                       TextMetrics const & tm = 
+                               mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
+                       int const backdes = tm.last().second->descent();
+
+                       switch (tabular.getVAlignment(cell)) { 
+                               case Tabular::LYX_VALIGN_TOP:
+                                       tabular.cell_info[r][c].voffset = 0; 
+                                       break; 
+                               case Tabular::LYX_VALIGN_MIDDLE:
+                                       tabular.cell_info[r][c].voffset = -(dim.des - backdes - TEXT_TO_INSET_OFFSET)/2; 
+                                       break; 
+                               case Tabular::LYX_VALIGN_BOTTOM:
+                                       tabular.cell_info[r][c].voffset = -(dim.des - backdes - TEXT_TO_INSET_OFFSET);
+                                       break;
+                       }
+                       maxAsc  = max(maxAsc, dim.asc - tabular.cell_info[r][c].voffset);
+                       maxDesc = max(maxDesc, dim.des + tabular.cell_info[r][c].voffset);
                }
                int const top_space = tabular.row_info[r].top_space_default ?
                        default_line_space :
@@ -3264,7 +3259,6 @@ void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
        dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
 }
 
-
 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col) 
        const
 {
@@ -3311,31 +3305,7 @@ void InsetTabular::draw(PainterInfo & pi, int x, int y) const
        bool const original_selection_state = pi.selected;
 
        idx_type idx = 0;
-       int cy = 0;
        first_visible_cell = Tabular::npos;
-
-       // determine the highest cell because its valignment sets the row valignment
-       // also store its height
-       for (row_type r = 0; r < tabular.nrows(); ++r) {
-               for (col_type c = 0; c < tabular.ncols(); ++c) {
-                       if (tabular.cell_info[r][c].height >= tabular.row_info[r].maxheight) {
-                               tabular.row_info[r].maxheight = tabular.cell_info[r][c].height;
-                               switch (tabular.getVAlignment(tabular.cellIndex(r, c))) {
-                                       case Tabular::LYX_VALIGN_TOP:
-                                               tabular.row_info[r].valignment = Tabular::LYX_VALIGN_TOP;
-                                               break;
-                                       case Tabular::LYX_VALIGN_MIDDLE:
-                                               tabular.row_info[r].valignment = Tabular::LYX_VALIGN_MIDDLE;
-                                               break;
-                                       case Tabular::LYX_VALIGN_BOTTOM:
-                                               tabular.row_info[r].valignment = Tabular::LYX_VALIGN_BOTTOM;
-                                               break;
-                               }
-                       }
-               }
-       }
-
-       // step over all cells
        for (row_type r = 0; r < tabular.nrows(); ++r) {
                int nx = x;
                for (col_type c = 0; c < tabular.ncols(); ++c) {
@@ -3353,75 +3323,8 @@ void InsetTabular::draw(PainterInfo & pi, int x, int y) const
                                first_visible_cell = idx;
 
                        pi.selected |= isCellSelected(cur, r, c);
-
-                       // set the position for the vertical alignment
-                       // if the cell is not the highest, align it according to the row valignment
-                       // and according to its own valignment
-                       // Note: The fractions of the scent values were found by testing
-                       // there is no bettwer way for the calculation because of LyX's definition
-                       // of ascent and descent. The correct way would be to calculate based
-                       // on the text height, the line space and paragraph separation.
-                       cy = y;
-                       if (tabular.cell_info[r][c].height < tabular.row_info[r].maxheight) {
-                               switch (tabular.row_info[r].valignment) {
-                                       case Tabular::LYX_VALIGN_TOP:
-                                               switch (tabular.getVAlignment(idx)) {
-                                                       case Tabular::LYX_VALIGN_TOP:
-                                                               break;
-                                                       case Tabular::LYX_VALIGN_MIDDLE:
-                                                               cy += - tabular.cell_info[r][c].descent / 2
-                                                                       + 0.3 * tabular.cell_info[r][c].ascent;
-                                                               // FIXME the row also needs to be increased at the upper border
-                                                               break;
-                                                       case Tabular::LYX_VALIGN_BOTTOM:
-                                                               cy += - tabular.cell_info[r][c].descent
-                                                                       + 0.6 * tabular.cell_info[r][c].ascent;
-                                                               // FIXME the row also needs to be increased at the upper border
-                                                               break;
-                                               }
-                                               break;
-                                       case Tabular::LYX_VALIGN_MIDDLE:
-                                               switch (tabular.getVAlignment(idx)) {
-                                                       case Tabular::LYX_VALIGN_TOP:
-                                                               cy += tabular.row_info[r].maxheight / 2
-                                                                       - 0.75 * tabular.cell_info[r][c].ascent;
-                                                               break;
-                                                       case Tabular::LYX_VALIGN_MIDDLE:
-                                                               cy += tabular.row_info[r].maxheight / 2
-                                                                       - tabular.cell_info[r][c].descent / 2
-                                                                       - 0.5 * tabular.cell_info[r][c].ascent;
-                                                               break;
-                                                       case Tabular::LYX_VALIGN_BOTTOM:
-                                                               cy += tabular.row_info[r].maxheight / 2
-                                                                       - tabular.cell_info[r][c].descent
-                                                                       - 0.2 * tabular.cell_info[r][c].ascent;
-                                                               break;
-                                               }
-                                               break;
-                                       case Tabular::LYX_VALIGN_BOTTOM:
-                                               switch (tabular.getVAlignment(idx)) {
-                                                       case Tabular::LYX_VALIGN_TOP:
-                                                               cy += tabular.row_info[r].maxheight
-                                                                       - 1.5 * tabular.cell_info[r][c].ascent;
-                                                               break;
-                                                       case Tabular::LYX_VALIGN_MIDDLE:
-                                                               cy += tabular.row_info[r].maxheight
-                                                                       - tabular.cell_info[r][c].descent / 2
-                                                                       - 1.2 * tabular.cell_info[r][c].ascent;
-                                                               // FIXME the row also needs to be increased at the lower border
-                                                               break;
-                                                       case Tabular::LYX_VALIGN_BOTTOM:
-                                                               cy += tabular.row_info[r].maxheight
-                                                                       - tabular.cell_info[r][c].descent
-                                                                       - 0.9 * tabular.cell_info[r][c].ascent;
-                                                               // FIXME the row also needs to be increased at the lower border
-                                                               break;
-                                               }
-                                               break;
-                               }
-                       }
-
                        int const cx = nx + tabular.textHOffset(idx);
+                       int const cy = y  + tabular.textVOffset(idx);
                        // Cache the Inset position.
                        bv->coordCache().insets().add(cell(idx).get(), cx, cy);
                        cell(idx)->draw(pi, cx, cy);