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
|
namespace phoenix {
static LRESULT CALLBACK TabFrame_windowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
Object* object = (Object*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
if(object == nullptr) return DefWindowProc(hwnd, msg, wparam, lparam);
TabFrame& tabFrame = (TabFrame&)*object;
return Shared_windowProc(tabFrame.p.windowProc, hwnd, msg, wparam, lparam);
}
void pTabFrame::append(string text, const image& image) {
unsigned selection = TabCtrl_GetItemCount(hwnd);
wchar_t wtext[] = L"";
TCITEM item;
item.mask = TCIF_TEXT;
item.pszText = wtext;
TabCtrl_InsertItem(hwnd, selection, &item);
setText(selection, text);
if(!image.empty()) setImage(selection, image);
}
void pTabFrame::remove(unsigned selection) {
TabCtrl_DeleteItem(hwnd, selection);
buildImageList();
}
void pTabFrame::setEnabled(bool enabled) {
pWidget::setEnabled(enabled);
for(auto& layout : tabFrame.state.layout) {
if(layout) layout->setEnabled(layout->enabled());
}
}
void pTabFrame::setGeometry(Geometry geometry) {
pWidget::setGeometry(geometry);
geometry.x += 1, geometry.width -= 4;
geometry.y += 21, geometry.height -= 23;
for(auto& layout : tabFrame.state.layout) {
if(layout) layout->setGeometry(geometry);
}
}
void pTabFrame::setImage(unsigned selection, const image& image) {
buildImageList();
}
void pTabFrame::setSelection(unsigned selection) {
TabCtrl_SetCurSel(hwnd, selection);
synchronizeLayout();
}
void pTabFrame::setText(unsigned selection, string text) {
utf16_t wtext(text);
TCITEM item;
item.mask = TCIF_TEXT;
item.pszText = (wchar_t*)wtext;
TabCtrl_SetItem(hwnd, selection, &item);
}
void pTabFrame::setVisible(bool visible) {
pWidget::setVisible(visible);
for(auto& layout : tabFrame.state.layout) {
if(layout) layout->setVisible(layout->visible());
}
}
void pTabFrame::constructor() {
hwnd = CreateWindow(WC_TABCONTROL, L"",
WS_CHILD | WS_TABSTOP,
0, 0, 0, 0, parentHwnd, (HMENU)id, GetModuleHandle(0), 0);
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)&tabFrame);
windowProc = (WindowProc)GetWindowLongPtr(hwnd, GWLP_WNDPROC);
SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)TabFrame_windowProc);
setDefaultFont();
for(auto& text : tabFrame.state.text) append(text, {});
buildImageList();
setSelection(tabFrame.state.selection);
synchronize();
}
void pTabFrame::destructor() {
if(imageList) { ImageList_Destroy(imageList); imageList = nullptr; }
DestroyWindow(hwnd);
}
void pTabFrame::orphan() {
destructor();
constructor();
}
void pTabFrame::buildImageList() {
if(imageList) ImageList_Destroy(imageList);
unsigned size = pFont::size(hfont, " ").height;
imageList = ImageList_Create(size, size, ILC_COLOR32, 1, 0);
for(auto& image : tabFrame.state.image) {
ImageList_Append(imageList, image, size);
}
TabCtrl_SetImageList(hwnd, imageList);
for(unsigned n = 0; n < tabFrame.state.image.size(); n++) {
TCITEM item;
item.mask = TCIF_IMAGE;
item.iImage = (tabFrame.state.image(n).empty() ? -1 : n);
TabCtrl_SetItem(hwnd, n, &item);
}
}
void pTabFrame::synchronizeLayout() {
unsigned selection = 0;
for(auto& layout : tabFrame.state.layout) {
if(layout) layout->setVisible(selection == tabFrame.state.selection);
selection++;
}
}
void pTabFrame::onChange() {
tabFrame.state.selection = TabCtrl_GetCurSel(hwnd);
synchronizeLayout();
if(tabFrame.onChange) tabFrame.onChange();
}
//called only if TCS_OWNERDRAWFIXED style is used
//this style disables XP/Vista theming of the TabFrame
void pTabFrame::onDrawItem(LPARAM lparam) {
LPDRAWITEMSTRUCT item = (LPDRAWITEMSTRUCT)lparam;
FillRect(item->hDC, &item->rcItem, GetSysColorBrush(COLOR_3DFACE));
SetBkMode(item->hDC, TRANSPARENT);
SetTextColor(item->hDC, GetSysColor(COLOR_BTNTEXT));
unsigned selection = item->itemID;
if(selection < tabFrame.state.text.size()) {
string text = tabFrame.state.text[selection];
Size size = pFont::size(hfont, text);
unsigned width = item->rcItem.right - item->rcItem.left + 1;
if(!tabFrame.state.image[selection].empty()) {
width += size.height + 2;
ImageList_Draw(imageList, selection, item->hDC, item->rcItem.left + (width - size.width) / 2 - (size.height + 3), item->rcItem.top + 2, ILD_NORMAL);
}
TextOut(item->hDC, item->rcItem.left + (width - size.width) / 2, item->rcItem.top + 2, utf16_t(text), text.size());
}
}
}
|