⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mytabctrl.cpp

📁 离线的RSS阅读器
💻 CPP
字号:
// MyTabCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "DiamondReader.h"
#include "MyTabCtrl.h"
#include "helper.h"
#include "db.h"
#include "MainFrm.h"
#include "ItemListView.h"
#include "ModifyCategoryDlg.h"
#include "NewCategory.h"
//#include "MyTree.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMyTabCtrl

extern char* gszFile;

CMyTabCtrl::CMyTabCtrl()
{
	
	
}

CMyTabCtrl::~CMyTabCtrl()
{
}


BEGIN_MESSAGE_MAP(CMyTabCtrl, CTabCtrl)
	//{{AFX_MSG_MAP(CMyTabCtrl)
	ON_NOTIFY_REFLECT(TCN_SELCHANGE, OnSelchange)
	ON_WM_PAINT()
	ON_WM_SIZE()
	ON_WM_DRAWITEM()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyTabCtrl message handlers


void CMyTabCtrl::OnSelchange(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	if (GetCurSel()==0)
	{
		m_channelList.ShowWindow(SW_SHOW);
		m_collectionList.ShowWindow(SW_HIDE);
	}
	else if(GetCurSel()==1){
		m_channelList.ShowWindow(SW_HIDE);
		m_collectionList.ShowWindow(SW_SHOW);
	}
	
	*pResult = 0;
}

void CMyTabCtrl::initChildren(){
	CRect rect;
	this->GetClientRect(&rect);
	rect.top = rect.top+24;
	//rect.bottom = 
	m_collectionList.Create(WS_VISIBLE | WS_TABSTOP | WS_CHILD | WS_BORDER| TVS_HASBUTTONS | TVS_LINESATROOT | TVS_HASLINES,rect,this,ID_COLLECTIONLISTTREE);
	m_channelList.Create(WS_VISIBLE | WS_TABSTOP | WS_CHILD | WS_BORDER| TVS_HASBUTTONS | TVS_LINESATROOT | TVS_HASLINES,rect,this,ID_CHANNELLISTTREE);
	m_channelList.ShowWindow(SW_SHOW);
	m_collectionList.ShowWindow(SW_HIDE);
	
}

void CMyTabCtrl::OnSize(UINT nType, int cx, int cy){
	CRect rect;
	this->GetClientRect(&rect);
	rect.top = rect.top+24;
	if (m_channelList.m_hWnd)
	{
		m_channelList.MoveWindow(&rect);
	}
	if (m_collectionList)
	{
		m_collectionList.MoveWindow(&rect);
	}
	if (GetCurSel()==0)
	{
		m_channelList.ShowWindow(SW_SHOW);
		m_collectionList.ShowWindow(SW_HIDE);
	}
	else if(GetCurSel()==1){
		m_channelList.ShowWindow(SW_HIDE);
		m_collectionList.ShowWindow(SW_SHOW);
	}
}

