/*
** Generated Code 
*/
#include <afxwin.h>
#include <afxext.h>


#include "main.h"

shell_p shell = (shell_p) NULL;

static _xd_initialised = 0;

typedef struct PixelResources_s { 
	CBrush magenta;
	CBrush cyan;
	CBrush yellow;
	CBrush blue;
	CBrush green;
	CBrush red;
	CBrush white;
	CBrush black;
} PixelResources_t, *PixelResources_p;

static PixelResources_t pixel_resources;

static void initialise_objects( CWnd *parent )
{
	if ( _xd_initialised ) return;
	_xd_initialised = 1;
	CDC *dc = parent->GetDC();
	pixel_resources.magenta.CreateSolidBrush ( dc->GetNearestColor ( RGB ( 255, 0, 255 ) ) );
	pixel_resources.cyan.CreateSolidBrush ( dc->GetNearestColor ( RGB ( 0, 255, 255 ) ) );
	pixel_resources.yellow.CreateSolidBrush ( dc->GetNearestColor ( RGB ( 255, 255, 0 ) ) );
	pixel_resources.blue.CreateSolidBrush ( dc->GetNearestColor ( RGB ( 0, 0, 255 ) ) );
	pixel_resources.green.CreateSolidBrush ( dc->GetNearestColor ( RGB ( 0, 255, 0 ) ) );
	pixel_resources.red.CreateSolidBrush ( dc->GetNearestColor ( RGB ( 255, 0, 0 ) ) );
	pixel_resources.white.CreateSolidBrush ( dc->GetNearestColor ( RGB ( 255, 255, 255 ) ) );
	pixel_resources.black.CreateSolidBrush ( dc->GetNearestColor ( RGB ( 0, 0, 0 ) ) );
	parent->ReleaseDC ( dc );
}


BEGIN_MESSAGE_MAP (drawing_c, CWnd)
//{{AFX_MSG_MAP(drawing_c)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void drawing_c::create (CWnd * parent, char *widget_name, unsigned int id, CRect *in_rect)
{
	CRect rect;

	if ( in_rect )
		rect = *in_rect;
	else
		rect.SetRect ( 0, 0, 250, 250 );

	if ( !widget_name )
		widget_name = "drawing";

	colours[black]=&pixel_resources.black;
	colours[white]=&pixel_resources.white;
	colours[red]=&pixel_resources.red;
	colours[green]=&pixel_resources.green;
	colours[blue]=&pixel_resources.blue;
	colours[yellow]=&pixel_resources.yellow;
	colours[cyan]=&pixel_resources.cyan;
	colours[magenta]=&pixel_resources.magenta;
	initialised = FALSE;    
	for ( unsigned char y = 0; y < 32; y++ )
		for ( unsigned char x = 0; x < 32; )
			for ( unsigned char c = (unsigned char)black; c <= (unsigned char)magenta; c++, x++ )
				cells [x][y] = (colour_type)c; 


	Create ( NULL, widget_name,WS_CHILD | WS_VISIBLE | WS_TABSTOP, rect, parent, IDC_shell_drawing);
}

BOOL drawing_c::OnEraseBkgnd ( CDC *pDC )
{
	CRect rc;
	CRgn rgn;
	GetClientRect ( &rc );
	rgn.CreateRectRgnIndirect ( rc );
	pDC->PaintRgn ( &rgn );
	return TRUE;
}

