Debian Codegraph @

phoenix/windows/widget/tab-frame.cpp

raw
1 namespace phoenix {
2
3 static LRESULT CALLBACK TabFrame_windowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
4 Object* object = (Object*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
5 if(object == nullptr) return DefWindowProc(hwnd, msg, wparam, lparam);
6 TabFrame& tabFrame = (TabFrame&)*object;
7 return Shared_windowProc(tabFrame.p.windowProc, hwnd, msg, wparam, lparam);
8 }
9
10 void pTabFrame::append(string text, const image& image) {
11 unsigned selection = TabCtrl_GetItemCount(hwnd);
12 wchar_t wtext[] = L"";
13 TCITEM item;
14 item.mask = TCIF_TEXT;
15 item.pszText = wtext;
16 TabCtrl_InsertItem(hwnd, selection, &item);
17 setText(selection, text);
18 if(!image.empty()) setImage(selection, image);
19 }
20
21 void pTabFrame::remove(unsigned selection) {
22 TabCtrl_DeleteItem(hwnd, selection);
23 buildImageList();
24 }
25
26 void pTabFrame::setEnabled(bool enabled) {
27 pWidget::setEnabled(enabled);
28 for(auto& layout : tabFrame.state.layout) {
29 if(layout) layout->setEnabled(layout->enabled());
30 }
31 }
32
33 void pTabFrame::setGeometry(Geometry geometry) {
34 pWidget::setGeometry(geometry);
35 geometry.x += 1, geometry.width -= 4;
36 geometry.y += 21, geometry.height -= 23;
37 for(auto& layout : tabFrame.state.layout) {
38 if(layout) layout->setGeometry(geometry);
39 }
40 }
41
42 void pTabFrame::setImage(unsigned selection, const image& image) {
43 buildImageList();
44 }
45
46 void pTabFrame::setSelection(unsigned selection) {
47 TabCtrl_SetCurSel(hwnd, selection);
48 synchronizeLayout();
49 }
50
51 void pTabFrame::setText(unsigned selection, string text) {
52 utf16_t wtext(text);
53 TCITEM item;
54 item.mask = TCIF_TEXT;
55 item.pszText = (wchar_t*)wtext;
56 TabCtrl_SetItem(hwnd, selection, &item);
57 }
58
59 void pTabFrame::setVisible(bool visible) {
60 pWidget::setVisible(visible);
61 for(auto& layout : tabFrame.state.layout) {
62 if(layout) layout->setVisible(layout->visible());
63 }
64 }
65
66 void pTabFrame::constructor() {
67 hwnd = CreateWindow(WC_TABCONTROL, L"",
68 WS_CHILD | WS_TABSTOP,
69 0, 0, 0, 0, parentHwnd, (HMENU)id, GetModuleHandle(0), 0);
70 SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)&tabFrame);
71
72 windowProc = (WindowProc)GetWindowLongPtr(hwnd, GWLP_WNDPROC);
73 SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)TabFrame_windowProc);
74
75 setDefaultFont();
76 for(auto& text : tabFrame.state.text) append(text, {});
77 buildImageList();
78 setSelection(tabFrame.state.selection);
79 synchronize();
80 }
81
82 void pTabFrame::destructor() {
83 if(imageList) { ImageList_Destroy(imageList); imageList = nullptr; }
84 DestroyWindow(hwnd);
85 }
86
87 void pTabFrame::orphan() {
88 destructor();
89 constructor();
90 }
91
92 void pTabFrame::buildImageList() {
93 if(imageList) ImageList_Destroy(imageList);
94 unsigned size = pFont::size(hfont, " ").height;
95 imageList = ImageList_Create(size, size, ILC_COLOR32, 1, 0);
96 for(auto& image : tabFrame.state.image) {
97 ImageList_Append(imageList, image, size);
98 }
99 TabCtrl_SetImageList(hwnd, imageList);
100 for(unsigned n = 0; n < tabFrame.state.image.size(); n++) {
101 TCITEM item;
102 item.mask = TCIF_IMAGE;
103 item.iImage = (tabFrame.state.image(n).empty() ? -1 : n);
104 TabCtrl_SetItem(hwnd, n, &item);
105 }
106 }
107
108 void pTabFrame::synchronizeLayout() {
109 unsigned selection = 0;
110 for(auto& layout : tabFrame.state.layout) {
111 if(layout) layout->setVisible(selection == tabFrame.state.selection);
112 selection++;
113 }
114 }
115
116 void pTabFrame::onChange() {
117 tabFrame.state.selection = TabCtrl_GetCurSel(hwnd);
118 synchronizeLayout();
119 if(tabFrame.onChange) tabFrame.onChange();
120 }
121
122 //called only if TCS_OWNERDRAWFIXED style is used
123 //this style disables XP/Vista theming of the TabFrame
124 void pTabFrame::onDrawItem(LPARAM lparam) {
125 LPDRAWITEMSTRUCT item = (LPDRAWITEMSTRUCT)lparam;
126 FillRect(item->hDC, &item->rcItem, GetSysColorBrush(COLOR_3DFACE));
127 SetBkMode(item->hDC, TRANSPARENT);
128 SetTextColor(item->hDC, GetSysColor(COLOR_BTNTEXT));
129
130 unsigned selection = item->itemID;
131 if(selection < tabFrame.state.text.size()) {
132 string text = tabFrame.state.text[selection];
133 Size size = pFont::size(hfont, text);
134 unsigned width = item->rcItem.right - item->rcItem.left + 1;
135 if(!tabFrame.state.image[selection].empty()) {
136 width += size.height + 2;
137 ImageList_Draw(imageList, selection, item->hDC, item->rcItem.left + (width - size.width) / 2 - (size.height + 3), item->rcItem.top + 2, ILD_NORMAL);
138 }
139 TextOut(item->hDC, item->rcItem.left + (width - size.width) / 2, item->rcItem.top + 2, utf16_t(text), text.size());
140 }
141 }
142
143 }