msgpanel.cpp 6.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
 * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
 * Copyright (C) 1992-2011 KiCad Developers, see AUTHORS.txt for contributors.
 *
 * 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 2
 * 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, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */
plyatov's avatar
plyatov committed
25

26 27 28 29 30
/**
 * @file msgpanel.cpp
 * @brief Message panel implementation file.
 */

plyatov's avatar
plyatov committed
31 32 33 34
#ifdef __GNUG__
#pragma implementation
#endif

35 36

#include <msgpanel.h>
37

plyatov's avatar
plyatov committed
38

39 40
BEGIN_EVENT_TABLE( EDA_MSG_PANEL, wxPanel )
    EVT_PAINT( EDA_MSG_PANEL::OnPaint )
plyatov's avatar
plyatov committed
41 42 43
END_EVENT_TABLE()


44 45 46
EDA_MSG_PANEL::EDA_MSG_PANEL( wxWindow* aParent, int aId,
                              const wxPoint& aPosition, const wxSize& aSize ) :
    wxPanel( aParent, aId, aPosition, aSize )
plyatov's avatar
plyatov committed
47
{
48 49
    SetFont( wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ) );
    SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
dickelbeck's avatar
dickelbeck committed
50
    m_last_x = 0;
51 52

    m_fontSize = computeFontSize();
plyatov's avatar
plyatov committed
53 54 55
}


56
EDA_MSG_PANEL::~EDA_MSG_PANEL()
plyatov's avatar
plyatov committed
57 58 59 60
{
}


61
wxSize EDA_MSG_PANEL::computeFontSize()
62 63
{
    // Get size of the wxSYS_DEFAULT_GUI_FONT
64
    wxSize     fontSizeInPixels;
65 66 67 68 69 70 71 72 73 74

    wxScreenDC dc;

    dc.SetFont( wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ) );
    dc.GetTextExtent( wxT( "W" ), &fontSizeInPixels.x, &fontSizeInPixels.y );

    return fontSizeInPixels;
}


75
int EDA_MSG_PANEL::GetRequiredHeight()
76 77 78 79 80 81
{
    // make space for two rows of text plus a number of pixels between them.
    return 2 * computeFontSize().y + 0;
}


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 88 89

    wxScreenDC dc;

    dc.SetFont( wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ) );
90
    dc.GetTextExtent( aText, &textSizeInPixels.x, &textSizeInPixels.y );
91 92 93 94 95

    return textSizeInPixels;
}


