tool_dispatcher.h 4.15 KB
Newer Older
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
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2013 CERN
 * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#ifndef __TOOL_DISPATCHER_H
#define __TOOL_DISPATCHER_H

#include <vector>

#include <tool/tool_event.h>

class TOOL_MANAGER;
class PCB_BASE_FRAME;

Maciej Suminski's avatar
Maciej Suminski committed
35 36
namespace KIGFX
{
37
class VIEW;
38 39
};

40
/**
41
 * Class TOOL_DISPATCHER
42
 *
43
 * - takes wx events,
44
 * - fixes all wx quirks (mouse warping, panning, ordering problems, etc)
45 46
 * - translates coordinates to world space
 * - low-level input conditioning (drag/click threshold), updating mouse position during view auto-scroll/pan.
47
 * - issues TOOL_EVENTS to the tool manager
48 49 50 51
 */

class TOOL_DISPATCHER
{
Maciej Suminski's avatar
Maciej Suminski committed
52 53 54 55 56 57 58 59 60 61
public:
    /**
     * Constructor
     *
     * @param aToolMgr: tool manager instance the events will be sent to
     * @param aEditFrame: the frame wx events come from
     */
    TOOL_DISPATCHER( TOOL_MANAGER* aToolMgr, PCB_BASE_FRAME* aEditFrame );
    virtual ~TOOL_DISPATCHER();

62 63 64 65
    /**
     * Function ResetState()
     * Brings the dispatcher to its initial state.
     */
Maciej Suminski's avatar
Maciej Suminski committed
66
    virtual void ResetState();
67 68 69 70 71 72 73

    /**
     * Function DispatchWxEvent()
     * Processes wxEvents (mostly UI events), translates them to TOOL_EVENTs, and makes tools
     * handle those.
     * @param aEvent is the wxWidgets event to be processed.
     */
Maciej Suminski's avatar
Maciej Suminski committed
74
    virtual void DispatchWxEvent( wxEvent& aEvent );
75 76 77 78 79 80 81 82

    /**
     * Function DispatchWxCommand()
     * Processes wxCommands (mostly menu related events) and runs appropriate actions (eg. run the
     * specified tool).
     * @param aEvent is the wxCommandEvent to be processed.
     */
    virtual void DispatchWxCommand( const wxCommandEvent& aEvent );
Maciej Suminski's avatar
Maciej Suminski committed
83 84

private:
85
    ///> Number of mouse buttons that is handled in events.
Maciej Suminski's avatar
Maciej Suminski committed
86
    static const int MouseButtonCount = 3;
87 88 89

    ///> The time threshold for a mouse button press that distinguishes between a single mouse
    ///> click and a beginning of drag event (expressed in milliseconds).
Maciej Suminski's avatar
Maciej Suminski committed
90
    static const int DragTimeThreshold = 300;
91 92 93

    ///> The distance threshold for mouse cursor that disinguishes between a single mouse click
    ///> and a beginning of drag event (expressed in screen pixels).
Maciej Suminski's avatar
Maciej Suminski committed
94 95
    static const int DragDistanceThreshold = 8;

96
    ///> Handles mouse related events (click, motion, dragging)
97
    bool handleMouseButton( wxEvent& aEvent, int aIndex, bool aMotion );
Maciej Suminski's avatar
Maciej Suminski committed
98

99
    ///> Saves the state of key modifiers (Alt, Ctrl and so on).
100
    template <class EventType>
101 102 103 104 105
    static int decodeModifiers( const EventType* aState )
    {
        int mods = 0;

        if( aState->ControlDown() )
Maciej Suminski's avatar
Maciej Suminski committed
106
            mods |= MD_CTRL;
107

108
        if( aState->AltDown() )
Maciej Suminski's avatar
Maciej Suminski committed
109
            mods |= MD_ALT;
110

111
        if( aState->ShiftDown() )
Maciej Suminski's avatar
Maciej Suminski committed
112
            mods |= MD_SHIFT;
113 114 115

        return mods;
    }
Maciej Suminski's avatar
Maciej Suminski committed
116

117
    ///> Stores all the informations regarding a mouse button state.
Maciej Suminski's avatar
Maciej Suminski committed
118
    struct BUTTON_STATE;
119 120

    ///> The last mouse cursor position (in world coordinates).
Maciej Suminski's avatar
Maciej Suminski committed
121
    VECTOR2D m_lastMousePos;
122 123

    ///> State of mouse buttons.
Maciej Suminski's avatar
Maciej Suminski committed
124
    std::vector<BUTTON_STATE*> m_buttons;
125

126
    ///> Returns the instance of VIEW, used by the application.
127
    KIGFX::VIEW* getView();
128 129

    ///> Instance of tool manager that cooperates with the dispatcher.
130
    TOOL_MANAGER* m_toolMgr;
131 132

    ///> Instance of wxFrame that is the source of UI events.
133
    PCB_BASE_FRAME* m_editFrame;
134 135 136
};

#endif