class_footprints_listbox.cpp 9.79 KB
Newer Older
1 2 3 4
/**
 * @file class_footprints_listbox.cpp
 * class to display the list fo available footprints
 */
5

6 7 8
#include <fctsys.h>
#include <wxstruct.h>
#include <macros.h>
9

10 11 12
#include <cvpcb.h>
#include <cvpcb_mainframe.h>
#include <cvstruct.h>
13 14 15 16 17 18


/***************************************/
/* ListBox handling the footprint list */
/***************************************/

19
FOOTPRINTS_LISTBOX::FOOTPRINTS_LISTBOX( CVPCB_MAINFRAME* parent,
20 21 22 23 24 25 26
                                        wxWindowID id, const wxPoint& loc,
                                        const wxSize& size,
                                        int nbitems, wxString choice[] ) :
    ITEMS_LISTBOX_BASE( parent, id, loc, size )
{
    m_UseFootprintFullList = true;
    m_ActiveFootprintList  = NULL;
27
    SetActiveFootprintList( true );
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
}


FOOTPRINTS_LISTBOX::~FOOTPRINTS_LISTBOX()
{
}


/*
 * Return number of items
 */
int FOOTPRINTS_LISTBOX::GetCount()
{
    return m_ActiveFootprintList->Count();
}


/*
 * Change an item text
 */
void FOOTPRINTS_LISTBOX::SetString( unsigned linecount, const wxString& text )
{
    if( linecount >= m_ActiveFootprintList->Count() )
        linecount = m_ActiveFootprintList->Count() - 1;
    if( linecount >= 0 )
        (*m_ActiveFootprintList)[linecount] = text;
}


wxString FOOTPRINTS_LISTBOX::GetSelectedFootprint()
{
Joe Ferner's avatar
Joe Ferner committed
59
    wxString footprintName;
60 61 62 63 64
    int      ii = GetFirstSelected();

    if( ii >= 0 )
    {
        wxString msg = (*m_ActiveFootprintList)[ii];
65 66
        msg.Trim( true );
        msg.Trim( false );
Joe Ferner's avatar
Joe Ferner committed
67
        footprintName = msg.AfterFirst( wxChar( ' ' ) );
68 69
    }

Joe Ferner's avatar
Joe Ferner committed
70
    return footprintName;
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
}


void FOOTPRINTS_LISTBOX::AppendLine( const wxString& text )
{
    m_ActiveFootprintList->Add( text );
    SetItemCount( m_ActiveFootprintList->Count() );
}


/*
 * Overlaid function: MUST be provided in wxLC_VIRTUAL mode
 * because real data is not handled by ITEMS_LISTBOX_BASE
 */
wxString FOOTPRINTS_LISTBOX::OnGetItemText( long item, long column ) const
{
    return m_ActiveFootprintList->Item( item );
}


/*
 * Enable or disable an item
 */
void FOOTPRINTS_LISTBOX::SetSelection( unsigned index, bool State )
{
    if( (int) index >= GetCount() )
        index = GetCount() - 1;

    if( (index >= 0)  && (GetCount() > 0) )
    {
#ifndef __WXMAC__
        Select( index, State );
#endif
        EnsureVisible( index );
#ifdef __WXMAC__
        Refresh();
#endif
    }
}


void FOOTPRINTS_LISTBOX::SetFootprintFullList( FOOTPRINT_LIST& list )
{
    wxString msg;
Joe Ferner's avatar
Joe Ferner committed
115
    int      oldSelection = GetSelection();
116 117 118

    m_FullFootprintList.Clear();

119 120 121
    for( unsigned ii = 0; ii < list.GetCount(); ii++ )
    {
        FOOTPRINT_INFO & footprint = list.GetItem(ii);
122
        msg.Printf( wxT( "%3d %s" ), (int) m_FullFootprintList.GetCount() + 1,
123
                   GetChars(footprint.m_Module) );
124 125 126
        m_FullFootprintList.Add( msg );
    }

127
    SetActiveFootprintList( true );
128

Joe Ferner's avatar
Joe Ferner committed
129
    if(  GetCount() == 0 || oldSelection < 0 || oldSelection >= GetCount() )
130
        SetSelection( 0, true );
131 132 133 134
    Refresh();
}