BEGIN_MESSAGE_MAP (tool_box_c, CButton)
//{{AFX_MSG_MAP(tool_box_c)
ON_BN_CLICKED( IDC_tool_box_widget16, OnValueChanged_widget16 )
ON_BN_CLICKED( IDC_tool_box_widget17, OnValueChanged_widget17 )
ON_BN_CLICKED( IDC_tool_box_widget18, OnValueChanged_widget18 )
ON_BN_CLICKED( IDC_tool_box_widget19, OnValueChanged_widget19 )
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void tool_box_c::create (CWnd * parent, char *widget_name, unsigned int id, CRect *in_rect)
{
	CRect rect;

	if ( in_rect )
		rect = *in_rect;
	else
		rect.SetRect ( 0, 0, 161, 68 );

	if ( !widget_name )
		widget_name = "tool_box";

tool = pencil;
	Create ( NULL, WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_GROUP | BS_GROUPBOX, rect, parent, IDC_shell_tool_box);
	widget16->Create ( "Pencil", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTORADIOBUTTON, CRect ( 5, 12, 62, 34 ), this, IDC_tool_box_widget16);
	widget16->SetCheck ( TRUE );
	widget17->Create ( "Line", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTORADIOBUTTON, CRect ( 5, 41, 62, 63 ), this, IDC_tool_box_widget17);
	widget18->Create ( "Rect", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTORADIOBUTTON, CRect ( 69, 12, 156, 34 ), this, IDC_tool_box_widget18);
	widget19->Create ( "Fill", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTORADIOBUTTON, CRect ( 69, 41, 156, 63 ), this, IDC_tool_box_widget19);
}

tool_box_c::tool_box_c()
{
	widget16 = new CButton;
	widget17 = new CButton;
	widget18 = new CButton;
	widget19 = new CButton;
}

tool_box_c::~tool_box_c()
{
	delete widget16;
	delete widget17;
	delete widget18;
	delete widget19;
}

afx_msg void tool_box_c::OnValueChanged_widget16()
{
	Pencil ();
}

afx_msg void tool_box_c::OnValueChanged_widget17()
{
	Line ();
}

afx_msg void tool_box_c::OnValueChanged_widget18()
{
	Rectangle ();
}

afx_msg void tool_box_c::OnValueChanged_widget19()
{
	Fill ();
}

BOOL tool_box_c::OnEraseBkgnd ( CDC *pDC )
{
	CRect rc;
	CRgn rgn;
	GetClientRect ( &rc );
	rgn.CreateRectRgnIndirect ( rc );
	pDC->PaintRgn ( &rgn );
	return TRUE;
}

BEGIN_MESSAGE_MAP (colour_box_c, CWnd)
//{{AFX_MSG_MAP(colour_box_c)
ON_BN_CLICKED( IDC_colour_box_widget22, OnValueChanged_widget22 )
ON_BN_CLICKED( IDC_colour_box_widget23, OnValueChanged_widget23 )
ON_BN_CLICKED( IDC_colour_box_widget24, OnValueChanged_widget24 )
ON_BN_CLICKED( IDC_colour_box_widget25, OnValueChanged_widget25 )
ON_BN_CLICKED( IDC_colour_box_widget26, OnValueChanged_widget26 )
ON_BN_CLICKED( IDC_colour_box_widget27, OnValueChanged_widget27 )
ON_BN_CLICKED( IDC_colour_box_widget28, OnValueChanged_widget28 )
ON_BN_CLICKED( IDC_colour_box_widget29, OnValueChanged_widget29 )
ON_WM_ERASEBKGND()
ON_WM_CTLCOLOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void colour_box_c::create (CWnd * parent, char *widget_name, unsigned int id, CRect *in_rect)
{
	CRect rect;

	if ( in_rect )
		rect = *in_rect;
	else
		rect.SetRect ( 0, 0, 161, 119 );

	if ( !widget_name )
		widget_name = "colour_box";

colour = black;
	Create ( NULL, widget_name,WS_CHILD | WS_VISIBLE | WS_TABSTOP, rect, parent, IDC_shell_colour_box);
	widget22->Create ( "Black", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTORADIOBUTTON, CRect ( 5, 5, 77, 27 ), this, IDC_colour_box_widget22);
	widget22->SetCheck ( TRUE );
	widget23->Create ( "White", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTORADIOBUTTON, CRect ( 5, 34, 77, 56 ), this, IDC_colour_box_widget23);
	widget24->Create ( "Red", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTORADIOBUTTON, CRect ( 5, 63, 77, 85 ), this, IDC_colour_box_widget24);
	widget25->Create ( "Green", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTORADIOBUTTON, CRect ( 5, 92, 77, 114 ), this, IDC_colour_box_widget25);
	widget26->Create ( "Blue", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTORADIOBUTTON, CRect ( 84, 5, 156, 27 ), this, IDC_colour_box_widget26);
	widget27->Create ( "Yellow", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTORADIOBUTTON, CRect ( 84, 34, 156, 56 ), this, IDC_colour_box_widget27);
	widget28->Create ( "Cyan", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTORADIOBUTTON, CRect ( 84, 63, 156, 85 ), this, IDC_colour_box_widget28);
	widget29->Create ( "Magenta", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTORADIOBUTTON, CRect ( 84, 92, 156, 114 ), this, IDC_colour_box_widget29);
}

