]> 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 4c586542e227f42857890e3694f96c32277cf4ac..a46c0a2c0c9b78d616ed8916436321aa710c4384 100644 (file)
@@ -518,6 +518,7 @@ Tabular::CellData::CellData(Buffer * buf)
          multirow(Tabular::CELL_NORMAL),
          alignment(LYX_ALIGN_CENTER),
          valignment(LYX_VALIGN_TOP),
+         voffset(0),
          top_line(false),
          bottom_line(false),
          left_line(false),
@@ -1221,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;
 }
 
 
@@ -1544,6 +1532,13 @@ Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number)
        CellData & cs = cellInfo(cell);
        cs.multirow = CELL_BEGIN_OF_MULTIROW;
        cs.valignment = LYX_VALIGN_MIDDLE;
+       // FIXME: the horizontal alignment of multirow cells can only
+       // be changed for the whole table,
+       // support for this needs to be implemented but us a fileformat
+       // change (assigning this to uwestoehr)
+       // until LyX supports this, use the deault alignment of multirow
+       // cells: left
+       cs.alignment = LYX_ALIGN_LEFT; 
 
        // set the bottom row of the last selected cell
        setBottomLine(cell, bottomLine(cell + (number - 1)*ncols()));
@@ -1601,6 +1596,7 @@ void Tabular::unsetMultiRow(idx_type cell)
                return;
 
        cellInfo(cell).valignment = LYX_VALIGN_TOP;
+       cellInfo(cell).alignment = LYX_ALIGN_CENTER;
        row_type const row = cellRow(cell);
        col_type const col = cellColumn(cell);
        row_type const span = rowSpan(cell);
@@ -3227,8 +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);
+
+                       // 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 :
@@ -3386,49 +3400,43 @@ void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
 
 
 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
-                                row_type row, idx_type cell, Change const & change) const
+                                                                row_type row, idx_type cell, Change const & change) const
 {
-       y = y - tabular.rowAscent(row);
+       y -= tabular.rowAscent(row);
        int const w = tabular.columnWidth(cell);
        int const h = tabular.cellHeight(cell);
-       bool on_off = false;
-       Color col = Color_tabularline;
-       Color onoffcol = Color_tabularonoffline;
-
-       if (change.changed()) {
-               col = change.color();
-               onoffcol = change.color();
-       }
-
-       bool topalreadydrawn = row > 0 && !tabular.rowTopLine(row) 
-               && tabular.bottomLine(tabular.cellAbove(cell));
-
-       if (!topalreadydrawn) {
-               on_off = !tabular.topLine(cell);
-               pain.line(x, y, x + w, y,
-                       on_off ? onoffcol : col,
-                       on_off ? Painter::line_onoffdash : Painter::line_solid);
-       }
-       on_off = !tabular.bottomLine(cell);
+       Color linecolor = change.changed() ? change.color() : Color_tabularline;
+       Color gridcolor = change.changed() ? change.color() : Color_tabularonoffline;
+
+       // Top
+       bool drawline = tabular.topLine(cell)
+               || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
+       pain.line(x, y, x + w, y,
+               drawline ? linecolor : gridcolor,
+               drawline ? Painter::line_solid : Painter::line_onoffdash);
+
+       // Bottom
+       drawline = tabular.bottomLine(cell);
        pain.line(x, y + h, x + w, y + h,
-               on_off ? onoffcol : col,
-               on_off ? Painter::line_onoffdash : Painter::line_solid);
-
-       col_type const column = tabular.cellColumn(cell);
-       bool leftalreadydrawn = column > 0  && !tabular.leftLine(cell)
-               && tabular.rightLine(tabular.cellIndex(row, column - 1));
-
-       if (!leftalreadydrawn) {
-               on_off = !tabular.leftLine(cell);
-               pain.line(x, y, x, y + h,
-                       on_off ? onoffcol : col,
-                       on_off ? Painter::line_onoffdash : Painter::line_solid);
-       }
-       on_off = !tabular.rightLine(cell);
-       pain.line(x + w - tabular.interColumnSpace(cell), y,
-               x + w - tabular.interColumnSpace(cell), y + h,
-               on_off ? onoffcol : col,
-               on_off ? Painter::line_onoffdash : Painter::line_solid);
+               drawline ? linecolor : gridcolor,
+               drawline ? Painter::line_solid : Painter::line_onoffdash);
+
+       // Left
+       col_type const col = tabular.cellColumn(cell);
+       drawline = tabular.leftLine(cell)
+               || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
+       pain.line(x, y, x, y + h,
+               drawline ? linecolor : gridcolor,
+               drawline ? Painter::line_solid : Painter::line_onoffdash);
+
+       // Right
+       x -= tabular.interColumnSpace(cell);
+       drawline = tabular.rightLine(cell)
+                  || (col + 1 < tabular.ncols()
+                      && tabular.leftLine(tabular.cellIndex(row, col + 1)));
+       pain.line(x + w, y, x + w, y + h,
+               drawline ? linecolor : gridcolor,
+               drawline ? Painter::line_solid : Painter::line_onoffdash);
 }