KiCad PCB EDA Suite
Loading...
Searching...
No Matches
ui_common.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <wx/arrstr.h>
21#include <wx/dcclient.h>
22#include <wx/checkbox.h>
23#include <wx/choice.h>
24#include <wx/menu.h>
25#include <wx/menuitem.h>
26#include <wx/listbox.h>
27#include <wx/dataview.h>
28#include <wx/radiobut.h>
29#include <wx/slider.h>
30#include <wx/spinctrl.h>
31#include <wx/srchctrl.h>
32#include <wx/stc/stc.h>
33#include <wx/scrolbar.h>
34#include <wx/grid.h>
35#include <wx/hyperlink.h>
36#include <wx/notebook.h>
37#include <widgets/ui_common.h>
38
39#include <algorithm>
40#include <dialog_shim.h>
41#include <pgm_base.h>
42#include <wx/settings.h>
45#include <string_utils.h>
46
47
48const wxString KIUI::s_FocusStealableInputName = wxS( "KI_NOFOCUS" );
49
50
52{
53 // This is the value used in (most) wxFB dialogs
54 return 5;
55}
56
57
58SEVERITY SeverityFromString( const wxString& aSeverity )
59{
60 if( aSeverity == wxT( "warning" ) )
62 else if( aSeverity == wxT( "ignore" ) )
64 else
65 return RPT_SEVERITY_ERROR;
66}
67
68
69wxString SeverityToString( const SEVERITY& aSeverity )
70{
71 if( aSeverity == RPT_SEVERITY_IGNORE )
72 return wxT( "ignore" );
73 else if( aSeverity == RPT_SEVERITY_WARNING )
74 return wxT( "warning" );
75 else
76 return wxT( "error" );
77}
78
79
80wxSize KIUI::GetTextSize( const wxString& aSingleLine, wxWindow* aWindow )
81{
82 wxCoord width;
83 wxCoord height;
84
85 {
86 wxClientDC dc( aWindow );
87 dc.SetFont( aWindow->GetFont() );
88 dc.GetTextExtent( aSingleLine, &width, &height );
89 }
90
91 return wxSize( width, height );
92}
93
94
95wxFont KIUI::GetMonospacedUIFont( int aRelativeSize )
96{
97 static const int guiFontSize = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ).GetPointSize();
98
99 wxFont font( guiFontSize + aRelativeSize, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL );
100
101#ifdef __WXMAC__
102 // https://trac.wxwidgets.org/ticket/19210
103 if( font.GetFaceName().IsEmpty() )
104 font.SetFaceName( wxS( "Menlo" ) );
105#endif
106
107 return font;
108}
109
110
111wxFont getGUIFont( wxWindow* aWindow, int aRelativeSize )
112{
113 wxFont font = aWindow->GetFont();
114
115 font.SetPointSize( font.GetPointSize() + aRelativeSize );
116
117 if( Pgm().GetCommonSettings() && Pgm().GetCommonSettings()->m_Appearance.apply_icon_scale_to_fonts )
118 font.SetPointSize( KiROUND( KiIconScale( aWindow ) * font.GetPointSize() / 4.0 ) );
119
120#ifdef __WXMAC__
121 // https://trac.wxwidgets.org/ticket/19210
122 if( font.GetFaceName().IsEmpty() )
123 font.SetFaceName( wxS( "San Francisco" ) );
124
125 // OSX 10.1 .. 10.9: Lucida Grande
126 // OSX 10.10: Helvetica Neue
127 // OSX 10.11 .. : San Francisco
128#endif
129
130 return font;
131}
132
133
134wxFont KIUI::GetStatusFont( wxWindow* aWindow )
135{
136#ifdef __WXMAC__
137 int scale = -2;
138#else
139 int scale = 0;
140#endif
141
142 return getGUIFont( aWindow, scale );
143}
144
145
146wxFont KIUI::GetDockedPaneFont( wxWindow* aWindow )
147{
148#ifdef __WXMAC__
149 int scale = -1;
150#else
151 int scale = 0;
152#endif
153
154 return getGUIFont( aWindow, scale );
155}
156
157
158wxFont KIUI::GetInfoFont( wxWindow* aWindow )
159{
160 return getGUIFont( aWindow, -1 );
161}
162
163
164wxFont KIUI::GetSmallInfoFont( wxWindow* aWindow )
165{
166 return getGUIFont( aWindow, - 2 );
167}
168
169
170wxFont KIUI::GetControlFont( wxWindow* aWindow )
171{
172 return getGUIFont( aWindow, 0 );
173}
174
175
176bool KIUI::EnsureTextCtrlWidth( wxTextCtrl* aCtrl, const wxString* aString )
177{
178 wxWindow* window = aCtrl->GetParent();
179
180 if( !window )
181 window = aCtrl;
182
183 wxString ctrlText;
184
185 if( !aString )
186 {
187 ctrlText = aCtrl->GetValue();
188 aString = &ctrlText;
189 }
190
191 wxSize textz = GetTextSize( *aString, window );
192 wxSize ctrlz = aCtrl->GetSize();
193
194 if( ctrlz.GetWidth() < textz.GetWidth() + 10 )
195 {
196 ctrlz.SetWidth( textz.GetWidth() + 10 );
197 aCtrl->SetSizeHints( ctrlz );
198 return true;
199 }
200
201 return false;
202}
203
204
205void KIUI::FollowSystemLinkColour( wxHyperlinkCtrl* aLink )
206{
207#ifdef __WXMAC__
208 // The visited purple is fixed for both themes, so a clicked link takes the link colour too
209 auto apply = [aLink]()
210 {
211 wxColour colour = wxSystemSettings::GetColour( wxSYS_COLOUR_HOTLIGHT );
212
213 aLink->SetNormalColour( colour );
214 aLink->SetVisitedColour( colour );
215 };
216
217 apply();
218
219 aLink->Bind( wxEVT_SYS_COLOUR_CHANGED,
220 [apply]( wxSysColourChangedEvent& aEvent )
221 {
222 apply();
223 aEvent.Skip();
224 } );
225#else
226 wxUnusedVar( aLink );
227#endif
228}
229
230
231wxString KIUI::EllipsizeStatusText( wxWindow* aWindow, const wxString& aString )
232{
233 wxString msg = UnescapeString( aString );
234
235 msg.Replace( wxT( "\n" ), wxT( " " ) );
236 msg.Replace( wxT( "\r" ), wxT( " " ) );
237 msg.Replace( wxT( "\t" ), wxT( " " ) );
238
239 wxClientDC dc( aWindow );
240 int statusWidth = aWindow->GetSize().GetWidth();
241
242 // 30% of the first 800 pixels plus 60% of the remaining width
243 int textWidth = std::min( statusWidth, 800 ) * 0.3 + std::max( statusWidth - 800, 0 ) * 0.6;
244
245 return wxControl::Ellipsize( msg, dc, wxELLIPSIZE_END, textWidth );
246}
247
248
249wxString KIUI::EllipsizeMenuText( const wxString& aString )
250{
251 wxString msg = UnescapeString( aString );
252
253 msg.Replace( wxT( "\n" ), wxT( " " ) );
254 msg.Replace( wxT( "\r" ), wxT( " " ) );
255 msg.Replace( wxT( "\t" ), wxT( " " ) );
256
257 if( msg.Length() > 36 )
258 msg = msg.Left( 34 ) + wxT( "..." );
259
260 return msg;
261}
262
263
264wxString KIUI::WrapText( wxWindow* aWindow, const wxString& aText, int aWidth )
265{
266 wxClientDC dc( aWindow );
267 dc.SetFont( aWindow->GetFont() );
268
269 auto measure =
270 [&]( const wxString& aLine )
271 {
272 return dc.GetTextExtent( aLine ).x;
273 };
274
275 wxString wrapped;
276
277 for( const wxString& line : wxSplit( aText, '\n', '\0' ) )
278 {
279 wxString current;
280
281 for( wxString word : wxSplit( line, ' ', '\0' ) )
282 {
283 const bool isPath = word.Contains( wxS( "/" ) ) || word.Contains( wxS( "\\" ) );
284
285 if( isPath && measure( word ) > aWidth )
286 word = wxControl::Ellipsize( word, dc, wxELLIPSIZE_MIDDLE, aWidth, wxELLIPSIZE_FLAGS_NONE );
287
288 const wxString joined = current.IsEmpty() ? word : current + wxS( " " ) + word;
289
290 if( measure( joined ) <= aWidth )
291 {
292 current = joined;
293 continue;
294 }
295
296 if( !current.IsEmpty() )
297 {
298 wrapped += current + wxS( "\n" );
299 current.clear();
300 }
301
302 for( const wxString& cluster : SplitGraphemeClusters( word ) )
303 {
304 if( !current.IsEmpty() && measure( current + cluster ) > aWidth )
305 {
306 wrapped += current + wxS( "\n" );
307 current.clear();
308 }
309
310 current += cluster;
311 }
312 }
313
314 wrapped += current + wxS( "\n" );
315 }
316
317 wrapped.RemoveLast();
318 return wrapped;
319}
320
321
322void KIUI::SelectReferenceNumber( wxTextEntry* aTextEntry )
323{
324 wxString ref = aTextEntry->GetValue();
325
326 if( ref.find_first_of( '?' ) != ref.npos )
327 {
328 aTextEntry->SetSelection( ref.find_first_of( '?' ), ref.find_last_of( '?' ) + 1 );
329 }
330 else if( ref.find_first_of( '*' ) != ref.npos )
331 {
332 aTextEntry->SetSelection( ref.find_first_of( '*' ), ref.find_last_of( '*' ) + 1 );
333 }
334 else
335 {
336 wxString num = ref;
337
338 while( !num.IsEmpty() && ( !isdigit( num.Last() ) || !isdigit( num.GetChar( 0 ) ) ) )
339 {
340 // Trim non-digit from end
341 if( !isdigit( num.Last() ) )
342 num.RemoveLast();
343
344 // Trim non-digit from the start
345 if( !num.IsEmpty() && !isdigit( num.GetChar( 0 ) ) )
346 num = num.Right( num.Length() - 1 );
347 }
348
349 aTextEntry->SetSelection( ref.Find( num ), ref.Find( num ) + num.Length() );
350
351 if( num.IsEmpty() )
352 aTextEntry->SetSelection( -1, -1 );
353 }
354}
355
356
357bool KIUI::IsInputControlFocused( wxWindow* aFocus )
358{
359 if( aFocus == nullptr )
360 aFocus = wxWindow::FindFocus();
361
362 if( !aFocus )
363 return false;
364
365 // These widgets are never considered focused
366 if( aFocus->GetName() == s_FocusStealableInputName )
367 return false;
368
369 wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( aFocus );
370 wxStyledTextCtrl* styledText = dynamic_cast<wxStyledTextCtrl*>( aFocus );
371 wxListBox* listBox = dynamic_cast<wxListBox*>( aFocus );
372 wxSearchCtrl* searchCtrl = dynamic_cast<wxSearchCtrl*>( aFocus );
373 wxCheckBox* checkboxCtrl = dynamic_cast<wxCheckBox*>( aFocus );
374 wxChoice* choiceCtrl = dynamic_cast<wxChoice*>( aFocus );
375 wxRadioButton* radioBtn = dynamic_cast<wxRadioButton*>( aFocus );
376 wxSpinCtrl* spinCtrl = dynamic_cast<wxSpinCtrl*>( aFocus );
377 wxSpinCtrlDouble* spinDblCtrl = dynamic_cast<wxSpinCtrlDouble*>( aFocus );
378 wxSlider* sliderCtl = dynamic_cast<wxSlider*>( aFocus );
379
380 // Data view control is annoying, the focus is on a "wxDataViewCtrlMainWindow" class that
381 // is not formally exported via the header.
382 wxDataViewCtrl* dataViewCtrl = nullptr;
383
384 wxWindow* parent = aFocus->GetParent();
385
386 if( parent )
387 dataViewCtrl = dynamic_cast<wxDataViewCtrl*>( parent );
388
389 return ( textEntry || styledText || listBox || searchCtrl || checkboxCtrl || choiceCtrl
390 || radioBtn || spinCtrl || spinDblCtrl || sliderCtl || dataViewCtrl );
391}
392
393
394bool KIUI::IsInputControlEditable( wxWindow* aFocus )
395{
396 wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( aFocus );
397 wxStyledTextCtrl* styledText = dynamic_cast<wxStyledTextCtrl*>( aFocus );
398 wxSearchCtrl* searchCtrl = dynamic_cast<wxSearchCtrl*>( aFocus );
399
400 if( textEntry )
401 return textEntry->IsEditable();
402 else if( styledText )
403 return styledText->IsEditable();
404 else if( searchCtrl )
405 return searchCtrl->IsEditable();
406
407 // Must return true if we can't determine the state, intentionally true for non inputs as well.
408 return true;
409}
410
411
413{
414 return !Pgm().m_ModalDialogs.empty();
415}
416
417
418void KIUI::Disable( wxWindow* aWindow )
419{
420 wxScrollBar* scrollBar = dynamic_cast<wxScrollBar*>( aWindow );
421 wxHyperlinkCtrl* hyperlink = dynamic_cast<wxHyperlinkCtrl*>( aWindow );
422 wxGrid* grid = dynamic_cast<wxGrid*>( aWindow );
423 wxStyledTextCtrl* scintilla = dynamic_cast<wxStyledTextCtrl*>( aWindow );
424 wxControl* control = dynamic_cast<wxControl*>( aWindow );
425
426 if( scrollBar || hyperlink )
427 {
428 // Leave navigation controls active
429 }
430 else if( grid )
431 {
432 for( int row = 0; row < grid->GetNumberRows(); ++row )
433 {
434 for( int col = 0; col < grid->GetNumberCols(); ++col )
435 grid->SetReadOnly( row, col );
436 }
437 }
438 else if( scintilla )
439 {
440 scintilla->SetReadOnly( true );
441 }
442 else if( control )
443 {
444 control->Disable();
445 }
446 else
447 {
448 for( wxWindow* child : aWindow->GetChildren() )
449 Disable( child );
450 }
451}
452
453
454void KIUI::AddBitmapToMenuItem( wxMenuItem* aMenu, const wxBitmapBundle& aImage )
455{
456 // Retrieve the global application show icon option:
457 bool useImagesInMenus = Pgm().GetCommonSettings() && Pgm().GetCommonSettings()->m_Appearance.use_icons_in_menus;
458
459 wxItemKind menu_type = aMenu->GetKind();
460
461 if( useImagesInMenus && menu_type != wxITEM_CHECK && menu_type != wxITEM_RADIO )
462 aMenu->SetBitmap( aImage );
463}
464
465
466wxMenuItem* KIUI::AddMenuItem( wxMenu* aMenu, int aId, const wxString& aText,
467 const wxBitmapBundle& aImage, wxItemKind aType )
468{
469 wxMenuItem* item = new wxMenuItem( aMenu, aId, aText, wxEmptyString, aType );
470 AddBitmapToMenuItem( item, aImage );
471
472 aMenu->Append( item );
473
474 return item;
475}
476
477
478wxMenuItem* KIUI::AddMenuItem( wxMenu* aMenu, int aId, const wxString& aText,
479 const wxString& aHelpText, const wxBitmapBundle& aImage,
480 wxItemKind aType )
481{
482 wxMenuItem* item = new wxMenuItem( aMenu, aId, aText, aHelpText, aType );
483 AddBitmapToMenuItem( item, aImage );
484
485 aMenu->Append( item );
486
487 return item;
488}
489
490
491wxMenuItem* KIUI::AddMenuItem( wxMenu* aMenu, wxMenu* aSubMenu, int aId, const wxString& aText,
492 const wxBitmapBundle& aImage )
493{
494 wxMenuItem* item = new wxMenuItem( aMenu, aId, aText );
495 item->SetSubMenu( aSubMenu );
496 AddBitmapToMenuItem( item, aImage );
497
498 aMenu->Append( item );
499
500 return item;
501}
502
503
504wxMenuItem* KIUI::AddMenuItem( wxMenu* aMenu, wxMenu* aSubMenu, int aId, const wxString& aText,
505 const wxString& aHelpText, const wxBitmapBundle& aImage )
506{
507 wxMenuItem* item = new wxMenuItem( aMenu, aId, aText, aHelpText );
508 item->SetSubMenu( aSubMenu );
509 AddBitmapToMenuItem( item, aImage );
510
511 aMenu->Append( item );
512
513 return item;
514}
515
516
517int FindNotebookPage( wxNotebook* aNotebook, wxWindow* aChildWindow )
518{
519 wxWindow* candidate = aChildWindow;
520
521 while( candidate && candidate->GetParent() != aNotebook )
522 candidate = candidate->GetParent();
523
524 if ( candidate )
525 return aNotebook->FindPage( candidate );
526
527 return 0;
528}
int KiIconScale(wxWindow *aWindow)
Return the automatic scale factor that would be used for a given window by KiScaledBitmap and KiScale...
Definition bitmap.cpp:130
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:985
APPEARANCE m_Appearance
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition pgm_base.cpp:600
std::vector< void * > m_ModalDialogs
Definition pgm_base.h:423
KICOMMON_API int GetStdMargin()
Get the standard margin around a widget in the KiCad UI.
Definition ui_common.cpp:51
KICOMMON_API wxFont GetDockedPaneFont(wxWindow *aWindow)
KICOMMON_API wxFont GetStatusFont(wxWindow *aWindow)
KICOMMON_API bool IsInputControlFocused(wxWindow *aFocus=nullptr)
Check if a input control has focus.
KICOMMON_API bool IsInputControlEditable(wxWindow *aControl)
Check if a input control has focus.
KICOMMON_API wxString EllipsizeMenuText(const wxString &aString)
Ellipsize text (at the end) to be no more than 36 characters.
KICOMMON_API wxFont GetInfoFont(wxWindow *aWindow)
KICOMMON_API wxString WrapText(wxWindow *aWindow, const wxString &aText, int aWidth)
Break text into lines no wider than aWidth pixels in aWindow's font.
KICOMMON_API wxFont GetSmallInfoFont(wxWindow *aWindow)
KICOMMON_API bool IsModalDialogFocused()
KICOMMON_API wxFont GetControlFont(wxWindow *aWindow)
KICOMMON_API wxMenuItem * AddMenuItem(wxMenu *aMenu, int aId, const wxString &aText, const wxBitmapBundle &aImage, wxItemKind aType=wxITEM_NORMAL)
Create and insert a menu item with an icon into aMenu.
KICOMMON_API wxString EllipsizeStatusText(wxWindow *aWindow, const wxString &aString)
Ellipsize text (at the end) to be no more than 1/3 of the window width.
KICOMMON_API void FollowSystemLinkColour(wxHyperlinkCtrl *aLink)
Keep a hyperlink in the system link colour across a light or dark switch.
KICOMMON_API const wxString s_FocusStealableInputName
Definition ui_common.cpp:48
KICOMMON_API void SelectReferenceNumber(wxTextEntry *aTextEntry)
Select the number (or "?") in a reference for ease of editing.
KICOMMON_API wxSize GetTextSize(const wxString &aSingleLine, wxWindow *aWindow)
Return the size of aSingleLine of text when it is rendered in aWindow using whatever font is currentl...
Definition ui_common.cpp:80
KICOMMON_API void AddBitmapToMenuItem(wxMenuItem *aMenu, const wxBitmapBundle &aImage)
Add a bitmap to a menuitem.
KICOMMON_API wxFont GetMonospacedUIFont(int aRelativeSize=0)
Definition ui_common.cpp:95
KICOMMON_API bool EnsureTextCtrlWidth(wxTextCtrl *aCtrl, const wxString *aString=nullptr)
Set the minimum pixel width on a text control in order to make a text string be fully visible within ...
KICOMMON_API void Disable(wxWindow *aWindow)
Makes a window read-only.
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
SEVERITY
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_IGNORE
const int scale
wxString UnescapeString(const wxString &aSource)
std::vector< wxString > SplitGraphemeClusters(const wxString &aText)
Split text into the units a reader sees as one character.
DATABASE_CONNECTION dc(odbcBackend(cs))
int FindNotebookPage(wxNotebook *aNotebook, wxWindow *aChildWindow)
SEVERITY SeverityFromString(const wxString &aSeverity)
Definition ui_common.cpp:58
wxString SeverityToString(const SEVERITY &aSeverity)
Definition ui_common.cpp:69
wxFont getGUIFont(wxWindow *aWindow, int aRelativeSize)
Functions to provide common constants and other functions to assist in making a consistent UI.