KiCad PCB EDA Suite
Loading...
Searching...
No Matches
cursors.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 The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <vector>
25
26#include <gal/cursors.h>
27#include <kiplatform/ui.h>
28
29// Cursor files
30#include <cursors/cursor-add.xpm>
31#include <cursors/cursor-component.xpm>
32#include <cursors/cursor-eraser.xpm>
33#include <cursors/cursor-label-global.xpm>
34#include <cursors/cursor-label-hier.xpm>
35#include <cursors/cursor-label-net.xpm>
36#include <cursors/cursor-line-bus.xpm>
37#include <cursors/cursor-line-graphic.xpm>
38#include <cursors/cursor-line-wire.xpm>
39#include <cursors/cursor-measure.xpm>
40#include <cursors/cursor-pencil.xpm>
41#include <cursors/cursor-select-lasso.xpm>
42#include <cursors/cursor-select-window.xpm>
43#include <cursors/cursor-subtract.xpm>
44#include <cursors/cursor-text.xpm>
45#include <cursors/cursor-xor.xpm>
46#include <cursors/cursor-zoom-in.xpm>
47#include <cursors/cursor-zoom-out.xpm>
48#include <cursors/cursor_tune.xpm>
49#include <cursors/voltage_probe.xpm>
50#include <cursors/current_probe.xpm>
51
52// HiDPI cursor files
53#include <cursors/cursor-add64.xpm>
54#include <cursors/cursor-component64.xpm>
55#include <cursors/cursor-eraser64.xpm>
56#include <cursors/cursor-label-global64.xpm>
57#include <cursors/cursor-label-hier64.xpm>
58#include <cursors/cursor-label-net64.xpm>
59#include <cursors/cursor-line-bus64.xpm>
60#include <cursors/cursor-line-graphic64.xpm>
61#include <cursors/cursor-line-wire64.xpm>
62#include <cursors/cursor-measure64.xpm>
63#include <cursors/cursor-pencil64.xpm>
64#include <cursors/cursor-select-lasso64.xpm>
65#include <cursors/cursor-select-window64.xpm>
66#include <cursors/cursor-subtract64.xpm>
67#include <cursors/cursor-text64.xpm>
68#include <cursors/cursor-xor64.xpm>
69#include <cursors/cursor-zoom-in64.xpm>
70#include <cursors/cursor-zoom-out64.xpm>
71#include <cursors/cursor_tune64.xpm>
72#include <cursors/voltage_probe64.xpm>
73#include <cursors/current_probe64.xpm>
74
75
76// Under MSW, the standard cursor is white on black. Elsewhere it is black on white
77#ifdef __WINDOWS__
78#include <cursors/cursor-place.xpm>
79#include <cursors/cursor-place64.xpm>
80#include <cursors/cursor-select-m.xpm>
81#include <cursors/cursor-select-m64.xpm>
82#else
83#include <cursors/cursor-place-black.xpm>
84#include <cursors/cursor-place-black64.xpm>
85#include <cursors/cursor-select-m-black.xpm>
86#include <cursors/cursor-select-m-black64.xpm>
87#endif
88
89#include <wx/bitmap.h>
90#include <wx/debug.h>
91
92
93static const std::vector<CURSOR_STORE::CURSOR_DEF> standard_cursors = {
94 {
96 nullptr,
97 nullptr,
98 voltage_probe_xpm,
99 { 32, 32 },
100 { 1, 31 },
101 },
102 {
104 nullptr,
105 nullptr,
106 current_probe_xpm,
107 { 32, 32 },
108 { 4, 27 },
109 },
110 {
112 nullptr,
113 nullptr,
114 cursor_tune_xpm,
115 { 32, 32 },
116 { 1, 30 },
117 },
118 {
120 nullptr,
121 nullptr,
122 cursor_pencil_xpm,
123 { 32, 32 },
124 { 4, 27 },
125 },
126 {
128 nullptr,
129 nullptr,
130#ifdef __WINDOWS__
131 cursor_select_m_xpm,
132#else
133 cursor_select_m_black_xpm,
134#endif
135 { 32, 32 },
136 { 1, 1 },
137 },
138 {
140 nullptr,
141 nullptr,
142 cursor_eraser_xpm,
143 { 32, 32 },
144 { 4, 4 },
145 },
146 {
148 nullptr,
149 nullptr,
150 cursor_text_xpm,
151 { 32, 32 },
152 { 7, 7 },
153 },
154 {
156 nullptr,
157 nullptr,
158 cursor_measure_xpm,
159 { 32, 32 },
160 { 4, 4 },
161 },
162 {
164 nullptr,
165 nullptr,
166 cursor_add_xpm,
167 { 32, 32 },
168 { 7, 7 },
169 },
170 {
172 nullptr,
173 nullptr,
174 cursor_subtract_xpm,
175 { 32, 32 },
176 { 7, 7 },
177 },
178 {
180 nullptr,
181 nullptr,
182 cursor_xor_xpm,
183 { 32, 32 },
184 { 7, 7 },
185 },
186 {
188 nullptr,
189 nullptr,
190 cursor_zoom_in_xpm,
191 { 32, 32 },
192 { 6, 6 },
193 },
194 {
196 nullptr,
197 nullptr,
198 cursor_zoom_out_xpm,
199 { 32, 32 },
200 { 6, 6 },
201 },
202 {
204 nullptr,
205 nullptr,
206 cursor_label_net_xpm,
207 { 32, 32 },
208 { 7, 7 },
209 },
210 {
212 nullptr,
213 nullptr,
214 cursor_label_global_xpm,
215 { 32, 32 },
216 { 7, 7 },
217 },
218 {
220 nullptr,
221 nullptr,
222 cursor_component_xpm,
223 { 32, 32 },
224 { 7, 7 },
225 },
226 {
228 nullptr,
229 nullptr,
230 cursor_select_lasso_xpm,
231 { 32, 32 },
232 { 7, 7 },
233 },
234 {
236 nullptr,
237 nullptr,
238 cursor_select_window_xpm,
239 { 32, 32 },
240 { 7, 10 },
241 },
242 {
244 nullptr,
245 nullptr,
246 cursor_line_bus_xpm,
247 { 32, 32 },
248 { 5, 26 },
249 },
250 {
252 nullptr,
253 nullptr,
254 cursor_line_wire_xpm,
255 { 32, 32 },
256 { 5, 26 },
257 },
258 {
260 nullptr,
261 nullptr,
262 cursor_line_graphic_xpm,
263 { 32, 32 },
264 { 5, 26 },
265 },
266 {
268 nullptr,
269 nullptr,
270 cursor_label_hier_xpm,
271 { 32, 32 },
272 { 7, 7 },
273 },
274 {
276 nullptr,
277 nullptr,
278#ifdef __WINDOWS__
279 cursor_place_xpm,
280#else
281 cursor_place_black_xpm,
282#endif
283 { 32, 32 },
284 { 1, 1 },
285 },
286};
287
288
289static const std::vector<CURSOR_STORE::CURSOR_DEF> hidpi_cursors = {
290 {
292 nullptr,
293 nullptr,
294 voltage_probe64_xpm,
295 { 64, 64 },
296 { 1, 62 },
297 },
298 {
300 nullptr,
301 nullptr,
302 current_probe64_xpm,
303 { 64, 64 },
304 { 8, 54 },
305 },
306 {
308 nullptr,
309 nullptr,
310 cursor_tune64_xpm,
311 { 64, 64 },
312 { 2, 60 },
313 },
314 {
316 nullptr,
317 nullptr,
318 cursor_pencil64_xpm,
319 { 64, 64 },
320 { 8, 54 },
321 },
322 {
324 nullptr,
325 nullptr,
326#ifdef __WINDOWS__
327 cursor_select_m64_xpm,
328#else
329 cursor_select_m_black64_xpm,
330#endif
331 { 64, 64 },
332 { 2, 2 },
333 },
334 {
336 nullptr,
337 nullptr,
338 cursor_eraser64_xpm,
339 { 64, 64 },
340 { 8, 8 },
341 },
342 {
344 nullptr,
345 nullptr,
346 cursor_text64_xpm,
347 { 64, 64 },
348 { 14, 14 },
349 },
350 {
352 nullptr,
353 nullptr,
354 cursor_measure64_xpm,
355 { 64, 64 },
356 { 8, 8 },
357 },
358 {
360 nullptr,
361 nullptr,
362 cursor_add64_xpm,
363 { 64, 64 },
364 { 14, 14 },
365 },
366 {
368 nullptr,
369 nullptr,
370 cursor_subtract64_xpm,
371 { 64, 64 },
372 { 14, 14 },
373 },
374 {
376 nullptr,
377 nullptr,
378 cursor_xor64_xpm,
379 { 64, 64 },
380 { 14, 14 },
381 },
382 {
384 nullptr,
385 nullptr,
386 cursor_zoom_in64_xpm,
387 { 64, 64 },
388 { 12, 12 },
389 },
390 {
392 nullptr,
393 nullptr,
394 cursor_zoom_out64_xpm,
395 { 64, 64 },
396 { 12, 12 },
397 },
398 {
400 nullptr,
401 nullptr,
402 cursor_label_net64_xpm,
403 { 64, 64 },
404 { 14, 14 },
405 },
406 {
408 nullptr,
409 nullptr,
410 cursor_label_global64_xpm,
411 { 64, 64 },
412 { 14, 14 },
413 },
414 {
416 nullptr,
417 nullptr,
418 cursor_component64_xpm,
419 { 64, 64 },
420 { 14, 14 },
421 },
422 {
424 nullptr,
425 nullptr,
426 cursor_select_lasso64_xpm,
427 { 64, 64 },
428 { 14, 14 },
429 },
430 {
432 nullptr,
433 nullptr,
434 cursor_select_window64_xpm,
435 { 64, 64 },
436 { 14, 20 },
437 },
438 {
440 nullptr,
441 nullptr,
442 cursor_line_bus64_xpm,
443 { 64, 64 },
444 { 10, 52 },
445 },
446 {
448 nullptr,
449 nullptr,
450 cursor_line_wire64_xpm,
451 { 64, 64 },
452 { 10, 52 },
453 },
454 {
456 nullptr,
457 nullptr,
458 cursor_line_graphic64_xpm,
459 { 64, 64 },
460 { 10, 52 },
461 },
462 {
464 nullptr,
465 nullptr,
466 cursor_label_hier64_xpm,
467 { 64, 64 },
468 { 14, 14 },
469 },
470 {
472 nullptr,
473 nullptr,
474#ifdef __WINDOWS__
475 cursor_place64_xpm,
476#else
477 cursor_place_black64_xpm,
478#endif
479 { 64, 64 },
480 { 2, 2 },
481 },
482};
483
495{
496 if( aDef.m_xpm != nullptr )
497 {
498 wxImage xpmImage = wxImage( aDef.m_xpm );
499
500 xpmImage.SetOption( wxIMAGE_OPTION_CUR_HOTSPOT_X, aDef.m_hotspot.x );
501 xpmImage.SetOption( wxIMAGE_OPTION_CUR_HOTSPOT_Y, aDef.m_hotspot.y );
502
503 return wxCursor( xpmImage );
504 }
505 else if( aDef.m_image_data != nullptr && aDef.m_mask_data != nullptr )
506 {
507#if defined( __WXMSW__ ) || defined( __WXMAC__ )
508
509 wxBitmap img_bitmap(
510 reinterpret_cast<const char*>( aDef.m_image_data ), aDef.m_size.x, aDef.m_size.y );
511 wxBitmap msk_bitmap(
512 reinterpret_cast<const char*>( aDef.m_mask_data ), aDef.m_size.x, aDef.m_size.y );
513 img_bitmap.SetMask( new wxMask( msk_bitmap ) );
514
515 wxImage image( img_bitmap.ConvertToImage() );
516
517#if defined( __WXMSW__ )
518 image.SetMaskColour( 255, 255, 255 );
519#endif
520
521 image.SetOption( wxIMAGE_OPTION_CUR_HOTSPOT_X, aDef.m_hotspot.x );
522 image.SetOption( wxIMAGE_OPTION_CUR_HOTSPOT_Y, aDef.m_hotspot.y );
523
524 return wxCursor{ image };
525
526#elif defined( __WXGTK__ ) || defined( __WXMOTIF__ )
527
528 return wxCursor{
529 reinterpret_cast<const char*>( aDef.m_image_data ),
530 aDef.m_size.x,
531 aDef.m_size.y,
532 aDef.m_hotspot.x,
533 aDef.m_hotspot.y,
534 reinterpret_cast<const char*>( aDef.m_mask_data ),
535 };
536
537#else
538 wxASSERT_MSG( false, wxS( "Unknown platform for cursor construction." ) );
539 return wxNullCursor;
540#endif
541 }
542
543 wxASSERT_MSG( false, wxS( "Unknown to find cursor" ) );
544 return wxNullCursor;
545}
546
547
548CURSOR_STORE::CURSOR_STORE( const std::vector<CURSOR_DEF>& aDefs )
549{
550 for( const auto& def : aDefs )
551 {
552 m_store[def.m_id_key] = constructCursor( def );
553 }
554}
555
556
557const wxCursor& CURSOR_STORE::Get( KICURSOR aIdKey ) const
558{
559 const auto find_iter = m_store.find( aIdKey );
560
561 if( find_iter != m_store.end() )
562 return find_iter->second;
563
564 wxASSERT_MSG( false, wxString::Format( "Could not find cursor with ID %d",
565 static_cast<int>( aIdKey ) ) );
566 return wxNullCursor;
567}
568
569
570const wxCursor CURSOR_STORE::GetCursor( KICURSOR aCursorType )
571{
572 wxStockCursor stock = GetStockCursor( aCursorType );
573
574 if( stock != wxCURSOR_MAX )
575 {
576 return wxCursor( stock );
577 }
578
579 static CURSOR_STORE store( standard_cursors );
580 return store.Get( aCursorType );
581}
582
583
584const wxCursor CURSOR_STORE::GetHiDPICursor( KICURSOR aCursorType )
585{
586 wxStockCursor stock = GetStockCursor( aCursorType );
587
588 if( stock != wxCURSOR_MAX )
589 {
590 return wxCursor( stock );
591 }
592
593 static CURSOR_STORE store( hidpi_cursors );
594 return store.Get( aCursorType );
595}
596
597
598wxStockCursor CURSOR_STORE::GetStockCursor( KICURSOR aCursorType )
599{
600 wxStockCursor stockCursor;
601 switch( aCursorType )
602 {
603 case KICURSOR::MOVING:
604 stockCursor = wxCURSOR_SIZING;
605 break;
606 case KICURSOR::BULLSEYE:
607 stockCursor = wxCURSOR_BULLSEYE;
608 break;
609 case KICURSOR::HAND:
610 stockCursor = wxCURSOR_HAND;
611 break;
612 case KICURSOR::ARROW:
613 stockCursor = wxCURSOR_ARROW;
614 break;
615 default:
616 stockCursor = wxCURSOR_MAX;
617 break;
618 }
619
620 if( !KIPLATFORM::UI::IsStockCursorOk( stockCursor ) )
621 {
622 stockCursor = wxCURSOR_MAX;
623 }
624
625 return stockCursor;
626}
Simple class to construct and store cursors against unique ID keys.
Definition: cursors.h:97
const wxCursor & Get(KICURSOR aIdKey) const
Get a given cursor by its ID.
Definition: cursors.cpp:557
static wxStockCursor GetStockCursor(KICURSOR aCursorType)
Definition: cursors.cpp:598
static const wxCursor GetHiDPICursor(KICURSOR aCursorType)
Definition: cursors.cpp:584
static const wxCursor GetCursor(KICURSOR aCursorType)
Definition: cursors.cpp:570
CURSOR_STORE(const std::vector< CURSOR_DEF > &aDefs)
Construct a store with a pre-set list of cursors.
Definition: cursors.cpp:548
std::map< KICURSOR, wxCursor > m_store
< Internal store of cursors by ID
Definition: cursors.h:147
wxCursor constructCursor(const CURSOR_STORE::CURSOR_DEF &aDef)
Construct a cursor for the given definition.
Definition: cursors.cpp:494
static const std::vector< CURSOR_STORE::CURSOR_DEF > hidpi_cursors
Definition: cursors.cpp:289
static const std::vector< CURSOR_STORE::CURSOR_DEF > standard_cursors
Definition: cursors.cpp:93
KICURSOR
Definition: cursors.h:34
bool IsStockCursorOk(wxStockCursor aCursor)
Checks if we designated a stock cursor for this OS as "OK" or else we may need to load a custom one.
Definition: wxgtk/ui.cpp:157
Definition of a cursor.
Definition: cursors.h:103
const unsigned char * m_image_data
The mask data bitmap.
Definition: cursors.h:108
const char ** m_xpm
The image size in pixels.
Definition: cursors.h:113
const unsigned char * m_mask_data
Definition: cursors.h:111
wxSize m_size
The "hotspot" where the cursor "is" in the image.
Definition: cursors.h:116