home *** CD-ROM | disk | FTP | other *** search
/ Prima Shareware 3 / DuCom_Prima-Shareware-3_cd1.bin / PROGRAMO / C / MRCE / SOURCE.ZIP / SIZEDLG.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-10  |  8.6 KB  |  272 lines

  1. // MRCEXT: Micro Focus Extension DLL for MFC 2.1+
  2. // Copyright (C)1994-5    Micro Focus Inc, 2465 East Bayshore Rd, Palo Alto, CA 94303.
  3. // 
  4. //  This program is free software; you can redistribute it and/or modify
  5. //  it under the terms of the GNU General Public License as published by
  6. //  the Free Software Foundation. In addition, you may also charge for any
  7. //  application    using MRCEXT, and are under no obligation to supply source
  8. //  code. You must accredit Micro Focus Inc in the "About Box", or banner
  9. //  of your application. 
  10. //
  11. //  This program is distributed in the hope that it will be useful,
  12. //  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. //  GNU General Public License for more details.
  15. //
  16. //  You should also have received a copy of the GNU General Public License with this
  17. //  software, also indicating additional rights you have when using MRCEXT.  
  18. //
  19. //
  20. // SIZEDLG.CPP  
  21. // $Date:   04 Dec 1995 15:24:26  $
  22. // $Revision:   1.0  $
  23. // $Author:   MRC  $
  24. //
  25. // Most of this code has been borrowed wholesale from MFC's CDialogBar(). 
  26.  
  27. #include "mrcstafx.h"
  28.  
  29. typedef struct _Rect_And_Hwnd
  30. {
  31.     CRect     m_rect;
  32.     HWND    m_hWnd;
  33. }    RECT_AND_HWND;
  34.  
  35.  
  36. #ifdef _DEBUG
  37. #undef THIS_FILE
  38. static char THIS_FILE[] = __FILE__;
  39. #endif
  40.  
  41. #define new DEBUG_NEW
  42.  
  43. const TCHAR _afxWndControlBar[] = _T("AfxControlBar");  // class for control bars
  44.  
  45.  
  46. CMRCSizeDialogBar::CMRCSizeDialogBar(int nStyle) : CMRCSizeControlBar(nStyle)
  47. {
  48.     m_GadgetResizeHandle = NULL;
  49. }
  50.  
  51.  
  52. CMRCSizeDialogBar::~CMRCSizeDialogBar()
  53. {
  54.     DestroyGadgetResizeHandle(m_GadgetResizeHandle);
  55. /*
  56.     for (int i = m_arrRect.GetUpperBound(); i >= 0; i--)
  57.         delete ((RECT_AND_HWND *)m_arrRect[i]);
  58.     m_arrRect.RemoveAll();                       */
  59.  
  60.     DestroyWindow();    // avoid PostNcDestroy problems  
  61. }
  62.  
  63.     
  64. BEGIN_MESSAGE_MAP(CMRCSizeDialogBar, CMRCSizeControlBar)
  65.     //{{AFX_MSG_MAP(CMRCSizeDialogBar)
  66.     ON_WM_ERASEBKGND()
  67.     //}}AFX_MSG_MAP
  68. END_MESSAGE_MAP()
  69.  
  70.  
  71.  
  72.  
  73. BOOL CMRCSizeDialogBar::Create(CWnd* pParentWnd, LPCTSTR lpszTemplateName,
  74.     UINT nStyle, UINT nID)
  75. {
  76.     ASSERT(pParentWnd != NULL);
  77.     ASSERT(lpszTemplateName != NULL);
  78.  
  79. // Alas, MFC doesn't export many of the debugging only functions, so we have to live without
  80. // this safety net.
  81. //#ifdef _DEBUG
  82. // dialog template must exist and be invisible with WS_CHILD set
  83. //if (!_AfxCheckDialogTemplate(lpszTemplateName, TRUE))
  84. //{
  85. //        ASSERT(FALSE);          // invalid dialog template name
  86. //        PostNcDestroy();        // cleanup if Create fails too soon
  87. //        return FALSE;
  88. //}
  89. //#endif //_DEBUG
  90.  
  91.     // allow chance to modify styles
  92.     m_dwStyle = nStyle;
  93.     CREATESTRUCT cs;
  94.     memset(&cs, 0, sizeof(cs));
  95.     cs.lpszClass = _afxWndControlBar;
  96.     cs.style = (DWORD)nStyle | WS_CHILD;
  97.     cs.hMenu = (HMENU)nID;
  98.     cs.hInstance = AfxGetInstanceHandle();
  99.     cs.hwndParent = pParentWnd->GetSafeHwnd();
  100.     if (!PreCreateWindow(cs))
  101.         return FALSE;
  102.  
  103.     // create a modeless dialog
  104.     HINSTANCE hInst = AfxFindResourceHandle(lpszTemplateName, RT_DIALOG);
  105.     AfxHookWindowCreate(this);
  106.     HWND hWnd = ::CreateDialog(hInst, lpszTemplateName,
  107.         pParentWnd->GetSafeHwnd(), NULL);
  108.     if (!AfxUnhookWindowCreate())
  109.         PostNcDestroy();        // cleanup if Create fails too soon
  110.  
  111.     if (hWnd == NULL)
  112.         return FALSE;
  113.     ASSERT(hWnd == m_hWnd);
  114.  
  115.     // dialog template MUST specify that the dialog
  116.     //  is an invisible child window
  117.     SetDlgCtrlID(nID);
  118.     CRect rect;
  119.     
  120.     
  121.     // force WS_CLIPSIBLINGS
  122.     // I also remove the titlebar. This means the resource can include a caption, which
  123.     // will be used when the frame is floating.
  124.     ModifyStyle(WS_CAPTION, WS_CLIPSIBLINGS);        
  125.     if (!ExecuteDlgInit(lpszTemplateName))
  126.         return FALSE;
  127.  
  128.     // m_sizeDefault isn't actually used by the CMRCSizeDialogBar, but since it's public, it
  129.     // seemed sensible to keep it available. Otherwise it might be difficult to get hold
  130.     // of the original size of the dialog (as specified in the resource file). 
  131.     
  132.     //GetWindowRect(&rect);
  133.     GetClientRect(&rect);
  134.     m_sizeDefault = rect.Size();    // set fixed size
  135.  
  136.     // Move to position 0,0
  137.     // NB: size not forced to zero, as this can affect resizing if bar is immediately docked
  138.     SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOZORDER|SWP_NOACTIVATE|SWP_SHOWWINDOW|SWP_NOSIZE);
  139.  
  140.     // set all the sizes to be the default after the positioning done above
  141.     m_VertDockSize     = m_sizeDefault;
  142.     m_HorzDockSize  = m_sizeDefault;
  143.     m_FloatSize     = m_sizeDefault;
  144.  
  145.     
  146.     // if auto-sizing, store the rectangles of all the child windows.
  147.     if (m_Style & SZBARF_DLGAUTOSIZE)    
  148.     {
  149.         m_GadgetResizeHandle = CreateGadgetResizeHandle(this);
  150.     }
  151.     return TRUE;
  152. }
  153.  
  154.  
  155. //--------------------------------------------------------------------------
  156. BOOL CMRCSizeDialogBar::OnEraseBkgnd(CDC* pDC)
  157. // need to paint the background...
  158. //--------------------------------------------------------------------------
  159. {
  160.     return CControlBar::OnEraseBkgnd(pDC);
  161. }
  162.  
  163.  
  164. //--------------------------------------------------------------------------
  165. void CMRCSizeDialogBar::OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler)
  166. //--------------------------------------------------------------------------
  167. {
  168.     UpdateDialogControls(pTarget, bDisableIfNoHndler);
  169. }
  170.  
  171.  
  172. //--------------------------------------------------------------------------
  173. void CMRCSizeDialogBar::OnSizedOrDocked(int cx, int cy, BOOL bFloating, int flags)
  174. //--------------------------------------------------------------------------
  175. {
  176.     CMRCSizeControlBar::OnSizedOrDocked(cx, cy, bFloating, flags);
  177.     if (cx == 0 || cy == 0)
  178.         return;
  179.     
  180. // if auto-sizing, go thru and reposition all the controls.
  181.     if (m_Style & SZBARF_DLGAUTOSIZE)    
  182.     {
  183.         ResizeGadgetsOnWindow(m_GadgetResizeHandle, cx, cy);
  184.     }
  185. }
  186.  
  187. /////////////////////////////////////////////////////////////////////////////
  188. // Gadget resizing functions - used to resize all the child windows of a dialog
  189. // bar above. Routines extracted as they may be more used in general
  190.  
  191. //---------------------------------------------------------------------------
  192. GADGETRESIZEHANDLE CreateGadgetResizeHandle(CWnd *pWnd)
  193. // For a given window, creates an array of the child windows, that can be used
  194. // for resizing later on.
  195. //---------------------------------------------------------------------------
  196. {
  197.     CPtrArray * pArray = new CPtrArray;
  198.     
  199.     // 1st element of the array stores the client rectangle of the given window
  200.     RECT_AND_HWND * pRectWnd = new RECT_AND_HWND;
  201.     pWnd->GetClientRect(&(pRectWnd->m_rect));
  202.     pRectWnd->m_hWnd == NULL;
  203.     pArray->Add(pRectWnd);
  204.     
  205.     for (HWND hWndChild = ::GetTopWindow(pWnd->m_hWnd); hWndChild != NULL;
  206.                         hWndChild = ::GetNextWindow(hWndChild, GW_HWNDNEXT))
  207.     {
  208.         RECT_AND_HWND * pRectWnd = new RECT_AND_HWND;
  209.         pRectWnd->m_hWnd = hWndChild;
  210.         ::GetWindowRect(hWndChild, &(pRectWnd->m_rect));
  211.         pWnd->ScreenToClient(&(pRectWnd->m_rect));
  212.         pArray->Add(pRectWnd);
  213.     }
  214.  
  215.     return (GADGETRESIZEHANDLE) pArray;
  216. }
  217.  
  218.  
  219. //---------------------------------------------------------------------------
  220. void DestroyGadgetResizeHandle(GADGETRESIZEHANDLE Handle)
  221. // deletes the underlying array and it's elements
  222. //---------------------------------------------------------------------------
  223. {
  224.     CPtrArray * pArray = (CPtrArray *)Handle;        
  225.     if (pArray != NULL)
  226.     {
  227.         for (int i = pArray->GetUpperBound(); i >= 0; i--)
  228.             delete ((RECT_AND_HWND *)pArray->GetAt(i));
  229.          delete pArray;
  230.     }
  231. }
  232.  
  233.  
  234. //---------------------------------------------------------------------------
  235. void ResizeGadgetsOnWindow(GADGETRESIZEHANDLE Handle, int cx, int cy)
  236. // Resizes all the gadgets in an array to match the new size.
  237. // Generally called from an OnSize routine (or the like)
  238. //---------------------------------------------------------------------------
  239. {
  240.     CPtrArray * pArray = (CPtrArray *)Handle;        
  241.     
  242.     if (pArray == NULL || cx == 0 || cy == 0)
  243.         return;
  244.  
  245.     ASSERT(pArray->GetSize() > 0);    // must be at least one element in the array
  246.  
  247. // if auto-sizing, go thru and reposition all the controls.
  248.     CSize sizeOrig = ((RECT_AND_HWND *)pArray->GetAt(0))->m_rect.Size();
  249.     for (int i = 1; i < pArray->GetSize(); i++)
  250.     {
  251.         RECT_AND_HWND * pRectWnd = (RECT_AND_HWND *)pArray->GetAt(i);
  252.         CRect newRect = pRectWnd->m_rect;
  253.         newRect.top    = newRect.top    * cy / sizeOrig.cy;
  254.         newRect.bottom = newRect.bottom * cy / sizeOrig.cy;
  255.         newRect.left   = newRect.left   * cx / sizeOrig.cx;
  256.         newRect.right  = newRect.right  * cx / sizeOrig.cx;
  257.         ::SetWindowPos(pRectWnd->m_hWnd, NULL,
  258.                      newRect.left, newRect.top, newRect.Width(), newRect.Height(),
  259.                     SWP_NOZORDER);
  260.     }
  261. }
  262.  
  263.  
  264.  
  265.  
  266.  
  267. #undef new
  268.  
  269. IMPLEMENT_DYNAMIC(CMRCSizeDialogBar, CMRCSizeControlBar)
  270.  
  271. ///////////////////////////////////////////////////////////////////////////
  272.