summaryrefslogtreecommitdiff
path: root/win/Piececmb.cpp
blob: f1f3e2920df4a97712c8345386f824d3ae264704 (plain)
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
// PieceCmb.cpp : implementation file
//

#include "stdafx.h"
#include "leocad.h"
#include "PieceCmb.h"
#include "PieceBar.h"
#include "pieceinf.h"
#include "project.h"
#include "globals.h"
#include "library.h"
#include "lc_application.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPiecesCombo

CPiecesCombo::CPiecesCombo()
{
	m_bAutoComplete = TRUE;
}

CPiecesCombo::~CPiecesCombo()
{
}


BEGIN_MESSAGE_MAP(CPiecesCombo, CComboBox)
	//{{AFX_MSG_MAP(CPiecesCombo)
	ON_CONTROL_REFLECT(CBN_EDITUPDATE, OnEditupdate)
	ON_CONTROL_REFLECT(CBN_SELCHANGE, OnSelchange)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPiecesCombo message handlers

void CPiecesCombo::OnEditupdate() 
{
	if (!m_bAutoComplete)
		return;

	char str[66];
	PiecesLibrary *pLib = lcGetPiecesLibrary();
	CPiecesBar* pBar = (CPiecesBar*)GetParent();
	PieceInfo* pInfo;

	if (int n = GetWindowText(str, 65))
	{
		char newstr[66];
		int sel = -1;
		strcpy (newstr, "Z");
		for (int i = 0; i < pLib->GetPieceCount(); i++)
		{
			pInfo = pLib->GetPieceInfo(i);

			if ((pInfo->m_strDescription[0] == '~') && !pBar->m_bSubParts)
				continue;

			if (_strnicmp (str, pInfo->m_strDescription, n) == 0)
			{
				if (_stricmp (newstr, pInfo->m_strDescription) > 0)
				{
					strcpy (newstr, pInfo->m_strDescription);
					sel = i;
				}
			}
			else
				if (_strnicmp (str, pInfo->m_strName, n) == 0)
				{
					if (_stricmp (newstr, pInfo->m_strName) > 0)
					{
						strcpy (newstr, pInfo->m_strName);
						sel = i;
					}
				}
		}

		if (sel >= 0)
			SelectPiece(pLib->GetPieceInfo(sel));

		if (strlen (newstr) > 1)
		{
			SetWindowText(newstr);
			SetEditSel(n, -1);
		}
	}
}

BOOL CPiecesCombo::PreTranslateMessage(MSG* pMsg) 
{
	if (pMsg->message == WM_KEYDOWN)
	{
		m_bAutoComplete = TRUE;
		int nVirtKey = (int) pMsg->wParam;
		if (nVirtKey == VK_DELETE || nVirtKey == VK_BACK)
		{
			CEdit* Edit = (CEdit*)GetWindow(GW_CHILD);
			m_bAutoComplete = FALSE;
			Edit->ReplaceSel("");
		}
		else if (nVirtKey == VK_RETURN)
		{
		  PiecesLibrary* Lib = lcGetPiecesLibrary();
			CString str;

			GetWindowText(str);

			int Index = Lib->FindCategoryIndex("Search Results");

			if (Index == -1)
				Lib->AddCategory("Search Results", (const char*)str);
			else
				Lib->SetCategory(Index, "Search Results", (const char*)str);
		}
	}

	return CComboBox::PreTranslateMessage(pMsg);
}

void CPiecesCombo::OnSelchange() 
{
	char str[66];
	CPiecesBar* pBar = (CPiecesBar*)GetParent();
  PiecesLibrary *pLib = lcGetPiecesLibrary();

	if (!GetLBText (GetCurSel(), str))
		return;

	for (int i = 0; i < pLib->GetPieceCount(); i++)
	{
		PieceInfo* pInfo = pLib->GetPieceInfo(i);

		if (strcmp(str, pInfo->m_strDescription) == 0)
			SelectPiece(pInfo);
	}
}

void CPiecesCombo::SelectPiece(PieceInfo* Info)
{
  PiecesLibrary *Lib = lcGetPiecesLibrary();
	CPiecesBar* Bar = (CPiecesBar*)GetParent();

	int Index = Lib->GetFirstCategory(Info);

	if (Index != -1)
		Bar->SelectPiece(Lib->GetCategoryName(Index), Info);
}