home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / tutsamp / perclien / textwin.cpp < prev    next >
C/C++ Source or Header  |  1997-08-05  |  14KB  |  515 lines

  1. /*+==========================================================================
  2.   File:      TEXTWIN.CPP
  3.  
  4.   Summary:   Implementation of the CTextWin C++ object that encapsulates
  5.              the the text edit control window used to edit text pages.
  6.  
  7.   Classes:   CTextWin.
  8.  
  9.   Functions: none.
  10.  
  11.   Origin:    5-24-97: atrent - Editor Inheritance from CMsgLog in
  12.              APPUTIL.CPP.
  13.  
  14. ----------------------------------------------------------------------------
  15.   This file is part of the Microsoft COM Tutorial Code Samples.
  16.  
  17.   Copyright (C) Microsoft Corporation, 1997.  All rights reserved.
  18.  
  19.   This source code is intended only as a supplement to Microsoft
  20.   Development Tools and/or on-line documentation.  See these other
  21.   materials for detailed information regarding Microsoft code samples.
  22.  
  23.   THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  24.   KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  25.   IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  26.   PARTICULAR PURPOSE.
  27. ==========================================================================+*/
  28.  
  29. /*---------------------------------------------------------------------------
  30.   We include WINDOWS.H for all Win32 applications.
  31.   We include TCHAR.H for general Unicode/Ansi prototype of utility
  32.     functions like wvsprintf.
  33.   We include APPUTIL.H because we will be building this application using
  34.     the convenient Virtual Window and Dialog classes and other
  35.     utility functions in the APPUTIL Library (ie, APPUTIL.LIB).
  36.   We include TEXTWIN.H for the defines and the CTextWin class
  37.     declaration needed in TEXTWIN.
  38. ---------------------------------------------------------------------------*/
  39. #include <windows.h>
  40. #include <tchar.h>
  41. #include <apputil.h>
  42. #include "textwin.h"
  43.  
  44.  
  45. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  46.   Method:   CTextWin::CTextWin
  47.  
  48.   Summary:  CTextWin Constructor.
  49.  
  50.   Args:     .
  51.  
  52.   Returns:  .
  53. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  54. CTextWin::CTextWin()
  55. {
  56.   m_hInst = NULL;
  57.   m_hWnd = NULL;
  58.   m_wWidth = 0;
  59.   m_wHeight = 0;
  60. }
  61.  
  62.  
  63. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  64.   Method:   CMainWindow::~CMainWindow
  65.  
  66.   Summary:  CMainWindow Destructor.
  67.  
  68.   Args:     .
  69.  
  70.   Returns:  .
  71. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  72. CTextWin::~CTextWin()
  73. {
  74. }
  75.  
  76.  
  77. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  78.   Method:   CTextWin::Init
  79.  
  80.   Summary:  Initializes the Text Edit Control Window. Creates this window
  81.             as a child window and fits it to fill the client area of the
  82.             specified parent window.
  83.  
  84.   Args:     HINSTANCE hInst,
  85.               Instance handle of the application.
  86.             HWND hWndparent,
  87.               Window handle for the parent window of the listbox.
  88.             HWND* phWndEdit)
  89.               Address of variable that is assigned the window handle
  90.               of the new text window (edit control window).
  91.  
  92.   Returns:  HRESULT
  93.               Standard result code. NOERROR for success.
  94. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  95. HRESULT CTextWin::Init(
  96.           HINSTANCE hInst,
  97.           HWND hWndParent,
  98.           HWND* phWndEdit)
  99. {
  100.   HRESULT hr = E_FAIL;
  101.   BOOL bResult = FALSE;
  102.   HWND hWnd;
  103.   RECT rect;
  104.   DWORD dwStyle = WS_VISIBLE | WS_CHILD | WS_VSCROLL | ES_MULTILINE
  105.                   | ES_LEFT | ES_AUTOVSCROLL | ES_AUTOHSCROLL;
  106.  
  107.   if (IsWindow(hWndParent))
  108.   {
  109.     GetClientRect(hWndParent, &rect);
  110.  
  111.     // Create the edit control window.
  112.     hWnd = ::CreateWindowEx(
  113.                0,                // Extended Window Style
  114.                TEXT("EDIT"),     // Class Name
  115.                NULL,             // Window Title
  116.                dwStyle,          // The window style
  117.                0,                // (x,y)=Upper left of Parent window
  118.                0,
  119.                rect.right,       // Width; Fill Client Window
  120.                rect.bottom,      // Height
  121.                hWndParent,       // Parent Window Handle
  122.                (HMENU) IDC_TEXTWIN, // No menu but rig ID for notifications.
  123.                hInst,            // App Instance Handle
  124.                NULL);            // Window Creation Data
  125.     if (NULL != hWnd)
  126.     {
  127.       // Set keyboard focus to this TextWin edit control.
  128.       SetFocus(hWnd);
  129.       // Remember the instance of this application.
  130.       m_hInst = hInst;
  131.       // Remember the window handle of this edit window.
  132.       m_hWnd = hWnd;
  133.       // Assign the window handle to the caller's variable.
  134.       if (NULL != phWndEdit)
  135.         *phWndEdit = hWnd;
  136.       // Return success.
  137.       hr = NOERROR;
  138.     }
  139.   }
  140.  
  141.   return (hr);
  142. }
  143.  
  144.  
  145. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  146.   Method:   CTextWin::GetLength
  147.  
  148.   Summary:  Gets the length of the text content in the edit control.
  149.  
  150.   Args:     INT* piTextLength
  151.               Address of INT variable that is assigned the length.
  152.  
  153.   Returns:  HRESULT
  154.               Standard result code. NOERROR for success.
  155. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  156. HRESULT CTextWin::GetLength(
  157.           INT* piTextLength)
  158. {
  159.   HRESULT hr = E_POINTER;
  160.   INT iLen;
  161.  
  162.   if (NULL != piTextLength)
  163.   {
  164.     // Set output in case of error.
  165.     *piTextLength = 0;
  166.  
  167.     hr = E_FAIL;
  168.     if (IsWindow(m_hWnd))
  169.     {
  170.       iLen = ::SendMessage(m_hWnd, WM_GETTEXTLENGTH, 0, 0);
  171.  
  172.       if (iLen >= 0)
  173.       {
  174.         // Assign caller's variable.
  175.         *piTextLength = iLen;
  176.         hr = NOERROR;
  177.       }
  178.     }
  179.   }
  180.  
  181.   return hr;
  182. }
  183.  
  184.  
  185. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  186.   Method:   CTextWin::GetText
  187.  
  188.   Summary:  Gets the text content of the edit control.
  189.  
  190.   Args:     WCHAR* pwszText
  191.               Address of output string buffer that is assigned the text.
  192.  
  193.   Returns:  HRESULT
  194.               Standard result code. NOERROR for success.
  195. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  196. HRESULT CTextWin::GetText(
  197.           WCHAR* pwszText)
  198. {
  199.   HRESULT hr = E_POINTER;
  200.   INT iTextLength;
  201.  
  202.   if (NULL != pwszText)
  203.   {
  204.     // Set output in case of error.
  205.     pwszText[0] = 0;
  206.  
  207.     if (IsWindow(m_hWnd))
  208.     {
  209.       hr = GetLength(&iTextLength);
  210.       if (SUCCEEDED(hr) && iTextLength >= 0)
  211.       {
  212. #if defined(UNICODE)
  213.         ::SendMessage(
  214.             m_hWnd,
  215.             WM_GETTEXT,
  216.             iTextLength+1,
  217.             (LPARAM) pwszText);
  218. #else
  219.         CHAR* pszText;
  220.  
  221.         // Allocate some temporary space for the text.
  222.         pszText = new CHAR[iTextLength+2];
  223.         if (NULL != pszText)
  224.         {
  225.           // Get the text from the the edit control.
  226.           ::SendMessage(
  227.               m_hWnd,
  228.               WM_GETTEXT,
  229.               iTextLength+1,
  230.               (LPARAM) pszText);
  231.           AnsiToUc(pszText, pwszText, iTextLength);
  232.           // Delete the temporary text buffer.
  233.           delete [] pszText;
  234.         }
  235.         else
  236.           hr = E_OUTOFMEMORY;
  237. #endif
  238.       }
  239.     }
  240.   }
  241.  
  242.   return hr;
  243. }
  244.  
  245.  
  246. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  247.   Method:   CTextWin::PutText
  248.  
  249.   Summary:  Puts text content into the edit control. Also
  250.             sets keyboard focus to the TextWin edit control.
  251.  
  252.   Args:     WCHAR* pwszText
  253.               Pointer to Zero-terminated wide character string.
  254.             INT iTextLength)
  255.               The length of the text data in characters including the
  256.               terminating 0.
  257.  
  258.   Returns:  HRESULT
  259.               Standard result code. NOERROR for success.
  260. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  261. HRESULT CTextWin::PutText(
  262.           WCHAR* pwszText,
  263.           INT iTextLength)
  264. {
  265.   HRESULT hr = E_POINTER;
  266.   LRESULT lr;
  267.  
  268.   if (IsWindow(m_hWnd) && NULL != pwszText)
  269.   {
  270. #if defined(UNICODE)
  271.     // Send the text to the edit control.
  272.     lr = ::SendMessage(
  273.              m_hWnd,
  274.              WM_SETTEXT,
  275.              0,
  276.              (LPARAM) pwszText);
  277.     hr = lr ? NOERROR : E_FAIL;
  278. #else
  279.     CHAR* pszText;
  280.  
  281.     // Allocate some temporary space for the text.
  282.     pszText = new CHAR[iTextLength+2];
  283.     if (NULL != pszText)
  284.     {
  285.       UcToAnsi(pwszText, pszText, 0);
  286.       // Send the text to the edit control.
  287.       lr = ::SendMessage(
  288.                m_hWnd,
  289.                WM_SETTEXT,
  290.                0,
  291.                (LPARAM) pszText);
  292.       hr = lr ? NOERROR : E_FAIL;
  293.       // Delete the temporary text buffer.
  294.       delete [] pszText;
  295.     }
  296.     else
  297.       hr = E_OUTOFMEMORY;
  298. #endif
  299.   }
  300.  
  301.   return hr;
  302. }
  303.  
  304.  
  305. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  306.   Method:   CTextWin::Resize
  307.  
  308.   Summary:  Resizes the edit control to a new width and height.  Called
  309.             during the parent window's WM_SIZE to fit the control to the
  310.             client area of the parent window.  It only honors this request
  311.             if it is an integral child window.
  312.  
  313.   Args:     INT nWidth
  314.               New width in pixels of the edit control.
  315.             INT nHeight
  316.               New height in pixels of the edit control.
  317.  
  318.   Returns:  HRESULT
  319.               Standard result code. NOERROR for success.
  320. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  321. HRESULT CTextWin::Resize(
  322.           INT nWidth,
  323.           INT nHeight)
  324. {
  325.   HRESULT hr = E_POINTER;
  326.   BOOL bResult = FALSE;
  327.  
  328.   if (IsWindow(m_hWnd))
  329.   {
  330.     bResult = ::MoveWindow(m_hWnd, 0, 0, nWidth, nHeight, TRUE);
  331.  
  332.     hr = bResult ? NOERROR : E_FAIL;
  333.   }
  334.  
  335.   return hr;
  336. }
  337.  
  338.  
  339. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  340.   Method:   CTextWin::Clear
  341.  
  342.   Summary:  Clears/Deletes all text content in the edit control. The
  343.             content is cut to the windows clipboard.
  344.  
  345.   Args:     void
  346.  
  347.   Returns:  HRESULT
  348.               Standard result code. NOERROR for success.
  349. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  350. HRESULT CTextWin::Clear(
  351.           void)
  352. {
  353.   HRESULT hr;
  354.  
  355.   hr = EditSelectAll();
  356.   if (SUCCEEDED(hr))
  357.     hr = EditDelete();
  358.  
  359.   return hr;
  360. }
  361.  
  362.  
  363. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  364.   Method:   CTextWin::EditUndo
  365.  
  366.   Summary:  Tell text edit control to undo last operation.
  367.  
  368.   Args:     void
  369.  
  370.   Returns:  HRESULT
  371.               Standard result code. NOERROR for success.
  372. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  373. HRESULT CTextWin::EditUndo(
  374.           void)
  375. {
  376.   HRESULT hr = E_FAIL;
  377.  
  378.   if (IsWindow(m_hWnd))
  379.   {
  380.     ::SendMessage(m_hWnd, WM_UNDO, 0, 0);
  381.     hr = NOERROR;
  382.   }
  383.  
  384.   return hr;
  385. }
  386.  
  387.  
  388. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  389.   Method:   CTextWin::EditSelectAll
  390.  
  391.   Summary:  Tell text edit control to select all text in it.
  392.  
  393.   Args:     void
  394.  
  395.   Returns:  HRESULT
  396.               Standard result code. NOERROR for success.
  397. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  398. HRESULT CTextWin::EditSelectAll(
  399.           void)
  400. {
  401.   HRESULT hr = E_FAIL;
  402.  
  403.   if (IsWindow(m_hWnd))
  404.   {
  405.     ::SendMessage(m_hWnd, EM_SETSEL, 0, -1);
  406.     hr = NOERROR;
  407.   }
  408.  
  409.   return hr;
  410. }
  411.  
  412.  
  413. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  414.   Method:   CTextWin::EditCut
  415.  
  416.   Summary:  Tell edit control to cut currently selected text to the
  417.             windows clipboard.
  418.  
  419.   Args:     void
  420.  
  421.   Returns:  HRESULT
  422.               Standard result code. NOERROR for success.
  423. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  424. HRESULT CTextWin::EditCut(
  425.           void)
  426. {
  427.   HRESULT hr = E_FAIL;
  428.  
  429.   if (IsWindow(m_hWnd))
  430.   {
  431.     ::SendMessage(m_hWnd, WM_CUT, 0, 0);
  432.     hr = NOERROR;
  433.   }
  434.  
  435.   return hr;
  436. }
  437.  
  438.  
  439. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  440.   Method:   CTextWin::EditCopy
  441.  
  442.   Summary:  Tell edit control to copy currently selected text to the
  443.             windows clipboard.
  444.  
  445.   Args:     void
  446.  
  447.   Returns:  HRESULT
  448.               Standard result code. NOERROR for success.
  449. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  450. HRESULT CTextWin::EditCopy(
  451.           void)
  452. {
  453.   HRESULT hr = E_FAIL;
  454.  
  455.   if (IsWindow(m_hWnd))
  456.   {
  457.     ::SendMessage(m_hWnd, WM_COPY, 0, 0);
  458.     hr = NOERROR;
  459.   }
  460.  
  461.   return hr;
  462. }
  463.  
  464.  
  465. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  466.   Method:   CTextWin::EditPaste
  467.  
  468.   Summary:  Tell edit control to paste the content of the windows
  469.             clipboard into the text edit control at the current
  470.             caret location.
  471.  
  472.   Args:     void
  473.  
  474.   Returns:  HRESULT
  475.               Standard result code. NOERROR for success.
  476. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  477. HRESULT CTextWin::EditPaste(
  478.           void)
  479. {
  480.   HRESULT hr = E_FAIL;
  481.  
  482.   if (IsWindow(m_hWnd))
  483.   {
  484.     ::SendMessage(m_hWnd, WM_PASTE, 0, 0);
  485.     hr = NOERROR;
  486.   }
  487.  
  488.   return hr;
  489. }
  490.  
  491.  
  492. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  493.   Method:   CTextWin::EditDelete
  494.  
  495.   Summary:  Tell the edit control to delete the currently selected text.
  496.  
  497.   Args:     void
  498.  
  499.   Returns:  HRESULT
  500.               Standard result code. NOERROR for success.
  501. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  502. HRESULT CTextWin::EditDelete(
  503.           void)
  504. {
  505.   HRESULT hr = E_FAIL;
  506.  
  507.   if (IsWindow(m_hWnd))
  508.   {
  509.     ::SendMessage(m_hWnd, WM_CLEAR, 0, 0);
  510.     hr = NOERROR;
  511.   }
  512.  
  513.   return hr;
  514. }
  515.