KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <[email protected]>
5 * Copyright (C) 2014-2021 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
36
37BEGIN_EVENT_TABLE( KIWAY_PLAYER, EDA_BASE_FRAME )
40END_EVENT_TABLE()
41
42
43KIWAY_PLAYER::KIWAY_PLAYER( KIWAY* aKiway, wxWindow* aParent, FRAME_T aFrameType,
44 const wxString& aTitle, const wxPoint& aPos, const wxSize& aSize,
45 long aStyle, const wxString& aFrameName,
46 const EDA_IU_SCALE& aIuScale ) :
47 EDA_BASE_FRAME( aParent, aFrameType, aTitle, aPos, aSize, aStyle, aFrameName, aKiway,
48 aIuScale ),
49 m_modal( false ),
50 m_modal_loop( nullptr ),
51 m_modal_resultant_parent( nullptr ),
52 m_modal_ret_val( false ),
53 m_socketServer( nullptr )
54{
55}
56
57
59{
60 // socket server must be destructed before we complete
61 // destructing the frame or else we could crash
62 // as the socket server holds a reference to this frame
63 if( m_socketServer )
64 {
65 // ensure any event handling stops
66 m_socketServer->Notify( false );
67
68 delete m_socketServer;
69 m_socketServer = nullptr;
70 }
71
72 // remove active sockets as well
73 for( wxSocketBase* socket : m_sockets )
74 {
75 if( !socket )
76 continue;
77
78 // ensure any event handling stops
79 socket->Notify( false );
80
81 delete socket;
82 }
83
84 m_sockets.clear();
85}
86
87
89{
90 // override this in derived classes.
91}
92
93
94bool KIWAY_PLAYER::ShowModal( wxString* aResult, wxWindow* aResultantFocusWindow )
95{
96 wxASSERT_MSG( IsModal(), wxT( "ShowModal() shouldn't be called on non-modal frame" ) );
97
98 /*
99 This function has a nice interface but a necessarily unsightly implementation.
100 Now the implementation is encapsulated, localizing future changes.
101
102 It works in tandem with DismissModal(). But only ShowModal() is in the
103 vtable and therefore cross-module capable.
104 */
105
106 // This is an exception safe way to zero a pointer before returning.
107 // Yes, even though DismissModal() clears this first normally, this is
108 // here in case there's an exception before the dialog is dismissed.
109 struct NULLER
110 {
111 void*& m_what;
112 NULLER( void*& aPtr ) : m_what( aPtr ) {}
113 ~NULLER() { m_what = nullptr; } // indeed, set it to NULL on destruction
114 } clear_this( (void*&) m_modal_loop );
115
116
117 m_modal_resultant_parent = aResultantFocusWindow;
118
119 Show( true );
120 Raise(); // Needed on some Window managers to always display the frame
121
122 SetFocus();
123
124 {
125 // We have to disable all frames but the modal one.
126 // wxWindowDisabler does that, but it also disables all top level windows
127 // We do not want to disable top level windows which are child of the modal one,
128 // if they are enabled.
129 // An example is an aui toolbar which was moved
130 // or a dialog or another frame or miniframe opened by the modal one.
131 wxWindowList wlist = GetChildren();
132 std::vector<wxWindow*> enabledTopLevelWindows;
133
134 for( unsigned ii = 0; ii < wlist.size(); ii++ )
135 {
136 if( wlist[ii]->IsTopLevel() && wlist[ii]->IsEnabled() )
137 enabledTopLevelWindows.push_back( wlist[ii] );
138 }
139
140 // exception safe way to disable all top level windows except the modal one,
141 // re-enables only those that were disabled on exit
142 wxWindowDisabler toggle( this );
143
144 for( unsigned ii = 0; ii < enabledTopLevelWindows.size(); ii++ )
145 enabledTopLevelWindows[ii]->Enable( true );
146
147 wxGUIEventLoop event_loop;
148 m_modal_loop = &event_loop;
149 event_loop.Run();
150
151 } // End of scope for some variables.
152 // End nesting before setting focus below.
153
154 if( aResult )
155 *aResult = m_modal_string;
156
157 if( aResultantFocusWindow )
158 {
159 aResultantFocusWindow->Raise();
160
161 // have the final say, after wxWindowDisabler reenables my parent and
162 // the events settle down, set the focus
163 wxSafeYield();
164 aResultantFocusWindow->SetFocus();
165 }
166
167 return m_modal_ret_val;
168}
169
170
172{
174
175 return EDA_BASE_FRAME::Destroy();
176}
177
178
180{
181 return !m_modal_loop;
182}
183
184
185void KIWAY_PLAYER::DismissModal( bool aRetVal, const wxString& aResult )
186{
187 m_modal_ret_val = aRetVal;
188 m_modal_string = aResult;
189
190 if( m_modal_loop )
191 {
192 m_modal_loop->Exit();
193 m_modal_loop = nullptr; // this marks it as dismissed.
194 }
195
196 Show( false );
197}
198
199
201{
202 // logging support
203 KiwayMailIn( aEvent ); // call the virtual, override in derived.
204}
205
206
207void KIWAY_PLAYER::language_change( wxCommandEvent& event )
208{
209 int id = event.GetId();
210
211 // tell all the KIWAY_PLAYERs about the language change.
212 Kiway().SetLanguage( id );
213}
214
215
216
217// LocalWords: ShowModal DismissModal vtable wxWindowDisabler aui
218// 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
interprocess communication
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:279
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
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
@ ID_LANGUAGE_CHOICE
Definition: id.h:105
@ ID_LANGUAGE_CHOICE_END
Definition: id.h:141
#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.