File: action_menu.cpp

package info (click to toggle)
kicad 9.0.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 770,320 kB
  • sloc: cpp: 961,692; ansic: 121,001; xml: 66,428; python: 18,387; sh: 1,010; awk: 301; asm: 292; makefile: 227; javascript: 167; perl: 10
file content (695 lines) | stat: -rw-r--r-- 21,457 bytes parent folder | download | duplicates (3)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2013-2019 CERN
 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
 * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
 * @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
 */

#include <bitmaps.h>
#include <eda_base_frame.h>
#include <functional>
#include <id.h>
#include <kiface_base.h>
#include <tool/action_menu.h>
#include <tool/actions.h>
#include <tool/tool_event.h>
#include <tool/tool_interactive.h>
#include <tool/tool_manager.h>
#include <trace_helpers.h>
#include <wx/log.h>
#include <wx/stc/stc.h>
#include <textentry_tricks.h>
#include <wx/listctrl.h>
#include <wx/grid.h>
#include <widgets/ui_common.h>

using namespace std::placeholders;


ACTION_MENU::ACTION_MENU( bool isContextMenu, TOOL_INTERACTIVE* aTool ) :
    m_isForcedPosition( false ),
    m_dirty( true ),
    m_titleDisplayed( false ),
    m_isContextMenu( isContextMenu ),
    m_icon( BITMAPS::INVALID_BITMAP ),
    m_selected( -1 ),
    m_tool( aTool )
{
    setupEvents();
}


ACTION_MENU::~ACTION_MENU()
{
    Disconnect( wxEVT_COMMAND_MENU_SELECTED, wxMenuEventHandler( ACTION_MENU::OnMenuEvent ),
                nullptr, this );
    Disconnect( wxEVT_IDLE, wxIdleEventHandler( ACTION_MENU::OnIdle ), nullptr, this );

    // Set parent to NULL to prevent submenus from unregistering from a nonexistent object
    for( ACTION_MENU* menu : m_submenus )
        menu->SetParent( nullptr );

    ACTION_MENU* parent = dynamic_cast<ACTION_MENU*>( GetParent() );

    if( parent )
        parent->m_submenus.remove( this );
}


void ACTION_MENU::SetIcon( BITMAPS aIcon )
{
    m_icon = aIcon;
}


void ACTION_MENU::setupEvents()
{
    Connect( wxEVT_COMMAND_MENU_SELECTED, wxMenuEventHandler( ACTION_MENU::OnMenuEvent ), nullptr,
             this );
    Connect( wxEVT_IDLE, wxIdleEventHandler( ACTION_MENU::OnIdle ), nullptr, this );
}


void ACTION_MENU::SetTitle( const wxString& aTitle )
{
    // Unfortunately wxMenu::SetTitle() does not work very well, so this is an alternative version
    m_title = aTitle;

    // Update the menu title
    if( m_titleDisplayed )
        DisplayTitle( true );
}


void ACTION_MENU::DisplayTitle( bool aDisplay )
{
    if( ( !aDisplay || m_title.IsEmpty() ) && m_titleDisplayed )
    {
        // Destroy the menu entry keeping the title..
        wxMenuItem* item = FindItemByPosition( 0 );
        wxASSERT( item->GetItemLabelText() == GetTitle() );
        Destroy( item );

        // ..and separator
        item = FindItemByPosition( 0 );
        wxASSERT( item->IsSeparator() );
        Destroy( item );
        m_titleDisplayed = false;
    }

    else if( aDisplay && !m_title.IsEmpty() )
    {
        if( m_titleDisplayed )
        {
            // Simply update the title
            FindItemByPosition( 0 )->SetItemLabel( m_title );
        }
        else
        {
            // Add a separator and a menu entry to display the title
            InsertSeparator( 0 );
            Insert( 0, new wxMenuItem( this, wxID_NONE, m_title, wxEmptyString, wxITEM_NORMAL ) );

            if( !!m_icon )
                KIUI::AddBitmapToMenuItem( FindItemByPosition( 0 ), KiBitmap( m_icon ) );

            m_titleDisplayed = true;
        }
    }
}


