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