kiway.cpp 11.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2014 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
 * Copyright (C) 2014 KiCad Developers, see CHANGELOG.TXT for contributors.
 *
 * 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
 */

25 26
#include <string.h>

27
#include <macros.h>
28
#include <kiway.h>
29
#include <kiway_player.h>
Dick Hollenbeck's avatar
Dick Hollenbeck committed
30
#include <kiway_express.h>
31
#include <pgm_base.h>
Dick Hollenbeck's avatar
Dick Hollenbeck committed
32
#include <config.h>
33
#include <id.h>
34

35 36 37
#include <wx/stdpaths.h>
#include <wx/debug.h>

38

39 40 41 42
KIFACE* KIWAY::m_kiface[KIWAY_FACE_COUNT];
int     KIWAY::m_kiface_version[KIWAY_FACE_COUNT];


Dick Hollenbeck's avatar
Dick Hollenbeck committed
43

44
KIWAY::KIWAY( PGM_BASE* aProgram, int aCtlBits, wxFrame* aTop ):
45
    m_program( aProgram ),
46
    m_ctl( aCtlBits ),
47
    m_top( 0 )
48
{
49
    SetTop( aTop );     // hook player_destroy_handler() into aTop.
50

51
    memset( m_player, 0, sizeof( m_player ) );
52 53
}

54

55
// Any event types derived from wxCommandEvt, like wxWindowDestroyEvent, are
56
// propogated upwards to parent windows if not handled below.  Therefore the
57
// m_top window should receive all wxWindowDestroyEvents originating from
58
// KIWAY_PLAYERs.  It does anyways, but now player_destroy_handler eavesdrops
59 60
// on that event stream looking for KIWAY_PLAYERs being closed.

61
void KIWAY::player_destroy_handler( wxWindowDestroyEvent& event )
62 63 64 65 66
{
    wxWindow* w = event.GetWindow();

    for( unsigned i=0; i<DIM(m_player);  ++i )
    {
67
        // if destroying one of our flock, then mark it as deceased.
68 69
        if( (wxWindow*) m_player[i] == w )
        {
70 71 72
            DBG(printf( "%s: m_player[%d] destroyed: %s\n",
                __func__, i, TO_UTF8( m_player[i]->GetName() ) );)

73 74 75
            m_player[i] = 0;
        }
    }
76 77

    // event.Skip();  skip to who, the wxApp?  I'm the top window.
78 79 80 81 82 83 84
}


void KIWAY::SetTop( wxFrame* aTop )
{
    if( m_top )
    {
85
        m_top->Disconnect( wxEVT_DESTROY, wxWindowDestroyEventHandler( KIWAY::player_destroy_handler ), NULL, this );
86 87 88 89
    }

    if( aTop )
    {
90
        aTop->Connect( wxEVT_DESTROY, wxWindowDestroyEventHandler( KIWAY::player_destroy_handler ), NULL, this );
91 92 93 94 95
    }

    m_top = aTop;
}

96

97
const wxString KIWAY::dso_full_path( FACE_T aFaceId )
98
{
99
    const wxChar*   name;
100

101 102
    switch( aFaceId )
    {
103 104 105 106 107 108 109
    case FACE_SCH:              name = KIFACE_PREFIX wxT( "eeschema" );         break;
    case FACE_PCB:              name = KIFACE_PREFIX wxT( "pcbnew" );           break;
    case FACE_CVPCB:            name = KIFACE_PREFIX wxT( "cvpcb" );            break;
    case FACE_GERBVIEW:         name = KIFACE_PREFIX wxT( "gerbview" );         break;
    case FACE_PL_EDITOR:        name = KIFACE_PREFIX wxT( "pl_editor" );        break;
    case FACE_PCB_CALCULATOR:   name = KIFACE_PREFIX wxT( "pcb_calculator" );   break;
    case FACE_BMP2CMP:          name = KIFACE_PREFIX wxT( "bitmap2component" ); break;
110 111 112 113 114

    default:
        wxASSERT_MSG( 0, wxT( "caller has a bug, passed a bad aFaceId" ) );
        return wxEmptyString;
    }
115 116 117 118 119 120 121 122 123 124

    wxFileName fn = wxStandardPaths::Get().GetExecutablePath();

    fn.SetName( name );

    // Here a "suffix" == an extension with a preceding '.',
    // so skip the preceding '.' to get an extension
    fn.SetExt( KIFACE_SUFFIX + 1 );         // + 1 => &KIFACE_SUFFIX[1]

    return fn.GetFullPath();
125 126 127
}


128
PROJECT& KIWAY::Prj() const
129
{
130
    return *(PROJECT*) &m_project;      // strip const-ness, function really is const.
131 132 133
}


