wxPictureWindow.cpp
author viric <viriketo@gmail.com>
Sun, 24 Apr 2011 14:03:52 +0200
changeset 1 506e0fc65ba3
parent 0 7e720dcafcaf
child 2 b2772bffb62f
permissions -rw-r--r--
Ara processo la tecla ESC, almenys, i ho preparo per un next_picture

#include <wx/scrolwin.h>
#include <wx/image.h>
#include <wx/bitmap.h>
#include <wx/dc.h>
#include <iostream>
#include "wxPictureWindow.hpp"
#include "MainWindow.hpp"

const int factor = 3;

BEGIN_EVENT_TABLE(wxPictureWindow, wxScrolledWindow)
  EVT_MOUSE_EVENTS (wxPictureWindow::OnMouse)
  EVT_KEY_UP (wxPictureWindow::OnKey)
END_EVENT_TABLE()

wxPictureWindow::wxPictureWindow(const wxImage &img, const CropArea area, wxWindow *parent)
    :wxScrolledWindow(parent, wxID_ANY),
    _area(area)
{
    SetImg(img);
    _area.x /= factor;
    _area.y /= factor;
    _area.width /= factor;
    _area.height /= factor;
    SetScrollRate(10, 10);
    SetFocus();
}

void
wxPictureWindow::SetImg(const wxImage &img)
{
    SetVirtualSize(_bmp.GetWidth(), _bmp.GetHeight());
    _bmp = wxBitmap(img);
}

void wxPictureWindow::OnDraw(wxDC &dc)
{
    dc.DrawBitmap(_bmp, 0, 0, false);

    wxRect rect(_area.x, _area.y, _area.width, _area.height);
    dc.SetPen(*wxGREEN_PEN);
    dc.SetBrush(*wxTRANSPARENT_BRUSH);
    dc.DrawRectangle(rect);
}

void wxPictureWindow::OnMouse(wxMouseEvent &ev)
{
    wxClientDC dc(this);
    DoPrepareDC(dc);

    if (ev.LeftDown())
    {
        _area.x = ev.GetLogicalPosition(dc).x;
        _area.y = ev.GetLogicalPosition(dc).y;
        _area.width = 0;
        _area.height = 0;
    }
    else if (ev.LeftIsDown())
    {
        _area.width = ev.GetLogicalPosition(dc).x - _area.x;
        _area.height = ev.GetLogicalPosition(dc).y - _area.y;
    }

    Refresh();
}

wxPictureWindow::~wxPictureWindow()
{
    std::cout << _area.x * factor << " ";
    std::cout << _area.y * factor << " ";
    std::cout << _area.width * factor << " ";
    std::cout << _area.height * factor << std::endl;
}

void
wxPictureWindow::OnKey(wxKeyEvent &e)
{
    using namespace std;
    if (e.GetKeyCode() == WXK_ESCAPE)
    {
        cout << "escape" << endl;;
        wxCommandEvent e(EVT_NEXTPICTURE);
        GetParent()->AddPendingEvent(e);
    }
    else
        e.Skip();
}