Click here to Skip to main content
15,891,253 members
Articles / Programming Languages / C

BasicAdmin - Personal Organizer

Rate me:
Please Sign up or sign in to vote.
4.94/5 (14 votes)
1 Aug 2009CPOL6 min read 50.9K   5.6K   60  
Finance, contacts, notes organizer
// FrmNotes.cpp : implementation file
//

#include "stdafx.h"
#include "../../BasicAdmin.h"
#include "../../Macros.h"
#include "FrmNotes.h"
#include "../../UtilsOp/DbOpers.h"
#include "../Modals/Others/FrmNotesData.h"
#include "FrmContactsTree.h"
#include "../Modals/Utils/FrmCatSearch.h"

// CFrmNotes

IMPLEMENT_DYNCREATE(CFrmNotes, CFormView)

CFrmNotes::CFrmNotes()
	: CFormView(CFrmNotes::IDD)
{
	ut.sizechange = 1;
	AddModify = FALSE;
}

CFrmNotes::~CFrmNotes()
{
}
void CFrmNotes::OnInit()
{
	ut.FormatGrid(&m_grid);
	ut.AccLoadArrayWithParent("CONTACTSCAT", "DESCRIPTION");
	
	uttree.pParent = (CTreeParams*)&notecat;
	uttree.type = TCatTypeNotesFiles;
	uttree.Table = "CONTACTSCAT";
	uttree.LoadTree(CboCat.GetTree());
	uttree.LoadTreeKeys(&CboCat);

	GridColumns();
	LoadGridData();
	ut.FormatButtons(&BtnAdd, &BtnModify, &BtnDelete, &BtnPrint);
	
	ut.PonerColorBoton(&BtnAcc);
	BtnAcc.SetIcon(IDI_SEARCH, (int)BTNST_AUTO_GRAY);
	BtnAcc.SetFlat(FALSE);
	RefreshButtons();
}
void CFrmNotes::LoadGridData()
{
	m_grid.SetRedraw(FALSE);
	CppSQLite3Query q = dbAdmin.execQuery("SELECT * FROM NOTES");
	int irow = 0;

	while (!q.eof())
	{
		irow ++;
		m_grid.SetRowCount(irow + 1);
		RefreshRow(irow, q.getIntField("ID"), q.getStringField("NAME"), q.getIntField("CATEGORY"), q.getStringField("OBSERVATIONS"), FALSE);
		ut.PintarFila(&m_grid, irow);
		q.nextRow();
	}
	ut.visiblerecords = irow;
	ut.RefreshMainRows();
	m_grid.SetRedraw(TRUE,TRUE);
}
void CFrmNotes::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_GRID, m_grid);
	DDX_Control(pDX, IDC_BTN_ADD, BtnAdd);
	DDX_Control(pDX, IDC_BTN_MODIFY, BtnModify);
	DDX_Control(pDX, IDC_BTN_DELETE, BtnDelete);
	DDX_Control(pDX, IDC_BTN_PRINT, BtnPrint);
	DDX_Control(pDX, IDC_ED_SEARCH, EdtSearch);
	DDX_Control(pDX, IDC_CBO_CAT, CboCat);
	DDX_Control(pDX, IDC_BTN_ACC_SEARCH, BtnAcc);
}

BEGIN_MESSAGE_MAP(CFrmNotes, CFormView)
	ON_WM_SIZE()
	ON_BN_CLICKED(IDC_BTN_ADD, OnBnClickedBtnAdd)
	ON_BN_CLICKED(IDC_BTN_MODIFY, OnBnClickedBtnModify)
	ON_BN_CLICKED(IDC_BTN_DELETE, OnBnClickedBtnDelete)
	ON_NOTIFY(NM_DBLCLK, IDC_GRID, OnGridDblClick)
	ON_EN_CHANGE(IDC_ED_SEARCH, Filter)
	ON_BN_CLICKED(IDC_BTN_PRINT, OnBnClickedBtnPrint)
	ON_COMMAND(CHANGETREEDATA, Filter)
	ON_WM_CTLCOLOR()
	ON_WM_ERASEBKGND()
	ON_COMMAND(CHANGECOLOR, CambiarColores)
	ON_BN_CLICKED(IDC_BTN_ACC_SEARCH,OnBnClickedAccSearch)
