summaryrefslogtreecommitdiff
path: root/win/Piecelst.cpp
blob: 1506890d41880aee506e96b96cae23c49b5dd6e2 (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
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
// PieceLst.cpp : implementation file
//

#include "stdafx.h"
#include "leocad.h"
#include "PieceLst.h"
#include "PieceBar.h"
#include "project.h"
#include "pieceinf.h"
#include "globals.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPiecesList

static int CALLBACK ListViewCompareProc(LPARAM lP1, LPARAM lP2, LPARAM lParamSort)
{
	if ((lP1 < 0) || (lP2 < 0)) return 0;

	if (lParamSort == 0)
		return strcmpi(((PieceInfo*)lP1)->m_strDescription, ((PieceInfo*)lP2)->m_strDescription);

	return strcmpi(((PieceInfo*)lP1)->m_strName, ((PieceInfo*)lP2)->m_strName);
}

CPiecesList::CPiecesList()
{
	// TODO: Load from registry
	memset(m_nLastPieces, 0, sizeof(m_nLastPieces));
}

CPiecesList::~CPiecesList()
{
	// TODO: save m_nLastPieces to registry
}


BEGIN_MESSAGE_MAP(CPiecesList, CListCtrl)
	//{{AFX_MSG_MAP(CPiecesList)
	ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, OnColumnclick)
	ON_NOTIFY_REFLECT(LVN_ITEMCHANGED, OnItemchanged)
	ON_WM_MOUSEMOVE()
	ON_WM_CREATE()
	ON_WM_KEYDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPiecesList message handlers

void CPiecesList::OnColumnclick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	SortItems((PFNLVCOMPARE)ListViewCompareProc, pNMListView->iSubItem);
	*pResult = 0;
}

void CPiecesList::OnItemchanged(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

	if (pNMListView->uNewState & LVIS_SELECTED)
	{
		CPiecesBar* pBar = (CPiecesBar*)GetParent();

		LV_ITEM lvi;
		lvi.iItem = pNMListView->iItem;
		lvi.iSubItem = 0;
		lvi.mask = LVIF_PARAM;
		GetItem (&lvi);

		// Let everybody know the selection changed.
		PieceInfo* pInfo = (PieceInfo*)lvi.lParam;
		project->SetCurrentPiece(pInfo);
		pBar->m_wndPiecePreview.SetPieceInfo(pInfo);
		pBar->m_wndPiecePreview.PostMessage(WM_PAINT);

		if (pBar->m_bGroups)
			m_nLastPieces[pBar->m_nCurGroup] = pNMListView->iItem;
	}

	*pResult = 0;
}

void CPiecesList::UpdateList()
{
	CWaitCursor wc;
	CPiecesBar* pBar = (CPiecesBar*)GetParent();

	SetRedraw (FALSE);
	DeleteAllItems();

	LV_ITEM lvi;
	lvi.mask = LVIF_TEXT | LVIF_PARAM;

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

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

		if ((!pBar->m_bGroups) ||
			((pInfo->m_nGroups & (DWORD)(1 << pBar->m_nCurGroup)) != 0))
		{
			TCHAR tmp[65], tmp2[10];
			strcpy (tmp, pInfo->m_strDescription);
			lvi.iItem = 0;
			lvi.iSubItem = 0;
			lvi.pszText = tmp;
			lvi.lParam = (LPARAM)pInfo;
			int idx = InsertItem(&lvi);

			strcpy (tmp2, pInfo->m_strName);
			SetItemText(idx, 1, tmp2);
		}
	}
	EnsureVisible(m_nLastPieces[pBar->m_nCurGroup], FALSE);
	SetItemState (m_nLastPieces[pBar->m_nCurGroup], LVIS_SELECTED | LVIS_FOCUSED , LVIS_SELECTED | LVIS_FOCUSED);
	SetRedraw (TRUE);
	Invalidate();
}

int CPiecesList::CellRectFromPoint(CPoint & point, RECT* cellrect, int* col) const
{
	int colnum;
	
	if((GetStyle() & LVS_TYPEMASK) != LVS_REPORT)
		return -1;
	
	// Get the top and bottom row visible
	int row = GetTopIndex();
	int bottom = row + GetCountPerPage();
	if( bottom > GetItemCount() )
		bottom = GetItemCount();
	
	// Get the number of columns
	CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0);
	if (!pHeader) return -1;
	int nColumnCount = pHeader->GetItemCount();
	
	// Loop through the visible rows
	for( ;row <=bottom;row++)
	{
		// Get bounding rect of item and check whether point falls in it.
		CRect rect;
		GetItemRect( row, &rect, LVIR_BOUNDS );
		if( rect.PtInRect(point) )
		{
			// Now find the column
			for (colnum = 0; colnum < nColumnCount; colnum++)
			{
				int colwidth = GetColumnWidth(colnum);
				if( point.x >= rect.left && 
					point.x <= (rect.left + colwidth ) )
				{
					// Found the column
					RECT rectClient;
					GetClientRect( &rectClient );
					if( point.x > rectClient.right )
						return -1;
					if( col ) 
						*col = colnum;
					rect.right = rect.left + colwidth;
					if( rect.right > rectClient.right ) 
						rect.right = rectClient.right;
					*cellrect = rect;
					return row;
				}
				rect.left += colwidth;
			}
		}
	}
	return -1;
}

void CPiecesList::OnMouseMove(UINT nFlags, CPoint point) 
{
	if( nFlags == 0 )
	{
		int row, col;
		RECT cellrect;
		row = CellRectFromPoint (point, &cellrect, &col);
		if (row != -1)
		{
			int offset = 7;
			if( col == 0 ) 
			{
				CRect rcLabel;
				GetItemRect(row, &rcLabel, LVIR_LABEL);
				offset = rcLabel.left - cellrect.left + offset / 2;
			}
			cellrect.top--;

			m_TitleTip.Show (cellrect, GetItemText(row, col), offset-1, GetItemState (row, LVIS_FOCUSED));
		}
	}
	
	CListCtrl::OnMouseMove(nFlags, point);
}

int CPiecesList::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CListCtrl::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	m_TitleTip.Create(this);
	
	return 0;
}

void CPiecesList::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	if (nChar == VK_INSERT)
	{
		project->HandleCommand(LC_PIECE_INSERT, 0);

		CFrameWnd* pFrame = (CFrameWnd*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveView();
		pView->SetFocus();
	}
	else
		CListCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
}