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