summaryrefslogtreecommitdiff
path: root/pcbnew/tools/conditional_menu.h
blob: af9ef2f1ce7d98af94507466f5c18ab248253176 (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2015 CERN
 * @author Maciej Suminski <maciej.suminski@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 CONDITIONAL_MENU_H
#define CONDITIONAL_MENU_H

#include "selection_conditions.h"
#include <boost/unordered_map.hpp>
#include <wx/wx.h>

class SELECTION_TOOL;
class TOOL_ACTION;
class TOOL_INTERACTIVE;
class CONTEXT_MENU;

class CONDITIONAL_MENU
{
public:
    ///> Constant to indicate that we do not care about an ENTRY location in the menu.
    static const int ANY_ORDER = -1;

    CONDITIONAL_MENU( TOOL_INTERACTIVE* aTool ) :
        m_tool( aTool )
    {}

    /**
     * Function AddItem()
     *
     * Adds a menu entry to run a TOOL_ACTION on selected items.
     * @param aAction is a menu entry to be added.
     * @param aCondition is a condition that has to be fulfilled to enable the menu entry.
     * @param aOrder determines location of the added item, higher numbers are put on the bottom.
     * You may use ANY_ORDER here if you think it does not matter.
     */
    void AddItem( const TOOL_ACTION& aAction,
                  const SELECTION_CONDITION& aCondition = SELECTION_CONDITIONS::ShowAlways,
                  int aOrder = ANY_ORDER );

    /**
     * Function AddMenu()
     *
     * Adds a submenu to the menu. CONDITIONAL_MENU takes ownership of the added menu, so it will
     * be freed when the CONDITIONAL_MENU object is destroyed.
     * @param aMenu is the submenu to be added.
     * @param aLabel is the label of added submenu.
     * @param aExpand determines if the added submenu items should be added as individual items
     * or as a submenu.
     * @param aCondition is a condition that has to be fulfilled to enable the submenu entry.
     * @param aOrder determines location of the added menu, higher numbers are put on the bottom.
     * You may use ANY_ORDER here if you think it does not matter.
     */
    void AddMenu( CONTEXT_MENU* aMenu, const wxString& aLabel, bool aExpand = false,
                  const SELECTION_CONDITION& aCondition = SELECTION_CONDITIONS::ShowAlways,
                  int aOrder = ANY_ORDER );

    /**
     * Function AddSeparator()
     *
     * Adds a separator to the menu.
     * @param aCondition is a condition that has to be fulfilled to enable the submenu entry.
     * @param aOrder determines location of the added menu, higher numbers are put on the bottom.
     * You may use ANY_ORDER here if you think it does not matter.
     */
    void AddSeparator( const SELECTION_CONDITION& aCondition = SELECTION_CONDITIONS::ShowAlways,
                       int aOrder = ANY_ORDER );

    /**
     * Function Generate()
     *
     * Generates a context menu that contains only entries that are satisfying assigned conditions.
     * @param aSelection is selection for which the conditions are checked against.
     * @return Menu filtered by the entry conditions.
     */
    CONTEXT_MENU* Generate( SELECTION& aSelection );

private:
    ///> Helper class to organize menu entries.
    class ENTRY
    {
    public:
        ENTRY( const TOOL_ACTION* aAction,
                            const SELECTION_CONDITION& aCondition = SELECTION_CONDITIONS::ShowAlways,
                            int aOrder = ANY_ORDER ) :
            m_type( ACTION ), m_condition( aCondition ), m_order( aOrder ), m_expand( false )
        {
            m_data.action = aAction;
        }

        ENTRY( CONTEXT_MENU* aMenu, const wxString aLabel, bool aExpand = false,
                            const SELECTION_CONDITION& aCondition = SELECTION_CONDITIONS::ShowAlways,
                            int aOrder = ANY_ORDER ) :
            m_type( MENU ), m_condition( aCondition ), m_order( aOrder ), m_label( aLabel ), m_expand( aExpand )
        {
            m_data.menu = aMenu;
        }

        ENTRY( wxMenuItem* aItem, const SELECTION_CONDITION& aCondition = SELECTION_CONDITIONS::ShowAlways,
                            int aOrder = ANY_ORDER ) :
            m_type( WXITEM ), m_condition( aCondition ), m_order( aOrder ), m_expand( false )
        {
            m_data.wxItem = aItem;
        }

        // Separator
        ENTRY( const SELECTION_CONDITION& aCondition = SELECTION_CONDITIONS::ShowAlways,
                            int aOrder = ANY_ORDER ) :
            m_type( SEPARATOR ), m_condition( aCondition ), m_order( aOrder ), m_expand( false )
        {
            m_data.wxItem = NULL;
        }

        ///> Possible entry types.
        enum ENTRY_TYPE {
            ACTION,
            MENU,
            WXITEM,
            SEPARATOR
        };

        inline ENTRY_TYPE Type() const
        {
            return m_type;
        }

        inline const TOOL_ACTION* Action() const
        {
            assert( m_type == ACTION );
            return m_data.action;
        }

        inline CONTEXT_MENU* Menu() const
        {
            assert( m_type == MENU );
            return m_data.menu;
        }

        inline wxMenuItem* wxItem() const
        {
            assert( m_type == WXITEM );
            return m_data.wxItem;
        }

        inline const wxString& Label() const
        {
            assert( m_type == MENU );
            return m_label;
        }

        inline bool Expand() const
        {
            assert( m_type == MENU );
            return m_expand;
        }

        inline const SELECTION_CONDITION& Condition() const
        {
            return m_condition;
        }

        inline int Order() const
        {
            return m_order;
        }

        inline void SetOrder( int aOrder )
        {
            m_order = aOrder;
        }

    private:
        ENTRY_TYPE m_type;

        union {
            const TOOL_ACTION* action;
            CONTEXT_MENU* menu;
            wxMenuItem* wxItem;
        } m_data;

        ///> Condition to be fulfilled to show the entry in menu.
        SELECTION_CONDITION m_condition;

        ///> Order number, the higher the number the lower position it takes it is in the menu.
        int m_order;

        /// CONTEXT_MENU specific fields.
        const wxString m_label;
        bool m_expand;
    };

    ///> Inserts the entry, preserving the requested order.
    void addEntry( ENTRY aEntry );

    ///> List of all menu entries.
    std::list<ENTRY> m_entries;

    ///> tool owning the menu
    TOOL_INTERACTIVE* m_tool;
};

#endif /* CONDITIONAL_MENU_H */