END_MESSAGE_MAP()

void CFrmNotes::GridColumns()
{
	m_grid.SetColumnCount(4);
	m_grid.GetCell(0,0)->SetText(GetStringFromRes(IDCOLCASH_ID));
	m_grid.GetCell(0,1)->SetText(GetStringFromRes(IDCOLCON_NAME));
	m_grid.GetCell(0,2)->SetText(GetStringFromRes(IDCOLCON_CAT));
	m_grid.GetCell(0,3)->SetText(GetStringFromRes(IDCOLCASH_OBS));

	m_grid.SetColumnWidth(0, 0);
	m_grid.SetColumnWidth(1, 50);
	m_grid.SetColumnWidth(2, 60);
	m_grid.SetColumnWidth(3, 100);
}
void CFrmNotes::RefreshRow(int irow, int ID, CString strName, int IdCat, CString strObs, BOOL refresh)
{
	CString strid;
	strid.Format("%d", ID);
	m_grid.GetCell(irow, 0)->SetText(strid);

	m_grid.GetCell(irow, 1)->SetText(strName);
	m_grid.GetCell(irow, 2)->SetText(ut.AccGetArrDescription(IdCat,0));
	m_grid.GetCell(irow, 3)->SetText(strObs);

	if (refresh)
		m_grid.Invalidate();
}
void CFrmNotes::OnSize(UINT nType, int cx, int cy)
{
	int WGridBefore, WGridNow;
	CFormView::OnSize(nType, cx, cy);
	if (cx <= 1 || cy <= 1 ) 
        return;
	
	if (!IsWindow(m_grid.m_hWnd)) return;

	WGridBefore = ut.GetCtrlWidth(&m_grid, this);

	ut.OnSizeControls(this,&m_grid, &BtnAdd, &BtnModify, &BtnDelete, &BtnPrint, cx, cy);

	WGridNow = ut.GetCtrlWidth(&m_grid, this);
	
	ut.ResizeGrid(&m_grid, WGridNow, WGridBefore);

	CRect rsearch, rbutton, rgrid, rprint;

	EdtSearch.GetWindowRect(&rsearch);
	BtnAdd.GetWindowRect(&rbutton);
	m_grid.GetWindowRect(&rgrid);
	BtnPrint.GetWindowRect(&rprint);

	ScreenToClient(&rsearch);
	ScreenToClient(&rbutton);
	ScreenToClient(&rgrid);
	ScreenToClient(&rprint);

	EdtSearch.MoveWindow(rsearch.left, rsearch.top, cx - rsearch.left - rbutton.Width() - (rgrid.left * 2), rsearch.Height());
}
void CFrmNotes::OnBnClickedBtnAdd()
{
	AddModify = TRUE;
	CFrmNotesData dlg;
	dlg.ID = 0;
	dlg.frm = this;

	dlg.DoModal();
	AddModify = FALSE;
}
void CFrmNotes::OnGridDblClick(NMHDR *pNotifyStruct, LRESULT* /*pResult*/)
{
	NM_GRIDVIEW* pItem = (NM_GRIDVIEW*) pNotifyStruct;
	if (pItem->iRow < 0 || pItem->iColumn < 0) return;

	if (pItem->iRow>=1)
		OnBnClickedBtnModify();
}
void CFrmNotes::OnBnClickedBtnModify()
{
	AddModify = TRUE;
	int irow = m_grid.GetSelectedCellRange().GetMinRow();
	CFrmNotesData dlg;
	dlg.frm = this;
	dlg.ID = atoi(m_grid.GetCell(irow, 0)->GetText());
	dlg.DoModal();
	AddModify = FALSE;
}
void CFrmNotes::OnBnClickedBtnDelete()
{
	int irow = m_grid.GetSelectedCellRange().GetMinRow();
	if (irow<=0 || irow > m_grid.GetRowCount() - 1) return;

	if (MostrarMensaje(GetStringFromRes(IDDEL_NOTE),GetStringFromRes(IDDEL_CONFIRM), TRUE) == 2) return;

	int ID = atoi(m_grid.GetCell(irow, 0)->GetText());
	CdbOpers dbop;
	dbop.DeleteNoteInfo(ID);
	m_grid.DeleteRow(irow);
	m_grid.Invalidate();
	ut.visiblerecords--;
	ut.RefreshMainRows();
	ut.SelectFirstRow(&m_grid);
	RefreshButtons();
}
void CFrmNotes::RefreshButtons()
{
	BOOL isdata = (ut.visiblerecords >= 1);
	ut.RefreshButtons(&BtnModify, &BtnDelete, &BtnPrint);
}
void CFrmNotes::Filter()
{
	if (AddModify) return;
	strsearch = GetTextValue(&EdtSearch);
	strgroup = GetTreeDesc();

	m_grid.SetRedraw(FALSE);

	for (int i=1;i<= m_grid.GetRowCount() - 1;i++)
		FilterRow(i);

	m_grid.SetRedraw(TRUE, TRUE);
	ut.SelectFirstRow(&m_grid);
	ut.RefreshMainRows();
	RefreshButtons();
}
BOOL CFrmNotes::FilterRow(int irow)
{
	CString gvalue, gvaluecat;
	BOOL showgrid = FALSE;
	gvaluecat = m_grid.GetCell(irow, 2)->GetText();
	if (!IsInCat(strgroup,gvaluecat) && strgroup!="")
		showgrid = FALSE;
	else
	{
		for (int j=1;j<=m_grid.GetColumnCount() - 1;j++)
		{
			gvalue = m_grid.GetCell(irow, j)->GetText();
			if (j!=2)
			{
				if (IsInText(gvalue,strsearch))
				{
					showgrid = TRUE;
					break;
				}
			}
		}
	}
	if (showgrid)
	{
		if (m_grid.GetRowHeight(irow) == 0)
			ut.visiblerecords++;
		m_grid.SetRowHeight(irow, CELLHEIGHT);
	}
	else
	{
		if (m_grid.GetRowHeight(irow) != 0)
			ut.visiblerecords--;
		m_grid.SetRowHeight(irow, 0);
	}
	return showgrid;
}
BOTONPRINT(CFrmNotes,OnBnClickedBtnPrint)

