PlayerSeekBar.cpp

00001 /* 
00002  *      Copyright (C) 2003-2005 Gabest
00003  *      http://www.gabest.org
00004  *
00005  *  This Program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2, or (at your option)
00008  *  any later version.
00009  *   
00010  *  This Program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013  *  GNU General Public License for more details.
00014  *   
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with GNU Make; see the file COPYING.  If not, write to
00017  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
00018  *  http://www.gnu.org/copyleft/gpl.html
00019  *
00020  */
00021 
00022 // PlayerSeekBar.cpp : implementation file
00023 //
00024 
00025 #include "stdafx.h"
00026 #include "mplayerc.h"
00027 #include "PlayerSeekBar.h"
00028 
00029 // CPlayerSeekBar
00030 
00031 IMPLEMENT_DYNAMIC(CPlayerSeekBar, CDialogBar)
00032 
00033 CPlayerSeekBar::CPlayerSeekBar() : 
00034         m_start(0), m_stop(100), m_pos(0), m_posreal(0), 
00035         m_fEnabled(false)
00036 {
00037 }
00038 
00039 CPlayerSeekBar::~CPlayerSeekBar()
00040 {
00041 }
00042 
00043 BOOL CPlayerSeekBar::Create(CWnd* pParentWnd)
00044 {
00045         if(!CDialogBar::Create(pParentWnd, IDD_PLAYERSEEKBAR, WS_CHILD|WS_VISIBLE|CBRS_ALIGN_BOTTOM, IDD_PLAYERSEEKBAR))
00046                 return FALSE;
00047 
00048         return TRUE;
00049 }
00050 
00051 BOOL CPlayerSeekBar::PreCreateWindow(CREATESTRUCT& cs)
00052 {
00053         if(!CDialogBar::PreCreateWindow(cs))
00054                 return FALSE;
00055 
00056         m_dwStyle &= ~CBRS_BORDER_TOP;
00057         m_dwStyle &= ~CBRS_BORDER_BOTTOM;
00058         m_dwStyle |= CBRS_SIZE_FIXED;
00059 
00060         return TRUE;
00061 }
00062 
00063 void CPlayerSeekBar::Enable(bool fEnable)
00064 {
00065         m_fEnabled = fEnable;
00066         Invalidate();
00067 }
00068 
00069 void CPlayerSeekBar::GetRange(__int64& start, __int64& stop)
00070 {
00071         start = m_start;
00072         stop = m_stop;
00073 }
00074 
00075 void CPlayerSeekBar::SetRange(__int64 start, __int64 stop) 
00076 {
00077         if(start > stop) start ^= stop, stop ^= start, start ^= stop;
00078         m_start = start;
00079         m_stop = stop;
00080         if(m_pos < m_start || m_pos >= m_stop) SetPos(m_start);
00081 }
00082 
00083 __int64 CPlayerSeekBar::GetPos()
00084 {
00085         return(m_pos);
00086 }
00087 
00088 __int64 CPlayerSeekBar::GetPosReal()
00089 {
00090         return(m_posreal);
00091 }
00092 
00093 void CPlayerSeekBar::SetPos(__int64 pos)
00094 {
00095         CWnd* w = GetCapture();
00096         if(w && w->m_hWnd == m_hWnd) return;
00097 
00098         SetPosInternal(pos);
00099 }
00100 
00101 void CPlayerSeekBar::SetPosInternal(__int64 pos)
00102 {
00103         if(m_pos == pos) return;
00104 
00105         CRect before = GetThumbRect();
00106         m_pos = min(max(pos, m_start), m_stop);
00107         m_posreal = pos;
00108         CRect after = GetThumbRect();
00109 
00110         if(before != after) InvalidateRect(before | after);
00111 }
00112 
00113 CRect CPlayerSeekBar::GetChannelRect()
00114 {
00115         CRect r;
00116         GetClientRect(&r);
00117         r.DeflateRect(8, 9, 9, 0);
00118         r.bottom = r.top + 5;
00119         return(r);
00120 }
00121 
00122 CRect CPlayerSeekBar::GetThumbRect()
00123 {
00124 //      bool fEnabled = m_fEnabled || m_start >= m_stop;
00125 
00126         CRect r = GetChannelRect();
00127 
00128         int x = r.left + (int)((m_start < m_stop /*&& fEnabled*/) ? (__int64)r.Width() * (m_pos - m_start) / (m_stop - m_start) : 0);
00129         int y = r.CenterPoint().y;
00130 
00131         r.SetRect(x, y, x, y);
00132         r.InflateRect(6, 7, 7, 8);
00133 
00134         return(r);
00135 }
00136 
00137 CRect CPlayerSeekBar::GetInnerThumbRect()
00138 {
00139         CRect r = GetThumbRect();
00140 
00141         bool fEnabled = m_fEnabled && m_start < m_stop;
00142         r.DeflateRect(3, fEnabled ? 5 : 4, 3, fEnabled ? 5 : 4);
00143 
00144         return(r);
00145 }
00146 
00147 void CPlayerSeekBar::MoveThumb(CPoint point)
00148 {
00149         CRect r = GetChannelRect();
00150         
00151         if(r.left >= r.right) return;
00152 
00153         if(point.x < r.left) SetPos(m_start);
00154         else if(point.x >= r.right) SetPos(m_stop);
00155         else
00156         {
00157                 __int64 w = r.right - r.left;
00158                 if(m_start < m_stop)
00159                         SetPosInternal(m_start + ((m_stop - m_start) * (point.x - r.left) + (w/2)) / w);
00160         }
00161 }
00162 
00163 BEGIN_MESSAGE_MAP(CPlayerSeekBar, CDialogBar)
00164         //{{AFX_MSG_MAP(CPlayerSeekBar)
00165         ON_WM_PAINT()
00166         ON_WM_SIZE()
00167         ON_WM_LBUTTONDOWN()
00168         ON_WM_LBUTTONUP()
00169         ON_WM_MOUSEMOVE()
00170         ON_WM_ERASEBKGND()
00171         //}}AFX_MSG_MAP
00172         ON_COMMAND_EX(ID_PLAY_STOP, OnPlayStop)
00173 END_MESSAGE_MAP()
00174 
00175 
00176 // CPlayerSeekBar message handlers
00177 
00178 void CPlayerSeekBar::OnPaint()
00179 {
00180         CPaintDC dc(this); // device context for painting
00181 
00182         bool fEnabled = m_fEnabled && m_start < m_stop;
00183 
00184         COLORREF 
00185                 white = GetSysColor(COLOR_WINDOW),
00186                 shadow = GetSysColor(COLOR_3DSHADOW), 
00187                 light = GetSysColor(COLOR_3DHILIGHT), 
00188                 bkg = GetSysColor(COLOR_BTNFACE);
00189 
00190         // thumb
00191         {
00192                 CRect r = GetThumbRect(), r2 = GetInnerThumbRect();
00193                 CRect rt = r, rit = r2;
00194 
00195                 dc.Draw3dRect(&r, light, 0);
00196                 r.DeflateRect(0, 0, 1, 1);
00197                 dc.Draw3dRect(&r, light, shadow);
00198                 r.DeflateRect(1, 1, 1, 1);
00199 
00200                 CBrush b(bkg);
00201 
00202                 dc.FrameRect(&r, &b);
00203                 r.DeflateRect(0, 1, 0, 1);
00204                 dc.FrameRect(&r, &b);
00205 
00206                 r.DeflateRect(1, 1, 0, 0);
00207                 dc.Draw3dRect(&r, shadow, bkg);
00208 
00209                 if(fEnabled)
00210                 {
00211                 r.DeflateRect(1, 1, 1, 2);
00212                 CPen white(PS_INSIDEFRAME, 1, white);
00213                 CPen* old = dc.SelectObject(&white);
00214                 dc.MoveTo(r.left, r.top);
00215                 dc.LineTo(r.right, r.top);
00216                 dc.MoveTo(r.left, r.bottom);
00217                 dc.LineTo(r.right, r.bottom);
00218                 dc.SelectObject(old);
00219                 dc.SetPixel(r.CenterPoint().x, r.top, 0);
00220                 dc.SetPixel(r.CenterPoint().x, r.bottom, 0);
00221                 }
00222 
00223                 dc.SetPixel(r.CenterPoint().x+5, r.top-4, bkg);
00224 
00225                 {
00226                         CRgn rgn1, rgn2;
00227                         rgn1.CreateRectRgnIndirect(&rt);
00228                         rgn2.CreateRectRgnIndirect(&rit);
00229                         ExtSelectClipRgn(dc, rgn1, RGN_DIFF);
00230                         ExtSelectClipRgn(dc, rgn2, RGN_OR);
00231                 }
00232         }
00233 
00234         // channel
00235         {
00236                 CRect r = GetChannelRect();
00237 
00238                 dc.FillSolidRect(&r, fEnabled ? white : bkg);
00239                 r.InflateRect(1, 1);
00240                 dc.Draw3dRect(&r, shadow, light);
00241                 dc.ExcludeClipRect(&r);
00242         }
00243 
00244         // background
00245         {
00246                 CRect r;
00247                 GetClientRect(&r);
00248                 CBrush b(bkg);
00249                 dc.FillRect(&r, &b);
00250         }
00251 
00252 
00253         // Do not call CDialogBar::OnPaint() for painting messages
00254 }
00255 
00256 
00257 void CPlayerSeekBar::OnSize(UINT nType, int cx, int cy)
00258 {
00259         CDialogBar::OnSize(nType, cx, cy);
00260 
00261         Invalidate();
00262 }
00263 
00264 void CPlayerSeekBar::OnLButtonDown(UINT nFlags, CPoint point)
00265 {
00266         if(m_fEnabled && (GetChannelRect() | GetThumbRect()).PtInRect(point))
00267         {
00268                 SetCapture();
00269                 MoveThumb(point);
00270                 GetParent()->PostMessage(WM_HSCROLL, MAKEWPARAM((short)m_pos, SB_THUMBPOSITION), (LPARAM)m_hWnd);
00271         }
00272 
00273         CDialogBar::OnLButtonDown(nFlags, point);
00274 }
00275 
00276 void CPlayerSeekBar::OnLButtonUp(UINT nFlags, CPoint point)
00277 {
00278         ReleaseCapture();
00279 
00280         CDialogBar::OnLButtonUp(nFlags, point);
00281 }
00282 
00283 void CPlayerSeekBar::OnMouseMove(UINT nFlags, CPoint point)
00284 {
00285         CWnd* w = GetCapture();
00286         if(w && w->m_hWnd == m_hWnd && (nFlags & MK_LBUTTON))
00287         {
00288                 MoveThumb(point);
00289                 GetParent()->PostMessage(WM_HSCROLL, MAKEWPARAM((short)m_pos, SB_THUMBTRACK), (LPARAM)m_hWnd);
00290         }
00291 
00292         CDialogBar::OnMouseMove(nFlags, point);
00293 }
00294 
00295 BOOL CPlayerSeekBar::OnEraseBkgnd(CDC* pDC)
00296 {
00297         return TRUE;
00298 }
00299 
00300 BOOL CPlayerSeekBar::OnPlayStop(UINT nID)
00301 {
00302         SetPos(0);
00303         return FALSE;
00304 }

Generated on Tue Dec 13 14:46:59 2005 for guliverkli by  doxygen 1.4.5