wxMenuItem* ACTION_MENU::Add( const wxString& aLabel, int aId, BITMAPS aIcon )
{
    wxASSERT_MSG( FindItem( aId ) == nullptr, wxS( "Duplicate menu IDs!" ) );

    wxMenuItem* item = new wxMenuItem( this, aId, aLabel, wxEmptyString, wxITEM_NORMAL );

    if( !!aIcon )
        KIUI::AddBitmapToMenuItem( item, KiBitmap( aIcon ) );

    return Append( item );
}


wxMenuItem* ACTION_MENU::Add( const wxString& aLabel, const wxString& aTooltip, int aId,
                              BITMAPS aIcon, bool aIsCheckmarkEntry )
{
    wxASSERT_MSG( FindItem( aId ) == nullptr, wxS( "Duplicate menu IDs!" ) );

    wxMenuItem* item = new wxMenuItem( this, aId, aLabel, aTooltip,
                                       aIsCheckmarkEntry ? wxITEM_CHECK : wxITEM_NORMAL );

    if( !!aIcon )
        KIUI::AddBitmapToMenuItem( item, KiBitmap( aIcon ) );

    return Append( item );
}


wxMenuItem* ACTION_MENU::Add( const TOOL_ACTION& aAction, bool aIsCheckmarkEntry,
                              const wxString& aOverrideLabel )
{
    // ID numbers for tool actions are assigned above ACTION_BASE_UI_ID inside TOOL_EVENT
    BITMAPS icon = aAction.GetIcon();

    // Allow the label to be overridden at point of use
    wxString menuLabel = aOverrideLabel.IsEmpty() ? aAction.GetMenuItem() : aOverrideLabel;

    wxMenuItem* item = new wxMenuItem( this, aAction.GetUIId(), menuLabel,
                                       aAction.GetTooltip(),
                                       aIsCheckmarkEntry ? wxITEM_CHECK : wxITEM_NORMAL );
    if( !!icon )
        KIUI::AddBitmapToMenuItem( item, KiBitmap( icon ) );

    m_toolActions[aAction.GetUIId()] = &aAction;

    return Append( item );
}


wxMenuItem* ACTION_MENU::Add( ACTION_MENU* aMenu )
{
    m_submenus.push_back( aMenu );

    wxASSERT_MSG( !aMenu->m_title.IsEmpty(),
                  wxS( "Set a title for ACTION_MENU using SetTitle()" ) );

    if( !!aMenu->m_icon )
    {
        wxMenuItem* newItem = new wxMenuItem( this, -1, aMenu->m_title );
        KIUI::AddBitmapToMenuItem( newItem, KiBitmap( aMenu->m_icon ) );
        newItem->SetSubMenu( aMenu );
        return Append( newItem );
    }
    else
    {
        return AppendSubMenu( aMenu, aMenu->m_title );
    }
}


void ACTION_MENU::AddClose( const wxString& aAppname )
{
#ifdef __WINDOWS__
    Add( _( "Close" ),
         wxString::Format( _( "Close %s" ), aAppname ),
         wxID_CLOSE,
         BITMAPS::exit );
#else
    Add( _( "Close" ) + wxS( "\tCtrl+W" ),
         wxString::Format( _( "Close %s" ), aAppname ),
         wxID_CLOSE,
         BITMAPS::exit );
#endif
}


void ACTION_MENU::AddQuitOrClose( KIFACE_BASE* aKiface, wxString aAppname )
{
    if( !aKiface || aKiface->IsSingle() ) // not when under a project mgr
    {
        // Don't use ACTIONS::quit; wxWidgets moves this on OSX and expects to find it via
        // wxID_EXIT
        Add( _( "Quit" ) + wxS( "\tCtrl+Q" ),
             wxString::Format( _( "Quit %s" ), aAppname ),
             wxID_EXIT,
             BITMAPS::exit );
    }
    else
    {
        AddClose( aAppname );
    }
}


