KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
kiway_player.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2014 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25
26#include <kiway_player.h>
27#include <kiway_express.h>
28#include <kiway.h>
29#include <id.h>
30#include <macros.h>
31#include <typeinfo>
32#include <wx/utils.h>
33#include <wx/evtloop.h>
34#include <wx/socket.h>
35#include <core/raii.h>
36
37
38BEGIN_EVENT_TABLE( KIWAY_PLAYER, EDA_BASE_FRAME )
41END_EVENT_TABLE()
42
43
44KIWAY_PLAYER::KIWAY_PLAYER( KIWAY* aKiway, wxWindow* aParent, FRAME_T aFrameType,
45 const wxString& aTitle, const wxPoint& aPos, const wxSize& aSize,
46 long aStyle, const wxString& aFrameName,
47 const EDA_IU_SCALE& aIuScale ) :
48 EDA_BASE_FRAME( aParent, aFrameType, aTitle, aPos, aSize, aStyle, aFrameName, aKiway,
49 aIuScale ),
50 m_modal( false ),
51 m_modal_loop( nullptr ),
52 m_modal_resultant_parent( nullptr ),
53 m_modal_ret_val( false ),
54 m_socketServer( nullptr )
55{
56}
57
58
60{
61 // socket server must be destructed before we complete
62 // destructing the frame or else we could crash
63 // as the socket server holds a reference to this frame
64 if( m_socketServer )
65 {
66 // ensure any event handling stops
67 m_socketServer->Notify( false );
68
69 delete m_socketServer;
70 m_socketServer = nullptr;
71 }
72
73 // remove active sockets as well
74 for( wxSocketBase* socket : m_sockets )
75 {
76 if( !socket )
77 continue;
78
79 // ensure any event handling stops
80 socket->Notify( false );
81
82 delete socket;
83 }
84
85 m_sockets.clear();
86}
87
88
90{
91 // override this in derived classes.
92}
93
94
95bool KIWAY_PLAYER::ShowModal( wxString* aResult, wxWindow* aResultantFocusWindow )
96{
97 wxASSERT_MSG( IsModal(), wxT( "ShowModal() shouldn't be called on non-modal frame" ) );
98
99 /*
100 This function has a nice interface but a necessarily unsightly implementation.
101 Now the implementation is encapsulated, localizing future changes.
102
103 It works in tandem with DismissModal(). But only ShowModal() is in the
104 vtable and therefore cross-module capable.
105 */
106
107 NULLER raii_nuller( (void*&) m_modal_loop );
108
109 m_modal_resultant_parent = aResultantFocusWindow;
110
111 Show( true );
112 Raise(); // Needed on some Window managers to always display the frame
113
114 SetFocus();
115
116 {
117 // Using wxWindowDisabler() has two issues: it will disable top-level windows that are
118 // our *children* (such as sub-frames), and it will disable all context menus we try to
119 // put up. Fortunatly we already had to cross this Rubicon for QuasiModal dialogs, so
120 // we re-use that strategy.
121 wxWindow* parent = GetParent();
122
123 while( parent && !parent->IsTopLevel() )
124 parent = parent->GetParent();
125
126 WINDOW_DISABLER raii_parent_disabler( parent );
127
128 wxGUIEventLoop event_loop;
129 m_modal_loop = &event_loop;
130 event_loop.Run();
131 }
132
133 if( aResult )
134 *aResult = m_modal_string;
135
136 if( aResultantFocusWindow )
137 {
138 aResultantFocusWindow->Raise();
139
140 // have the final say, after wxWindowDisabler reenables my parent and
141 // the events settle down, set the focus
142 wxSafeYield();
143 aResultantFocusWindow->SetFocus();
144 }
145
146 return m_modal_ret_val;
147}
148
149
151{
153
154 return EDA_BASE_FRAME::Destroy();
155}
156
157
159{
160 return !m_modal_loop;
161}
162
163
164void KIWAY_PLAYER::DismissModal( bool aRetVal, const wxString& aResult )
165{
166 m_modal_ret_val = aRetVal;
167 m_modal_string = aResult;
168
169 if( m_modal_loop )
170 {
171 m_modal_loop->Exit();
172 m_modal_loop = nullptr; // this marks it as dismissed.
173 }
174
175 Show( false );
176}
177
178
180{
181 // logging support
182 KiwayMailIn( aEvent ); // call the virtual, override in derived.
183}
184
185
186void KIWAY_PLAYER::language_change( wxCommandEvent& event )
187{
188 int id = event.GetId();
189
190 // tell all the KIWAY_PLAYERs about the language change.
191 Kiway().SetLanguage( id );
192}
193
194
195
196// LocalWords: ShowModal DismissModal vtable wxWindowDisabler aui
197// LocalWords: miniframe reenables KIWAY PLAYERs
The base frame for deriving all KiCad main window classes.
FRAME_T GetFrameType() const
Carry a payload from one KIWAY_PLAYER to another within a PROJECT.
Definition: kiway_express.h:40
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Definition: kiway_holder.h:55
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
Definition: kiway_player.h:65
virtual bool ShowModal(wxString *aResult=nullptr, wxWindow *aResultantFocusWindow=nullptr)
Show this wxFrame as if it were a modal dialog, with all other instantiated wxFrames disabled until t...
bool IsDismissed()
wxString m_modal_string
Definition: kiway_player.h:195
virtual void KiwayMailIn(KIWAY_EXPRESS &aEvent)
Receive KIWAY_EXPRESS messages from other players.
void language_change(wxCommandEvent &event)
An event handler called on a language menu selection.
bool m_modal_ret_val
Definition: kiway_player.h:196
void kiway_express(KIWAY_EXPRESS &aEvent)
Event handler, routes to derivative specific virtual KiwayMailIn().
wxWindow * m_modal_resultant_parent
Definition: kiway_player.h:194
bool Destroy() override
Our version of Destroy() which is virtual from wxWidgets.
wxGUIEventLoop * m_modal_loop
Points to nested event_loop. NULL means not modal and dismissed.
Definition: kiway_player.h:193
std::vector< wxSocketBase * > m_sockets
Definition: kiway_player.h:199
void DismissModal(bool aRetVal, const wxString &aResult=wxEmptyString)
bool IsModal() const override
Return true if the frame is shown in our modal mode and false if the frame is shown as an usual frame...
Definition: kiway_player.h:154
wxSocketServer * m_socketServer
Definition: kiway_player.h:198
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:285
virtual void SetLanguage(int aLanguage)
Change the language and then calls ShowChangedLanguage() on all #KIWAY_PLAYERs.
Definition: kiway.cpp:543
void PlayerDidClose(FRAME_T aFrameType)
Notifies a Kiway that a player has been closed.
Definition: kiway.cpp:521
Definition: raii.h:38
Temporarily disable a window, and then re-enable on destruction.
Definition: raii.h:87
EVT_MENU_RANGE(ID_GERBVIEW_DRILL_FILE1, ID_GERBVIEW_DRILL_FILEMAX, GERBVIEW_FRAME::OnDrlFileHistory) EVT_MENU_RANGE(ID_GERBVIEW_ZIP_FILE1
FRAME_T
The set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
Definition: frame_type.h:33
Common command IDs shared by more than one of the KiCad applications.
@ ID_LANGUAGE_CHOICE
Definition: id.h:96
@ ID_LANGUAGE_CHOICE_END
Definition: id.h:133
#define EVT_KIWAY_EXPRESS(func)
Event table definition for the KIWAY_EXPRESS event class.
Definition: kiway_express.h:91
This file contains miscellaneous commonly used macros and functions.