kicad/common/displlst.cpp

196 lines
4.7 KiB
C++
Raw Normal View History

/****************/
/* displlst.cpp */
/****************/
#include "fctsys.h"
#include "wxstruct.h"
#include "gr_basic.h"
#include "common.h"
2007-11-05 07:07:00 +00:00
#include "macros.h"
#include "kicad_string.h"
2010-11-18 21:16:28 +00:00
#include "dialog_helpers.h"
enum listbox {
2007-10-07 03:08:24 +00:00
ID_LISTBOX_LIST = 8000
};
2007-12-11 23:12:37 +00:00
BEGIN_EVENT_TABLE( WinEDAListBox, wxDialog )
EVT_BUTTON( wxID_OK, WinEDAListBox::OnOkClick )
EVT_BUTTON( wxID_CANCEL, WinEDAListBox::OnCancelClick )
EVT_LISTBOX( ID_LISTBOX_LIST, WinEDAListBox::ClickOnList )
EVT_LISTBOX_DCLICK( ID_LISTBOX_LIST, WinEDAListBox::D_ClickOnList )
EVT_CHAR( WinEDAListBox::OnKeyEvent )
EVT_CHAR_HOOK( WinEDAListBox::OnKeyEvent )
EVT_CLOSE( WinEDAListBox::OnClose )
END_EVENT_TABLE()
2010-11-19 18:50:23 +00:00
/**
* Used to display a list of elements for selection, and display comment of info lines
* about the selected item.
* @param aParent = apointeur to the parent window
* @param aTitle = the title shown on top.
* @param aItemList = a wxArrayString: the list of elements.
* @param aRefText = an item name if an item must be preselected.
* @param aCallBackFunction callback function to display comments
* @param aPos = position of the dialog.
2007-12-11 23:12:37 +00:00
*/
WinEDAListBox::WinEDAListBox( EDA_DRAW_FRAME* aParent, const wxString& aTitle,
2010-11-19 18:50:23 +00:00
const wxArrayString& aItemList, const wxString& aRefText,
void(* aCallBackFunction)(wxString& Text), wxPoint aPos ) :
wxDialog( aParent, wxID_ANY, aTitle, aPos, wxDefaultSize,
wxDEFAULT_DIALOG_STYLE | MAYBE_RESIZE_BORDER )
{
2010-11-19 18:50:23 +00:00
m_callBackFct = aCallBackFunction;
m_messages = NULL;
2007-12-11 23:12:37 +00:00
wxBoxSizer* GeneralBoxSizer = new wxBoxSizer( wxVERTICAL );
2007-12-11 23:12:37 +00:00
SetSizer( GeneralBoxSizer );
2010-11-19 18:50:23 +00:00
m_listBox = new wxListBox( this, ID_LISTBOX_LIST, wxDefaultPosition,
wxSize( 300, 200 ), 0, NULL,
wxLB_NEEDED_SB | wxLB_SINGLE | wxLB_HSCROLL );
2007-12-11 23:12:37 +00:00
2010-11-19 18:50:23 +00:00
GeneralBoxSizer->Add( m_listBox, 0, wxGROW | wxALL, 5 );
2007-12-11 23:12:37 +00:00
2010-11-19 18:50:23 +00:00
InsertItems( aItemList, 0 );
2007-12-11 23:12:37 +00:00
2010-11-19 18:50:23 +00:00
if( m_callBackFct )
{
2010-11-19 18:50:23 +00:00
m_messages = new wxTextCtrl( this, -1, wxEmptyString,
wxDefaultPosition, wxSize( -1, 60 ),
wxTE_READONLY | wxTE_MULTILINE );
2007-12-11 23:12:37 +00:00
2010-11-19 18:50:23 +00:00
GeneralBoxSizer->Add( m_messages, 0, wxGROW | wxALL, 5 );
2007-12-11 23:12:37 +00:00
}
wxSizer* buttonSizer = CreateButtonSizer( wxOK | wxCANCEL );
if( buttonSizer )
GeneralBoxSizer->Add( buttonSizer, 0, wxGROW | wxALL, 5 );
2007-12-11 23:12:37 +00:00
GetSizer()->Fit( this );
GetSizer()->SetSizeHints( this );
2010-11-19 18:50:23 +00:00
Centre();
}
2007-12-11 23:12:37 +00:00
WinEDAListBox::~WinEDAListBox()
{
}
void WinEDAListBox::MoveMouseToOrigin()
{
2007-12-11 23:12:37 +00:00
int x, y, w, h;
2010-11-19 18:50:23 +00:00
wxSize list_size = m_listBox->GetSize();
int orgx = m_listBox->GetRect().GetLeft();
int orgy = m_listBox->GetRect().GetTop();
2007-12-11 23:12:37 +00:00
wxClientDisplayRect( &x, &y, &w, &h );
WarpPointer( x + orgx + 20, y + orgy + (list_size.y / 2) );
}
2007-12-11 23:12:37 +00:00
wxString WinEDAListBox::GetTextSelection()
{
2010-11-19 18:50:23 +00:00
wxString text = m_listBox->GetStringSelection();
2007-12-11 23:12:37 +00:00
return text;
2007-05-06 16:03:28 +00:00
}
2007-12-11 23:12:37 +00:00
void WinEDAListBox::Append( const wxString& item )
{
2010-11-19 18:50:23 +00:00
m_listBox->Append( item );
}
2007-12-11 23:12:37 +00:00
void WinEDAListBox::InsertItems( const wxArrayString& itemlist, int position )
{
2010-11-19 18:50:23 +00:00
m_listBox->InsertItems( itemlist, position );
}
2007-12-11 23:12:37 +00:00
void WinEDAListBox::OnCancelClick( wxCommandEvent& event )
{
2010-11-19 18:50:23 +00:00
EndModal( wxID_CANCEL );
}
2007-12-11 23:12:37 +00:00
void WinEDAListBox::ClickOnList( wxCommandEvent& event )
{
2007-12-11 23:12:37 +00:00
wxString text;
2010-11-19 18:50:23 +00:00
if( m_callBackFct )
2007-12-11 23:12:37 +00:00
{
2010-11-19 18:50:23 +00:00
m_messages->Clear();
text = m_listBox->GetStringSelection();
m_callBackFct( text );
m_messages->WriteText( text );
2007-12-11 23:12:37 +00:00
}
}
2007-12-11 23:12:37 +00:00
void WinEDAListBox::D_ClickOnList( wxCommandEvent& event )
{
2010-11-19 18:50:23 +00:00
EndModal( wxID_OK );
}
2007-12-11 23:12:37 +00:00
void WinEDAListBox::OnOkClick( wxCommandEvent& event )
{
2010-11-19 18:50:23 +00:00
EndModal( wxID_OK );
}
2007-12-11 23:12:37 +00:00
void WinEDAListBox::OnClose( wxCloseEvent& event )
{
2010-11-19 18:50:23 +00:00
EndModal( wxID_CANCEL );
}
/* Sort alphabetically, case insensitive.
2007-10-07 03:08:24 +00:00
*/
static int SortItems( const wxString** ptr1, const wxString** ptr2 )
{
2007-12-11 23:12:37 +00:00
return StrNumICmp( (*ptr1)->GetData(), (*ptr2)->GetData() );
}
2007-09-01 12:00:30 +00:00
void WinEDAListBox:: SortList()
{
2010-11-19 18:50:23 +00:00
int ii, NbItems = m_listBox->GetCount();
2007-12-11 23:12:37 +00:00
const wxString** BufList;
2007-12-11 23:12:37 +00:00
if( NbItems <= 0 )
return;
2007-12-11 23:12:37 +00:00
BufList = (const wxString**) MyZMalloc( 100 * NbItems * sizeof(wxString*) );
for( ii = 0; ii < NbItems; ii++ )
{
2010-11-19 18:50:23 +00:00
BufList[ii] = new wxString( m_listBox->GetString( ii ) );
2007-12-11 23:12:37 +00:00
}
2007-12-11 23:12:37 +00:00
qsort( BufList, NbItems, sizeof(wxString*),
( int( * ) ( const void*, const void* ) )SortItems );
2010-11-19 18:50:23 +00:00
m_listBox->Clear();
2007-12-11 23:12:37 +00:00
for( ii = 0; ii < NbItems; ii++ )
{
2010-11-19 18:50:23 +00:00
m_listBox->Append( *BufList[ii] );
2007-12-11 23:12:37 +00:00
delete BufList[ii];
}
2007-12-11 23:12:37 +00:00
free( BufList );
}
2007-12-11 23:12:37 +00:00
void WinEDAListBox::OnKeyEvent( wxKeyEvent& event )
{
2007-12-11 23:12:37 +00:00
event.Skip();
}