colour_box_c::colour_box_c()
{
	widget22 = new CButton;
	widget23 = new CButton;
	widget24 = new CButton;
	widget25 = new CButton;
	widget26 = new CButton;
	widget27 = new CButton;
	widget28 = new CButton;
	widget29 = new CButton;
}

colour_box_c::~colour_box_c()
{
	delete widget22;
	delete widget23;
	delete widget24;
	delete widget25;
	delete widget26;
	delete widget27;
	delete widget28;
	delete widget29;
}

afx_msg void colour_box_c::OnValueChanged_widget22()
{
	Black ();
}

afx_msg void colour_box_c::OnValueChanged_widget23()
{
	White ();
}

afx_msg void colour_box_c::OnValueChanged_widget24()
{
	Red ();
}

afx_msg void colour_box_c::OnValueChanged_widget25()
{
	Green ();
}

afx_msg void colour_box_c::OnValueChanged_widget26()
{
	Blue ();
}

afx_msg void colour_box_c::OnValueChanged_widget27()
{
	Yellow ();
}

afx_msg void colour_box_c::OnValueChanged_widget28()
{
	Cyan ();
}

afx_msg void colour_box_c::OnValueChanged_widget29()
{
	Magenta ();
}

BOOL colour_box_c::OnEraseBkgnd ( CDC *pDC )
{
	CRect rc;
	CRgn rgn;
	GetClientRect ( &rc );
	rgn.CreateRectRgnIndirect ( rc );
	pDC->PaintRgn ( &rgn );
	return TRUE;
}

HBRUSH colour_box_c::OnCtlColor ( CDC *pDC, CWnd *pWnd, UINT nCtlColor )
{
	CDC *dc = GetDC();
	HBRUSH brush = CWnd::OnCtlColor ( pDC, pWnd, nCtlColor );
	if ( pWnd == widget22 )
	{
		pDC->SetTextColor ( RGB ( 255, 255, 255 ) );
		pDC->SetBkColor ( RGB ( 0, 0, 0 ) );
		brush = (HBRUSH)pixel_resources.black.m_hObject;
	}
	if ( pWnd == widget23 )
	{
		pDC->SetBkColor ( RGB ( 255, 255, 255 ) );
		brush = (HBRUSH)pixel_resources.white.m_hObject;
	}
	if ( pWnd == widget24 )
	{
		pDC->SetBkColor ( RGB ( 255, 0, 0 ) );
		brush = (HBRUSH)pixel_resources.red.m_hObject;
	}
	if ( pWnd == widget25 )
	{
		pDC->SetBkColor ( RGB ( 0, 255, 0 ) );
		brush = (HBRUSH)pixel_resources.green.m_hObject;
	}
	if ( pWnd == widget26 )
	{
		pDC->SetTextColor ( RGB ( 255, 255, 255 ) );
		pDC->SetBkColor ( RGB ( 0, 0, 255 ) );
		brush = (HBRUSH)pixel_resources.blue.m_hObject;
	}
	if ( pWnd == widget27 )
	{
		pDC->SetBkColor ( RGB ( 255, 255, 0 ) );
		brush = (HBRUSH)pixel_resources.yellow.m_hObject;
	}
	if ( pWnd == widget28 )
	{
		pDC->SetBkColor ( RGB ( 0, 255, 255 ) );
		brush = (HBRUSH)pixel_resources.cyan.m_hObject;
	}
	if ( pWnd == widget29 )
	{
		pDC->SetBkColor ( RGB ( 255, 0, 255 ) );
		brush = (HBRUSH)pixel_resources.magenta.m_hObject;
	}
	ReleaseDC ( dc );
	return brush;
}

