KiCad PCB EDA Suite
msgpanel.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) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
5  * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <widgets/msgpanel.h>
27 
28 #include <wx/dcscreen.h>
29 #include <wx/dcclient.h>
30 #include <wx/settings.h>
31 #include <wx/toplevel.h>
32 
33 #include <widgets/ui_common.h>
34 
35 
36 BEGIN_EVENT_TABLE( EDA_MSG_PANEL, wxPanel )
37  EVT_PAINT( EDA_MSG_PANEL::OnPaint )
38 END_EVENT_TABLE()
39 
40 
41 EDA_MSG_PANEL::EDA_MSG_PANEL( wxWindow* aParent, int aId,
42  const wxPoint& aPosition, const wxSize& aSize,
43  long style, const wxString &name ) :
44  wxPanel( aParent, aId, aPosition, aSize, style, name )
45 {
46  SetFont( KIUI::GetStatusFont( this ) );
47  SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
48 
49  // informs wx not to paint the background itself as we will paint it later in erase()
50  SetBackgroundStyle( wxBG_STYLE_PAINT );
51 
52  m_last_x = 0;
53 
54  m_fontSize = GetTextExtent( wxT( "W" ) );
55 }
56 
57 
59 {
60 }
61 
62 
63 int EDA_MSG_PANEL::GetRequiredHeight( wxWindow* aWindow )
64 {
65  wxSize fontSizeInPixels;
66  wxScreenDC dc;
67 
68  dc.SetFont( KIUI::GetControlFont( aWindow ) );
69  dc.GetTextExtent( wxT( "W" ), &fontSizeInPixels.x, &fontSizeInPixels.y );
70 
71  // make space for two rows of text plus a number of pixels between them.
72  return 2 * fontSizeInPixels.y + 0;
73 }
74 
75 
76 void EDA_MSG_PANEL::OnPaint( wxPaintEvent& aEvent )
77 {
78  wxPaintDC dc( this );
79 
80  erase( &dc );
81 
82  dc.SetBackground( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
83  dc.SetBackgroundMode( wxSOLID );
84  dc.SetTextBackground( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
85  dc.SetFont( KIUI::GetControlFont( this ) );
86 
87  for( const MSG_PANEL_ITEM& item : m_Items )
88  showItem( dc, item );
89 
90  aEvent.Skip();
91 }
92 
93 
94 void EDA_MSG_PANEL::AppendMessage( const wxString& aUpperText, const wxString& aLowerText,
95  int aPadding )
96 {
97  wxString text;
98  wxSize drawSize = GetClientSize();
99 
100  text = ( aUpperText.Len() > aLowerText.Len() ) ? aUpperText : aLowerText;
101  text.Append( ' ', aPadding );
102 
103  MSG_PANEL_ITEM item;
104 
105  /* Don't put the first message a window client position 0. Offset by
106  * one 'W' character width. */
107  if( m_last_x == 0 )
108  m_last_x = m_fontSize.x;
109 
110  item.m_X = m_last_x;
111 
112  item.m_UpperY = ( drawSize.y / 2 ) - m_fontSize.y;
113  item.m_LowerY = drawSize.y - m_fontSize.y;
114 
115  item.m_UpperText = aUpperText;
116  item.m_LowerText = aLowerText;
117  m_Items.push_back( item );
118  m_last_x += GetTextExtent( text ).x;
119 
120  // Add an extra space between texts for a better look:
121  m_last_x += m_fontSize.x;
122 
123  Refresh();
124 }
125 
126 
127 void EDA_MSG_PANEL::SetMessage( int aXPosition, const wxString& aUpperText,
128  const wxString& aLowerText )
129 {
130  wxPoint pos;
131  wxSize drawSize = GetClientSize();
132 
133  if( aXPosition >= 0 )
134  m_last_x = pos.x = aXPosition * (m_fontSize.x + 2);
135  else
136  pos.x = m_last_x;
137 
138  MSG_PANEL_ITEM item;
139 
140  item.m_X = pos.x;
141 
142  item.m_UpperY = (drawSize.y / 2) - m_fontSize.y;
143  item.m_LowerY = drawSize.y - m_fontSize.y;
144 
145  item.m_UpperText = aUpperText;
146  item.m_LowerText = aLowerText;
147 
148  int ndx;
149 
150  // update the vector, which is sorted by m_X
151  int limit = m_Items.size();
152 
153  for( ndx=0; ndx<limit; ++ndx )
154  {
155  // replace any item with same X
156  if( m_Items[ndx].m_X == item.m_X )
157  {
158  m_Items[ndx] = item;
159  break;
160  }
161 
162  if( m_Items[ndx].m_X > item.m_X )
163  {
164  m_Items.insert( m_Items.begin() + ndx, item );
165  break;
166  }
167  }
168 
169  if( ndx == limit ) // mutually exclusive with two above if tests
170  m_Items.push_back( item );
171 
172  Refresh();
173 }
174 
175 
176 void EDA_MSG_PANEL::showItem( wxDC& aDC, const MSG_PANEL_ITEM& aItem )
177 {
178  COLOR4D color;
179 
180  // Change the text to a disabled color when the window isn't active
181  wxTopLevelWindow* tlw = dynamic_cast<wxTopLevelWindow*>( wxGetTopLevelParent( this ) );
182 
183  if( tlw && !tlw->IsActive() )
184  color = wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT );
185  else
186  color = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
187 
188  aDC.SetTextForeground( color.ToColour() );
189 
190  if( !aItem.m_UpperText.IsEmpty() )
191  aDC.DrawText( aItem.m_UpperText, aItem.m_X, aItem.m_UpperY );
192 
193  if( !aItem.m_LowerText.IsEmpty() )
194  aDC.DrawText( aItem.m_LowerText, aItem.m_X, aItem.m_LowerY );
195 }
196 
197 
199 {
200  m_Items.clear();
201  m_last_x = 0;
202  Refresh();
203 }
204 
205 
206 void EDA_MSG_PANEL::erase( wxDC* aDC )
207 {
208  wxPen pen;
209  wxBrush brush;
210 
211  wxSize size = GetClientSize();
212  wxColour color = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
213 
214  pen.SetColour( color );
215 
216  brush.SetColour( color );
217  brush.SetStyle( wxBRUSHSTYLE_SOLID );
218 
219  aDC->SetPen( pen );
220  aDC->SetBrush( brush );
221  aDC->DrawRectangle( 0, 0, size.x, size.y );
222 }
void showItem(wxDC &dc, const MSG_PANEL_ITEM &aItem)
Definition: msgpanel.cpp:176
int color
Definition: DXF_plotter.cpp:57
static int GetRequiredHeight(wxWindow *aWindow)
Return the required height (in pixels) of a EDA_MSG_PANEL.
Definition: msgpanel.cpp:63
void SetMessage(int aXPosition, const wxString &aUpperText, const wxString &aLowerText)
Set a message at aXPosition to aUpperText and aLowerText in the message panel.
Definition: msgpanel.cpp:127
wxString m_LowerText
Definition: msgpanel.h:92
wxFont GetControlFont(wxWindow *aWindow)
Definition: ui_common.cpp:150
A panel to display various information messages.
Definition: msgpanel.h:100
void OnPaint(wxPaintEvent &aEvent)
Definition: msgpanel.cpp:76
wxSize m_fontSize
Definition: msgpanel.h:169
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
Functions to provide common constants and other functions to assist in making a consistent UI.
void EraseMsgBox()
Definition: msgpanel.cpp:198
std::vector< MSG_PANEL_ITEM > m_Items
Definition: msgpanel.h:167
int m_last_x
the last used x coordinate
Definition: msgpanel.h:168
wxFont GetStatusFont(wxWindow *aWindow)
Definition: ui_common.cpp:132
void erase(wxDC *DC)
Definition: msgpanel.cpp:206
const char * name
Definition: DXF_plotter.cpp:56
EDA_MSG_PANEL items for displaying messages.
Definition: msgpanel.h:53
void AppendMessage(const wxString &aUpperText, const wxString &aLowerText, int aPadding=6)
Append a message to the message panel.
Definition: msgpanel.cpp:94
Message panel definition file.
wxString m_UpperText
Definition: msgpanel.h:91
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103