home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / animbar.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.8 KB  |  323 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "stdafx.h"
  20. #include "animbar.h"
  21.  
  22. #define ANIMATION_WIDTH     20
  23. #define ANIMATION_HEIGHT    20
  24. #undef ANIMATION_PERIOD
  25. #define ANIMATION_PERIOD (4000)
  26.  
  27. CAnimation::CAnimation(CWnd *pParent ): CWnd()
  28. {
  29.     m_uAnimationClock = 0;
  30.     m_bInited = FALSE;
  31.     m_bCaptured = FALSE;
  32.     m_bDepressed = FALSE;
  33.     m_hAnimBitmap = NULL;
  34.     m_iAnimationCount = 0;
  35.  
  36.     m_iconSize.cx = ANIMATION_WIDTH;
  37.     m_iconSize.cy = ANIMATION_HEIGHT;
  38.  
  39.     CreateEx(0, NULL, _T("NSAnimation"), 
  40.              WS_CHILD|WS_VISIBLE,
  41.              0, 0, 
  42.              m_iconSize.cx + 4,
  43.              m_iconSize.cy + 4,
  44.              pParent->GetSafeHwnd(), (HMENU) 101);
  45.              
  46. //Changed AfxGetInstanceHandle to AfxGetResourceHandle to insure loading bitmaps
  47. //from the LANG DLL and not netscape.exe benito
  48.     if (!m_bInited) {
  49.         CDC *pDC = GetDC();
  50.         
  51.         m_hAnimBitmap = wfe_LoadBitmap(AfxGetResourceHandle(), pDC->m_hDC, MAKEINTRESOURCE(IDB_ANIMSMALL_0));
  52.         BITMAP bm;
  53.         GetObject( m_hAnimBitmap, sizeof(BITMAP), &bm );
  54.         m_iFrameCount = bm.bmWidth / m_iconSize.cx;
  55.  
  56.         ReleaseDC( pDC );
  57.         
  58.         m_bInited = TRUE;
  59.     }
  60. }
  61.  
  62. BEGIN_MESSAGE_MAP(CAnimation, CWnd)
  63.     ON_WM_PAINT()
  64.     ON_WM_TIMER()
  65.     ON_WM_LBUTTONDOWN()
  66.     ON_WM_LBUTTONUP()
  67.     ON_WM_MOUSEMOVE()
  68.     ON_WM_DESTROY()
  69. END_MESSAGE_MAP()
  70.  
  71. void CAnimation::OnPaint()
  72. {
  73.     CPaintDC dc(this);
  74.     RECT rect;
  75.     GetClientRect(&rect);
  76.     WFE_Draw3DButtonRect( dc.m_hDC, &rect, m_bDepressed );
  77.     AnimateIcon();
  78. }
  79.  
  80.  
  81. void CAnimation::OnTimer(UINT nID)
  82. {
  83.     CWnd::OnTimer(nID);
  84.     if (nID == WIN_ANIMATE_ICON_TIMER) {
  85.         if(m_bInited)
  86.         {
  87.             m_iAnimationCount = ( m_iAnimationCount % m_iFrameCount ) + 1;
  88.             AnimateIcon();
  89.         }
  90.     }
  91. }
  92.  
  93. void CAnimation::OnLButtonDown( UINT nFlags, CPoint point )
  94. {
  95.     RECT rect;
  96.     GetClientRect(&rect);
  97.     m_bDepressed = ::PtInRect(&rect, point);
  98.     if (m_bDepressed) {
  99.         SetCapture();
  100.         m_bCaptured = TRUE;
  101.         Invalidate();
  102.         UpdateWindow();
  103.     }
  104. }
  105.  
  106. void CAnimation::OnMouseMove( UINT nFlags, CPoint point )
  107. {
  108.     if (m_bCaptured) {
  109.         RECT rect;
  110.         GetClientRect(&rect);
  111.         BOOL bDepressed = ::PtInRect(&rect, point);
  112.         if (bDepressed != m_bDepressed) {
  113.             m_bDepressed = bDepressed;
  114.             Invalidate();
  115.             UpdateWindow();
  116.         }
  117.     }
  118. }
  119.  
  120. void CAnimation::OnLButtonUp( UINT nFlags, CPoint point )
  121. {
  122.  
  123.     if (m_bCaptured) {
  124.         ReleaseCapture();
  125.         m_bCaptured = FALSE;
  126.  
  127.         RECT rect;
  128.         GetClientRect(&rect);
  129.         BOOL bDepressed = ::PtInRect(&rect, point);
  130.         GetParentFrame()->SendMessage( WM_COMMAND, (WPARAM) ID_ANIMATION_BONK, (LPARAM) 0 );
  131.     }
  132.  
  133.     m_bDepressed = FALSE;
  134.     Invalidate();
  135.     UpdateWindow();
  136. }
  137.  
  138. void CAnimation::OnDestroy()
  139. {
  140.     if (m_hAnimBitmap)
  141.         VERIFY(DeleteObject( m_hAnimBitmap ));
  142.     // Right now, the context has already been deleted, since
  143.     // it's not a true COM object.
  144. //    m_pIMWContext->Release();
  145. }
  146.  
  147. void CAnimation::StopAnimation()
  148. {
  149.     m_iAnimationCount = 0;
  150.     if (m_uAnimationClock)
  151.         KillTimer(m_uAnimationClock);
  152.     m_uAnimationClock = 0;
  153.     AnimateIcon();
  154. }
  155.  
  156. void CAnimation::StartAnimation()
  157. {
  158.     m_iAnimationCount = 1;
  159.     m_uAnimationClock = SetTimer(WIN_ANIMATE_ICON_TIMER, ANIMATION_PERIOD/m_iFrameCount, NULL);
  160. }
  161.  
  162. void CAnimation::AnimateIcon()
  163. {
  164.     CClientDC dc(this);
  165.     CRect rect;
  166.     GetClientRect(&rect);
  167.     
  168.     // check if our application level CDC exists
  169.     if(!theApp.pIconDC) {
  170.         theApp.pIconDC = new CDC;
  171.         theApp.pIconDC->CreateCompatibleDC(&dc);
  172.     }
  173.  
  174.     HBITMAP hOldBitmap = (HBITMAP) theApp.pIconDC->SelectObject( m_hAnimBitmap );
  175.     
  176.     dc.BitBlt(  2, 2,
  177.                 m_iconSize.cx, m_iconSize.cy, 
  178.                 theApp.pIconDC, 
  179.                 m_iconSize.cx * m_iAnimationCount,      // X offset into the strip
  180.                 0, 
  181.                 SRCCOPY);
  182.  
  183.     //  Reselect the old object
  184.     theApp.pIconDC->SelectObject( hOldBitmap );
  185. }
  186.  
  187. /////////////////////////////////////////////////////////////////////////////
  188. // CAnimationBar toolbar derived class
  189.  
  190. CAnimationBar::CAnimationBar( ): CAnimationBarParent ()
  191. {
  192.     m_iconPt.x = 0;
  193.     m_iconPt.y = 0;
  194.  
  195.     m_wndAnimation = NULL;
  196. }
  197.  
  198. CAnimationBar::~CAnimationBar()
  199. {
  200.     if ( m_wndAnimation ) {
  201.         m_wndAnimation->DestroyWindow();
  202.     }
  203. }
  204.  
  205. void CAnimationBar::StartAnimation()
  206. {
  207.     if (m_wndAnimation)
  208.         m_wndAnimation->StartAnimation();
  209. }
  210.  
  211. void CAnimationBar::StopAnimation()
  212. {
  213.     if (m_wndAnimation)
  214.         m_wndAnimation->StopAnimation();
  215. }
  216.  
  217. #ifdef XP_WIN32
  218. CSize CAnimationBar::CalcFixedLayout(BOOL bStretch, BOOL bHorz)
  219. {
  220.     CSize size = CAnimationBarParent::CalcFixedLayout(bStretch, bHorz);
  221.     if (m_wndAnimation) {
  222.         CRect rectAnimation;
  223.         m_wndAnimation->GetWindowRect(&rectAnimation);
  224.  
  225.         int cy = rectAnimation.Height() + m_cyTopBorder + m_cyBottomBorder + 4;
  226.         if (size.cy < cy )
  227.             size.cy = cy;
  228.     }
  229.     return size;
  230. }
  231.  
  232. CSize CAnimationBar::CalcDynamicLayout(int nLength, DWORD dwMode )
  233. {
  234.     int nToolbarHeight = 0;
  235.     int nAnimationHeight = 0;
  236.  
  237.     CRect animationRect;
  238.  
  239.     if(m_wndAnimation)
  240.     {
  241.         m_wndAnimation->GetWindowRect(animationRect);
  242.         nAnimationHeight = animationRect.Height() + 4;
  243.     }
  244.  
  245.     if(m_pToolbar)
  246.     {
  247.         nToolbarHeight = m_pToolbar->GetHeight() + 4;
  248.     }
  249.  
  250.     return CSize(32767, 
  251.         (nToolbarHeight > nAnimationHeight) ? nToolbarHeight : nAnimationHeight);
  252.  
  253. }
  254. #endif
  255.  
  256. void CAnimationBar::PlaceToolbar(void)
  257. {
  258.     CRect rectAnimation;
  259.  
  260.     if ( m_wndAnimation ) {
  261.         m_wndAnimation->GetWindowRect(&rectAnimation);
  262.         CRect rectBar;
  263.         GetClientRect(&rectBar);
  264.  
  265.         m_iconPt.x = rectBar.Width() - rectAnimation.Width() - GetSystemMetrics(SM_CXBORDER) - 1;
  266.         m_iconPt.y = rectBar.Height() / 2 - rectAnimation.Height() / 2 + 2;
  267.  
  268.         if(m_pToolbar)
  269.         {
  270.             int nHeight = m_pToolbar->GetHeight();
  271.         //    m_pToolbar->MoveWindow(0, (rectBar.Height() - nHeight) / 2 + 1, cx - rectAnimation.Width(), nHeight, FALSE);
  272.             m_pToolbar->SetWindowPos(NULL, 0, (rectBar.Height() - nHeight) / 2 + 1, m_iconPt.x, nHeight,
  273.                 /*SWP_NOMOVE|*/ SWP_NOREDRAW);
  274.         }
  275.  
  276.     }
  277.  
  278. }
  279.  
  280. BEGIN_MESSAGE_MAP(CAnimationBar, CAnimationBarParent)
  281.     ON_WM_CREATE()
  282.     ON_WM_SIZE()
  283. END_MESSAGE_MAP()
  284.  
  285. int CAnimationBar::OnCreate( LPCREATESTRUCT lpCreateStruct )
  286. {
  287.     int res = CAnimationBarParent::OnCreate( lpCreateStruct );
  288.  
  289.     if ( res != -1 ) {
  290.         m_wndAnimation = new CAnimation( this );
  291.         res = m_wndAnimation ? 0 : -1;
  292.     }
  293.     return res;
  294. }
  295.  
  296. void CAnimationBar::OnSize( UINT nType, int cx, int cy )
  297. {
  298.     CRect rectAnimation;
  299.  
  300.     if ( nType != SIZE_MINIMIZED && m_wndAnimation ) {
  301.         m_wndAnimation->GetWindowRect(&rectAnimation);
  302.         CRect rectBar;
  303.         GetClientRect(&rectBar);
  304.         CClientDC dc( this );
  305.  
  306.         m_iconPt.x = rectBar.Width() - rectAnimation.Width() - GetSystemMetrics(SM_CXBORDER) - 1;
  307.         m_iconPt.y = rectBar.Height() / 2 - rectAnimation.Height() / 2 + 2;
  308.         m_wndAnimation->SetWindowPos(NULL, m_iconPt.x, m_iconPt.y, 0, 0, 
  309.                                      SWP_NOZORDER|SWP_NOSIZE);
  310.  
  311.         if(m_pToolbar)
  312.         {
  313.             int nHeight = m_pToolbar->GetHeight();
  314.         //    m_pToolbar->MoveWindow(0, (rectBar.Height() - nHeight) / 2 + 1, cx - rectAnimation.Width(), nHeight, FALSE);
  315.             m_pToolbar->SetWindowPos(NULL, 0, (rectBar.Height() - nHeight) / 2 + 1, m_iconPt.x, nHeight,
  316.                 /*SWP_NOMOVE|*/ SWP_NOREDRAW);
  317.         }
  318.  
  319.     }
  320.  
  321.  
  322. }
  323.