//NetTalk
/*------------------------------------------------------------------------------*\
 =============================
   模块名称: MsgTalk.cpp
 =============================
 
 [版权]
 
   2000-2002  115软件工厂  版权所有
                                              
\*------------------------------------------------------------------------------*/
#include "wndx.h"
#include "WndX.h"
#include "MsgTalk.h"
#include "NewMsgBox.h"
#include <Stdio.h>


/*------------------------------------------------------------------------------*/
CMsgTalkDlg::CMsgTalkDlg()
{
}
/*------------------------------------------------------------------------------*/
CMsgTalkDlg::~CMsgTalkDlg()
{

}
/*------------------------------------------------------------------------------*/
LRESULT CMsgTalkDlg::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
	
	case WM_INITDIALOG:
		return OnInitDialog();
		break;
	case WM_COMMAND:
		return OnCommand(wParam,lParam);
	case WM_CLOSE:
		ShowWindow(m_hWnd,SW_HIDE);
		break;
	case WM_SHOWWINDOW:
		if(wParam==TRUE)
		{
			LoadList();
		}
		break;
	
	default:
		return CGraphDlg::WndProc(uMsg,wParam,lParam);
	}
	return TRUE;
}
/*------------------------------------------------------------------------------*/
BOOL CMsgTalkDlg::OnCommand(WPARAM wParam, LPARAM lParam)
{
	CGraphDlg::OnCommand(wParam,lParam);
	switch(LOWORD(wParam))
	{
	case IDC_SEND:
		{
			char sz[500];
			//check if the combo box is empty
			if(GetWindowTextLength(GetDlgItem(m_hWnd,IDC_OBJ))>0)
			{
				char s[64];
				GetDlgItemText(m_hWnd,IDC_OBJ,s,64);
				//if the IP  not exist in the combo box,insert it
				if(SendDlgItemMessage(m_hWnd,IDC_OBJ,CB_FINDSTRING,0,(LPARAM)s)==CB_ERR)
				{
					SendDlgItemMessage(m_hWnd,IDC_OBJ,CB_INSERTSTRING,0,(LPARAM)s);
				}
				//get the Msg content
				if(GetDlgItemText(m_hWnd,IDC_T,sz,256)>0)
				{
					SetDlgItemText(m_hWnd,IDC_T,"");
					SendMessage(GetParent(m_hWnd),WM_COMMAND,IDC_SEND,(LPARAM)sz);
					
				}
			}
			else
			{
				CMsgBox::MessageBox(m_hWnd,IDS_MSG_NOOBJ,IDS_MSG,MB_OK,IDI_INFO);
				SetFocus(GetDlgItem(m_hWnd,IDC_OBJ));
			}
		}
		break;
	case IDC_CLEAR:
		{
			SendDlgItemMessage(m_hWnd,IDC_MSGLIST,LB_RESETCONTENT,0,0);
			SendDlgItemMessage(m_hWnd,IDC_MSGLIST,LB_SETHORIZONTALEXTENT,0,0);
		}
		break;

	case IDC_MSGLIST:
		{
			if(HIWORD(wParam)==LBN_DBLCLK)
			{
				int i=SendMessage((HWND)lParam,LB_GETCURSEL,0,0);
				if(i!=LB_ERR)
				{
					
					char sz[256];
					SendMessage((HWND)lParam,LB_GETTEXT,i,(LPARAM)sz);
					SetDlgItemText(m_hWnd,IDC_T,sz);
					SetFocus(GetDlgItem(m_hWnd,IDC_T));
					
				}
			}
		}break;
	case IDC_SAVE:
		{
			if(SendDlgItemMessage(m_hWnd,IDC_MSGLIST,LB_GETCOUNT,0,0)>0)
			{
				OPENFILENAME ofn;
				ZeroMemory(&ofn,sizeof(ofn));
				ofn.lStructSize=sizeof(ofn);
				ofn.hInstance=GetModuleHandle(0);
				ofn.hwndOwner=m_hWnd;
				char szFilter[100];
				char* p=szFilter;
				strcpy(p,"文本文件(*.txt)");
				p+=strlen(p)+1;
				strcpy(p,"*.txt");
				p+=strlen(p)+1;
				
				strcpy(p,"所有文件(*.*)");
				p+=strlen(p)+1;
				strcpy(p,"*.*");
				p+=strlen(p)+1;
				*p=0;
				ofn.lpstrFilter=szFilter;
				char szFile[256]="";
				ofn.lpstrFileTitle=szFile;
				ofn.nMaxFileTitle=256;
				ofn.lpstrTitle="保存对话记录";
				ofn.Flags=OFN_PATHMUSTEXIST|OFN_OVERWRITEPROMPT;
				char ext[]="txt";
				ofn.lpstrDefExt=ext;
				if(GetSaveFileName(&ofn))
				{
					FILE* pf=fopen(szFile,"a+");
					if(pf)
					{
						char content[500];
						//保存记录
						for(int i=0;i<SendDlgItemMessage(m_hWnd,IDC_MSGLIST,LB_GETCOUNT,0,0);i++)
						{
							SendDlgItemMessage(m_hWnd,IDC_MSGLIST,LB_GETTEXT,i,(LPARAM)content);
							fprintf(pf,"%s\n",content);
						}
						fclose(pf);
					}
					else
					{
						CMsgBox::MessageBox(m_hWnd,IDS_ERR_SAVEREC,IDS_ERR,MB_OK,IDI_INFO);
					}
				}
			}
		}
		break;
	}
	return TRUE;
}
/*------------------------------------------------------------------------------*/
BOOL CMsgTalkDlg::OnInitDialog()
{
	CGraphDlg::OnInitDialog();
	m_MyEdit.Associate(m_hWnd,IDC_T);
	SetWindowText(m_hWnd,"文本对话");
	SendDlgItemMessage(m_hWnd,IDC_T,EM_LIMITTEXT,255,0);
	
	LoadList();

	return TRUE;
}
/*------------------------------------------------------------------------------*/
BOOL CMsgTalkDlg::Create(HWND hParent)
{
	return CGraphDlg::Create(LPCTSTR(IDD_TXT_DLG),hParent);
}


