Shinya Kitaoka 810553
#pragma once
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TCOLUMNSET_INCLUDED
Toshihiro Shimizu 890ddd
#define TCOLUMNSET_INCLUDED
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tsmartpointer.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#undef DVAPI
Toshihiro Shimizu 890ddd
#undef DVVAR
Toshihiro Shimizu 890ddd
#ifdef TXSHEET_EXPORTS
Toshihiro Shimizu 890ddd
#define DVAPI DV_EXPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_EXPORT_VAR
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define DVAPI DV_IMPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_IMPORT_VAR
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DVAPI TColumnHeader : public TSmartObject {
Shinya Kitaoka 120a6e
  DECLARE_CLASS_CODE
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int m_index;  //!< The header's index in a columns set
Shinya Kitaoka 120a6e
  int m_pos;    //!< The header's screen X pos in a columns viewer
Shinya Kitaoka 120a6e
  int m_width;  //!< The header's width in a columns viewer
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  bool m_inColumnsSet;  //!< (TO BE REMOVED ASAP) Whether the header
Shinya Kitaoka 120a6e
                        //!< belongs to a columns set. Should be
Shinya Kitaoka 120a6e
                        //!< redirected to a negative m_index.
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TColumnHeader();
Shinya Kitaoka 120a6e
  virtual ~TColumnHeader() {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int getIndex() const { return m_index; }
Shinya Kitaoka 120a6e
  int getPos() const { return m_pos; }
Shinya Kitaoka 120a6e
  int getX0() const { return m_pos; }
Shinya Kitaoka 120a6e
  int getX1() const { return m_pos + m_width - 1; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void getCoords(int &x0, int &x1) const {
Shinya Kitaoka 120a6e
    x0 = m_pos;
Shinya Kitaoka 120a6e
    x1 = m_pos + m_width - 1;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int getWidth() const { return m_width; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  bool inColumnsSet() const { return m_inColumnsSet; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  template <class t=""></class>
Shinya Kitaoka 120a6e
  friend class TColumnSetT;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
template class DVAPI TSmartPointerT<tcolumnheader>;</tcolumnheader>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
typedef TSmartPointerT<tcolumnheader> TColumnHeaderP;</tcolumnheader>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class t=""></class>
Shinya Kitaoka 120a6e
class TColumnSetT {
Shinya Kitaoka 120a6e
  typedef TSmartPointerT<t> ColumnP;</t>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  std::vector<columnp> m_columns;</columnp>
Shinya Kitaoka 120a6e
  int m_defaultWidth;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  static bool compareColumnPos(const int pos, const ColumnP &ch2) {
Shinya Kitaoka 120a6e
    return pos <= ch2->getX1();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TColumnSetT(int defaultWidth = 100) : m_defaultWidth(defaultWidth) {}
Shinya Kitaoka 120a6e
  ~TColumnSetT() {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int getColumnCount() const { return m_columns.size(); }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void clear() { m_columns.clear(); }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  //---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void col2pos(int index, int &x0, int &x1) const {
Shinya Kitaoka 120a6e
    assert(index >= 0);
Shinya Kitaoka 120a6e
    int columnCount = (int)m_columns.size();
Shinya Kitaoka 120a6e
    if (index < columnCount)
Shinya Kitaoka 120a6e
      m_columns[index]->getCoords(x0, x1);
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      x0 = (columnCount > 0 ? m_columns.back()->getX1() + 1 : 0) +
Shinya Kitaoka 120a6e
           (index - columnCount) * m_defaultWidth;
Shinya Kitaoka 120a6e
      x1 = x0 + m_defaultWidth - 1;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  //---------------------------------------------------------
Shinya Kitaoka 120a6e
  // versione con upper_bound
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int pos2col(int pos) const {
Shinya Kitaoka 120a6e
    // n.b. endPos e' la coordinata del primo pixel non occupato da colonne
Shinya Kitaoka 120a6e
    int endPos = m_columns.empty() ? 0 : m_columns.back()->getX1() + 1;
Shinya Kitaoka 120a6e
    if (pos >= endPos)
Shinya Kitaoka 120a6e
      return m_columns.size() + (pos - endPos) / m_defaultWidth;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    typename std::vector<columnp>::const_iterator loc;</columnp>
Shinya Kitaoka 120a6e
    loc = std::upper_bound(m_columns.begin(), m_columns.end(), pos,
Shinya Kitaoka 120a6e
                           compareColumnPos);
Shinya Kitaoka 120a6e
    return std::distance(m_columns.begin(), loc);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  //---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  const ColumnP &getColumn(int index) const {
Shinya Kitaoka 120a6e
    static const ColumnP empty;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    if (index >= 0 && index < (int)m_columns.size()) return m_columns[index];
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    return empty;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  //---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  const ColumnP &touchColumn(int index, int type = 0) {
Shinya Kitaoka 120a6e
    assert(index >= 0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    const int count = m_columns.size();
Shinya Kitaoka 120a6e
    if (index < count) return m_columns[index];
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    for (int i = count; i <= index; ++i) {
Shinya Kitaoka 120a6e
      int cType   = (i != index) ? 0 : type;
Shinya Kitaoka 120a6e
      ColumnP col = T::createEmpty(cType);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      m_columns.push_back(col);
Shinya Kitaoka 120a6e
    }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    update(count);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    assert(index == (int)m_columns.size() - 1);
Shinya Kitaoka 120a6e
    return m_columns[index];
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  //---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  const ColumnP &insertColumn(int index, const ColumnP &column) {
Shinya Kitaoka 120a6e
    // assert(column && column->m_index < 0);
Shinya Kitaoka 120a6e
    assert(column && !column->m_inColumnsSet);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    if (index > 0) touchColumn(index - 1);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    m_columns.insert(m_columns.begin() + index, column);
Shinya Kitaoka 120a6e
    update(index);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    return column;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  //---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  const ColumnP removeColumn(int index) {
Shinya Kitaoka 120a6e
    assert(index >= 0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    int columnCount = m_columns.size();
Shinya Kitaoka 120a6e
    if (index >= columnCount)  // Shouldn't be asserted instead ?
Shinya Kitaoka 120a6e
      return ColumnP();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    ColumnP column = m_columns[index];
Shinya Kitaoka 120a6e
    // column->m_index = -1;                           // We should enforce
Shinya Kitaoka 120a6e
    // this. Unfortunately, must be tested extensively.
Shinya Kitaoka 120a6e
    column->m_inColumnsSet = false;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    m_columns.erase(m_columns.begin() + index);
Shinya Kitaoka 120a6e
    update(index);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    return column;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  //---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void rollLeft(int index, int count) {
Shinya Kitaoka 120a6e
    assert(index >= 0);
Shinya Kitaoka 120a6e
    assert(count > 1);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    int columnCount                        = m_columns.size();
Shinya Kitaoka 120a6e
    if (index + count > columnCount) count = columnCount - index;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    if (count < 2) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    assert(0 <= index && index + count - 1 < columnCount);
Shinya Kitaoka 120a6e
    assert(count > 0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    int i = index, j = index + count - 1;
Shinya Kitaoka 120a6e
    ColumnP tmp = m_columns[i];
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    for (int k = i; k < j; ++k) m_columns[k] = m_columns[k + 1];
Shinya Kitaoka 120a6e
    m_columns[j]                             = tmp;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    update(0);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  //---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void rollRight(int index, int count) {
Shinya Kitaoka 120a6e
    assert(index >= 0);
Shinya Kitaoka 120a6e
    assert(count > 1);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    int columnCount                        = m_columns.size();
Shinya Kitaoka 120a6e
    if (index + count > columnCount) count = columnCount - index;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    if (count < 2) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    assert(0 <= index && index + count - 1 < columnCount);
Shinya Kitaoka 120a6e
    assert(count > 0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    int i = index, j = index + count - 1;
Shinya Kitaoka 120a6e
    ColumnP tmp = m_columns[j];
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    for (int k = j; k > i; --k) m_columns[k] = m_columns[k - 1];
Shinya Kitaoka 120a6e
    m_columns[i]                             = tmp;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    update(0);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  //---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void update(int fromIdx) {
Shinya Kitaoka 120a6e
    int pos = 0, index = 0;
Shinya Kitaoka 120a6e
    if (fromIdx > 0) {
Shinya Kitaoka 120a6e
      assert(fromIdx <= (int)m_columns.size());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      const ColumnP &left = m_columns[fromIdx - 1];
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      pos   = left->getX1() + 1;
Shinya Kitaoka 120a6e
      index = left->m_index + 1;
Shinya Kitaoka 120a6e
    }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    int c, cCount = m_columns.size();
Shinya Kitaoka 120a6e
    for (c = fromIdx; c != cCount; ++c) {
Shinya Kitaoka 120a6e
      const ColumnP &col = m_columns[c];
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      col->m_pos   = pos;
Shinya Kitaoka 120a6e
      col->m_index = index++;
Shinya Kitaoka 120a6e
      pos += col->m_width;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      col->m_inColumnsSet = true;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  // Not copyable
Shinya Kitaoka 120a6e
  TColumnSetT(const TColumnSetT &);
Shinya Kitaoka 120a6e
  TColumnSetT &operator=(const TColumnSetT &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#endif  // TCOLUMNSET_INCLUDED