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

📄 alieditorview.cpp

📁 AliEditor_Source code
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// AliEditorView.cpp : implementation of the CAliEditorView class
//

#include "stdafx.h"
#include "AliEditor.h"

#include "AliEditorDoc.h"
#include "AliEditorView.h"
#include "FileDlg.h"
#include "MainFrm.h"
#include "ProgInfoEdit.h"
#include "Navigator.h"
#include "MovePos.h"
#include "SwitchView.h"
#include "DlgFavoriteEdit.h"
#include "DlgGroupRename.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAliEditorView

IMPLEMENT_DYNCREATE(CAliEditorView, CListView)

BEGIN_MESSAGE_MAP(CAliEditorView, CListView)
	//{{AFX_MSG_MAP(CAliEditorView)
	ON_COMMAND(IDM_EDIT_CHANNEL, OnEditChannel)
	ON_COMMAND(IDM_MOVE_CHANNEL, OnMoveChannel)
	ON_COMMAND(IDM_SWITCH_CHANNEL, OnSwitchChannel)
	ON_COMMAND(IDM_INSERT_CHANNEL, OnInsertChannel)
	ON_COMMAND(IDM_DEL_CHANNEL, OnDelChannel)
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(IDM_MOVE_UP_CHANNEL, OnMoveUpChannel)
	ON_COMMAND(IDM_MOVE_DOWN_CHANNEL, OnMoveDownChannel)
	ON_WM_KEYDOWN()
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_COMMAND(IDM_EIDT_FAV_GROUP, OnEidtFavGroup)
	ON_COMMAND(IDM_REMOVEITEM_FAV_GROUP, OnRemoveitemFavGroup)
	ON_COMMAND(IDM_RENAME_FAV_GROUP, OnRenameFavGroup)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CListView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

//cook_node_t cook_favor[] =
//{
//	{"0",		"None"		},
//	{"1",		"Sport"		},
//	{"2",		"News"		},
//	{"4",		"Music"		},
//	{"8",		"Movie"		},
//	{"16",		"Shopping"	},
//	{"32",		"Education"	},
//	{"64",		"Leisure"	},
//};
//cook_node_t cook_favor[33];

// CAliEditorView construction/destruction

CAliEditorView::CAliEditorView()
{
	// TODO: add construction code here

}

CAliEditorView::~CAliEditorView()
{
}

BOOL CAliEditorView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CListView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CAliEditorView drawing

void CAliEditorView::OnDraw(CDC* pDC)
{
	CAliEditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

void CAliEditorView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();


	// TODO: You may populate your ListView with items by directly accessing
	//  its list control through a call to GetListCtrl().
	CListCtrl& m_list = GetListCtrl();
	m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

	m_av_type = 2;
	m_pCurSat = NULL;

	InitProgField();
}

/////////////////////////////////////////////////////////////////////////////
// CAliEditorView printing

BOOL CAliEditorView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CAliEditorView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CAliEditorView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CAliEditorView diagnostics

#ifdef _DEBUG
void CAliEditorView::AssertValid() const
{
	CListView::AssertValid();
}

void CAliEditorView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}

CAliEditorDoc* CAliEditorView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAliEditorDoc)));
	return (CAliEditorDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CAliEditorView message handlers

BOOL CAliEditorView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	// TODO: Add your specialized code here and/or call the base class
	dwStyle = dwStyle | LVS_REPORT | LVS_SHOWSELALWAYS;
	return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}

void CAliEditorView::InitProgField()
{
	char bufsz[1024];
	DWORD len;

	len = GetModuleFileName(NULL, bufsz, sizeof(bufsz));
	for(int i = len -1; i >= 0; i--)
	{
		if(bufsz[i] == '\\' || bufsz[i] == '/')
		{
			bufsz[i + 1] = 0;
			break;
		}
	}

	CString script_path(bufsz);

	script_path = script_path + "script.ini";

	if(InitScript(script_path))
	{
		UpdateMainInfoByFile(PROG_NODE);
	}
	else
	{
		if(AfxMessageBox("Load Template ?", MB_YESNO) == IDYES)
		{
			LoadTemplate(TRUE);			
		}
		else
		{
			unlink(script_path);
		}
	}
}