96
void EDA_MSG_PANEL::OnPaint( wxPaintEvent& aEvent )
plyatov's avatar
plyatov committed
97
{
98
    wxPaintDC dc( this );
plyatov's avatar
plyatov committed
99

dickelbeck's avatar
dickelbeck committed
100
    erase( &dc );
dickelbeck's avatar
dickelbeck committed
101

102
    dc.SetBackground( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
dickelbeck's avatar
dickelbeck committed
103
    dc.SetBackgroundMode( wxSOLID );
104 105
    dc.SetTextBackground( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
    dc.SetFont( wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ) );
dickelbeck's avatar
dickelbeck committed
106

dickelbeck's avatar
dickelbeck committed
107 108
    for( unsigned i=0;  i<m_Items.size();  ++i )
        showItem( dc, m_Items[i] );
dickelbeck's avatar
dickelbeck committed
109

110
    aEvent.Skip();
plyatov's avatar
plyatov committed
111 112
}

113 114 115 116

void EDA_MSG_PANEL::AppendMessage( const wxString& aUpperText,
                                   const wxString& aLowerText,
                                   EDA_COLOR_T aColor, int aPad )
117 118 119 120
{
    wxString    text;
    wxSize      drawSize = GetClientSize();

121 122
    text = ( aUpperText.Len() > aLowerText.Len() ) ? aUpperText : aLowerText;
    text.Append( ' ', aPad );
123

124
    MSG_PANEL_ITEM item;
125 126 127 128 129 130 131 132 133 134 135

    /* Don't put the first message a window client position 0.  Offset by
     * one 'W' character width. */
    if( m_last_x == 0 )
        m_last_x = m_fontSize.x;

    item.m_X = m_last_x;

    item.m_UpperY = ( drawSize.y / 2 ) - m_fontSize.y;
    item.m_LowerY = drawSize.y - m_fontSize.y;

136 137 138
    item.m_UpperText = aUpperText;
    item.m_LowerText = aLowerText;
    item.m_Color = aColor;
139 140 141
    m_Items.push_back( item );
    m_last_x += computeTextSize( text ).x;

142 143 144
    // Add an extra space between texts for a better look:
    m_last_x += m_fontSize.x;

145 146
    Refresh();
}
plyatov's avatar
plyatov committed
147

148

149
void EDA_MSG_PANEL::SetMessage( int aXPosition, const wxString& aUpperText,
150
                                const wxString& aLowerText, EDA_COLOR_T aColor )
plyatov's avatar
plyatov committed
151
{
152 153
    wxPoint pos;
    wxSize drawSize = GetClientSize();
154

155 156
    if( aXPosition >= 0 )
        m_last_x = pos.x = aXPosition * (m_fontSize.x + 2);
157
    else
dickelbeck's avatar
dickelbeck committed
158
        pos.x = m_last_x;
159

160
    MSG_PANEL_ITEM item;
dickelbeck's avatar
dickelbeck committed
161

dickelbeck's avatar
dickelbeck committed
162
    item.m_X = pos.x;
dickelbeck's avatar
dickelbeck committed
163

164 165
    item.m_UpperY = (drawSize.y / 2) - m_fontSize.y;
    item.m_LowerY = drawSize.y - m_fontSize.y;
dickelbeck's avatar
dickelbeck committed
166

167 168 169
    item.m_UpperText = aUpperText;
    item.m_LowerText = aLowerText;
    item.m_Color = aColor;
dickelbeck's avatar
dickelbeck committed
170

dickelbeck's avatar
dickelbeck committed
171 172
    int ndx;

dickelbeck's avatar
dickelbeck committed
173
    // update the vector, which is sorted by m_X
dickelbeck's avatar
dickelbeck committed
174
    int limit = m_Items.size();
175

dickelbeck's avatar
dickelbeck committed
176
    for( ndx=0;  ndx<limit;  ++ndx )
177
    {
dickelbeck's avatar
dickelbeck committed
178 179 180 181 182 183
        // replace any item with same X
        if( m_Items[ndx].m_X == item.m_X )
        {
            m_Items[ndx] = item;
            break;
        }
dickelbeck's avatar
dickelbeck committed
184

dickelbeck's avatar
dickelbeck committed
185 186
        if( m_Items[ndx].m_X > item.m_X )
        {
187
            m_Items.insert( m_Items.begin() + ndx, item );
dickelbeck's avatar
dickelbeck committed
188 189
            break;
        }
190
    }
dickelbeck's avatar
dickelbeck committed
191

192
    if( ndx == limit )        // mutually exclusive with two above if tests
193
    {
dickelbeck's avatar
dickelbeck committed
194 195
        m_Items.push_back( item );
    }
dickelbeck's avatar
dickelbeck committed
196 197

    Refresh();
dickelbeck's avatar
dickelbeck committed
198 199 200
}


201
void EDA_MSG_PANEL::showItem( wxDC& aDC, const MSG_PANEL_ITEM& aItem )
dickelbeck's avatar
dickelbeck committed
202
{
203
    EDA_COLOR_T color = aItem.m_Color;
dickelbeck's avatar
dickelbeck committed
204

dickelbeck's avatar
dickelbeck committed
205 206
    if( color >= 0 )
    {
207
        color = ColorGetBase( color );
208
        aDC.SetTextForeground( MakeColour( color ) );
dickelbeck's avatar
dickelbeck committed
209 210 211 212
    }

    if( !aItem.m_UpperText.IsEmpty() )
    {
213
        aDC.DrawText( aItem.m_UpperText, aItem.m_X, aItem.m_UpperY );
dickelbeck's avatar
dickelbeck committed
214
    }
dickelbeck's avatar
dickelbeck committed
215

dickelbeck's avatar
dickelbeck committed
216 217
    if( !aItem.m_LowerText.IsEmpty() )
    {
218
        aDC.DrawText( aItem.m_LowerText, aItem.m_X, aItem.m_LowerY );
219
    }
plyatov's avatar
plyatov committed
220 221
}

222

223
void EDA_MSG_PANEL::EraseMsgBox()
plyatov's avatar
plyatov committed
224
{
dickelbeck's avatar
dickelbeck committed
225 226 227
   m_Items.clear();
   m_last_x = 0;
   Refresh();
plyatov's avatar
plyatov committed
228 229
}

230

231
void EDA_MSG_PANEL::erase( wxDC* aDC )
plyatov's avatar
plyatov committed
232
{
233 234 235
    wxPen   pen;
    wxBrush brush;

dickelbeck's avatar
dickelbeck committed
236
    wxSize  size  = GetClientSize();
237
    wxColor color = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
dickelbeck's avatar
dickelbeck committed
238

239
    pen.SetColour( color );
dickelbeck's avatar
dickelbeck committed
240

241 242
    brush.SetColour( color );
    brush.SetStyle( wxSOLID );
dickelbeck's avatar
dickelbeck committed
243

244 245 246
    aDC->SetPen( pen );
    aDC->SetBrush( brush );
    aDC->DrawRectangle( 0, 0, size.x, size.y );
plyatov's avatar
plyatov committed
247
}