/*------------------------------------------------------------------------------*/

void CMsgTalkDlg::AddMsg(char *pszMsg)
{
	
	int i=SendDlgItemMessage(m_hWnd,IDC_MSGLIST,LB_ADDSTRING,0,(LPARAM)pszMsg);

	//ListBox在横向不会自动Scroll,所以必须处理一下:-)
	CRectX rc;
	HDC hdc=GetDC(m_hWnd);
	HFONT hof=(HFONT)SelectObject(hdc,(HFONT)SendMessage(m_hWnd,WM_GETFONT,0,0));
	DrawText(hdc,pszMsg,strlen(pszMsg), &rc, DT_CALCRECT);
	SelectObject(hdc,hof);
	ReleaseDC(m_hWnd,hdc);
	if(rc.Width()>SendDlgItemMessage(m_hWnd,IDC_MSGLIST,LB_GETHORIZONTALEXTENT,0,0))
	{
		SendDlgItemMessage(m_hWnd,IDC_MSGLIST,LB_SETHORIZONTALEXTENT,rc.Width()+5,0);
	}
	//不知道如何控制纵向滚动条,索性选中最后一个item
	i=SendDlgItemMessage(m_hWnd,IDC_MSGLIST,LB_GETCOUNT,0,0);
	SendDlgItemMessage(m_hWnd,IDC_MSGLIST,LB_SETCURSEL,i-1,0);
	
	
}

void CMsgTalkDlg::LoadList()
{

	SendDlgItemMessage(m_hWnd,IDC_OBJ,CB_RESETCONTENT,0,0);
	//载入通讯录

	char szPath[256];
	GetModuleFileName(0,szPath,255);
	int i;
	for(i=strlen(szPath);i>0;i--)
	{
		if(szPath[i]=='\\')
		{
			szPath[i+1]=0;
			break;
		}
	}
	strcpy(szPath+strlen(szPath),"addr.db");
	
	int count=GetPrivateProfileInt("addr","count",0,szPath);
	char sz[64];
	
	for(i=0;i<count;i++)
	{
		char s[10];
		sprintf(s,"%db",i);
		GetPrivateProfileString("addr",s,"",sz,64,szPath);
		SendDlgItemMessage(m_hWnd,IDC_OBJ,CB_ADDSTRING,0,(LPARAM)sz);
	}
	//载入最近呼叫
	count=GetPrivateProfileInt("recent","count",0,"NetTalk.ini");
	if(count>=20)
	{
		count=20;
	}
	char szStr[256];
	for(i=0;i<count;i++)
	{
		char szI[4];
		sprintf(szI,"%d",i);
		
		if(GetPrivateProfileString("recent",szI,"",szStr,256,"NetTalk.ini"))
		{
			SendMessage(GetDlgItem(m_hWnd,IDC_OBJ),CB_INSERTSTRING,0,(LPARAM)szStr);
			
		}				
		
	}
	SendDlgItemMessage(m_hWnd,IDC_OBJ,CB_SETCURSEL,0,0);
}