BEGIN_MESSAGE_MAP (shell_c, CFrameWnd)
//{{AFX_MSG_MAP(shell_c)
ON_COMMAND( ID_shell_widget7, OnActivate_widget7 )
ON_WM_SIZE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void shell_c::create (CWinApp *win_app, CRect *in_rect)
{
	CRect rect;

	if ( in_rect )
		rect = *in_rect;
	else
		rect.SetRect ( 0, 0, 432, 260 );
	char xd_wnd_class[] = "AfxFrameOrView";
	m_bAutoMenuEnable = FALSE;

	initialise_objects ( this );
	AdjustWindowRectEx ( rect, WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX, TRUE, 0 );
	rect.SetRect ( 0, 0, rect.Width(), rect.Height() );
	Create ( xd_wnd_class, "shell", WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX, rect);
	LoadAccelTable ( "IDR_shell" );
	widget3->CreateMenu ( );
	widget3->AppendMenu (MF_STRING, ID_shell_widget5, "New" );
	widget3->AppendMenu (MF_STRING, ID_shell_widget6, "Open" );
	widget3->AppendMenu (MF_STRING, ID_shell_widget7, "Exit" );
	widget8->CreateMenu ( );
	widget8->AppendMenu (MF_STRING, ID_shell_widget10, "Cut" );
	widget8->AppendMenu (MF_STRING, ID_shell_widget11, "Copy" );
	widget8->AppendMenu (MF_STRING, ID_shell_widget12, "Paste" );
	widget2->CreateMenu ( );
	widget2->AppendMenu (MF_POPUP, (UINT)widget3->m_hMenu, "File" );
	widget2->AppendMenu (MF_POPUP, (UINT)widget8->m_hMenu, "Edit" );
	SetMenu ( widget2 );
	rect.SetRect ( 177, 5, 427, 255 );
	drawing->create ( this, "drawing", IDC_shell_drawing, &rect );
	rect.SetRect ( 8, 1, 169, 69 );
	tool_box->create ( this, "tool_box", IDC_shell_tool_box, &rect );
	rect.SetRect ( 8, 77, 169, 196 );
	colour_box->create ( this, "colour_box", IDC_shell_colour_box, &rect );
}

shell_c::shell_c()
{
	widget2 = new CMenu;
	widget3 = new CMenu;
	widget8 = new CMenu;
	drawing = new drawing_c;
	tool_box = new tool_box_c;
	colour_box = new colour_box_c;
}

shell_c::~shell_c()
{
	widget2->Detach();
	delete widget2;
	widget3->Detach();
	delete widget3;
	widget8->Detach();
	delete widget8;
	delete drawing;
	delete tool_box;
	delete colour_box;
}

afx_msg void shell_c::OnActivate_widget7()
{
	DoExit ();
}

void shell_c::OnSize ( UINT nType, int cx, int cy)
{
	CFrameWnd::OnSize ( nType, cx, cy );
	if ( drawing->m_hWnd )
		drawing->MoveWindow ( 177, 5, cx - 182, cy - 10, TRUE );
}


CXApplicationApp::CXApplicationApp ( const char *pszAppName )
	:CWinApp ( pszAppName )
{
}

BOOL CXApplicationApp::InitInstance ()
{
	BOOL base_return = CWinApp::InitInstance();
	shell = new shell_c;
	shell->create ( this );
	m_pMainWnd = shell;
	m_pMainWnd->ShowWindow ( m_nCmdShow );
	m_pMainWnd->UpdateWindow ( );
	return TRUE;
}

int CXApplicationApp::ExitInstance ()
{
	return CWinApp::ExitInstance();
}

CXApplicationApp XApplication;
