get_component_dialog.cpp 6.54 KB
Newer Older
1
/*********************************/
2
/*  get_component_dialog.cpp     */
3 4 5 6 7 8
/*********************************/

#include "fctsys.h"
#include "gr_basic.h"
#include "common.h"
#include "macros.h"
9
#include "wxstruct.h"
10
#include "get_component_dialog.h"
11

12 13 14 15 16 17

/****************************************************************************/
/* Show a dialog frame to choose a name from an history list, or a new name */
/* to select a component or a module                                        */
/****************************************************************************/

18
static unsigned s_HistoryMaxCount = 8;  // Max number of items displayed in history list
19

20 21 22 23 24 25 26 27

BEGIN_EVENT_TABLE( WinEDA_SelectCmp, wxDialog )
    EVT_BUTTON( ID_ACCEPT_NAME, WinEDA_SelectCmp::Accept )
    EVT_BUTTON( ID_ACCEPT_KEYWORD, WinEDA_SelectCmp::Accept )
    EVT_BUTTON( ID_CANCEL, WinEDA_SelectCmp::Accept )
    EVT_BUTTON( ID_LIST_ALL, WinEDA_SelectCmp::Accept )
    EVT_BUTTON( ID_EXTRA_TOOL, WinEDA_SelectCmp::GetExtraSelection )
    EVT_LISTBOX( ID_SEL_BY_LISTBOX, WinEDA_SelectCmp::Accept )
28 29 30
END_EVENT_TABLE()


31 32 33 34
/*
 * Dialog frame to choose a component or a footprint
 *   This dialog shows an history of last selected items
 */
35 36 37 38 39 40
WinEDA_SelectCmp::WinEDA_SelectCmp( WinEDA_DrawFrame* parent,
                                    const wxPoint&    framepos,
                                    wxArrayString&    HistoryList,
                                    const wxString&   Title,
                                    bool              show_extra_tool ) :
    wxDialog( parent, -1, Title, framepos, wxDefaultSize, DIALOG_STYLE )
41
{
42 43 44 45
    wxButton*     Button;
    wxStaticText* Text;

    m_AuxTool = show_extra_tool;
46
    m_GetExtraFunction = false;
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

    wxBoxSizer* MainBoxSizer = new wxBoxSizer( wxHORIZONTAL );
    SetSizer( MainBoxSizer );

    wxBoxSizer* LeftBoxSizer = new wxBoxSizer( wxVERTICAL );
    MainBoxSizer->Add( LeftBoxSizer,
                       0,
                       wxALIGN_CENTER_HORIZONTAL | wxALL | wxADJUST_MINSIZE,
                       5 );
    wxBoxSizer* RightBoxSizer = new wxBoxSizer( wxVERTICAL );
    MainBoxSizer->Add( RightBoxSizer, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5 );

    Text = new wxStaticText( this, -1, _( "Name:" ) );
    LeftBoxSizer->Add( Text, 0, wxALIGN_LEFT | wxLEFT | wxRIGHT | wxTOP, 5 );

62
    m_TextCtrl = new wxTextCtrl( this, ID_ENTER_NAME, m_Text );
63
    m_TextCtrl->SetInsertionPoint( 1 );
64
    m_TextCtrl->SetFocus();
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    LeftBoxSizer->Add( m_TextCtrl,
                       0,
                       wxGROW | wxLEFT | wxRIGHT | wxBOTTOM | wxADJUST_MINSIZE,
                       5 );

    Text = new wxStaticText( this, -1, _( "History list:" ) );
    LeftBoxSizer->Add( Text, 0, wxALIGN_LEFT | wxLEFT | wxRIGHT | wxTOP, 5 );

    m_List = new wxListBox( this, ID_SEL_BY_LISTBOX, wxDefaultPosition,
                            wxSize( 220, -1 ), HistoryList, wxLB_SINGLE );
    LeftBoxSizer->Add( m_List,
                       0,
                       wxGROW | wxLEFT | wxRIGHT | wxBOTTOM | wxADJUST_MINSIZE,
                       5 );

    Button = new wxButton( this, ID_ACCEPT_NAME, _( "OK" ) );
    Button->SetDefault();
    RightBoxSizer->Add( Button,
                        0,
                        wxGROW | wxLEFT | wxRIGHT | wxTOP | wxBOTTOM,
                        5 );

87
    Button = new wxButton( this, ID_ACCEPT_KEYWORD, _( "Search by Keyword" ) );
88 89 90 91 92 93 94
    RightBoxSizer->Add( Button, 0, wxGROW | wxLEFT | wxRIGHT | wxBOTTOM, 5 );

    Button = new wxButton( this, ID_CANCEL, _( "Cancel" ) );
    RightBoxSizer->Add( Button, 0, wxGROW | wxLEFT | wxRIGHT | wxBOTTOM, 5 );

    Button = new wxButton( this, ID_LIST_ALL, _( "List All" ) );
    RightBoxSizer->Add( Button, 0, wxGROW | wxLEFT | wxRIGHT | wxBOTTOM, 5 );
95 96

#ifndef __WXMAC__
97 98
    if( m_AuxTool )     /* The selection can be done by an extra function */
    {
99
        Button = new wxButton( this, ID_EXTRA_TOOL, _( "Select by Browser" ) );
100 101 102 103 104 105
        RightBoxSizer->Add( Button, 0, wxGROW | wxLEFT | wxRIGHT | wxBOTTOM, 5 );
    }
#endif

    GetSizer()->Fit( this );
    GetSizer()->SetSizeHints( this );
106 107
}

