    001: #include <wx/wx.h>
    002:
    003: /**
    004:    A window on which the program user can draw
    005:    a triangle by clicking on the three corners.
    006: */
    007: class TriangleWindow : public wxWindow
    008: {
    009: public:
    010:    /**
    011:       Initializes the base class.
    012:       @param parent the parent window
    013:    */
    014:    TriangleWindow(wxWindow* parent);
    015:    /**
    016:       Paints the corners and lines that have already been
    017:       entered.
    018:       @param event the event descriptor
    019:    */
    020:    void OnPaint(wxPaintEvent& event);
    021:    /**
    022:       Adds another corner to the triangle.
    023:       @param event the event descriptor
    024:    */
    025:    void OnMouseEvent(wxMouseEvent& event);
    026: private:
    027:    int x[3];
    028:    int y[3];
    029:    int corners;
    030:    DECLARE_EVENT_TABLE()
    031: };
    032:
    033: /**
    034:    A frame with a window that shows a triangle.
    035: */
    036: class MouseFrame : public wxFrame
    037: {
    038: public:
    039:    /**
    040:       Constructs the window.
    041:    */
    042:    MouseFrame();
    043: private:
    044:    TriangleWindow* window;
    045: };
    046:
    047: /**
    048:    An application to demonstrate mouse event handling.
    049: */
    050: class MouseApp : public wxApp
    051: {
    052: public:
    053:    /**
    054:       Constructs the frame.
    055:    */
    056:    MouseApp();
    057:    /**
    058:       Shows the frame.
    059:       @return true
    060:    */
    061:    virtual bool OnInit();
    062: private:
    063:    MouseFrame* frame;
    064: };
    065:
    066: DECLARE_APP(MouseApp)
    067:
    068: IMPLEMENT_APP(MouseApp)
    069:
    070: BEGIN_EVENT_TABLE(TriangleWindow, wxWindow)
    071:    EVT_MOUSE_EVENTS(TriangleWindow::OnMouseEvent)
    072:    EVT_PAINT(TriangleWindow::OnPaint)
    073: END_EVENT_TABLE()
    074:
    075: TriangleWindow::TriangleWindow(wxWindow* parent)
    076:    : wxWindow(parent, -1)
    077: {
    078:    corners = 0;
    079: }
    080:
    081: void TriangleWindow::OnMouseEvent(wxMouseEvent& event)
    082: {
    083:    if (event.ButtonDown() && corners < 3)
    084:    {
    085:       x[corners] = event.GetX();
    086:       y[corners] = event.GetY();
    087:       corners++;
    088:       Refresh();
    089:    }
    090: }
    091:
    092: void TriangleWindow::OnPaint(wxPaintEvent& event)
    093: {
    094:    const int RADIUS = 2;
    095:    wxPaintDC dc(this);
    096:    dc.SetBrush(*wxTRANSPARENT_BRUSH);
    097:    if (corners == 1)
    098:       dc.DrawEllipse(x[0] - RADIUS, y[0] - RADIUS,
    099:          2 * RADIUS, 2 * RADIUS);
    100:    if (corners >= 2)
    101:       dc.DrawLine(x[0], y[0], x[1], y[1]);
    102:    if (corners >= 3)
    103:    {
    104:       dc.DrawLine(x[1], y[1], x[2], y[2]);
    105:       dc.DrawLine(x[2], y[2], x[0], y[0]);
    106:    }
    107: }
    108:
    109: MouseFrame::MouseFrame()
    110:    : wxFrame(NULL, -1, "MouseFrame")
    111: {
    112:    window = new TriangleWindow(this);
    113: }
    114:
    115: MouseApp::MouseApp()
    116: {
    117:    frame = new MouseFrame();
    118: }
    119:
    120: bool MouseApp::OnInit()
    121: {
    122:    frame->Show(true);
    123:    return true;
    124: }