File: chessboard.h

package info (click to toggle)
wxpython4.0 4.2.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 232,540 kB
  • sloc: cpp: 958,937; python: 233,059; ansic: 150,441; makefile: 51,662; sh: 8,687; perl: 1,563; javascript: 584; php: 326; xml: 200
file content (93 lines) | stat: -rw-r--r-- 3,663 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/////////////////////////////////////////////////////////////////////////////
// Name:        samples/event/chessboard.h
// Purpose:     Part of wxWidgets event sample, shows how to create a new wxEvent class
// Author:      PB <pbfordev@gmail.com>
// Created:     2019-10-29
// Copyright:   (c) 2019 wxWidgets development team
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

#ifndef _WX_CHESSBOARD_H_

#include "wx/wx.h"

// A chessboard consists of 8x8 squares organized
// in columns (called files) and rows (called ranks).
// In a 2D view, files are labeled from left to right
// 'a' to 'h' while ranks are numbered from bottom to
// top 1 to 8.
// ChessBoardEvent tells us something happened on
// the chessboard. Either a square was clicked
// (myEVT_CHESSBOARD_CLICKED) or the mouse cursor was dragged
// from one square to another (myEVT_CHESSBOARD_DRAGGED).
class ChessBoardEvent : public wxCommandEvent
{
public:
    ChessBoardEvent(wxEventType commandEventType = wxEVT_NULL, int id = 0)
        : wxCommandEvent(commandEventType, id),
          m_file('a'), m_rank(1), m_fileTo('a'), m_rankTo(1)
    {}

    // myEVT_CHESSBOARD_CLICKED: Returns File/Rank for the square clicked.
    // myEVT_CHESSBOARD_DRAGGED: Returns File/Rank for the square
    //                           the mouse cursor was dragged from.
    char GetFile() const    { return m_file; }
    wxUint8 GetRank() const { return m_rank; }

    // Valid only for myEVT_CHESSBOARD_DRAGGED,
    // returns File/Rank for the square where the mouse cursor was dragged to.
    char GetFileTo() const    { return m_fileTo; }
    wxUint8 GetRankTo() const { return m_rankTo; }

    void SetFile(char file)    { m_file = file; }
    void SetRank(wxUint8 rank) { m_rank = rank; }

    void SetFileTo(char file)    { m_fileTo = file; }
    void SetRankTo(wxUint8 rank) { m_rankTo = rank; }

    virtual wxEvent* Clone() const wxOVERRIDE { return new ChessBoardEvent(*this); }
private:
    char  m_file;
    wxUint8 m_rank;
    char m_fileTo;
    wxUint8 m_rankTo;
};


// Declare new event types,
// the matching definitions are in chessboard.cpp.
wxDECLARE_EVENT(myEVT_CHESSBOARD_CLICKED, ChessBoardEvent);
wxDECLARE_EVENT(myEVT_CHESSBOARD_DRAGGED, ChessBoardEvent);

// The following typedef and macro are needed only when the new event
// class is to be used with event table macros or the legacy Connect(),
// to cast the type of a function handling it to the type expected by
// the event table machinery, see its use in EVT_CHESSBOARD_xxx below.
// I.e., when only Bind() is going to be used you do not need them.
typedef void (wxEvtHandler::*ChessBoardEventFunction)(ChessBoardEvent&);
#define ChessBoardEventHandler(func) wxEVENT_HANDLER_CAST(ChessBoardEventFunction, func)

// These defines are needed only if the new event is to be used with
// event tables, i.e., when only Bind() is going to be used you do not need them.
#define EVT_CHESSBOARD_CLICKED(id, func) \
    wx__DECLARE_EVT1(myEVT_CHESSBOARD_CLICKED, id, ChessBoardEventHandler(func))
#define EVT_CHESSBOARD_DRAGGED(id, func) \
    wx__DECLARE_EVT1(myEVT_CHESSBOARD_DRAGGED, id, ChessBoardEventHandler(func))


// Dialog displaying the chessboard
// and handling ChessBoardEvents
class MyChessBoardDialog : public wxDialog
{
public:
    MyChessBoardDialog(wxWindow* parent);
private:
    void OnChessBoardClicked(ChessBoardEvent& event);
    void OnChessBoardDragged(ChessBoardEvent& event);

    // Just for demonstration, we will use an event table
    // for myEVT_CHESSBOARD_CLICKED.
    wxDECLARE_EVENT_TABLE();
};

#endif // #ifndef _WX_CHESSBOARD_H_