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 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/wx.h>
21 #include <wx/dataview.h>
22 #include <wx/stc/stc.h>
23 #include <widgets/ui_common.h>
24 
25 #include <algorithm>
26 #include <dialog_shim.h>
27 #include <pgm_base.h>
28 
30 {
31  // This is the value used in (most) wxFB dialogs
32  return 5;
33 }
34 
35 
36 SEVERITY SeverityFromString( const wxString& aSeverity )
37 {
38  if( aSeverity == wxT( "warning" ) )
39  return RPT_SEVERITY_WARNING;
40  else if( aSeverity == wxT( "ignore" ) )
41  return RPT_SEVERITY_IGNORE;
42  else
43  return RPT_SEVERITY_ERROR;
44 }
45 
46 
47 wxString SeverityToString( const SEVERITY& aSeverity )
48 {
49  if( aSeverity == RPT_SEVERITY_IGNORE )
50  return wxT( "ignore" );
51  else if( aSeverity == RPT_SEVERITY_WARNING )
52  return wxT( "warning" );
53  else
54  return wxT( "error" );
55 }
56 
57 
58 wxSize KIUI::GetTextSize( const wxString& aSingleLine, wxWindow* aWindow )
59 {
60  wxCoord width;
61  wxCoord height;
62 
63  {
64  wxClientDC dc( aWindow );
65  dc.SetFont( aWindow->GetFont() );
66  dc.GetTextExtent( aSingleLine, &width, &height );
67  }
68 
69  return wxSize( width, height );
70 }
71 
72 
73 bool KIUI::EnsureTextCtrlWidth( wxTextCtrl* aCtrl, const wxString* aString )
74 {
75  wxWindow* window = aCtrl->GetParent();
76 
77  if( !window )
78  window = aCtrl;
79 
80  wxString ctrlText;
81 
82  if( !aString )
83  {
84  ctrlText = aCtrl->GetValue();
85  aString = &ctrlText;
86  }
87 
88  wxSize textz = GetTextSize( *aString, window );
89  wxSize ctrlz = aCtrl->GetSize();
90 
91  if( ctrlz.GetWidth() < textz.GetWidth() + 10 )
92  {
93  ctrlz.SetWidth( textz.GetWidth() + 10 );
94  aCtrl->SetSizeHints( ctrlz );
95  return true;
96  }
97 
98  return false;
99 }
100 
101 
102 void KIUI::SelectReferenceNumber( wxTextEntry* aTextEntry )
103 {
104  wxString ref = aTextEntry->GetValue();
105 
106  if( ref.find_first_of( '?' ) != ref.npos )
107  {
108  aTextEntry->SetSelection( ref.find_first_of( '?' ), ref.find_last_of( '?' ) + 1 );
109  }
110  else if( ref.find_first_of( '*' ) != ref.npos )
111  {
112  aTextEntry->SetSelection( ref.find_first_of( '*' ), ref.find_last_of( '*' ) + 1 );
113  }
114  else
115  {
116  wxString num = ref;
117 
118  while( !num.IsEmpty() && ( !isdigit( num.Last() ) || !isdigit( num.GetChar( 0 ) ) ) )
119  {
120  // Trim non-digit from end
121  if( !isdigit( num.Last() ) )
122  num.RemoveLast();
123 
124  // Trim non-digit from the start
125  if( !num.IsEmpty() && !isdigit( num.GetChar( 0 ) ) )
126  num = num.Right( num.Length() - 1 );
127  }
128 
129  aTextEntry->SetSelection( ref.Find( num ), ref.Find( num ) + num.Length() );
130 
131  if( num.IsEmpty() )
132  aTextEntry->SetSelection( -1, -1 );
133  }
134 }
135 
136 
138 {
139  wxWindow* focus = wxWindow::FindFocus();
140  wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( focus );
141  wxStyledTextCtrl* styledText = dynamic_cast<wxStyledTextCtrl*>( focus );
142  wxListBox* listBox = dynamic_cast<wxListBox*>( focus );
143  wxDataViewCtrl* dataViewCtrl = dynamic_cast<wxDataViewCtrl*>( focus );
144 
145  return ( textEntry || styledText || listBox || dataViewCtrl );
146 }
147 
148 
150 {
151  return Pgm().m_ModalDialogCount > 0;
152 }
void SelectReferenceNumber(wxTextEntry *aTextEntry)
Select the number (or "?") in a reference for ease of editing.
Definition: ui_common.cpp:102
bool IsModalDialogFocused()
Definition: ui_common.cpp:149
SEVERITY
Definition: ui_common.h:83
int GetStdMargin()
Get the standard margin around a widget in the KiCad UI.
Definition: ui_common.cpp:29
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:102
bool IsInputControlFocused()
Checks if a input control has focus.
Definition: ui_common.cpp:137
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:58
Functions to provide common constants and other functions to assist in making a consistent UI.
bool EnsureTextCtrlWidth(wxTextCtrl *aCtrl, const wxString *aString=NULL)
Set the minimum pixel width on a text control in order to make a text string be fully visible within ...
Definition: ui_common.cpp:73
see class PGM_BASE
wxString SeverityToString(const SEVERITY &aSeverity)
Definition: ui_common.cpp:47
SEVERITY SeverityFromString(const wxString &aSeverity)
Definition: ui_common.cpp:36