dialog_find.cpp 5.22 KB
Newer Older
1 2 3
/*
 * This program source code file is part of KICAD, a free EDA CAD application.
 *
4 5
 * Copyright (C) 2012 Marco Mattila <marcom99@gmail.com>
 * Copyright (C) 2006 Jean-Pierre Charras <jean-pierre.charras@gipsa-lab.inpg.fr>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 59 60 61 62 63 64 65 66 67 68 69 70 71
 * Copyright (C) 1992-2012 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
 */

#include <fctsys.h>
#include <gr_basic.h>
#include <class_drawpanel.h>
#include <confirm.h>
#include <kicad_string.h>
#include <wxPcbStruct.h>

#include <class_board.h>
#include <class_module.h>
#include <class_marker_pcb.h>

#include <pcbnew.h>
#include <pcbnew_id.h>
#include <dialog_find_base.h>


class DIALOG_FIND : public DIALOG_FIND_BASE
{
public:
    DIALOG_FIND( PCB_BASE_FRAME* aParent );

private:
    PCB_BASE_FRAME* parent;

    int itemCount, markerCount;
    static wxString prevSearchString;
    static bool warpMouse;

    void onButtonFindItemClick( wxCommandEvent& event );
    void onButtonFindMarkerClick( wxCommandEvent& event );
    void onButtonCloseClick( wxCommandEvent& event );
    void onClose( wxCloseEvent& event );
};


// Initialize static member variables
wxString DIALOG_FIND::prevSearchString;
bool DIALOG_FIND::warpMouse = true;


DIALOG_FIND::DIALOG_FIND( PCB_BASE_FRAME* aParent ) : DIALOG_FIND_BASE( aParent )
{
    parent = aParent;
    GetSizer()->SetSizeHints( this );

    m_SearchTextCtrl->AppendText( prevSearchString );
jean-pierre charras's avatar
jean-pierre charras committed
72 73
    m_SearchTextCtrl->SetFocus();
    m_SearchTextCtrl->SetSelection( -1, -1 );
74 75 76 77
    m_NoMouseWarpCheckBox->SetValue( !warpMouse );

    itemCount = markerCount = 0;

78
    Center();
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 115 116 117 118 119 120
}


void DIALOG_FIND::onButtonCloseClick( wxCommandEvent& aEvent )
{
    Close( true );
}


void DIALOG_FIND::onButtonFindItemClick( wxCommandEvent& aEvent )
{
    PCB_SCREEN* screen = (PCB_SCREEN*) ( parent->GetScreen() );
    wxPoint     pos;
    BOARD_ITEM* foundItem = 0;

    wxString searchString = m_SearchTextCtrl->GetValue();

    if( !searchString.IsSameAs( prevSearchString, false ) )
    {
        itemCount = 0;
    }
    prevSearchString = searchString;

    parent->GetCanvas()->GetViewStart( &screen->m_StartVisu.x, &screen->m_StartVisu.y );

    int count = 0;

    for( MODULE* module = parent->GetBoard()->m_Modules; module; module = module->Next() )
    {
        if( WildCompareString( searchString, module->GetReference().GetData(), false ) )
        {
            count++;

            if( count > itemCount )
            {
                foundItem = module;
                pos = module->GetPosition();
                itemCount++;
                break;
            }
        }

121
        if( WildCompareString( searchString, module->GetValue().GetData(), false ) )
122 123 124 125 126 127
        {
            count++;

            if( count > itemCount )
            {
                foundItem = module;
128
                pos = module->GetPosition();
129 130 131 132 133 134 135
                itemCount++;
                break;
            }
        }
    }

    wxString msg;
136

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    if( foundItem )
    {
        parent->SetCurItem( foundItem );
        msg.Printf( _( "<%s> found" ), GetChars( searchString ) );
        parent->SetStatusText( msg );

        parent->CursorGoto( pos, !m_NoMouseWarpCheckBox->IsChecked() );
    }
    else
    {
        parent->SetStatusText( wxEmptyString );
        msg.Printf( _( "<%s> not found" ), GetChars( searchString ) );
        DisplayError( this, msg, 10 );
        itemCount = 0;
    }
}


void DIALOG_FIND::onButtonFindMarkerClick( wxCommandEvent& aEvent )
{
    PCB_SCREEN* screen = (PCB_SCREEN*) ( parent->GetScreen() );
    wxPoint     pos;
    BOARD_ITEM* foundItem = 0;

    parent->GetCanvas()->GetViewStart( &screen->m_StartVisu.x, &screen->m_StartVisu.y );

    MARKER_PCB* marker = parent->GetBoard()->GetMARKER( markerCount++ );

    if( marker )
    {
        foundItem = marker;
        pos = marker->GetPosition();
    }

    wxString msg;
    if( foundItem )
    {
        parent->SetCurItem( foundItem );
        msg = _( "Marker found" );
        parent->SetStatusText( msg );

        parent->CursorGoto( pos, !m_NoMouseWarpCheckBox->IsChecked() );
    }
    else
    {
        parent->SetStatusText( wxEmptyString );
        msg = _( "No marker found" );
        DisplayError( this, msg, 10 );
        markerCount = 0;
    }
}


void DIALOG_FIND::onClose( wxCloseEvent& aEvent )
{
    warpMouse = !m_NoMouseWarpCheckBox->IsChecked();

    EndModal( 1 );
}


void PCB_EDIT_FRAME::InstallFindFrame()
{
    DIALOG_FIND dlg( this );
    dlg.ShowModal();
}