www.pudn.com > rDUNclientBeta1.zip > Window.cpp
//Window engine [window.cpp]
//Copyright (C) Robert Merrison 2002
//This program 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.
//This program 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 this program; if not, write to the
//Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "debug.h"
#include "window.h"
////
//cWindow:cWindow - Default constructor
////
cWindow::cWindow( debug_engine* DebugObject )
{
m_hWnd = 0; //Nullify window handle
//Clear out client and screen area rectangle structures
memset( &m_rcClient, 0, sizeof( RECT ));
memset( &m_rcScreen, 0, sizeof( RECT ));
memset( &m_Caption, 0, sizeof( m_Caption )); //Clear out caption string
m_phInstance = NULL;
m_bExists = false; //Window has not been created
m_DebugObject = DebugObject; //Set internal debug engine
}
////
//cWindow::~cWindow - Default destructor
////
cWindow::~cWindow()
{
DestroyWindow( m_hWnd ); //Kill the window handle
m_hWnd = 0; //Nullify the window handle
m_bExists = false; //Window has not been created
}
////
//cWindow::Create - Create the window
////
int cWindow::Create( HINSTANCE hInstance, HICON hIcon, HCURSOR hCursor, char *MenuName, char *ClassName, char* Caption,
int x, int y, int Width, int Height, DWORD dwStyle )
{
if( m_bExists ){ //Check if window already exists
Log_Debug_Message( MSG_ERROR, "Cannot create window, already exists" );
}
strcpy( (char*)&m_Caption, Caption ); //Copy caption to internal string, to make debugging easier
WNDCLASS wc; //Window class structure
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
wc.hCursor = hCursor;
wc.hIcon = hIcon;
wc.hInstance = hInstance;
wc.lpfnWndProc = cWindow::StaticWndProc;
wc.lpszClassName = ClassName;
wc.lpszMenuName = MenuName;
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_OWNDC;
if( RegisterClass( &wc ) == 0 ){ //Register the class
Log_Debug_Message( MSG_ERROR, "Error registering window class." );
return 0;
}
Log_Debug_Message( MSG_NOTICE, "Window class registered." );
m_bExists = true; //Window now exists
//Now to actually create the window
m_hWnd = CreateWindowEx( 0,
ClassName,
Caption,
dwStyle, //(comment for) no System menu
x, y, //Window position
Width, Height, //Window size
0,
0,
hInstance,
0 );
if( m_hWnd == 0 ){ //Was there an error creating it?
Log_Debug_Message( MSG_ERROR, "Error creating window" );
return 0;
}
m_phInstance = &hInstance;
Log_Debug_Message( MSG_NOTICE, "Window created." );
m_dwStyle = dwStyle;
cWindow::OnSize( 0, 0 ); //This just sets up the size rectangles
SetWindowLong( m_hWnd, GWL_USERDATA, (LONG)this ); //Associate class instance with window handle, for use in wndproc
return 1;
}
////
//cWindow::PumpMessages - Checks the message queue and dispatches messages on to the wndproc
////
UINT cWindow::PumpMessage( void )
{
MSG msg; //Current message
if( PeekMessage( &msg, m_hWnd, 0,0, PM_REMOVE )){ //Check for a new message on the queue
TranslateMessage( &msg ); //Translate the new message
DispatchMessage( &msg ); //Send the message on to the wndproc
return msg.message;
}
return 0;
}
////
//cWindow::StaticWndProc - WndProc interface to keep Windows happy
////
LRESULT __stdcall cWindow::StaticWndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
cWindow *wnd = (cWindow*) GetWindowLong( hWnd, GWL_USERDATA ); //Get a pointer to the cWindow instance related to
//the window that recieved the message
if( wnd != 0 ){
return wnd->WndProc( uMsg, wParam, lParam );
}
else{
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
}
////
//cWindow::WndProc - Real wndproc message handler
////
LRESULT cWindow::WndProc( UINT uMsg, WPARAM wParam, LPARAM lParam )
{
LRESULT retVal = 0;
//Pass messages on to their appropriate handlers
switch( uMsg ){
case WM_ACTIVATEAPP: {
retVal = OnActivateApp(( wParam = true ), (DWORD) lParam );
}break;
case WM_CREATE: {
retVal = OnCreate((LPCREATESTRUCT) lParam );
}break;
case WM_SETCURSOR: {
retVal = OnSetCursor((DWORD) lParam );
}break;
case WM_COMMAND: {
retVal = OnCommand( HIWORD( wParam ), LOWORD( lParam ), (HWND) lParam );
}break;
case WM_INITMENU: {
retVal = OnInitMenu((HMENU) wParam );
}break;
case WM_ERASEBKGND: {
retVal = OnEraseBkgnd((HDC) wParam );
}break;
case WM_PAINT: {
retVal = OnPaint();
}break;
case WM_DESTROY: {
retVal = OnDestroy();
}break;
case WM_ENTERMENULOOP: {
retVal = OnEnterMenuLoop(( wParam == TRUE ));
}break;
case WM_EXITMENULOOP: {
retVal = OnExitMenuLoop(( wParam == TRUE ));
}break;
case WM_KEYDOWN: {
retVal = OnKeyDown((DWORD) wParam );
}break;
case WM_KEYUP: {
retVal = OnKeyUp((DWORD) wParam );
}break;
case WM_SIZE: {
retVal = OnSize( LOWORD( lParam ), HIWORD( lParam ));
}break;
case WM_MOVE: {
retVal = OnMove( LOWORD( lParam ), HIWORD( lParam ));
}break;
default: {
retVal = OnUnknown( uMsg, wParam, lParam );
}break;
}
if( retVal == 0 ){
return DefWindowProc( m_hWnd, uMsg, wParam, lParam );
}
else{
return retVal;
}
}
////
//cWindow::Destroy - Destroy window
////
void cWindow::Destroy( void )
{
Log_Debug_Message( MSG_NOTICE, "Destroying window." );
DestroyWindow( m_hWnd ); //Kill the window handle
m_hWnd = 0;
}
////
//cWindow::LogDebugMessage - Interface for debug engine
////
void cWindow::Log_Debug_Message( short msg_type, char *message, ... )
{
//Check if we are using the debug engine or not
if( m_DebugObject == NULL ){
return;
}
//Read in any paramters that may have been passed
va_list parameters;
char ParamString[128];
memset( ParamString, 0, sizeof( ParamString ) );
va_start( parameters, message );
vsprintf( ParamString, message, parameters );
va_end( parameters );
//Construct the string to log
char string_to_output[256];
memset( string_to_output, 0, sizeof( string_to_output ));
if( m_Caption != NULL ){ //If the window caption has been set, include it in the error string
sprintf( string_to_output, "Window Message (%s): %s", m_Caption, ParamString );
}
else{
sprintf( string_to_output, "Window Message (Unknown): %s", ParamString );
}
//Pass the message on to the logging function
m_DebugObject->log_message( msg_type, string_to_output );
return;
}
////
//Message handlers
////
LRESULT cWindow::OnActivateApp( bool bActivate, DWORD dwThread )
{
return 0;
}
LRESULT cWindow::OnCreate( LPCREATESTRUCT createData )
{
return 0;
}
LRESULT cWindow::OnSetCursor( DWORD dwhitTest )
{
return 0;
}
LRESULT cWindow::OnCommand( WORD wNotificationCode, WORD wIdentifier, HWND hCtrl )
{
return 0;
}
LRESULT cWindow::OnInitMenu( HMENU hMenu )
{
return 0;
}
LRESULT cWindow::OnEraseBkgnd( HDC hDC )
{
return 0;
}
LRESULT cWindow::OnPaint()
{
return 0;
}
LRESULT cWindow::OnDestroy()
{
return 0;
}
LRESULT cWindow::OnEnterMenuLoop( bool bTrackPopupMenu )
{
return 0;
}
LRESULT cWindow::OnExitMenuLoop( bool bShortcutMenu )
{
return 0;
}
LRESULT cWindow::OnKeyDown( DWORD dwKey )
{
return 0;
}
LRESULT cWindow::OnKeyUp( DWORD dwKey )
{
return 0;
}
LRESULT cWindow::OnSize( WORD wWidth, WORD wHeight )
{
GetClientRect( m_hWnd, &m_rcClient ); //Update the client rectangle to reflect size change
m_rcScreen = m_rcClient;
ClientToScreen( m_hWnd, (POINT*) &m_rcScreen.left );
ClientToScreen( m_hWnd, (POINT*) &m_rcScreen.right );
return 0;
}
LRESULT cWindow::OnMove( WORD x, WORD y )
{
GetClientRect( m_hWnd, &m_rcClient ); //Update the client rectangle to reflect position change
m_rcScreen = m_rcClient;
ClientToScreen( m_hWnd, (POINT*) &m_rcScreen.left );
ClientToScreen( m_hWnd, (POINT*) &m_rcScreen.right );
return 0;
}
LRESULT cWindow::OnUnknown( UINT uMsg, WPARAM wParam, LPARAM lParam )
{
return 0;
}