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 (C) 2018-2022 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/listbox.h>
24#include <wx/dataview.h>
25#include <wx/radiobut.h>
26#include <wx/slider.h>
27#include <wx/spinctrl.h>
28#include <wx/srchctrl.h>
29#include <wx/stc/stc.h>
30#include <wx/scrolbar.h>
31#include <wx/grid.h>
32#include <widgets/ui_common.h>
33
34#include <algorithm>
35#include <dialog_shim.h>
36#include <pgm_base.h>
37#include <wx/settings.h>
39#include <string_utils.h>
40
41
42const wxString KIUI::s_FocusStealableInputName = wxS( "KI_NOFOCUS");
43
44
46{
47 // This is the value used in (most) wxFB dialogs
48 return 5;
49}
50
51
52SEVERITY SeverityFromString( const wxString& aSeverity )
53{
54 if( aSeverity == wxT( "warning" ) )
56 else if( aSeverity == wxT( "ignore" ) )
58 else
59 return RPT_SEVERITY_ERROR;
60}
61
62
63wxString SeverityToString( const SEVERITY& aSeverity )
64{
65 if( aSeverity == RPT_SEVERITY_IGNORE )
66 return wxT( "ignore" );
67 else if( aSeverity == RPT_SEVERITY_WARNING )
68 return wxT( "warning" );
69 else
70 return wxT( "error" );
71}
72
73
74wxSize KIUI::GetTextSize( const wxString& aSingleLine, wxWindow* aWindow )
75{
76 wxCoord width;
77 wxCoord height;
78
79 {
80 wxClientDC dc( aWindow );
81 dc.SetFont( aWindow->GetFont() );
82 dc.GetTextExtent( aSingleLine, &width, &height );
83 }
84
85 return wxSize( width, height );
86}
87
88
90{
91 static int guiFontSize = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ).GetPointSize();
92
93 wxFont font( guiFontSize, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL );
94
95#ifdef __WXMAC__
96 // https://trac.wxwidgets.org/ticket/19210
97 if( font.GetFaceName().IsEmpty() )
98 font.SetFaceName( wxS( "Menlo" ) );
99#endif
100
101 return font;
102}
103
104
105wxFont getGUIFont( wxWindow* aWindow, int aRelativeSize )
106{
107 wxFont font = aWindow->GetFont();
108
109 font.SetPointSize( font.GetPointSize() + aRelativeSize );
110
111 if( Pgm().GetCommonSettings()->m_Appearance.apply_icon_scale_to_fonts )
112 {
113 double icon_scale_fourths;
114
115 if( Pgm().GetCommonSettings()->m_Appearance.icon_scale <= 0 )
116 icon_scale_fourths = KiIconScale( aWindow );
117 else
118 icon_scale_fourths = Pgm().GetCommonSettings()->m_Appearance.icon_scale;
119
120 font.SetPointSize( KiROUND( icon_scale_fourths * font.GetPointSize() / 4.0 ) );
121 }
122
123#ifdef __WXMAC__
124 // https://trac.wxwidgets.org/ticket/19210
125 if( font.GetFaceName().IsEmpty() )
126 font.SetFaceName( wxS( "San Francisco" ) );
127 // OSX 10.1 .. 10.9: Lucida Grande
128 // OSX 10.10: Helvetica Neue
129 // OSX 10.11 .. : San Francisco
130#endif
131
132 return font;
133}
134
135
136wxFont KIUI::GetStatusFont( wxWindow* aWindow )
137{
138#ifdef __WXMAC__
139 int scale = -2;
140#else
141 int scale = 0;
142#endif
143
144 return getGUIFont( aWindow, scale );
145}
146
147
148wxFont KIUI::GetDockedPaneFont( wxWindow* aWindow )
149{
150#ifdef __WXMAC__
151 int scale = -1;
152#else
153 int scale = 0;
154#endif
155
156 return getGUIFont( aWindow, scale );
157}
158
159
160wxFont KIUI::GetInfoFont( wxWindow* aWindow )
161{
162 return getGUIFont( aWindow, -1 );
163}
164
165
166wxFont KIUI::GetControlFont( wxWindow* aWindow )
167{
168 return getGUIFont( aWindow, 0 );
169}
170
171
172bool KIUI::EnsureTextCtrlWidth( wxTextCtrl* aCtrl, const wxString* aString )
173{
174 wxWindow* window = aCtrl->GetParent();
175
176 if( !window )
177 window = aCtrl;
178
179 wxString ctrlText;
180
181 if( !aString )
182 {
183 ctrlText = aCtrl->GetValue();
184 aString = &ctrlText;
185 }
186
187 wxSize textz = GetTextSize( *aString, window );
188 wxSize ctrlz = aCtrl->GetSize();
189
190 if( ctrlz.GetWidth() < textz.GetWidth() + 10 )
191 {
192 ctrlz.SetWidth( textz.GetWidth() + 10 );
193 aCtrl->SetSizeHints( ctrlz );
194 return true;
195 }
196
197 return false;
198}
199
200
201wxString KIUI::EllipsizeStatusText( wxWindow* aWindow, const wxString& aString )
202{
203 wxString msg = UnescapeString( aString );
204
205 msg.Replace( wxT( "\n" ), wxT( " " ) );
206 msg.Replace( wxT( "\r" ), wxT( " " ) );
207 msg.Replace( wxT( "\t" ), wxT( " " ) );
208
209 wxClientDC dc( aWindow );
210 int statusWidth = aWindow->GetSize().GetWidth();
211
212 // 30% of the first 800 pixels plus 60% of the remaining width
213 int textWidth = std::min( statusWidth, 800 ) * 0.3 + std::max( statusWidth - 800, 0 ) * 0.6;
214
215 return wxControl::Ellipsize( msg, dc, wxELLIPSIZE_END, textWidth );
216}
217
218
219wxString KIUI::EllipsizeMenuText( const wxString& aString )
220{
221 wxString msg = UnescapeString( aString );
222
223 msg.Replace( wxT( "\n" ), wxT( " " ) );
224 msg.Replace( wxT( "\r" ), wxT( " " ) );
225 msg.Replace( wxT( "\t" ), wxT( " " ) );
226
227 if( msg.Length() > 36 )
228 msg = msg.Left( 34 ) + wxT( "..." );
229
230 return msg;
231}
232
233
234void KIUI::SelectReferenceNumber( wxTextEntry* aTextEntry )
235{
236 wxString ref = aTextEntry->GetValue();
237
238 if( ref.find_first_of( '?' ) != ref.npos )
239 {
240 aTextEntry->SetSelection( ref.find_first_of( '?' ), ref.find_last_of( '?' ) + 1 );
241 }
242 else if( ref.find_first_of( '*' ) != ref.npos )
243 {
244 aTextEntry->SetSelection( ref.find_first_of( '*' ), ref.find_last_of( '*' ) + 1 );
245 }
246 else
247 {
248 wxString num = ref;
249
250 while( !num.IsEmpty() && ( !isdigit( num.Last() ) || !isdigit( num.GetChar( 0 ) ) ) )
251 {
252 // Trim non-digit from end
253 if( !isdigit( num.Last() ) )
254 num.RemoveLast();
255
256 // Trim non-digit from the start
257 if( !num.IsEmpty() && !isdigit( num.GetChar( 0 ) ) )
258 num = num.Right( num.Length() - 1 );
259 }
260
261 aTextEntry->SetSelection( ref.Find( num ), ref.Find( num ) + num.Length() );
262
263 if( num.IsEmpty() )
264 aTextEntry->SetSelection( -1, -1 );
265 }
266}
267
268
269bool KIUI::IsInputControlFocused( wxWindow* aFocus )
270{
271 if( aFocus == nullptr )
272 aFocus = wxWindow::FindFocus();
273
274 if( !aFocus )
275 return false;
276
277 // These widgets are never considered focused
278 if( aFocus->GetName() == s_FocusStealableInputName )
279 return false;
280
281 wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( aFocus );
282 wxStyledTextCtrl* styledText = dynamic_cast<wxStyledTextCtrl*>( aFocus );
283 wxListBox* listBox = dynamic_cast<wxListBox*>( aFocus );
284 wxSearchCtrl* searchCtrl = dynamic_cast<wxSearchCtrl*>( aFocus );
285 wxCheckBox* checkboxCtrl = dynamic_cast<wxCheckBox*>( aFocus );
286 wxChoice* choiceCtrl = dynamic_cast<wxChoice*>( aFocus );
287 wxRadioButton* radioBtn = dynamic_cast<wxRadioButton*>( aFocus );
288 wxSpinCtrl* spinCtrl = dynamic_cast<wxSpinCtrl*>( aFocus );
289 wxSpinCtrlDouble* spinDblCtrl = dynamic_cast<wxSpinCtrlDouble*>( aFocus );
290 wxSlider* sliderCtl = dynamic_cast<wxSlider*>( aFocus );
291
292 // Data view control is annoying, the focus is on a "wxDataViewCtrlMainWindow" class that
293 // is not formally exported via the header.
294 wxDataViewCtrl* dataViewCtrl = nullptr;
295
296 wxWindow* parent = aFocus->GetParent();
297
298 if( parent )
299 dataViewCtrl = dynamic_cast<wxDataViewCtrl*>( parent );
300
301 return ( textEntry || styledText || listBox || searchCtrl || checkboxCtrl || choiceCtrl
302 || radioBtn || spinCtrl || spinDblCtrl || sliderCtl || dataViewCtrl );
303}
304
305
306bool KIUI::IsInputControlEditable( wxWindow* aFocus )
307{
308 wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( aFocus );
309 wxStyledTextCtrl* styledText = dynamic_cast<wxStyledTextCtrl*>( aFocus );
310 wxSearchCtrl* searchCtrl = dynamic_cast<wxSearchCtrl*>( aFocus );
311
312 if( textEntry )
313 return textEntry->IsEditable();
314 else if( styledText )
315 return styledText->IsEditable();
316 else if( searchCtrl )
317 return searchCtrl->IsEditable();
318
319 return true; // Must return true if we can't determine the state, intentionally true for non inputs as well
320}
321
322
324{
325 return !Pgm().m_ModalDialogs.empty();
326}
327
328
329void KIUI::Disable( wxWindow* aWindow )
330{
331 wxScrollBar* scrollBar = dynamic_cast<wxScrollBar*>( aWindow );
332 wxGrid* grid = dynamic_cast<wxGrid*>( aWindow );
333 wxStyledTextCtrl* scintilla = dynamic_cast<wxStyledTextCtrl*>( aWindow );
334 wxControl* control = dynamic_cast<wxControl*>( aWindow );
335
336 if( scrollBar )
337 {
338 // Leave a scroll bar active
339 }
340 else if( grid )
341 {
342 for( int row = 0; row < grid->GetNumberRows(); ++row )
343 {
344 for( int col = 0; col < grid->GetNumberCols(); ++col )
345 grid->SetReadOnly( row, col );
346 }
347 }
348 else if( scintilla )
349 {
350 scintilla->SetReadOnly( true );
351 }
352 else if( control )
353 {
354 control->Disable();
355 }
356 else
357 {
358 for( wxWindow* child : aWindow->GetChildren() )
359 Disable( child );
360 }
361}
int KiIconScale(wxWindow *aWindow)
Return the automatic scale factor that would be used for a given window by KiScaledBitmap and KiScale...
Definition: bitmap.cpp:123
wxFont GetMonospacedUIFont()
Definition: ui_common.cpp:89
int GetStdMargin()
Get the standard margin around a widget in the KiCad UI.
Definition: ui_common.cpp:45
wxFont GetDockedPaneFont(wxWindow *aWindow)
Definition: ui_common.cpp:148
wxFont GetStatusFont(wxWindow *aWindow)
Definition: ui_common.cpp:136
bool IsInputControlFocused(wxWindow *aFocus=nullptr)
Check if a input control has focus.
Definition: ui_common.cpp:269
bool IsInputControlEditable(wxWindow *aControl)
Check if a input control has focus.
Definition: ui_common.cpp:306
wxString EllipsizeMenuText(const wxString &aString)
Ellipsize text (at the end) to be no more than 36 characters.
Definition: ui_common.cpp:219
wxFont GetInfoFont(wxWindow *aWindow)
Definition: ui_common.cpp:160
bool IsModalDialogFocused()
Definition: ui_common.cpp:323
wxFont GetControlFont(wxWindow *aWindow)
Definition: ui_common.cpp:166
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:201
const wxString s_FocusStealableInputName
Definition: ui_common.cpp:42
void SelectReferenceNumber(wxTextEntry *aTextEntry)
Select the number (or "?") in a reference for ease of editing.
Definition: ui_common.cpp:234
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:74
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:172
void Disable(wxWindow *aWindow)
Makes a window read-only.
Definition: ui_common.cpp:329
see class PGM_BASE
SEVERITY
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_IGNORE
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:115
const int scale
wxString UnescapeString(const wxString &aSource)
SEVERITY SeverityFromString(const wxString &aSeverity)
Definition: ui_common.cpp:52
wxString SeverityToString(const SEVERITY &aSeverity)
Definition: ui_common.cpp:63
wxFont getGUIFont(wxWindow *aWindow, int aRelativeSize)
Definition: ui_common.cpp:105
Functions to provide common constants and other functions to assist in making a consistent UI.
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:85