home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / WINUTIL.CPP < prev   
Encoding:
C/C++ Source or Header  |  1996-10-30  |  6.5 KB  |  221 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1997 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12. #include <malloc.h>
  13.  
  14. #ifdef AFX_CORE2_SEG
  15. #pragma code_seg(AFX_CORE2_SEG)
  16. #endif
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // Other helpers
  25.  
  26. BOOL AFXAPI AfxCustomLogFont(UINT nIDS, LOGFONT* pLogFont)
  27. {
  28.     ASSERT(pLogFont != NULL);
  29.     ASSERT(nIDS != 0);
  30.  
  31.     TCHAR szFontInfo[256];
  32.     if (!AfxLoadString(nIDS, szFontInfo))
  33.         return FALSE;
  34.  
  35.     LPTSTR lpszSize = _tcschr(szFontInfo, '\n');
  36.     if (lpszSize != NULL)
  37.     {
  38.         // get point size and convert to pixels
  39.         pLogFont->lfHeight = _ttoi(lpszSize+1);
  40.         pLogFont->lfHeight =
  41.             MulDiv(pLogFont->lfHeight, afxData.cyPixelsPerInch, 72);
  42.         *lpszSize = '\0';
  43.     }
  44.     lstrcpyn(pLogFont->lfFaceName, szFontInfo, LF_FACESIZE);
  45.     return TRUE;
  46. }
  47.  
  48. BOOL AFXAPI _AfxIsComboBoxControl(HWND hWnd, UINT nStyle)
  49. {
  50.     if (hWnd == NULL)
  51.         return FALSE;
  52.     // do cheap style compare first
  53.     if ((UINT)(::GetWindowLong(hWnd, GWL_STYLE) & 0x0F) != nStyle)
  54.         return FALSE;
  55.  
  56.     // do expensive classname compare next
  57.     static const TCHAR szComboBox[] = _T("combobox");
  58.     TCHAR szCompare[_countof(szComboBox)+1];
  59.     ::GetClassName(hWnd, szCompare, _countof(szCompare));
  60.     return lstrcmpi(szCompare, szComboBox) == 0;
  61. }
  62.  
  63. BOOL AFXAPI _AfxCompareClassName(HWND hWnd, LPCTSTR lpszClassName)
  64. {
  65.     ASSERT(::IsWindow(hWnd));
  66.     TCHAR szTemp[32];
  67.     ::GetClassName(hWnd, szTemp, _countof(szTemp));
  68.     return lstrcmpi(szTemp, lpszClassName) == 0;
  69. }
  70.  
  71. HWND AFXAPI _AfxChildWindowFromPoint(HWND hWnd, POINT pt)
  72. {
  73.     ASSERT(hWnd != NULL);
  74.  
  75.     // check child windows
  76.     ::ClientToScreen(hWnd, &pt);
  77.     HWND hWndChild = ::GetWindow(hWnd, GW_CHILD);
  78.     for (; hWndChild != NULL; hWndChild = ::GetWindow(hWndChild, GW_HWNDNEXT))
  79.     {
  80.         if (_AfxGetDlgCtrlID(hWndChild) != (WORD)-1 &&
  81.             (::GetWindowLong(hWndChild, GWL_STYLE) & WS_VISIBLE))
  82.         {
  83.             // see if point hits the child window
  84.             CRect rect;
  85.             ::GetWindowRect(hWndChild, rect);
  86.             if (rect.PtInRect(pt))
  87.                 return hWndChild;
  88.         }
  89.     }
  90.  
  91.     return NULL;    // not found
  92. }
  93.  
  94. void AFXAPI AfxSetWindowText(HWND hWndCtrl, LPCTSTR lpszNew)
  95. {
  96.     int nNewLen = lstrlen(lpszNew);
  97.     TCHAR szOld[256];
  98.     // fast check to see if text really changes (reduces flash in controls)
  99.     if (nNewLen > _countof(szOld) ||
  100.         ::GetWindowText(hWndCtrl, szOld, _countof(szOld)) != nNewLen ||
  101.         lstrcmp(szOld, lpszNew) != 0)
  102.     {
  103.         // change it
  104.         ::SetWindowText(hWndCtrl, lpszNew);
  105.     }
  106. }
  107.  
  108. void AFXAPI AfxDeleteObject(HGDIOBJ* pObject)
  109. {
  110.     ASSERT(pObject != NULL);
  111.     if (*pObject != NULL)
  112.     {
  113.         DeleteObject(*pObject);
  114.         *pObject = NULL;
  115.     }
  116. }
  117.  
  118. void AFXAPI AfxCancelModes(HWND hWndRcvr)
  119. {
  120.     // if we receive a message destined for a window, cancel any combobox
  121.     //  popups that could be in toolbars or dialog bars
  122.     HWND hWndCancel = ::GetFocus();
  123.     if (hWndCancel == NULL)
  124.         return;     // nothing to cancel
  125.  
  126.     if (hWndCancel == hWndRcvr)
  127.         return;     // let input go to window with focus
  128.  
  129.     // focus is in part of a combo-box
  130.     if (!_AfxIsComboBoxControl(hWndCancel, (UINT)CBS_DROPDOWNLIST))
  131.     {
  132.         // check as a dropdown
  133.         hWndCancel = ::GetParent(hWndCancel);   // parent of edit is combo
  134.         if (hWndCancel == hWndRcvr)
  135.             return;     // let input go to part of combo
  136.  
  137.         if (!_AfxIsComboBoxControl(hWndCancel, (UINT)CBS_DROPDOWN))
  138.             return;     // not a combo-box that is active
  139.     }
  140.  
  141.     // combo-box is active, but if receiver is a popup, do nothing
  142.     if (hWndRcvr != NULL &&
  143.       (::GetWindowLong(hWndRcvr, GWL_STYLE) & WS_CHILD) != 0 &&
  144.       ::GetParent(hWndRcvr) == ::GetDesktopWindow())
  145.         return;
  146.  
  147.     // finally, we should cancel the mode!
  148.     ::SendMessage(hWndCancel, CB_SHOWDROPDOWN, FALSE, 0L);
  149. }
  150.  
  151. #ifdef _MAC
  152. // The EqualRect API provided by the Windows Portability Libraries for Macintosh
  153. // returns true if the input rectangles are both empty, even if they do not have
  154. // identical coordinates. _AfxIdenticalRect only returns true if the rectangles
  155. // have exactly identical coordinates.
  156. BOOL AFXAPI _AfxIdenticalRect(LPCRECT lpRectOne, LPCRECT lpRectTwo)
  157. {
  158.     ASSERT(sizeof(RECT) == 4 * sizeof(LONG));   // require no gaps between fields
  159.     return memcmp(lpRectOne, lpRectTwo, sizeof(RECT)) == 0;
  160. }
  161. #endif //_MAC
  162.  
  163. void AFXAPI AfxGlobalFree(HGLOBAL hGlobal)
  164. {
  165.     if (hGlobal == NULL)
  166.         return;
  167.  
  168.     // avoid bogus warning error messages from various debugging tools
  169.     ASSERT(GlobalFlags(hGlobal) != GMEM_INVALID_HANDLE);
  170.     UINT nCount = GlobalFlags(hGlobal) & GMEM_LOCKCOUNT;
  171.     while (nCount--)
  172.         GlobalUnlock(hGlobal);
  173.  
  174.     // finally, really free the handle
  175.     GlobalFree(hGlobal);
  176. }
  177.  
  178. /////////////////////////////////////////////////////////////////////////////
  179. // Special new handler for safety pool on temp maps
  180.  
  181. #ifndef _AFX_PORTABLE
  182.  
  183. #define MIN_MALLOC_OVERHEAD 4   // LocalAlloc or other overhead
  184.  
  185. int AFX_CDECL AfxCriticalNewHandler(size_t nSize)
  186.     // nSize is already rounded
  187. {
  188.     // called during critical memory allocation
  189.     //  free up part of the app's safety cache
  190.     TRACE0("Warning: Critical memory allocation failed!\n");
  191.     _AFX_THREAD_STATE* pThreadState = AfxGetThreadState();
  192.     if (pThreadState != NULL && pThreadState->m_pSafetyPoolBuffer != NULL)
  193.     {
  194.         size_t nOldBufferSize = _msize(pThreadState->m_pSafetyPoolBuffer);
  195.         if (nOldBufferSize <= nSize + MIN_MALLOC_OVERHEAD)
  196.         {
  197.             // give it all up
  198.             TRACE0("Warning: Freeing application's memory safety pool!\n");
  199.             free(pThreadState->m_pSafetyPoolBuffer);
  200.             pThreadState->m_pSafetyPoolBuffer = NULL;
  201.         }
  202.         else
  203.         {
  204.             BOOL bEnable = AfxEnableMemoryTracking(FALSE);
  205.             _expand(pThreadState->m_pSafetyPoolBuffer,
  206.                 nOldBufferSize - (nSize + MIN_MALLOC_OVERHEAD));
  207.             AfxEnableMemoryTracking(bEnable);
  208.             TRACE3("Warning: Shrinking safety pool from %d to %d to satisfy request of %d bytes.\n",
  209.                  nOldBufferSize, _msize(pThreadState->m_pSafetyPoolBuffer), nSize);
  210.         }
  211.         return 1;       // retry it
  212.     }
  213.  
  214.     TRACE0("ERROR: Critical memory allocation from safety pool failed!\n");
  215.     AfxThrowMemoryException();      // oops
  216.     return 0;
  217. }
  218. #endif // !_AFX_PORTABLE
  219.  
  220. /////////////////////////////////////////////////////////////////////////////
  221.