update for HEAD-2003091401
[reactos.git] / subsys / system / explorer / Seashell / SeaShellExt / TextProgressCtrl.cpp
1 // TextProgressCtrl.cpp : implementation file
2 //
3 // Written by Chris Maunder (cmaunder@mail.com)
4 // Copyright 1998.
5 //
6 // TextProgressCtrl is a drop-in replacement for the standard 
7 // CProgressCtrl that displays text in a progress control.
8 //
9 // This code may be used in compiled form in any way you desire. This
10 // file may be redistributed by any means PROVIDING it is not sold for
11 // profit without the authors written consent, and providing that this
12 // notice and the authors name is included. If the source code in 
13 // this file is used in any commercial application then an email to
14 // the me would be nice.
15 //
16 // This file is provided "as is" with no expressed or implied warranty.
17 // The author accepts no liability if it causes any damage to your
18 // computer or anything else vaguely within it's vicinity.
19 //
20 // Expect bugs.
21 // 
22 // Please use and enjoy. Please let me know of any bugs/mods/improvements 
23 // that you have found/implemented and I will fix/incorporate them into this
24 // file. 
25 // Modified by Philip Oldaker 2000
26
27 #include "stdafx.h"
28 #include "TextProgressCtrl.h"
29
30 #ifdef _DEBUG
31 #define new DEBUG_NEW
32 #undef THIS_FILE
33 static char THIS_FILE[] = __FILE__;
34 #endif
35
36 IMPLEMENT_DYNAMIC(CTextProgressCtrl,CUIODColumnCtrl)
37 /////////////////////////////////////////////////////////////////////////////
38 // CTextProgressCtrl
39
40 CTextProgressCtrl::CTextProgressCtrl()
41 {
42     m_nPos      = 0;
43     m_nStepSize = 1;
44     m_nMax      = 100;
45     m_nMin      = 0;
46     m_bShowText = TRUE;
47     m_crBarClr          = CLR_DEFAULT;
48     m_crBgClr           = CLR_DEFAULT;
49         m_crTextClr             = CLR_DEFAULT;
50     m_crSelBarClr   = CLR_DEFAULT;
51     m_crSelBgClr    = CLR_DEFAULT;
52         m_crSelTextClr  = CLR_DEFAULT;
53 }
54
55 CTextProgressCtrl::~CTextProgressCtrl()
56 {
57 }
58
59 void CTextProgressCtrl::DoPaint(CDC *PaintDC,CRect rcClient,bool bSelected) 
60 {
61     if (m_nMin >= m_nMax) 
62         return;
63
64         bool bInvert=bSelected;
65     COLORREF crBarColour = (m_crBarClr == CLR_DEFAULT)? RGB(10,20,200) : m_crBarClr;
66     COLORREF crBgColour = (m_crBgClr == CLR_DEFAULT)? ::GetSysColor(COLOR_MENU) : m_crBgClr;
67         COLORREF crTextColor = (m_crTextClr == CLR_DEFAULT)? ::GetSysColor(COLOR_BTNTEXT)  : m_crTextClr;
68         if (bSelected)
69         {
70                 if (m_crSelBarClr != CLR_DEFAULT)
71                 {
72                         crBarColour = m_crSelBarClr;
73                         bInvert = false;
74                 }
75                 if (m_crSelBgClr != CLR_DEFAULT)
76                 {
77                         crBgColour = m_crSelBgClr;
78                         bInvert = false;
79                 }
80                 if (m_crSelTextClr != CLR_DEFAULT)
81                 {
82                         crTextColor = m_crSelTextClr;
83                         bInvert = false;
84                 }
85         }
86     double Fraction = (double)(m_nPos - m_nMin) / ((double)(m_nMax - m_nMin));
87
88         CDC &dc = *PaintDC;
89         dc.Rectangle(rcClient.left+1,rcClient.top+1,rcClient.right-1,rcClient.bottom-1);
90         rcClient.DeflateRect(2,2);
91     CRect LeftRect(rcClient);
92         CRect RightRect(rcClient);
93
94     LeftRect.right = LeftRect.left + (int)((LeftRect.right - LeftRect.left)*Fraction);
95     RightRect.left = LeftRect.right;
96     dc.FillSolidRect(LeftRect, crBarColour);
97     dc.FillSolidRect(RightRect, crBgColour);
98     // Draw Text if not vertical
99     if (m_bShowText)
100     {
101         CString str;
102         if (m_strText.GetLength())
103             str = m_strText;
104         else
105             str.Format(_T("%d%%"), (int)(Fraction*100.0));
106                 CSize sz = dc.GetTextExtent(str);
107                 int nTextPos = 0;
108                 if (rcClient.Width() > sz.cx)
109                         nTextPos = (rcClient.Width() - sz.cx) / 2;
110                 if (!bSelected && LeftRect.Width() >= nTextPos) 
111                 {
112                         dc.SetTextColor(RGB(255,255,255));
113                 }
114                 else
115                         dc.SetTextColor(crTextColor);
116         dc.SetBkMode(TRANSPARENT);
117         DWORD dwTextStyle = DT_CENTER | DT_VCENTER | DT_SINGLELINE;
118         dc.DrawText(str, rcClient, dwTextStyle);
119     }
120         if (bInvert)
121         {
122                 dc.InvertRect(rcClient);
123         }
124 }
125
126 /////////////////////////////////////////////////////////////////////////////
127 // CTextProgressCtrl operations
128
129 void CTextProgressCtrl::SetShowText(BOOL bShow)
130
131     m_bShowText = bShow;
132 }
133
134 void CTextProgressCtrl::SetRange(int nLower, int nUpper)
135 {
136     m_nMax = nUpper;
137     m_nMin = nLower;
138 }
139
140 void CTextProgressCtrl::GetRange(int& nLower, int& nUpper) const
141 {
142     nUpper = m_nMax;
143     nLower = m_nMin;
144 }
145
146 int CTextProgressCtrl::SetPos(int nPos) 
147 {    
148     int nOldPos = m_nPos;
149     m_nPos = nPos;
150     return nOldPos;
151 }
152
153 int CTextProgressCtrl::StepIt() 
154 {    
155    return SetPos(m_nPos + m_nStepSize);
156 }
157
158 int CTextProgressCtrl::OffsetPos(int nPos)
159 {
160     return SetPos(m_nPos + nPos);
161 }
162
163 int CTextProgressCtrl::SetStep(int nStep)
164 {
165     int nOldStep = m_nStepSize;
166     m_nStepSize = nStep;
167     return nOldStep;
168 }
169
170 COLORREF CTextProgressCtrl::SetBarColor(COLORREF crBarClr /*= CLR_DEFAULT*/,COLORREF crSelBarClr /*= CLR_DEFAULT*/)
171 {
172     COLORREF crOldBarClr = m_crBarClr;
173     m_crBarClr = crBarClr;
174     m_crSelBarClr = crSelBarClr;
175     return crOldBarClr;
176 }
177
178 COLORREF CTextProgressCtrl::GetBarColor() const
179
180     return m_crBarClr;
181 }
182
183 COLORREF CTextProgressCtrl::GetSelBarColor() const
184
185     return m_crSelBarClr;
186 }
187
188 COLORREF CTextProgressCtrl::SetBgColor(COLORREF crBgClr /*= CLR_DEFAULT*/,COLORREF crSelBgClr /*= CLR_DEFAULT*/)
189 {
190     COLORREF crOldBgClr = m_crBgClr;
191     m_crBgClr = crBgClr;
192         m_crSelBgClr = crSelBgClr;
193     return crOldBgClr;
194 }
195
196 COLORREF CTextProgressCtrl::GetBgColor() const
197
198     return m_crBgClr;
199 }
200
201
202 COLORREF CTextProgressCtrl::GetSelBgColor() const
203
204     return m_crSelBgClr;
205 }
206
207 COLORREF CTextProgressCtrl::SetTextColor(COLORREF crTextClr /*= CLR_DEFAULT*/,COLORREF crSelTextClr /*= CLR_DEFAULT*/)
208 {
209     COLORREF crOldTextClr = m_crTextClr;
210     m_crTextClr = crTextClr;
211         m_crSelTextClr = crSelTextClr;
212     return crOldTextClr;
213 }
214
215 COLORREF CTextProgressCtrl::GetTextColor() const
216
217     return m_crTextClr;
218 }
219
220 COLORREF CTextProgressCtrl::GetSelTextColor() const
221
222     return m_crSelTextClr;
223 }