BOOL CMyTabCtrl::deleteCategory(BOOL notify){
	HTREEITEM h = m_channelList.GetSelectedItem();
	MyTreeNode* data = (MyTreeNode*)m_channelList.GetItemData(h);
	//此动作不会发生
	if (data->type!=0)
	{
		return false;
	}
	if (notify)
	{
		DirNode *node = (DirNode*)data;
		//下面有子节点
		if (node->firstChild!=NULL)
		{
			return false;
		}
	}
	HTREEITEM hparent = m_channelList.GetParentItem(h);
	if(hparent==NULL){
		MyTreeNode *roots = (MyTreeNode*)m_channelList.GetItemData(m_channelList.GetRootItem());
		if (data==roots)
		{
			char sql[256];
			if (roots->nextSubling)
			{
				HTREEITEM hChild = m_channelList.GetChildItem(h);
				while (hChild!=NULL)
				{
					HTREEITEM temp = m_channelList.GetNextSiblingItem(hChild);
					m_channelList.Select(hChild,TVGN_CARET);
					deleteCategory(false);
					hChild = temp ;
				}

				sqlite3_exec(db,"delete from dirList where id=0",NULL,NULL,NULL);
				
				m_channelList.DeleteItem(h);
				sprintf(sql,"update DirList set id = 0 where id=%d",roots->nextSubling->id);
				sqlite3_exec(db,sql,NULL,NULL,NULL);
				//MyTreeNode *data = (MyTreeNode*)m_channelList.GetItemData(h);
				delete data;
				m_channelList.DeleteItem(h);
				m_channelList.Invalidate();
				return true;
			}
			else{
				sqlite3_exec(db,"delete from Dirlist where type = 0",NULL,NULL,NULL);
				sqlite3_exec(db,sql,NULL,NULL,NULL);
				sqlite3_exec(db,"delete from ChannelList",NULL,NULL,NULL);
				sqlite3_exec(db,sql,NULL,NULL,NULL);
				sqlite3_exec(db,"delete from ItemList",NULL,NULL,NULL);
				sqlite3_exec(db,sql,NULL,NULL,NULL);
				MyTreeNode *data = (MyTreeNode*)m_channelList.GetItemData(h);
				
				deleteTreeNode(data);
				
				//delete data;

				m_channelList.DeleteAllItems();
			}
		}
		else{
			while (roots->nextSubling!=data)
			{
				roots = roots->nextSubling;
			}
			roots->nextSubling = data->nextSubling;

			HTREEITEM hChild = m_channelList.GetChildItem(h);
			while (hChild!=NULL)
			{
				m_channelList.Select(hChild,TVGN_CARET);
				HTREEITEM temp = m_channelList.GetNextSiblingItem(hChild);
				deleteCategory(false);
				hChild = temp;
			}
			
			char sql[255];
			int nextSublingID = -1;
			if (data->nextSubling)
			{
				nextSublingID = data->nextSubling->id;
			}

			
			sprintf(sql,"update DirList set nextSublingID=%d where id = %d",nextSublingID,roots->id);
			sqlite3_exec(db,sql,NULL,NULL,NULL);
			sprintf(sql,"delete from DirList where ID = %d",data->id);
			sqlite3_exec(db,sql,NULL,NULL,NULL);

			delete data;
			data = NULL;

			m_channelList.DeleteItem(h);
			m_channelList.Invalidate();
		}
		return true;
	}
	else{
		DirNode *parentData = (DirNode*)m_channelList.GetItemData(hparent);
		deleteCategoryData(parentData,data);
		if(data==parentData->firstChild){
			parentData->firstChild = data->nextSubling;
			delete data;
		}
		else{
			MyTreeNode *child = parentData->firstChild;
			while (child->nextSubling!=data)
			{
				child = child->nextSubling;
			}
			child->nextSubling = data->nextSubling;

			deleteTreeNode(data);
		}
	}
	
	m_channelList.DeleteItem(h);
	m_channelList.Invalidate();
	return true;
	//执行相应的数据操作
}

BOOL CMyTabCtrl::addCategory(char *newname){
	HTREEITEM h = m_channelList.GetSelectedItem();
	if (h==NULL)
	{
		//h = m_channelList.GetRootItem();
		MyTreeNode *roots = (MyTreeNode*)m_channelList.GetItemData(m_channelList.GetRootItem());
		while (roots->nextSubling)
		{
			roots = roots->nextSubling;
		}
		DirNode *newDir = new DirNode(newname);
		roots->nextSubling = newDir;
		HTREEITEM hroot = m_channelList.InsertItem(newname,TVI_ROOT,TVI_LAST);
		m_channelList.SetItemData(hroot,(DWORD)newDir);
		m_channelList.SetItemImage(hroot,0,0);

		char sql[255];
		sprintf(sql,"update dirList set nextSublingID = %d where id = %d",newDir->id,roots->id);
		sqlite3_exec(db,sql,NULL,NULL,NULL);
		sprintf(sql,"insert into DirList(type,firstChildID,ID,name,nextSublingID) values(%d,%d,%d,'%s',%d)",0,-1,newDir->id,newDir->name,-1);
		sqlite3_exec(db,sql,NULL,NULL,NULL);
		return true;

	}
	MyTreeNode *m = (MyTreeNode*)m_channelList.GetItemData(h);
	if (m->type==1)
	{
		return false;
	}
	DirNode *dir = (DirNode*)m;
	MyTreeNode *child = dir->firstChild;
	while (child!=NULL)
	{
		if (!strcmp(child->name,newname)&&child->type==0)
		{
			AfxMessageBox("分类已存在");
			return false;
		}
		child = child->nextSubling;
	}
	DirNode *newdir = new DirNode(newname);
	MyTreeNode *p = dir->firstChild;
	dir->firstChild = newdir;
	newdir->nextSubling = p;
	h = m_channelList.InsertItem(newname,h);
	m_channelList.SetItemImage(h,0,0);
	m_channelList.SetItemData(h,(DWORD)newdir);
	m_channelList.Invalidate();
	DBInsertItem(dir,newdir,db);

	return true;
}

