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-2011 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 
31 #include <msgpanel.h>
32 
33 #include <wx/dcscreen.h>
34 #include <wx/dcclient.h>
35 #include <wx/settings.h>
36 
37 
38 BEGIN_EVENT_TABLE( EDA_MSG_PANEL, wxPanel )
39  EVT_PAINT( EDA_MSG_PANEL::OnPaint )
40 END_EVENT_TABLE()
41 
42 
43 EDA_MSG_PANEL::EDA_MSG_PANEL( wxWindow* aParent, int aId,
44  const wxPoint& aPosition, const wxSize& aSize,
45  long style, const wxString &name ) :
46  wxPanel( aParent, aId, aPosition, aSize, style, name )
47 {
48  SetFont( wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ) );
49  SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
50  m_last_x = 0;
51 
52  m_fontSize = computeFontSize();
53 }
54 
55 
57 {
58 }
59 
60 
62 {
63  // Get size of the wxSYS_DEFAULT_GUI_FONT
64  wxSize fontSizeInPixels;
65 
66  wxScreenDC dc;
67 
68  dc.SetFont( wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ) );
69  dc.GetTextExtent( wxT( "W" ), &fontSizeInPixels.x, &fontSizeInPixels.y );
70 
71  return fontSizeInPixels;
72 }
73 
74 
76 {
77  // make space for two rows of text plus a number of pixels between them.
78  return 2 * computeFontSize().y + 0;
79 }
80 
81 
82 wxSize EDA_MSG_PANEL::computeTextSize( const wxString& aText ) const
83 {
84  // Get size of the wxSYS_DEFAULT_GUI_FONT
85  wxSize textSizeInPixels;
86 
87  wxScreenDC dc;
88 
89  dc.SetFont( wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ) );
90  dc.GetTextExtent( aText, &textSizeInPixels.x, &textSizeInPixels.y );
91 
92  return textSizeInPixels;
93 }
94 
95 
96 void EDA_MSG_PANEL::OnPaint( wxPaintEvent& aEvent )
97 {
98  wxPaintDC dc( this );
99 
100  erase( &dc );
101 
102  dc.SetBackground( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
103  dc.SetBackgroundMode( wxSOLID );
104  dc.SetTextBackground( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
105  dc.SetFont( wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ) );
106 
107  for( unsigned i=0; i<m_Items.size(); ++i )
108  showItem( dc, m_Items[i] );
109 
110  aEvent.Skip();
111 }
112 
113 
114 void EDA_MSG_PANEL::AppendMessage( const wxString& aUpperText,
115  const wxString& aLowerText,
116  COLOR4D aColor, int aPad )
117 {
118  wxString text;
119  wxSize drawSize = GetClientSize();
120 
121  text = ( aUpperText.Len() > aLowerText.Len() ) ? aUpperText : aLowerText;
122  text.Append( ' ', aPad );
123 
124  MSG_PANEL_ITEM item;
125 
126  /* Don't put the first message a window client position 0. Offset by
127  * one 'W' character width. */
128  if( m_last_x == 0 )
129  m_last_x = m_fontSize.x;
130 
131  item.m_X = m_last_x;
132 
133  item.m_UpperY = ( drawSize.y / 2 ) - m_fontSize.y;
134  item.m_LowerY = drawSize.y - m_fontSize.y;
135 
136  item.m_UpperText = aUpperText;
137  item.m_LowerText = aLowerText;
138  item.m_Color = aColor;
139  m_Items.push_back( item );
140  m_last_x += computeTextSize( text ).x;
141 
142  // Add an extra space between texts for a better look:
143  m_last_x += m_fontSize.x;
144 
145  Refresh();
146 }
147 
148 
149 void EDA_MSG_PANEL::SetMessage( int aXPosition, const wxString& aUpperText,
150  const wxString& aLowerText, COLOR4D aColor )
151 {
152  wxPoint pos;
153  wxSize drawSize = GetClientSize();
154 
155  if( aXPosition >= 0 )
156  m_last_x = pos.x = aXPosition * (m_fontSize.x + 2);
157  else
158  pos.x = m_last_x;
159 
160  MSG_PANEL_ITEM item;
161 
162  item.m_X = pos.x;
163 
164  item.m_UpperY = (drawSize.y / 2) - m_fontSize.y;
165  item.m_LowerY = drawSize.y - m_fontSize.y;
166 
167  item.m_UpperText = aUpperText;
168  item.m_LowerText = aLowerText;
169  item.m_Color = aColor;
170 
171  int ndx;
172 
173  // update the vector, which is sorted by m_X
174  int limit = m_Items.size();
175 
176  for( ndx=0; ndx<limit; ++ndx )
177  {
178  // replace any item with same X
179  if( m_Items[ndx].m_X == item.m_X )
180  {
181  m_Items[ndx] = item;
182  break;
183  }
184 
185  if( m_Items[ndx].m_X > item.m_X )
186  {
187  m_Items.insert( m_Items.begin() + ndx, item );
188  break;
189  }
190  }
191 
192  if( ndx == limit ) // mutually exclusive with two above if tests
193  {
194  m_Items.push_back( item );
195  }
196 
197  Refresh();
198 }
199 
200 
201 void EDA_MSG_PANEL::showItem( wxDC& aDC, const MSG_PANEL_ITEM& aItem )
202 {
203  // COLOR4D color = aItem.m_Color;
204  COLOR4D color = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
205 
206  aDC.SetTextForeground( color.ToColour() );
207 
208  if( !aItem.m_UpperText.IsEmpty() )
209  {
210  aDC.DrawText( aItem.m_UpperText, aItem.m_X, aItem.m_UpperY );
211  }
212 
213  if( !aItem.m_LowerText.IsEmpty() )
214  {
215  aDC.DrawText( aItem.m_LowerText, aItem.m_X, aItem.m_LowerY );
216  }
217 }
218 
219 
221 {
222  m_Items.clear();
223  m_last_x = 0;
224  Refresh();
225 }
226 
227 
228 void EDA_MSG_PANEL::erase( wxDC* aDC )
229 {
230  wxPen pen;
231  wxBrush brush;
232 
233  wxSize size = GetClientSize();
234  wxColour color = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
235 
236  pen.SetColour( color );
237 
238  brush.SetColour( color );
239  brush.SetStyle( wxBRUSHSTYLE_SOLID );
240 
241  aDC->SetPen( pen );
242  aDC->SetBrush( brush );
243  aDC->DrawRectangle( 0, 0, size.x, size.y );
244 }
void showItem(wxDC &dc, const MSG_PANEL_ITEM &aItem)
Definition: msgpanel.cpp:201
COLOR4D m_Color
Definition: msgpanel.h:61
MSG_PANEL_ITEMS m_Items
Definition: msgpanel.h:115
int color
Definition: DXF_plotter.cpp:60
wxString m_LowerText
Definition: msgpanel.h:60
EDA_MSG_PANEL is a panel to display various information messages.
Definition: msgpanel.h:112
void OnPaint(wxPaintEvent &aEvent)
Definition: msgpanel.cpp:96
wxSize m_fontSize
Definition: msgpanel.h:117
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
wxSize computeTextSize(const wxString &text) const
Calculate the width and height of a text string using the system UI font.
Definition: msgpanel.cpp:82
void EraseMsgBox()
Definition: msgpanel.cpp:220
int m_last_x
the last used x coordinate
Definition: msgpanel.h:116
void SetMessage(int aXPosition, const wxString &aUpperText, const wxString &aLowerText, COLOR4D aColor)
Function SetMessage sets a message at aXPosition to aUpperText and aLowerText in the message panel.
Definition: msgpanel.cpp:149
void AppendMessage(const wxString &aUpperText, const wxString &aLowerText, COLOR4D aColor, int aPad=6)
Function AppendMessage appends a message to the message panel.
Definition: msgpanel.cpp:114
void erase(wxDC *DC)
Definition: msgpanel.cpp:228
const char * name
Definition: DXF_plotter.cpp:59
static wxSize computeFontSize()
Function getFontSize computes the height and width of a 'W' in the system font.
Definition: msgpanel.cpp:61
EDA_MSG_ITEM is used EDA_MSG_PANEL as the item type for displaying messages.
Definition: msgpanel.h:54
Message panel definition file.
static int GetRequiredHeight()
Function GetRequiredHeight returns the required height (in pixels) of a EDA_MSG_PANEL.
Definition: msgpanel.cpp:75
wxString m_UpperText
Definition: msgpanel.h:59
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:100