home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / som / somd / cpp / animal / anitest.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-24  |  14.8 KB  |  393 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.  
  14. /* %Z% %I% %W% %G% %U% [%H% %T%] */
  15.  
  16. /*
  17.  *
  18.  * DISCLAIMER OF WARRANTIES.
  19.  * The following [enclosed] code is sample code created by IBM
  20.  * Corporation. This sample code is not part of any standard or IBM
  21.  * product and is provided to you solely for the purpose of assisting
  22.  * you in the development of your applications.  The code is provided
  23.  * "AS IS". IBM MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT
  24.  * NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25.  * FOR A PARTICULAR PURPOSE, REGARDING THE FUNCTION OR PERFORMANCE OF
  26.  * THIS CODE.  IBM shall not be liable for any damages arising out of
  27.  * your use of the sample code, even if they have been advised of the
  28.  * possibility of such damages.
  29.  *
  30.  * DISTRIBUTION.
  31.  * This sample code can be freely distributed, copied, altered, and
  32.  * incorporated into other software, provided that it bears the above
  33.  * Copyright notice and DISCLAIMER intact.
  34.  */
  35.  
  36. /*----------------------------------------
  37.    ANISAMP.C -- SOM Sample Program
  38.   ----------------------------------------*/
  39.  
  40. #include <windows.h>
  41. #include <somd.xh>    
  42. #include "danimal.xh"
  43. #include "dldog.xh"
  44. #include "dbdog.xh"
  45. #include "dloc.xh"
  46. #include "nlsutil.h"
  47. #include "anitest.h"
  48.  
  49. /***************************************************
  50. Messages for NLS support
  51. ****************************************************/
  52. static char *nlsmsgs[ENDNLSID-STARTNLSID+1];
  53. #define GetNlsMessage(id) nlsmsgs[id-STARTNLSID]
  54. #define SetNlsMessage(id, str) nlsmsgs[id-STARTNLSID] = (str);
  55.  
  56. struct
  57.      {
  58.      long style ;
  59.      char *text ;
  60.      }
  61.      button[] =
  62.      {
  63.      BS_PUSHBUTTON,      GetNlsMessage(PoohId),
  64.      BS_PUSHBUTTON,      GetNlsMessage(SnoopyId),
  65.      BS_PUSHBUTTON,      GetNlsMessage(LassieId),
  66.      } ;
  67.  
  68.  
  69. /**********************************************************
  70. 18953: NLS support
  71. This procedure must be called first to initialize all the messages
  72. to be used by irdump. These messages are obtained from the resource file.
  73. ****************************************************************/
  74. static void InitNlsMsgs(){
  75.  
  76. SetNlsMessage(IamId, NlsMsgAlloc(IamId));
  77. SetNlsMessage(PoohId ,NlsMsgAlloc(PoohId));
  78. SetNlsMessage(SnoopyId ,NlsMsgAlloc(SnoopyId));
  79. SetNlsMessage(LassieId, NlsMsgAlloc(LassieId));
  80. SetNlsMessage(TitleId, NlsMsgAlloc(TitleId));
  81. SetNlsMessage(PressButtonId, NlsMsgAlloc(PressButtonId));
  82. SetNlsMessage(WinniePoohId, NlsMsgAlloc(WinniePoohId));
  83. SetNlsMessage(StorybookBearId, NlsMsgAlloc(StorybookBearId));
  84. SetNlsMessage(HoneyId, NlsMsgAlloc(HoneyId));
  85. SetNlsMessage(AustinId, NlsMsgAlloc(AustinId));
  86. SetNlsMessage(TexasId, NlsMsgAlloc(TexasId));
  87. SetNlsMessage(CartoonBeagleId, NlsMsgAlloc(CartoonBeagleId));
  88. SetNlsMessage(CookiesId, NlsMsgAlloc(CookiesId));
  89. SetNlsMessage(DublinId, NlsMsgAlloc(DublinId));
  90. SetNlsMessage(OhioId, NlsMsgAlloc(OhioId));
  91. SetNlsMessage(DancingId, NlsMsgAlloc(DancingId));
  92. SetNlsMessage(TVCollieId, NlsMsgAlloc(TVCollieId));
  93. SetNlsMessage(SteakId, NlsMsgAlloc(SteakId));
  94. SetNlsMessage(HollywoodId, NlsMsgAlloc(HollywoodId));
  95. SetNlsMessage(CaliforniaId, NlsMsgAlloc(CaliforniaId));
  96. SetNlsMessage(SavingLivesId, NlsMsgAlloc(SavingLivesId));
  97. SetNlsMessage(MynameId, NlsMsgAlloc(MynameId));
  98. SetNlsMessage(ILikeToEatId, NlsMsgAlloc(ILikeToEatId));
  99. SetNlsMessage(ILiveInId, NlsMsgAlloc(ILiveInId));
  100. SetNlsMessage(IWeighStrId, NlsMsgAlloc(IWeighStrId));
  101. SetNlsMessage(IWeighFloatId, NlsMsgAlloc(IWeighFloatId));
  102. /*SetNlsMessage(MyTrickId, NlsMsgAlloc(MyTrickId));*/
  103. SetNlsMessage(ErrorId, NlsMsgAlloc(ErrorId));
  104. SetNlsMessage(MinorErrorId, NlsMsgAlloc(MinorErrorId));
  105. SetNlsMessage(CompletionErrorId, NlsMsgAlloc(CompletionErrorId));
  106. button[0].text =  GetNlsMessage(PoohId);
  107. button[1].text =  GetNlsMessage(SnoopyId);
  108. button[2].text =  GetNlsMessage(LassieId);
  109. }
  110.  
  111. #define NUM (sizeof button / sizeof button [0])
  112. #define checkEv(ev) ((ev)->_major != NO_EXCEPTION)
  113.  
  114.  
  115. void printEv(Environment *ev);
  116.  
  117. long APIENTRY WndProc (HWND, UINT, UINT, LONG);
  118.  
  119. int APIENTRY WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  120.                       LPSTR lpszCmdLine, int nCmdShow)
  121.      {
  122.      static char szAppName[] = "DAnisamp" ;
  123.      HWND        hwnd ;
  124.      MSG         msg ;
  125.      WNDCLASS    wndclass ;
  126.  
  127.      InitNlsMsgs();
  128.  
  129.      somEnvironmentNew(); 
  130.  
  131.      if (!hPrevInstance)
  132.           {
  133.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  134.           wndclass.lpfnWndProc   = WndProc ;
  135.           wndclass.cbClsExtra    = 0 ;
  136.           wndclass.cbWndExtra    = 0 ;
  137.           wndclass.hInstance     = hInstance ;
  138.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  139.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  140.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  141.           wndclass.lpszMenuName  = NULL ;
  142.           wndclass.lpszClassName = szAppName ;
  143.  
  144.           RegisterClass (&wndclass) ;
  145.           }
  146.  
  147.      hwnd = CreateWindow (szAppName, GetNlsMessage(TitleId),
  148.                           WS_OVERLAPPEDWINDOW,
  149.                           CW_USEDEFAULT, CW_USEDEFAULT,
  150.                           CW_USEDEFAULT, CW_USEDEFAULT,
  151.                           NULL, NULL, hInstance, NULL) ;
  152.  
  153.      ShowWindow (hwnd, nCmdShow) ;
  154.      UpdateWindow (hwnd) ;
  155.  
  156.      while (GetMessage (&msg, NULL, 0, 0))
  157.           {
  158.           TranslateMessage (&msg) ;
  159.           DispatchMessage (&msg) ;
  160.           }
  161.      return msg.wParam ;
  162.      }
  163.  
  164. long APIENTRY WndProc (HWND hwnd, UINT message, UINT wParam,
  165.                        LONG lParam)
  166.      {
  167.      static char  *szTop= NULL; /* d18953 */
  168.      static HWND  hwndButton [NUM] ;
  169.      static int   buttonx [NUM] = {  5, 20, 35 };
  170.      static int   buttony [NUM] = {  8, 8, 8};
  171.      static RECT  NEAR  rect ;
  172.      static int   cxChar, cyChar ;
  173.      HDC          hdc ;
  174.      HCURSOR      hCursor;
  175.      PAINTSTRUCT  ps ;
  176.      int          i ;
  177.      TEXTMETRIC   tm ;
  178.      static dAnimal     *Pooh = (dAnimal *) NULL;
  179.      static dLittleDog  *Snoopy = (dLittleDog *) NULL;
  180.      static dBigDog     *Lassie = (dBigDog *) NULL;
  181.      static dLocation   *locPooh = (dLocation *) NULL;
  182.      static dLocation   *locSnoopy = (dLocation*) NULL;
  183.      static dLocation   *locLassie = (dLocation *) NULL;
  184.      static Environment *ev;
  185.      string buffer;
  186.  
  187.      /* d18953 */
  188.      if (szTop == NULL)
  189.     /* initialize szTop */
  190.     szTop = GetNlsMessage(PressButtonId); 
  191.  
  192.      switch (message)
  193.           {
  194.           case WM_CREATE:
  195.  
  196.                 /* local and DSOM initialization */
  197.                 ev = SOM_CreateLocalEnvironment();
  198.                 SOMD_Init(ev);
  199.                 Pooh = (dAnimal *) NULL;
  200.                 Snoopy = (dLittleDog *) NULL;
  201.                 Lassie = (dBigDog *) NULL;
  202.                 locPooh = (dLocation *) NULL;
  203.                 locSnoopy = (dLocation *) NULL;
  204.                 locLassie = (dLocation *) NULL;
  205.  
  206.                 /* Make sure classes are registered with SOMClassMgr: */
  207.                 dAnimalNewClass(0,0);
  208.                 dLittleDogNewClass(0,0);
  209.                 dBigDogNewClass(0,0);
  210.                 dLocationNewClass(0,0);
  211.  
  212.                /* create remote animal object */
  213.                Pooh = (dAnimal *) SOMD_ObjectMgr->somdNewObject(ev,
  214.                                                         "dAnimal", NULL);
  215.                if (checkEv(ev)) goto error;
  216.  
  217.                /* save pointers to dLocation object */
  218.                locPooh = Pooh->_get_loc(ev);
  219.                if (checkEv(ev)) goto error;
  220.  
  221.                Snoopy = (dLittleDog *) SOMD_ObjectMgr->somdNewObject(ev,
  222.                                                         "dLittleDog", NULL);
  223.                if (checkEv(ev)) goto error;
  224.  
  225.                locSnoopy = Snoopy->_get_loc(ev);
  226.                if (checkEv(ev)) goto error;
  227.  
  228.                Lassie = (dBigDog *)SOMD_ObjectMgr->somdNewObject(ev,
  229.                                                         "dBigDog", NULL);
  230.                if (checkEv(ev)) goto error;
  231.  
  232.                locLassie = Lassie->_get_loc(ev);
  233.                if (checkEv(ev)) goto error;
  234.  
  235.                 /* set instance data for animal objects */
  236.                 Pooh->_set_name(ev, GetNlsMessage(WinniePoohId));
  237.                 if (checkEv(ev)) goto error;
  238.                 Pooh->_set_type(ev, GetNlsMessage(StorybookBearId));
  239.                 if (checkEv(ev)) goto error;
  240.                 Pooh->_set_food(ev, GetNlsMessage(HoneyId));
  241.                 if (checkEv(ev)) goto error;
  242.                 locPooh->_set_city(ev, GetNlsMessage(AustinId));
  243.                 if (checkEv(ev)) goto error;
  244.                 locPooh->_set_state(ev, GetNlsMessage(TexasId));
  245.                 if (checkEv(ev)) goto error;
  246.  
  247.                 Snoopy->_set_name(ev, GetNlsMessage(SnoopyId));
  248.                 if (checkEv(ev)) goto error;
  249.                 Snoopy->_set_type(ev, GetNlsMessage(CartoonBeagleId));
  250.                 if (checkEv(ev)) goto error;
  251.                 Snoopy->_set_food(ev, GetNlsMessage(CookiesId));
  252.                 if (checkEv(ev)) goto error;
  253.                 locSnoopy->_set_city(ev, GetNlsMessage(DublinId));
  254.                 if (checkEv(ev)) goto error;
  255.                 locSnoopy->_set_state(ev, GetNlsMessage(OhioId));
  256.                 if (checkEv(ev)) goto error;
  257.                 Snoopy->_set_trick(ev, GetNlsMessage(DancingId));
  258.                 if (checkEv(ev)) goto error;
  259.                 Snoopy->_set_LDweight(ev, 320.47);
  260.                 if (checkEv(ev)) goto error;
  261.  
  262.                 Lassie->_set_name(ev, GetNlsMessage(LassieId));
  263.                 if (checkEv(ev)) goto error;
  264.                 Lassie->_set_type(ev, GetNlsMessage(TVCollieId));
  265.                 if (checkEv(ev)) goto error;
  266.                 Lassie->_set_food(ev, GetNlsMessage(SteakId));
  267.                 if (checkEv(ev)) goto error;
  268.                 locLassie->_set_city(ev, GetNlsMessage(HollywoodId));
  269.                 if (checkEv(ev)) goto error;
  270.                 locLassie->_set_state(ev, GetNlsMessage(CaliforniaId));
  271.                 if (checkEv(ev)) goto error;
  272.                 Lassie->_set_trick(ev, GetNlsMessage(SavingLivesId));
  273.                 if (checkEv(ev)) goto error;
  274.                 Lassie->_set_BDweight(ev, 42.38);
  275.                 if (checkEv(ev)) goto error;
  276.  
  277.                /* Display one button per animal: */
  278.                hdc = GetDC (hwnd) ;
  279.                GetTextMetrics (hdc, &tm) ;
  280.                cxChar = tm.tmAveCharWidth ;
  281.                cyChar = tm.tmHeight + tm.tmExternalLeading ;
  282.                ReleaseDC (hwnd, hdc) ;
  283.                for (i = 0 ; i < NUM ; i++)
  284.                  hwndButton [i] =
  285.                      CreateWindow ("button",
  286.                                    button[i].text,
  287.                                    WS_CHILD | WS_VISIBLE | button[i].style,
  288.                                    buttonx[i] * cxChar,
  289.                                    cyChar * buttony[i],
  290.                                    10 * cxChar,
  291.                                    7 * cyChar / 4,
  292.                                    hwnd,
  293.                                    (HMENU)i,
  294.                                    (HINSTANCE)((LPCREATESTRUCT)lParam)->hInstance,
  295.                                    0);
  296.                return 0 ;
  297.  
  298.                 error:
  299.                    printEv(ev);
  300.                    SendMessage(hwnd, WM_CLOSE, 0, 0L);
  301.                    return 0;
  302.  
  303.           case WM_SIZE:
  304.                rect.left   =  5 * cxChar ;
  305.                rect.top    =  2 * cyChar ;
  306.                rect.right  = LOWORD (lParam) ;
  307.                rect.bottom = HIWORD (lParam) ;
  308.                return 0 ;
  309.  
  310.           case WM_PAINT:
  311.                InvalidateRect (hwnd, &rect, TRUE) ;
  312.                hdc = BeginPaint (hwnd, &ps) ;
  313.      /*static char  szTop[] = GetNlsMessage(PressButtonId); */
  314.                TextOut (hdc, 6 * cxChar, 3 * cyChar, szTop, strlen(szTop)/*38*/);
  315.                EndPaint (hwnd, &ps) ;
  316.                return 0 ;
  317.  
  318.           case WM_COMMAND:
  319.           case WM_DRAWITEM:
  320.                hCursor = SetCursor (LoadCursor (NULL, IDC_WAIT));
  321.                ShowCursor (TRUE);
  322.  
  323.                /* Produce a pop-up window for the animal: */
  324.                 switch (wParam)
  325.                         {
  326.                         case 0:
  327.                           buffer = Pooh->display (ev);
  328.                           break;
  329.                         case 1:
  330.                           buffer = Snoopy->display (ev);
  331.                           break;
  332.                         case 2:
  333.                           buffer = Lassie->display (ev);
  334.                           break;
  335.                         }
  336.               ShowCursor (FALSE);
  337.               SetCursor (hCursor);
  338.               if (ev->_major == NO_EXCEPTION) {
  339.                   MessageBox (hwnd, buffer, button[wParam].text, MB_OK);
  340.                }
  341.            InvalidateRect(hwnd,NULL,TRUE);
  342.                return 0 ;
  343.  
  344.           case WM_DESTROY:
  345.                 /* destroy local proxy objects and remote animal objects */
  346.                 if (locPooh)
  347.                    ((SOMDClientProxy *)locPooh)->somdProxyFree(ev);
  348.                 if (Pooh)
  349.                    SOMD_ObjectMgr->somdDestroyObject(ev, Pooh);
  350.                 if (locSnoopy)
  351.                    ((SOMDClientProxy *)locSnoopy)->somdProxyFree(ev);
  352.                 if (Snoopy)
  353.                    SOMD_ObjectMgr->somdDestroyObject(ev, Snoopy);
  354.                 if (locLassie)
  355.                    ((SOMDClientProxy *)locLassie)->somdProxyFree(ev);
  356.                 if (Lassie)
  357.                    SOMD_ObjectMgr->somdDestroyObject(ev, Lassie);
  358.                 SOMD_Uninit(ev);
  359.                 SOM_DestroyLocalEnvironment(ev);
  360.                 PostQuitMessage (0) ;
  361.                 return 0 ;
  362.           }
  363.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  364.      }
  365.  
  366. /*
  367.  *  Prints exception information.
  368.  */
  369.  
  370. void printEv(Environment *ev)
  371. {
  372.   char *exId, *bp;
  373.   char buffer[200];
  374.   StExcep *params;
  375.  
  376.   exId = somExceptionId(ev);
  377.   if (!exId) exId = "None";
  378.   params = (StExcep *) somExceptionValue(ev);
  379.   bp = buffer;
  380.  
  381.   wsprintf(bp, GetNlsMessage(ErrorId), (LPSTR) exId);
  382.   bp += strlen(bp);
  383.   wsprintf(bp, GetNlsMessage(MinorErrorId), (params ? params->minor : 0));
  384.   bp += strlen(bp);
  385.   wsprintf(bp, GetNlsMessage(CompletionErrorId) ,
  386.                 (LPSTR) (params ? (params->completed == YES ? "YES" :
  387.                 params->completed == NO ? "NO": "MAYBE") : "YES"));
  388.   MessageBox ((HWND)NULL, buffer, (LPSTR) "Exception",
  389.                                 MB_ICONEXCLAMATION | MB_OK);
  390.   somdExceptionFree(ev);
  391.   return;
  392. }
  393.