2018-10-03 10:02:43 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2022-08-22 16:39:19 +00:00
|
|
|
* Copyright (C) 2018-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2018-10-03 10:02:43 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-06-07 22:38:35 +00:00
|
|
|
#include <wx/dcclient.h>
|
2021-07-06 03:47:16 +00:00
|
|
|
#include <wx/checkbox.h>
|
|
|
|
#include <wx/choice.h>
|
2021-06-07 22:38:35 +00:00
|
|
|
#include <wx/listbox.h>
|
2020-11-05 02:06:36 +00:00
|
|
|
#include <wx/dataview.h>
|
2021-07-06 03:47:16 +00:00
|
|
|
#include <wx/radiobut.h>
|
|
|
|
#include <wx/slider.h>
|
|
|
|
#include <wx/spinctrl.h>
|
2021-07-05 15:39:05 +00:00
|
|
|
#include <wx/srchctrl.h>
|
2020-11-05 02:06:36 +00:00
|
|
|
#include <wx/stc/stc.h>
|
2021-10-05 20:30:15 +00:00
|
|
|
#include <wx/scrolbar.h>
|
|
|
|
#include <wx/grid.h>
|
2018-10-03 10:02:43 +00:00
|
|
|
#include <widgets/ui_common.h>
|
|
|
|
|
2020-05-08 14:34:28 +00:00
|
|
|
#include <algorithm>
|
2020-11-26 15:43:48 +00:00
|
|
|
#include <dialog_shim.h>
|
|
|
|
#include <pgm_base.h>
|
2021-06-29 17:27:05 +00:00
|
|
|
#include <wx/settings.h>
|
2021-09-11 09:56:09 +00:00
|
|
|
#include <bitmaps/bitmap_types.h>
|
2022-08-22 16:39:19 +00:00
|
|
|
#include <string_utils.h>
|
2018-10-03 10:02:43 +00:00
|
|
|
|
2023-05-31 02:06:49 +00:00
|
|
|
|
|
|
|
const wxString KIUI::s_FocusStealableInputName = wxS( "KI_NOFOCUS");
|
|
|
|
|
|
|
|
|
2018-10-03 10:02:43 +00:00
|
|
|
int KIUI::GetStdMargin()
|
|
|
|
{
|
|
|
|
// This is the value used in (most) wxFB dialogs
|
|
|
|
return 5;
|
2020-02-28 00:05:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-08 02:19:46 +00:00
|
|
|
SEVERITY SeverityFromString( const wxString& aSeverity )
|
|
|
|
{
|
|
|
|
if( aSeverity == wxT( "warning" ) )
|
|
|
|
return RPT_SEVERITY_WARNING;
|
|
|
|
else if( aSeverity == wxT( "ignore" ) )
|
|
|
|
return RPT_SEVERITY_IGNORE;
|
|
|
|
else
|
|
|
|
return RPT_SEVERITY_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString SeverityToString( const SEVERITY& aSeverity )
|
|
|
|
{
|
|
|
|
if( aSeverity == RPT_SEVERITY_IGNORE )
|
|
|
|
return wxT( "ignore" );
|
|
|
|
else if( aSeverity == RPT_SEVERITY_WARNING )
|
|
|
|
return wxT( "warning" );
|
|
|
|
else
|
|
|
|
return wxT( "error" );
|
|
|
|
}
|
2020-10-25 16:41:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
wxSize KIUI::GetTextSize( const wxString& aSingleLine, wxWindow* aWindow )
|
|
|
|
{
|
|
|
|
wxCoord width;
|
|
|
|
wxCoord height;
|
|
|
|
|
|
|
|
{
|
|
|
|
wxClientDC dc( aWindow );
|
|
|
|
dc.SetFont( aWindow->GetFont() );
|
|
|
|
dc.GetTextExtent( aSingleLine, &width, &height );
|
|
|
|
}
|
|
|
|
|
|
|
|
return wxSize( width, height );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-29 17:27:05 +00:00
|
|
|
wxFont KIUI::GetMonospacedUIFont()
|
|
|
|
{
|
|
|
|
static int guiFontSize = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ).GetPointSize();
|
|
|
|
|
|
|
|
wxFont font( guiFontSize, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL );
|
|
|
|
|
|
|
|
#ifdef __WXMAC__
|
|
|
|
// https://trac.wxwidgets.org/ticket/19210
|
|
|
|
if( font.GetFaceName().IsEmpty() )
|
2023-01-17 04:14:38 +00:00
|
|
|
font.SetFaceName( wxS( "Menlo" ) );
|
2021-06-29 17:27:05 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-11 19:07:33 +00:00
|
|
|
wxFont getGUIFont( wxWindow* aWindow, int aRelativeSize )
|
2021-06-29 17:27:05 +00:00
|
|
|
{
|
2021-09-11 12:44:12 +00:00
|
|
|
wxFont font = aWindow->GetFont();
|
2021-09-10 14:59:27 +00:00
|
|
|
|
2021-09-11 12:44:12 +00:00
|
|
|
font.SetPointSize( font.GetPointSize() + aRelativeSize );
|
2021-09-10 14:59:27 +00:00
|
|
|
|
2021-09-16 17:29:29 +00:00
|
|
|
if( Pgm().GetCommonSettings()->m_Appearance.apply_icon_scale_to_fonts )
|
|
|
|
{
|
|
|
|
double icon_scale_fourths;
|
2021-09-10 14:59:27 +00:00
|
|
|
|
2021-09-16 17:29:29 +00:00
|
|
|
if( Pgm().GetCommonSettings()->m_Appearance.icon_scale <= 0 )
|
|
|
|
icon_scale_fourths = KiIconScale( aWindow );
|
|
|
|
else
|
|
|
|
icon_scale_fourths = Pgm().GetCommonSettings()->m_Appearance.icon_scale;
|
2021-09-11 09:56:09 +00:00
|
|
|
|
2021-09-16 17:29:29 +00:00
|
|
|
font.SetPointSize( KiROUND( icon_scale_fourths * font.GetPointSize() / 4.0 ) );
|
2021-09-11 12:44:12 +00:00
|
|
|
}
|
2021-06-29 17:27:05 +00:00
|
|
|
|
|
|
|
#ifdef __WXMAC__
|
|
|
|
// https://trac.wxwidgets.org/ticket/19210
|
|
|
|
if( font.GetFaceName().IsEmpty() )
|
2023-01-17 04:14:38 +00:00
|
|
|
font.SetFaceName( wxS( "San Francisco" ) );
|
2021-09-11 09:56:09 +00:00
|
|
|
// OSX 10.1 .. 10.9: Lucida Grande
|
|
|
|
// OSX 10.10: Helvetica Neue
|
|
|
|
// OSX 10.11 .. : San Francisco
|
2021-06-29 17:27:05 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-11 19:07:33 +00:00
|
|
|
wxFont KIUI::GetStatusFont( wxWindow* aWindow )
|
|
|
|
{
|
|
|
|
#ifdef __WXMAC__
|
|
|
|
int scale = -2;
|
|
|
|
#else
|
2021-09-12 11:17:22 +00:00
|
|
|
int scale = 0;
|
2021-09-11 19:07:33 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return getGUIFont( aWindow, scale );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-13 00:35:08 +00:00
|
|
|
wxFont KIUI::GetDockedPaneFont( wxWindow* aWindow )
|
|
|
|
{
|
|
|
|
#ifdef __WXMAC__
|
|
|
|
int scale = -1;
|
|
|
|
#else
|
|
|
|
int scale = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return getGUIFont( aWindow, scale );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-11 19:07:33 +00:00
|
|
|
wxFont KIUI::GetInfoFont( wxWindow* aWindow )
|
|
|
|
{
|
|
|
|
return getGUIFont( aWindow, -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxFont KIUI::GetControlFont( wxWindow* aWindow )
|
|
|
|
{
|
|
|
|
return getGUIFont( aWindow, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-25 16:41:38 +00:00
|
|
|
bool KIUI::EnsureTextCtrlWidth( wxTextCtrl* aCtrl, const wxString* aString )
|
|
|
|
{
|
|
|
|
wxWindow* window = aCtrl->GetParent();
|
|
|
|
|
|
|
|
if( !window )
|
|
|
|
window = aCtrl;
|
|
|
|
|
|
|
|
wxString ctrlText;
|
|
|
|
|
|
|
|
if( !aString )
|
|
|
|
{
|
|
|
|
ctrlText = aCtrl->GetValue();
|
|
|
|
aString = &ctrlText;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxSize textz = GetTextSize( *aString, window );
|
|
|
|
wxSize ctrlz = aCtrl->GetSize();
|
|
|
|
|
|
|
|
if( ctrlz.GetWidth() < textz.GetWidth() + 10 )
|
|
|
|
{
|
|
|
|
ctrlz.SetWidth( textz.GetWidth() + 10 );
|
|
|
|
aCtrl->SetSizeHints( ctrlz );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-22 16:39:19 +00:00
|
|
|
wxString KIUI::EllipsizeStatusText( wxWindow* aWindow, const wxString& aString )
|
|
|
|
{
|
|
|
|
wxString msg = UnescapeString( aString );
|
|
|
|
|
|
|
|
msg.Replace( wxT( "\n" ), wxT( " " ) );
|
|
|
|
msg.Replace( wxT( "\r" ), wxT( " " ) );
|
|
|
|
msg.Replace( wxT( "\t" ), wxT( " " ) );
|
|
|
|
|
|
|
|
wxClientDC dc( aWindow );
|
2022-08-22 16:57:55 +00:00
|
|
|
int statusWidth = aWindow->GetSize().GetWidth();
|
|
|
|
|
|
|
|
// 30% of the first 800 pixels plus 60% of the remaining width
|
|
|
|
int textWidth = std::min( statusWidth, 800 ) * 0.3 + std::max( statusWidth - 800, 0 ) * 0.6;
|
|
|
|
|
|
|
|
return wxControl::Ellipsize( msg, dc, wxELLIPSIZE_END, textWidth );
|
2022-08-22 16:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString KIUI::EllipsizeMenuText( const wxString& aString )
|
|
|
|
{
|
|
|
|
wxString msg = UnescapeString( aString );
|
|
|
|
|
|
|
|
msg.Replace( wxT( "\n" ), wxT( " " ) );
|
|
|
|
msg.Replace( wxT( "\r" ), wxT( " " ) );
|
|
|
|
msg.Replace( wxT( "\t" ), wxT( " " ) );
|
|
|
|
|
|
|
|
if( msg.Length() > 36 )
|
|
|
|
msg = msg.Left( 34 ) + wxT( "..." );
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-25 16:41:38 +00:00
|
|
|
void KIUI::SelectReferenceNumber( wxTextEntry* aTextEntry )
|
|
|
|
{
|
|
|
|
wxString ref = aTextEntry->GetValue();
|
|
|
|
|
|
|
|
if( ref.find_first_of( '?' ) != ref.npos )
|
|
|
|
{
|
|
|
|
aTextEntry->SetSelection( ref.find_first_of( '?' ), ref.find_last_of( '?' ) + 1 );
|
|
|
|
}
|
2020-11-25 16:25:26 +00:00
|
|
|
else if( ref.find_first_of( '*' ) != ref.npos )
|
|
|
|
{
|
|
|
|
aTextEntry->SetSelection( ref.find_first_of( '*' ), ref.find_last_of( '*' ) + 1 );
|
|
|
|
}
|
2020-10-25 16:41:38 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
wxString num = ref;
|
|
|
|
|
|
|
|
while( !num.IsEmpty() && ( !isdigit( num.Last() ) || !isdigit( num.GetChar( 0 ) ) ) )
|
|
|
|
{
|
|
|
|
// Trim non-digit from end
|
|
|
|
if( !isdigit( num.Last() ) )
|
|
|
|
num.RemoveLast();
|
|
|
|
|
|
|
|
// Trim non-digit from the start
|
|
|
|
if( !num.IsEmpty() && !isdigit( num.GetChar( 0 ) ) )
|
|
|
|
num = num.Right( num.Length() - 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
aTextEntry->SetSelection( ref.Find( num ), ref.Find( num ) + num.Length() );
|
|
|
|
|
|
|
|
if( num.IsEmpty() )
|
|
|
|
aTextEntry->SetSelection( -1, -1 );
|
|
|
|
}
|
2020-11-05 02:06:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-05 23:47:03 +00:00
|
|
|
bool KIUI::IsInputControlFocused( wxWindow* aFocus )
|
2020-11-05 02:06:36 +00:00
|
|
|
{
|
2021-07-05 23:47:03 +00:00
|
|
|
if( aFocus == nullptr )
|
|
|
|
aFocus = wxWindow::FindFocus();
|
2021-07-05 15:39:05 +00:00
|
|
|
|
2021-07-05 23:47:03 +00:00
|
|
|
if( !aFocus )
|
2021-07-05 17:44:52 +00:00
|
|
|
return false;
|
|
|
|
|
2023-05-31 02:06:49 +00:00
|
|
|
// These widgets are never considered focused
|
|
|
|
if( aFocus->GetName() == s_FocusStealableInputName )
|
|
|
|
return false;
|
|
|
|
|
2021-07-05 23:47:03 +00:00
|
|
|
wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( aFocus );
|
|
|
|
wxStyledTextCtrl* styledText = dynamic_cast<wxStyledTextCtrl*>( aFocus );
|
|
|
|
wxListBox* listBox = dynamic_cast<wxListBox*>( aFocus );
|
|
|
|
wxSearchCtrl* searchCtrl = dynamic_cast<wxSearchCtrl*>( aFocus );
|
2021-07-06 03:47:16 +00:00
|
|
|
wxCheckBox* checkboxCtrl = dynamic_cast<wxCheckBox*>( aFocus );
|
|
|
|
wxChoice* choiceCtrl = dynamic_cast<wxChoice*>( aFocus );
|
|
|
|
wxRadioButton* radioBtn = dynamic_cast<wxRadioButton*>( aFocus );
|
|
|
|
wxSpinCtrl* spinCtrl = dynamic_cast<wxSpinCtrl*>( aFocus );
|
|
|
|
wxSpinCtrlDouble* spinDblCtrl = dynamic_cast<wxSpinCtrlDouble*>( aFocus );
|
|
|
|
wxSlider* sliderCtl = dynamic_cast<wxSlider*>( aFocus );
|
2021-07-05 17:44:52 +00:00
|
|
|
|
2021-09-08 20:34:02 +00:00
|
|
|
// Data view control is annoying, the focus is on a "wxDataViewCtrlMainWindow" class that
|
2022-10-03 16:53:13 +00:00
|
|
|
// is not formally exported via the header.
|
2021-07-05 17:44:52 +00:00
|
|
|
wxDataViewCtrl* dataViewCtrl = nullptr;
|
|
|
|
|
2021-07-05 23:47:03 +00:00
|
|
|
wxWindow* parent = aFocus->GetParent();
|
2021-07-05 17:44:52 +00:00
|
|
|
|
|
|
|
if( parent )
|
|
|
|
dataViewCtrl = dynamic_cast<wxDataViewCtrl*>( parent );
|
2020-11-05 02:06:36 +00:00
|
|
|
|
2021-09-08 20:34:02 +00:00
|
|
|
return ( textEntry || styledText || listBox || searchCtrl || checkboxCtrl || choiceCtrl
|
|
|
|
|| radioBtn || spinCtrl || spinDblCtrl || sliderCtl || dataViewCtrl );
|
2020-11-26 15:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-05 23:47:03 +00:00
|
|
|
bool KIUI::IsInputControlEditable( wxWindow* aFocus )
|
|
|
|
{
|
|
|
|
wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( aFocus );
|
|
|
|
wxStyledTextCtrl* styledText = dynamic_cast<wxStyledTextCtrl*>( aFocus );
|
|
|
|
wxSearchCtrl* searchCtrl = dynamic_cast<wxSearchCtrl*>( aFocus );
|
|
|
|
|
|
|
|
if( textEntry )
|
|
|
|
return textEntry->IsEditable();
|
|
|
|
else if( styledText )
|
|
|
|
return styledText->IsEditable();
|
|
|
|
else if( searchCtrl )
|
|
|
|
return searchCtrl->IsEditable();
|
|
|
|
|
|
|
|
return true; // Must return true if we can't determine the state, intentionally true for non inputs as well
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-26 15:43:48 +00:00
|
|
|
bool KIUI::IsModalDialogFocused()
|
|
|
|
{
|
2023-01-12 19:51:02 +00:00
|
|
|
return !Pgm().m_ModalDialogs.empty();
|
2021-10-05 20:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void KIUI::Disable( wxWindow* aWindow )
|
|
|
|
{
|
|
|
|
wxScrollBar* scrollBar = dynamic_cast<wxScrollBar*>( aWindow );
|
|
|
|
wxGrid* grid = dynamic_cast<wxGrid*>( aWindow );
|
|
|
|
wxStyledTextCtrl* scintilla = dynamic_cast<wxStyledTextCtrl*>( aWindow );
|
|
|
|
wxControl* control = dynamic_cast<wxControl*>( aWindow );
|
|
|
|
|
|
|
|
if( scrollBar )
|
|
|
|
{
|
|
|
|
// Leave a scroll bar active
|
|
|
|
}
|
|
|
|
else if( grid )
|
|
|
|
{
|
|
|
|
for( int row = 0; row < grid->GetNumberRows(); ++row )
|
|
|
|
{
|
|
|
|
for( int col = 0; col < grid->GetNumberCols(); ++col )
|
|
|
|
grid->SetReadOnly( row, col );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( scintilla )
|
|
|
|
{
|
|
|
|
scintilla->SetReadOnly( true );
|
|
|
|
}
|
|
|
|
else if( control )
|
|
|
|
{
|
|
|
|
control->Disable();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( wxWindow* child : aWindow->GetChildren() )
|
|
|
|
Disable( child );
|
|
|
|
}
|
2023-01-12 19:51:02 +00:00
|
|
|
}
|