void ACTION_MENU::AddQuit( const wxString& aAppname )
{
    // Don't use ACTIONS::quit; wxWidgets moves this on OSX and expects to find it via
    // wxID_EXIT
    Add( _( "Quit" ) + wxS( "\tCtrl+Q" ),
         wxString::Format( _( "Quit %s" ), aAppname ),
         wxID_EXIT,
         BITMAPS::exit );
}


void ACTION_MENU::Clear()
{
    m_titleDisplayed = false;

    for( int i = GetMenuItemCount() - 1; i >= 0; --i )
        Destroy( FindItemByPosition( i ) );

    m_toolActions.clear();
    m_submenus.clear();

    wxASSERT( GetMenuItemCount() == 0 );
}


bool ACTION_MENU::HasEnabledItems() const
{
    for( wxMenuItem* item : GetMenuItems() )
    {
        if( item->IsEnabled() && !item->IsSeparator() )
            return true;
    }

    return false;
}


void ACTION_MENU::UpdateAll()
{
    try
    {
        update();
    }
    catch( std::exception& )
    {
    }

    if( m_tool )
        updateHotKeys();

    runOnSubmenus( std::bind( &ACTION_MENU::UpdateAll, _1 ) );
}


void ACTION_MENU::ClearDirty()
{
    m_dirty = false;
    runOnSubmenus( std::bind( &ACTION_MENU::ClearDirty, _1 ) );
}


void ACTION_MENU::SetDirty()
{
    m_dirty = true;
    runOnSubmenus( std::bind( &ACTION_MENU::SetDirty, _1 ) );
}


void ACTION_MENU::SetTool( TOOL_INTERACTIVE* aTool )
{
    m_tool = aTool;
    runOnSubmenus( std::bind( &ACTION_MENU::SetTool, _1, aTool ) );
}


ACTION_MENU* ACTION_MENU::Clone() const
{
    ACTION_MENU* clone = create();
    clone->Clear();
    clone->copyFrom( *this );
    return clone;
}


ACTION_MENU* ACTION_MENU::create() const
{
    ACTION_MENU* menu = new ACTION_MENU( false );

    wxASSERT_MSG( typeid( *this ) == typeid( *menu ),
                  wxString::Format( "You need to override create() method for class %s",
                                    typeid( *this ).name() ) );

    return menu;
}


TOOL_MANAGER* ACTION_MENU::getToolManager() const
{
    return m_tool ? m_tool->GetManager() : nullptr;
}


void ACTION_MENU::updateHotKeys()
{
    TOOL_MANAGER* toolMgr = getToolManager();

    wxASSERT( toolMgr );

    for( std::pair<const int, const TOOL_ACTION*>& ii : m_toolActions )
    {
        int                id = ii.first;
        const TOOL_ACTION& action = *ii.second;
        int                key = toolMgr->GetHotKey( action ) & ~MD_MODIFIER_MASK;

        if( key > 0 )
        {
            int mod = toolMgr->GetHotKey( action ) & MD_MODIFIER_MASK;
            int flags = 0;
            wxMenuItem* item = FindChildItem( id );

            if( item )
            {
                flags |= ( mod & MD_ALT ) ? wxACCEL_ALT : 0;
                flags |= ( mod & MD_CTRL ) ? wxACCEL_CTRL : 0;
                flags |= ( mod & MD_SHIFT ) ? wxACCEL_SHIFT : 0;

                if( !flags )
                    flags = wxACCEL_NORMAL;

                wxAcceleratorEntry accel( flags, key, id, item );
                item->SetAccel( &accel );
            }
        }
    }
}


