// PadajuciIzbornikDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PadajuciIzbornik.h"
#include "PadajuciIzbornikDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CPadajuciIzbornikDlg dialog




CPadajuciIzbornikDlg::CPadajuciIzbornikDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPadajuciIzbornikDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPadajuciIzbornikDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CPadajuciIzbornikDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(GumbBT, &CPadajuciIzbornikDlg::OnBnClickedGumbbt)
	ON_LBN_SELCHANGE(PadIzbLB, &CPadajuciIzbornikDlg::OnLbnSelchangePadizblb)
	ON_BN_CLICKED(StoBT, &CPadajuciIzbornikDlg::OnBnClickedStobt)
	ON_LBN_DBLCLK(PadIzbLB, &CPadajuciIzbornikDlg::OnLbnDblclkPadizblb)
	ON_EN_UPDATE(TraziEB, &CPadajuciIzbornikDlg::OnEnUpdateTrazieb)
END_MESSAGE_MAP()


// CPadajuciIzbornikDlg message handlers

BOOL CPadajuciIzbornikDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	CListBox *pw = (CListBox*) GetDlgItem(PadIzbLB);
	pw->AddString(CString("Kruška"));
	pw->AddString(CString("Jabuka"));
	pw->AddString(CString("Šljiva"));


	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CPadajuciIzbornikDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CPadajuciIzbornikDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CPadajuciIzbornikDlg::OnBnClickedGumbbt()
{
	// TODO: Add your control notification handler code here
	CListBox *pLB = (CListBox*) GetDlgItem(PadIzbLB);
	CWnd     *pEB = this->GetDlgItem (VoceEB);
	CString  tekst;
	pEB->GetWindowTextW (tekst);
//	pw->AddString(CString("Jagoda"));
	int p = pLB->FindString (0, tekst);
	if (p == -1) pLB->AddString(tekst);
	else MessageBox(CString("Tekst već postoji!"));
}

void CPadajuciIzbornikDlg::OnLbnSelchangePadizblb()
{
	// TODO: Add your control notification handler code here
}

void CPadajuciIzbornikDlg::OnBnClickedStobt()
{
	// TODO: Add your control notification handler code here
	CListBox *pLB = (CListBox*) GetDlgItem(PadIzbLB);
	CWnd *pPopis  =  GetDlgItem(PopisLB);
	CString  tekst;
	int i = pLB->GetCurSel ();
	if (i>=0) {
		pLB->GetText (i, tekst);
		MessageBox(tekst);
		pLB->DeleteString (i);
		CString stari;
		pPopis->GetWindowTextW (stari);
		stari += CString("\n") + tekst;
		pPopis->SetWindowTextW (stari);
	}
}

void CPadajuciIzbornikDlg::OnLbnDblclkPadizblb()
{
	// TODO: Add your control notification handler code here
	OnBnClickedStobt();

}

void CPadajuciIzbornikDlg::OnEnUpdateTrazieb()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_UPDATE flag ORed into the lParam mask.

	// TODO:  Add your control notification handler code here

	//MessageBox(CString("Klik!"));
	CListBox *pLB = (CListBox*) GetDlgItem(PadIzbLB);
	CWnd *pTrazi  =  GetDlgItem(TraziEB);
	CString  tekst, redak;
	pTrazi->GetWindowTextW (tekst);
	
	//isprazni label
	for (int i=0; i<pLB->GetCount() ; ++i) {
		pLB->GetText (i, redak);
		if (redak.Find(tekst) >= 0) {
//			MessageBox(CString("ima"));
			CWnd *pNiz =  GetDlgItem(NizLB);
			CString stari;
			pNiz->GetWindowTextW (stari);
			stari += CString("\n") + redak;
			pNiz->SetWindowTextW (stari);
		}

//		else MessageBox(CString("nema"));
	}


}
