home *** CD-ROM | disk | FTP | other *** search
/ CD Loisirs 18 / cd.iso / PLANETE / MUDWIN / SOURCE.ZIP / PLAYER.C < prev    next >
C/C++ Source or Header  |  1994-10-04  |  24KB  |  928 lines

  1. //
  2. //  MODULE    Player.c
  3. //
  4. //  PURPOSE    Contains routines for managing MDI child windows
  5. //
  6. //  EXPORTS    Player_CreateNew()
  7. //        Player_DestroyConnection ()
  8. //
  9.  
  10. #include "defcon.h"
  11. #include "global.h"
  12. #pragma hdrstop
  13. #include <mem.h>
  14. #include <string.h>
  15.  
  16. /* note when we were last compiled */
  17. TS(Player)
  18.  
  19. BOOL            bEatMessage;
  20.  
  21. /*******************************************************************
  22.  
  23.     NAME:       Player_CreateNew
  24.  
  25.     SYNOPSIS:   Creates a new MDI child window
  26.  
  27.     ENTRY:      pPlayerData - Points to a CLIENT_DATA structure
  28.             containing data for this new session
  29.  
  30.     RETURNS:    HWND - Handle to the new child window
  31.  
  32. ********************************************************************/
  33. HWND FAR
  34. Player_CreateNew(LPCHILDINFOSTRUCT pPlayerData)
  35. {
  36.     MDICREATESTRUCT mcs;
  37.     HWND            hwnd;
  38.         char            szTitle[MAX_HOST + 20];
  39.  
  40.     mcs.szClass = szPlayer;
  41.     wsprintf(szTitle, "%s:%d", pPlayerData->szServer, pPlayerData->wPort);
  42.     mcs.szTitle = szTitle;
  43.     mcs.hOwner = hInst;
  44.     mcs.x = CW_USEDEFAULT;
  45.     mcs.y = CW_USEDEFAULT;
  46.     mcs.cx = CW_USEDEFAULT;
  47.     mcs.cy = CW_USEDEFAULT;
  48.     mcs.style = 0;
  49.     mcs.lParam = (LPARAM) pPlayerData;
  50.  
  51.     hwnd = FORWARD_WM_MDICREATE(hClient, &mcs, SendMessage);
  52.  
  53.     //  Success!
  54.     ShowWindow(hwnd,
  55.            GetPrivateInt(NULL, "MDI Create", "State", SW_SHOW));
  56.  
  57.     return hwnd;
  58.  
  59. }
  60.  
  61. /*******************************************************************
  62.  
  63.     NAME:       Player_DestroyConnection
  64.  
  65.     SYNOPSIS:   Destroys an existing connection
  66.  
  67.     ENTRY:      hwnd - The client-side window handle.
  68.  
  69.         pPlayerData - Points to a CLIENT_DATA structure
  70.             describing the connection
  71.  
  72. ********************************************************************/
  73. #pragma argsused
  74. VOID 
  75. Player_DestroyConnection(HWND hwnd,
  76.              LPCHILDINFOSTRUCT pPlayerData)
  77. {
  78.     if (pPlayerData->sReply != INVALID_SOCKET) {
  79.         ResetSocket(pPlayerData->sReply);
  80.         pPlayerData->sReply = INVALID_SOCKET;
  81.     }
  82. }
  83.  
  84. //  FUNCTION    SetWrap
  85. //
  86. //  PURPOSE    Toggles word wrapping of commands
  87. //
  88. //  PARAMETERS    hwnd - Window handle
  89. //        fWrap - Desired state of wrapping
  90. //
  91. //  COMMENTS    Since this change cannot be done by direct means,
  92. //        the function creates a new edit control, moves data
  93. //        from the old control to the new control and destroys
  94. //        the original control. Note that the function assumes
  95. //        that the child being modified is currently active.
  96.  
  97. VOID NEAR 
  98. SetWrap(HWND hWnd, BOOL fWrap)
  99. {
  100.     LONG            dws;
  101.     HWND            hwndOld, hwndNew;
  102.     RECT            r;
  103.     LPCHILDINFOSTRUCT lpCIS;
  104.  
  105.     lpCIS = CLIENTPTR(hWnd);
  106.     if (!lpCIS)
  107.         return;
  108.  
  109.     /* Change word wrap mode */
  110.     if (fWrap)
  111.         lpCIS->Flags |= CIS_WORDWRAP;
  112.     else
  113.         lpCIS->Flags &= ~CIS_WORDWRAP;
  114.  
  115.     /* Create the appropriate window style. */
  116.     /* Add horizontal scroll if not wrapping. */
  117.     dws = WS_CHILD | WS_MAXIMIZE | WS_VISIBLE | WS_VSCROLL
  118.         | ES_AUTOVSCROLL | ES_MULTILINE;
  119.     if (!fWrap)
  120.         dws |= WS_HSCROLL | ES_AUTOHSCROLL;
  121.  
  122.     /* Create a new child window */
  123.     GetClientRect(hWnd, &r);
  124.     hwndNew = CreateWindow(szEdit, NULL, dws,
  125.                    0, 0, r.right, r.bottom,
  126.                    hWnd, 0, hInst, NULL);
  127.  
  128.     /* Get handle to current edit control */
  129.     hwndOld = lpCIS->hEdit;
  130.  
  131.     /*
  132.      * Create a dummy data handle and make it the handle to the old edit
  133.      * control (hT still references the text of old control).
  134.      * Free the new window's old data handle and set it to hT (text of
  135.      * old edit control)
  136.      */
  137.     if (hwndOld) {
  138.         HANDLE hT = Edit_GetHandle(hwndOld);
  139.  
  140.         Edit_SetHandle(hwndOld, LocalAlloc(LHND, 0));
  141.         DestroyWindow(hwndOld);
  142.  
  143.         LocalFree((HANDLE) Edit_GetHandle(hwndOld));
  144.         Edit_SetHandle(hwndNew, hT);
  145.     }
  146.  
  147.     /* Point and set focus to the new edit control */
  148.     lpCIS->hEdit     = hwndNew;
  149.     SetFocus(hwndNew);
  150. }
  151.  
  152. //  FUNCTION    SetEcho
  153. //
  154. //  PURPOSE    Toggles local echoing of commands
  155. //
  156. //  PARAMETERS    hwnd - Window handle
  157. //        fEcho - Desired state of echoing
  158.  
  159. VOID NEAR 
  160. SetEcho(HWND hWnd, BOOL fEcho)
  161. {
  162.     LPCHILDINFOSTRUCT lpCIS;
  163.  
  164.     lpCIS = CLIENTPTR(hWnd);
  165.     if (!lpCIS)
  166.         return;
  167.  
  168.     /* Change word wrap mode */
  169.     if (fEcho)
  170.         lpCIS->Flags |= CIS_ECHODATA;
  171.     else
  172.         lpCIS->Flags &= ~CIS_ECHODATA;
  173. }
  174.  
  175. //  FUNCTION    SetColor
  176. //
  177. //  PURPOSE    Sets the Color used by a window
  178. //
  179. //  PARAMETERS    hwnd - Window handle
  180. //        junk - Desired Color
  181.  
  182. VOID NEAR 
  183. SetColor(HWND hwnd, LPCOLORREF lprgb)
  184. {
  185.     LPCHILDINFOSTRUCT lpCIS;
  186.     LOGFONT    lfTemp;
  187.  
  188.     lpCIS = CLIENTPTR(hwnd);
  189.     if (!lpCIS) return;
  190.  
  191.     lpCIS->Flags |= CIS_BACKGRND;
  192.     lpCIS->sis.crBgndColor = *lprgb;
  193.     if (lpCIS->sis.hbr) DeleteObject(lpCIS->sis.hbr);
  194.     lpCIS->sis.hbr = CreateSolidBrush(*lprgb);
  195.  
  196.     // redraw the client
  197.     InvalidateRect(lpCIS->hEdit, NULL, TRUE);
  198. }
  199.  
  200. //  FUNCTION    SetFont
  201. //
  202. //  PURPOSE    Sets the font used by a window
  203. //
  204. //  PARAMETERS    hwnd - Window handle
  205. //        junk - Desired font
  206.  
  207. VOID NEAR 
  208. SetFont(HWND hwnd, LPLOGFONT lpcf, LPCOLORREF lpcr)
  209. {
  210.     LPCHILDINFOSTRUCT lpCIS;
  211.     LOGFONT    lfTemp;
  212.     int nyLogPix;
  213.  
  214.     if (!(*lpcf->lfFaceName) || !lpcf->lfHeight) return;
  215.  
  216.     lpCIS = CLIENTPTR(hwnd);
  217.     if (!lpCIS) return;
  218.  
  219.     lpCIS->Flags |= CIS_FONTINFO;
  220.     lpCIS->sis.crTextColor = *lpcr;
  221.     if (lpCIS->sis.hScreenFont != NULL)
  222.         DeleteObject(lpCIS->sis.hScreenFont);
  223.     lpCIS->sis.hScreenFont = CreateFontIndirect(&lpCIS->sis.lfCurrFont);
  224.     SendMessage(lpCIS->hEdit, WM_SETFONT,
  225.             (WPARAM) lpCIS->sis.hScreenFont,
  226.             (LPARAM) MAKELONG((WORD) TRUE, 0));
  227.  
  228.     // create printer font using temp
  229.     lfTemp = lpCIS->sis.lfCurrFont;
  230.  
  231. /*** problem: hPrnDC doesn't exist here! ***/
  232.     // adjust font height for correct size on printer */
  233.     //nyLogPix = GetDeviceCaps(hPrnDC, LOGPIXELSY);
  234.     //lfTemp.lfHeight = -((cf->iPointSize/10) * nyLogPix)/72;
  235.  
  236.     if (lpCIS->sis.hScreenFont != NULL)
  237.         DeleteObject(lpCIS->sis.hScreenFont);
  238.     lpCIS->sis.hScreenFont = CreateFontIndirect(&lfTemp);
  239.  
  240.     // redraw the client
  241.     InvalidateRect(lpCIS->hEdit, NULL, TRUE);
  242. }
  243.  
  244. /*******************************************************************
  245.  
  246.     NAME:       Player_On..
  247.  
  248.     SYNOPSIS:   Handles WM_... messages
  249.  
  250. ********************************************************************/
  251.  
  252. VOID 
  253. Player_OnClose(HWND hwnd)
  254. {
  255.     LPCHILDINFOSTRUCT pPlayerData;
  256.  
  257.     if (Player_OnQueryEndSession(hwnd)) {
  258.         pPlayerData = CLIENTPTR(hwnd);
  259.         if (pPlayerData != NULL) {
  260.             //  This window has client data.  Close any open
  261.             //  socket/file handles before freeing the buffer
  262.             Player_DestroyConnection(NULL, pPlayerData);
  263.             (void) GlobalFreePtr(pPlayerData);
  264.             SetWindowLong(hwnd, 0, 0L);
  265.         }
  266.         FORWARD_WM_CLOSE(hwnd, DefMDIChildProc);
  267.     }
  268. }
  269.  
  270. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  271.  
  272. #pragma argsused
  273. VOID 
  274. Player_OnCommand(HWND hwnd,
  275.          int id,
  276.          HWND hwndCtl,
  277.          UINT codeNotify)
  278. {
  279.     LPCHILDINFOSTRUCT pPlayerData = CLIENTPTR(hwnd);
  280.  
  281.     switch (id) {
  282.  
  283.         /* this never seems to fire */
  284.     case EN_ERRSPACE:
  285.             {
  286.             INT             nLines, ichSize;
  287.             DWORD           SaveSel;
  288.  
  289.             SaveSel = Edit_GetSel(pPlayerData->hEdit);
  290.             nLines = Edit_GetLineCount(pPlayerData->hEdit);
  291.             MsgBox(MB_OK, IDS_DEBUGBOTH, (LPCSTR)"ERRSPACE", nLines,
  292.                 GetWindowTextLength(pPlayerData->hEdit));
  293.             ichSize = Edit_LineIndex(pPlayerData->hEdit, nLines << 2);
  294.             /* no cracker or replacement for the following */
  295.             SendMessage((hwndCtl), EM_SETSEL, 1, ichSize);
  296.             Edit_ReplaceSel(pPlayerData->hEdit, "");
  297.             /* no cracker or replacement for the following */
  298.             SendMessage(pPlayerData->hEdit, EM_SETSEL, 1, SaveSel);
  299.                 }
  300.         break;
  301.  
  302.     case IDM_FILE_SAVE:
  303.         if ((pPlayerData->Flags & CIS_UNTITLED) && !ChangeFile(hwnd))
  304.             break;
  305.                    SaveFile(hwnd);
  306.         break;
  307.  
  308.     case IDM_FILE_SAVEAS:
  309.         if (ChangeFile(hwnd))
  310.             SaveFile(hwnd);
  311.         break;
  312.  
  313.     case IDM_FILE_PRINT:
  314.         MsgBox(MB_OK, IDS_DEBUGBOTH, (LPCSTR)"lines/chars",
  315.                 Edit_GetLineCount(pPlayerData->hEdit),
  316.                 GetWindowTextLength(pPlayerData->hEdit));
  317.         //ShowPrintBox(hwnd);
  318.         break;
  319.  
  320.     case IDM_EDIT_COPY:
  321.         if (pPlayerData && pPlayerData->hEdit)
  322.             FORWARD_WM_COPY(pPlayerData->hEdit, SendMessage);
  323.         break;
  324.  
  325.     case IDM_EDI