// wxWidgets doesn't tell us when a menu command was generated from a hotkey or from
// a menu selection.  It's important to us because a hotkey can be an immediate action
// while the menu selection can not (as it has no associated position).
//
// We get around this by storing the last highlighted menuId.  If it matches the command
// id then we know this is a menu selection.  (You might think we could use the menuOpen
// menuClose events, but these are actually generated for hotkeys as well.)

static int g_last_menu_highlighted_id = 0;


// We need to store the position of the mouse when the menu was opened so it can be passed
// to the command event generated when the menu item is selected.
static VECTOR2D g_menu_open_position;


void ACTION_MENU::OnIdle( wxIdleEvent& event )
{
    g_last_menu_highlighted_id = 0;
    g_menu_open_position.x = 0.0;
    g_menu_open_position.y = 0.0;
}


void ACTION_MENU::OnMenuEvent( wxMenuEvent& aEvent )
{
    OPT_TOOL_EVENT evt;
    wxString       menuText;
    wxEventType    type    = aEvent.GetEventType();
    wxWindow*      focus   = wxWindow::FindFocus();
    TOOL_MANAGER*  toolMgr = getToolManager();

    if( type == wxEVT_MENU_OPEN )
    {
        if( m_dirty && toolMgr )
            toolMgr->RunAction<ACTION_MENU*>( ACTIONS::updateMenu, this );

        wxMenu* parent = dynamic_cast<wxMenu*>( GetParent() );

        // Don't update the position if this menu has a parent or is a menubar menu
        if( !parent && !IsAttached() && toolMgr )
            g_menu_open_position = toolMgr->GetMousePosition();

        g_last_menu_highlighted_id = 0;
    }
    else if( type == wxEVT_MENU_HIGHLIGHT )
    {
        if( aEvent.GetId() > 0 )
            g_last_menu_highlighted_id = aEvent.GetId();

        evt = TOOL_EVENT( TC_COMMAND, TA_CHOICE_MENU_UPDATE, aEvent.GetId() );
    }
    else if( type == wxEVT_COMMAND_MENU_SELECTED )
    {
        // Despite our attempts to catch the theft of text editor CHAR_HOOK and CHAR events
        // in TOOL_DISPATCHER::DispatchWxEvent, wxWidgets sometimes converts those it knows
        // about into menu commands without ever generating the appropriate CHAR_HOOK and CHAR
        // events first.
        if( dynamic_cast<wxTextEntry*>( focus )
                || dynamic_cast<wxStyledTextCtrl*>( focus )
                || dynamic_cast<wxListView*>( focus )
                || dynamic_cast<wxGrid*>( focus ) )
        {
            // Original key event has been lost, so we have to re-create it from the menu's
            // wxAcceleratorEntry.
            wxMenuItem* menuItem = FindItem( aEvent.GetId() );
            wxAcceleratorEntry* acceleratorKey = menuItem ? menuItem->GetAccel() : nullptr;

            if( acceleratorKey )
            {
                wxKeyEvent keyEvent( wxEVT_CHAR_HOOK );
                keyEvent.m_keyCode = acceleratorKey->GetKeyCode();
                keyEvent.m_controlDown = ( acceleratorKey->GetFlags() & wxMOD_CONTROL ) > 0;
                keyEvent.m_shiftDown = ( acceleratorKey->GetFlags() & wxMOD_SHIFT ) > 0;
                keyEvent.m_altDown = ( acceleratorKey->GetFlags() & wxMOD_ALT ) > 0;

                if( wxTextEntry* ctrl = dynamic_cast<wxTextEntry*>( focus ) )
                    TEXTENTRY_TRICKS::OnCharHook( ctrl, keyEvent );
                else
                    focus->HandleWindowEvent( keyEvent );

                if( keyEvent.GetSkipped() )
                {
                    keyEvent.SetEventType( wxEVT_CHAR );
                    focus->HandleWindowEvent( keyEvent );
                }

                // Don't bubble-up dangerous actions; the target may be behind a modeless dialog.
                // Cf. https://gitlab.com/kicad/code/kicad/-/issues/17229
                if( keyEvent.GetKeyCode() == WXK_BACK || keyEvent.GetKeyCode() == WXK_DELETE )
                    return;

                // If the event was used as a KEY event (not skipped) by the focused window,
                // just finish.  Otherwise this is actually a wxEVT_COMMAND_MENU_SELECTED (or the
                // focused window is read only).
                if( !keyEvent.GetSkipped() )
                    return;
            }
        }

        // Store the selected position, so it can be checked by the tools
        m_selected = aEvent.GetId();

        ACTION_MENU* parent = dynamic_cast<ACTION_MENU*>( GetParent() );

        while( parent )
        {
            parent->m_selected = m_selected;
            parent = dynamic_cast<ACTION_MENU*>( parent->GetParent() );
        }

        // Check if there is a TOOL_ACTION for the given UI ID
        if( toolMgr && toolMgr->GetActionManager()->IsActionUIId( m_selected ) )
            evt = findToolAction( m_selected );

        if( !evt )
        {
#ifdef __WINDOWS__
            if( !evt )
            {
                // Try to find the submenu which holds the selected item
                wxMenu* menu = nullptr;
                FindItem( m_selected, &menu );

                // This conditional compilation is probably not needed.
                if( menu )
                {
                    ACTION_MENU* cxmenu = static_cast<ACTION_MENU*>( menu );
                    evt = cxmenu->eventHandler( aEvent );
                }
            }
#else
            if( !evt )
                runEventHandlers( aEvent, evt );
#endif

            // Handling non-ACTION menu entries.  Two ranges of ids are supported:
            //   between 0 and ID_CONTEXT_MENU_ID_MAX
            //   between ID_POPUP_MENU_START and ID_POPUP_MENU_END

            #define ID_CONTEXT_MENU_ID_MAX wxID_LOWEST  /* = 100 should be plenty */

            if( !evt &&
                    ( ( m_selected >= 0 && m_selected < ID_CONTEXT_MENU_ID_MAX ) ||
                      ( m_selected >= ID_POPUP_MENU_START && m_selected <= ID_POPUP_MENU_END ) ) )
            {
                ACTION_MENU* actionMenu = dynamic_cast<ACTION_MENU*>( GetParent() );

                if( actionMenu && actionMenu->PassHelpTextToHandler() )
                    menuText = GetHelpString( aEvent.GetId() );
                else
                    menuText = GetLabelText( aEvent.GetId() );

                evt = TOOL_EVENT( TC_COMMAND, TA_CHOICE_MENU_CHOICE, m_selected, AS_GLOBAL );
                evt->SetParameter( &menuText );
            }
        }
    }

    // forward the action/update event to the TOOL_MANAGER
    // clients that don't supply a tool will have to check GetSelected() themselves
    if( evt && toolMgr )
    {
        wxLogTrace( kicadTraceToolStack, wxS( "ACTION_MENU::OnMenuEvent %s" ), evt->Format() );

        // WARNING: if you're squeamish, look away.
        // What follows is a series of egregious hacks necessitated by a lack of information from
        // wxWidgets on where context-menu-commands and command-key-events originated.

        // If it's a context menu then fetch the mouse position from our context-menu-position
        // hack.
        if( m_isContextMenu )
        {
            evt->SetMousePosition( g_menu_open_position );
        }
        // Check if it is a menubar event, and don't get any position if it is.  Note that we
        // can't  use IsAttached() here, as it only differentiates a context menu, and we also
        // want a hotkey to generate a position.
        else if( g_last_menu_highlighted_id == aEvent.GetId() )
        {
            evt->SetHasPosition( false );
        }
        // Otherwise it's a command-key-event and we need to get the mouse position from the tool
        // manager so that immediate actions work.
        else
        {
            evt->SetMousePosition( toolMgr->GetMousePosition() );
        }

        toolMgr->ProcessEvent( *evt );
    }
    else
    {
        aEvent.Skip();
    }
}


