home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / som / somd / cpp / event / eventcli.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  10.5 KB  |  293 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.    EVENTCLI.C -- DSOM Sample Program
  38.   ----------------------------------------*/
  39.  
  40. #include <windows.h>
  41. #include <string.h>
  42. #include <malloc.h>
  43. #include <stdio.h>
  44. #include <errno.h>
  45.  
  46. #include <somd.xh>
  47. #include <orb.xh>
  48. #include "cell.xh"
  49.  
  50. /************************************************************
  51. 19982
  52. Given file name, return a file name with the prefix for
  53. the temporary directory . Return NULL if insufficient buffer space.
  54. Note: This implementation returns pointer to a static variable.
  55. *************************************************************/
  56. #define TEMP_FILE_NAME_SIZE 256
  57. static char tempFileName[TEMP_FILE_NAME_SIZE];
  58. char *TmpFileName(char *fname){
  59. int len;
  60.     len = GetEnvironmentVariable("TEMP", tempFileName, TEMP_FILE_NAME_SIZE);
  61.     if ( len == 0 || (len+strlen(fname)+1 > TEMP_FILE_NAME_SIZE) )
  62.         return NULL;
  63.     strcat(tempFileName,"\\");
  64.     strcat(tempFileName, fname);
  65.     return tempFileName;
  66. }
  67.  
  68. long APIENTRY WndProc (HWND, UINT, UINT, LONG) ;
  69.  
  70. struct {
  71.      long style ;
  72.      char *text ;
  73. }
  74. button[] = {
  75.      BS_PUSHBUTTON, "Increment cell value", /* 18953: will be translated in InitNlsMsgs */
  76.      BS_PUSHBUTTON, "Terminate the supplier",
  77. } ;
  78.  
  79. /***************************************************
  80. Messages for NLS support
  81. ****************************************************/
  82. #include "eventcli.h"
  83. #include "nlsutil.h"
  84. static char *nlsmsgs[ENDNLSID-STARTNLSID+1];
  85. #define GetNlsMessage(id) nlsmsgs[id-STARTNLSID]
  86. #define SetNlsMessage(id, str) nlsmsgs[id-STARTNLSID] = (str);
  87.  
  88. /**********************************************************
  89. 18953: NLS support
  90. This procedure must be called first to initialize all the messages
  91. to be used by irdump. These messages are obtained from the resource file.
  92. ****************************************************************/
  93. static void InitNlsMsgs(){
  94. SetNlsMessage(ObjRefErrorId, NlsMsgAlloc(ObjRefErrorId));
  95. SetNlsMessage(CreationErrorId, NlsMsgAlloc(CreationErrorId));
  96. SetNlsMessage(IinitialValueId, NlsMsgAlloc(IinitialValueId));
  97. SetNlsMessage(CurrentValueId, NlsMsgAlloc(CurrentValueId));
  98. SetNlsMessage(IncrementCellId, NlsMsgAlloc(IncrementCellId));
  99. SetNlsMessage(TerminateSupplierId, NlsMsgAlloc(TerminateSupplierId));
  100. SetNlsMessage(SampleNameId    , NlsMsgAlloc(SampleNameId    ));
  101. SetNlsMessage(ObjRefCellErrorId , NlsMsgAlloc(ObjRefCellErrorId ));
  102. SetNlsMessage(FileOpenErrorId , NlsMsgAlloc(FileOpenErrorId ));
  103.  
  104. /* Set up string for button */
  105. button[0].text = GetNlsMessage(IncrementCellId);
  106. button[1].text = GetNlsMessage(TerminateSupplierId);
  107. }
  108.  
  109. #define NUM (sizeof button / sizeof button [0])
  110.  
  111. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  112.                     LPSTR lpszCmdLine, int nCmdShow)
  113.      {
  114.      static char szAppName[] = "eventcli" ;
  115.      HWND        hwnd ;
  116.      MSG         msg ;
  117.      WNDCLASS    wndclass ;
  118.  
  119.      InitNlsMsgs();
  120.  
  121.      somEnvironmentNew();
  122.  
  123.      if (!hPrevInstance)
  124.           {
  125.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  126.           wndclass.lpfnWndProc   = WndProc ;
  127.           wndclass.cbClsExtra    = 0 ;
  128.           wndclass.cbWndExtra    = 0 ;
  129.           wndclass.hInstance     = hInstance ;
  130.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  131.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  132.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  133.           wndclass.lpszMenuName  = NULL ;
  134.           wndclass.lpszClassName = szAppName ;
  135.  
  136.           RegisterClass (&wndclass) ;
  137.           }
  138.  
  139.      hwnd = CreateWindow (szAppName, "DSOM Event Sample -- Supplier",
  140.                           WS_OVERLAPPEDWINDOW,
  141.                           CW_USEDEFAULT, CW_USEDEFAULT,
  142.                           CW_USEDEFAULT, CW_USEDEFAULT,
  143.                           NULL, NULL, hInstance, NULL) ;
  144.  
  145.      ShowWindow (hwnd, nCmdShow) ;
  146.      UpdateWindow (hwnd) ;
  147.  
  148.      while (GetMessage (&msg, NULL, 0, 0))
  149.           {
  150.           TranslateMessage (&msg) ;
  151.           DispatchMessage (&msg) ;
  152.           }
  153.      return msg.wParam ;
  154.      }
  155.  
  156. long APIENTRY WndProc (HWND hwnd, UINT message, UINT wParam,
  157.                        LONG lParam)
  158.      {
  159.      static HWND  hwndButton [NUM] ;
  160.      static int   buttonx [NUM] = { 10, 10 };
  161.      static int   buttony [NUM] = { 9, 12};
  162.      static int   cxChar, cyChar ;
  163.      int i;
  164.      HDC          hdc ;
  165.      TEXTMETRIC   tm ;
  166.      static HCURSOR hCursor ;
  167.      PAINTSTRUCT  ps ;
  168.      char buffer[100];
  169.  
  170.      static Environment NEAR ev;
  171.      static Cell *cellObj;
  172.      static long  init_val;
  173.      FILE *fileref;
  174.      string stringref;
  175.      char *fname; /* 19982 */
  176.  
  177.      switch (message)
  178.           {
  179.           case WM_CREATE:
  180.  
  181.                 /* local and DSOM initialization */
  182.                 SOM_InitEnvironment(&ev);
  183.                 SOMD_Init(&ev);
  184.                 CellNewClass(0,0);
  185.                 cellObj = (Cell *)NULL;
  186.  
  187.                 fname= TmpFileName("cell.rep");
  188.                 if (fname == NULL || !(fileref = fopen(fname 
  189.                         /* 19982 "cell.rep" */,"r"))) {
  190.                         MessageBox ((HWND)NULL,
  191.             GetNlsMessage(ObjRefCellErrorId),
  192.                                GetNlsMessage(FileOpenErrorId), MB_ICONEXCLAMATION | MB_OK);
  193.                         SendMessage(hwnd, WM_CLOSE, 0, 0L);
  194.                         return 0 ;
  195.                 }
  196.                 stringref = (string)SOMMalloc(512);
  197.                 fscanf(fileref,"%s",stringref);
  198.                 fclose(fileref);
  199.                 cellObj = (Cell *)(SOMD_ORBObject->string_to_object(&ev,stringref));
  200.                 if (!cellObj) {
  201.                         wsprintf(buffer, GetNlsMessage(ObjRefErrorId),
  202.                                     stringref);
  203.                         MessageBox ((HWND)NULL,  buffer, GetNlsMessage(CreationErrorId),
  204.                                     MB_ICONEXCLAMATION | MB_OK);
  205.                         SendMessage(hwnd, WM_CLOSE, 0, 0L);
  206.                         return 0 ;
  207.                 }
  208.                 SOMFree(stringref);
  209.                 /* initialize the cell to 10: */
  210.                 init_val = 10;
  211.                 cellObj->_set_val(&ev, init_val);
  212.  
  213.                /* Display button selections: */
  214.                hdc = GetDC (hwnd) ;
  215.                GetTextMetrics (hdc, &tm) ;
  216.                cxChar = tm.tmAveCharWidth ;
  217.                cyChar = tm.tmHeight + tm.tmExternalLeading ;
  218.                ReleaseDC (hwnd, hdc) ;
  219.                for (i = 0 ; i < NUM ; i++)
  220.                     hwndButton [i] =
  221.                        CreateWindow ("button",
  222.                                      button[i].text,
  223.                                      WS_CHILD | WS_VISIBLE | button[i].style,
  224.                                      buttonx[i] * cxChar, cyChar * buttony[i],
  225.                                      40 * cxChar,
  226.                                      7 * cyChar / 4,
  227.                                      hwnd,
  228.                                      (HMENU)i,
  229.                                      ((LPCREATESTRUCT)lParam)->hInstance,
  230.                                      NULL) ;
  231.                return 0 ;
  232.  
  233.           case WM_PAINT:
  234.  
  235.                 hdc = BeginPaint (hwnd, &ps) ;
  236.                 GetTextMetrics (hdc, &tm) ;
  237.                 cyChar = tm.tmHeight + tm.tmExternalLeading;
  238.                 cxChar = tm.tmAveCharWidth;
  239.  
  240.                 wsprintf(buffer, GetNlsMessage(IinitialValueId), init_val);
  241.                 TextOut (hdc, cxChar*10, 4*cyChar,
  242.                         (LPSTR)buffer, strlen(buffer));
  243.  
  244.                 wsprintf(buffer, GetNlsMessage(CurrentValueId),
  245.                                 cellObj->_get_val(&ev));
  246.                 TextOut (hdc, cxChar*10, 6*cyChar,
  247.                         (LPSTR)buffer, strlen(buffer));
  248.  
  249.                 EndPaint (hwnd, &ps) ;
  250.                 return 0 ;
  251.  
  252.           case WM_COMMAND:
  253.           case WM_DRAWITEM:
  254.  
  255.                 ValidateRect (hwnd, NULL) ;
  256.                 switch (wParam)
  257.                         {
  258.                         case 0: /* increment cell */
  259.  
  260.                                 SetCapture(hwnd);
  261.                                 hCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
  262.                                 ShowCursor(TRUE);
  263.  
  264.                                 cellObj->_set_val(&ev,
  265.                                       (cellObj->_get_val(&ev) + 1) );
  266.  
  267.                                 ReleaseCapture();
  268.                                 ShowCursor(FALSE);
  269.                                 SetCursor(hCursor);
  270.                                 InvalidateRect(hwnd, NULL, TRUE);
  271.                                 break;
  272.  
  273.                         case 1: /* quit */
  274.  
  275.                                 SendMessage(hwnd, WM_CLOSE, 0, 0L);
  276.                                 break;
  277.                 }
  278.                 return 0;
  279.  
  280.           case WM_DESTROY:
  281.  
  282.                 if (cellObj)
  283.                    SOMD_ObjectMgr->somdReleaseObject(&ev, cellObj);
  284.                 /* DSOM uninitialization: */
  285.                 SOMD_Uninit(&ev);
  286.                 SOM_UninitEnvironment(&ev);
  287.  
  288.                 PostQuitMessage (0) ;
  289.                 return 0 ;
  290.           }
  291.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  292.      }
  293.