KiCad PCB EDA Suite
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
42{
43 // This is the value used in (most) wxFB dialogs
44 return 5;
45}
46
47
48SEVERITY SeverityFromString( const wxString& aSeverity )
49{
50 if( aSeverity == wxT( "warning" ) )
52 else if( aSeverity == wxT( "ignore" ) )
54 else
55 return RPT_SEVERITY_ERROR;
56}
57
58
59wxString SeverityToString( const SEVERITY& aSeverity )
60{
61 if( aSeverity == RPT_SEVERITY_IGNORE )
62 return wxT( "ignore" );
63 else if( aSeverity == RPT_SEVERITY_WARNING )
64 return wxT( "warning" );
65 else
66 return wxT( "error" );
67}
68
69
70wxSize KIUI::GetTextSize( const wxString& aSingleLine, wxWindow* aWindow )
71{
72 wxCoord width;
73 wxCoord height;
74
75 {
76 wxClientDC dc( aWindow );
77 dc.SetFont( aWindow->GetFont() );
78 dc.GetTextExtent( aSingleLine, &width, &height );
79 }
80
81 return wxSize( width, height );
82}
83
84
86{
87 static int guiFontSize = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ).GetPointSize();
88
89 wxFont font( guiFontSize, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL );
90
91#ifdef __WXMAC__
92 // https://trac.wxwidgets.org/ticket/19210
93 if( font.GetFaceName().IsEmpty() )
94 font.SetFaceName( wxS( "Menlo" ) );
95#endif
96
97 return font;
98}
99
100
101wxFont getGUIFont( wxWindow* aWindow, int aRelativeSize )
102{
103 wxFont font = aWindow->GetFont();
104
105 font.SetPointSize( font.GetPointSize() + aRelativeSize );
106
107 if( Pgm().GetCommonSettings()->m_Appearance.apply_icon_scale_to_fonts )
108 {
109 double icon_scale_fourths;
110
111 if( Pgm().GetCommonSettings()->m_Appearance.icon_scale <= 0 )
112 icon_scale_fourths = KiIconScale( aWindow );
113 else
114 icon_scale_fourths = Pgm().GetCommonSettings()->m_Appearance.icon_scale;
115
116 font.SetPointSize( KiROUND( icon_scale_fourths * font.GetPointSize() / 4.0 ) );
117 }
118
119#ifdef __WXMAC__
120 // https://trac.wxwidgets.org/ticket/19210
121 if( font.GetFaceName().IsEmpty() )
122 font.SetFaceName( wxS( "San Francisco" ) );
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 wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( aFocus );
274 wxStyledTextCtrl* styledText = dynamic_cast<wxStyledTextCtrl*>( aFocus );
275 wxListBox* listBox = dynamic_cast<wxListBox*>( aFocus );
276 wxSearchCtrl* searchCtrl = dynamic_cast<wxSearchCtrl*>( aFocus );
277 wxCheckBox* checkboxCtrl = dynamic_cast<wxCheckBox*>( aFocus );
278 wxChoice* choiceCtrl = dynamic_cast<wxChoice*>( aFocus );
279 wxRadioButton* radioBtn = dynamic_cast<wxRadioButton*>( aFocus );
280 wxSpinCtrl* spinCtrl = dynamic_cast<wxSpinCtrl*>( aFocus );
281 wxSpinCtrlDouble* spinDblCtrl = dynamic_cast<wxSpinCtrlDouble*>( aFocus );
282 wxSlider* sliderCtl = dynamic_cast<wxSlider*>( aFocus );
283
284 // Data view control is annoying, the focus is on a "wxDataViewCtrlMainWindow" class that
285 // is not formally exported via the header.
286 wxDataViewCtrl* dataViewCtrl = nullptr;
287
288 wxWindow* parent = aFocus->GetParent();
289
290 if( parent )
291 dataViewCtrl = dynamic_cast<wxDataViewCtrl*>( parent );
292
293 return ( textEntry || styledText || listBox || searchCtrl || checkboxCtrl || choiceCtrl
294 || radioBtn || spinCtrl || spinDblCtrl || sliderCtl || dataViewCtrl );
295}
296
297
298bool KIUI::IsInputControlEditable( wxWindow* aFocus )
299{
300 wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( aFocus );
301 wxStyledTextCtrl* styledText = dynamic_cast<wxStyledTextCtrl*>( aFocus );
302 wxSearchCtrl* searchCtrl = dynamic_cast<wxSearchCtrl*>( aFocus );
303
304 if( textEntry )
305 return textEntry->IsEditable();
306 else if( styledText )
307 return styledText->IsEditable();
308 else if( searchCtrl )
309 return searchCtrl->IsEditable();
310
311 return true; // Must return true if we can't determine the state, intentionally true for non inputs as well
312}
313
314
316{
317 return !Pgm().m_ModalDialogs.empty();
318}
319
320
321void KIUI::Disable( wxWindow* aWindow )
322{
323 wxScrollBar* scrollBar = dynamic_cast<wxScrollBar*>( aWindow );
324 wxGrid* grid = dynamic_cast<wxGrid*>( aWindow );
325 wxStyledTextCtrl* scintilla = dynamic_cast<wxStyledTextCtrl*>( aWindow );
326 wxControl* control = dynamic_cast<wxControl*>( aWindow );
327
328 if( scrollBar )
329 {
330 // Leave a scroll bar active
331 }
332 else if( grid )
333 {
334 for( int row = 0; row < grid->GetNumberRows(); ++row )
335 {
336 for( int col = 0; col < grid->GetNumberCols(); ++col )
337 grid->SetReadOnly( row, col );
338 }
339 }
340 else if( scintilla )
341 {
342 scintilla->SetReadOnly( true );
343 }
344 else if( control )
345 {
346 control->Disable();
347 }
348 else
349 {
350 for( wxWindow* child : aWindow->GetChildren() )
351 Disable( child );
352 }
353}
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:85
int GetStdMargin()
Get the standard margin around a widget in the KiCad UI.
Definition: ui_common.cpp:41
wxFont GetDockedPaneFont(wxWindow *aWindow)
Definition: ui_common.cpp:144
wxFont GetStatusFont(wxWindow *aWindow)
Definition: ui_common.cpp:132
bool IsInputControlFocused(wxWindow *aFocus=nullptr)
Check if a input control has focus.
Definition: ui_common.cpp:265
bool IsInputControlEditable(wxWindow *aControl)
Check if a input control has focus.
Definition: ui_common.cpp:298
wxString EllipsizeMenuText(const wxString &aString)
Ellipsize text (at the end) to be no more than 36 characters.
Definition: ui_common.cpp:215
wxFont GetInfoFont(wxWindow *aWindow)
Definition: ui_common.cpp:156
bool IsModalDialogFocused()
Definition: ui_common.cpp:315
wxFont GetControlFont(wxWindow *aWindow)
Definition: ui_common.cpp:162
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
void SelectReferenceNumber(wxTextEntry *aTextEntry)
Select the number (or "?") in a reference for ease of editing.
Definition: ui_common.cpp:230
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:70
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
void Disable(wxWindow *aWindow)
Makes a window read-only.
Definition: ui_common.cpp:321
must_if< error >::control< Rule > control
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:111
const int scale
wxString UnescapeString(const wxString &aSource)
SEVERITY SeverityFromString(const wxString &aSeverity)
Definition: ui_common.cpp:48
wxString SeverityToString(const SEVERITY &aSeverity)
Definition: ui_common.cpp:59
wxFont getGUIFont(wxWindow *aWindow, int aRelativeSize)
Definition: ui_common.cpp:101
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