108 109

void WinEDA_SelectCmp::Accept( wxCommandEvent& event )
110
{
111 112
    int id = wxID_OK;

113 114 115
    switch( event.GetId() )
    {
    case ID_SEL_BY_LISTBOX:
116
        m_Text = m_List->GetStringSelection();
117 118 119
        break;

    case ID_ACCEPT_NAME:
120
        m_Text = m_TextCtrl->GetValue();
121 122 123
        break;

    case ID_ACCEPT_KEYWORD:
124
        m_Text = wxT( "= " ) + m_TextCtrl->GetValue();
125 126 127
        break;

    case ID_CANCEL:
128 129
        m_Text = wxEmptyString;
        id = wxID_CANCEL;
130 131 132
        break;

    case ID_LIST_ALL:
133
        m_Text = wxT( "*" );
134 135 136
        break;
    }

137 138 139 140 141 142 143
    m_Text.Trim( false );      // Remove blanks at beginning
    m_Text.Trim( true );       // Remove blanks at end

    if( IsModal() )
        EndModal( id );
    else
        Close( id );
144 145 146
}


147
/* Get the component name by the extra function */
148
void WinEDA_SelectCmp::GetExtraSelection( wxCommandEvent& event )
149 150
{
    m_GetExtraFunction = true;
151

152 153 154 155 156 157 158 159
    if( IsModal() )
        EndModal( wxID_OK );
    else
        Close( wxID_OK );
}


wxString WinEDA_SelectCmp::GetComponentName( void )
160
{
161
    return m_Text;
162 163 164
}


165 166 167 168 169
void WinEDA_SelectCmp::SetComponentName( const wxString& name )
{
    if( m_TextCtrl )
        m_TextCtrl->SetValue( name );
}
170

171 172

wxPoint GetComponentDialogPosition( void )
plyatov's avatar
plyatov committed
173
{
174
    wxPoint pos;
175 176
    int     x, y, w, h;

177
    pos = wxGetMousePosition();
178
    wxClientDisplayRect( &x, &y, &w, &h );
179 180 181 182 183 184 185 186
    pos.x -= 100;
    pos.y -= 50;
    if( pos.x < x )
        pos.x = x;
    if( pos.y < y )
        pos.y = y;
    if( pos.x < x )
        pos.x = x;
187
    x += w - 350;
188 189 190 191
    if( pos.x > x )
        pos.x = x;
    if( pos.y < y )
        pos.y = y;
192

193 194
    return pos;
}
195

196

197 198
/*
 * Add the string "Name" to the history list HistoryList
199
 */
200
void AddHistoryComponentName( wxArrayString& HistoryList, const wxString& Name )
201
{
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    int ii, c_max;

    if( HistoryList.GetCount() > 0 )
    {
        if( Name == HistoryList[0] )
            return;

        /* remove an old identical selection if exists */
        for( ii = 1; (unsigned) ii < HistoryList.GetCount(); ii++ )
        {
            if( Name == HistoryList[ii] )
            {
                HistoryList.RemoveAt( ii ); ii--;
            }
        }

        /* shift the list */
        if( HistoryList.GetCount() < s_HistoryMaxCount )
            HistoryList.Add( wxT( "" ) );

        c_max = HistoryList.GetCount() - 2;
        for( ii = c_max; ii >= 0; ii-- )
            HistoryList[ii + 1] = HistoryList[ii];

        /* Add the new name at the beginning of the history list */
        HistoryList[0] = Name;
    }
    else
        HistoryList.Add( Name );
231
}