KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <wx/dcclient.h>
21#include <wx/checkbox.h>
22#include <wx/choice.h>
23#include <wx/menu.h>
24#include <wx/menuitem.h>
25#include <wx/listbox.h>
26#include <wx/dataview.h>
27#include <wx/radiobut.h>
28#include <wx/slider.h>
29#include <wx/spinctrl.h>
30#include <wx/srchctrl.h>
31#include <wx/stc/stc.h>
32#include <wx/scrolbar.h>
33#include <wx/grid.h>
34#include <widgets/ui_common.h>
35
36#include <algorithm>
37#include <dialog_shim.h>
38#include <pgm_base.h>
39#include <wx/settings.h>
42#include <string_utils.h>
43#include <wx/hyperlink.h>
44
45
46const wxString KIUI::s_FocusStealableInputName = wxS( "KI_NOFOCUS" );
47
48
50{
51 // This is the value used in (most) wxFB dialogs
52 return 5;
53}
54
55
56SEVERITY SeverityFromString( const wxString& aSeverity )
57{
58 if( aSeverity == wxT( "warning" ) )
60 else if( aSeverity == wxT( "ignore" ) )
62 else
63 return RPT_SEVERITY_ERROR;
64}
65
66
67wxString SeverityToString( const SEVERITY& aSeverity )
68{
69 if( aSeverity == RPT_SEVERITY_IGNORE )
70 return wxT( "ignore" );
71 else if( aSeverity == RPT_SEVERITY_WARNING )
72 return wxT( "warning" );
73 else
74 return wxT( "error" );
75}
76
77
78wxSize KIUI::GetTextSize( const wxString& aSingleLine, wxWindow* aWindow )
79{
80 wxCoord width;
81 wxCoord height;
82
83 {
84 wxClientDC dc( aWindow );
85 dc.SetFont( aWindow->GetFont() );
86 dc.GetTextExtent( aSingleLine, &width, &height );
87 }
88
89 return wxSize( width, height );
90}
91
92
94{
95 static int guiFontSize = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ).GetPointSize();
96
97 wxFont font( guiFontSize, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL );
98
99#ifdef __WXMAC__
100 // https://trac.wxwidgets.org/ticket/19210
101 if( font.GetFaceName().IsEmpty() )
102 font.SetFaceName( wxS( "Menlo" ) );
103#endif
104
105 return font;
106}
107
108
109wxFont getGUIFont( wxWindow* aWindow, int aRelativeSize )
110{
111 wxFont font = aWindow->GetFont();
112
113 font.SetPointSize( font.GetPointSize() + aRelativeSize );
114
115 if( Pgm().GetCommonSettings()->m_Appearance.apply_icon_scale_to_fonts )
116 font.SetPointSize( KiROUND( KiIconScale( aWindow ) * font.GetPointSize() / 4.0 ) );
117
118#ifdef __WXMAC__
119 // https://trac.wxwidgets.org/ticket/19210
120 if( font.GetFaceName().IsEmpty() )
121 font.SetFaceName( wxS( "San Francisco" ) );
122
123 // OSX 10.1 .. 10.9: Lucida Grande
124 // OSX 10.10: Helvetica Neue
125 // OSX 10.11 .. : San Francisco
126#endif
127
128 return font;
129}
130
131
132wxFont KIUI::GetStatusFont( wxWindow* aWindow )
133{
134#ifdef __WXMAC__
135 int scale = -2;
136#else
137 int scale = 0;
138#endif
139
140 return getGUIFont( aWindow, scale );
141}
142
143
144wxFont KIUI::GetDockedPaneFont( wxWindow* aWindow )
145{
146#ifdef __WXMAC__
147 int scale = -1;
148#else
149 int scale = 0;
150#endif
151
152 return getGUIFont( aWindow, scale );
153}
154
155
156wxFont KIUI::GetInfoFont( wxWindow* aWindow )
157{
158 return getGUIFont( aWindow, -1 );
159}
160
161
162wxFont KIUI::GetControlFont( wxWindow* aWindow )
163{
164 return getGUIFont( aWindow, 0 );
165}
166
167
168bool KIUI::EnsureTextCtrlWidth( wxTextCtrl* aCtrl, const wxString* aString )
169{
170 wxWindow* window = aCtrl->GetParent();
171
172 if( !window )
173 window = aCtrl;
174
175 wxString ctrlText;
176
177 if( !aString )
178 {
179 ctrlText = aCtrl->GetValue();
180 aString = &ctrlText;
181 }
182
183 wxSize textz = GetTextSize( *aString, window );
184 wxSize ctrlz = aCtrl->GetSize();
185
186 if( ctrlz.GetWidth() < textz.GetWidth() + 10 )
187 {
188 ctrlz.SetWidth( textz.GetWidth() + 10 );
189 aCtrl->SetSizeHints( ctrlz );
190 return true;
191 }
192
193 return false;
194}
195
196
197wxString KIUI::EllipsizeStatusText( wxWindow* aWindow, const wxString& aString )
198{
199 wxString msg = UnescapeString( aString );
200
201 msg.Replace( wxT( "\n" ), wxT( " " ) );
202 msg.Replace( wxT( "\r" ), wxT( " " ) );
203 msg.Replace( wxT( "\t" ), wxT( " " ) );
204
205 wxClientDC dc( aWindow );
206 int statusWidth = aWindow->GetSize().GetWidth();
207
208 // 30% of the first 800 pixels plus 60% of the remaining width
209 int textWidth = std::min( statusWidth, 800 ) * 0.3 + std::max( statusWidth - 800, 0 ) * 0.6;
210
211 return wxControl::Ellipsize( msg, dc, wxELLIPSIZE_END, textWidth );
212}
213
214
215wxString KIUI::EllipsizeMenuText( const wxString& aString )
216{
217 wxString msg = UnescapeString( aString );
218
219 msg.Replace( wxT( "\n" ), wxT( " " ) );
220 msg.Replace( wxT( "\r" ), wxT( " " ) );
221 msg.Replace( wxT( "\t" ), wxT( " " ) );
222
223 if( msg.Length() > 36 )
224 msg = msg.Left( 34 ) + wxT( "..." );
225
226 return msg;
227}
228
229
230void KIUI::SelectReferenceNumber( wxTextEntry* aTextEntry )
231{
232 wxString ref = aTextEntry->GetValue();
233
234 if( ref.find_first_of( '?' ) != ref.npos )
235 {
236 aTextEntry->SetSelection( ref.find_first_of( '?' ), ref.find_last_of( '?' ) + 1 );
237 }
238 else if( ref.find_first_of( '*' ) != ref.npos )
239 {
240 aTextEntry->SetSelection( ref.find_first_of( '*' ), ref.find_last_of( '*' ) + 1 );
241 }
242 else
243 {
244 wxString num = ref;
245
246 while( !num.IsEmpty() && ( !isdigit( num.Last() ) || !isdigit( num.GetChar( 0 ) ) ) )
247 {
248 // Trim non-digit from end
249 if( !isdigit( num.Last() ) )
250 num.RemoveLast();
251
252 // Trim non-digit from the start
253 if( !num.IsEmpty() && !isdigit( num.GetChar( 0 ) ) )
254 num = num.Right( num.Length() - 1 );
255 }
256
257 aTextEntry->SetSelection( ref.Find( num ), ref.Find( num ) + num.Length() );
258
259 if( num.IsEmpty() )
260 aTextEntry->SetSelection( -1, -1 );
261 }
262}
263
264
265bool KIUI::IsInputControlFocused( wxWindow* aFocus )
266{
267 if( aFocus == nullptr )
268 aFocus = wxWindow::FindFocus();
269
270 if( !aFocus )
271 return false;
272
273 // These widgets are never considered focused
274 if( aFocus->GetName() == s_FocusStealableInputName )
275 return false;
276
277 wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( aFocus );
278 wxStyledTextCtrl* styledText = dynamic_cast<wxStyledTextCtrl*>( aFocus );
279 wxListBox* listBox = dynamic_cast<wxListBox*>( aFocus );
280 wxSearchCtrl* searchCtrl = dynamic_cast<wxSearchCtrl*>( aFocus );
281 wxCheckBox* checkboxCtrl = dynamic_cast<wxCheckBox*>( aFocus );
282 wxChoice* choiceCtrl = dynamic_cast<wxChoice*>( aFocus );
283 wxRadioButton* radioBtn = dynamic_cast<wxRadioButton*>( aFocus );
284 wxSpinCtrl* spinCtrl = dynamic_cast<wxSpinCtrl*>( aFocus );
285 wxSpinCtrlDouble* spinDblCtrl = dynamic_cast<wxSpinCtrlDouble*>( aFocus );
286 wxSlider* sliderCtl = dynamic_cast<wxSlider*>( aFocus );
287
288 // Data view control is annoying, the focus is on a "wxDataViewCtrlMainWindow" class that
289 // is not formally exported via the header.
290 wxDataViewCtrl* dataViewCtrl = nullptr;
291
292 wxWindow* parent = aFocus->GetParent();
293
294 if( parent )
295 dataViewCtrl = dynamic_cast<wxDataViewCtrl*>( parent );
296
297 return ( textEntry || styledText || listBox || searchCtrl || checkboxCtrl || choiceCtrl
298 || radioBtn || spinCtrl || spinDblCtrl || sliderCtl || dataViewCtrl );
299}
300
301
302bool KIUI::IsInputControlEditable( wxWindow* aFocus )
303{
304 wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( aFocus );
305 wxStyledTextCtrl* styledText = dynamic_cast<wxStyledTextCtrl*>( aFocus );
306 wxSearchCtrl* searchCtrl = dynamic_cast<wxSearchCtrl*>( aFocus );
307
308 if( textEntry )
309 return textEntry->IsEditable();
310 else if( styledText )
311 return styledText->IsEditable();
312 else if( searchCtrl )
313 return searchCtrl->IsEditable();
314
315 // Must return true if we can't determine the state, intentionally true for non inputs as well.
316 return true;
317}
318
319
321{
322 return !Pgm().m_ModalDialogs.empty();
323}
324
325
326void KIUI::Disable( wxWindow* aWindow )
327{
328 wxScrollBar* scrollBar = dynamic_cast<wxScrollBar*>( aWindow );
329 wxHyperlinkCtrl* hyperlink = dynamic_cast<wxHyperlinkCtrl*>( aWindow );
330 wxGrid* grid = dynamic_cast<wxGrid*>( aWindow );
331 wxStyledTextCtrl* scintilla = dynamic_cast<wxStyledTextCtrl*>( aWindow );
332 wxControl* control = dynamic_cast<wxControl*>( aWindow );
333
334 if( scrollBar || hyperlink )
335 {
336 // Leave navigation controls active
337 }
338 else if( grid )
339 {
340 for( int row = 0; row < grid->GetNumberRows(); ++row )
341 {
342 for( int col = 0; col < grid->GetNumberCols(); ++col )
343 grid->SetReadOnly( row, col );
344 }
345 }
346 else if( scintilla )
347 {
348 scintilla->SetReadOnly( true );
349 }
350 else if( control )
351 {
352 control->Disable();
353 }
354 else
355 {
356 for( wxWindow* child : aWindow->GetChildren() )
357 Disable( child );
358 }
359}
360
361
362void KIUI::AddBitmapToMenuItem( wxMenuItem* aMenu, const wxBitmapBundle& aImage )
363{
364 // Retrieve the global application show icon option:
365 bool useImagesInMenus = Pgm().GetCommonSettings()->m_Appearance.use_icons_in_menus;
366
367 wxItemKind menu_type = aMenu->GetKind();
368
369 if( useImagesInMenus && menu_type != wxITEM_CHECK && menu_type != wxITEM_RADIO )
370 {
371 aMenu->SetBitmap( aImage );
372 }
373}
374
375
376wxMenuItem* KIUI::AddMenuItem( wxMenu* aMenu, int aId, const wxString& aText,
377 const wxBitmapBundle& aImage, wxItemKind aType )
378{
379 wxMenuItem* item = new wxMenuItem( aMenu, aId, aText, wxEmptyString, aType );
380 AddBitmapToMenuItem( item, aImage );
381
382 aMenu->Append( item );
383
384 return item;
385}
386
387
388wxMenuItem* KIUI::AddMenuItem( wxMenu* aMenu, int aId, const wxString& aText,
389 const wxString& aHelpText, const wxBitmapBundle& aImage,
390 wxItemKind aType )
391{
392 wxMenuItem* item = new wxMenuItem( aMenu, aId, aText, aHelpText, aType );
393 AddBitmapToMenuItem( item, aImage );
394
395 aMenu->Append( item );
396
397 return item;
398}
399
400
401wxMenuItem* KIUI::AddMenuItem( wxMenu* aMenu, wxMenu* aSubMenu, int aId, const wxString& aText,
402 const wxBitmapBundle& aImage )
403{
404 wxMenuItem* item = new wxMenuItem( aMenu, aId, aText );
405 item->SetSubMenu( aSubMenu );
406 AddBitmapToMenuItem( item, aImage );
407
408 aMenu->Append( item );
409
410 return item;
411}
412
413
414wxMenuItem* KIUI::AddMenuItem( wxMenu* aMenu, wxMenu* aSubMenu, int aId, const wxString& aText,
415 const wxString& aHelpText, const wxBitmapBundle& aImage )
416{
417 wxMenuItem* item = new wxMenuItem( aMenu, aId, aText, aHelpText );
418 item->SetSubMenu( aSubMenu );
419 AddBitmapToMenuItem( item, aImage );
420
421 aMenu->Append( item );
422
423 return item;
424}
int KiIconScale(wxWindow *aWindow)
Return the automatic scale factor that would be used for a given window by KiScaledBitmap and KiScale...
Definition: bitmap.cpp:122
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
APPEARANCE m_Appearance
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition: pgm_base.cpp:687
std::vector< void * > m_ModalDialogs
Definition: pgm_base.h:380
KICOMMON_API wxFont GetMonospacedUIFont()
Definition: ui_common.cpp:93
KICOMMON_API int GetStdMargin()
Get the standard margin around a widget in the KiCad UI.
Definition: ui_common.cpp:49
KICOMMON_API wxFont GetDockedPaneFont(wxWindow *aWindow)
Definition: ui_common.cpp:144
KICOMMON_API wxFont GetStatusFont(wxWindow *aWindow)
Definition: ui_common.cpp:132
KICOMMON_API bool IsInputControlFocused(wxWindow *aFocus=nullptr)
Check if a input control has focus.
Definition: ui_common.cpp:265
KICOMMON_API bool IsInputControlEditable(wxWindow *aControl)
Check if a input control has focus.
Definition: ui_common.cpp:302
KICOMMON_API wxString EllipsizeMenuText(const wxString &aString)
Ellipsize text (at the end) to be no more than 36 characters.
Definition: ui_common.cpp:215
KICOMMON_API wxFont GetInfoFont(wxWindow *aWindow)
Definition: ui_common.cpp:156
KICOMMON_API bool IsModalDialogFocused()
Definition: ui_common.cpp:320
KICOMMON_API wxFont GetControlFont(wxWindow *aWindow)
Definition: ui_common.cpp:162
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.
Definition: ui_common.cpp:376
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.
Definition: ui_common.cpp:197
KICOMMON_API const wxString s_FocusStealableInputName
Definition: ui_common.cpp:46
KICOMMON_API void SelectReferenceNumber(wxTextEntry *aTextEntry)
Select the number (or "?") in a reference for ease of editing.
Definition: ui_common.cpp:230
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:78
KICOMMON_API void AddBitmapToMenuItem(wxMenuItem *aMenu, const wxBitmapBundle &aImage)
Add a bitmap to a menuitem.
Definition: ui_common.cpp:362
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 ...
Definition: ui_common.cpp:168
KICOMMON_API void Disable(wxWindow *aWindow)
Makes a window read-only.
Definition: ui_common.cpp:326
PGM_BASE & Pgm()
The global program "get" accessor.
Definition: pgm_base.cpp:1071
see class PGM_BASE
SEVERITY
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_IGNORE
const int scale
wxString UnescapeString(const wxString &aSource)
SEVERITY SeverityFromString(const wxString &aSeverity)
Definition: ui_common.cpp:56
wxString SeverityToString(const SEVERITY &aSeverity)
Definition: ui_common.cpp:67
wxFont getGUIFont(wxWindow *aWindow, int aRelativeSize)
Definition: ui_common.cpp:109
Functions to provide common constants and other functions to assist in making a consistent UI.