-
Notifications
You must be signed in to change notification settings - Fork 21
/
ToolPanel.h
698 lines (603 loc) · 18.7 KB
/
ToolPanel.h
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
// Copyright (C) FlatGlobus([email protected]) All rights reserved.
//
// This file is a part of the WTLBuilder.
// The use and distribution terms for this software are covered by the
// Microsoft Public License (http://opensource.org/licenses/MS-PL)
// which can be found in the file MS-PL.txt at the root folder.
#ifndef __TOOLPANEL_H
#define __TOOLPANEL_H
//////////////////////////////////////////////////////////////////////////
#include <atlbase.h>
#include <atlapp.h>
extern CAppModule _Module;
#include <GDIUtil.h>
#include <atlcrack.h>
#include <atltheme.h>
#include "_util.h"
#define _BUTTON_WIDTH 10
#define WM_SELCHANGEPANEL WM_USER+255
#define WM_SELCHANGE WM_USER+256
#define BTN_WIDTH 27
#define BTN_HEIGHT 27
#define PAGE_IMG_WIDTH 16
#define PAGE_IMG_HEIGHT 16
class CButtonInfo
{
public:
CButtonInfo():id(-1)
{}
CButtonInfo(const CString &_name,DWORD _id):name(_name),id(_id)
{}
CString name;
DWORD id;
};
typedef CSimpleArray<CButtonInfo> CButtonInfoArray;
class CToolPanel:
public CWindowImpl< CToolPanel, CWindow, CControlWinTraits >, public CThemeImpl<CToolPanel>
{
protected:
CImageList images;
static long selected;
int visibleCount;
int imageCount;
int colWidth;
int colHeight;
int firstVisible;
int lastVisible;
int curToolTipItem;
BOOL leftPressed;
BOOL rightPressed;
static CButtonInfoArray buttons;
CString name;
WORD pageID;
CToolTipCtrl m_toolTip;
public:
CToolPanel(const CString & str,WORD _pageID,int cx=BTN_WIDTH,int cy=BTN_HEIGHT):colHeight(cy),colWidth(cx),firstVisible(0),lastVisible(0),
leftPressed(FALSE),rightPressed(FALSE),name(str),pageID(_pageID),imageCount(0),curToolTipItem(-1)
{
}
BEGIN_MSG_MAP(CToolPanel)
CHAIN_MSG_MAP(CThemeImpl<CToolPanel>)
MESSAGE_RANGE_HANDLER(WM_MOUSEFIRST, WM_MOUSELAST, OnMouseMessage)
MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
MESSAGE_HANDLER(WM_LBUTTONUP, OnLButtonUp)
MESSAGE_HANDLER(WM_MOUSEMOVE, OnMouseMove)
MESSAGE_HANDLER(WM_LBUTTONDBLCLK,OnLButtonDblClk)
MESSAGE_HANDLER(WM_PAINT, OnPaint)
MESSAGE_HANDLER(WM_SIZE, OnSize)
REFLECTED_NOTIFY_CODE_HANDLER_EX(TTN_NEEDTEXTW,OnNeedText)
END_MSG_MAP()
HWND Create(HWND Parent,CRect & rc,UINT id)
{
CWindowImpl<CToolPanel>::Create(Parent,rc,NULL,WS_CHILD,0,id);
DWORD clsStyle=::GetClassLong(m_hWnd,GCL_STYLE);
clsStyle^=CS_DBLCLKS;
::SetClassLong(m_hWnd,GCL_STYLE,clsStyle);
images.Create(colWidth-2,colHeight-2,ILC_COLOR8|ILC_MASK,0,1);
images.SetBkColor(RGB(192,192,192)/*CLR_NONE*/ /*::GetSysColor(COLOR_BTNFACE)*/);
CalculateVisibleCount();
m_toolTip.Create(*this,NULL,NULL,TTS_ALWAYSTIP,WS_EX_TOPMOST);
// Create inactive
m_toolTip.Activate(false);
// Enable multiline
m_toolTip.SetMaxTipWidth(400);
CRect rect;
GetClientRect(rect);
m_toolTip.AddTool(*this,_T(""), rect,1);
m_toolTip.Activate(TRUE);
return m_hWnd;
}
LRESULT OnMouseMessage(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
if(m_toolTip.IsWindow())
m_toolTip.RelayEvent((LPMSG)m_pCurrentMsg);
bHandled = FALSE;
return TRUE;
}
LRESULT OnNeedText(LPNMHDR pnmh)
{
TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pnmh;
pTTT->lpszText=_T("");
return 0;
}
LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
CPoint point;
point.x = LOWORD(lParam);
point.y = HIWORD(lParam);
if(visibleCount < imageCount)
{
CRect clientRc;
GetClientRect(&clientRc);
CRect leftRect(clientRc);
leftRect.right=leftRect.left+_BUTTON_WIDTH;
if(leftRect.PtInRect(point))
{
ScrollRight();
leftPressed=TRUE;
}
CRect rightRect(clientRc);
rightRect.left=rightRect.right-_BUTTON_WIDTH;
if(rightRect.PtInRect(point))
{
ScrollLeft();
rightPressed=TRUE;
}
}
if(!rightPressed && !leftPressed)
{
selected=IndexFromPoint(CPoint(point.x-(visibleCount < imageCount ? _BUTTON_WIDTH : 0),point.y));
if(LOWORD(selected) < imageCount)
::PostMessage(GetParent(),WM_SELCHANGEPANEL,(WPARAM)m_hWnd,selected);
else
selected=-1;
}
Invalidate();
return 0;
}
LRESULT OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
CPoint point;
point.x = LOWORD(lParam);
point.y = HIWORD(lParam);
long sel=MAKELONG(IndexFromPoint(CPoint(point.x-(visibleCount < imageCount ? _BUTTON_WIDTH : 0),point.y)),pageID);
if(sel!=curToolTipItem)
{
curToolTipItem = sel;
CString cmpPage, cmpName;
ExtractName(get_SelectedName(sel), cmpPage, cmpName);
m_toolTip.UpdateTipText(ATL::_U_STRINGorID(cmpName),(HWND)*this,1);
}
return 0;
}
LRESULT OnLButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
if(leftPressed || rightPressed)
{
leftPressed=FALSE;
rightPressed=FALSE;
Invalidate();
}
return 0;
}
LRESULT OnLButtonDblClk(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
m_toolTip.RelayEvent((LPMSG)GetCurrentMessage());
CPoint point;
point.x = LOWORD(lParam);
point.y = HIWORD(lParam);
CRect clientRc;
GetClientRect(&clientRc);
CRect leftRect(clientRc);
leftRect.right=leftRect.left+_BUTTON_WIDTH;
if(leftRect.PtInRect(point))
{
leftPressed=TRUE;
ScrollRight();
Invalidate();
}
CRect rightRect(clientRc);
rightRect.left=rightRect.right-_BUTTON_WIDTH;
if(rightRect.PtInRect(point))
{
rightPressed=TRUE;
ScrollLeft();
Invalidate();
}
if(!rightPressed && !leftPressed)
{
selected=IndexFromPoint(CPoint(point.x-_BUTTON_WIDTH,point.y));
if(selected!=-1)
::PostMessage(GetParent(),WM_SELCHANGEPANEL,(WPARAM)m_hWnd,selected);
}
rightPressed=leftPressed=FALSE;
Invalidate();
return 0;
}
LRESULT OnPaint(UINT, WPARAM,LPARAM,BOOL&)
{
CPaintDC pdc(m_hWnd);
CRect clientRect;
GetClientRect(clientRect);
CPaintDCEx dc(pdc,clientRect);
PaintGrid(dc);
return FALSE;
}
LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
CRect rc;
GetClientRect(&rc);
CalculateVisibleCount();
bHandled = FALSE;
return 0;
}
CString SplitName(const CString& cmdName)
{
if(cmdName.IsEmpty()==FALSE)
return name+_T(":")+cmdName;
return _T("");
}
int Find(const CString& cmdName)
{
CString str=SplitName(cmdName);
for(int i=0; i < buttons.GetSize();i++)
{
if(buttons[i].name==str)
return i;
}
return -1;
}
BOOL AddImage(HBITMAP bitmap,const CString& cmdName)
{
int ret=FALSE;
ret=images.Add(bitmap,RGB(192,192,192));
if(ret!=-1)
{
imageCount=images.GetImageCount();
if(Find(cmdName)!=-1)
return ret!=-1;
buttons.Add(CButtonInfo(SplitName(cmdName),MAKELONG(ret,pageID)));
InvalidateRect(NULL,TRUE);
}
return ret!=-1;
}
BOOL AddImage(ATL::_U_STRINGorID bitmap,const CString& cmdName)
{
int ret=FALSE;
HBITMAP hBitmap=::LoadBitmap(_Module.GetResourceInstance(), bitmap.m_lpstr);
return AddImage(hBitmap,cmdName);
}
void PaintGrid(CDC & dc)
{
CloseThemeData();
OpenThemeData (L"TAB");
CRect clientRc;
GetClientRect(&clientRc);
if(m_hTheme == NULL)
dc.FillSolidRect(&clientRc,GetSysColor(COLOR_3DFACE));
else
DrawThemeBackground(dc,TABP_BODY,0,&clientRc,NULL);
if(images.IsNull())
return;
int imCount=images.GetImageCount();
int shift=0;
if(visibleCount < imageCount)
shift=_BUTTON_WIDTH;
int colCount=min(visibleCount,imageCount);
for(int col=0;col < colCount;col++)
{
if(MAKELONG((firstVisible+col),pageID)==selected)
dc.Draw3dRect(shift+col*colWidth, 0, colWidth,colHeight,RGB(0,0,0),RGB(255,255,255));
images.Draw(dc,firstVisible+col,CPoint(shift+col*colWidth+1,1),ILD_TRANSPARENT/*ILD_NORMAL*/);
}
if(visibleCount < imageCount)
{
DrawLeftScroolButton(dc);
DrawRightScroolButton(dc);
}
}
long IndexFromPoint(CPoint & point)
{
return MAKELONG((firstVisible+(point.x / (colWidth))),pageID);
}
int get_Selected()
{
return selected;
}
void UnSelect()
{
selected=-1;
Invalidate();
::PostMessage(GetParent(),WM_SELCHANGEPANEL,(WPARAM)m_hWnd,selected);
}
void SetColWidth(int cx)
{
if(colWidth != cx)
{
colWidth = cx;
CalculateVisibleCount();
Invalidate();
}
}
void DrawLeftScroolButton(CDC & dc)
{
CloseThemeData();
OpenThemeData (L"BUTTON");
CRect rc;
GetClientRect(&rc);
rc.right=rc.left+_BUTTON_WIDTH;
rc.bottom=27;
if(m_hTheme == NULL)
{
dc.FillSolidRect(rc,GetSysColor(COLOR_BTNFACE));
if(!leftPressed)
dc.Draw3dRect(rc,RGB(255,255,255),RGB(0,0,0));
else
{
dc.Draw3dRect(rc,RGB(0,0,0),RGB(255,255,255));
rc.left+=1;
}
}
else
DrawThemeBackground(dc,BP_PUSHBUTTON,leftPressed ? PBS_PRESSED : PBS_NORMAL,&rc,NULL);
DrawLeftArrow(dc,CPoint(rc.left+2,(rc.top+rc.bottom)/2));
}
void DrawRightScroolButton(CDC & dc)
{
CloseThemeData();
OpenThemeData (L"BUTTON");
CRect rc;
GetClientRect(&rc);
rc.left=rc.right-_BUTTON_WIDTH;
rc.bottom = 27;
if(m_hTheme == NULL)
{
dc.FillSolidRect(rc,GetSysColor(COLOR_BTNFACE));
if(!rightPressed)
dc.Draw3dRect(rc,RGB(255,255,255),RGB(0,0,0));
else
{
dc.Draw3dRect(rc,RGB(0,0,0),RGB(255,255,255));
rc.right+=1;
}
}
else
DrawThemeBackground(dc,BP_PUSHBUTTON,leftPressed ? PBS_PRESSED : PBS_NORMAL,&rc,NULL);
DrawRightArrow(dc,CPoint(rc.right-3,(rc.top+rc.bottom)/2));
}
void DrawRightArrow(CDC & dc,CPoint at,COLORREF fillColor=RGB(0x0,0x0,0x0))
{
POINT pts[]={
{-4,0},
{-4,-4},
{0,0},
{-4,4},
{-4,0}
};
CPen Pen;
Pen.CreatePen(PS_INSIDEFRAME,1,RGB(0,0,0));
dc.SelectPen(Pen);
CBrush Brush;
Brush.CreateSolidBrush(fillColor);
dc.SelectBrush(Brush);
for(int i=0; i < 5;i++)
{
pts[i].x+=at.x;
pts[i].y+=at.y;
}
dc.Polygon(pts,5);
}
// <-
void DrawLeftArrow(CDC & dc,CPoint at,COLORREF fillColor=RGB(0x0,0x0,0x0))
{
POINT pts[]={
{4,0},
{4,-4},
{0,0},
{4,4},
{4,0}
};
CPen Pen;
Pen.CreatePen(PS_INSIDEFRAME,1,RGB(0,0,0));
dc.SelectPen(Pen);
CBrush Brush;
Brush.CreateSolidBrush(fillColor);
dc.SelectBrush(Brush);
for(int i=0; i < 5;i++)
{
pts[i].x+=at.x;
pts[i].y+=at.y;
}
dc.Polygon(pts,5);
}
void ScrollRight()
{
if(images.IsNull())
return;
CRect clientRect;
GetClientRect(clientRect);
firstVisible=firstVisible!=0 ? firstVisible-1 : 0;
}
void ScrollLeft()
{
if(images.IsNull())
return;
if(firstVisible ==(imageCount-visibleCount))
return;
firstVisible=firstVisible < (imageCount-visibleCount) ? firstVisible+1 : imageCount-visibleCount+1;
}
void CalculateVisibleCount(void)
{
CRect clientRect;
GetClientRect(clientRect);
visibleCount=clientRect.Width() / colWidth;
if(visibleCount >= imageCount)
firstVisible=0;
}
const CString & GetName()
{
return name;
}
static CString get_SelectedName(void)
{
for(int i=0; i < buttons.GetSize();i++)
if(buttons[i].id==selected)
return buttons[i].name;
return _T("");
}
CString get_SelectedName(int idx)
{
for(int i=0; i < buttons.GetSize();i++)
if(buttons[i].id==idx)
return buttons[i].name;
return _T("");
}
static void ReleaseButtonArray()
{
buttons.RemoveAll();
}
};
//////////////////////////////////////////////////////////////////////////
typedef CSimpleArray<CToolPanel *> CPanelArray;
class CToolTab :
public CWindowImpl<CToolTab, CTabCtrl>,
public CDynamicChain
{
public:
DECLARE_WND_SUPERCLASS(NULL, CTabCtrl::GetWndClassName())
CPanelArray pages;
int currentPanel;
HWND m_hWndClient;
CImageList imageList;
BEGIN_MSG_MAP(CToolTab)
MESSAGE_HANDLER(WM_SIZE, OnSize)
MESSAGE_HANDLER(WM_SELCHANGEPANEL,OnChange);
REFLECTED_NOTIFY_CODE_HANDLER_EX(TCN_SELCHANGE, OnTabSelect)
END_MSG_MAP()
CToolTab():m_hWndClient(NULL),currentPanel(-1)
{
}
virtual ~CToolTab()
{
for(int i=0; i < pages.GetSize();i++)
delete pages[i];
pages.RemoveAll();
CToolPanel::ReleaseButtonArray();
}
HWND Create(HWND hWndParent, CRect rc,int ID)
{
imageList.Create(PAGE_IMG_WIDTH,PAGE_IMG_HEIGHT,ILC_COLOR8|ILC_MASK,0,1);
//imageList.SetBkColor(RGB(192,192,192));
HWND tabWnd = CWindowImpl<CToolTab, CTabCtrl>::Create(hWndParent,rc,NULL,
WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | TCS_TABS | TCS_FOCUSNEVER,0,ID);
if(::IsWindow(tabWnd))
SetImageList(imageList);
return tabWnd;
}
LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
if(currentPanel!=-1)
{
m_hWndClient=*pages[currentPanel];
CRect client,item;
GetItemRect(currentPanel,&item);
GetClientRect(&client);
client.top = item.bottom + 1;
if(::IsWindow(m_hWndClient))
::MoveWindow(m_hWndClient,client.left,client.top,client.Width(),client.Height(), FALSE);
}
return 0;
}
LRESULT OnTabSelect(LPNMHDR pnmh)
{
HWND hwndOld=NULL, hwndNew=NULL;
if(currentPanel!=-1)
hwndOld=*pages[currentPanel];
currentPanel=GetCurSel();
if(currentPanel!=-1)
{
hwndNew=*pages[currentPanel];
SetChainEntry(0,pages[currentPanel]);
}
m_hWndClient = hwndNew;
CRect item, client;
GetItemRect(currentPanel,&item);
GetClientRect(&client);
client.top = item.bottom+1;
if(::IsWindow(hwndOld))
::ShowWindow(hwndOld, SW_HIDE);
if (::IsWindow(hwndNew))
{
::MoveWindow(hwndNew, client.left, client.top, client.Width(), client.Height(), FALSE);
::ShowWindow(hwndNew, SW_SHOW);
}
return 0;
};
LRESULT OnChange(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
::SendMessage(::GetParent(m_hWnd),WM_SELCHANGE,(WPARAM)m_hWnd,lParam);
bHandled=FALSE;
return 0;
}
CToolPanel *AddPage(const CString & name)
{
CToolPanel * temp=GetPanel(name);
if(temp)
return temp;
HBITMAP bmp=NULL;
SendEvent(evGetPageBitmap,(LPCTSTR)name,&bmp);
int imgIdx = -1;
if(bmp!=NULL)
imgIdx=imageList.Add(bmp,RGB(192,192,192));
TCITEM tab = { 0 };
tab.mask = TCIF_TEXT|TCIF_IMAGE;
tab.pszText = (LPTSTR)(LPCTSTR)name;
tab.cchTextMax = name.GetLength();
tab.iImage = imgIdx;
int currIdx = AddItem(&tab);
SetFont(AtlGetStockFont(ANSI_VAR_FONT));
temp=new CToolPanel(name,currIdx);
pages.Add(temp);
CRect client,item;
GetItemRect(currIdx,&item);
GetClientRect(&client);
client.top = item.bottom+1;
temp->Create(m_hWnd, client,100 + currIdx);
temp->AddImage(IDB_ARROW,_T(""));
BOOL isFirst = FALSE;
SendEvent(evIsFirstPage, (LPCTSTR)name, &isFirst);
if(isFirst == TRUE)
{
currentPanel= currIdx;
m_hWndClient=*pages[currentPanel];
SetChainEntry(0, pages[currentPanel]);
SetCurSel(currentPanel);
pages[currentPanel]->ShowWindow(SW_SHOW);
}
return temp;
}
CToolPanel *GetPanel(int idx)
{
return pages[idx];
}
CToolPanel *GetPanel(const CString& str)
{
for(int i=0; i < pages.GetSize(); i++)
if(pages[i]->GetName()==str)
return pages[i];
return NULL;
}
BOOL AddImage(const CString& str,HBITMAP bitmap,const CString& cmdName)
{
CToolPanel *panel=GetPanel(str);
if(panel==NULL)
{
panel = AddPage(str);
if(panel == NULL)
return FALSE;
}
BOOL ret=panel->AddImage(bitmap,cmdName);
return ret;
}
BOOL AddImage(const CString& str,ATL::_U_STRINGorID bitmap,const CString& cmdName)
{
CToolPanel *panel=GetPanel(str);
if(panel==NULL)
{
if((panel=AddPage(str))==NULL)
return FALSE;
}
return panel->AddImage(bitmap,cmdName);
}
CString get_SelectedName(void)
{
return CToolPanel::get_SelectedName();
}
void UnSelect()
{
for(int i=0; i < pages.GetSize(); i++)
pages[i]->UnSelect();
}
};
//////////////////////////////////////////////////////////////////////////
#endif