BOOL CMyTabCtrl::addChannel(Channel *channel){
	HTREEITEM h = m_channelList.GetSelectedItem();
	if (h==NULL)
	{
		h = m_channelList.GetRootItem();
	}
	MyTreeNode *m = (MyTreeNode*)m_channelList.GetItemData(h);
	if (m->type==1)
	{
		return false;
	}
	DirNode *dir = (DirNode*)m;
	ChannelNode *newNode = new ChannelNode(channel->title,channel->source,channel->pubTime);
	MyTreeNode *p = dir->firstChild;
	dir->firstChild = newNode;
	newNode->nextSubling = p;
	h = m_channelList.InsertItem(channel->title,h);
	m_channelList.SetItemImage(h,1,1);
	m_channelList.SetItemData(h,(DWORD)newNode);
	m_channelList.Invalidate();
	DBInsertItem(dir,newNode,db);
	DBUpdateChannel(newNode->id,channel->items,db);
	return true;
}

int CMyTabCtrl::getSelId(){
	int i = this->GetCurSel();
	if (i==0)
	{
		HTREEITEM h = m_channelList.GetSelectedItem();
		MyTreeNode *data = (MyTreeNode*)m_channelList.GetItemData(h);
		return data->id;
	}
	else if (i==1)
	{
		HTREEITEM h = m_collectionList.GetSelectedItem();
		MyTreeNode *data = (MyTreeNode*)m_collectionList.GetItemData(h);
		return data->id;
	}
	return -1;
}

MyTreeNode *CMyTabCtrl::getSelData(){
	int i = this->GetCurSel();
	if (i==0)
	{
		HTREEITEM h = m_channelList.GetSelectedItem();
		if (h==0)
		{
			return NULL;
		}
		MyTreeNode *data = (MyTreeNode*)m_channelList.GetItemData(h);
		return data;
	}
	else if (i==1)
	{
		HTREEITEM h = m_collectionList.GetSelectedItem();
		if (h==0)
		{
			return NULL;
		}
		MyTreeNode *data = (MyTreeNode*)m_collectionList.GetItemData(h);
		return data;
	}
	return NULL;
}

BOOL CMyTabCtrl::deleteChannel(){
	HTREEITEM h = m_channelList.GetSelectedItem();
	MyTreeNode *data = (MyTreeNode*)m_channelList.GetItemData(h);
	HTREEITEM parent = m_channelList.GetParentItem(h);
	DirNode *parentData = (DirNode*)m_channelList.GetItemData(parent);
	MyTreeNode *child = parentData->firstChild;
	DBDeleteItem(parentData,data,db);
	if(child==data){
		parentData->firstChild = data->nextSubling;
		delete data;
	}
	else{
		while (child->nextSubling!=data)
		{
			child = child->nextSubling;
		}
		child->nextSubling = data->nextSubling;
		delete data;
	}
	m_channelList.DeleteItem(h);
	//DBDeleteItem(parentData,data,db);
	return true;
}

void CMyTabCtrl::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	LOGFONT lf;
	memset(&lf,0,sizeof(LOGFONT));
	lf.lfHeight = 13;
	strcpy(lf.lfFaceName,"宋体");
	CFont cf;
	cf.CreateFontIndirect(&lf);

	this->SetFont(&cf);
	CTabCtrl::OnPaint();
	// Do not call CTabCtrl::OnPaint() for painting messages
}


void CMyTabCtrl::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	// TODO: Add your message handler code here and/or call default
	
	  

	//CTabCtrl::OnDrawItem(nIDCtl, lpDrawItemStruct);
}

BOOL CMyTabCtrl::PreCreateWindow(CREATESTRUCT& cs) 
{
	// TODO: Add your specialized code here and/or call the base class
	//cs.style = cs.style & TCS_OWNERDRAWFIXED;
	return CTabCtrl::PreCreateWindow(cs);
}

void CMyTabCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct){
	   TC_ITEM tci;
     CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
     CRect rect(lpDrawItemStruct->rcItem);
     char szTabText[256];
     memset(szTabText,'\0',sizeof(szTabText));
     
	CBrush *hbr = new CBrush(RGB(222,222,222));
	CRect rc = lpDrawItemStruct->rcItem;
	rc.top-= 2;
	rc.bottom +=2;
	 pDC->FillRect(&rc,hbr);
	 delete hbr;

     tci.mask = TCIF_TEXT;
     tci.pszText = szTabText;
     tci.cchTextMax = sizeof(szTabText) -1;
     GetItem(lpDrawItemStruct->itemID, &tci);
     pDC->SetBkMode(TRANSPARENT);
     if (lpDrawItemStruct->itemAction & ODA_DRAWENTIRE)
     {
         pDC->TextOut(rect.left+5, rect.top+5, tci.pszText);
     }
     
     if ((lpDrawItemStruct->itemState & ODS_SELECTED) && 
         (lpDrawItemStruct->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
     {
         //Make the color of text of the selected tab to be BLUE.
         pDC->SetTextColor(RGB(0,0,255));
         pDC->TextOut(rect.left+5, rect.top+5, tci.pszText);
     }
	 
}

void CMyTabCtrl::modifyCategory(){
	HTREEITEM h = m_channelList.GetSelectedItem();
	char c;
	char *pc = &c;
	CModifyCategoryDlg *cmgd = new CModifyCategoryDlg((LPTSTR)(LPCTSTR)m_channelList.GetItemText(h),&pc);
	if (!cmgd->DoModal())
	{
		DirNode *data = (DirNode*)m_channelList.GetItemData(h);
		
		HTREEITEM hparert = m_channelList.GetParentItem(h);
		if (hparert==NULL)
		{
			MyTreeNode *roots = (DirNode *)m_channelList.GetItemData(m_channelList.GetRootItem());
			while (roots)
			{
				if (!strcmp(pc,roots->name))
				{
					AfxMessageBox("分类名已经存在");
					return;
				}
				roots = roots->nextSubling;
			}
		}
		else{
			DirNode * parentData = (DirNode*)m_channelList.GetItemData(hparert);
			MyTreeNode *child = parentData->firstChild;
			while (child)
			{
				if (!strcmp(pc,child->name))
				{
					AfxMessageBox("分类名已经存在");
					return;
				}
				child = child->nextSubling;
			}
		}

		delete data->name;
		data->name = pc;
		m_channelList.SetItemText(h,pc);
		m_channelList.Invalidate();
		char sql[255];
		sprintf(sql,"update DirList set name='%s' where id=%d",pc,data->id);
		sqlite3_exec(db,sql,NULL,NULL,NULL);
	}
}

void CMyTabCtrl::newCategoryRoot(){

	char c ;
	char *name = &c;

	CNewCategory cnc(&name,NULL);
	while (cnc.DoModal()!=1)
	{
		return;
	}

	sqlite3 *db;
	sqlite3_open(gszFile,&db);
	char sql[1024];
	HTREEITEM h = m_channelList.GetRootItem();
	if (h!=NULL)
	{
		MyTreeNode *root = (MyTreeNode*)m_channelList.GetItemData(h);
		MyTreeNode *temp = root;
		while (temp)
		{
			if(!strcmp(temp->name,name)){
				AfxMessageBox("分类已存在");
				return;
			}
			temp = temp->nextSubling;
		}

		DirNode *dir = new DirNode(name,MyTreeNode::nextID++);
		
		while (root->nextSubling)
		{
			root = root->nextSubling;
		}
		root->nextSubling = dir;

		HTREEITEM h = m_channelList.InsertItem(name,TVI_ROOT,TVI_LAST);
		m_channelList.SetItemData(h,(DWORD)dir);
		m_channelList.SetItemImage(h,0,0);
		
		
		sprintf(sql,"insert into DirList(type,firstChildID,ID,name,nextSublingID) values(%d,%d,%d,'%s',%d)",0,-1,dir->id,dir->name,-1);
		sqlite3_exec(db,sql,NULL,NULL,NULL);
		sprintf(sql,"update dirList set nextsublingid=%d where id=%d",dir->id,root->id);
		sqlite3_exec(db,sql,NULL,NULL,NULL);
		
	}

	else{
		DirNode *data = new DirNode(name,0);

		HTREEITEM h = m_channelList.InsertItem(data->name,TVI_ROOT);
		m_channelList.SetItemData(h,(DWORD)data);
		m_channelList.SetItemImage(h,0,0);
		sprintf(sql,"insert into DirList(type,firstChildID,ID,name,nextSublingID) values(%d,%d,%d,'%s',%d)",0,-1,0,name,-1);
		sqlite3_exec(db,sql,NULL,NULL,NULL);	
	}

	sqlite3_close(db);
	delete[] name;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -