ppsspp/Windows/W32Util/TabControl.cpp

185 lines
4.2 KiB
C++
Raw Normal View History

2013-09-28 20:57:02 +02:00
#include "TabControl.h"
#include "DialogManager.h"
#include "Windows/WndMainWindow.h"
#include <windowsx.h>
#include <commctrl.h>
const DWORD tabControlStyleMask = ~(WS_POPUP | WS_TILEDWINDOW);
2013-09-28 20:57:02 +02:00
TabControl::TabControl(HWND handle): hwnd(handle)
{
SetWindowLongPtr(hwnd,GWLP_USERDATA,(LONG_PTR)this);
oldProc = (WNDPROC) SetWindowLongPtr(hwnd,GWLP_WNDPROC,(LONG_PTR)wndProc);
}
HWND TabControl::AddTabWindow(wchar_t* className, wchar_t* title, DWORD style)
{
style = (style |WS_CHILD) & tabControlStyleMask;
2013-09-28 20:57:02 +02:00
TCITEM tcItem;
ZeroMemory (&tcItem,sizeof (tcItem));
tcItem.mask = TCIF_TEXT;
tcItem.dwState = 0;
tcItem.pszText = title;
tcItem.cchTextMax = (int)wcslen(tcItem.pszText)+1;
tcItem.iImage = 0;
int index = TabCtrl_GetItemCount(hwnd);
int result = TabCtrl_InsertItem(hwnd,index,&tcItem);
RECT tabRect;
GetWindowRect(hwnd,&tabRect);
MapWindowPoints(HWND_DESKTOP,GetParent(hwnd),(LPPOINT)&tabRect,2);
TabCtrl_AdjustRect(hwnd, FALSE, &tabRect);
HWND tabHandle = CreateWindowEx(0,className,title,style,
tabRect.left,tabRect.top,tabRect.right-tabRect.left,tabRect.bottom-tabRect.top,
GetParent(hwnd),0,MainWindow::GetHInstance(),0);
tabs.push_back(tabHandle);
ShowTab(index);
return tabHandle;
}
void TabControl::AddTabDialog(Dialog* dialog, wchar_t* title)
{
HWND handle = dialog->GetDlgHandle();
AddTab(handle,title);
}
2013-09-28 20:57:02 +02:00
void TabControl::AddTab(HWND handle, wchar_t* title)
{
2013-09-28 20:57:02 +02:00
TCITEM tcItem;
ZeroMemory (&tcItem,sizeof (tcItem));
tcItem.mask = TCIF_TEXT;
tcItem.dwState = 0;
tcItem.pszText = title;
tcItem.cchTextMax = (int)wcslen(tcItem.pszText)+1;
tcItem.iImage = 0;
int index = TabCtrl_GetItemCount(hwnd);
int result = TabCtrl_InsertItem(hwnd,index,&tcItem);
RECT tabRect;
GetWindowRect(hwnd,&tabRect);
MapWindowPoints(HWND_DESKTOP,GetParent(hwnd),(LPPOINT)&tabRect,2);
TabCtrl_AdjustRect(hwnd, FALSE, &tabRect);
SetParent(handle,GetParent(hwnd));
DWORD style = (GetWindowLong(handle,GWL_STYLE) | WS_CHILD) & tabControlStyleMask;
2013-09-28 20:57:02 +02:00
SetWindowLong(handle, GWL_STYLE, style);
MoveWindow(handle,tabRect.left,tabRect.top,tabRect.right-tabRect.left,tabRect.bottom-tabRect.top,TRUE);
tabs.push_back(handle);
ShowTab(index);
}
void TabControl::ShowTab(int index, bool setControlIndex)
{
for (size_t i = 0; i < tabs.size(); i++)
{
ShowWindow(tabs[i],i == index ? SW_NORMAL : SW_HIDE);
}
if (setControlIndex)
{
TabCtrl_SetCurSel(hwnd,index);
}
}
void TabControl::ShowTab(HWND pageHandle)
{
for (size_t i = 0; i < tabs.size(); i++)
{
if (tabs[i] == pageHandle)
{
TabCtrl_SetCurSel(hwnd,i);
}
ShowWindow(tabs[i],tabs[i] == pageHandle ? SW_NORMAL : SW_HIDE);
}
}
int TabControl::CurrentTabIndex()
{
return TabCtrl_GetCurSel(hwnd);
}
2013-09-28 20:57:02 +02:00
void TabControl::NextTab(bool cycle)
{
int index = CurrentTabIndex()+1;
if (index == tabs.size())
{
if (cycle == false)
index--;
else
index = 0;
}
2013-09-28 20:57:02 +02:00
ShowTab(index);
}
void TabControl::PreviousTab(bool cycle)
{
int index = CurrentTabIndex()-1;
if (index < 0)
{
if (cycle == false)
index = 0;
else
index = (int) tabs.size()-1;
}
2013-09-28 20:57:02 +02:00
ShowTab(index);
}
void TabControl::HandleNotify(LPARAM lParam)
{
NMHDR* pNotifyMessage = NULL;
pNotifyMessage = (LPNMHDR)lParam;
if (pNotifyMessage->hwndFrom == hwnd)
{
int iPage = TabCtrl_GetCurSel(hwnd);
ShowTab(iPage,false);
}
}
void TabControl::OnResize()
{
RECT tabRect;
GetWindowRect(hwnd,&tabRect);
MapWindowPoints(HWND_DESKTOP,GetParent(hwnd),(LPPOINT)&tabRect,2);
2013-09-29 01:02:05 +02:00
InvalidateRect(hwnd,NULL,TRUE);
2013-09-28 20:57:02 +02:00
UpdateWindow(hwnd);
// now resize tab children
TabCtrl_AdjustRect(hwnd, FALSE, &tabRect);
int current = TabCtrl_GetCurSel(hwnd);
for (size_t i = 0; i < tabs.size(); i++)
{
InvalidateRect(tabs[i],NULL,FALSE);
2013-09-29 01:02:05 +02:00
MoveWindow(tabs[i],tabRect.left,tabRect.top,tabRect.right-tabRect.left,tabRect.bottom-tabRect.top,TRUE);
2013-09-28 20:57:02 +02:00
if (i == current)
{
2013-09-29 01:02:05 +02:00
InvalidateRect(tabs[i],NULL,TRUE);
2013-09-28 20:57:02 +02:00
UpdateWindow(tabs[i]);
}
}
}
LRESULT CALLBACK TabControl::wndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
TabControl* tabControl = (TabControl*) GetWindowLongPtr(hwnd,GWLP_USERDATA);
switch (msg)
{
case WM_SIZE:
tabControl->OnResize();
break;
}
return (LRESULT)CallWindowProc((WNDPROC)tabControl->oldProc,hwnd,msg,wParam,lParam);
}