/
WindowListDlg.cpp
375 lines (298 loc) · 8.04 KB
/
WindowListDlg.cpp
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/********************************************************************************
Gnucleus - An Application for the Gnutella Network
Copyright (C) 2000-2005 John Marshall Group
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
By contributing code you grant John Marshall Group an unlimited, non-exclusive
license to your contribution.
For support, questions, commercial use, etc...
E-Mail: swabby@c0re.net
********************************************************************************/
#include "stdafx.h"
#include "gnucleus.h"
#include "WindowListDlg.h"
#include "FrameConnect.h"
#include "FrameStatistics.h"
#include "FrameShare.h"
#include "FrameChat.h"
#include "FrameTransfers.h"
#include "FrameSearch.h"
#include "FrameBrowser.h"
#include <algorithm>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CWindowListDlg::CWindowListDlg()
{
//{{AFX_DATA_INIT(CWindowListDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_Slots = 0L;
}
//void CWindowListDlg::DoDataExchange(CDataExchange* pDX)
//{
//{{AFX_DATA_MAP(CWindowListDlg)
//}}AFX_DATA_MAP
//}
BOOL CWindowListDlg::Create(CWnd* pParentWnd, UINT nIDTemplate, UINT nStyle, UINT nID)
{
BOOL bRet = CDialogBar::Create(pParentWnd, nIDTemplate, nStyle, nID);
ASSERT(bRet);
VERIFY(m_ctlWindowList.SubclassDlgItem(IDC_WINDOW_TABS, this));
m_ImageLst.Create (16, 16, ILC_COLOR8 | TRUE, 0, 10);
m_ImageLst.Add(AfxGetApp()->LoadIcon(IDR_CONNECT));
m_ImageLst.Add(AfxGetApp()->LoadIcon(IDR_SHARE));
m_ImageLst.Add(AfxGetApp()->LoadIcon(IDR_TRANSFERS));
m_ImageLst.Add(AfxGetApp()->LoadIcon(IDR_CHAT));
m_ImageLst.Add(AfxGetApp()->LoadIcon(IDR_STATISTICS));
m_ImageLst.Add(AfxGetApp()->LoadIcon(IDR_BROWSER));
m_ImageLst.Add(AfxGetApp()->LoadIcon(IDR_SEARCH));
m_ctlWindowList.SetImageList(&m_ImageLst);
return bRet;
}
BEGIN_MESSAGE_MAP(CWindowListDlg, CDialogBar)
//{{AFX_MSG_MAP(CWindowListDlg)
ON_NOTIFY(TCN_SELCHANGE, IDC_WINDOW_TABS, OnSelchangeWindowTabs)
ON_WM_SIZE()
ON_WM_CONTEXTMENU()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CWindowListDlg::OnSelchangeWindowTabs(NMHDR* pNMHDR, LRESULT* pResult)
{
CWnd* pWnd = GetWnd(m_ctlWindowList.GetCurSel());
if (IsWindow(pWnd->m_hWnd))
{
WINDOWPLACEMENT placement = { sizeof(WINDOWPLACEMENT) };
pWnd->GetWindowPlacement(&placement);
if (placement.showCmd == SW_HIDE ||
placement.showCmd == SW_MINIMIZE ||
placement.showCmd == SW_SHOWMINIMIZED)
{
pWnd->ShowWindow(SW_SHOWNORMAL);
}
pWnd->BringWindowToTop();
}
*pResult = 0;
}
void CWindowListDlg::Activate(CWnd* pWnd)
{
if (!IsWindow(m_ctlWindowList.m_hWnd))
{
ASSERT(0);
return;
}
int nIndex = MyFind(pWnd);
if (nIndex == -1)
{
nIndex = AddNew(pWnd);
}
m_ctlWindowList.SetCurSel(nIndex);
}
void CWindowListDlg::Close(CWnd* pWnd)
{
int nIndex = MyFind(pWnd);
if (nIndex == -1)
{
// ASSERT(nIndex != -1);
return;
}
int nIcon = GetIconByWindowType(GetWnd(nIndex));
if (nIcon != eSearch)
{
unsigned long nBit = 1;;
nBit <<= nIcon;
m_Slots &= ~nBit;
}
m_ctlWindowList.DeleteItem(nIndex);
}
int CWindowListDlg::MyFind(CWnd* pWnd)
{
int nCount = m_ctlWindowList.GetItemCount();
for (int i = 0; i < nCount; ++i)
{
if (GetWnd(i) == pWnd)
{
return i;
}
}
return -1; // didn't find it.
}
void CWindowListDlg::Update(CWnd* pWnd)
{
int nIndex = MyFind(pWnd);
CString strTitle;
pWnd->GetWindowText(strTitle);
TCITEM aItem;
aItem.mask = TCIF_TEXT;
aItem.pszText = (char*)(LPCTSTR)strTitle;
m_ctlWindowList.SetItem(nIndex, &aItem);
}
int CWindowListDlg::AddNew(CWnd* pWnd)
{
CString strTitle;
pWnd->GetWindowText(strTitle);
int nIcon = GetIconByWindowType(pWnd);
int nIndex = FindSlot(nIcon);
if (nIndex >= 999)
{
m_ctlWindowList.InsertItem(TCIF_TEXT | TCIF_IMAGE | TCIF_PARAM, 999, strTitle, nIcon, (long)pWnd);
m_ctlWindowList.SetCurSel(nIndex);
}
else
{
m_ctlWindowList.InsertItem(TCIF_TEXT | TCIF_IMAGE | TCIF_PARAM, nIndex, strTitle, nIcon, (long)pWnd);
m_ctlWindowList.SetCurSel(nIndex);
}
// Little but in CTabCtrl, it's doesn't always repaint the background
m_ctlWindowList.RedrawWindow();
return nIndex;
}
int CWindowListDlg::FindSlot(int nIcon)
{
if (nIcon == eSearch)
{
return 999; // At end
}
unsigned long nBit = 1;
int nIndex = 0;
for (int i = 0; i < nIcon; ++i)
{
if (nBit & m_Slots)
{
++nIndex; // Counting the number of bits before our position
}
nBit <<= 1; // Shift left one
}
m_Slots |= nBit; // Fill the slot
return nIndex; // Return where to place
}
int CWindowListDlg::GetIconByWindowType(CWnd* pWnd)
{
int nIcon = -1;
if (pWnd->IsKindOf(RUNTIME_CLASS(CFrameConnect)))
{
nIcon = eConnect;
}
else if (pWnd->IsKindOf(RUNTIME_CLASS(CFrameShare)))
{
nIcon = eShare;
}
else if (pWnd->IsKindOf(RUNTIME_CLASS(CFrameTransfers)))
{
nIcon = eTransfers;
}
else if (pWnd->IsKindOf(RUNTIME_CLASS(CFrameChat)))
{
nIcon = eChat;
}
else if (pWnd->IsKindOf(RUNTIME_CLASS(CFrameStatistics)))
{
nIcon = eStatistics;
}
else if (pWnd->IsKindOf(RUNTIME_CLASS(CFrameBrowser)))
{
nIcon = eBrowser;
}
else if (pWnd->IsKindOf(RUNTIME_CLASS(CFrameSearch)))
{
nIcon = eSearch;
}
return nIcon;
}
void CWindowListDlg::OnSize(UINT nType, int cx, int cy)
{
CDialogBar::OnSize(nType, cx, cy);
if (::IsWindow(m_ctlWindowList.m_hWnd))
{
CRect win_size;
GetClientRect(&win_size);
CRect was;
m_ctlWindowList.GetWindowRect(was);
ScreenToClient(was);
win_size.top = was.top;
win_size.left = was.left;
CRect lastItem;
m_ctlWindowList.GetItemRect(m_ctlWindowList.GetItemCount() - 1, lastItem);
// if (lastItem.right > 0)
// {
// win_size.right = win_size.left + lastItem.right + 1;
// }
m_ctlWindowList.MoveWindow(win_size);
}
}
void CWindowListDlg::OnContextMenu(CWnd*, CPoint point)
{
CPoint ptClient(point);
m_ctlWindowList.ScreenToClient(&ptClient);
TCHITTESTINFO info;
info.flags = TCHT_ONITEM;
info.pt = ptClient;
int nIndex = m_ctlWindowList.HitTest(&info);
CWnd* pWnd = NULL;
if (nIndex != -1)
{
CMenu menu;
menu.LoadMenu(IDR_WNDLIST_RCLICK);
CMenu *pMenu = menu.GetSubMenu(0);
ASSERT(pMenu != NULL);
WINDOWPLACEMENT placement = { sizeof(WINDOWPLACEMENT) };
pWnd = GetWnd(nIndex);
pWnd->GetWindowPlacement(&placement);
if (placement.showCmd == SW_HIDE)
{
menu.EnableMenuItem(ID_WNDLIST_CLOSE, MF_BYCOMMAND | MF_GRAYED);
}
else if (placement.showCmd == SW_SHOWMAXIMIZED)
{
menu.EnableMenuItem(ID_WNDLIST_MAX, MF_BYCOMMAND | MF_GRAYED);
}
else if (placement.showCmd == SW_SHOWMINIMIZED)
{
menu.EnableMenuItem(ID_WNDLIST_MIN, MF_BYCOMMAND | MF_GRAYED);
}
else if (m_ctlWindowList.GetCurSel() == nIndex)
{
menu.EnableMenuItem(ID_WNDLIST_SHOW, MF_BYCOMMAND | MF_GRAYED);
}
pWnd->GetWindowPlacement(&placement);
int res = pMenu->TrackPopupMenu( (TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD),
point.x, point.y, this);
switch(res)
{
case ID_WNDLIST_CLOSE:
pWnd->PostMessage(WM_CLOSE);
break;
case ID_WNDLIST_MAX:
placement.showCmd = SW_SHOWMAXIMIZED;
pWnd->SetWindowPlacement(&placement);
break;
case ID_WNDLIST_MIN:
placement.showCmd = SW_SHOWMINIMIZED;
pWnd->SetWindowPlacement(&placement);
break;
case ID_WNDLIST_SHOW:
pWnd->ShowWindow(SW_SHOWNORMAL);
pWnd->BringWindowToTop();
break;
}
}
}
CWnd* CWindowListDlg::GetWnd(int nIndex)
{
TCITEM aItem;
aItem.mask = TCIF_PARAM;
m_ctlWindowList.GetItem(nIndex, &aItem);
CWnd* pWnd = (CWnd*)aItem.lParam;
return pWnd;
}