draw_panel_gal.cpp 8.44 KB
Newer Older
1 2 3
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
4
 * Copyright (C) 2013-2015 CERN
5
 * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6
 * @author Maciej Suminski <maciej.suminski@cern.ch>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * 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 <wx/wx.h>
#include <wx/frame.h>
#include <wx/window.h>
#include <wx/event.h>
#include <wx/colour.h>
31
#include <wx/filename.h>
32
#include <confirm.h>
33

34
#include <class_draw_panel_gal.h>
35 36 37 38 39 40 41 42
#include <view/view.h>
#include <view/wx_view_controls.h>
#include <pcb_painter.h>

#include <gal/graphics_abstraction_layer.h>
#include <gal/opengl/opengl_gal.h>
#include <gal/cairo/cairo_gal.h>

43 44 45
#include <tool/tool_dispatcher.h>
#include <tool/tool_manager.h>

46 47
#include <boost/foreach.hpp>

48 49 50 51
#ifdef __WXDEBUG__
#include <profile.h>
#endif /* __WXDEBUG__ */

52 53 54
EDA_DRAW_PANEL_GAL::EDA_DRAW_PANEL_GAL( wxWindow* aParentWindow, wxWindowID aWindowId,
                                        const wxPoint& aPosition, const wxSize& aSize,
                                        GalType aGalType ) :
55
    wxWindow( aParentWindow, aWindowId, aPosition, aSize )
56
{
57
    m_parent     = aParentWindow;
58
    m_gal        = NULL;
59
    m_backend    = GAL_TYPE_NONE;
60 61
    m_view       = NULL;
    m_painter    = NULL;
62
    m_eventDispatcher = NULL;
63

64
    SwitchBackend( aGalType );
65 66
    SetBackgroundStyle( wxBG_STYLE_CUSTOM );

67
    m_painter = new KIGFX::PCB_PAINTER( m_gal );
68

69
    m_view = new KIGFX::VIEW( true );
70
    m_view->SetPainter( m_painter );
71 72
    m_view->SetGAL( m_gal );

73
    m_viewControls = new KIGFX::WX_VIEW_CONTROLS( m_view, this );
74

75 76
    Connect( wxEVT_SIZE, wxSizeEventHandler( EDA_DRAW_PANEL_GAL::onSize ), NULL, this );
    Connect( wxEVT_ENTER_WINDOW, wxEventHandler( EDA_DRAW_PANEL_GAL::onEnter ), NULL, this );
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91
    const wxEventType events[] =
    {
        wxEVT_LEFT_UP, wxEVT_LEFT_DOWN, wxEVT_LEFT_DCLICK,
        wxEVT_RIGHT_UP, wxEVT_RIGHT_DOWN, wxEVT_RIGHT_DCLICK,
        wxEVT_MIDDLE_UP, wxEVT_MIDDLE_DOWN, wxEVT_MIDDLE_DCLICK,
        wxEVT_MOTION, wxEVT_MOUSEWHEEL, wxEVT_CHAR, KIGFX::WX_VIEW_CONTROLS::EVT_REFRESH_MOUSE
    };

    BOOST_FOREACH( wxEventType eventType, events )
    {
        Connect( eventType, wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ),
                 NULL, m_eventDispatcher );
    }

92
    // Set up timer that prevents too frequent redraw commands
93
    m_refreshTimer.SetOwner( this );
94 95
    m_pendingRefresh = false;
    m_drawing = false;
96
    Connect( wxEVT_TIMER, wxTimerEventHandler( EDA_DRAW_PANEL_GAL::onRefreshTimer ), NULL, this );
97 98 99 100 101
}


EDA_DRAW_PANEL_GAL::~EDA_DRAW_PANEL_GAL()
{
Maciej Suminski's avatar
Maciej Suminski committed
102 103 104 105
    delete m_painter;
    delete m_viewControls;
    delete m_view;
    delete m_gal;
106 107 108
}