void ACTION_MENU::runEventHandlers( const wxMenuEvent& aMenuEvent, OPT_TOOL_EVENT& aToolEvent )
{
    aToolEvent = eventHandler( aMenuEvent );

    if( !aToolEvent )
        runOnSubmenus( std::bind( &ACTION_MENU::runEventHandlers, _1, aMenuEvent, aToolEvent ) );
}


void ACTION_MENU::runOnSubmenus( std::function<void(ACTION_MENU*)> aFunction )
{
    try
    {
        std::for_each( m_submenus.begin(), m_submenus.end(),
                       [&]( ACTION_MENU* m )
                       {
                           aFunction( m );
                           m->runOnSubmenus( aFunction );
                       } );
    }
    catch( std::exception& )
    {
    }
}


OPT_TOOL_EVENT ACTION_MENU::findToolAction( int aId )
{
    OPT_TOOL_EVENT evt;

    auto findFunc =
            [&]( ACTION_MENU* m )
            {
                if( evt )
                    return;

                const auto it = m->m_toolActions.find( aId );

                if( it != m->m_toolActions.end() )
                    evt = it->second->MakeEvent();
            };

    findFunc( this );

    if( !evt )
        runOnSubmenus( findFunc );

    return evt;
}


void ACTION_MENU::copyFrom( const ACTION_MENU& aMenu )
{
    m_icon = aMenu.m_icon;
    m_title = aMenu.m_title;
    m_titleDisplayed = aMenu.m_titleDisplayed;
    m_selected = -1; // aMenu.m_selected;
    m_tool = aMenu.m_tool;
    m_toolActions = aMenu.m_toolActions;

    // Copy all menu entries
    for( int i = 0; i < (int) aMenu.GetMenuItemCount(); ++i )
    {
        wxMenuItem* item = aMenu.FindItemByPosition( i );
        appendCopy( item );
    }
}


