home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / ADDON.PAK / TESTDLG.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  8.3 KB  |  264 lines

  1. /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  2.  
  3.   testdlg.cpp
  4.   Copyright (c) 1987, 1996 Borland International Inc.  All Rights Reserved.
  5.   $Revision:   1.18  $
  6.    
  7. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/  
  8. #ifndef __AOEXPCH_H
  9.   #include "aoexpch.h"
  10. #endif
  11.  
  12. #pragma hdrstop
  13.  
  14.  
  15. #include <limits.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <ctype.h>
  19. #include <windowsx.h>
  20.  
  21. #include "tests.hrc"
  22. #include "tests.h"
  23.  
  24. //.............................................................................
  25. class TestDlgOutput : public TestOutput {
  26.  public:
  27.   TestDlgOutput() { _hEditBox = NULL; } 
  28.   virtual void Show( const char * str );
  29.   void RegisterEditBox( HWND hEditBox ) { 
  30.     _hEditBox = hEditBox; 
  31.     SendMessage( _hEditBox, EM_LIMITTEXT, 0, 0 ); // don't limit text
  32.   }
  33.  protected:
  34.   HWND _hEditBox; 
  35. };
  36. //.............................................................................
  37. void TestDlgOutput::Show( const char * str ) {
  38.   if ( _hEditBox ) {
  39.     SendMessage( _hEditBox, EM_REPLACESEL, 0, (LPARAM) str );
  40.     SendMessage( _hEditBox, EM_REPLACESEL, 0, (LPARAM) "\r\n" );
  41.   }
  42. }
  43.  
  44. //.............................................................................
  45. class TestDlg {
  46.  public:
  47.   TestDlg();
  48.   ~TestDlg() {}
  49.   void InitDialog();
  50.   void EnableTesting();
  51.   BOOL FindTesterFromCurrentSelection();
  52.   void DialogCommand( WORD wNotifyCode, WORD wID ); 
  53.   long WndProc( HWND hwnd, UINT message, UINT wParam, LONG lParam );
  54.   HWND Create( HINSTANCE hInstance );
  55.   void Destroy();
  56.   void Topmost();
  57.   void Show();
  58.  protected:
  59.   HWND _hwnd;
  60.   Tests * _tests;
  61.   TestObject * _currentTestObj;
  62.   BOOL _topmost;
  63.   TestDlgOutput _testDlgOutput;
  64. };
  65. static TestDlg theTestDlg;
  66.  
  67. //.............................................................................
  68. TestDlg::TestDlg() {
  69.   _hwnd = NULL;
  70.   _tests = NULL;
  71.   _currentTestObj = NULL;
  72.   _topmost = FALSE;
  73. }  
  74.  
  75. //.............................................................................
  76. void TestDlg::EnableTesting() {
  77.   int fEnable = (_currentTestObj != NULL);
  78.   EnableWindow(GetDlgItem(_hwnd, IDC_LIST1), !fEnable);
  79.   EnableWindow(GetDlgItem(_hwnd, IDC_INIT), !fEnable);
  80.   EnableWindow(GetDlgItem(_hwnd, IDC_UNINIT), fEnable);
  81.   EnableWindow(GetDlgItem(_hwnd, IDC_TEST1), fEnable);
  82.   EnableWindow(GetDlgItem(_hwnd, IDC_TEST2), fEnable);
  83.   EnableWindow(GetDlgItem(_hwnd, IDC_TEST3), fEnable);
  84.   EnableWindow(GetDlgItem(_hwnd, IDC_TEST4), fEnable);
  85.   EnableWindow(GetDlgItem(_hwnd, IDC_TEST1_HELP), fEnable);
  86.   EnableWindow(GetDlgItem(_hwnd, IDC_TEST2_HELP), fEnable);
  87.   EnableWindow(GetDlgItem(_hwnd, IDC_TEST3_HELP), fEnable);
  88.   EnableWindow(GetDlgItem(_hwnd, IDC_TEST4_HELP), fEnable);
  89. }
  90. //.............................................................................
  91. void TestDlg::InitDialog() {
  92.   if ( !_tests ) {
  93.     HWND hwndCtl = GetDlgItem(_hwnd, IDC_LIST1);
  94.     _tests = Tests::GetTests();
  95.     for ( int i = 0; i < _tests->NumTestObjects(); ++i ) {
  96.       ListBox_AddString( hwndCtl, _tests->GetTestObject( i )->GetName() );
  97.     } 
  98.     _testDlgOutput.RegisterEditBox( GetDlgItem( _hwnd, IDC_OUTPUT ) );
  99.     _tests->RegisterOutput( &_testDlgOutput );
  100.     EnableTesting();
  101.   }
  102. }
  103. //.............................................................................
  104. BOOL TestDlg::FindTesterFromCurrentSelection() {
  105.   char szBuffer[256];
  106.   if ( _tests ) {
  107.     HWND hwndCtl = GetDlgItem( _hwnd, IDC_LIST1);
  108.     int index = ListBox_GetCurSel( hwndCtl );
  109.     ListBox_GetText( hwndCtl, index, szBuffer );
  110.     _currentTestObj = _tests->FindTestObject( szBuffer );
  111.   }
  112.   return _currentTestObj != NULL;
  113. }
  114. //.............................................................................
  115. void TestDlg::DialogCommand( WORD wNotifyCode, WORD wID ) {
  116.   switch (wNotifyCode) {
  117.    case BN_CLICKED:
  118.    {
  119.     switch (wID)
  120.     {
  121.       case IDC_CLOSE:
  122.         //.... simulate IDC_UNINIT command .....
  123.         DialogCommand( BN_CLICKED, IDC_UNINIT );
  124.         DestroyWindow( _hwnd );
  125.         return;
  126.       case IDC_INIT:
  127.        if ( FindTesterFromCurrentSelection() ) {
  128.         _currentTestObj->Init();
  129.         EnableTesting();
  130.        }
  131.        break;
  132.       case IDC_UNINIT:
  133.        if ( _currentTestObj ) {
  134.         _currentTestObj->UnInit();
  135.         _currentTestObj = NULL;
  136.         EnableTesting();
  137.        }
  138.        break;
  139.       case IDC_TEST1:
  140.        if ( _currentTestObj ) {
  141.         _currentTestObj->DoTest( 1 );
  142.        }
  143.        break;
  144.       case IDC_TEST2:
  145.        if ( _currentTestObj ) {
  146.         _currentTestObj->DoTest( 2 );
  147.        }
  148.        break;
  149.       case IDC_TEST3:
  150.        if ( _currentTestObj ) {
  151.         _currentTestObj->DoTest( 3 );
  152.        }
  153.        break;
  154.       case IDC_TEST4:
  155.        if ( _currentTestObj ) {
  156.         _currentTestObj->DoTest( 4 );
  157.        }
  158.        break;
  159.     case IDC_TEST1_HELP:
  160.       if ( _currentTestObj ) {
  161.         MessageBox( _hwnd, _currentTestObj->GetTestDescription( 1 ), "Test 1", MB_OK );
  162.       }
  163.       break;
  164.     case IDC_TEST2_HELP:
  165.       if ( _currentTestObj ) {
  166.         MessageBox( _hwnd, _currentTestObj->GetTestDescription( 2 ), "Test 2", MB_OK );
  167.       }
  168.       break;
  169.     case IDC_TEST3_HELP:
  170.       if ( _currentTestObj ) {
  171.         MessageBox( _hwnd, _currentTestObj->GetTestDescription( 3 ), "Test 3", MB_OK );
  172.       }
  173.       break;
  174.     case IDC_TEST4_HELP:
  175.       if ( _currentTestObj ) {
  176.         MessageBox( _hwnd, _currentTestObj->GetTestDescription( 4 ), "Test 4", MB_OK );
  177.       }
  178.       break;
  179.     case IDC_TOPMOST:
  180.       _topmost = IsDlgButtonChecked( _hwnd, IDC_TOPMOST );
  181.       Show();
  182.       break; 
  183.     }
  184.    };
  185.    break;
  186.    case LBN_DBLCLK:
  187.    {
  188.    }
  189.    break;
  190.   }
  191. }
  192. //.............................................................................
  193. long CALLBACK __export theWndProc (HWND hwnd, UINT message, UINT wParam ,LONG lParam) {
  194.   return theTestDlg.WndProc( hwnd, message, wParam, lParam );
  195. }
  196. //.............................................................................
  197. long TestDlg::WndProc( HWND hwnd, UINT message, UINT wParam, LONG lParam ) {
  198.   _hwnd = hwnd;
  199.   switch (message) {
  200.     case WM_SHOWWINDOW:
  201.       Topmost();
  202.       InitDialog();
  203.       break;
  204.     case WM_COMMAND: {
  205.         WORD wNotifyCode = HIWORD(wParam); // notification code
  206.         WORD wID = LOWORD(wParam);      // item, control, or accelerator identifier
  207.         DialogCommand( wNotifyCode, wID );
  208.         break;
  209.     }
  210.     case WM_CLOSE:
  211.       //.... simulate IDC_UNINIT command .....
  212.       DialogCommand( BN_CLICKED, IDC_UNINIT );
  213.       DestroyWindow( _hwnd );
  214.       _hwnd = NULL;
  215.       break ;
  216.   }
  217.   return DefDlgProc( _hwnd, message, wParam, lParam );
  218. }
  219. //.............................................................................
  220. HWND TestDlg::Create( HINSTANCE hInstance ) {
  221.   WNDCLASS wndclass;
  222.   wndclass.style       = CS_HREDRAW | CS_VREDRAW ;
  223.   wndclass.lpfnWndProc   = theWndProc ;
  224.   wndclass.cbClsExtra   = 0 ;
  225.   wndclass.cbWndExtra   = DLGWINDOWEXTRA ;
  226.   wndclass.hInstance    = hInstance ;
  227.   wndclass.hIcon       = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_ICON1)) ;
  228.   wndclass.hCursor      = LoadCursor (NULL, IDC_ARROW) ;
  229.   wndclass.hbrBackground  = (HBRUSH)(COLOR_BACKGROUND+1); // COLOR_BTNFACE
  230.   wndclass.lpszMenuName = NULL ;
  231.   wndclass.lpszClassName  = "AddOnTester";
  232.   RegisterClass (&wndclass) ;
  233.  
  234.   _hwnd = CreateDialog( hInstance, MAKEINTRESOURCE(IDD_DIALOG1), 0, NULL) ;
  235.   Show();
  236.   return _hwnd;
  237. };
  238. //.............................................................................
  239. void TestDlg::Destroy() {
  240.   DestroyWindow( _hwnd );
  241. }
  242. //.............................................................................
  243. void TestDlg::Topmost() {
  244.   if ( _hwnd ) {
  245.     CheckDlgButton( _hwnd, IDC_TOPMOST, _topmost );
  246.     SetWindowPos( _hwnd, _topmost? HWND_TOPMOST : HWND_NOTOPMOST, 
  247.               0,0,0,0, SWP_NOMOVE | SWP_NOSIZE );
  248.   }
  249. }
  250.   
  251. //.............................................................................
  252. void TestDlg::Show() {  
  253.   Topmost();
  254.   ShowWindow( _hwnd, SW_SHOW );
  255. }
  256. //.............................................................................
  257. HWND CreateTestDialog(HINSTANCE hInstance) {
  258.   return theTestDlg.Create( hInstance );
  259. }
  260. //.............................................................................
  261. void DestroyTestDialog() {
  262.   theTestDlg.Destroy();
  263. }
  264.