forked from nickgammon/mushclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMDITabs.cpp
341 lines (293 loc) · 9.1 KB
/
MDITabs.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
/****************************************************************************\
Datei : MDITabs.h
Projekt: MDITabs, a tabcontrol for switching between MDI-views
Inhalt : CMDITabs implementation
Datum : 03.10.2001
Autor : Christian Rodemeyer
Hinweis: 2001 by Christian Rodemeyer
\****************************************************************************/
#include "stdafx.h"
#include "MDITabs.h"
#include <AFXPRIV.H>
#include <algorithm>
#include <vector>
#include "MUSHclient.h"
#include "TextDocument.h"
#include "TextView.h"
#include "doc.h"
#include "childfrm.h"
#include "textchildfrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMDITabs
CMDITabs::CMDITabs()
{
m_mdiClient = NULL;
m_minViews = 0;
m_bImages = false;
m_bTop = false;
}
BEGIN_MESSAGE_MAP(CMDITabs, CTabCtrl)
//{{AFX_MSG_MAP(CMDITabs)
ON_NOTIFY_REFLECT(TCN_SELCHANGE, OnSelChange)
ON_WM_PAINT()
ON_WM_NCPAINT()
ON_WM_CONTEXTMENU()
ON_WM_LBUTTONDBLCLK()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_SIZEPARENT, OnSizeParent)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMDITabs message handlers
afx_msg LRESULT CMDITabs::OnSizeParent(WPARAM, LPARAM lParam)
{
if (GetItemCount() < m_minViews)
{
ShowWindow(SW_HIDE);
}
else
{
AFX_SIZEPARENTPARAMS* pParams = reinterpret_cast<AFX_SIZEPARENTPARAMS*>(lParam);
const int height = 26 + (m_bImages ? 1 : 0);
const int offset = 2;
m_height = height + offset;
m_width = pParams->rect.right - pParams->rect.left;
if (m_bTop)
{
pParams->rect.top += height;
MoveWindow(pParams->rect.left, pParams->rect.top - height, m_width, m_height, true);
}
else
{
pParams->rect.bottom -= height;
MoveWindow(pParams->rect.left, pParams->rect.bottom - offset, m_width, m_height, true);
}
ShowWindow(SW_NORMAL);
}
return 0;
}
void CMDITabs::OnSelChange(NMHDR* pNMHDR, LRESULT* pResult)
{
TCITEM item;
item.mask = TCIF_PARAM;
GetItem(GetCurSel(), &item);
::BringWindowToTop(HWND(item.lParam));
*pResult = 0;
}
CString GetTabText (HWND hwnd)
{
CString strName;
char text[256];
CWnd * pWnd = CWnd::FromHandle (hwnd);
::GetWindowText(hwnd, text, sizeof text);
strName = text;
if (pWnd->IsKindOf(RUNTIME_CLASS(CChildFrame)))
{
CChildFrame * pWorldFrame = (CChildFrame *) pWnd;
CMUSHclientDoc * pDoc = pWorldFrame->m_pDoc;
// custom title?
if (pDoc->m_strWindowTitle.IsEmpty ())
strName = pDoc->m_mush_name;
else
strName = pDoc->m_strWindowTitle;
if (pDoc->m_new_lines && !pDoc->m_bDoNotShowOutstandingLines)
strName += CFormat (" (%i)", pDoc->m_new_lines);
}
else if (pWnd->IsKindOf(RUNTIME_CLASS(CTextChildFrame)))
{
CTextChildFrame * pNotepadFrame = (CTextChildFrame *) pWnd;
CTextDocument * pDoc = pNotepadFrame->m_pDoc;
if (!pDoc->m_strTitle.IsEmpty ())
strName = pDoc->m_strTitle;
if (pDoc->IsModified () && !pDoc->m_bReadOnly)
strName += " *";
}
strName.Replace ("&", "&&");
return strName;
}
void CMDITabs::Update()
{
SetRedraw(false);
HWND active = ::GetTopWindow(m_mdiClient); // get active view window (actually the frame of the view)
typedef std::vector<HWND> TWndVec;
typedef TWndVec::iterator TWndIter;
TWndVec vChild; // put all child windows in a list (actually a vector)
for (HWND child = active; child; child = ::GetNextWindow(child, GW_HWNDNEXT))
{
vChild.push_back(child);
}
TCITEM item;
// char text[256];
// item.pszText = text;
int i;
for (i = GetItemCount(); i--;) // for each tab
{
item.mask = TCIF_PARAM;
GetItem(i, &item);
TWndIter it = std::find(vChild.begin(), vChild.end(), HWND(item.lParam));
if (it == vChild.end()) // associatete view does no longer exist, so delete the tab
{
DeleteItem(i);
if (m_bImages) RemoveImage(i);
}
else // update the tab's text, image and selection state
{
item.mask = TCIF_TEXT;
// ::GetWindowText(*it, text, sizeof text);
CString sText = GetTabText (*it);
item.pszText = (char *) (LPCTSTR) sText;
if (m_bImages)
m_images.Replace(i, (HICON)::GetClassLong(*it, GCL_HICONSM));
SetItem(i, &item);
if (*it == active)
SetCurSel(i); // associated view is active => make it the current selection
vChild.erase(it); // remove view from list
}
}
// all remaining views in vChild have to be added as new tabs
i = GetItemCount();
for (TWndIter it = vChild.begin(), end = vChild.end(); it != end; ++it)
{
item.mask = TCIF_TEXT|TCIF_PARAM|TCIF_IMAGE;
// ::GetWindowText(*it, text, sizeof text);
CString sText = GetTabText (*it);
item.pszText = (char *) (LPCTSTR) sText;
if (m_bImages)
m_images.Add((HICON)::GetClassLong(*it, GCL_HICONSM));
item.iImage = i;
item.lParam = LPARAM(*it);
InsertItem(i, &item);
if (*it == active) SetCurSel(i);
++i;
}
// this removes the control when there are no tabs and shows it when there is at least one tab
bool bShow = GetItemCount() >= m_minViews;
if ((!bShow && IsWindowVisible()) || (bShow && !IsWindowVisible()))
{
static_cast<CMDIFrameWnd*>(FromHandlePermanent(::GetParent(m_mdiClient)))->RecalcLayout();
}
RedrawWindow(NULL, NULL, RDW_FRAME|RDW_INVALIDATE|RDW_ERASE);
SetRedraw(true);
}
void CMDITabs::OnPaint()
{
CPaintDC dc(this);
if (GetItemCount() == 0) return; // do nothing
int dcState = dc.SaveDC();
// windows should draw the control as usual
_AFX_THREAD_STATE* pThreadState = AfxGetThreadState();
pThreadState->m_lastSentMsg.wParam = WPARAM(HDC(dc));
Default();
dc.RestoreDC(dcState);
DWORD face = ::GetSysColor(COLOR_3DFACE);
if (m_bTop)
{
CRect rect(0, m_height - 7, m_width, m_height);
dc.FillSolidRect(&rect, face);
}
else
{
CRect rect(0, 0, m_width, 3);
dc.FillSolidRect(&rect, face);
}
}
void CMDITabs::OnNcPaint()
{
HDC hdc = ::GetWindowDC(m_hWnd);
CRect rect;
rect.left = 0;
rect.top = m_bTop ? 0 : -2;
rect.right = m_width;
rect.bottom = m_height;
HPEN pen = ::CreatePen(PS_SOLID, 0, ::GetSysColor(COLOR_3DFACE));
HGDIOBJ old = ::SelectObject(hdc, pen);
if (m_bTop)
{
DrawEdge(hdc, rect, EDGE_SUNKEN, BF_LEFT|BF_RIGHT|BF_TOP);
::MoveToEx(hdc, 2, m_height - 1, NULL);
::LineTo(hdc, m_width - 2, m_height - 1);
::MoveToEx(hdc, 2, m_height - 2, NULL);
::LineTo(hdc, m_width - 2, m_height - 2);
}
else
{
DrawEdge(hdc, rect, EDGE_SUNKEN, BF_LEFT|BF_RIGHT|BF_BOTTOM);
::MoveToEx(hdc, 2, 0, NULL);
::LineTo(hdc, m_width - 2, 0);
::MoveToEx(hdc, 2, 1, NULL);
::LineTo(hdc, m_width - 2, 1);
}
::SelectObject(hdc, old);
::DeleteObject(pen);
::ReleaseDC(m_hWnd, hdc);
}
void CMDITabs::Create(CFrameWnd* pMainFrame, DWORD dwStyle)
{
m_bTop = (dwStyle & MT_TOP);
m_minViews = (dwStyle & MT_HIDEWLT2VIEWS) ? 2 : 1;
CTabCtrl::Create(WS_CHILD|WS_VISIBLE|(m_bTop?0:TCS_BOTTOM)|TCS_SINGLELINE|TCS_FOCUSNEVER|TCS_FORCEICONLEFT|WS_CLIPSIBLINGS, CRect(0, 0, 0, 0), pMainFrame, 42);
ModifyStyleEx(0, WS_EX_CLIENTEDGE);
SendMessage(WM_SETFONT, WPARAM(GetStockObject(DEFAULT_GUI_FONT)), 0);
HWND wnd;
for (wnd = ::GetTopWindow(*pMainFrame); wnd; wnd = ::GetNextWindow(wnd, GW_HWNDNEXT))
{
char wndClass[32];
::GetClassName(wnd, wndClass, 32);
if (strncmp(wndClass, "MDIClient", 32) == 0) break;
}
m_mdiClient = wnd;
ASSERT(m_mdiClient); // Ooops, no MDIClient window?
// manipulate Z-order so, that our tabctrl is above the mdi client, but below any status bar
::SetWindowPos(m_hWnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
::SetWindowPos(m_mdiClient, m_hWnd, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
m_bImages = (dwStyle & MT_IMAGES) != 0;
if (m_bImages)
{
if (m_images.GetSafeHandle())
{
m_images.SetImageCount(0);
}
else
{
m_images.Create(16, 16, ILC_COLORDDB|ILC_MASK, 1, 1);
}
SetImageList(&m_images);
}
//SetItemSize(CSize(50, 0)); // Fixed Width Experiment
}
void CMDITabs::OnContextMenu(CWnd* pWnd, CPoint point)
{
TCHITTESTINFO hit;
hit.pt = point;
ScreenToClient(&hit.pt);
int i = HitTest(&hit);
if (i >= 0)
{
TCITEM item;
item.mask = TCIF_PARAM;
GetItem(i, &item);
HWND hWnd = HWND(item.lParam);
SetCurSel(i);
::BringWindowToTop(hWnd);
HMENU menu = HMENU(::SendMessage(::GetTopWindow(hWnd), WM_GETTABSYSMENU, 0, 0));
if (menu == 0) menu = ::GetSystemMenu(hWnd, FALSE);
UINT cmd = ::TrackPopupMenu(menu, TPM_RETURNCMD|TPM_RIGHTBUTTON|TPM_VCENTERALIGN, point.x, point.y, 0, m_hWnd, NULL);
::SendMessage(hWnd, WM_SYSCOMMAND, cmd, 0);
}
}
void CMDITabs::OnLButtonDblClk(UINT nFlags, CPoint point)
{
int i = GetCurSel();
if (i >= 0)
{
TCITEM item;
item.mask = TCIF_PARAM;
GetItem(i, &item);
HWND hWnd = HWND(item.lParam);
::ShowWindow(hWnd, SW_MAXIMIZE);
}
}