135
void FOOTPRINTS_LISTBOX::SetFootprintFilteredList( COMPONENT_INFO* Component,
136 137 138 139 140
                                                   FOOTPRINT_LIST& list )
{
    wxString msg;
    unsigned jj;
    int      OldSelection = GetSelection();
Joe Ferner's avatar
Joe Ferner committed
141
    bool     hasItem = false;
142 143 144

    m_FilteredFootprintList.Clear();

145 146 147
    for( unsigned ii = 0; ii < list.GetCount(); ii++ )
    {
        FOOTPRINT_INFO& footprint = list.GetItem(ii);
148 149 150 151
        // Search for matching footprints
        // The search is case insensitive
        wxString module = footprint.m_Module.Upper();
        wxString candidate;
152 153
        for( jj = 0; jj < Component->m_FootprintFilter.GetCount(); jj++ )
        {
154 155
            candidate = Component->m_FootprintFilter[jj].Upper();
            if( !module.Matches( candidate ) )
156 157 158 159
                continue;
            msg.Printf( wxT( "%3d %s" ), m_FilteredFootprintList.GetCount() + 1,
                       footprint.m_Module.GetData() );
            m_FilteredFootprintList.Add( msg );
Joe Ferner's avatar
Joe Ferner committed
160
            hasItem = true;
161 162 163
        }
    }

Joe Ferner's avatar
Joe Ferner committed
164
    if( hasItem )
165
        SetActiveFootprintList( false );
166
    else
167
        SetActiveFootprintList( true );
168 169

    if( ( GetCount() == 0 ) || ( OldSelection >= GetCount() ) )
170
        SetSelection( 0, true );
171 172 173 174

    Refresh();
}

175 176 177
void FOOTPRINTS_LISTBOX::SetFootprintFilteredByPinCount( COMPONENT_INFO* Component,
                                                         FOOTPRINT_LIST& list ) {
    wxString msg;
Joe Ferner's avatar
Joe Ferner committed
178 179
    int      oldSelection = GetSelection();
    bool     hasItem = false;
180 181 182 183 184 185 186

    m_FilteredFootprintList.Clear();

    for( unsigned ii = 0; ii < list.GetCount(); ii++ )
    {
        FOOTPRINT_INFO& footprint = list.GetItem(ii);

Joe Ferner's avatar
Joe Ferner committed
187 188 189
        if( Component->m_pinCount == footprint.m_padCount )
        {
            msg.Printf( wxT( "%3d %s" ), m_FilteredFootprintList.GetCount() + 1,
190
                     footprint.m_Module.GetData() );
Joe Ferner's avatar
Joe Ferner committed
191 192
            m_FilteredFootprintList.Add( msg );
            hasItem = true;
193 194 195
        }
    }

Joe Ferner's avatar
Joe Ferner committed
196
    if( hasItem )
197 198 199 200
        SetActiveFootprintList( false );
    else
        SetActiveFootprintList( true );

Joe Ferner's avatar
Joe Ferner committed
201
    if( ( GetCount() == 0 ) || ( oldSelection >= GetCount() ) )
202 203 204 205
        SetSelection( 0, true );

    Refresh();
}
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

/** Set the footprint list. We can have 2 footprint list:
 *  The full footprint list
 *  The filtered footprint list (if the current selected component has a
 * filter for footprints)
 *  @param FullList true = full footprint list, false = filtered footprint list
 *  @param Redraw = true to redraw the window
 */
void FOOTPRINTS_LISTBOX::SetActiveFootprintList( bool FullList, bool Redraw )
{
    bool old_selection = m_UseFootprintFullList;

#ifdef __WINDOWS__

    /* Workaround for a curious bug in wxWidgets:
     * if we switch from a long list of footprints to a short list (a
     * filtered footprint list), and if the selected item is near the end
     * of the long list,  the new list is not displayed from the top of
     * the list box
     */
    if( m_ActiveFootprintList )
    {
        bool new_selection;
        if( FullList )
230
            new_selection = true;
231
        else
232
            new_selection = false;
233
        if( new_selection != old_selection )
234
            SetSelection( 0, true );
235 236 237 238
    }
#endif
    if( FullList )
    {
239
        m_UseFootprintFullList = true;
240 241 242 243 244
        m_ActiveFootprintList  = &m_FullFootprintList;
        SetItemCount( m_FullFootprintList.GetCount() );
    }
    else
    {
245
        m_UseFootprintFullList = false;
246 247 248 249 250 251
        m_ActiveFootprintList  = &m_FilteredFootprintList;
        SetItemCount( m_FilteredFootprintList.GetCount() );
    }

    if( Redraw )
    {
252
        if( !m_UseFootprintFullList || ( m_UseFootprintFullList != old_selection ) )
253 254 255 256 257
        {
            Refresh();
        }
    }

258
    GetParent()->DisplayStatus();
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
}


