get_component_dialog.cpp 6.83 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

BEGIN_EVENT_TABLE( WinEDA_SelectCmp, wxDialog )
    EVT_BUTTON( ID_ACCEPT_NAME, WinEDA_SelectCmp::Accept )
    EVT_BUTTON( ID_ACCEPT_KEYWORD, WinEDA_SelectCmp::Accept )
jp's avatar
jp committed
24
    EVT_BUTTON( wxID_CANCEL, WinEDA_SelectCmp::Accept )
25 26 27
    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
{
jp's avatar
jp committed
42 43 44 45 46 47 48 49 50 51
    m_AuxTool = show_extra_tool;
    InitDialog( HistoryList );

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

void WinEDA_SelectCmp::InitDialog( wxArrayString& aHistoryList )
{
    
52 53 54
    wxButton*     Button;
    wxStaticText* Text;

55
    m_GetExtraFunction = false;
56 57 58 59 60 61 62 63 64 65 66 67 68 69

    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 );
jp's avatar
jp committed
70 71
    m_TextCtrl = new wxTextCtrl( this, wxID_ANY );
    m_TextCtrl->SetFocus();         // text value will be initialized later by calling GetComponentName()
72 73 74 75 76
    LeftBoxSizer->Add( m_TextCtrl,
                       0,
                       wxGROW | wxLEFT | wxRIGHT | wxBOTTOM | wxADJUST_MINSIZE,
                       5 );

jp's avatar
jp committed
77

78 79 80 81
    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,
jp's avatar
jp committed
82
                            wxSize( 220, -1 ), aHistoryList, wxLB_SINGLE );
83 84 85 86 87 88 89 90 91 92 93 94
    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 );

95
    Button = new wxButton( this, ID_ACCEPT_KEYWORD, _( "Search by Keyword" ) );
96 97
    RightBoxSizer->Add( Button, 0, wxGROW | wxLEFT | wxRIGHT | wxBOTTOM, 5 );

jp's avatar
jp committed
98
    Button = new wxButton( this, wxID_CANCEL, _( "Cancel" ) );
99 100 101 102
    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 );
103 104

#ifndef __WXMAC__
105 106
    if( m_AuxTool )     /* The selection can be done by an extra function */
    {
107
        Button = new wxButton( this, ID_EXTRA_TOOL, _( "Select by Browser" ) );
108 109 110
        RightBoxSizer->Add( Button, 0, wxGROW | wxLEFT | wxRIGHT | wxBOTTOM, 5 );
    }
#endif
111 112
}

113 114

void WinEDA_SelectCmp::Accept( wxCommandEvent& event )
115
{
116 117
    int id = wxID_OK;

118 119 120
    switch( event.GetId() )
    {
    case ID_SEL_BY_LISTBOX:
121
        m_Text = m_List->GetStringSelection();
122 123 124
        break;

    case ID_ACCEPT_NAME:
125
        m_Text = m_TextCtrl->GetValue();
126 127 128
        break;

    case ID_ACCEPT_KEYWORD:
129
        m_Text = wxT( "= " ) + m_TextCtrl->GetValue();
130 131
        break;

jp's avatar
jp committed
132
    case wxID_CANCEL:
133 134
        m_Text = wxEmptyString;
        id = wxID_CANCEL;
135 136 137
        break;

    case ID_LIST_ALL:
138
        m_Text = wxT( "*" );
139 140 141
        break;
    }

142 143 144 145 146 147 148
    m_Text.Trim( false );      // Remove blanks at beginning
    m_Text.Trim( true );       // Remove blanks at end

    if( IsModal() )
        EndModal( id );
    else
        Close( id );
149 150 151
}


152
/* Get the component name by the extra function */
153
void WinEDA_SelectCmp::GetExtraSelection( wxCommandEvent& event )
154 155
{
    m_GetExtraFunction = true;
156

157 158 159 160 161 162 163
    if( IsModal() )
        EndModal( wxID_OK );
    else
        Close( wxID_OK );
}


jp's avatar
jp committed
164
// Return the component name selected by the dialog
165
wxString WinEDA_SelectCmp::GetComponentName( void )
166
{
167
    return m_Text;
168 169 170
}


jp's avatar
jp committed
171 172
/* Initialize the default component name default choice
*/
173 174 175
void WinEDA_SelectCmp::SetComponentName( const wxString& name )
{
    if( m_TextCtrl )
jp's avatar
jp committed
176
    {
177
        m_TextCtrl->SetValue( name );
jp's avatar
jp committed
178 179
        m_TextCtrl->SetSelection(-1, -1);
    }
180
}
181

182 183

wxPoint GetComponentDialogPosition( void )
plyatov's avatar
plyatov committed
184
{
185
    wxPoint pos;
186 187
    int     x, y, w, h;

188
    pos = wxGetMousePosition();
189
    wxClientDisplayRect( &x, &y, &w, &h );
190 191 192 193 194 195 196 197
    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;
198
    x += w - 350;
199 200 201 202
    if( pos.x > x )
        pos.x = x;
    if( pos.y < y )
        pos.y = y;
203

204 205
    return pos;
}
206

207

208 209
/*
 * Add the string "Name" to the history list HistoryList
210
 */
211
void AddHistoryComponentName( wxArrayString& HistoryList, const wxString& Name )
212
{
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    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 );
242
}