Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "thumbnailviewer.h"
Toshihiro Shimizu 890ddd
#include "thumbnail.h"
Toshihiro Shimizu 890ddd
#include "tfilepath.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tw/textfield.h"
Toshihiro Shimizu 890ddd
#include "tw/colors.h"
Toshihiro Shimizu 890ddd
#include "tw/event.h"
Toshihiro Shimizu 890ddd
#include "tw/dragdrop.h"
Toshihiro Shimizu 890ddd
#include "tw/keycodes.h"
Toshihiro Shimizu 890ddd
#include "tw/mainshell.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tw/message.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace TwConsts;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class ThumbnailViewer::NameField : public TTextField {
Shinya Kitaoka 120a6e
  Thumbnail *m_thumbnail;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  NameField(TWidget *parent) : TTextField(parent), m_thumbnail(0) {}
Shinya Kitaoka 120a6e
  void onFocusChange(bool on) {
Shinya Kitaoka 120a6e
    if (!on) close();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void close() {
Shinya Kitaoka 120a6e
    if (m_thumbnail) {
Shinya Kitaoka 120a6e
      try {
Shinya Kitaoka 120a6e
        m_thumbnail->setName(toString(getText()));
Shinya Kitaoka 120a6e
      } catch (...) {
Shinya Kitaoka 120a6e
        TMessage::error("unable to change name");
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    hide();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void keyDown(int key, unsigned long flags, const TPoint &p) {
Shinya Kitaoka 120a6e
    if (key == TK_Return)
Shinya Kitaoka 120a6e
      close();
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      TTextField::keyDown(key, flags, p);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void setThumbnail(Thumbnail *thumbnail) {
Shinya Kitaoka 120a6e
    m_thumbnail = thumbnail;
Shinya Kitaoka 120a6e
    setText(m_thumbnail->getName());
Shinya Kitaoka 120a6e
    selectAll();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ThumbnailViewer::ThumbnailViewer(TWidget *parent, string name)
Shinya Kitaoka 120a6e
    : TScrollView(parent, name)
Shinya Kitaoka 120a6e
    , m_selectedItemIndex(-1)
Shinya Kitaoka 120a6e
    , m_itemSize(94, 100)
Shinya Kitaoka 120a6e
    , m_itemSpace(10, 10)
Shinya Kitaoka 120a6e
    , m_margins(10, 10)
Shinya Kitaoka 120a6e
    , m_playButtonBox(TPoint(85, 2), TDimension(10, 10))
Shinya Kitaoka 120a6e
    , m_iconBox(TPoint(2 + 4 - 3, 20 + 4), TDimension(96 - 8, 78 - 8))
Shinya Kitaoka 120a6e
    , m_textBox(TPoint(2 + 4 + 2 + 10, 2 + 2), TDimension(65, 18))
Shinya Kitaoka 120a6e
    , m_flag(false)
Shinya Kitaoka 120a6e
    , m_playing(false)
Shinya Kitaoka 120a6e
    , m_loading(false)
Shinya Kitaoka 120a6e
    , m_timerActive(false)
Shinya Kitaoka 120a6e
    , m_dragDropArmed(false)
Shinya Kitaoka 120a6e
    , m_oldPos(0, 0) {
Shinya Kitaoka 120a6e
  setBackgroundColor(White);
Shinya Kitaoka 120a6e
  m_nameField = new NameField(this);
Shinya Kitaoka 120a6e
  m_nameField->hide();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ThumbnailViewer::~ThumbnailViewer() { clearPointerContainer(m_items); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int ThumbnailViewer::getColumnCount() const {
Shinya Kitaoka 120a6e
  int columnWidth    = m_itemSize.lx + m_itemSpace.lx;
Shinya Kitaoka 120a6e
  int availableWidth = getLx() - m_margins.lx * 2 + m_itemSpace.lx;
Shinya Kitaoka 120a6e
  return tmax(1, (availableWidth) / columnWidth);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TPoint ThumbnailViewer::getItemPos(int index) {
Shinya Kitaoka 120a6e
  int columnCount = getColumnCount();
Shinya Kitaoka 120a6e
  int row         = index / columnCount;
Shinya Kitaoka 120a6e
  int col         = index % columnCount;
Shinya Kitaoka 120a6e
  int x           = m_margins.lx + col * (m_itemSize.lx + m_itemSpace.lx);
Shinya Kitaoka 120a6e
  int y           = getLy() - m_margins.ly - m_itemSize.ly -
Shinya Kitaoka 120a6e
          row * (m_itemSize.ly + m_itemSpace.ly);
Shinya Kitaoka 120a6e
  return TPoint(x, y);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TRect ThumbnailViewer::getItemBounds(int index) {
Shinya Kitaoka 120a6e
  return TRect(getItemPos(index), m_itemSize);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int ThumbnailViewer::findItem(const TPoint &pos) {
Shinya Kitaoka 120a6e
  int columnCount = getColumnCount();
Shinya Kitaoka 120a6e
  int col         = (pos.x - m_margins.lx) / (m_itemSize.lx + m_itemSpace.lx);
Shinya Kitaoka 120a6e
  int row         = (getLy() - m_margins.ly - m_itemSpace.ly - pos.y) /
Shinya Kitaoka 120a6e
            (m_itemSize.ly + m_itemSpace.ly);
Shinya Kitaoka 120a6e
  int index = row * columnCount + col;
Shinya Kitaoka 120a6e
  if (0 <= index && index < (int)m_items.size() &&
Shinya Kitaoka 120a6e
      getItemBounds(index).contains(pos))
Shinya Kitaoka 120a6e
    return index;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return -1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int ThumbnailViewer::getItemCount() const { return m_items.size(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
Thumbnail *ThumbnailViewer::getItem(int index) const {
Shinya Kitaoka 120a6e
  assert(0 <= index && index < (int)m_items.size());
Shinya Kitaoka 120a6e
  return m_items[index];
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ThumbnailViewer::configureNotify(const TDimension &d) {
Shinya Kitaoka 120a6e
  TScrollView::configureNotify(d);
Shinya Kitaoka 120a6e
  // m_nameField->setGeometry(TPoint(10,10), TDimension(100,30));
Shinya Kitaoka 120a6e
  updateContentSize();
Shinya Kitaoka 120a6e
  invalidate();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ThumbnailViewer::drawFrame(int index) {
Shinya Kitaoka 120a6e
  TPoint base = getItemPos(index);
Shinya Kitaoka 120a6e
  assert(0 <= index && index < (int)m_items.size());
Shinya Kitaoka 120a6e
  assert(m_items[index]->getRaster());
Shinya Kitaoka 120a6e
  assert(m_iconBox.getSize() == m_items[index]->getRaster()->getSize());
Shinya Kitaoka 120a6e
  rectwrite(m_items[index]->getRaster(), base + m_iconBox.getP00());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ThumbnailViewer::drawItem(int index) {
Shinya Kitaoka 120a6e
  TPoint base = getItemPos(index);
Shinya Kitaoka 120a6e
  if (index < 0 || index >= (int)m_items.size()) {
Shinya Kitaoka 120a6e
    setColor(getBackgroundColor());
Shinya Kitaoka 120a6e
    fillRect(TRect(base, m_itemSize));
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // setColor(Black);
Shinya Kitaoka 120a6e
  // fillRect(TRect(base, m_itemSize));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Thumbnail *item = m_items[index];
Shinya Kitaoka 120a6e
  bool selected   = item != 0 && index == m_selectedItemIndex;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRect r0(base, m_itemSize);
Shinya Kitaoka 120a6e
  TRect r1 = m_iconBox + base;
Shinya Kitaoka 120a6e
  TRect r2 = m_textBox + base;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  setColor(selected ? Cyan : getBackgroundColor());
Shinya Kitaoka 120a6e
  fillRect(r0.x0, r1.y0, r1.x0 - 1, r1.y1);  // sinistra
Shinya Kitaoka 120a6e
  fillRect(r1.x1 + 1, r1.y0, r0.x1, r1.y1);  // destra
Shinya Kitaoka 120a6e
  fillRect(r0.x0, r0.y0, r0.x1, r1.y0 - 1);  // sotto
Shinya Kitaoka 120a6e
  // fillRect(r1.x0,r1.y1+1,r1.x1,r0.y1); // sopra
Shinya Kitaoka 120a6e
  TRect topLine(r0.x0, r1.y1 + 1, r0.x1, r0.y1);
Shinya Kitaoka 120a6e
  TRect sceneLine = topLine;
Shinya Kitaoka 120a6e
  sceneLine.y0    = sceneLine.y1 - 3;
Shinya Kitaoka 120a6e
  topLine.y1      = sceneLine.y0 - 1;
Shinya Kitaoka 120a6e
  fillRect(topLine);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (item->getType() == Thumbnail::SCENE) {
Shinya Kitaoka 120a6e
    setColor(Black);
Shinya Kitaoka 120a6e
    drawRect(sceneLine);
Shinya Kitaoka 120a6e
    setColor(White);
Shinya Kitaoka 120a6e
    fillRect(sceneLine.enlarge(-1));
Shinya Kitaoka 120a6e
    int x;
Shinya Kitaoka 120a6e
    setColor(Black);
Shinya Kitaoka 120a6e
    for (x = sceneLine.x0 + 5; x + 5 < sceneLine.x1; x += 10) {
Shinya Kitaoka 120a6e
      int y = sceneLine.y0 + 1;
Shinya Kitaoka 120a6e
      drawLine(x, y, x + 4, y);
Shinya Kitaoka 120a6e
      y++;
Shinya Kitaoka 120a6e
      x++;
Shinya Kitaoka 120a6e
      drawLine(x, y, x + 4, y);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    setColor(White);
Shinya Kitaoka 120a6e
    fillRect(sceneLine);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  string name   = item->getName();
Shinya Kitaoka 120a6e
  int maxWidth  = r2.getLx() - 4;
Shinya Kitaoka 120a6e
  TDimension ts = getTextSize(name);
Shinya Kitaoka 120a6e
  if (ts.lx > maxWidth) {
Shinya Kitaoka 120a6e
    do {
Shinya Kitaoka 120a6e
      name.pop_back();
Shinya Kitaoka 120a6e
      ts = getTextSize(name);
Shinya Kitaoka 120a6e
    } while (name.size() > 1 && ts.lx > maxWidth);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TPoint textOrigin = r2.getP00() + TPoint(2 - 10, (r2.getLy() - ts.ly) / 2);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  setColor(Black);
Shinya Kitaoka 120a6e
  string fname          = name;
Shinya Kitaoka 120a6e
  string type           = item->getPath().getType();
Shinya Kitaoka 120a6e
  if (type != "") fname = fname + "." + type;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  fname = item->getPath().getLevelName();
Shinya Kitaoka 120a6e
  drawText(textOrigin, fname);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (selected) {
Shinya Kitaoka 120a6e
    TRect r = m_playButtonBox + base;
Shinya Kitaoka 120a6e
    setColor(Blue);
Shinya Kitaoka 120a6e
    if (m_playing) {
Shinya Kitaoka 120a6e
      int x0 = r.x0, x3 = r.x1;
Shinya Kitaoka 120a6e
      int x1 = (x0 + x3) / 2 - 1;
Shinya Kitaoka 120a6e
      int x2 = x3 - (x1 - x0);
Shinya Kitaoka 120a6e
      drawRect(x0, r.y0, x1, r.y1);
Shinya Kitaoka 120a6e
      drawRect(x2, r.y0, x3, r.y1);
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      vector<tpoint> pts;</tpoint>
Shinya Kitaoka 120a6e
      int d = (r.y1 - r.y0 + 1) / 2;
Shinya Kitaoka 120a6e
      int x = r.x0;
Shinya Kitaoka 120a6e
      int y = r.y0;
Shinya Kitaoka 120a6e
      pts.push_back(TPoint(x, y));
Shinya Kitaoka 120a6e
      pts.push_back(TPoint(x + d, y + d));
Shinya Kitaoka 120a6e
      pts.push_back(TPoint(x, y + d * 2));
Shinya Kitaoka 120a6e
      fillPolygon(Cyan, Blue, &pts[0], 3);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  assert(0 <= index && index < (int)m_items.size());
Shinya Kitaoka 120a6e
  assert(m_items[index]->getRaster());
Shinya Kitaoka 120a6e
  assert(m_iconBox.getSize() == m_items[index]->getRaster()->getSize());
Shinya Kitaoka 120a6e
  rectwrite(m_items[index]->getRaster(), base + m_iconBox.getP00());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ThumbnailViewer::drawBackground() {
Shinya Kitaoka 120a6e
  int itemCount   = m_items.size();
Shinya Kitaoka 120a6e
  int columnCount = getColumnCount();
Shinya Kitaoka 120a6e
  int rowCount    = (itemCount + columnCount - 1) / columnCount;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRect clipRect;
Shinya Kitaoka 120a6e
  getClipRect(clipRect);  // coordinate window
Shinya Kitaoka 120a6e
  clipRect -= TPoint(m_xoff, m_yoff);
Shinya Kitaoka 120a6e
  TRect rect;
Shinya Kitaoka 120a6e
  rect.x0 = m_margins.lx;
Shinya Kitaoka 120a6e
  rect.x1 = m_margins.lx + (m_itemSize.lx + m_itemSpace.lx) * columnCount -
Shinya Kitaoka 120a6e
            m_itemSpace.lx - 1;
Shinya Kitaoka 120a6e
  rect.y1 = getLy() - m_margins.ly - 1;
Shinya Kitaoka 120a6e
  rect.y0 = rect.y1 - (m_itemSize.ly + m_itemSpace.ly) * rowCount +
Shinya Kitaoka 120a6e
            m_itemSpace.ly + 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  setColor(getBackgroundColor());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // alto
Shinya Kitaoka 120a6e
  if (rect.y1 + 1 <= clipRect.y1)
Shinya Kitaoka 120a6e
    fillRect(clipRect.x0, rect.y1 + 1, clipRect.x1, clipRect.y1);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // basso
Shinya Kitaoka 120a6e
  if (clipRect.y0 <= rect.y0 - 1)
Shinya Kitaoka 120a6e
    fillRect(clipRect.x0, clipRect.y0, clipRect.x1, rect.y0 - 1);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int y0 = tmax(clipRect.y0, rect.y0);
Shinya Kitaoka 120a6e
  int y1 = tmin(clipRect.y1, rect.y1);
Shinya Kitaoka 120a6e
  if (y0 <= y1) {
Shinya Kitaoka 120a6e
    // sinistra
Shinya Kitaoka 120a6e
    if (clipRect.x0 <= rect.x0 - 1) fillRect(clipRect.x0, y0, rect.x0 - 1, y1);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // destra
Shinya Kitaoka 120a6e
    if (rect.x1 + 1 <= clipRect.x1) fillRect(rect.x1 + 1, y0, clipRect.x1, y1);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // interlinee verticali
Shinya Kitaoka 120a6e
  for (int r = 0; r < rowCount; r++) {
Shinya Kitaoka 120a6e
    int y1 = rect.y1 - (m_itemSize.ly + m_itemSpace.ly) * r + m_itemSpace.ly;
Shinya Kitaoka 120a6e
    int y0 = y1 - m_itemSpace.ly + 1;
Shinya Kitaoka 120a6e
    fillRect(clipRect.x0, y0, clipRect.x1, y1);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  for (int c = 0; c < columnCount - 1; c++) {
Shinya Kitaoka 120a6e
    int x0 =
Shinya Kitaoka 120a6e
        m_margins.lx + (m_itemSize.lx + m_itemSpace.lx) * c + m_itemSize.lx;
Shinya Kitaoka 120a6e
    int x1 = x0 + m_itemSpace.lx - 1;
Shinya Kitaoka 120a6e
    fillRect(x0, clipRect.y0, x1, clipRect.y1);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // ultima riga
Shinya Kitaoka 120a6e
  c = itemCount % columnCount;
Shinya Kitaoka 120a6e
  if (c != 0) {
Shinya Kitaoka 120a6e
    TRect box = getItemBounds(itemCount - 1);
Shinya Kitaoka 120a6e
    if (box.y0 <= clipRect.y1 && box.y1 >= clipRect.y0 &&
Shinya Kitaoka 120a6e
        box.x1 + 1 <= clipRect.x1)
Shinya Kitaoka 120a6e
      fillRect(box.x1 + 1, box.y0, clipRect.x1, box.y1);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ThumbnailViewer::repaint() {
Shinya Kitaoka 120a6e
  if (m_flag) {
Shinya Kitaoka 120a6e
    if (m_selectedItemIndex) drawFrame(m_selectedItemIndex);
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    // m_loadingDirectory = false;
Shinya Kitaoka 120a6e
    // setColor(Black);
Shinya Kitaoka 120a6e
    // fillRect(getBounds());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    drawBackground();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TRect clipRect;
Shinya Kitaoka 120a6e
    getClipRect(clipRect);  // coordinate window
Shinya Kitaoka 120a6e
    clipRect -= TPoint(m_xoff, m_yoff);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    bool loading = false;
Shinya Kitaoka 120a6e
    for (int i = 0; i < (int)m_items.size(); i++) {
Shinya Kitaoka 120a6e
      TRect bounds = getItemBounds(i);
Shinya Kitaoka 120a6e
      if (bounds.overlaps(clipRect)) {
Shinya Kitaoka 120a6e
        drawItem(i);
Shinya Kitaoka 120a6e
        if (m_items[i]->isIconLoaded() == false) loading = true;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (loading) {
Shinya Kitaoka 120a6e
      m_loading = true;
Shinya Kitaoka 120a6e
      if (!m_timerActive) {
Shinya Kitaoka 120a6e
        m_timerActive = true;
Shinya Kitaoka 120a6e
        startTimer(200);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ThumbnailViewer::select(int index) {
Shinya Kitaoka 120a6e
  assert(index == -1 || 0 <= index && index < (int)m_items.size());
Shinya Kitaoka 120a6e
  m_playing = false;
Shinya Kitaoka 120a6e
  if (m_timerActive) {
Shinya Kitaoka 120a6e
    m_timerActive = false;
Shinya Kitaoka 120a6e
    stopTimer();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  Thumbnail *thumbnail;
Shinya Kitaoka 120a6e
  if (m_selectedItemIndex >= 0) {
Shinya Kitaoka 120a6e
    thumbnail = m_items[m_selectedItemIndex];
Shinya Kitaoka 120a6e
    thumbnail->setPlaying(false);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  m_selectedItemIndex = index;
Shinya Kitaoka 120a6e
  onSelect(m_selectedItemIndex);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ThumbnailViewer::startPlaying() {
Shinya Kitaoka 120a6e
  if (m_playing) return;
Shinya Kitaoka 120a6e
  if (m_selectedItemIndex < 0) return;
Shinya Kitaoka 120a6e
  m_playing = true;
Shinya Kitaoka 120a6e
  assert(0 <= m_selectedItemIndex && m_selectedItemIndex < (int)m_items.size());
Shinya Kitaoka 120a6e
  m_items[m_selectedItemIndex]->setPlaying(true);
Shinya Kitaoka 120a6e
  if (!m_timerActive) {
Shinya Kitaoka 120a6e
    m_timerActive = true;
Shinya Kitaoka 120a6e
    startTimer(200);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ThumbnailViewer::stopPlaying() {
Shinya Kitaoka 120a6e
  if (!m_playing) return;
Shinya Kitaoka 120a6e
  m_playing = false;
Shinya Kitaoka 120a6e
  if (m_selectedItemIndex >= 0) {
Shinya Kitaoka 120a6e
    assert(0 <= m_selectedItemIndex &&
Shinya Kitaoka 120a6e
           m_selectedItemIndex < (int)m_items.size());
Shinya Kitaoka 120a6e
    m_items[m_selectedItemIndex]->gotoFrame(0);
Shinya Kitaoka 120a6e
    m_items[m_selectedItemIndex]->setPlaying(false);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (m_timerActive) {
Shinya Kitaoka 120a6e
    m_timerActive = false;
Shinya Kitaoka 120a6e
    stopTimer();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ThumbnailViewer::leftButtonDown(const TMouseEvent &e) {
Shinya Kitaoka 120a6e
  TPoint pos      = e.m_pos - TPoint(m_xoff, m_yoff);
Shinya Kitaoka 120a6e
  m_oldPos        = e.m_pos;
Shinya Kitaoka 120a6e
  m_dragDropArmed = false;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_nameField->hide();
Shinya Kitaoka 120a6e
  //  if(setGeometry(m_textBox + bounds.getP00());
Shinya Kitaoka 120a6e
  //           m_nameField->setText(m_items[index]->getName());
Shinya Kitaoka 120a6e
  //           m_nameField->show();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int index = findItem(pos);
Shinya Kitaoka 120a6e
  if (index >= 0) {
Shinya Kitaoka 120a6e
    if (m_selectedItemIndex == index) {
Shinya Kitaoka 120a6e
      TRect bounds = getItemBounds(index);
Shinya Kitaoka 120a6e
      pos -= bounds.getP00();
Shinya Kitaoka 120a6e
      if (m_playButtonBox.contains(pos)) {
Shinya Kitaoka 120a6e
        if (!m_playing)
Shinya Kitaoka 120a6e
          startPlaying();
Shinya Kitaoka 120a6e
        else
Shinya Kitaoka 120a6e
          stopPlaying();
Shinya Kitaoka 120a6e
        invalidate();
Shinya Kitaoka 120a6e
      } else if (m_textBox.contains(pos)) {
Shinya Kitaoka 120a6e
        stopPlaying();
Shinya Kitaoka 120a6e
        m_nameField->setGeometry(m_textBox + bounds.getP00() + TPoint(-10, 0));
Shinya Kitaoka 120a6e
        m_nameField->setThumbnail(m_items[index]);
Shinya Kitaoka 120a6e
        m_nameField->show();
Shinya Kitaoka 120a6e
        setFocusOwner(m_nameField);
Shinya Kitaoka 120a6e
      } else {
Shinya Kitaoka 120a6e
        m_dragDropArmed = true;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      select(index);
Shinya Kitaoka 120a6e
      m_dragDropArmed = true;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    select(-1);
Shinya Kitaoka 120a6e
  invalidate();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ThumbnailViewer::leftButtonDrag(const TMouseEvent &e) {
Shinya Kitaoka 120a6e
  if (!m_dragDropArmed || m_selectedItemIndex < 0) return;
Shinya Kitaoka 120a6e
  if (norm2(e.m_pos - m_oldPos) < 100) return;
Shinya Kitaoka 120a6e
  m_dragDropArmed = false;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Thumbnail *item = m_items[m_selectedItemIndex];
Shinya Kitaoka 120a6e
  TFilePath path  = item->getPath();
Shinya Kitaoka 120a6e
  if (path.getWideString() != toWideString("")) {
Shinya Kitaoka 120a6e
    TDropSource dropSource;
Shinya Kitaoka 120a6e
    std::vector<std::string> v;</std::string>
Shinya Kitaoka 120a6e
    v.push_back(toString(path.getWideString()));
Shinya Kitaoka 120a6e
    TDataObject data(v);
Shinya Kitaoka 120a6e
    dropSource.doDragDrop(data);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ThumbnailViewer::leftButtonUp(const TMouseEvent &e) {
Shinya Kitaoka 120a6e
  m_dragDropArmed = false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ThumbnailViewer::leftButtonDoubleClick(const TMouseEvent &e) {
Shinya Kitaoka 120a6e
  TPoint pos = e.m_pos - TPoint(m_xoff, m_yoff);
Shinya Kitaoka 120a6e
  m_nameField->hide();
Shinya Kitaoka 120a6e
  int index = findItem(pos);
Shinya Kitaoka 120a6e
  if (index >= 0) onDoubleClick(index);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ThumbnailViewer::onTimer(int) {
Shinya Kitaoka 120a6e
  stopTimer();
Shinya Kitaoka 120a6e
  m_timerActive = false;
Shinya Kitaoka 120a6e
  if (m_playing && m_selectedItemIndex >= 0) {
Shinya Kitaoka 120a6e
    Thumbnail *thumbnail = m_items[m_selectedItemIndex];
Shinya Kitaoka 120a6e
    bool ret             = thumbnail->nextFrame();
Shinya Kitaoka 120a6e
    if (ret) {
Shinya Kitaoka 120a6e
      m_timerActive = true;
Shinya Kitaoka 120a6e
      startTimer(100);
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      thumbnail->gotoFrame(0);
Shinya Kitaoka 120a6e
      thumbnail->setPlaying(false);
Shinya Kitaoka 120a6e
      m_playing = false;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    invalidate();
Shinya Kitaoka 120a6e
  } else if (m_loading) {
Shinya Kitaoka 120a6e
    for (int i = 0; i < (int)m_items.size(); i++)
Shinya Kitaoka 120a6e
      if (!m_items[i]->isIconLoaded()) {
Shinya Kitaoka 120a6e
        m_items[i]->loadIcon();
Shinya Kitaoka 120a6e
        invalidate();
Shinya Kitaoka 120a6e
        return;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ThumbnailViewer::addItem(Thumbnail *item) { m_items.push_back(item); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ThumbnailViewer::removeItem(Thumbnail *item) {
Shinya Kitaoka 120a6e
  std::vector<thumbnail *="">::iterator it =</thumbnail>
Shinya Kitaoka 120a6e
      std::find(m_items.begin(), m_items.end(), item);
Shinya Kitaoka 120a6e
  if (it != m_items.end()) m_items.erase(it);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_selectedItemIndex = -1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ThumbnailViewer::clearItems() {
Shinya Kitaoka 120a6e
  clearPointerContainer(m_items);
Shinya Kitaoka 120a6e
  m_selectedItemIndex = -1;
Shinya Kitaoka 120a6e
  m_playing           = false;
Shinya Kitaoka 120a6e
  m_loading           = false;
Shinya Kitaoka 120a6e
  if (m_timerActive) {
Shinya Kitaoka 120a6e
    stopTimer();
Shinya Kitaoka 120a6e
    m_timerActive = false;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ThumbnailViewer::loadDirectory(const TFilePath &dirPath,
Shinya Kitaoka 120a6e
                                    const vector<string> &fileTypes) {</string>
Shinya Kitaoka 120a6e
  clearItems();
Shinya Kitaoka 120a6e
  TFilePathSet fps;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    fps = TSystem::readDirectory(dirPath);
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
    TMessage::error(toString(dirPath.getWideString()) +
Shinya Kitaoka 120a6e
                    ": can't read directory");
Shinya Kitaoka 120a6e
    updateContentSize();
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  fps = TSystem::packLevelNames(fps);
Shinya Kitaoka 120a6e
  fps.sort();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (TFilePathSet::iterator it = fps.begin(); it != fps.end(); it++) {
Shinya Kitaoka 120a6e
    try {
Shinya Kitaoka 120a6e
      TFilePath fp = *it;
Shinya Kitaoka 120a6e
      if (fp.getType() == "bmp" && fp.getName().find("_icon") != string::npos)
Shinya Kitaoka 120a6e
        continue;
Shinya Kitaoka 120a6e
      if (std::find(fileTypes.begin(), fileTypes.end(), fp.getType()) ==
Shinya Kitaoka 120a6e
          fileTypes.end())
Shinya Kitaoka 120a6e
        continue;
Shinya Kitaoka 120a6e
      Thumbnail *item = FileThumbnail::create(m_iconBox.getSize(), fp);
Shinya Kitaoka 120a6e
      if (item) m_items.push_back(item);
Shinya Kitaoka 120a6e
    } catch (...) {
Shinya Kitaoka 120a6e
      TMessage::error(toString(it->getWideString()) + ": can't read thumbnail");
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  updateContentSize();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ThumbnailViewer::updateContentSize() {
Shinya Kitaoka 120a6e
  int itemCount   = m_items.size();
Shinya Kitaoka 120a6e
  int columnCount = getColumnCount();
Shinya Kitaoka 120a6e
  int rowCount    = (itemCount + columnCount - 1) / columnCount;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRect rect;
Shinya Kitaoka 120a6e
  int yrange = rowCount * (m_itemSize.ly + m_itemSpace.ly) - m_itemSpace.ly +
Shinya Kitaoka 120a6e
               m_margins.ly * 2;
Shinya Kitaoka 120a6e
  setContentSize(TDimension(getLx(), yrange));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ThumbnailViewer::scrollPage(int y) { doScrollTo_y(tmax(y, 0)); }