/////////////////////////////
// ECCSkelton
// CLISTBOXクラス定義ファイル
// Copyright (c) August, 2022
//        By Ysama
/////////////////////////////

class CLISTBOX : public CCTRL
{
public:
public:		//メンバー関数
	void Init(HINSTANCE);
	bool Create(LPWSTR, DWORD,
				DWORD, HWND,
				int, int, int,
				int, int);			//ウィンドウ作成関数
	void Reset();
	bool AddString(WCHAR*);
	bool InsertString(int, WCHAR*);
	bool DeleteString(int);
	int GetCursel();
	int GetCount();
	int GetTextLen(int);
	bool GetText(int, WCHAR*);
	bool SetCursel(int);
	int SelectString(WCHAR*);
};

void CLISTBOX::Init(HINSTANCE hInst) {

	m_lpClassName = L"LISTBOX";
	m_hInstance = hInst;
}

bool CLISTBOX::Create(LPWSTR WindowName,
				DWORD Style = WS_CHILD | LBS_STANDARD, DWORD ExStyle = NULL,
				HWND hParent= NULL, int hMenu = NULL,
				int x = CW_USEDEFAULT, int y = CW_USEDEFAULT,
				int w = CW_USEDEFAULT, int h = CW_USEDEFAULT) {

	m_hWnd = CreateWindowExW(m_dwExStyle = ExStyle,
					    	m_lpClassName,
							m_lpWindowName = WindowName,
							m_dwStyle = Style,
							m_x = x,
							m_y = y,
							m_Width = w,
							m_Height = h,
							m_hParent = hParent,
							m_hMenu = (HMENU)hMenu,
							m_hInstance,
							m_lpParam
	);
	if(!m_hWnd) {
		MessageBoxW(0, L"ウィンドウを作れませんでした!", L"エラー",
				MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
		return FALSE;
	}
	//ウィンドウの表示と再描画
	ShowWindow(m_hWnd, SW_SHOWNORMAL);
	UpdateWindow(m_hWnd);
	return TRUE;
}

void CLISTBOX::Reset() {
	SendMessageW(m_hWnd, LB_RESETCONTENT, 0, 0);
}

bool CLISTBOX::AddString(WCHAR* str) {
	return SendMessageW(m_hWnd, LB_ADDSTRING, 0, (LPARAM)str) != LB_ERR;
}

bool CLISTBOX::InsertString(int n, WCHAR* str) {
	return SendMessageW(m_hWnd, LB_INSERTSTRING, n, (LPARAM)str) != LB_ERR;
}

bool CLISTBOX::DeleteString(int n) {
	return SendMessageW(m_hWnd, LB_DELETESTRING, (WPARAM)n, 0) != LB_ERR;
}

int CLISTBOX::GetCursel() {
	return SendMessageW(m_hWnd, LB_GETCURSEL, 0, 0);
}

int CLISTBOX::GetCount() {
	return SendMessageW(m_hWnd, LB_GETCOUNT, 0, 0);
}

int CLISTBOX::GetTextLen(int n) {
	return SendMessageW(m_hWnd, LB_GETTEXTLEN, (WPARAM)n, 0);
}

bool CLISTBOX::GetText(int n, WCHAR* buff) {
	return SendMessageW(m_hWnd, LB_GETTEXT, (WPARAM)n, (LPARAM)buff) != LB_ERR;
}

bool CLISTBOX::SetCursel(int n) {
	return SendMessageW(m_hWnd, LB_SETCURSEL, (WPARAM)n, 0) != LB_ERR;
}

int CLISTBOX::SelectString(WCHAR* str) {
	return SendMessageW(m_hWnd, LB_SELECTSTRING, (WPARAM)-1, (LPARAM)str);
}

/*
通知コード		解説
-------------   ----------------------------------------------------
LBN_DBLCLK		項目をダブルクリック
LBN_ERRSPACE	リストボックスがメモリを使いきりメモリを取得できない
LBN_KILLFOCUS	キーボードフォーカスを失った
LBN_SELCANCEL	選択項目をキャンセル
LBN_SELCHANGE	選択項目に、何らかの変更があった
LBN_SETFOCUS	キーボードフォーカスを得た
*/