/**************************************/
/* Event table for the footprint list */
/**************************************/

BEGIN_EVENT_TABLE( FOOTPRINTS_LISTBOX, ITEMS_LISTBOX_BASE )
EVT_SIZE( ITEMS_LISTBOX_BASE::OnSize )
EVT_CHAR( FOOTPRINTS_LISTBOX::OnChar )
END_EVENT_TABLE()


/********************************************************/
void FOOTPRINTS_LISTBOX::OnLeftClick( wxListEvent& event )
/********************************************************/
{
276
    FOOTPRINT_INFO* Module;
Joe Ferner's avatar
Joe Ferner committed
277
    wxString   footprintName = GetSelectedFootprint();
278

Joe Ferner's avatar
Joe Ferner committed
279
    Module = GetParent()->m_footprints.GetModuleInfo( footprintName );
280
    wxASSERT(Module);
281
    if( GetParent()->m_DisplayFootprintFrame )
282
    {
283 284
        // Refresh current selected footprint view:
        GetParent()->CreateScreenCmp();
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    }

    if( Module )
    {
        wxString msg;
        msg = Module->m_Doc;
        GetParent()->SetStatusText( msg, 0 );

        msg  = wxT( "KeyW: " );
        msg += Module->m_KeyWord;
        GetParent()->SetStatusText( msg, 1 );
    }
}


/******************************************************/
void FOOTPRINTS_LISTBOX::OnLeftDClick( wxListEvent& event )
/******************************************************/
{
Joe Ferner's avatar
Joe Ferner committed
304
    wxString footprintName = GetSelectedFootprint();
305

Joe Ferner's avatar
Joe Ferner committed
306
    GetParent()->SetNewPkg( footprintName );
307 308 309
}


310 311
/**
 * Function OnChar
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
 * called on a key pressed
 * Call default handler for some special keys,
 * and for "ascii" keys, select the first footprint
 * that the name starts by the letter.
 * This is the defaut behaviour of a listbox, but because we use
 * virtual lists, the listbox does not know anything to what is displayed,
 * we must handle this behaviour here.
 * Furthermore the footprint name is not at the beginning of
 * displayed lines (the first word is the line number)
 */
void FOOTPRINTS_LISTBOX::OnChar( wxKeyEvent& event )
{
    int key = event.GetKeyCode();
    switch( key )
    {
        case WXK_LEFT:
        case WXK_NUMPAD_LEFT:
            GetParent()->m_ListCmp->SetFocus();
            return;

        case WXK_HOME:
        case WXK_END:
        case WXK_UP:
        case WXK_DOWN:
        case WXK_PAGEUP:
        case WXK_PAGEDOWN:
        case WXK_RIGHT:
        case WXK_NUMPAD_RIGHT:
            event.Skip();
            return;

        default:
            break;
    }
    // Search for an item name starting by the key code:
    key = toupper(key);
    for( unsigned ii = 0; ii < m_ActiveFootprintList->GetCount(); ii++ )
    {
        wxString text = m_ActiveFootprintList->Item(ii);
        /* search for the start char of the footprint name.
         * we must skip the line number
        */
        text.Trim(false);      // Remove leading spaces in line
        unsigned jj = 0;
        for( ; jj < text.Len(); jj++ )
Joe Ferner's avatar
Joe Ferner committed
357 358
        {
            // skip line number
359 360 361
            if( text[jj] == ' ' )
                break;
        }
Joe Ferner's avatar
Joe Ferner committed
362

363 364 365 366 367
        for( ; jj < text.Len(); jj++ )
        {   // skip blanks
            if( text[jj] != ' ' )
                break;
        }
Joe Ferner's avatar
Joe Ferner committed
368 369 370

        int start_char = toupper( text[jj] );
        if( key == start_char )
371 372 373 374 375 376 377
        {
            Focus( ii );
            SetSelection( ii, true );   // Ensure visible
            break;
        }
    }
}