109
void EDA_DRAW_PANEL_GAL::onPaint( wxPaintEvent& WXUNUSED( aEvent ) )
Maciej Suminski's avatar
Maciej Suminski committed
110
{
111 112 113
    m_pendingRefresh = false;
    m_lastRefresh = wxGetLocalTimeMillis();

114 115
    if( m_drawing )
        return;
116

117
    m_drawing = true;
118

119 120 121
    m_view->UpdateItems();
    m_gal->BeginDrawing();
    m_gal->ClearScreen( m_painter->GetSettings()->GetBackgroundColor() );
Maciej Suminski's avatar
Maciej Suminski committed
122

123 124 125 126 127 128
    if( m_view->IsDirty() )
    {
        m_view->ClearTargets();

        // Grid has to be redrawn only when the NONCACHED target is redrawn
        if( m_view->IsTargetDirty( KIGFX::TARGET_NONCACHED ) )
Maciej Suminski's avatar
Maciej Suminski committed
129
                m_gal->DrawGrid();
Maciej Suminski's avatar
Maciej Suminski committed
130

131 132
        m_view->Redraw();
    }
Maciej Suminski's avatar
Maciej Suminski committed
133

134 135
    m_gal->DrawCursor( m_viewControls->GetCursorPosition() );
    m_gal->EndDrawing();
Maciej Suminski's avatar
Maciej Suminski committed
136

137
    m_drawing = false;
Maciej Suminski's avatar
Maciej Suminski committed
138 139 140
}


141 142 143
void EDA_DRAW_PANEL_GAL::onSize( wxSizeEvent& aEvent )
{
    m_gal->ResizeScreen( aEvent.GetSize().x, aEvent.GetSize().y );
144 145
    m_view->MarkTargetDirty( KIGFX::TARGET_CACHED );
    m_view->MarkTargetDirty( KIGFX::TARGET_NONCACHED );
146 147 148
}


149
void EDA_DRAW_PANEL_GAL::onRefreshTimer( wxTimerEvent& aEvent )
150 151 152 153 154 155
{
    wxPaintEvent redrawEvent;
    wxPostEvent( this, redrawEvent );
}


156
void EDA_DRAW_PANEL_GAL::Refresh( bool aEraseBackground, const wxRect* aRect )
157
{
Maciej Suminski's avatar
Maciej Suminski committed
158
    if( m_pendingRefresh )
159 160 161 162 163
        return;

    wxLongLong t = wxGetLocalTimeMillis();
    wxLongLong delta = t - m_lastRefresh;

Maciej Suminski's avatar
Maciej Suminski committed
164
    if( delta >= MinRefreshPeriod )
165
    {
166
        ForceRefresh();
167
        m_pendingRefresh = true;
Maciej Suminski's avatar
Maciej Suminski committed
168 169 170 171 172
    }
    else
    {
        // One shot timer
        m_refreshTimer.Start( ( MinRefreshPeriod - delta ).ToLong(), true );
173 174
        m_pendingRefresh = true;
    }
175
}
176 177


178 179 180 181 182 183 184
void EDA_DRAW_PANEL_GAL::ForceRefresh()
{
    wxPaintEvent redrawEvent;
    wxPostEvent( this, redrawEvent );
}


185 186 187 188
void EDA_DRAW_PANEL_GAL::SetEventDispatcher( TOOL_DISPATCHER* aEventDispatcher )
{
    m_eventDispatcher = aEventDispatcher;

189
#if wxCHECK_VERSION( 3, 0, 0 )
Maciej Suminski's avatar
Maciej Suminski committed
190
    const wxEventType eventTypes[] = { wxEVT_TOOL };
191
#else
Maciej Suminski's avatar
Maciej Suminski committed
192 193 194
    const wxEventType eventTypes[] = { wxEVT_COMMAND_MENU_SELECTED, wxEVT_COMMAND_TOOL_CLICKED };
#endif

195 196
    if( m_eventDispatcher )
    {
Maciej Suminski's avatar
Maciej Suminski committed
197 198 199 200 201
        BOOST_FOREACH( wxEventType type, eventTypes )
        {
            m_parent->Connect( type, wxCommandEventHandler( TOOL_DISPATCHER::DispatchWxCommand ),
                               NULL, m_eventDispatcher );
        }
202 203 204
    }
    else
    {
Maciej Suminski's avatar
Maciej Suminski committed
205 206 207 208 209 210 211
        BOOST_FOREACH( wxEventType type, eventTypes )
        {
            // While loop is used to be sure that all event handlers are removed.
            while( m_parent->Disconnect( type,
                                         wxCommandEventHandler( TOOL_DISPATCHER::DispatchWxCommand ),
                                         NULL, m_eventDispatcher ) );
        }
212 213 214 215
    }
}


