/*
	ZazouMiniWebServer

	Copyright (C) 2003-2009 Xavier Garreau <xavier@xgarreau.org>

	This file is part of ZazouMiniWebServer.

    ZazouMiniWebServer is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    ZazouMiniWebServer is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ZazouMiniWebServer; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
#include "stdafx.h"
#include "ZMWSConsole.h"

static BOOL ConsoleCtrlHandler(DWORD fdwCtrlType);


BOOL CreateConsole () {
	if (AllocConsole()) {
		SetConsoleCtrlHandler((PHANDLER_ROUTINE)ConsoleCtrlHandler, TRUE);

		// Truc adapté de http://mail.python.org/pipermail/python-list/2002-February/085621.html
		// format a "unique" newWindowTitle
		TCHAR newWindowTitle[256];
		wsprintf(newWindowTitle,"%d/%d",
			GetTickCount(),
			GetCurrentProcessId());
		// change current window title
		SetConsoleTitle(newWindowTitle);
		// ensure window title has been updated
		Sleep(40);
		// look for newWindowTitle
		HWND hwndFound=FindWindow(NULL, newWindowTitle);
		// If found, hide it
		if ( hwndFound != NULL) {
			HMENU hMenu = GetSystemMenu(hwndFound,0);
			if(hMenu != NULL)
			{
				DeleteMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
				DrawMenuBar(hwndFound);
			}
		}

		SetConsoleTitle ("ZazouMiniWebServer console");
		return TRUE;
	}
	return FALSE;
}

// Gestionnaire d'évènements pour la Console
static BOOL ConsoleCtrlHandler(DWORD fdwCtrlType) 
{ 
    switch (fdwCtrlType) 
    { 
        case CTRL_C_EVENT: 
        case CTRL_BREAK_EVENT: 
            return TRUE; 
        default: 
            return FALSE; 
	} 
} 
