blob: 4df32bb46d3361785da8a9b1706fe37a4141d17a (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
#include "./loginStatus.h"
#include "./common.h"
LoginStatus::LoginStatus(HWND hTarget)
: ref(1), hwnd(hTarget)
{
InitializeCriticalSection(&lock);
}
LoginStatus::~LoginStatus()
{
DeleteCriticalSection(&lock);
}
HRESULT LoginStatus::CreateInstance(HWND hTarget, LoginStatus **instance)
{
if (NULL == instance)
return E_POINTER;
*instance = new LoginStatus(hTarget);
if (NULL == *instance) return E_OUTOFMEMORY;
return S_OK;
}
ULONG LoginStatus::AddRef()
{
return InterlockedIncrement((LONG*)&ref);
}
ULONG LoginStatus::Release()
{
if (0 == ref)
return ref;
LONG r = InterlockedDecrement((LONG*)&ref);
if (0 == r)
delete(this);
return r;
}
UINT LoginStatus::Add(BSTR status)
{
EnterCriticalSection(&lock);
Record r;
r.cookie = GetNextCookie();
r.text = status;
list.push_back(r);
LeaveCriticalSection(&lock);
UpdateWindowText();
return r.cookie;
}
BOOL LoginStatus::Set(UINT cookie, BSTR status)
{
BOOL foundOk = FALSE;
EnterCriticalSection(&lock);
size_t index = list.size();
while(index--)
{
if (cookie == list[index].cookie)
{
SysFreeString(list[index].text);
list[index].text = status;
foundOk = TRUE;
break;
}
}
LeaveCriticalSection(&lock);
UpdateWindowText();
return foundOk;
}
void LoginStatus::Remove(UINT cookie)
{
EnterCriticalSection(&lock);
size_t index = list.size();
while(index--)
{
if (cookie == list[index].cookie)
{
SysFreeString(list[index].text);
list.eraseAt(index);
break;
}
}
LeaveCriticalSection(&lock);
UpdateWindowText();
}
BOOL LoginStatus::AttachWindow(HWND hTarget)
{
DetachWindow();
hwnd = hTarget;
UpdateWindowText();
return TRUE;
}
BOOL LoginStatus::DetachWindow()
{
hwnd = NULL;
return TRUE;
}
UINT LoginStatus::GetNextCookie()
{
size_t i, count;
count = list.size();
UINT cookie = (UINT)count;
do
{
for (i = 0; i < count; i++)
{
if (list[i].cookie == cookie)
{
cookie++;
break;
}
}
} while(i != count);
return cookie;
}
BOOL LoginStatus::UpdateWindowText()
{
EnterCriticalSection(&lock);
BOOL resultOk = FALSE;
if (NULL != hwnd)
{
BSTR text = NULL;
size_t index = list.size();
while(index--)
{
if (NULL != list[index].text && L'\0' != list[index].text)
{
text = list[index].text;
break;
}
}
resultOk = (BOOL)SendMessage(hwnd, WM_SETTEXT, 0, (LPARAM)text);
}
LeaveCriticalSection(&lock);
return resultOk;
}
|