listboxes.cpp 12.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*************************************************************************/
/* listboxes.cpp: class for displaying footprint list and component list */
/*************************************************************************/

#include "fctsys.h"

#include "wxstruct.h"
#include "common.h"
#include "cvpcb.h"

#include "id.h"

#include "protos.h"

plyatov's avatar
plyatov committed
15 16 17 18 19 20
/************************************************************************/
/* Class (from wxListView) for displaying component and footprint lists */
/************************************************************************/

ListBoxBase::ListBoxBase(WinEDA_CvpcbFrame * parent,
				wxWindowID id, const wxPoint& loc, const wxSize& size):
21 22
			LIST_BOX_TYPE(parent, id, loc, size,
				wxSUNKEN_BORDER | wxLC_NO_HEADER |
plyatov's avatar
plyatov committed
23 24
				wxLC_SINGLE_SEL|wxLC_REPORT | wxLC_VIRTUAL)
{
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
	m_Parent = parent;
    InsertColumn(0, wxEmptyString);
	SetColumnWidth(0, wxLIST_AUTOSIZE );
}



ListBoxBase::~ListBoxBase(void)
{
}

/************************************************/
void ListBoxBase::OnSize(wxSizeEvent& event)
/************************************************/
// Ajust the column width to the entire available window width
{
wxSize size = GetClientSize();
int width = 0;
	
//	SetColumnWidth(0, wxLIST_AUTOSIZE );
//	width = GetColumnWidth(0);
	SetColumnWidth(0, MAX(width, size.x) );

	event.Skip();
plyatov's avatar
plyatov committed
49
}
50 51 52


/*********************************/
plyatov's avatar
plyatov committed
53
int ListBoxBase::GetSelection(void)
54 55 56 57 58 59
/*********************************/
// Return an index for the selected item
{
	return GetFirstSelected();
}

plyatov's avatar
plyatov committed
60 61 62 63 64 65 66 67 68

/************************************************************/
/* ListBox derivee pour l'affichage de la liste des Modules */
/************************************************************/

FootprintListBox::FootprintListBox(WinEDA_CvpcbFrame * parent,
				wxWindowID id, const wxPoint& loc, const wxSize& size,
				int nbitems, wxString choice[]):
			ListBoxBase(parent, id, loc, size)
69 70
{
	SetActiveFootprintList(TRUE);
plyatov's avatar
plyatov committed
71
}
72 73 74 75 76



FootprintListBox::~FootprintListBox(void)
{
plyatov's avatar
plyatov committed
77
}
78 79 80


/**********************************/
plyatov's avatar
plyatov committed
81
int FootprintListBox::GetCount(void)
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
/**********************************/
// Return number of items
{
	return m_ActiveFootprintList->Count();
}


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

/***************************************************/
plyatov's avatar
plyatov committed
100
wxString FootprintListBox::GetSelectedFootprint(void)
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
/***************************************************/
// Return an index for the selected item
{
wxString FootprintName;
int ii = GetFirstSelected();
	
	if ( ii >= 0 )
	{
	wxString msg = (*m_ActiveFootprintList)[ii];
		msg.Trim(TRUE); msg.Trim(FALSE);
		FootprintName = msg.AfterFirst(wxChar(' '));
	}

	return FootprintName;
}


/*********************************************************/
void FootprintListBox::AppendLine(const wxString & text)
/*********************************************************/
// Add an item at end of list
{
	m_ActiveFootprintList->Add(text);
	SetItemCount(m_ActiveFootprintList->Count() );
plyatov's avatar
plyatov committed
125
}
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151


/*********************************************************************/
wxString FootprintListBox::OnGetItemText(long item, long column) const
/*********************************************************************/
/* Overlayed function: MUST be provided in wxLC_VIRTUAL mode
	because real datas are not handled by ListBoxBase
*/
{
	return m_ActiveFootprintList->Item(item);
}

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

#ifndef __WXMAC__
	Select(index, State);
#endif
	EnsureVisible(index);
#ifdef __WXMAC__
	Refresh();
#endif
plyatov's avatar
plyatov committed
152
}
153

plyatov's avatar
plyatov committed
154 155 156 157 158 159 160 161 162 163 164 165

/***************************************************************/
/* ListBox derivee pour l'affichage de la liste des Composants */
/***************************************************************/

ListBoxCmp::ListBoxCmp(WinEDA_CvpcbFrame * parent, wxWindowID id,
				const wxPoint& loc, const wxSize& size,
				int nbitems, wxString choice[]):
			ListBoxBase(parent, id, loc, size)
{
}

166 167 168 169


ListBoxCmp::~ListBoxCmp(void)
{
plyatov's avatar
plyatov committed
170
}
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192



