// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "GoRang.h"

#include "MainFrm.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_WM_SETFOCUS()
	ON_WM_ACTIVATE()
	ON_WM_ACTIVATEAPP()
	ON_WM_DESTROY()
	ON_COMMAND(ID_VIEW_TOOLBAR, OnViewToolbar)
	ON_COMMAND(IDM_BLOCKINGTEST, OnBlockingtest)
	ON_COMMAND(IDM_VIEW_TOPORDER, OnViewToporder)
	ON_UPDATE_COMMAND_UI(IDM_VIEW_TOPORDER, OnUpdateViewToporder)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_BATGORANG,OnNeTraceWindowCommand)

	ON_WM_QUERYENDSESSION()
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	m_IsTopWinFlag = 0;
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	// create a view to occupy the client area of the frame
	if (!m_wndView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
		CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
	{
		TRACE0("Failed to create view window\n");
		return -1;
	}
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	//if (!m_wndStatusBar.Create(this) ||
	//	!m_wndStatusBar.SetIndicators(indicators,
	//	  sizeof(indicators)/sizeof(UINT)))
	//{
	//	TRACE0("Failed to create status bar\n");
	//	return -1;      // fail to create
	//}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	SetWindowText(BATGORANG_WINDOW);
	HICON hicon =AfxGetApp()->LoadIcon(IDI_MINICON);
	//HICON hicon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	//SetIcon(hicon,TRUE);
	SetIcon(hicon,FALSE);
	int cx = GetSystemMetrics(SM_CXSCREEN);
	int cy = GetSystemMetrics(SM_CYSCREEN);
	//MoveWindow(5,80,cx/2,cy/2,TRUE);
    CRect rcwnd;
    GetWindowRect(&rcwnd);
    //::SetWindowPos(GetSafeHwnd(), NULL, -7, cy - rcwnd.Height()-14, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW);// | SWP_ASYNCWINDOWPOS);
	::SetWindowPos(GetSafeHwnd(), NULL, cx - rcwnd.Width()+6, cy - rcwnd.Height()-32, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW);// | SWP_ASYNCWINDOWPOS);

	ShowControlBar(&m_wndToolBar,FALSE,TRUE);
	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	int sx = GetSystemMetrics(SM_CXSCREEN); //GetSystemMetrics(SM_CXVIRTUALSCREEN);//멀티모니터 가상크기
	//cs.style = WS_OVERLAPPED | WS_CAPTION | FWS_ADDTOTITLE
	//	| WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
	cs.style |= WS_CLIPCHILDREN;
	cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
	cs.cx = sx - 100;
	cs.cy = 250;
	cs.lpszClass = RegisterWindowClass(); //AfxRegisterWndClass(0);
	return TRUE;
}

LPCTSTR CMainFrame::RegisterWindowClass(void)
{
	WNDCLASS wndcls;
	HINSTANCE hInst = AfxGetInstanceHandle();
	LPCTSTR lpClassName = BUILDOPT_LOGGERCLASSNAME;
	if (!(::GetClassInfo(hInst, lpClassName, &wndcls)))
	{
		// otherwise we need to register a new class
		wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
		//// 2003.10.13 NEW UPDATE KRKIM,
		////==========================================================================================
		////wndcls.lpfnWndProc		= ::DefWindowProc;
		////The DefFrameProc function provides default processing for any window messages that 
		////the window procedure of a multiple document interface (MDI) frame window does not process.
		////All window messages that are not explicitly processed by the window procedure must be
		////passed to the DefFrameProc function, not the DefWindowProc function. 
		//wndcls.lpfnWndProc = (WNDPROC)::DefFrameProc;
		////==========================================================================================
		wndcls.lpfnWndProc = ::DefWindowProc;
		wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
		wndcls.hInstance = hInst;
		wndcls.hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
		wndcls.hCursor = LoadCursor(hInst, IDC_ARROW);//NULL;
		wndcls.hbrBackground = NULL;//(HBRUSH) (COLOR_WINDOW + 1);/*NULL*/
		wndcls.lpszMenuName = NULL;
		wndcls.lpszClassName = lpClassName;

		if (!AfxRegisterClass(&wndcls)) {
			AfxThrowResourceException();
			return NULL;
		}
	}
	return lpClassName;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
void CMainFrame::OnSetFocus(CWnd* pOldWnd)
{
	// forward focus to the view window
	m_wndView.SetFocus();
}

BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
	// let the view have first crack at the command
	if (m_wndView.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
		return TRUE;

	// otherwise, do default handling
	return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}


void CMainFrame::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized) 
{
	CFrameWnd::OnActivate(nState, pWndOther, bMinimized);
	
	// TODO: Add your message handler code here
	
}

void CMainFrame::OnActivateApp(BOOL bActive, DWORD hTask) 
{
	CFrameWnd::OnActivateApp(bActive, hTask);
	
	m_wndView.ActivateApp(bActive,hTask);
	// TODO: Add your message handler code here
	
}

LONG CMainFrame::OnNeTraceWindowCommand(WPARAM wParam, LPARAM lParam)
{
	m_wndView.SendMessage(WM_BATGORANG,wParam,lParam);
	//	OnNeTraceWindowCommand(wParam,lParam);

	return TRUE;
}

void CMainFrame::OnDestroy() 
{
	CFrameWnd::OnDestroy();
	
	// TODO: Add your message handler code here
	
}

void CMainFrame::OnViewToolbar() 
{
	BOOL bShow = FALSE;
	bShow = !m_wndToolBar.IsWindowVisible(); 
	ShowControlBar(&m_wndToolBar,bShow,TRUE);
}

#define WM_MYTESTFUNC	WM_USER + 10

void CMainFrame::OnBlockingtest() 
{
	m_wndView.PostMessage(WM_MYTESTFUNC,0,0);
	//testfunc();
}

void CMainFrame::OnViewToporder() 
{
	//WINDOW를 항상 가장 최상위로
	// TODO: Add your command handler code here
	if(m_IsTopWinFlag==0)
	{
		::SetWindowPos(AfxGetMainWnd()->m_hWnd,HWND_TOPMOST,-1,-1,-1,-1
		,SWP_NOMOVE|SWP_NOSIZE);
	}
	else
	{
	::SetWindowPos(AfxGetMainWnd()->m_hWnd,HWND_NOTOPMOST,-1,-1,-1,-1
		,SWP_NOMOVE|SWP_NOSIZE);
	}
	m_IsTopWinFlag^=1;
	
}

void CMainFrame::OnUpdateViewToporder(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(m_IsTopWinFlag);	
}


BOOL CMainFrame::OnQueryEndSession()
{
	if (!CFrameWnd::OnQueryEndSession())
		return FALSE;
	//Applications should respect the user's intentions and return TRUE. 
	//By default, the DefWindowProc function returns TRUE for this message.
	//TRUE = close app, FALSE = prevent close
	if (!m_wndView.CloseNow())
		return FALSE;//종료취소
	// 시스템 종료전에 옵션을 저장한다.


	return TRUE;
}
