D:/Zythum/DinoKod/Common/Console.cpp

00001 //---------------------------------------------------------------------------------------------
00002 //      This file is a part of "DinoKod".
00003 //      Copyright © 2003 Dino Productions. All Rights Reserved.
00004 //      
00005 //      File                    : Console.cpp
00006 //      Author                  : Sebastien LEIX        sebastien.leix@wanadoo.fr
00007 //      Date                    : 07/09/2002
00008 //      Modification    :
00009 //
00010 //---------------------------------------------------------------------------------------------
00011 #include <stdio.h>
00012 #include <string.h>
00013 #include <time.h>
00014 #include "Common/Console.h"
00015 
00016 #include <richedit.h>
00017 
00018 #define WINDOW_CLASSNAME        "DinoConsoleWindow"
00019 
00020 KConsole                g_Console;
00021 
00022 //---------------------------------------------------------------------------------------------------------------------
00023 KConsole::KConsole()
00024 {
00025         m_hConsole              = 0;
00026         m_hLogFile              = 0;
00027         m_hParent               = NULL;
00028         m_CurrentType   = KCMT_NORMAL;
00029 
00030         m_bGraphic              = false;
00031         m_hWnd                  = NULL;
00032         m_hEdit                 = NULL;
00033 
00034         m_bNewLine              = true;
00035 
00036         m_hModuleRichEdit        = NULL;
00037 }
00038 
00039 //---------------------------------------------------------------------------------------------------------------------
00040 bool KConsole::Create( char* pName, bool bGraphic, HINSTANCE hInstance, HWND hFather, bool bCreateWindow )
00041 {
00042         m_bGraphic      = bGraphic;
00043         m_hParent       = hFather;
00044 
00045         if( m_bGraphic )
00046         {
00047                 // Console Graphique
00048                 if( !CreateGraphic( hInstance, hFather, bCreateWindow ) )
00049                         return false;
00050         }
00051         else
00052         {
00053                 // Console Texte
00054                 AllocConsole();
00055                 
00056                 m_hConsole = CreateFile( "CONOUT$",
00057                                         GENERIC_READ | GENERIC_WRITE,
00058                                         FILE_SHARE_READ | FILE_SHARE_WRITE,
00059                                         NULL,
00060                                         OPEN_EXISTING,
00061                                         0,
00062                                         NULL );
00063 /*              m_hConsole = CreateConsoleScreenBuffer( GENERIC_WRITE,
00064                                                                                                 FILE_SHARE_WRITE,
00065                                                                                                 NULL,
00066                                                                                                 CONSOLE_TEXTMODE_BUFFER,
00067                                                                                                 NULL );
00068 
00069                 SetConsoleActiveScreenBuffer( m_hConsole );
00070 */
00071                 if ( m_hConsole == INVALID_HANDLE_VALUE )
00072                         return false;
00073         }
00074 
00075         //
00076         //      fichier .log
00077         //
00078         char    pFileName[1024];
00079 
00080         sprintf( pFileName, "%s.log", pName ? pName : "Console" );
00081 
00082         m_hLogFile = CreateFile(        pFileName,
00083                                                                 GENERIC_WRITE,
00084                                                                 FILE_SHARE_READ,
00085                                                                 NULL,
00086                                                                 CREATE_ALWAYS,
00087                                                                 FILE_ATTRIBUTE_NORMAL,
00088                                                                 NULL );
00089                                                                 
00090         if ( m_hLogFile == INVALID_HANDLE_VALUE )
00091                 return false;
00092 
00093         // Titre
00094         if( pName )
00095                 SetTitle( pName );
00096 
00097         *this << "Console V1.0 - Build : " << __DATE__ << KENDL;
00098 
00099         return true;
00100 }
00101 
00102 //---------------------------------------------------------------------------------------------------------------------
00103 void KConsole::SetTitle( char* pTitle )
00104 {
00105         SetConsoleTitle( pTitle );
00106 }
00107 /*
00108 //---------------------------------------------------------------------------------------------------------------------
00109 void KConsole::Error( const char* text, ... )
00110 {
00111         LPVOID  lpMsgBuf = NULL;
00112         char    buf[1024];
00113         char    buf2[1024];
00114         va_list arg_ptr;
00115 
00116         va_start(arg_ptr, text);
00117         wvsprintf(buf, text, arg_ptr);
00118 
00119         FormatMessage(  FORMAT_MESSAGE_ALLOCATE_BUFFER | 
00120                                         FORMAT_MESSAGE_FROM_SYSTEM | 
00121                                         FORMAT_MESSAGE_IGNORE_INSERTS,
00122                                         NULL,
00123                                         GetLastError(),
00124                                         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00125                                         (LPTSTR) &lpMsgBuf,
00126                                         0,
00127                                         NULL );
00128 
00129         if ( lpMsgBuf )
00130                 wsprintf( buf2, "%s\n%s", buf, lpMsgBuf );
00131         else
00132                 strcpy(buf2, buf);
00133 
00134         Printf( "%s", buf2 );
00135 //      MessageBox( NULL, buf2, "Error", MB_OK | MB_ICONINFORMATION );
00136 
00137         LocalFree( lpMsgBuf );
00138 
00139 
00140 }
00141 */
00142 //---------------------------------------------------------------------------------------------------------------------
00143 bool KConsole::Close()
00144 {
00145         if( m_hModuleRichEdit )
00146         {
00147                 FreeLibrary( m_hModuleRichEdit );
00148                 m_hModuleRichEdit = NULL;
00149         }
00150 
00151         CloseHandle( m_hConsole );
00152         CloseHandle( m_hLogFile );
00153 
00154         if ( !FreeConsole() )
00155                 return false;
00156 
00157         m_hConsole = NULL;
00158         m_hLogFile = NULL;
00159 
00160         return true;
00161 }
00162 
00163 //---------------------------------------------------------------------------------------------------------------------
00164 void KConsole::SetTextAttribut( s16 Attribut )
00165 {
00166         SetConsoleTextAttribute( m_hConsole, Attribut );
00167 }
00168 
00169 //---------------------------------------------------------------------------------------------------------------------
00170 void KConsole::SetCursorPos( u16 x, u16 y )
00171 {
00172         COORD   Pos;
00173 
00174         Pos.X = x;
00175         Pos.Y = y;
00176         SetConsoleCursorPosition( m_hConsole, Pos );    
00177 }
00178 
00179 //---------------------------------------------------------------------------------------------------------------------
00180 KPt KConsole::GetCursorPos()
00181 {
00182         CONSOLE_SCREEN_BUFFER_INFO              ConsoleScreenBufferInfo;
00183         GetConsoleScreenBufferInfo( m_hConsole, &ConsoleScreenBufferInfo );
00184 
00185         return KPt( ConsoleScreenBufferInfo.dwCursorPosition.X, ConsoleScreenBufferInfo.dwCursorPosition.Y );
00186 }
00187 
00188 //---------------------------------------------------------------------------------------------------------------------
00189 void KConsole::Printf( u16 x, u16 y, u16 Attribut, const char *text, ...)
00190 {
00191         CONSOLE_SCREEN_BUFFER_INFO              ConsoleScreenBufferInfo;
00192 
00193         GetConsoleScreenBufferInfo( m_hConsole, &ConsoleScreenBufferInfo );
00194         
00195         SetCursorPos( x, y );
00196         SetTextAttribut( Attribut );
00197         
00198         DWORD   Length;
00199         char    buf[1024];
00200         va_list arg_ptr;
00201 
00202         if( !m_hConsole )
00203                 return;
00204 
00205         char    pSpace[81];
00206         strcpy( pSpace, "                                                                                " );
00207 
00208         va_start(arg_ptr, text);
00209         wvsprintf(buf, text, arg_ptr);
00210         strcat( buf, &pSpace[MIN(strlen(buf),80)] );
00211 
00212         WriteFile(      m_hConsole,
00213                                 buf,
00214                                 (DWORD)strlen( buf ),
00215                                 &Length,
00216                                 NULL );
00217 
00218         SetCursorPos( ConsoleScreenBufferInfo.dwCursorPosition.X, ConsoleScreenBufferInfo.dwCursorPosition.Y );
00219         SetTextAttribut( ConsoleScreenBufferInfo.wAttributes );
00220 }
00221 
00222 //---------------------------------------------------------------------------------------------------------------------
00223 void KConsole::Printf(const char *text, ...)
00224 {
00225         char    pBuffer[1024];
00226         va_list arg_ptr;
00227 
00228         va_start( arg_ptr, text );
00229         wvsprintf( pBuffer, text, arg_ptr );
00230         strcat( pBuffer,"\n");
00231 
00232         Write( pBuffer );
00233 }
00234 
00235 //---------------------------------------------------------------------------------------------------------------------
00236 KConsole& KConsole::operator <<( KStr sString )
00237 {
00238         if( sString == KStr( KENDL ) )
00239                 m_CurrentType = KCMT_NORMAL;
00240 
00241         Write( sString.GetpString(), m_CurrentType );
00242 
00243         return *this;
00244 }
00245 
00246 //---------------------------------------------------------------------------------------------------------------------
00247 KConsole& KConsole::operator <<( int Value )
00248 {
00249         char    pBuffer[256];
00250 
00251         sprintf( pBuffer, "%i", Value );
00252         Write( pBuffer, m_CurrentType );
00253 
00254         return *this;
00255 }
00256 
00257 //---------------------------------------------------------------------------------------------------------------------
00258 KConsole& KConsole::operator <<( unsigned int Value )
00259 {
00260         char    pBuffer[256];
00261 
00262         sprintf( pBuffer, "%i", Value );
00263         Write( pBuffer, m_CurrentType );
00264 
00265         return *this;
00266 }
00267 
00268 //---------------------------------------------------------------------------------------------------------------------
00269 KConsole& KConsole::operator <<( float Value )
00270 {
00271         char    pBuffer[256];
00272 
00273         sprintf( pBuffer, "%.3f", Value );
00274         Write( pBuffer, m_CurrentType );
00275 
00276         return *this;
00277 }
00278 
00279 //---------------------------------------------------------------------------------------------------------------------
00280 KConsole& KConsole::operator <<( KCMESSAGETYPE Type )
00281 {
00282         m_CurrentType = Type;
00283         
00284         return *this;
00285 }
00286 
00287 //---------------------------------------------------------------------------------------------------------------------
00288 void KConsole::Write( char* pString, KCMESSAGETYPE Type )
00289 {
00290         DWORD   Length;
00291         
00292         char*   pNewString = NULL;
00293 
00294         // Rajoute la date
00295         if( m_bNewLine )
00296         {
00297                 struct tm* newtime;
00298                 __time64_t long_time;
00299 
00300                 _time64( &long_time );
00301                 newtime = _localtime64( &long_time );
00302 
00303                 char    pBufferDate[256];
00304                 sprintf( pBufferDate, "%.2i\\%.2i\\%.4i %.2i:%.2i:%.2i> ", newtime->tm_mday, 1 + newtime->tm_mon, 1900 + newtime->tm_year, newtime->tm_hour, newtime->tm_min, newtime->tm_sec );
00305 
00306                 s32 Size = (s32)strlen( pBufferDate ) + (s32)strlen( pString ) + 1;
00307                 pNewString = (char*)malloc( Size );
00308                 strcpy( pNewString, pBufferDate );
00309                 strcat( pNewString, pString );
00310 
00311                 pString = pNewString;
00312 
00313                 m_bNewLine = false;
00314         }
00315 
00316         // Affiche la date la prochaine fois
00317         if( !strcmp( pString, KENDL ) )
00318         {
00319                 m_bNewLine = true;
00320         }
00321 
00322         // Display
00323         if( m_bGraphic )
00324         {
00325                 CHARFORMAT      Format;
00326                 memset( &Format, 0, sizeof( Format ) );
00327                 Format.cbSize           = sizeof( Format );
00328                 Format.dwMask           = CFM_COLOR | CFM_SIZE | CFM_FACE;
00329                 Format.yHeight          = 170;
00330                 strcpy( Format.szFaceName, "Tahoma" );
00331 
00332                 switch( Type )
00333                 {
00334                 case KCMT_NORMAL:
00335                         Format.crTextColor      = RGB( 255, 255, 255 );
00336                         break;
00337                 case KCMT_WARNING:
00338                         Format.crTextColor      = RGB( 255, 200, 0 );
00339                         break;
00340                 case KCMT_ERROR:
00341                         Format.crTextColor      = RGB( 255, 0, 0 );
00342                         break;
00343                 }
00344 /*
00345                 int     Size = (int)SendMessage( m_hEdit, WM_GETTEXTLENGTH, 0, 0 );
00346                 char* pText = new char[Size+strlen(pString)+1];
00347                 
00348                 SendMessage( m_hEdit, WM_GETTEXT, Size + 1, (LPARAM)pText );
00349                 strcat( pText, pString );
00350                 SendMessage( m_hEdit, WM_SETTEXT, 0, (LPARAM)pText );
00351                 
00352                 Deletep( pText );
00353 */
00354                 int     Size = (int)SendMessage( m_hEdit, WM_GETTEXTLENGTH, 0, 0 );
00355                 SendMessage( m_hEdit, EM_SETSEL, (WPARAM)-1, (LPARAM)-1 );
00356                 SendMessage( m_hEdit, EM_SETCHARFORMAT, (WPARAM)SCF_SELECTION, (LPARAM)&Format );
00357                 SendMessage( m_hEdit, EM_REPLACESEL, (WPARAM)FALSE, (LPARAM)pString );
00358                 SendMessage( m_hEdit, EM_SCROLL, SB_PAGEDOWN, 0 );
00359                 
00360 
00361 /*              SendMessage( m_hEdit, EM_SETCHARFORMAT, (WPARAM)SCF_SELECTION, (LPARAM)&Format );
00362                 SendMessage( m_hEdit, EM_SETSEL, (WPARAM)-1, (LPARAM)-1 );
00363                 SendMessage( m_hEdit, EM_REPLACESEL, (WPARAM)FALSE, (LPARAM)pString );*/
00364         }
00365         else
00366         {
00367                 if( m_hConsole )
00368                         WriteFile(      m_hConsole,
00369                                                 pString,
00370                                                 (DWORD)strlen( pString ),
00371                                                 &Length,
00372                                                 NULL );
00373         }
00374 
00375         if( m_hLogFile )
00376                 WriteFile(      m_hLogFile,
00377                                         pString,
00378                                         (DWORD)strlen( pString ),
00379                                         &Length,
00380                                         NULL );
00381     
00382         KOutputConsole* pConsole;
00383         
00384         for( pConsole = m_OutputConsole.GetFirst(); pConsole; pConsole = m_OutputConsole.GetNext( pConsole ) )
00385         {
00386                 pConsole->Write( pString );
00387         }
00388 
00389         SafeFreep( pNewString );
00390 }
00391 
00392 //---------------------------------------------------------------------------------------------------------------------
00393 void KConsole::RegisterConsole( KOutputConsole* pConsole )
00394 {
00395         m_OutputConsole.Add( pConsole );
00396 }
00397 
00398 //---------------------------------------------------------------------------------------------------------------------
00399 void KConsole::UnregisterConsole( KOutputConsole* pConsole )
00400 {
00401         m_OutputConsole.Remove( pConsole );
00402 }
00403 
00404 //---------------------------------------------------------------------------------------------------------------------
00405 bool KConsole::CreateGraphic( HINSTANCE hInstance, HWND hFather, bool bCreateWindow )
00406 {
00407         HWND    hEditFather = NULL;
00408 
00409         if( bCreateWindow )
00410         {
00411                 //
00412                 // Creation de la fenetre
00413                 //
00414                 WNDCLASS        WndClass;
00415 
00416                 memset( &WndClass, 0, sizeof( WndClass ) );
00417                 WndClass.style                  = CS_HREDRAW | CS_VREDRAW;
00418                 WndClass.lpfnWndProc    = WindowProc;
00419                 WndClass.hInstance              = hInstance;
00420                 WndClass.hIcon                  = NULL;//LoadIcon( m_hInstance, MAKEINTRESOURCE(IDI_ICON_APP));
00421                 WndClass.hCursor                = NULL;
00422                 WndClass.hbrBackground  = (HBRUSH)GetStockObject(BLACK_BRUSH);
00423                 WndClass.lpszMenuName   = NULL;
00424                 WndClass.lpszClassName  = WINDOW_CLASSNAME;
00425 
00426                 if( !RegisterClass( &WndClass ) )
00427                         return false;
00428 
00429                 RECT    Rect = { 0, 0, 400, 300 };
00430 
00431                 if( hFather )
00432                 {
00433                         GetWindowRect( hFather, &Rect );
00434                         Rect.left       = Rect.right;
00435                         Rect.right      = Rect.left + 400;
00436                         Rect.bottom     = Rect.top + 300;
00437                 }
00438 
00439                 DWORD   Style = /*WS_CAPTION | */WS_SYSMENU | WS_SIZEBOX | WS_MINIMIZEBOX;// | WS_POPUP;// | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
00440                 m_hWnd = CreateWindowEx(        WS_EX_APPWINDOW,//WS_EX_TOOLWINDOW,
00441                                                                         WINDOW_CLASSNAME,
00442                                                                         "Console",
00443                                                                         Style,
00444                                                                         Rect.left,
00445                                                                         Rect.top,
00446                                                                         Rect.right - Rect.left,
00447                                                                         Rect.bottom - Rect.top,
00448                                                                         hFather,
00449                                                                         NULL,
00450                                                                         hInstance,
00451                                                                         NULL );
00452 
00453                 if ( !m_hWnd )
00454                         return false;
00455 
00456                 ShowWindow( m_hWnd, SW_NORMAL );
00457 
00458                 hEditFather = m_hWnd;
00459         }
00460         else
00461         {
00462                 hEditFather = hFather;
00463         }
00464 
00465         //
00466         // Creation de l'édit
00467         //
00468         m_hModuleRichEdit = LoadLibrary( "RICHED20.DLL" );
00469         if( !m_hModuleRichEdit )
00470                 return false;
00471 
00472         m_hEdit = CreateWindowEx(       WS_EX_STATICEDGE,
00473                                                                 RICHEDIT_CLASS,
00474                                                                 NULL,
00475                                                                 WS_CHILD | //WS_BORDER |
00476                                                                 WS_HSCROLL | WS_VSCROLL | ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_MULTILINE/* | ES_NOHIDESEL /* | ES_SAVESEL | ES_SELECTIONBAR */| ES_READONLY,
00477                                                                 0,
00478                                                                 0,
00479                                                                 100,
00480                                                                 100,
00481                                                                 hEditFather,
00482                                                                 NULL,
00483                                                                 hInstance,
00484                                                                 NULL );
00485 
00486         if ( !m_hEdit )
00487                 return false;
00488 
00489         SetBackGroundColor( KRGB( 64, 64, 64 ) );
00490 
00491         ShowWindow( m_hEdit, SW_SHOWMAXIMIZED );
00492 
00493         return true;
00494 }
00495 
00496 //---------------------------------------------------------------------------------------------------------------------
00497 void KConsole::SetBackGroundColor( KCOLOR Color )
00498 {
00499         SendMessage( m_hEdit, EM_SETBKGNDCOLOR, (WPARAM)0, (LPARAM)RGB( KGETR( Color), KGETG( Color ), KGETB( Color ) ) );
00500 }
00501 
00502 //---------------------------------------------------------------------------------------------------------------------
00503 LRESULT CALLBACK KConsole::WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
00504 {
00505         switch( uMsg )
00506         {
00507         case WM_CREATE:
00508                 break;
00509         
00510         case WM_CLOSE:
00511                 PostQuitMessage( 0 );
00512                 break;
00513         case WM_SIZING:
00514                 {
00515                         RECT    Rect;
00516                         GetClientRect( hWnd, &Rect );
00517                         MoveWindow( g_Console.m_hEdit, 0, 0, Rect.right, Rect.bottom, TRUE );
00518                 }
00519                 break;
00520         }
00521 
00522         return DefWindowProc( hWnd, uMsg, wParam, lParam);
00523 }
00524 
00525 //---------------------------------------------------------------------------------------------------------------------
00526 void KConsole::SetParentWindow( HWND hWnd )
00527 {
00528         KASSERT( 0 );   
00529         // TODO : Bug pere-fils
00530         SetParent( m_hWnd, hWnd );
00531         LONG    Style = GetWindowLong( m_hWnd, GWL_STYLE );
00532         Style &= ~WS_CHILD;
00533         Style |= WS_POPUP;
00534         SetWindowLong( m_hWnd, GWL_STYLE, Style );
00535 }
00536 

Generated on Sun Mar 25 20:02:10 2007 for Zythum Project by  doxygen 1.5.1-p1