/***********************************************************************/
/* Construction de la table des evenements pour la fenetre des composants */
/***********************************************************************/

BEGIN_EVENT_TABLE(ListBoxCmp, LIST_BOX_TYPE)
	EVT_SIZE( ListBoxBase::OnSize)

END_EVENT_TABLE()

/****************************/
void ListBoxCmp::Clear(void)
/****************************/
// Reset ALL datas
{
	m_ComponentList.Clear();
	SetItemCount(0);
}

/******************************/
plyatov's avatar
plyatov committed
193
int ListBoxCmp::GetCount(void)
194 195 196 197
/******************************/
// Return number of items
{
	return m_ComponentList.Count();
plyatov's avatar
plyatov committed
198
}
199 200 201 202 203 204 205 206
/********************************************************************/
void ListBoxCmp::SetString(unsigned linecount, const wxString & text)
/********************************************************************/
// Change an item text
{
	if (linecount >= m_ComponentList.Count())
		linecount = m_ComponentList.Count()-1;
	if ( linecount >= 0 ) m_ComponentList[linecount] = text;
plyatov's avatar
plyatov committed
207
}
208 209 210 211 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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

/****************************************************/
void ListBoxCmp::AppendLine(const wxString & text)
/****************************************************/
// Add an item at end of list
{
	m_ComponentList.Add(text);
	SetItemCount(m_ComponentList.Count() );
}


/****************************************************************/
wxString ListBoxCmp::OnGetItemText(long item, long column) const
/****************************************************************/
/* Overlayed function: MUST be provided in wxLC_VIRTUAL mode
	because real datas are not handled by ListBoxBase
*/
{
	return m_ComponentList.Item(item);
}

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

#ifndef __WXMAC__
	Select(index, State);
#endif
	EnsureVisible(index);
#ifdef __WXMAC__
	Refresh();
#endif
}

/********************************************/
void WinEDA_CvpcbFrame::BuildCmpListBox(void)
/********************************************/
/* Construit la fenetre d'affichage de la liste des composant.
	dimx et dimy sont les dimensions de la surface totale d'affichage
	et non les dims de la fenetre des Cmp
	Si la fenetre d'affichage a deja ete creee, il y a seulement
	mise a jour de la liste
*/
{
int ii;
STORECMP * Composant;
plyatov's avatar
plyatov committed
257
wxString msg;
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
wxSize size(10,10);

	if( m_ListCmp == NULL )
	{
		m_ListCmp = new ListBoxCmp(this, ID_CVPCB_COMPONENT_LIST,
							wxDefaultPosition, size,
							0, NULL);
		m_ListCmp->SetBackgroundColour(wxColour(225,255,255));
		m_ListCmp->SetForegroundColour(wxColour(0,0,0));
		m_ListCmp->SetFont(*g_FixedFont);
	}

	m_ListCmp->m_ComponentList.Clear();
	Composant = g_BaseListeCmp;
	for (ii = 1 ; Composant != NULL; Composant = Composant->Pnext, ii++ )
	{
		msg.Printf(CMP_FORMAT ,ii,
			Composant->m_Reference.GetData(), Composant->m_Valeur.GetData(),
			Composant->m_Module.GetData());
		m_ListCmp->m_ComponentList.Add(msg);
	}
	m_ListCmp->SetItemCount(m_ListCmp->m_ComponentList.Count() );

	m_ListCmp->SetSelection(0, TRUE);
}


/*************************************************************/
void WinEDA_CvpcbFrame::BuildFootprintListBox(void)
/*************************************************************/
/* Construit la fenetre d'affichage de la liste des Modules.
	dimx et dimy sont les dimensions de la surface totale d'affichage
	et non les dims de la fenetre des Modules
	Si la fenetre d'affichage a deja ete creee, il y a seulement
	mise a jour de la liste
*/
{
wxString msg;
wxSize size(10,10);

	if( m_FootprintList == NULL )
	{
		m_FootprintList = new FootprintListBox(this, ID_CVPCB_FOOTPRINT_LIST,
							wxDefaultPosition, size,
							0, NULL);
		m_FootprintList->SetBackgroundColour(wxColour(225,255,225));
		m_FootprintList->SetForegroundColour(wxColour(0,0,0));
		m_FootprintList->SetFont(*g_FixedFont);
	}

	m_FootprintList->SetFootprintFullList();
	
	msg.Printf(_("Footprints: %d"), m_FootprintList->GetCount());
	SetStatusText(msg,2);
}