BOOL CFrmNotes::IsInCat(CString strgroup, CString strgrid)
{
	return (strgrid.Left(strgroup.GetLength()) == strgroup);
}

CString CFrmNotes::GetTreeDesc()
{
	if (GetTextValue(&CboCat)=="") return "";
	int id = CboCat.GetSelectedID();
	return ut.AccGetArrDescription(id, 0);
}
HBRUSH CFrmNotes::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);
	int nID = pWnd->GetDlgCtrlID();

	switch (nID)
    {
		case LBLCATNOTES: case LBLSEARCHNOTES:
			pDC->SetBkMode(OPAQUE);
			pDC->SetBkColor(DarColor());
			return (HBRUSH) GetStockObject(NULL_BRUSH);
	}
	return hbr;
}
BOOL CFrmNotes::OnEraseBkgnd(CDC* pDC)
{
	CRect rect;
	GetClientRect(&rect);
	pDC->FillSolidRect(0,0,rect.Width(),rect.Height(),DarColor());
	return TRUE;
}
CAMBIARCOLORES(CFrmNotes)
void CFrmNotes::OnBnClickedAccSearch()
{
	CFrmCatSearch frmcat;
	frmcat.m_treecbo = &CboCat;
	frmcat.cattype =TCatTypeNotesFiles; 
	frmcat.DoModal();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Argentina Argentina
System developer from Argentina.

Programmed in VB 5,6,.NET, C#, Java, PL-SQL, Transac-SQL, C, C++ and even some "calculator" language.

Love to build small, useful applications.
Usually building big and complicated apps based on solid, reliable components.

Hobbies: reading, photography, chess, paddle, running.

Comments and Discussions