//程序初始化时必须读取卫星,TP和节目的信息
BOOL CAliEditorView::InitScript(CString pathname)
{
	if(pathname.IsEmpty())
	{
		AfxMessageBox("script path is empty");
		ASSERT(0);
	}
	FILE *fd = fopen(pathname, "rb");
	if(!fd)
	{
		AfxMessageBox('\"' + pathname + '\"' + " not exist");

		return FALSE;
	}

	ReadScriptFile(pathname);

	fclose(fd);

	return TRUE;
}

BOOL CAliEditorView::ReadScriptFile(CString script_path)
{
	// TODO: Add your command handler code here
	FILE *fd = fopen(script_path, "rb");
	if(!fd) return FALSE;


	m_strlines.FreeNodeFieldInfo(TP_NODE);
	m_strlines.FreeNodeFieldInfo(SAT_NODE);
	m_strlines.FreeNodeFieldInfo(PROG_NODE);

	m_strlines.ReadNodeFieldFromScript(fd);
	
	fclose(fd);

	return TRUE;
}

void CAliEditorView::UpdateMainInfoByFile(enum NodeType type)
{
	m_cur_node_type = type;
	
	NodeField **pNode = m_strlines.GetNodeFieldInfo(type);

	if(!pNode) return;

	UpdateListByNodeInfo(pNode);
}

void CAliEditorView::UpdateListByNodeInfo(NodeField**pNode)
{
	int i = 0;

	CListCtrl& m_list = GetListCtrl();

	DeleteAllProg();

	LVCOLUMN col;

	col.mask = LVCF_WIDTH;

	// Double the column width of the first column.
	while(m_list.GetColumn(0, &col))
	{
		m_list.DeleteColumn(0);
	}

	m_list.InsertColumn(0, "Index", LVCFMT_LEFT, 44);

	while(pNode[i])
	{
		if(!pNode[i]->field_mask && (pNode[i]->field_pos != -1))
		{
			if(!strcmp(pNode[i]->name, PKEYZ_SERVICE_NAME))
			{
				m_list.InsertColumn(pNode[i]->field_pos, pNode[i]->name, LVCFMT_LEFT, 114);
			}
			else if(!strcmp(pNode[i]->name, PKEYZ_TV_RADIO))
			{
				m_list.InsertColumn(pNode[i]->field_pos, pNode[i]->name, LVCFMT_LEFT, 85);
			}
			else if(!strcmp(pNode[i]->name, PKEYZ_AUDIO_PID1))
			{
				m_list.InsertColumn(pNode[i]->field_pos, pNode[i]->name, LVCFMT_LEFT, 73);
			}
			else if(!strcmp(pNode[i]->name, PKEYZ_AUDIO_PID2))
			{
				m_list.InsertColumn(pNode[i]->field_pos, pNode[i]->name, LVCFMT_LEFT, 73);
			}
			else if(!strcmp(pNode[i]->name, PKEYZ_AUDIO_PID3))
			{
				m_list.InsertColumn(pNode[i]->field_pos, pNode[i]->name, LVCFMT_LEFT, 73);
			}
			else if(!strcmp(pNode[i]->name, PKEYZ_PCR_PID))
			{
				m_list.InsertColumn(pNode[i]->field_pos, pNode[i]->name, LVCFMT_LEFT, 58);
			}
			else if(!strcmp(pNode[i]->name, PKEYZ_FAV_GROUP))
			{
				m_list.InsertColumn(pNode[i]->field_pos, pNode[i]->name, LVCFMT_LEFT, 90);
			}
			else if(!strcmp(pNode[i]->name, PKEYZ_SCRAMBLE))
			{
				m_list.InsertColumn(pNode[i]->field_pos, pNode[i]->name, LVCFMT_LEFT, 60);
			}
			else if(!strcmp(pNode[i]->name, PKEYZ_PRESET_FLAG))
			{
				m_list.InsertColumn(pNode[i]->field_pos, pNode[i]->name, LVCFMT_LEFT, 60);
			}
			else if(!strcmp(pNode[i]->name, PKEYZ_PROG_NUMBER))
			{
				m_list.InsertColumn(pNode[i]->field_pos, pNode[i]->name, LVCFMT_LEFT, 76);
			}
			else 
			{
				m_list.InsertColumn(pNode[i]->field_pos, pNode[i]->name, LVCFMT_LEFT, 70);
			}
		}
		
		i++;
	};
}

