home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / animbar2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  14.2 KB  |  606 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 "tooltip.h"
  21. #include "animbar2.h"
  22. #include "statbar.h"
  23. #include "prefapi.h"
  24. #include "custom.h"
  25. #include "sysinfo.h"
  26.  
  27. #define ANIMATION_WIDTH     16
  28. #define ANIMATION_HEIGHT    16
  29. #define ANIMATION_WIDTH_L   32
  30. #define ANIMATION_HEIGHT_L  32
  31. #undef ANIMATION_PERIOD
  32. #define ANIMATION_PERIOD (8000)
  33.  
  34. #define IDT_BUTTONFOCUS 16411
  35. #define BUTTONFOCUS_DELAY_MS 10
  36.  
  37. #define BUTTON_WIDTH        2
  38.  
  39. // We cache the animation bitmaps for speed
  40. HBITMAP CAnimation2::m_hSmall    = NULL;
  41. HBITMAP CAnimation2::m_hBig      = NULL;
  42. ULONG   CAnimation2::m_uRefCount = 0L;
  43.  
  44. void DrawUpButton(HDC hDC, CRect & rect)
  45. {
  46.     // Highlight (the white color)
  47.     HBRUSH br = ::CreateSolidBrush(sysInfo.m_clrBtnHilite);
  48.     CRect rc(rect.left, rect.top, rect.right-1, rect.top+1);
  49.     ::FillRect(hDC, rc, br);
  50.     rc.SetRect(rect.left, rect.top, rect.left+1, rect.bottom-1);
  51.     ::FillRect(hDC, rc, br);
  52.  
  53. #ifdef _WIN32
  54.     ::DeleteObject(br);
  55.     
  56.     // Light shadow (the light gray color)
  57.     br = ::CreateSolidBrush(::GetSysColor(COLOR_3DLIGHT));
  58. #endif // _WIN32
  59.  
  60.     rc.SetRect(rect.left+1, rect.top+1, rect.right - 2 , rect.top+2);
  61.     ::FillRect(hDC, rc, br);
  62.     rc.SetRect(rect.left+1, rect.top+1, rect.left+2, rect.bottom - 2);
  63.     ::FillRect(hDC, rc, br);
  64.     ::DeleteObject(br);
  65.  
  66.     // Shadow (the dark grey color)
  67.     br = ::CreateSolidBrush(sysInfo.m_clrBtnShadow);
  68.     rc.SetRect(rect.left+1, rect.bottom - 2, rect.right-1, rect.bottom-1);
  69.     ::FillRect(hDC, rc, br);
  70.     rc.SetRect(rect.right - 2, rect.top+1, rect.right-1, rect.bottom-1);
  71.     ::FillRect(hDC, rc, br);
  72.  
  73. #ifdef _WIN32
  74.     ::DeleteObject(br);
  75.  
  76.     // Dark Shadow (the black color)
  77.     br = ::CreateSolidBrush(::GetSysColor(COLOR_3DDKSHADOW));
  78. #endif // _WIN32
  79.  
  80.     rc.SetRect(rect.left, rect.bottom - 1, rect.right, rect.bottom);
  81.     ::FillRect(hDC, rc, br);
  82.     rc.SetRect(rect.right - 1, rect.top, rect.right, rect.bottom);
  83.     ::FillRect(hDC, rc, br);
  84.     ::DeleteObject(br);
  85.  
  86. void DrawDownButton(HDC hDC, CRect & rect)
  87. {
  88.     // Highlight (the white color)
  89. #ifdef XP_WIN16
  90.     HBRUSH br = ::CreateSolidBrush(sysInfo.m_clrBtnShadow);
  91. #else // _WIN32
  92.     HBRUSH br = ::CreateSolidBrush(::GetSysColor(COLOR_3DDKSHADOW));
  93. #endif
  94.     
  95.     CRect rc(rect.left, rect.top, rect.right-1, rect.top+1);
  96.     ::FillRect(hDC, rc, br);
  97.     rc.SetRect(rect.left, rect.top, rect.left+1, rect.bottom-1);
  98.     ::FillRect(hDC, rc, br);
  99.  
  100. #ifdef _WIN32
  101.     ::DeleteObject(br);
  102.     
  103.     // Light shadow (the light gray color)
  104.     br = ::CreateSolidBrush(sysInfo.m_clrBtnShadow);
  105. #endif
  106.  
  107.     rc.SetRect(rect.left+1, rect.top+1, rect.right - 2 , rect.top+2);
  108.     ::FillRect(hDC, rc, br);
  109.     rc.SetRect(rect.left+1, rect.top+1, rect.left+2, rect.bottom - 2);
  110.     ::FillRect(hDC, rc, br);
  111.     ::DeleteObject(br);
  112.  
  113.     // Shadow (the dark grey color)
  114. #ifdef XP_WIN16
  115.     br = ::CreateSolidBrush(sysInfo.m_clrBtnHilite);
  116. #else
  117.     br = ::CreateSolidBrush(::GetSysColor(COLOR_3DLIGHT));
  118. #endif // _WIN16
  119.  
  120.     rc.SetRect(rect.left+1, rect.bottom - 2, rect.right-1, rect.bottom-1);
  121.     ::FillRect(hDC, rc, br);
  122.     rc.SetRect(rect.right - 2, rect.top+1, rect.right-1, rect.bottom-1);
  123.     ::FillRect(hDC, rc, br);
  124.  
  125. #ifdef _WIN32
  126.     ::DeleteObject(br);
  127.  
  128.     // Dark Shadow (the black color)
  129.     br = ::CreateSolidBrush(sysInfo.m_clrBtnHilite);
  130. #endif // _WIN32
  131.  
  132.     rc.SetRect(rect.left, rect.bottom - 1, rect.right, rect.bottom);
  133.     ::FillRect(hDC, rc, br);
  134.     rc.SetRect(rect.right - 1, rect.top, rect.right, rect.bottom);
  135.     ::FillRect(hDC, rc, br);
  136.     ::DeleteObject(br);
  137.     
  138. }
  139.  
  140. CAnimation2::CAnimation2( CWnd *pParent, LPUNKNOWN pUnk ): CWnd()
  141. {
  142.     m_uAnimationClock = 0;
  143.     m_bInited = FALSE;
  144.     m_bCaptured = FALSE;
  145.     m_bDepressed = FALSE;
  146.     m_hAnimBitmap = NULL;
  147.     m_iAnimationCount = 0;
  148.         
  149.     m_iconSize.cx = 0;
  150.     m_iconSize.cy = 0;
  151.  
  152.     CreateEx(0, NULL, _T("NSAnimation"), 
  153.              WS_CHILD|WS_VISIBLE,
  154.              0, 0, 0, 0,
  155.              pParent->GetSafeHwnd(), (HMENU) 101);
  156.     
  157.     if( !m_bInited ) 
  158.     {
  159.         SetBitmap();
  160.         
  161.         m_bInited = TRUE;
  162.     }
  163.  
  164. #ifdef WIN32
  165.     m_ToolTip.Create(this, TTS_ALWAYSTIP);
  166. #else
  167.     m_ToolTip.Create(this);
  168. #endif
  169.  
  170.     char *pURL;
  171.     PREF_CopyConfigString("toolbar.logo.url",&pURL);
  172.  
  173.     if( !CUST_IsCustomAnimation( &m_iFrameCount ) )
  174.     {
  175.         m_ToolTip.AddTool(this, szLoadString(IDS_NETSCAPE_TIP));
  176.         m_ToolTip.Activate(TRUE);
  177.         m_ToolTip.SetDelayTime(250);
  178.     }
  179.         
  180.     m_hFocusTimer = 0;
  181.     m_bHaveFocus = FALSE;
  182.     
  183.     m_pIMWContext = NULL;
  184.     if (pUnk)
  185.         pUnk->QueryInterface( IID_IMWContext, (LPVOID *) &m_pIMWContext );
  186.         
  187.     CAnimation2::m_uRefCount++;
  188. }
  189.  
  190. CAnimation2::~CAnimation2()
  191. {
  192.     CAnimation2::m_uRefCount--;
  193.     ASSERT( CAnimation2::m_uRefCount >= 0 );
  194.     
  195.     if( !CAnimation2::m_uRefCount )
  196.     {
  197.         if( CAnimation2::m_hSmall )
  198.         {
  199.             DeleteObject( CAnimation2::m_hSmall );
  200.             CAnimation2::m_hSmall = NULL;
  201.         }
  202.         
  203.         if( CAnimation2::m_hBig )
  204.         {
  205.             DeleteObject( CAnimation2::m_hBig );
  206.             CAnimation2::m_hBig = NULL;
  207.         }
  208.     }
  209. }
  210.  
  211. BEGIN_MESSAGE_MAP(CAnimation2, CWnd)
  212.     ON_WM_ERASEBKGND()
  213.     ON_WM_PAINT()
  214.     ON_WM_TIMER()
  215.     ON_WM_LBUTTONDOWN()
  216.     ON_WM_LBUTTONUP()
  217.     ON_WM_MOUSEMOVE()
  218.     ON_WM_DESTROY()
  219.     ON_WM_SETCURSOR()
  220. END_MESSAGE_MAP()
  221.  
  222. BOOL CAnimation2::OnEraseBkgnd( CDC* pDC )
  223. {
  224.     return TRUE;
  225. }
  226.  
  227. void CAnimation2::OnPaint()
  228. {
  229.     // Do not remove next useless line, somehow needed or
  230.     //  painting will hang.
  231.     CPaintDC dc(this);
  232.     
  233.     AnimateIcon();
  234. }
  235.  
  236.  
  237. void CAnimation2::OnTimer(UINT nID)
  238. {
  239.     CWnd::OnTimer(nID);
  240.     if (nID == WIN_ANIMATE_ICON_TIMER) {
  241.  
  242.         if(m_bInited)
  243.         {
  244.             m_iAnimationCount = ( m_iAnimationCount % m_iFrameCount ) + 1;
  245.             
  246.             AnimateIcon();
  247.         }
  248.     }
  249.     else if(nID == IDT_BUTTONFOCUS)
  250.     {
  251.         RemoveButtonFocus();
  252.     }
  253. }
  254.  
  255. BOOL CAnimation2::OnSetCursor( CWnd *, UINT, UINT )
  256. {
  257.     SetCursor( theApp.LoadCursor( IDC_SELECTANCHOR ) );
  258.     
  259.     return TRUE;
  260. }
  261.  
  262. void CAnimation2::OnLButtonDown( UINT nFlags, CPoint point )
  263. {
  264.     RECT rect;
  265.     GetClientRect(&rect);
  266.     m_bDepressed = ::PtInRect(&rect, point);
  267.     if (m_bDepressed) {
  268.         SetCapture();
  269.         m_bCaptured = TRUE;
  270.         Invalidate();
  271.         UpdateWindow();
  272.     }
  273.     
  274.     MSG msg = *(GetCurrentMessage());
  275.     m_ToolTip.RelayEvent(&msg);
  276. }
  277.  
  278. void CAnimation2::OnMouseMove( UINT nFlags, CPoint point )
  279. {
  280.     RECT rect;
  281.     GetClientRect(&rect);
  282.  
  283.     if (m_bCaptured) 
  284.     {
  285.             
  286.             BOOL bDepressed = ::PtInRect(&rect, point);
  287.             if (bDepressed != m_bDepressed)
  288.             {
  289.                 m_bDepressed = bDepressed;
  290.                 Invalidate();
  291.                 UpdateWindow();
  292.             }
  293.     }
  294.  
  295.     if (!m_bHaveFocus)
  296.     {
  297.         if(GetParentFrame() == GetActiveWindow() || GetParentFrame() == AfxGetMainWnd())
  298.         {
  299.             m_bHaveFocus = TRUE;
  300.             if( !CUST_IsCustomAnimation( &m_iFrameCount ) && m_hFocusTimer == 0)
  301.             {
  302.                 m_hFocusTimer = SetTimer(IDT_BUTTONFOCUS, BUTTONFOCUS_DELAY_MS, NULL);
  303.  
  304.                     WFE_GetOwnerFrame(this)->SendMessage( WM_SETMESSAGESTRING,
  305.                                                            (WPARAM)0, (LPARAM)szLoadString(IDS_NETSCAPE_TIP));
  306.  
  307.             }
  308.  
  309.             m_ToolTip.Activate(TRUE);
  310.         }
  311.     }
  312.  
  313.     if(GetParentFrame() == GetActiveWindow() || GetParentFrame() == AfxGetMainWnd())
  314.     {
  315.         MSG msg = *(GetCurrentMessage());
  316.         m_ToolTip.RelayEvent(&msg);
  317.     }
  318. }
  319.  
  320. void CAnimation2::RemoveButtonFocus()
  321. {
  322.     POINT point;
  323.  
  324.     KillTimer(IDT_BUTTONFOCUS);
  325.     m_hFocusTimer = 0;
  326.     GetCursorPos(&point);
  327.  
  328.     CRect rcClient;
  329.     GetWindowRect(&rcClient);
  330.  
  331.     if (!rcClient.PtInRect(point))
  332.     {
  333.         m_bHaveFocus = FALSE;
  334.  
  335.         if ( WFE_GetOwnerFrame(this) != NULL) {
  336.             WFE_GetOwnerFrame(this)->SendMessage( WM_SETMESSAGESTRING,
  337.                                            (WPARAM) 0, (LPARAM) "" );
  338.         }
  339.         m_bDepressed = FALSE;
  340.         Invalidate();
  341.         UpdateWindow();
  342.     }
  343.     else
  344.         m_hFocusTimer = SetTimer(IDT_BUTTONFOCUS, BUTTONFOCUS_DELAY_MS, NULL);
  345. }
  346.  
  347. void CAnimation2::OnLButtonUp( UINT nFlags, CPoint point )
  348. {
  349.  
  350.     if (m_bCaptured) {
  351.         ReleaseCapture();
  352.         m_bCaptured = FALSE;
  353.  
  354.         RECT rect;
  355.         GetClientRect(&rect);
  356.         BOOL bDepressed = ::PtInRect(&rect, point);
  357.         if ( bDepressed) {
  358.             CAbstractCX * pCX = FEU_GetLastActiveFrameContext();
  359.             ASSERT(pCX != NULL);
  360.             if (pCX != NULL)
  361.             {
  362.                 char *pURL;
  363.                 PREF_CopyConfigString("toolbar.logo.url",&pURL);
  364.                 if (pURL && *pURL) {
  365.                     pCX->NormalGetUrl(pURL);
  366.                     XP_FREE(pURL);
  367.                 }
  368.  
  369.             }
  370.         }
  371.     }
  372.  
  373.     m_bDepressed = FALSE;
  374.     Invalidate();
  375.     UpdateWindow();
  376. }
  377.  
  378. void CAnimation2::OnDestroy()
  379. {
  380. }
  381.  
  382. void CAnimation2::StopAnimation()
  383. {
  384.     m_iAnimationCount = 0;
  385.     if (m_uAnimationClock)
  386.         KillTimer(m_uAnimationClock);
  387.     m_uAnimationClock = 0;
  388.     AnimateIcon();
  389. }
  390.  
  391. void CAnimation2::StartAnimation()
  392. {
  393.     m_iAnimationCount = 1;
  394.     m_uAnimationClock = SetTimer(WIN_ANIMATE_ICON_TIMER, ANIMATION_PERIOD/m_iFrameCount, NULL);
  395. }
  396.  
  397. void CAnimation2::AnimateIcon()
  398. {
  399.  
  400.     CClientDC dc(this);
  401.     CRect rect;
  402.     GetClientRect(&rect);
  403.     
  404.     // check if our application level CDC exists
  405.     if(!theApp.pIconDC) {
  406.         theApp.pIconDC = new CDC;
  407.         theApp.pIconDC->CreateCompatibleDC(&dc);
  408.  
  409.     }
  410.  
  411.     HBITMAP hOldBitmap = (HBITMAP) theApp.pIconDC->SelectObject( m_hAnimBitmap );
  412.     
  413.  
  414.     dc.BitBlt(  2, 2,
  415.                 m_iconSize.cx, m_iconSize.cy, 
  416.                 theApp.pIconDC, 
  417.                 m_iconSize.cx * m_iAnimationCount,      // X offset into the strip
  418.                 0, 
  419.                 SRCCOPY);
  420.  
  421.     if (m_bDepressed)
  422.         DrawDownButton(dc.m_hDC, rect);
  423.     else DrawUpButton(dc.m_hDC, rect);
  424.  
  425.     //  Reselect the old object
  426.     theApp.pIconDC->SelectObject( hOldBitmap );
  427.  
  428. }
  429.  
  430. void CAnimation2::Initialize( LPUNKNOWN pUnk )
  431. {
  432.     ASSERT(pUnk);
  433.     if (m_pIMWContext) {
  434.         m_pIMWContext->Release();
  435.         m_pIMWContext = NULL;
  436.     }
  437.     if (pUnk) {
  438.         pUnk->QueryInterface( IID_IMWContext, (LPVOID *) &m_pIMWContext );
  439.     }
  440. }
  441.  
  442. void CAnimation2::SetBitmap( BOOL bSmall /*=TRUE*/ )
  443. {
  444.     CDC *pDC = GetDC();
  445.  
  446.     if( !CAnimation2::m_hSmall )
  447.     {
  448.         CAnimation2::m_hSmall = wfe_LoadBitmap( AfxGetResourceHandle(), pDC->m_hDC, MAKEINTRESOURCE(IDB_ANIMSMALL_0) );
  449.         ASSERT( CAnimation2::m_hSmall );
  450.         
  451.         CAnimation2::m_hBig = wfe_LoadBitmap( AfxGetResourceHandle(), pDC->m_hDC, MAKEINTRESOURCE(IDB_ANIM_0) );        
  452.         ASSERT( CAnimation2::m_hBig );
  453.     }
  454.  
  455.     if( CUST_IsCustomAnimation( &m_iFrameCount ) )
  456.     {
  457.         ASSERT( m_iFrameCount > 0 );
  458.         
  459.          m_hAnimBitmap = bSmall ? CAnimation2::m_hSmall : CAnimation2::m_hBig;
  460.  
  461.          BITMAP bm;
  462.            GetObject( m_hAnimBitmap, sizeof(BITMAP), &bm );
  463.  
  464.          m_iconSize.cx = bm.bmWidth / m_iFrameCount;
  465.          m_iconSize.cy = bm.bmHeight;
  466.     }
  467.     else
  468.     {
  469.         if( bSmall )
  470.         {
  471.             m_iconSize.cx = ANIMATION_WIDTH;
  472.             m_iconSize.cy = ANIMATION_HEIGHT;
  473.              m_hAnimBitmap = CAnimation2::m_hSmall;
  474.         }
  475.         else
  476.         {
  477.             m_iconSize.cx = ANIMATION_WIDTH_L;
  478.             m_iconSize.cy = ANIMATION_HEIGHT_L;
  479.              m_hAnimBitmap = CAnimation2::m_hBig;
  480.         }
  481.         
  482.          BITMAP bm;
  483.            GetObject( m_hAnimBitmap, sizeof(BITMAP), &bm );
  484.          m_iFrameCount = bm.bmWidth / m_iconSize.cx;
  485.     }
  486.  
  487.     ReleaseDC( pDC );
  488.     
  489.     SetWindowPos( NULL, 0, 0, m_iconSize.cx+4, m_iconSize.cy+4, SWP_NOMOVE | SWP_NOZORDER );
  490.     GetParent()->SetWindowPos( NULL, 0, 0, m_iconSize.cx+4, m_iconSize.cy+4, SWP_NOMOVE | SWP_NOZORDER );
  491. }
  492.  
  493. HBITMAP CAnimation2::GetBitmap( BOOL bSmall /*=TRUE*/ )
  494. {
  495.     return bSmall ? CAnimation2::m_hSmall : CAnimation2::m_hBig;
  496. }
  497.  
  498. void CAnimation2::GetSize( CSize &size )
  499. {
  500.     size = m_iconSize;
  501. }
  502.  
  503. /////////////////////////////////////////////////////////////////////////////
  504. // CAnimationBar2 toolbar derived class
  505.  
  506. CAnimationBar2::CAnimationBar2( LPUNKNOWN pUnk ): CAnimationBar2Parent ()
  507. {
  508.     m_iconPt.x = 0;
  509.     m_iconPt.y = 0;
  510.  
  511.     m_wndAnimation = NULL;
  512.     m_pUnk = pUnk;
  513. }
  514.  
  515. CAnimationBar2::~CAnimationBar2()
  516. {
  517.     if ( m_wndAnimation ) {
  518.         m_wndAnimation->DestroyWindow();
  519.         delete m_wndAnimation;
  520.     }
  521. }
  522.  
  523. void CAnimationBar2::StartAnimation()
  524. {
  525.     if( !m_wndAnimation )
  526.     {
  527.         return;
  528.     }
  529.     
  530.     m_wndAnimation->StartAnimation();
  531. }
  532.  
  533. void CAnimationBar2::StopAnimation()
  534. {
  535.     if( !m_wndAnimation )
  536.     {
  537.         return;
  538.     }
  539.     
  540.     m_wndAnimation->StopAnimation();
  541. }
  542.  
  543. void CAnimationBar2::Initialize( LPUNKNOWN pUnk )
  544. {
  545.     m_pUnk = pUnk;
  546.     if (m_wndAnimation) 
  547.         m_wndAnimation->Initialize( pUnk );
  548. }
  549.  
  550. BEGIN_MESSAGE_MAP(CAnimationBar2, CAnimationBar2Parent)
  551.     ON_WM_CREATE()
  552.     ON_WM_SIZE()
  553. END_MESSAGE_MAP()
  554.  
  555. int CAnimationBar2::OnCreate( LPCREATESTRUCT lpCreateStruct )
  556. {
  557.  
  558.     int res = CAnimationBar2Parent::OnCreate( lpCreateStruct );
  559.  
  560.     if ( res != -1 ) {
  561.         m_wndAnimation = new CAnimation2( this, m_pUnk );
  562.         res = m_wndAnimation ? 0 : -1;
  563.     }
  564.     return res;
  565. }
  566.  
  567. void CAnimationBar2::OnSize( UINT nType, int cx, int cy )
  568. {
  569. }
  570.  
  571. BOOL CAnimationBar2::OnEraseBkgnd( CDC* pDC )
  572. {
  573.     return TRUE;
  574. }
  575.   
  576. void CAnimationBar2::GetSize( CSize &size, BOOL bSmall /*=TRUE*/ )
  577. {
  578.     if( !m_wndAnimation )
  579.     {
  580.         return;
  581.     }
  582.  
  583.     int iFrameCount = 0;
  584.     
  585.     if( CUST_IsCustomAnimation( &iFrameCount ) )
  586.     {
  587.         ASSERT( iFrameCount > 0 );
  588.         
  589.          BITMAP bm;
  590.            GetObject( m_wndAnimation->GetBitmap( bSmall ), sizeof(BITMAP), &bm );
  591.  
  592.          size.cx = bm.bmWidth / iFrameCount;
  593.          size.cy = bm.bmHeight;
  594.     }
  595.     else
  596.     {
  597.            size.cx = (bSmall ? ANIMATION_WIDTH  : ANIMATION_WIDTH_L);
  598.            size.cy = (bSmall ? ANIMATION_HEIGHT : ANIMATION_HEIGHT_L);
  599.     }
  600.  
  601.     size.cx += 4;
  602.     size.cy += 4;
  603.  
  604. }
  605.