namespace faint{

MouseCapture::MouseCapture(wxWindow* window,
  const OnLoss& onLoss,
  const OnRelease& onRelease)
  : m_onCaptureLost(onLoss.Get()),
    m_onRelease(onRelease.Get()),
    m_window(window)
{
  bind(window, wxEVT_MOUSE_CAPTURE_LOST,
    [&](){
      if (m_onCaptureLost != nullptr){
        m_onCaptureLost();
        if (m_onRelease != nullptr){
          m_onRelease();
        }
      }
    });
}

MouseCapture::MouseCapture(wxWindow* w, const OnLoss& onLoss)
  : MouseCapture(w, onLoss, OnRelease(nullptr))
{}

MouseCapture::MouseCapture(wxWindow* w)
  : MouseCapture(w, OnLoss(nullptr))
{}

MouseCapture::~MouseCapture(){
  Release();
}

void MouseCapture::Capture(){
  if (!m_window->HasCapture()){
    m_window->CaptureMouse();
  }
}

bool MouseCapture::HasCapture() const{
  return m_window->HasCapture();
}

void MouseCapture::Release(){
  if (m_window->HasCapture()){
    m_window->ReleaseMouse();
    if (m_onRelease != nullptr){
      m_onRelease();
    }
  }
}

} // namespace