BOOL CAliEditorView::LoadTemplate(BOOL bSaveTemplate)
{
	char bufsz[1024];
	DWORD len;

	len = GetModuleFileName(NULL, bufsz, sizeof(bufsz));
	for(int i = len -1; i >= 0; i--)
	{
		if(bufsz[i] == '\\' || bufsz[i] == '/')
		{
			bufsz[i + 1] = 0;
			break;
		}
	}

	CString script_path(bufsz);

	script_path = script_path + "script.ini";

	CFileDlg fd(TRUE, NULL, "", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "Template File(*.h)");
	if(fd.DoModal() == IDOK)
	{
		BOOL bRet = ParseTemplate(fd.GetPathName());
		if(!bRet)
		{
			AfxMessageBox("ParseTemplate failed!");

			return FALSE;
		}

		UpdateMainInfoByFile(PROG_NODE);

		if(bSaveTemplate) SaveScriptFile(script_path);

		return TRUE;
	}
	else
		return FALSE;
}

BOOL CAliEditorView::ParseTemplate(CString tpath)
{
	if(tpath.IsEmpty()) return FALSE;
	
	CString filestr = ClearFileComment(tpath);
//	/*
	CString pathname("parse.input");
	FILE *pfd = fopen((LPCSTR)pathname, "wb");
	ASSERT(pfd);

	fwrite((LPCSTR)filestr, 1, filestr.GetLength(), pfd);

	fclose(pfd);
//*/
	int count = m_strlines.GetNodeFieldCount("/*#tp_node@*/", (LPCSTR)filestr);
	if(count <= 0) return FALSE;

	count = m_strlines.GetNodeFieldCount("/*#sat_node@*/", (LPCSTR)filestr);
	if(count <= 0) return FALSE;

	count = m_strlines.GetNodeFieldCount("/*#prog_node@*/", (LPCSTR)filestr);
	if(count <= 0) return FALSE;

	count = m_strlines.GetNodeFieldCount("/*#def_prog_node@*/", (LPCSTR)filestr);
//	if(count <= 0) return FALSE;

	NodeField **pNode = m_strlines.ParseNodeField("/*#tp_node@*/", (LPCSTR)filestr);
	if(!pNode) return FALSE;

	pNode = m_strlines.ParseNodeField("/*#sat_node@*/", (LPCSTR)filestr);
	if(!pNode) return FALSE;

	pNode = m_strlines.ParseNodeField("/*#prog_node@*/", (LPCSTR)filestr);
	if(!pNode) return FALSE;

	pNode = m_strlines.ParseNodeField("/*#def_prog_node@*/", (LPCSTR)filestr);
//	if(!pNode) return FALSE;

	return TRUE;
}

BOOL CAliEditorView::SaveScriptFile(CString script_path)
{
	// TODO: Add your command handler code here
	FILE *fd = fopen(script_path, "wb");
	if(!fd) return FALSE;

	BOOL bRet =	m_strlines.WriteNodeFieldToScript(fd);
	if(!bRet) AfxMessageBox("Template doesn't exist!");
	else AfxMessageBox("Save template ok.");
	
	fclose(fd);

	return TRUE;
}

CString CAliEditorView::ClearFileComment(CString filepath)
{
	CString linestr;
	CString filestr;

	filestr = "";
	CStdioFile stdfd(filepath, CFile::modeRead);
	while(stdfd.ReadString(linestr))
	{
		filestr = filestr + m_strlines.FilterLineComment(linestr) + '\n';
	};

	if(filestr.IsEmpty()) return filestr;

	filestr = m_strlines.FilterFileComment(filestr);

	return filestr;
}

long CAliEditorView::GetDataValue(const char *bufsz)
{
	if(!bufsz) return 0;

	CString str(bufsz);

	long lval = m_strlines.GetDigitValue(str);

	return lval;
}
int CAliEditorView::GetCurSel()
{
	CListCtrl &m_list = GetListCtrl();

	POSITION pos = m_list.GetFirstSelectedItemPosition();
	if (pos == NULL) return -1;

	int nItem = m_list.GetNextSelectedItem(pos);
	
	return nItem;
}

void CAliEditorView::SetCurSel(int index, BOOL bClearFoucs)
{

	LVITEM lvm;
	lvm.mask = LVIF_STATE;
	lvm.iItem = index;
	lvm.stateMask = -1;
	lvm.state =  LVIS_SELECTED | LVIS_FOCUSED;

	lvm.iSubItem = 0;

	int old = GetCurSel();
	
	CListCtrl &m_list = GetListCtrl();

⌨️ 快捷键说明

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