File: get_component_dialog.cpp

package info (click to toggle)
kicad 0.0.20060829-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 56,544 kB
  • ctags: 9,194
  • sloc: cpp: 88,990; ansic: 4,790; makefile: 88; sh: 39
file content (252 lines) | stat: -rw-r--r-- 7,775 bytes parent folder | download
1
2
3
4
5
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
72
73
74
75
76
77
78
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
121
122
123
124
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
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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*********************************/
/*	get_component_dialog.cpp	 */
/*********************************/

#include "fctsys.h"
#include "gr_basic.h"

#include "common.h"
#include "macros.h"

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

static unsigned s_HistoryMaxCount = 8;	// Max number of items displayed in history list
static wxString s_ItemName;


enum selcmp_id {
	ID_ACCEPT_NAME = 3900,
	ID_ACCEPT_KEYWORD,
	ID_ENTER_NAME,
	ID_CANCEL,
	ID_LIST_ALL,
	ID_EXTRA_TOOL,
	ID_SEL_BY_LISTBOX
};

/***************************************/
class WinEDA_SelectCmp: public wxDialog
/***************************************/
{
private:
	WinEDA_DrawFrame * m_Parent;
	bool m_AuxTool;
	wxString * m_Text;
	wxTextCtrl * m_TextCtrl;
	wxListBox * m_List;
public:
	bool m_GetExtraFunction;

public:

	// Constructor and destructor
	WinEDA_SelectCmp(WinEDA_DrawFrame *parent, const wxPoint& framepos,
			wxArrayString & HistoryList, const wxString & Title,
			bool show_extra_tool );
	~WinEDA_SelectCmp(void) {};

private:
	void Accept(wxCommandEvent& event);
	void GetExtraSelection(wxCommandEvent& event);

	DECLARE_EVENT_TABLE()

};
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)
END_EVENT_TABLE()


/****************************************************************************/
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)
/****************************************************************************/
/* Dialog frame to choose a component or a footprint
	This dialog shows an history of last selected items
*/
{
wxButton * Button;
wxStaticText * Text;
	
	m_Parent = parent;
	m_AuxTool = show_extra_tool;
	m_GetExtraFunction = FALSE;
	
	SetFont(*g_DialogFont);
	s_ItemName.Empty();
	m_Text =  & s_ItemName;
	
    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);
	
	m_TextCtrl = new wxTextCtrl(this, ID_ENTER_NAME, *m_Text);
	m_TextCtrl->SetInsertionPoint(1);
    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->SetForegroundColour(*wxRED);
	Button->SetDefault();
    RightBoxSizer->Add(Button, 0, wxGROW|wxLEFT|wxRIGHT|wxTOP|wxBOTTOM, 5);

	Button = new wxButton(this, ID_ACCEPT_KEYWORD, _("Search KeyWord"));
	Button->SetForegroundColour(*wxRED);
    RightBoxSizer->Add(Button, 0, wxGROW|wxLEFT|wxRIGHT|wxBOTTOM, 5);

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

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

#ifndef __WXMAC__
	if ( m_AuxTool )	/* The selection can be done by an extra function */
	{
		
		Button = new wxButton(this, ID_EXTRA_TOOL, _("By Lib Browser"));
		Button->SetForegroundColour(wxColor(80, 0, 80));
		RightBoxSizer->Add(Button, 0, wxGROW|wxLEFT|wxRIGHT|wxBOTTOM, 5);
	}
#endif	

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

/*********************************************************/
void WinEDA_SelectCmp::Accept(wxCommandEvent& event)
/*********************************************************/
{
	switch (event.GetId() )
	{
		case ID_SEL_BY_LISTBOX:
			*m_Text = m_List->GetStringSelection();
			break;

		case ID_ACCEPT_NAME:
			*m_Text = m_TextCtrl->GetValue();
			break;

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

		case ID_CANCEL:
			*m_Text = wxEmptyString;
			break;
	
		case ID_LIST_ALL:
			*m_Text = wxT("*");
			break;
	
	}
	m_Text->Trim(FALSE);	// Remove blanks at beginning
	m_Text->Trim(TRUE);	// Remove blanks at end
	Close(TRUE);
}


/**************************************************************/
void WinEDA_SelectCmp::GetExtraSelection(wxCommandEvent& event)
/**************************************************************/
/* Get the component name by the extra function
*/
{
	m_GetExtraFunction = TRUE;
	Close(TRUE);
}



/******************************************************************************/
wxString GetComponentName(WinEDA_DrawFrame * frame,
		wxArrayString & HistoryList, const wxString & Title,
		wxString(*AuxTool)(WinEDA_DrawFrame *parent))
/*******************************************************************************/
/* Dialog frame to choose a component name
*/
{
wxPoint framepos;
int x, y, w, h;
bool GetExtraFunction;
	
	framepos = wxGetMousePosition();
	wxClientDisplayRect(&x, &y, &w, &h);
	framepos.x -= 100; framepos.y -= 50;
	if ( framepos.x < x ) framepos.x = x;
	if ( framepos.y < y ) framepos.y = y;
	if ( framepos.x < x ) framepos.x = x;
	x += w -350; if ( framepos.x > x ) framepos.x = x;
	if ( framepos.y < y ) framepos.y = y;
WinEDA_SelectCmp * selframe =
		new WinEDA_SelectCmp(frame, framepos, HistoryList, Title, AuxTool ? TRUE : FALSE);
	selframe->ShowModal();
	GetExtraFunction = selframe->m_GetExtraFunction;
	selframe->Destroy();
	
	if ( GetExtraFunction ) s_ItemName = AuxTool(frame);
	return s_ItemName;
}


/*******************************************************************************/
void AddHistoryComponentName(wxArrayString & HistoryList, const wxString & Name)
/*******************************************************************************/
/* Add the string "Name" to the history list HistoryList
*/
{
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);
}