216 217
void EDA_DRAW_PANEL_GAL::StartDrawing()
{
218 219
    m_drawing = false;
    m_pendingRefresh = true;
220 221 222 223 224 225 226
    Connect( wxEVT_PAINT, wxPaintEventHandler( EDA_DRAW_PANEL_GAL::onPaint ), NULL, this );

    wxPaintEvent redrawEvent;
    wxPostEvent( this, redrawEvent );
}


227 228
void EDA_DRAW_PANEL_GAL::StopDrawing()
{
229 230
    m_pendingRefresh = false;
    m_drawing = true;
231
    m_refreshTimer.Stop();
232
    Disconnect( wxEVT_PAINT, wxPaintEventHandler( EDA_DRAW_PANEL_GAL::onPaint ), NULL, this );
233 234 235
}


236
void EDA_DRAW_PANEL_GAL::SetHighContrastLayer( LAYER_ID aLayer )
237 238 239 240 241 242 243 244 245 246 247 248 249
{
    // Set display settings for high contrast mode
    KIGFX::RENDER_SETTINGS* rSettings = m_view->GetPainter()->GetSettings();

    SetTopLayer( aLayer );

    rSettings->ClearActiveLayers();
    rSettings->SetActiveLayer( aLayer );

    m_view->UpdateAllLayersColor();
}


250
void EDA_DRAW_PANEL_GAL::SetTopLayer( LAYER_ID aLayer )
251 252 253 254 255 256 257
{
    m_view->ClearTopLayers();
    m_view->SetTopLayer( aLayer );
    m_view->UpdateAllLayersOrder();
}


258
bool EDA_DRAW_PANEL_GAL::SwitchBackend( GalType aGalType )
259
{
260
    // Do not do anything if the currently used GAL is correct
261
    if( aGalType == m_backend && m_gal != NULL )
262
        return true;
263

264
    // Prevent refreshing canvas during backend switch
265
    StopDrawing();
266

267
    KIGFX::GAL* new_gal = NULL;
268

269
    try
270
    {
271 272 273 274 275
        switch( aGalType )
        {
        case GAL_TYPE_OPENGL:
            new_gal = new KIGFX::OPENGL_GAL( this, this, this );
            break;
276

277 278 279
        case GAL_TYPE_CAIRO:
            new_gal = new KIGFX::CAIRO_GAL( this, this, this );
            break;
280

281 282 283
        case GAL_TYPE_NONE:
            return false;
        }
284

285 286
        delete m_gal;
        m_gal = new_gal;
287

288 289
        wxSize size = GetClientSize();
        m_gal->ResizeScreen( size.GetX(), size.GetY() );
290

291 292 293 294 295 296 297 298
        if( m_painter )
            m_painter->SetGAL( m_gal );

        if( m_view )
            m_view->SetGAL( m_gal );

        m_backend = aGalType;
    }
Maciej Suminski's avatar
Maciej Suminski committed
299
    catch( std::runtime_error& err )
300 301 302 303
    {
            DisplayError( m_parent, wxString( err.what() ) );
            return false;
    }
304

305
    return true;
306
}
307

Maciej Suminski's avatar
Maciej Suminski committed
308

309 310 311 312 313 314 315 316 317 318 319
void EDA_DRAW_PANEL_GAL::onEvent( wxEvent& aEvent )
{
    if( !m_eventDispatcher )
        aEvent.Skip();
    else
        m_eventDispatcher->DispatchWxEvent( aEvent );

    Refresh();
}


320
void EDA_DRAW_PANEL_GAL::onEnter( wxEvent& aEvent )
321
{
322
    // Getting focus is necessary in order to receive key events properly
323 324
    SetFocus();
}