home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / som / somd / cpp / stack / somstack.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-24  |  11.0 KB  |  300 lines

  1. //
  2. //   COMPONENT_NAME: somx
  3. //
  4. //   ORIGINS: 27
  5. //
  6. //
  7. //   10H9767, 10H9769  (C) COPYRIGHT International Business Machines Corp. 1992,1994
  8. //   All Rights Reserved
  9. //   Licensed Materials - Property of IBM
  10. //   US Government Users Restricted Rights - Use, duplication or
  11. //   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  12. //
  13. /* %Z% %I% %W% %G% %U% [%H% %T%] */
  14.  
  15. /*
  16.  *
  17.  * DISCLAIMER OF WARRANTIES.
  18.  * The following [enclosed] code is sample code created by IBM
  19.  * Corporation. This sample code is not part of any standard or IBM
  20.  * product and is provided to you solely for the purpose of assisting
  21.  * you in the development of your applications.  The code is provided
  22.  * "AS IS". IBM MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT
  23.  * NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24.  * FOR A PARTICULAR PURPOSE, REGARDING THE FUNCTION OR PERFORMANCE OF
  25.  * THIS CODE.  IBM shall not be liable for any damages arising out of
  26.  * your use of the sample code, even if they have been advised of the
  27.  * possibility of such damages.
  28.  *
  29.  * DISTRIBUTION.
  30.  * This sample code can be freely distributed, copied, altered, and
  31.  * incorporated into other software, provided that it bears the above
  32.  * Copyright notice and DISCLAIMER intact.
  33.  */
  34.  
  35. /*----------------------------------------
  36.    SOMSTACK.CPP -- SOM Sample Program
  37.   ----------------------------------------*/
  38. #include <windows.h>
  39.  
  40. #include <somd.xh>
  41. #include "stack.xh"
  42. #include "somstack.h"
  43. #include "nlsutil.h"
  44. //#include <bccstruc.h>
  45.  
  46. /***************************************************
  47. Messages for NLS support
  48. ****************************************************/
  49. static char *nlsmsgs[ENDNLSID-STARTNLSID+1];
  50. #define GetNlsMessage(id) nlsmsgs[id-STARTNLSID]
  51. #define SetNlsMessage(id, str) nlsmsgs[id-STARTNLSID] = (str);
  52.  
  53. /**********************************************************
  54. 18953: NLS support
  55. This procedure must be called first to initialize all the messages
  56. to be used by irdump. These messages are obtained from the resource file.
  57. ****************************************************************/
  58. static void InitNlsMsgs(){
  59.  
  60. SetNlsMessage(EmptyStackId, NlsMsgAlloc(EmptyStackId));
  61. SetNlsMessage(StackTopId ,NlsMsgAlloc(StackTopId));
  62. SetNlsMessage(StackAboutId, NlsMsgAlloc(StackAboutId));
  63. SetNlsMessage(PushId    , NlsMsgAlloc(PushId    ));
  64. SetNlsMessage(ValueId, NlsMsgAlloc(ValueId));
  65. SetNlsMessage(ErrorId, NlsMsgAlloc(ErrorId));
  66. SetNlsMessage(ErrorMinorCodeId, NlsMsgAlloc(ErrorMinorCodeId));
  67. SetNlsMessage(ErrorCompletionId, NlsMsgAlloc(ErrorCompletionId));
  68. SetNlsMessage(YesId     , NlsMsgAlloc(YesId     ));
  69. SetNlsMessage(NoId, NlsMsgAlloc(NoId));
  70. SetNlsMessage(MaybeId, NlsMsgAlloc(MaybeId));
  71. SetNlsMessage(ExceptionId, NlsMsgAlloc(ExceptionId));
  72. SetNlsMessage(PushAcceptedId, NlsMsgAlloc(PushAcceptedId));
  73. }
  74.  
  75. //long FAR PASCAL _export WndProc (HWND, UINT, UINT, LONG) ;
  76. long FAR PASCAL WndProc (HWND, UINT, UINT, LONG) ;
  77. int RegisterWindowClasses(void);
  78. HWND CreateMainAppWindow(HANDLE);
  79. void printEv(Environment *ev);
  80.  
  81. Stack *stk;
  82. Environment NEAR ev;
  83.  
  84. char szMainWindowClass[32];
  85. char szMessage[64];
  86. int pushValue;
  87. HANDLE hCurrent;
  88.  
  89. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  90.                         LPSTR lpszCmdParam, int nCmdShow)
  91. {
  92.         HWND hwnd;
  93.         MSG msg;
  94.         static char szAppName[] = "DSOM Stack sample" ;
  95.         WNDCLASS    wndclass ;
  96.  
  97.     InitNlsMsgs();
  98.  
  99. //        SOM_MainProgram();
  100.  
  101.         hCurrent = hInstance;
  102.         LoadString(hInstance, IDS_APPNAME, szMainWindowClass,
  103.                                 sizeof(szMainWindowClass));
  104.  
  105.         if (!hPrevInstance)
  106.           {
  107.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  108.           wndclass.lpfnWndProc   = WndProc ;
  109.           wndclass.cbClsExtra    = 0 ;
  110.           wndclass.cbWndExtra    = 0 ;
  111.           wndclass.hInstance     = hInstance ;
  112.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  113.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  114.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  115.           wndclass.lpszMenuName  = "StackMenu";
  116.           wndclass.lpszClassName = szAppName ;
  117.  
  118.           RegisterClass (&wndclass) ;
  119.           }
  120.  
  121.         hwnd = CreateWindow (szAppName, "Distributed Stack Sample",
  122.                           WS_OVERLAPPEDWINDOW,
  123.                           CW_USEDEFAULT, CW_USEDEFAULT,
  124.                           CW_USEDEFAULT, CW_USEDEFAULT,
  125.                           NULL, NULL, hInstance, NULL) ;
  126.  
  127.         ShowWindow (hwnd, nCmdShow) ;
  128.         UpdateWindow (hwnd) ;
  129.  
  130.  
  131.         while (GetMessage(&msg, NULL, 0, 0))
  132.         {
  133.                 TranslateMessage(&msg);
  134.                 DispatchMessage(&msg);
  135.         }
  136.  
  137.         return msg.wParam;
  138. }
  139.  
  140.  
  141. BOOL FAR PASCAL AboutDlgProc(HWND hdlg, WORD message, WORD wParam, LONG lParam)
  142. {
  143.         switch (message)
  144.         {
  145.                 case WM_INITDIALOG:
  146.                         return TRUE;
  147.                 case WM_COMMAND:
  148.                         switch (wParam)
  149.                         {
  150.                                 case ID_OK:
  151.                                         EndDialog(hdlg, TRUE);
  152.                                         return TRUE;
  153.                         }
  154.                         break;
  155.         }
  156.         return FALSE;
  157. }
  158.  
  159. BOOL FAR PASCAL PushDlgProc(HWND hdlg, WORD message, WORD wParam, LONG lParam)
  160. {
  161.         static HWND hEdit;
  162.         static char valstr[64];
  163.  
  164.         switch (message)
  165.         {
  166.                 case WM_INITDIALOG:
  167.                         pushValue = 100;
  168.                         hEdit = GetDlgItem(hdlg, ID_TEXT);
  169.                         SetFocus(hEdit);
  170.                         return TRUE;
  171.                 case WM_COMMAND:
  172.                         switch (wParam)
  173.                         {
  174.                                 case ID_OK:
  175.                                         GetDlgItemText(hdlg, ID_TEXT, valstr, 63);
  176.                                         pushValue = atoi(valstr);
  177.                                         EndDialog(hdlg, TRUE);
  178.                                         return TRUE;
  179.                                 case ID_CANCEL:
  180.                                         EndDialog(hdlg, FALSE);
  181.                                         return TRUE;
  182.                                 case ID_TEXT:
  183.                                         if (HIWORD(lParam) == EN_CHANGE)
  184.                                         {
  185.                                                 GetDlgItemText(hdlg, ID_TEXT, valstr, 63);
  186.                                                 pushValue = atoi(valstr);
  187.                                         }
  188.                                         return TRUE;
  189.                         }
  190.                         break;
  191.         }
  192.         return FALSE;
  193. }
  194.  
  195.  
  196. long FAR PASCAL WndProc (HWND hwnd, UINT message, UINT wParam,
  197.                          LONG lParam)
  198. {
  199.         HDC hDevContext;
  200.         PAINTSTRUCT ps;
  201.         RECT rect;
  202.         static FARPROC lpfnAboutDlgProc;
  203.         static FARPROC lpfnPushDlgProc;
  204.  
  205.         switch(message)
  206.         {
  207.                 case WM_CREATE:
  208.                         lpfnAboutDlgProc = MakeProcInstance((FARPROC)AboutDlgProc, hCurrent);
  209.                         lpfnPushDlgProc = MakeProcInstance((FARPROC)PushDlgProc, hCurrent);
  210.                         SOM_InitEnvironment(&ev);
  211.                         SOMD_Init(&ev);
  212.                         StackNewClass(Stack_MajorVersion,Stack_MinorVersion);
  213.             stk = (Stack *)(SOMD_ObjectMgr->somdNewObject(&ev, "Stack", NULL));
  214.                         if (ev._major != NO_EXCEPTION) {
  215.                                 printEv(&ev);
  216.                                 SendMessage(hwnd, WM_CLOSE, 0, 0L);
  217.                         }
  218.                         return 0;
  219.  
  220.                 case WM_PAINT:
  221.                         hDevContext = BeginPaint(hwnd, &ps);
  222.                         GetClientRect(hwnd, &rect);
  223.                         if (stk->empty(&ev))
  224.                            wsprintf((LPSTR)szMessage, GetNlsMessage(EmptyStackId));
  225.                         else
  226.                            wsprintf((LPSTR)szMessage, GetNlsMessage(StackTopId), (int *)stk->top(&ev));
  227.                         DrawText(hDevContext, szMessage, -1, &rect,
  228.                                         DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  229.                         EndPaint(hwnd, &ps);
  230.                         return 0;
  231.  
  232.                 case WM_DESTROY:
  233.                         if (stk)
  234.                 SOMD_ObjectMgr->somdDestroyObject(&ev, stk);
  235.                         SOMD_Uninit(&ev);
  236.                         SOM_UninitEnvironment(&ev);
  237.                         PostQuitMessage(0);
  238.                         return 0;
  239.  
  240.                 case WM_COMMAND:
  241.                         switch(wParam)
  242.                         {
  243.                         case IDM_ABOUT:
  244.                                 DialogBox(hCurrent, GetNlsMessage(StackAboutId), hwnd, lpfnAboutDlgProc);
  245.                                 return 0;
  246.                         case IDM_EXIT:
  247.                                 SendMessage(hwnd, WM_CLOSE, 0, 0L);
  248.                                 return 0;
  249.                         case IDM_PUSH:
  250.                                 if (DialogBox(hCurrent, GetNlsMessage(PushId), hwnd, lpfnPushDlgProc))
  251.                                 {
  252.                                         wsprintf(szMessage, GetNlsMessage(ValueId), pushValue);
  253.                                         MessageBox(hwnd, szMessage, GetNlsMessage(PushAcceptedId),
  254.                                                 MB_OK | MB_DEFBUTTON1 | MB_ICONINFORMATION);
  255.                                         stk->push(&ev,(long)pushValue);
  256.                     somdExceptionFree(&ev);
  257.                                         InvalidateRect(hwnd, NULL, TRUE);
  258.                                 }
  259.                                 return 0;
  260.                         case IDM_POP:
  261.                                 stk->pop(&ev);
  262.                 somdExceptionFree(&ev);
  263.                                 InvalidateRect(hwnd, NULL, TRUE);
  264.                                 return 0;
  265.                         }
  266.         }
  267.  
  268.         return DefWindowProc(hwnd, message, wParam, lParam);
  269. }
  270.  
  271. /*
  272.  *  Prints exception information.
  273.  */
  274.  
  275. void printEv(Environment *ev)
  276. {
  277.   char *exId, *bp;
  278.   char buffer[200];
  279.   StExcep *params;
  280.  
  281.   exId = somExceptionId(ev);
  282.   if (!exId) exId = "None";
  283.   params = (StExcep *)(somExceptionValue(ev));
  284.   bp = buffer;
  285.  
  286.   wsprintf(bp, GetNlsMessage(ErrorId), (LPSTR) exId);
  287.   bp += strlen(bp);
  288.   wsprintf(bp, GetNlsMessage(ErrorMinorCodeId), (params ? params->minor : 0));
  289.   bp += strlen(bp);
  290.   wsprintf(bp, GetNlsMessage(ErrorCompletionId),
  291.                 (LPSTR) (params ? (params->completed == YES ? 
  292.         GetNlsMessage(YesId) :
  293.                 params->completed == NO ? GetNlsMessage(NoId): GetNlsMessage(MaybeId)) : GetNlsMessage(YesId)));
  294.   MessageBox ((HWND)NULL, buffer, GetNlsMessage(ExceptionId),
  295.                 MB_ICONEXCLAMATION | MB_OK);
  296.   somdExceptionFree(ev);
  297.   return;
  298. }
  299.  
  300.