134
KIFACE*  KIWAY::KiFACE( FACE_T aFaceId, bool doLoad )
135
{
136 137 138
    // Since this will be called from python, cannot assume that code will
    // not pass a bad aFaceId.
    if( unsigned( aFaceId ) >= DIM( m_kiface ) )
139
    {
140 141
        // @todo : throw an exception here for python's benefit, at least that
        // way it gets some explanatory text.
142 143 144 145 146

        wxASSERT_MSG( 0, wxT( "caller has a bug, passed a bad aFaceId" ) );
        return NULL;
    }

147 148 149 150 151
    // return the previously loaded KIFACE, if it was.
    if( m_kiface[aFaceId] )
        return m_kiface[aFaceId];

    // DSO with KIFACE has not been loaded yet, does caller want to load it?
152 153
    if( doLoad  )
    {
154 155 156 157 158 159
        wxString dname = dso_full_path( aFaceId );

        wxDynamicLibrary dso;

        void*   addr = NULL;

160
        if( !dso.Load( dname, wxDL_VERBATIM | wxDL_NOW | wxDL_GLOBAL ) )
161
        {
162 163 164
            // Failure: error reporting UI was done via wxLogSysError().
            // No further reporting required here.
        }
165

166 167 168 169 170
        else if( ( addr = dso.GetSymbol( wxT( KIFACE_INSTANCE_NAME_AND_VERSION ) ) ) == NULL )
        {
            // Failure: error reporting UI was done via wxLogSysError().
            // No further reporting required here.
        }
171

172 173 174 175
        else
        {
            KIFACE_GETTER_FUNC* getter = (KIFACE_GETTER_FUNC*) addr;

176
            KIFACE* kiface = getter( &m_kiface_version[aFaceId], KIFACE_VERSION, m_program );
177 178 179 180 181 182 183

            // KIFACE_GETTER_FUNC function comment (API) says the non-NULL is unconditional.
            wxASSERT_MSG( kiface,
                wxT( "attempted DSO has a bug, failed to return a KIFACE*" ) );

            // Give the DSO a single chance to do its "process level" initialization.
            // "Process level" specifically means stay away from any projects in there.
184
            if( kiface->OnKifaceStart( m_program, m_ctl ) )
185 186 187 188 189 190
            {
                // Tell dso's wxDynamicLibrary destructor not to Unload() the program image.
                (void) dso.Detach();

                return m_kiface[aFaceId] = kiface;
            }
191
        }
192 193 194

        // In any of the failure cases above, dso.Unload() should be called here
        // by dso destructor.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
        // However:

        // There is a file installation bug. We only look for KIFACE_I's which we know
        // to exist, and we did not find one.  If we do not find one, this is an
        // installation bug.

        wxString msg = wxString::Format( wxT(
            "Fatal Installation Bug\nmissing file:\n'%s'\n\nargv[0]:\n'%s'" ),
            GetChars( dname ),
            GetChars( wxStandardPaths::Get().GetExecutablePath() )
            );

        // This is a fatal error, one from which we cannot recover, nor do we want
        // to protect against in client code which would require numerous noisy
        // tests in numerous places.  So we inform the user that the installation
210 211
        // is bad.  This exception will likely not get caught until way up in the
        // wxApp derivative, at which point the process will exit gracefully.
212
        THROW_IO_ERROR( msg );
213 214 215 216
    }

    return NULL;
}
217 218 219 220 221 222 223 224 225


KIWAY::FACE_T KIWAY::KifaceType( FRAME_T aFrameType )
{
    switch( aFrameType )
    {
    case FRAME_SCH:
    case FRAME_SCH_LIB_EDITOR:
    case FRAME_SCH_VIEWER:
226
    case FRAME_SCH_VIEWER_MODAL:
227 228 229 230 231
        return FACE_SCH;

    case FRAME_PCB:
    case FRAME_PCB_MODULE_EDITOR:
    case FRAME_PCB_MODULE_VIEWER:
232 233
    case FRAME_PCB_MODULE_VIEWER_MODAL:
    case FRAME_PCB_FOOTPRINT_WIZARD_MODAL:
234 235 236 237 238 239 240 241 242 243 244 245 246
    case FRAME_PCB_DISPLAY3D:
        return FACE_PCB;

    case FRAME_CVPCB:
    case FRAME_CVPCB_DISPLAY:
        return FACE_CVPCB;

    case FRAME_GERBER:
        return FACE_GERBVIEW;

    case FRAME_PL_EDITOR:
        return FACE_PL_EDITOR;

247 248 249 250 251 252
    case FRAME_CALC:
        return FACE_PCB_CALCULATOR;

    case FRAME_BM2CMP:
        return FACE_BMP2CMP;

253 254 255 256 257 258
    default:
        return FACE_T( -1 );
    }
}


