/////////////////////////////////////////////////////////////////////////////
// Name:        minimal.cpp
// Purpose:     Minimal wxWindows sample
// Author:      Julian Smart
// Modified by:
// Created:     04/01/98
// RCS-ID:      $Id: frame1.cpp,v 1.1 2001/02/25 11:56:04 aleksandras Exp $
// Copyright:   (c) Julian Smart
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

#ifdef __GNUG__
    #pragma implementation "minimal.cpp"
    #pragma interface "minimal.cpp"
#endif

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#include "frame1.h"
#include "wx/wspp/myodbc.h"
#include "wx/wspp/dblist.h"
#include "wx/wspp/datectrl.h"

#include "wx/xml/xmlres.h"

IMPLEMENT_DYNAMIC_CLASS( MyFrame1, wxFrame )

BEGIN_EVENT_TABLE(MyFrame1, wxFrame)

    EVT_MENU(Minimal_Quit,  MyFrame1::OnQuit)
    EVT_MENU(Minimal_About, MyFrame1::OnAbout)

	EVT_BUTTON( XMLID("insert"), MyFrame1::OnInsert )
	EVT_BUTTON( XMLID("remove"), MyFrame1::OnRemove )
	EVT_BUTTON( XMLID("edit"),   MyFrame1::OnEdit )

	EVT_BUTTON( XMLID("hello"),  MyFrame1::OnHello )

	EVT_INIT_DIALOG( MyFrame1::OnInit )

END_EVENT_TABLE()

MyFrame1::MyFrame1()
	: mpDbList( NULL )
{}

// event handlers

void MyFrame1::OnInit(wxInitDialogEvent& event)
{
	wxDateCtrl* date = (wxDateCtrl*)FindWindow( XMLID("datectrl1") );

	wxDateTime dt( 15, wxDateTime::Apr, 2003, 0,0,0,0 );

	date->SetValue( dt );
}

void MyFrame1::OnQuit(wxCommandEvent& WXUNUSED(event))
{
    // TRUE is to force the frame to close
    Close(TRUE);
}

void MyFrame1::OnAbout(wxCommandEvent& WXUNUSED(event))
{
    wxString msg;
    msg.Printf( _T("This is the about dialog of minimal sample.\n")
                _T("Welcome to %s"), wxVERSION_STRING);

    wxMessageBox(msg, "About WSPP Demo v-0.1", wxOK | wxICON_INFORMATION, this);
}

void MyFrame1::OnHello(wxCommandEvent& event)
{
	wxMessageBox( "Hello WSPP" );
}

/*** event handlers ***/

void MyFrame1::OnInsert(wxCommandEvent& event)
{
	wxStringList lst;
	wxString id = "3";
	lst.Add( id );

	MyODBCCursor* cursor = (MyODBCCursor*)GetDbList().GetDataCursor();

	bool ok =
		cursor->GetConnection()->ExecuteDirectSQL( "insert into md_recs (id, is_local) values (" + id + ",'true')" );

	if ( ok )

		GetDbList().OnRowChanged( wxDbListCtrl::INSERTED, id, 0, lst, TRUE );
}

void MyFrame1::OnRemove(wxCommandEvent& event)
{
	wxStringList lst;
	wxString id = "3";

	MyODBCCursor* cursor = (MyODBCCursor*)GetDbList().GetDataCursor();

	bool ok =
		cursor->GetConnection()->ExecuteDirectSQL( "delete from md_recs where id = " + id );

	if ( ok )

		GetDbList().OnRowChanged( wxDbListCtrl::REMOVED, id, 0, lst, TRUE );
}

void MyFrame1::OnEdit(wxCommandEvent& event)
{
	wxStringList lst;
	wxString id = "5";

	MyODBCCursor* cursor = (MyODBCCursor*)GetDbList().GetDataCursor();

	lst.Add( id );

	wxString val;
	val.Printf( "v-%d", rand() % 1000 );

	lst.Add( val );

	bool ok =
		cursor->GetConnection()->ExecuteDirectSQL( "update md_recs set is_local='haha' where id = " + id );

	if ( ok )

		GetDbList().OnRowChanged( wxDbListCtrl::MODIFIED, id, 0, lst, TRUE );
}

wxDbListCtrl& MyFrame1::GetDbList()
{
	if ( mpDbList == NULL )

		mpDbList = (wxDbListCtrl*)FindWindow( XMLID("dblist1") );

	return *mpDbList;
}