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
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
74
76{
77 // override this in derived classes.
78}
79
80
81bool KIWAY_PLAYER::ShowModal( wxString* aResult, wxWindow* aResultantFocusWindow )
82{
83 wxASSERT_MSG( IsModal(), wxT( "ShowModal() shouldn't be called on non-modal frame" ) );
84
85 /*
86 This function has a nice interface but a necessarily unsightly implementation.
87 Now the implementation is encapsulated, localizing future changes.
88
89 It works in tandem with DismissModal(). But only ShowModal() is in the
90 vtable and therefore cross-module capable.
91 */
92
93 // This is an exception safe way to zero a pointer before returning.
94 // Yes, even though DismissModal() clears this first normally, this is
95 // here in case there's an exception before the dialog is dismissed.
96 struct NULLER
97 {
98 void*& m_what;
99 NULLER( void*& aPtr ) : m_what( aPtr ) {}
100 ~NULLER() { m_what = nullptr; } // indeed, set it to NULL on destruction
101 } clear_this( (void*&) m_modal_loop );
102
103
104 m_modal_resultant_parent = aResultantFocusWindow;
105
106 Show( true );
107 Raise(); // Needed on some Window managers to always display the frame
108
109 SetFocus();
110
111 {
112 // We have to disable all frames but the modal one.
113 // wxWindowDisabler does that, but it also disables all top level windows
114 // We do not want to disable top level windows which are child of the modal one,
115 // if they are enabled.
116 // An example is an aui toolbar which was moved
117 // or a dialog or another frame or miniframe opened by the modal one.
118 wxWindowList wlist = GetChildren();
119 std::vector<wxWindow*> enabledTopLevelWindows;
120
121 for( unsigned ii = 0; ii < wlist.size(); ii++ )
122 {
123 if( wlist[ii]->IsTopLevel() && wlist[ii]->IsEnabled() )
124 enabledTopLevelWindows.push_back( wlist[ii] );
125 }
126
127 // exception safe way to disable all top level windows except the modal one,
128 // re-enables only those that were disabled on exit
129 wxWindowDisabler toggle( this );
130
131 for( unsigned ii = 0; ii < enabledTopLevelWindows.size(); ii++ )
132 enabledTopLevelWindows[ii]->Enable( true );
133
134 WX_EVENT_LOOP event_loop;
135 m_modal_loop = &event_loop;
136 event_loop.Run();
137
138 } // End of scope for some variables.
139 // End nesting before setting focus below.
140
141 if( aResult )
142 *aResult = m_modal_string;
143
144 if( aResultantFocusWindow )
145 {
146 aResultantFocusWindow->Raise();
147
148 // have the final say, after wxWindowDisabler reenables my parent and
149 // the events settle down, set the focus
150 wxSafeYield();
151 aResultantFocusWindow->SetFocus();
152 }
153
154 return m_modal_ret_val;
155}
156
157
159{
161
162 return EDA_BASE_FRAME::Destroy();
163}
164
165
167{
168 return !m_modal_loop;
169}
170
171
172void KIWAY_PLAYER::DismissModal( bool aRetVal, const wxString& aResult )
173{
174 m_modal_ret_val = aRetVal;
175 m_modal_string = aResult;
176
177 if( m_modal_loop )
178 {
179 m_modal_loop->Exit();
180 m_modal_loop = nullptr; // this marks it as dismissed.
181 }
182
183 Show( false );
184}
185
186
188{
189 // logging support
190 KiwayMailIn( aEvent ); // call the virtual, override in derived.
191}
192
193
194void KIWAY_PLAYER::language_change( wxCommandEvent& event )
195{
196 int id = event.GetId();
197
198 // tell all the KIWAY_PLAYERs about the language change.
199 Kiway().SetLanguage( id );
200}
201
202
203
204// LocalWords: ShowModal DismissModal vtable wxWindowDisabler aui
205// 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:39
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Definition: kiway_holder.h:53
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
Definition: kiway_player.h:67
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:197
virtual void KiwayMailIn(KIWAY_EXPRESS &aEvent)
Receive KIWAY_EXPRESS messages from other players.
WX_EVENT_LOOP * m_modal_loop
< Points to nested event_loop. NULL means not modal and dismissed.
Definition: kiway_player.h:195
void language_change(wxCommandEvent &event)
An event handler called on a language menu selection.
bool m_modal_ret_val
Definition: kiway_player.h:198
void kiway_express(KIWAY_EXPRESS &aEvent)
event handler, routes to derivative specific virtual KiwayMailIn()
wxWindow * m_modal_resultant_parent
Definition: kiway_player.h:196
bool Destroy() override
Our version of Destroy() which is virtual from wxWidgets.
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:156
wxSocketServer * m_socketServer
Definition: kiway_player.h:200
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:569
void PlayerDidClose(FRAME_T aFrameType)
Notifies a Kiway that a player has been closed.
Definition: kiway.cpp:547
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:90
#define WX_EVENT_LOOP
Definition: kiway_player.h:42
This file contains miscellaneous commonly used macros and functions.