259
KIWAY_PLAYER* KIWAY::Player( FRAME_T aFrameType, bool doCreate )
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
{
    // Since this will be called from python, cannot assume that code will
    // not pass a bad aFrameType.
    if( unsigned( aFrameType ) >= DIM( m_player ) )
    {
        // @todo : throw an exception here for python's benefit, at least that
        // way it gets some explanatory text.

        wxASSERT_MSG( 0, wxT( "caller has a bug, passed a bad aFrameType" ) );
        return NULL;
    }

    // return the previously opened window
    if( m_player[aFrameType] )
        return m_player[aFrameType];

276 277 278 279
    if( doCreate )
    {
        FACE_T face_type = KifaceType( aFrameType );
        wxASSERT( face_type != FACE_T(-1) );
280

281
        KIFACE* kiface = KiFACE( face_type );
282 283
        wxASSERT( kiface );

284 285
        if( kiface )
        {
286 287 288 289 290 291
            KIWAY_PLAYER* frame = (KIWAY_PLAYER*) kiface->CreateWindow(
                    m_top,
                    aFrameType,
                    this,
                    m_ctl    // questionable need, these same flags where passed to the KIFACE::OnKifaceStart()
                    );
292
            wxASSERT( frame );
293

294 295
            return m_player[aFrameType] = frame;
        }
296
    }
297

298
    return NULL;
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
}


bool KIWAY::PlayerClose( FRAME_T aFrameType, bool doForce )
{
    // Since this will be called from python, cannot assume that code will
    // not pass a bad aFrameType.
    if( unsigned( aFrameType ) >= DIM( m_player ) )
    {
        // @todo : throw an exception here for python's benefit, at least that
        // way it gets some explanatory text.

        wxASSERT_MSG( 0, wxT( "caller has a bug, passed a bad aFrameType" ) );
        return false;
    }

    if( m_player[aFrameType] )
    {
        if( m_player[aFrameType]->Close( doForce ) )
        {
            m_player[aFrameType] = 0;
            return true;
        }

        return false;
    }

    return true;    // window is closed already.
}


bool KIWAY::PlayersClose( bool doForce )
{
    bool ret = true;

    for( unsigned i=0; i < DIM( m_player );  ++i )
    {
        ret = ret && PlayerClose( FRAME_T( i ), doForce );
    }

    return ret;
}
Dick Hollenbeck's avatar
Dick Hollenbeck committed
341 342 343


void KIWAY::ExpressMail( FRAME_T aDestination,
344
                MAIL_T aCommand, const std::string& aPayload, wxWindow* aSource )
Dick Hollenbeck's avatar
Dick Hollenbeck committed
345 346 347 348 349 350 351
{
    KIWAY_EXPRESS   mail( aDestination, aCommand, aPayload, aSource );

    ProcessEvent( mail );
}


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
void KIWAY::SetLanguage( int aLanguage )
{
    Pgm().SetLanguageIdentifier( aLanguage );
    Pgm().SetLanguage();

#if 1
    // This is a risky hack that goes away if we allow the language to be
    // set only from the top most frame if !Kiface.IsSingle()

    // Only for the C++ project manager, and not for the python one and not for
    // single_top do we look for the EDA_BASE_FRAME as the top level window.
    // For single_top this is not needed because that window is registered in
    // the array below.
    if( m_ctl & KFCTL_CPP_PROJECT_SUITE )
    {
        EDA_BASE_FRAME* top = (EDA_BASE_FRAME*) m_top;
        if( top )
            top->ShowChangedLanguage();
    }
#endif

    for( unsigned i=0;  i < DIM( m_player );  ++i )
    {
        KIWAY_PLAYER* frame = m_player[i];

        if( frame )
        {
            frame->ShowChangedLanguage();
        }
    }
}


Dick Hollenbeck's avatar
Dick Hollenbeck committed
385 386 387 388 389 390 391 392 393 394 395 396
bool KIWAY::ProcessEvent( wxEvent& aEvent )
{
    KIWAY_EXPRESS* mail = dynamic_cast<KIWAY_EXPRESS*>( &aEvent );

    if( mail )
    {
        FRAME_T dest = mail->Dest();

        // see if recipient is alive
        KIWAY_PLAYER* alive = Player( dest, false );

        if( alive )
397
        {
Dick Hollenbeck's avatar
Dick Hollenbeck committed
398
#if 1
Dick Hollenbeck's avatar
Dick Hollenbeck committed
399
            return alive->ProcessEvent( aEvent );
400 401 402 403 404
#else
            alive->KiwayMailIn( *mail );
            return true;
#endif
        }
Dick Hollenbeck's avatar
Dick Hollenbeck committed
405 406 407 408
    }

    return false;
}
409 410 411 412 413 414 415 416 417 418 419


void KIWAY::OnKiwayEnd()
{
    for( unsigned i=0;  i < DIM( m_kiface );  ++i )
    {
        if( m_kiface[i] )
            m_kiface[i]->OnKifaceEnd();
    }
}