wxMenuItem* ACTION_MENU::appendCopy( const wxMenuItem* aSource )
{
    wxMenuItem* newItem = new wxMenuItem( this, aSource->GetId(), aSource->GetItemLabel(),
                                          aSource->GetHelp(), aSource->GetKind() );

    // Add the source bitmap if it is not the wxNullBitmap
    // On Windows, for Checkable Menu items, adding a bitmap adds also
    // our predefined checked alternate bitmap
    // On other OS, wxITEM_CHECK and wxITEM_RADIO Menu items do not use custom bitmaps.

#if defined( __WXMSW__ )
    // On Windows, AddBitmapToMenuItem() uses the unchecked bitmap for wxITEM_CHECK and
    // wxITEM_RADIO menuitems and automatically adds a checked bitmap.
    // For other menuitrms, use the "checked" bitmap.
    bool use_checked_bm = ( aSource->GetKind() == wxITEM_CHECK ||
                            aSource->GetKind() == wxITEM_RADIO ) ? false : true;
    const wxBitmap& src_bitmap = aSource->GetBitmap( use_checked_bm );
#else
    const wxBitmap& src_bitmap = aSource->GetBitmap();
#endif

    if( src_bitmap.IsOk() && src_bitmap.GetHeight() > 1 )    // a null bitmap has a 0 size
        KIUI::AddBitmapToMenuItem( newItem, src_bitmap );

    if( aSource->IsSubMenu() )
    {
        ACTION_MENU* menu = dynamic_cast<ACTION_MENU*>( aSource->GetSubMenu() );
        wxASSERT_MSG( menu, wxS( "Submenus are expected to be a ACTION_MENU" ) );

        if( menu )
        {
            ACTION_MENU* menuCopy = menu->Clone();
            newItem->SetSubMenu( menuCopy );
            m_submenus.push_back( menuCopy );
        }
    }

    // wxMenuItem has to be added before enabling/disabling or checking
    Append( newItem );

    if( aSource->IsCheckable() )
        newItem->Check( aSource->IsChecked() );

    newItem->Enable( aSource->IsEnabled() );

    return newItem;
}