home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / program / c / WMLOOKUP.ZIP / WMLOOKUP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-03  |  6.8 KB  |  306 lines

  1.  
  2. #include <afxwin.h>
  3. #include <afxcoll.h>
  4. #include <iostream.h>
  5. #include <strstrea.h>
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9.  
  10. #include "wmlookup.h"
  11.  
  12.  
  13. ////////////////////////////////////////
  14. //
  15. // Serializable Message Structure
  16. //
  17.  
  18. IMPLEMENT_SERIAL(CMessages, CObject, 0) 
  19.  
  20.  
  21. void CMessages::Serialize( CArchive& archive)
  22. {
  23.     CObject::Serialize( archive ) ;
  24.     
  25.     if (archive.IsStoring() )
  26.     {
  27.         archive << messageText ;
  28.     }
  29.     else
  30.     {
  31.         archive >> messageText ;
  32.     }
  33. }
  34. ////////////////////////////////////////
  35. //
  36. // Dialog Code
  37. //
  38.  
  39. BEGIN_MESSAGE_MAP(CDlgLookUp, CDialog)
  40.     ON_WM_CLOSE()
  41.     ON_COMMAND(IDOK,OnOK) 
  42.     ON_COMMAND(IDCANCEL, OnCancel) 
  43. END_MESSAGE_MAP()
  44.  
  45. BOOL CDlgLookUp::bRegistered = FALSE ;
  46.  
  47. CDlgLookUp::CDlgLookUp()
  48. {
  49.     pMap = NULL ;
  50.     
  51.     if (!bRegistered)
  52.     {
  53.      bRegistered = RegisterMyClass() ;    
  54.     }
  55.     
  56.     Create(MAKEINTRESOURCE(100)) ;
  57.  
  58. CDlgLookUp::~CDlgLookUp()
  59. {
  60.     delete pMap ;
  61. }
  62.  
  63. BOOL CDlgLookUp::RegisterMyClass() {
  64.     WNDCLASS wndclass;
  65.      
  66.     wndclass.style          = 0;
  67.               
  68.     // Let's use DefDlgProc for our dialog's Window Procedure, we
  69.     // don't need to modify any of the behavior anyway.
  70.        wndclass.lpfnWndProc    = DefDlgProc;
  71.        wndclass.cbClsExtra     = 0 ;
  72.                                     
  73.     // This field MUST be set to DLGWINDOWEXTRA, or this class we're
  74.    // registering won't work properly with our dialog boxes.
  75.        wndclass.cbWndExtra     = DLGWINDOWEXTRA ;
  76.                                                             
  77.     // Use the app's instance.  AfxGetInstanceHandle() gets this for
  78.     // us nicely.
  79.        wndclass.hInstance      = AfxGetInstanceHandle();
  80.                                                                 
  81.     // Load our custom icon.  
  82.         wndclass.hIcon          = ::LoadIcon (AfxGetInstanceHandle(), "WMLOOKUP_ICON") ;
  83.  
  84.    // Use an arrow cursor for kicks
  85.         wndclass.hCursor        = ::LoadCursor (NULL, IDC_ARROW) ;
  86.        wndclass.hbrBackground  = COLOR_WINDOW + 1 ;
  87.         wndclass.lpszMenuName   = NULL ;
  88.                         
  89.     // Make up a unique name for our dialog class.  This same name must
  90.    // be used in our dialog box template to force the dialog box to
  91.     // use this new class.
  92.        wndclass.lpszClassName  = "WMLOOKUPCLASS";
  93.                                                      
  94.    return ::RegisterClass(&wndclass);
  95. }
  96.                                                             
  97.   
  98.  
  99. BOOL CDlgLookUp::OnInitDialog()
  100. {
  101.     CFile archiveFile ;
  102.     
  103.     if( archiveFile.Open("wmlookup.jnk", CFile::modeRead ) )
  104.     {
  105.         CArchive theInArchive(&archiveFile, CArchive::load) ;
  106.         theInArchive >> pMap ;
  107.         theInArchive.Close() ;
  108.         archiveFile.Close() ;
  109.     }
  110.     else
  111.     {
  112.  
  113.         int ret = MessageBox("Cannot find file WMLOOKUP.JNK.\n Should I create it?",
  114.                         "WmLookUp", MB_YESNO | MB_ICONQUESTION ) ;
  115.                     
  116.         pMap = new CMapWordToOb ;
  117.         
  118.         buildMap() ;
  119.  
  120.         if (ret == IDYES)
  121.         {
  122.             // Create the file WMLOOKUP.JNK
  123.             archiveFile.Open("wmlookup.jnk", CFile::modeWrite | CFile::modeCreate );
  124.     
  125.             CArchive theOutArchive(&archiveFile, CArchive::store) ;
  126.             theOutArchive << pMap ;
  127.             theOutArchive.Close() ;
  128.             archiveFile.Close() ;
  129.         }
  130.     }
  131.     
  132.     fileListBox() ;
  133.     return TRUE;
  134. }
  135.  
  136. void CDlgLookUp::OnCancel()
  137. {
  138.     delete this ;
  139. }
  140.  
  141. afx_msg void CDlgLookUp::OnOK()
  142. {
  143.     UINT messageId ;
  144.  
  145.     // Is Message Id Hex or Decimal
  146.         BOOL bDec = DecimalCheck().GetCheck() ;
  147.     
  148.     // Get Message Id
  149.         if ( DecimalCheck().GetCheck() )
  150.         {
  151.             // Decimal Value
  152.             BOOL bRet ;
  153.             messageId = GetDlgItemInt(idMessageID, &bRet, FALSE ) ;
  154.             if (!bRet)
  155.             {
  156.                 MessageBox("Invalid Message Id.","WM LookUp", MB_OK | MB_ICONEXCLAMATION) ;
  157.                 return ;
  158.             }
  159.         }
  160.         else
  161.         {
  162.             // Hex Value
  163.             char  text[20] ;
  164.             if( GetDlgItemText(idMessageID, text, sizeof(text)) == 0 )
  165.             {
  166.                 MessageBox("Invalid Message Id.","WM LookUp", MB_OK | MB_ICONEXCLAMATION) ;
  167.                 return ;
  168.             }
  169.             
  170.             istrstream astrstr(text) ;  
  171.             astrstr >> hex >> messageId ;
  172.             if (astrstr.fail() )
  173.             {
  174.                 MessageBox("Invalid Message Id.","WM LookUp", MB_OK | MB_ICONEXCLAMATION) ;
  175.                 return ;
  176.             }
  177.             
  178.         }
  179.         
  180.     // Find Message Text
  181.     
  182.         //*** CString *pstr ;
  183.         CMessages *pMessage ;
  184.         if (pMap->Lookup(messageId, (CObject *&)pMessage) )
  185.         {
  186.             // Display Message Text
  187.             MessageTextList().SelectString(-1, pMessage->messageText) ;
  188.         }
  189.         else
  190.         {
  191.             MessageTextList().SelectString(-1, lpszNotFound ) ;
  192.             MessageBeep(0) ;
  193.         }
  194. }
  195.  
  196.  
  197. /////////////////////////////////////////////////////////////////////////////
  198. void CDlgLookUp::fileListBox() 
  199. {
  200.         // Turn off listbox updating
  201.         int stops = (LOWORD(GetDialogBaseUnits()) / 4 ) * 2 * 30 ;
  202.         MessageTextList().SetTabStops(1, &stops ) ;
  203.         MessageTextList().SetRedraw(FALSE) ;
  204.         MessageTextList().AddString(lpszNotFound) ;
  205.  
  206.         char tempbuf[80] ;
  207.         CMessages *message ;
  208.         WORD messageId ;
  209.         POSITION pos ;
  210.         for( pos = pMap->GetStartPosition() ; pos != NULL ; /**/)
  211.         {
  212.               pMap->GetNextAssoc(pos, messageId, (CObject*&)message) ;
  213.  
  214.             // Add to listbox
  215.                 wsprintf(tempbuf,"%s\t0x%04x",
  216.                                 (LPCSTR)message->messageText, messageId) ;
  217.  
  218.                 MessageTextList().AddString(tempbuf) ;
  219.                 
  220.             }
  221.                     
  222.     // Turn on listbox updating
  223.         MessageTextList().SetRedraw(TRUE) ;
  224.  
  225.  
  226.  
  227. }
  228. /////////////////////////////////////////////////////////////////////////////
  229. // buildMap --- Reads in the Windows.h file and builds a CMapWordToString 
  230. //                     with it.  The information could have been directly placed 
  231. //                     into the listbox, however, we wanted to play a little with
  232. //                     the MFC collections.
  233. //
  234. BOOL CDlgLookUp::buildMap()
  235. {
  236.     
  237.     // Put these into class
  238.         char hdrName[180] ;
  239.         char match[250] ;
  240.         wsprintf(match,"#define %s%%s 0x%%x",msgType) ;
  241.         
  242.         _searchenv("windows.h", "INCLUDE", hdrName) ;
  243.         if ( !(*hdrName) )
  244.         {
  245.             MessageBox("Cannot Find File.", "WM LookUp", MB_OK | MB_ICONEXCLAMATION ) ;
  246.             return FALSE ;
  247.         }
  248.         
  249.     // Open File
  250.         CStdioFile f;
  251.         if (!f.Open(hdrName,    CFile::modeRead | CFile::typeText) )
  252.                                 
  253.         {
  254.             #ifdef _DEBUG
  255.                 afxDump << "File could not be opened " << "\r\n" ;
  256.             #endif
  257.             return FALSE ;
  258.         }
  259.     
  260.     const UINT LLENGTH = 200 ;    
  261.     char buf[LLENGTH] ;
  262.  
  263.     char messageText[40] ;
  264.     WORD messageId ;
  265.     int count ;
  266.  
  267.     while ( f.ReadString( buf, LLENGTH) )
  268.     {
  269.         // Blow it all off and use scanf...
  270.         
  271.             count = sscanf(buf, match ,&messageText ,&messageId ) ;
  272.             if ( count != 0 )
  273.             {
  274.                     // Put into data structure
  275.                         CMessages *pMessage = new CMessages ;
  276.                         pMessage->messageText = CString(msgType) + CString(messageText) ; 
  277.                         pMap->SetAt(messageId, pMessage) ;
  278.                 
  279.             }
  280.     }
  281.  
  282.     return TRUE ;
  283.     
  284. }
  285. /////////////////////////////////////////////////////////////////////////////
  286. // Application class
  287. //
  288. class CTheApp : public CWinApp
  289. {
  290. public:
  291.     virtual BOOL InitInstance();
  292. };
  293.  
  294. CTheApp anApp;
  295.  
  296. BOOL CTheApp::InitInstance()
  297. {
  298.     m_pMainWnd = new CDlgLookUp() ;
  299.     m_pMainWnd->ShowWindow(m_nCmdShow) ;
  300.     m_pMainWnd->UpdateWindow() ;
  301.     return TRUE;
  302. }
  303.  
  304. /////////////////////////////////////////////////////////////////////////////
  305.