msgpanel.cpp 6.21 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

#ifdef __GNUG__
#pragma implementation
#endif

30 31 32 33
#include <fctsys.h>
#include <wxstruct.h>
#include <common.h>
#include <colors.h>
34

plyatov's avatar
plyatov committed
35

36 37
BEGIN_EVENT_TABLE( EDA_MSG_PANEL, wxPanel )
    EVT_PAINT( EDA_MSG_PANEL::OnPaint )
plyatov's avatar
plyatov committed
38 39 40
END_EVENT_TABLE()


41 42
EDA_MSG_PANEL::EDA_MSG_PANEL( EDA_DRAW_FRAME* parent, int id,
                              const wxPoint& pos, const wxSize& size ) :
43
    wxPanel( parent, id, pos, size )
plyatov's avatar
plyatov committed
44
{
45 46
    SetFont( wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ) );
    SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
dickelbeck's avatar
dickelbeck committed
47
    m_last_x = 0;
48 49

    m_fontSize = computeFontSize();
plyatov's avatar
plyatov committed
50 51 52
}


53
EDA_MSG_PANEL::~EDA_MSG_PANEL()
plyatov's avatar
plyatov committed
54 55 56 57
{
}


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

    wxScreenDC dc;

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

    return fontSizeInPixels;
}


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


79
wxSize EDA_MSG_PANEL::computeTextSize( const wxString& text )
80 81 82 83 84 85 86 87 88 89 90 91 92
{
    // Get size of the wxSYS_DEFAULT_GUI_FONT
    wxSize      textSizeInPixels;

    wxScreenDC dc;

    dc.SetFont( wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ) );
    dc.GetTextExtent( text, &textSizeInPixels.x, &textSizeInPixels.y );

    return textSizeInPixels;
}


93
void EDA_MSG_PANEL::OnPaint( wxPaintEvent& event )
plyatov's avatar
plyatov committed
94
{
95
    wxPaintDC dc( this );
plyatov's avatar
plyatov committed
96

dickelbeck's avatar
dickelbeck committed
97
    erase( &dc );
dickelbeck's avatar
dickelbeck committed
98

99
    dc.SetBackground( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
dickelbeck's avatar
dickelbeck committed
100
    dc.SetBackgroundMode( wxSOLID );
101 102
    dc.SetTextBackground( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
    dc.SetFont( wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ) );
dickelbeck's avatar
dickelbeck committed
103

dickelbeck's avatar
dickelbeck committed
104 105
    for( unsigned i=0;  i<m_Items.size();  ++i )
        showItem( dc, m_Items[i] );
dickelbeck's avatar
dickelbeck committed
106

dickelbeck's avatar
dickelbeck committed
107
    event.Skip();
plyatov's avatar
plyatov committed
108 109
}

110 111 112
void EDA_MSG_PANEL::AppendMessage( const wxString& textUpper,
                                   const wxString& textLower,
                                   int color, int pad )
113 114 115 116 117 118 119
{
    wxString    text;
    wxSize      drawSize = GetClientSize();

    text = ( textUpper.Len() > textLower.Len() ) ? textUpper : textLower;
    text.Append( ' ', pad );

120
    EDA_MSG_ITEM item;
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

    /* 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;

    item.m_UpperText = textUpper;
    item.m_LowerText = textLower;
    item.m_Color = color;
    m_Items.push_back( item );
    m_last_x += computeTextSize( text ).x;

138 139 140
    // Add an extra space between texts for a better look:
    m_last_x += m_fontSize.x;

141 142
    Refresh();
}
plyatov's avatar
plyatov committed
143

144

145 146
void EDA_MSG_PANEL::SetMessage( int aXPosition, const wxString& aUpperText,
                                const wxString& aLowerText, int aColor )
plyatov's avatar
plyatov committed
147
{
148 149
    wxPoint pos;
    wxSize drawSize = GetClientSize();
150

151 152
    if( aXPosition >= 0 )
        m_last_x = pos.x = aXPosition * (m_fontSize.x + 2);
153
    else
dickelbeck's avatar
dickelbeck committed
154
        pos.x = m_last_x;
155

156
    EDA_MSG_ITEM item;
dickelbeck's avatar
dickelbeck committed
157

dickelbeck's avatar
dickelbeck committed
158
    item.m_X = pos.x;
dickelbeck's avatar
dickelbeck committed
159

160 161
    item.m_UpperY = (drawSize.y / 2) - m_fontSize.y;
    item.m_LowerY = drawSize.y - m_fontSize.y;
dickelbeck's avatar
dickelbeck committed
162

163 164 165
    item.m_UpperText = aUpperText;
    item.m_LowerText = aLowerText;
    item.m_Color = aColor;
dickelbeck's avatar
dickelbeck committed
166

dickelbeck's avatar
dickelbeck committed
167 168
    int ndx;

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

dickelbeck's avatar
dickelbeck committed
172
    for( ndx=0;  ndx<limit;  ++ndx )
173
    {
dickelbeck's avatar
dickelbeck committed
174 175 176 177 178 179
        // 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
180

dickelbeck's avatar
dickelbeck committed
181 182
        if( m_Items[ndx].m_X > item.m_X )
        {
183
            m_Items.insert( m_Items.begin() + ndx, item );
dickelbeck's avatar
dickelbeck committed
184 185
            break;
        }
186
    }
dickelbeck's avatar
dickelbeck committed
187

188
    if( ndx == limit )        // mutually exclusive with two above if tests
189
    {
dickelbeck's avatar
dickelbeck committed
190 191
        m_Items.push_back( item );
    }
dickelbeck's avatar
dickelbeck committed
192 193

    Refresh();
dickelbeck's avatar
dickelbeck committed
194 195 196
}


197
void EDA_MSG_PANEL::showItem( wxDC& dc, const EDA_MSG_ITEM& aItem )
dickelbeck's avatar
dickelbeck committed
198 199
{
    int color = aItem.m_Color;
dickelbeck's avatar
dickelbeck committed
200

dickelbeck's avatar
dickelbeck committed
201 202 203
    if( color >= 0 )
    {
        color &= MASKCOLOR;
dickelbeck's avatar
dickelbeck committed
204
        dc.SetTextForeground( wxColour( ColorRefs[color].m_Red,
dickelbeck's avatar
dickelbeck committed
205 206 207 208 209 210
                                        ColorRefs[color].m_Green,
                                        ColorRefs[color].m_Blue ) );
    }

    if( !aItem.m_UpperText.IsEmpty() )
    {
211
        dc.DrawText( aItem.m_UpperText, aItem.m_X, aItem.m_UpperY );
dickelbeck's avatar
dickelbeck committed
212
    }
dickelbeck's avatar
dickelbeck committed
213

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

220

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

228

229
void EDA_MSG_PANEL::erase( wxDC* DC )
plyatov's avatar
plyatov committed
230
{
231 232 233
    wxPen   pen;
    wxBrush brush;

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

237
    pen.SetColour( color );
dickelbeck's avatar
dickelbeck committed
238

239 240
    brush.SetColour( color );
    brush.SetStyle( wxSOLID );
dickelbeck's avatar
dickelbeck committed
241

242 243 244 245
    DC->SetPen( pen );
    DC->SetBrush( brush );

    DC->DrawRectangle( 0, 0, size.x, size.y );
plyatov's avatar
plyatov committed
246
}