home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / d / d020_1_4 / 5.ddi / DDE / SERVER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-01  |  9.9 KB  |  416 lines

  1. /***************************************************************************
  2.  
  3.     PROGRAM: SERVER
  4.  
  5.     PURPOSE: Illustrates server side of DDE conversation
  6.  
  7.     MODULES:
  8.  
  9.         SERVER.C    Window and dialog procedures.
  10.     SERVDATA.C  Maintains conversation information.
  11.         SERVDDE.C   Processes incoming and outgoing DDE messages.
  12.  
  13. ****************************************************************************/
  14.  
  15. #include "windows.h"
  16. #include "dde.h" 
  17. #include "server.h"
  18. #include "servres.h"
  19. #include <string.h>
  20. #include <stdlib.h>
  21.  
  22.  
  23.  
  24. static int     xDelta;
  25. static int     yDelta;
  26.  
  27. long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
  28. BOOL InitApplication(HANDLE);
  29. void InitAddedInstance(HANDLE, HANDLE);
  30. BOOL InitInstance(HANDLE, int);
  31. void MaybeAdviseData(int);
  32. BOOL FAR PASCAL AboutDlgProc(HWND, unsigned, WORD, LONG);
  33.     
  34.  
  35. /****************************************************************************
  36.  
  37.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  38.  
  39.     PURPOSE:  Calls initialization function, processes message loop
  40.  
  41. ****************************************************************************/
  42.  
  43. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  44. HANDLE hInstance;
  45. HANDLE hPrevInstance;
  46. LPSTR lpCmdLine;
  47. int nCmdShow;
  48. {
  49.     MSG msg;
  50.     HANDLE hAccel;
  51.  
  52.     if (!hPrevInstance)
  53.     {
  54.     if (!InitApplication(hInstance))
  55.         return (FALSE);
  56.     }
  57.     else
  58.     {
  59.         InitAddedInstance(hInstance, hPrevInstance);
  60.     }
  61.  
  62.     if (!InitInstance(hInstance, nCmdShow))
  63.         return (FALSE);
  64.  
  65.     hAccel = LoadAccelerators(hInstance, "ServerAcc");
  66.  
  67.     while (GetMessage(&msg, NULL, NULL, NULL)) 
  68.     {
  69.     if (!TranslateAccelerator(hwndMain, hAccel, &msg))
  70.     {
  71.         TranslateMessage(&msg);
  72.         DispatchMessage(&msg);
  73.     }
  74.  
  75.     }
  76.     return (msg.wParam);
  77. }
  78.  
  79.  
  80. /****************************************************************************
  81.  
  82.     FUNCTION: InitApplication(HANDLE)
  83.  
  84.     PURPOSE: Initializes window data and registers window class
  85.  
  86. ****************************************************************************/
  87.  
  88. BOOL InitApplication(hInstance)
  89. HANDLE hInstance;
  90. {
  91.     WNDCLASS  wc;
  92.  
  93.     nDoc = 1;
  94.  
  95.     wc.style = NULL;
  96.     wc.lpfnWndProc = MainWndProc;
  97.     wc.cbClsExtra = 0;
  98.     wc.cbWndExtra = 0;
  99.     wc.hInstance = hInstance;
  100.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  101.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  102.     wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
  103.     wc.lpszMenuName =  "ServerMenu";
  104.     wc.lpszClassName = "ServerWClass";
  105.  
  106.     if (!RegisterClass(&wc))
  107.     return (FALSE);
  108.  
  109.     wc.style = NULL;
  110.     wc.lpfnWndProc = DDEWndProc;
  111.     wc.cbClsExtra = 0;
  112.     wc.cbWndExtra = 0;
  113.     wc.hInstance = hInstance;
  114.     wc.hIcon = NULL;
  115.     wc.hCursor = NULL;
  116.     wc.hbrBackground = NULL;
  117.     wc.lpszMenuName =  NULL;
  118.     wc.lpszClassName = "ServerDDEWndClass";
  119.  
  120.     return (RegisterClass(&wc));
  121. }
  122.  
  123.  
  124. /****************************************************************************
  125.  
  126.     FUNCTION: InitAddedInstance
  127.  
  128.     PURPOSE:  Increment document number
  129.  
  130. ****************************************************************************/
  131. void InitAddedInstance(hInstance, hPrevInstance)
  132.     HANDLE  hInstance;
  133.     HANDLE  hPrevInstance;
  134. {
  135.     GetInstanceData(hPrevInstance, (NPSTR)&nDoc, sizeof(int));
  136.     nDoc++;
  137.     return;
  138. }
  139.  
  140.  
  141.  
  142. /****************************************************************************
  143.  
  144.     FUNCTION:  InitInstance(HANDLE, int)
  145.  
  146.     PURPOSE:  Saves instance handle, creates main window, and creates
  147.           3 child edit controls with id's 1, 2, and 3.
  148.  
  149. ****************************************************************************/
  150.  
  151. BOOL InitInstance(hInstance, nCmdShow)
  152.     HANDLE          hInstance;
  153.     int             nCmdShow;
  154. {
  155.     char        szNumber[4];
  156.     char        szCaption[20];
  157.     HDC         hDC;
  158.     PAINTSTRUCT ps;
  159.     TEXTMETRIC  tm;
  160.     int         nItem;
  161.     int         nHorzRes, nVertRes;
  162.  
  163.     InitAckTimeOut(); /* in module SERVDDE */
  164.  
  165.     hInst = hInstance;
  166.  
  167.     strcpy(szDocName, "FILE");
  168.     itoa(nDoc, szNumber, 10);
  169.     strcat(szDocName, szNumber);
  170.     strcpy(szCaption, "Server -- ");
  171.     strcat(szCaption, szDocName);
  172.  
  173.     hwndMain = CreateWindow(
  174.         "ServerWClass",
  175.         szCaption,
  176.         WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  177.         CW_USEDEFAULT,
  178.         CW_USEDEFAULT,
  179.         CW_USEDEFAULT,
  180.         CW_USEDEFAULT,
  181.         NULL,
  182.         NULL,
  183.         hInstance,
  184.         NULL
  185.     );
  186.  
  187.     if (!hwndMain)
  188.         return (FALSE);
  189.  
  190.     hDC = GetDC(hwndMain);
  191.     GetTextMetrics(hDC, (LPTEXTMETRIC)&tm);
  192.     xDelta = tm.tmAveCharWidth;
  193.     yDelta = tm.tmHeight + tm.tmExternalLeading;
  194.     nHorzRes = GetDeviceCaps(hDC, HORZRES);
  195.     nVertRes = GetDeviceCaps(hDC, VERTRES);
  196.     ReleaseDC(hwndMain, hDC);
  197.  
  198.     MoveWindow(hwndMain,
  199.     nHorzRes/2 + xDelta*(nDoc+5),
  200.         ((nDoc-1)&1)*nVertRes/2 + yDelta*nDoc,
  201.         xDelta*30, 
  202.     yDelta*12,
  203.         FALSE);
  204.  
  205.     for (nItem = 1; nItem < 4; nItem++)
  206.     {
  207.         CreateWindow("edit", NULL,
  208.         WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | WS_TABSTOP,
  209.             9*xDelta, (2*nItem-1)*yDelta, 12*xDelta, (yDelta*3)/2,
  210.         hwndMain, nItem, hInst, NULL);
  211.     }
  212.  
  213.     if (!(cfLink = RegisterClipboardFormat("Link")))
  214.     return (FALSE);
  215.  
  216.     ShowWindow(hwndMain, nCmdShow);
  217.     UpdateWindow(hwndMain);
  218.  
  219.  
  220.     return (TRUE);
  221.  
  222. }
  223.  
  224. /****************************************************************************
  225.  
  226.     FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
  227.  
  228.     PURPOSE:  Processes messages for server
  229.  
  230. ****************************************************************************/
  231.  
  232. long FAR PASCAL MainWndProc(hwnd, message, wParam, lParam)
  233. HWND hwnd;
  234. unsigned message;
  235. WORD wParam;
  236. LONG lParam;
  237. {
  238.     HDC  hDC;
  239.     PAINTSTRUCT ps;
  240.     HWND hctlItem;
  241.     int  nItem;
  242.     char szItemName[8];
  243.     FARPROC lpAboutDlgProc;
  244.  
  245.  
  246.     switch (message)
  247.     {
  248.         case WM_SETFOCUS:
  249.             SetFocus(GetDlgItem(hwnd,1));
  250.             break;
  251.  
  252.         case WM_PAINT:
  253.         hDC = BeginPaint(hwnd, &ps);
  254.             strcpy(szItemName, "Item1:");
  255.             for (nItem = 1; nItem < 4; nItem++)
  256.             {
  257.         /* display labels for the edit controls */
  258.                 TextOut(hDC, xDelta, (2*nItem-1)*yDelta, szItemName, 6);
  259.                 szItemName[4]++;
  260.             }
  261.         EndPaint(hwnd, &ps);
  262.             break;
  263.  
  264.         case WM_COMMAND:
  265.             switch (wParam) 
  266.             {
  267.                case IDM_COPY:
  268.            hctlItem = GetFocus();
  269.            for (nItem = 1; nItem <= 3; nItem++)
  270.            {
  271.                if (hctlItem == GetDlgItem(hwnd, nItem))
  272.                {
  273.                DoEditCopy(nItem);
  274.                break;
  275.                }
  276.            }
  277.            break;
  278.  
  279.            case ID_TAB:
  280.            case ID_SHIFT_TAB:
  281.            if (IsChild(hwndMain, GetFocus()))
  282.            {
  283.                nItem = GetWindowWord(GetFocus(), GWW_ID);
  284.                if (wParam == ID_TAB)
  285.                {
  286.                if (nItem++ == 3)
  287.                    nItem = 1;
  288.                }
  289.                else
  290.                {
  291.                if (nItem-- == 1)
  292.                    nItem = 3;
  293.                }
  294.            }
  295.            else
  296.            {
  297.                nItem = 1;
  298.            }
  299.            SetFocus(GetDlgItem(hwndMain, nItem));
  300.            break;
  301.  
  302.            case 1:
  303.                case 2:
  304.                case 3:
  305.                   if (HIWORD(lParam)==EN_KILLFOCUS)
  306.                   {
  307.                      hctlItem = GetDlgItem(hwnd, wParam);
  308.                      if (SendMessage(hctlItem, EM_GETMODIFY, 0, 0L))
  309.                      {
  310.             MaybeAdviseData(wParam);
  311.                         SendMessage(hctlItem, EM_SETMODIFY, 0, 0L);
  312.                      }
  313.                   }
  314.                   break;
  315.  
  316.         case IDM_ABOUT:
  317.             lpAboutDlgProc = MakeProcInstance(AboutDlgProc, hInst);
  318.             DialogBox(hInst,
  319.             "About",
  320.             hwndMain,
  321.             lpAboutDlgProc);
  322.             FreeProcInstance(lpAboutDlgProc);
  323.             break;
  324.         }
  325.             break;
  326.  
  327.         case WM_DDE_INITIATE:
  328.             ServerInitiate((HWND)wParam, lParam);
  329.             break;
  330.  
  331.         case WM_DESTROY:
  332.  
  333.             /* Terminate all DDE conversations before destroying 
  334.                client window */
  335.         TerminateConversations();
  336.             PostQuitMessage(0);
  337.             break;
  338.  
  339.         default:
  340.             return (DefWindowProc(hwnd, message, wParam, lParam));
  341.     }
  342.     return (0L);
  343. }
  344.  
  345.  
  346.  
  347. /****************************************************************************
  348.  
  349.     FUNCTION: MaybeAdviseData
  350.  
  351.     PURPOSE:  Send data to all clients for which a hot or warm link
  352.           has been established for the specified item.
  353.  
  354. ****************************************************************************/
  355. void MaybeAdviseData(nItem)
  356.     int   nItem;
  357. {
  358.     HWND hwndServerDDE;
  359.     char szItemName[ITEM_NAME_MAX_SIZE+1];
  360.     char szItemValue[ITEM_VALUE_MAX_SIZE+1];
  361.     BOOL bDeferUpdate;
  362.     BOOL bAckRequest;
  363.  
  364.     hwndServerDDE = NULL;
  365.     
  366.     while (1)
  367.     {
  368.     if (hwndServerDDE = GetNextAdvise(hwndServerDDE, nItem))
  369.         {
  370.         GetAdviseData(hwndServerDDE,
  371.                 nItem,
  372.                 szItemName,
  373.                 szItemValue,
  374.                 &bDeferUpdate,
  375.                 &bAckRequest); 
  376.  
  377.         SendData(hwndServerDDE,
  378.         GetHwndClientDDE(hwndServerDDE),
  379.                 szItemName,
  380.                 szItemValue,
  381.                 bDeferUpdate,
  382.                 bAckRequest);
  383.         }
  384.         else return;
  385.     }
  386. }
  387.  
  388.  
  389. /****************************************************************************
  390.  
  391.     FUNCTION: AboutDlgProc(HWND, unsigned, WORD, LONG)
  392.  
  393.     PURPOSE:  Processes messages for "About" dialog box
  394.  
  395. ****************************************************************************/
  396.  
  397. BOOL FAR PASCAL AboutDlgProc(hDlg, message, wParam, lParam)
  398.     HWND hDlg;
  399.     unsigned message;
  400.     WORD wParam;
  401.     LONG lParam;
  402. {
  403.     switch (message) {
  404.     case WM_INITDIALOG:
  405.         return (TRUE);
  406.  
  407.     case WM_COMMAND:
  408.         if (wParam == IDOK || wParam == IDCANCEL) {
  409.         EndDialog(hDlg, TRUE);
  410.         return (TRUE);
  411.         }
  412.         break;
  413.     }
  414.     return (FALSE);
  415. }
  416.