1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
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
341
342
343
344
345
346
347
348
349
350
351
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2013-2015 CERN
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
* @author Maciej Suminski <maciej.suminski@cern.ch>
*
* 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>
#include <wx/filename.h>
#include <confirm.h>
#include <class_draw_panel_gal.h>
#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>
#include <tool/tool_dispatcher.h>
#include <tool/tool_manager.h>
#include <boost/foreach.hpp>
#ifdef __WXDEBUG__
#include <profile.h>
#endif /* __WXDEBUG__ */
EDA_DRAW_PANEL_GAL::EDA_DRAW_PANEL_GAL( wxWindow* aParentWindow, wxWindowID aWindowId,
const wxPoint& aPosition, const wxSize& aSize,
GAL_TYPE aGalType ) :
wxScrolledCanvas( aParentWindow, aWindowId, aPosition, aSize )
{
m_parent = aParentWindow;
m_gal = NULL;
m_backend = GAL_TYPE_NONE;
m_view = NULL;
m_painter = NULL;
m_eventDispatcher = NULL;
m_lostFocus = false;
SetLayoutDirection( wxLayout_LeftToRight );
SwitchBackend( aGalType );
SetBackgroundStyle( wxBG_STYLE_CUSTOM );
// Scrollbars broken in GAL on OSX
#ifdef __WXMAC__
ShowScrollbars( wxSHOW_SB_NEVER, wxSHOW_SB_NEVER );
#else
ShowScrollbars( wxSHOW_SB_ALWAYS, wxSHOW_SB_ALWAYS );
#endif
EnableScrolling( false, false ); // otherwise Zoom Auto disables GAL canvas
m_painter = new KIGFX::PCB_PAINTER( m_gal );
m_view = new KIGFX::VIEW( true );
m_view->SetPainter( m_painter );
m_view->SetGAL( m_gal );
Connect( wxEVT_SIZE, wxSizeEventHandler( EDA_DRAW_PANEL_GAL::onSize ), NULL, this );
Connect( wxEVT_ENTER_WINDOW, wxEventHandler( EDA_DRAW_PANEL_GAL::onEnter ), NULL, this );
Connect( wxEVT_KILL_FOCUS, wxFocusEventHandler( EDA_DRAW_PANEL_GAL::onLostFocus ), NULL, this );
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,
#ifdef USE_OSX_MAGNIFY_EVENT
wxEVT_MAGNIFY,
#endif
KIGFX::WX_VIEW_CONTROLS::EVT_REFRESH_MOUSE
};
BOOST_FOREACH( wxEventType eventType, events )
{
Connect( eventType, wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ),
NULL, m_eventDispatcher );
}
// View controls is the first in the event handler chain, so the Tool Framework operates
// on updated viewport data.
m_viewControls = new KIGFX::WX_VIEW_CONTROLS( m_view, this );
// Set up timer that prevents too frequent redraw commands
m_refreshTimer.SetOwner( this );
m_pendingRefresh = false;
m_drawing = false;
m_drawingEnabled = false;
Connect( wxEVT_TIMER, wxTimerEventHandler( EDA_DRAW_PANEL_GAL::onRefreshTimer ), NULL, this );
}
EDA_DRAW_PANEL_GAL::~EDA_DRAW_PANEL_GAL()
{
delete m_painter;
delete m_viewControls;
delete m_view;
delete m_gal;
}
void EDA_DRAW_PANEL_GAL::SetFocus()
{
// Windows has a strange manner on bringing up and activating windows
// containing a GAL canvas just after moving the mouse cursor into its area.
// Feel free to uncomment or extend the following #ifdef if you experience
// similar problems on your platform.
#ifdef __WINDOWS__
if( !GetParent()->IsDescendant( wxWindow::FindFocus() ) )
return;
#endif
wxScrolledCanvas::SetFocus();
m_lostFocus = false;
}
void EDA_DRAW_PANEL_GAL::onPaint( wxPaintEvent& WXUNUSED( aEvent ) )
{
m_pendingRefresh = false;
if( m_drawing )
return;
m_drawing = true;
// Scrollbars broken in GAL on OSX
#ifndef __WXMAC__
m_viewControls->UpdateScrollbars();
#endif
m_view->UpdateItems();
m_gal->BeginDrawing();
m_gal->ClearScreen( m_painter->GetSettings()->GetBackgroundColor() );
KIGFX::COLOR4D gridColor = static_cast<KIGFX::PCB_RENDER_SETTINGS*> (m_painter->GetSettings())->GetLayerColor( ITEM_GAL_LAYER ( GRID_VISIBLE ) );
m_gal->SetGridColor ( gridColor );
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 ) )
m_gal->DrawGrid();
m_view->Redraw();
}
m_gal->DrawCursor( m_viewControls->GetCursorPosition() );
m_gal->EndDrawing();
m_lastRefresh = wxGetLocalTimeMillis();
m_drawing = false;
}
void EDA_DRAW_PANEL_GAL::onSize( wxSizeEvent& aEvent )
{
m_gal->ResizeScreen( aEvent.GetSize().x, aEvent.GetSize().y );
m_view->MarkTargetDirty( KIGFX::TARGET_CACHED );
m_view->MarkTargetDirty( KIGFX::TARGET_NONCACHED );
}
void EDA_DRAW_PANEL_GAL::Refresh( bool aEraseBackground, const wxRect* aRect )
{
if( m_pendingRefresh )
return;
m_pendingRefresh = true;
#ifdef __WXMAC__
// Timers on OS X may have a high latency (seen up to 500ms and more) which
// makes repaints jerky. No negative impact seen without throttling, so just
// do an unconditional refresh for OS X.
ForceRefresh();
#else
wxLongLong t = wxGetLocalTimeMillis();
wxLongLong delta = t - m_lastRefresh;
if( delta >= MinRefreshPeriod )
{
ForceRefresh();
}
else
{
// One shot timer
m_refreshTimer.Start( ( MinRefreshPeriod - delta ).ToLong(), true );
}
#endif
}
void EDA_DRAW_PANEL_GAL::ForceRefresh()
{
wxPaintEvent redrawEvent;
wxPostEvent( this, redrawEvent );
}
void EDA_DRAW_PANEL_GAL::SetEventDispatcher( TOOL_DISPATCHER* aEventDispatcher )
{
m_eventDispatcher = aEventDispatcher;
const wxEventType eventTypes[] = { wxEVT_TOOL };
if( m_eventDispatcher )
{
BOOST_FOREACH( wxEventType type, eventTypes )
{
m_parent->Connect( type, wxCommandEventHandler( TOOL_DISPATCHER::DispatchWxCommand ),
NULL, m_eventDispatcher );
}
}
else
{
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 ) );
}
}
}
void EDA_DRAW_PANEL_GAL::StartDrawing()
{
// Start querying GAL if it is ready
m_refreshTimer.StartOnce( 100 );
}
void EDA_DRAW_PANEL_GAL::StopDrawing()
{
m_drawingEnabled = false;
Disconnect( wxEVT_PAINT, wxPaintEventHandler( EDA_DRAW_PANEL_GAL::onPaint ), NULL, this );
m_pendingRefresh = false;
m_drawing = true;
m_refreshTimer.Stop();
}
void EDA_DRAW_PANEL_GAL::SetHighContrastLayer( LAYER_ID aLayer )
{
// 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();
}
void EDA_DRAW_PANEL_GAL::SetTopLayer( LAYER_ID aLayer )
{
m_view->ClearTopLayers();
m_view->SetTopLayer( aLayer );
m_view->UpdateAllLayersOrder();
}
double EDA_DRAW_PANEL_GAL::GetLegacyZoom() const
{
double zoomFactor = m_gal->GetWorldScale() / m_gal->GetZoomFactor();
return ( 1.0 / ( zoomFactor * m_view->GetScale() ) );
}
bool EDA_DRAW_PANEL_GAL::SwitchBackend( GAL_TYPE aGalType )
{
// Do not do anything if the currently used GAL is correct
if( aGalType == m_backend && m_gal != NULL )
return true;
bool result = true; // assume everything will be fine
// Prevent refreshing canvas during backend switch
StopDrawing();
KIGFX::GAL* new_gal = NULL;
try
{
switch( aGalType )
{
case GAL_TYPE_OPENGL:
new_gal = new KIGFX::OPENGL_GAL( this, this, this );
break;
case GAL_TYPE_CAIRO:
new_gal = new KIGFX::CAIRO_GAL( this, this, this );
break;
default:
assert( false );
// warn about unhandled GAL canvas type, but continue with the fallback option
case GAL_TYPE_NONE:
// KIGFX::GAL is a stub - it actually does cannot display anything,
// but prevents code relying on GAL canvas existence from crashing
new_gal = new KIGFX::GAL();
break;
}
}
catch( std::runtime_error& err )
{
new_gal = new KIGFX::GAL();
aGalType = GAL_TYPE_NONE;
DisplayError( m_parent, wxString( err.what() ) );
result = false;
}
assert( new_gal );
delete m_gal;
m_gal = new_gal;
wxSize size = GetClientSize();
m_gal->ResizeScreen( size.GetX(), size.GetY() );
if( m_painter )
m_painter->SetGAL( m_gal );
if( m_view )
m_view->SetGAL( m_gal );
m_backend = aGalType;
return result;
}
void EDA_DRAW_PANEL_GAL::onEvent( wxEvent& aEvent )
{
if( m_lostFocus )
SetFocus();
if( !m_eventDispatcher )
aEvent.Skip();
else
m_eventDispatcher->DispatchWxEvent( aEvent );
Refresh();
}
void EDA_DRAW_PANEL_GAL::onEnter( wxEvent& aEvent )
{
// Getting focus is necessary in order to receive key events properly
SetFocus();
aEvent.Skip();
}
void EDA_DRAW_PANEL_GAL::onLostFocus( wxFocusEvent& aEvent )
{
m_lostFocus = true;
aEvent.Skip();
}
void EDA_DRAW_PANEL_GAL::onRefreshTimer( wxTimerEvent& aEvent )
{
if( !m_drawingEnabled )
{
if( m_gal->IsInitialized() )
{
m_drawing = false;
m_pendingRefresh = true;
Connect( wxEVT_PAINT, wxPaintEventHandler( EDA_DRAW_PANEL_GAL::onPaint ), NULL, this );
m_drawingEnabled = true;
}
else
{
// Try again soon
m_refreshTimer.Start( 100, true );
return;
}
}
wxPaintEvent redrawEvent;
wxPostEvent( this, redrawEvent );
}
|