/************************************************/
void FootprintListBox::SetFootprintFullList(void)
/************************************************/
{
STOREMOD * FootprintItem;
wxString msg;
int OldSelection = GetSelection();
	
	m_FullFootprintList.Clear();
	FootprintItem = g_BaseListePkg;

	for ( int ii = 1; FootprintItem != NULL; FootprintItem = FootprintItem->Pnext, ii++ )
	{
		msg.Printf( wxT("%3d %s"), ii, FootprintItem->m_Module.GetData());
		m_FullFootprintList.Add(msg);
	}
	
	SetActiveFootprintList(TRUE);

	if ( (GetCount() == 0) ||  (OldSelection < 0) || ( OldSelection >= GetCount() ) )
		SetSelection(0, TRUE);
	Refresh();
}

/**********************************************************************/
void FootprintListBox::SetFootprintFilteredList(STORECMP * Component)
/*********************************************************************/
{
STOREMOD * FootprintItem;
wxString msg;
int OldSelection = GetSelection();
bool HasItem = FALSE;
	
	m_FilteredFootprintList.Clear();
	FootprintItem = g_BaseListePkg;

	int cmpnum = 1;
	for ( int ii = 0; FootprintItem != NULL; FootprintItem = FootprintItem->Pnext, ii++ )
	{
		/* Search for matching footprints */
		for ( unsigned jj = 0; jj < Component->m_FootprintFilter.GetCount(); jj ++ )
		{
			if ( ! FootprintItem->m_Module.Matches(Component->m_FootprintFilter[jj]) )
				continue;
			msg.Printf( wxT("%3d %s"), cmpnum++, FootprintItem->m_Module.GetData());
			m_FilteredFootprintList.Add(msg);
			HasItem = TRUE;
		}
	}
	
	
	if ( HasItem ) SetActiveFootprintList(FALSE);
	else SetActiveFootprintList(TRUE);

	
	if ( (GetCount() == 0) || ( OldSelection >= GetCount() ) )
		SetSelection(0, TRUE);
	
	Refresh();
}


/**************************************************************************/
void FootprintListBox::SetActiveFootprintList(bool FullList, bool Redraw)
/**************************************************************************/
{
bool old_selection = m_UseFootprintFullList;
	
	if ( FullList )
	{
		m_UseFootprintFullList = TRUE;
		m_ActiveFootprintList = &m_FullFootprintList;
		SetItemCount(m_FullFootprintList.GetCount() );
	}
	else
	{
		m_UseFootprintFullList = FALSE;
		m_ActiveFootprintList = &m_FilteredFootprintList;
		SetItemCount(m_FilteredFootprintList.GetCount() );
	}

	if ( Redraw )
	{
		if ( ! m_UseFootprintFullList || (m_UseFootprintFullList != old_selection) )
		{
			Refresh();
		}
	}

	if ( ! m_UseFootprintFullList || (m_UseFootprintFullList != old_selection) )
	{
		m_Parent->SetStatusText(wxEmptyString,0);
		m_Parent->SetStatusText(wxEmptyString,1);
	}
	
	wxString msg;
	if ( FullList )
		msg.Printf(_("Footprints (All): %d"), m_ActiveFootprintList->GetCount());
	else
		msg.Printf(_("Footprints (filtered): %d"), m_ActiveFootprintList->GetCount());
	m_Parent->SetStatusText(msg,2);
}

/***********************************************************************/
/* Construction de la table des evenements pour la fenetre des modules */
/***********************************************************************/

BEGIN_EVENT_TABLE(FootprintListBox, LIST_BOX_TYPE)
	EVT_SIZE( ListBoxBase::OnSize)

END_EVENT_TABLE()


/********************************************************/
void FootprintListBox::OnLeftClick(wxListEvent & event)
/********************************************************/
{
STOREMOD * Module;
wxString msg;
wxString FootprintName = GetSelectedFootprint();
	
	Module = GetModuleDescrByName(FootprintName);
	if( m_Parent->DrawFrame )
	{
		m_Parent->CreateScreenCmp(); /* refresh general */
	}

	if ( Module ) msg = Module->m_Doc;
	m_Parent->SetStatusText(msg,0);

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


/******************************************************/
void FootprintListBox::OnLeftDClick(wxListEvent & event)
/******************************************************/
{
wxString FootprintName = GetSelectedFootprint();

	m_Parent->SetNewPkg(FootprintName);
}

/**************************************************************/
STOREMOD * GetModuleDescrByName(const wxString & FootprintName)
/**************************************************************/
{
STOREMOD * FootprintItem = g_BaseListePkg;

	for ( ; FootprintItem != NULL; FootprintItem = FootprintItem->Pnext )
	{
		if( FootprintItem->m_Module == FootprintName)
			break;	// found !
	}
	return FootprintItem;
}