Initial
This commit is contained in:
232
mdiwin/TOOLBAR.CPP
Normal file
232
mdiwin/TOOLBAR.CPP
Normal file
@@ -0,0 +1,232 @@
|
||||
#include <mdiwin/main.hpp>
|
||||
#include <mdiwin/toolbar.hpp>
|
||||
|
||||
char ToolBar::szClassName[]="Rocinante";
|
||||
char ToolBar::szMenuName[]={'\0'};
|
||||
|
||||
ToolBar::ToolBar(HWND hParent,String caption)
|
||||
: mButtons(0), mButtonDown(FALSE), mIsDestroyed(FALSE),
|
||||
mWindowWidth(0), mCaption(caption), mhParent(hParent),
|
||||
mhInstance(Main::processInstance())
|
||||
{
|
||||
registerClass();
|
||||
::CreateWindow((LPSTR)szClassName,(LPSTR)mCaption,
|
||||
WS_CHILD|WS_CAPTION,
|
||||
CW_USEDEFAULT,CW_USEDEFAULT,
|
||||
CW_USEDEFAULT,WindowHeight,
|
||||
mhParent,(HMENU)0x01,mhInstance,(LPSTR)this);
|
||||
synchronizeToolBar();
|
||||
Show(SW_HIDE);
|
||||
Update();
|
||||
}
|
||||
|
||||
ToolBar::~ToolBar()
|
||||
{
|
||||
for(int i=0;i<mButtons;i++)::DeleteObject(mButton[i].mhBitmap);
|
||||
if(!mIsDestroyed && GetHandle())::SendMessage(GetHandle(),WM_DESTROY,(WPARAM)GetHandle(),0L);
|
||||
}
|
||||
|
||||
void ToolBar::showWindow(int visible)
|
||||
{
|
||||
if(visible)
|
||||
{
|
||||
setWidth(LastVisible);
|
||||
Show(SW_SHOW);
|
||||
}
|
||||
else
|
||||
{
|
||||
synchronizeToolBar();
|
||||
Show(SW_HIDE);
|
||||
}
|
||||
Update();
|
||||
}
|
||||
|
||||
void ToolBar::registerClass(void)
|
||||
{
|
||||
WNDCLASS wndClass;
|
||||
|
||||
if(::GetClassInfo(mhInstance,szClassName,(WNDCLASS FAR *)&wndClass))return;
|
||||
wndClass.style =CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS|CS_SAVEBITS;
|
||||
wndClass.lpfnWndProc =(WNDPROC)Window::WndProc;
|
||||
wndClass.cbClsExtra =0;
|
||||
wndClass.cbWndExtra =sizeof(ToolBar*);
|
||||
wndClass.hInstance =mhInstance;
|
||||
wndClass.hIcon =0;
|
||||
wndClass.hCursor =::LoadCursor(NULL,IDC_ARROW);
|
||||
wndClass.hbrBackground=(HBRUSH)::GetStockObject(LTGRAY_BRUSH);
|
||||
wndClass.lpszMenuName =szMenuName;
|
||||
wndClass.lpszClassName=szClassName;
|
||||
::RegisterClass(&wndClass);
|
||||
}
|
||||
|
||||
long ToolBar::WndProc(UINT message,WPARAM wParam,LPARAM lParam)
|
||||
{
|
||||
switch(message)
|
||||
{
|
||||
case WM_CREATE :
|
||||
::SendMessage(GetHandle(),WM_NCACTIVATE,TRUE,0L);
|
||||
return FALSE;
|
||||
case WM_SIZE :
|
||||
return FALSE;
|
||||
case WM_PAINT :
|
||||
Paint();
|
||||
return FALSE;
|
||||
case WM_LBUTTONUP :
|
||||
LeftButtonUp();
|
||||
return FALSE;
|
||||
case WM_LBUTTONDOWN :
|
||||
LeftButtonDown(LOWORD(lParam),HIWORD(lParam));
|
||||
return FALSE;
|
||||
case WM_MOUSEMOVE :
|
||||
mouseMove(LOWORD(lParam),HIWORD(lParam));
|
||||
return FALSE;
|
||||
case WM_DESTROY :
|
||||
handleDestroyEvent();
|
||||
return FALSE;
|
||||
}
|
||||
return ::DefWindowProc(GetHandle(),message,wParam,lParam);
|
||||
}
|
||||
|
||||
void ToolBar::Paint(void)
|
||||
{
|
||||
HBITMAP hOldBitMap;
|
||||
HDC hDisplayDC;
|
||||
HDC hMemDC;
|
||||
PAINTSTRUCT ps;
|
||||
|
||||
if(::IsIconic(GetHandle()))
|
||||
{
|
||||
Show(SW_SHOWMAXIMIZED);
|
||||
setWidth(LastVisible);
|
||||
}
|
||||
hDisplayDC=::BeginPaint(GetHandle(),&ps);
|
||||
for(int i=0;i<mButtons;i++)
|
||||
{
|
||||
hMemDC=::CreateCompatibleDC(hDisplayDC);
|
||||
hOldBitMap=(HBITMAP)::SelectObject(hMemDC,mButton[i].mhBitmap);
|
||||
::BitBlt(hDisplayDC,mButton[i].mRect.left,0,
|
||||
mButton[i].mRect.right-mButton[i].mRect.left+1,
|
||||
mButton[i].mRect.bottom,hMemDC,0,0,SRCCOPY);
|
||||
::SelectObject(hMemDC,hOldBitMap);
|
||||
::DeleteDC(hMemDC);
|
||||
}
|
||||
::EndPaint(GetHandle(),&ps);
|
||||
}
|
||||
|
||||
void ToolBar::Paint(WORD BitMap)
|
||||
{
|
||||
HBITMAP hOldBitMap;
|
||||
HDC hDisplayDC;
|
||||
HDC hMemDC;
|
||||
|
||||
hDisplayDC=::GetDC(GetHandle());
|
||||
hMemDC=::CreateCompatibleDC(hDisplayDC);
|
||||
hOldBitMap=(HBITMAP)::SelectObject(hMemDC,mButton[BitMap].mhBitmap);
|
||||
::BitBlt(hDisplayDC,mButton[BitMap].mRect.left,0,
|
||||
mButton[BitMap].mRect.right-mButton[BitMap].mRect.left,
|
||||
mButton[BitMap].mRect.bottom,hMemDC,0,0,DSTINVERT);
|
||||
::SelectObject(hMemDC,hOldBitMap);
|
||||
::DeleteDC(hMemDC);
|
||||
::ReleaseDC(GetHandle(),hDisplayDC);
|
||||
}
|
||||
|
||||
WORD ToolBar::addBitmap(String bmpString,UINT message,WPARAM wParam,LPARAM lParam)
|
||||
{
|
||||
BITMAP bitmapInfo;
|
||||
|
||||
if(mButtons+1>MaxButtons)return FALSE;
|
||||
if(0==(mButton[mButtons].mhBitmap=::LoadBitmap(mhInstance,(LPSTR)bmpString)))return FALSE;
|
||||
mButton[mButtons].mMessage=message;
|
||||
mButton[mButtons].mwParam=wParam;
|
||||
mButton[mButtons].mlParam=lParam;
|
||||
::GetObject(mButton[mButtons].mhBitmap,sizeof(BITMAP),(LPSTR)&bitmapInfo);
|
||||
if(!mButtons)
|
||||
::SetRect((RECT FAR *)&mButton[mButtons].mRect,0,0,bitmapInfo.bmWidth,bitmapInfo.bmHeight);
|
||||
else
|
||||
SetRect((RECT FAR *)&mButton[mButtons].mRect,mButton[mButtons-1].mRect.right,0,mButton[mButtons-1].mRect.right+bitmapInfo.bmWidth,bitmapInfo.bmHeight);
|
||||
setWidth(LastVisible);
|
||||
if(::IsWindowVisible(GetHandle()))::InvalidateRect(GetHandle(),&mButton[mButtons].mRect,FALSE);
|
||||
mButtons++;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void ToolBar::LeftButtonDown(WORD xPos,WORD yPos)
|
||||
{
|
||||
mButtonDown=isInButton(xPos,yPos);
|
||||
if(!HIWORD(mButtonDown))return;
|
||||
Paint(LOWORD(mButtonDown));
|
||||
::SetCapture(GetHandle());
|
||||
}
|
||||
|
||||
void ToolBar::LeftButtonUp()
|
||||
{
|
||||
if(!HIWORD(mButtonDown))return;
|
||||
::InvalidateRect(GetHandle(),&mButton[LOWORD(mButtonDown)].mRect,FALSE);
|
||||
::UpdateWindow(GetHandle());
|
||||
::PostMessage(GetParent(GetHandle()),mButton[LOWORD(mButtonDown)].mMessage,
|
||||
mButton[LOWORD(mButtonDown)].mwParam,mButton[LOWORD(mButtonDown)].mlParam);
|
||||
mButtonDown=FALSE;
|
||||
::ReleaseCapture();
|
||||
}
|
||||
|
||||
void ToolBar::mouseMove(WORD xPos,WORD yPos)
|
||||
{
|
||||
DWORD mouseOverButton;
|
||||
|
||||
if(!mButtonDown)return;
|
||||
mouseOverButton=isInButton(xPos,yPos);
|
||||
if(!HIWORD(mouseOverButton))
|
||||
{
|
||||
::InvalidateRect(GetHandle(),&mButton[LOWORD(mButtonDown)].mRect,FALSE);
|
||||
mButtonDown=FALSE;
|
||||
::ReleaseCapture();
|
||||
}
|
||||
else if(LOWORD(mouseOverButton)!=LOWORD(mButtonDown))
|
||||
{
|
||||
::InvalidateRect(GetHandle(),&mButton[LOWORD(mButtonDown)].mRect,FALSE);
|
||||
mButtonDown=FALSE;
|
||||
::ReleaseCapture();
|
||||
}
|
||||
}
|
||||
|
||||
DWORD ToolBar::isInButton(WORD xPos,WORD yPos)const
|
||||
{
|
||||
for(int i=0;i<mButtons;i++)
|
||||
{
|
||||
if((xPos>= mButton[i].mRect.left && xPos<=mButton[i].mRect.right)
|
||||
&& (yPos>=mButton[i].mRect.top && yPos<=mButton[i].mRect.bottom))
|
||||
return (i+0x10000UL);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ToolBar::setWidth(Position position)
|
||||
{
|
||||
RECT parentRect;
|
||||
POINT cP;
|
||||
|
||||
mWindowWidth=0;
|
||||
for(int i=0;i<mButtons;i++)mWindowWidth+=(mButton[i].mRect.right-mButton[i].mRect.left);
|
||||
::GetWindowRect(mhParent,(RECT FAR *)&parentRect);
|
||||
if(UpperLeft==position)::memset((char*)&cP,0,sizeof(POINT));
|
||||
else
|
||||
{
|
||||
cP.x=parentRect.left+mLastOffset.x;
|
||||
cP.y=parentRect.top+mLastOffset.y;
|
||||
::ScreenToClient(mhParent,(POINT FAR *)&cP);
|
||||
}
|
||||
::MoveWindow(GetHandle(),cP.x,cP.y,mWindowWidth,WindowHeight,TRUE);
|
||||
}
|
||||
|
||||
void ToolBar::synchronizeToolBar(void)
|
||||
{
|
||||
RECT parentRect;
|
||||
RECT toolBarRect;
|
||||
|
||||
::GetWindowRect(mhParent,(RECT FAR *)&parentRect);
|
||||
::GetWindowRect(GetHandle(),(RECT FAR *)&toolBarRect);
|
||||
mLastOffset.x=toolBarRect.left-parentRect.left;
|
||||
mLastOffset.y=toolBarRect.top-parentRect.top;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user