-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTabObject.c
More file actions
300 lines (264 loc) · 9.2 KB
/
TabObject.c
File metadata and controls
300 lines (264 loc) · 9.2 KB
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
// TabObject.c | Tymber © 2020 by Thomas Führinger
#include "Tymber.h"
static PyObject*
TyTab_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
{
TyTabObject* self = (TyTabObject*)TyTabType.tp_base->tp_new(type, args, kwds);
if (self != NULL) {
self->pyCurrentPage = NULL;
return (PyObject*)self;
}
else
return NULL;
}
static LRESULT CALLBACK TyTabProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
static int
TyTab_init(TyTabObject* self, PyObject* args, PyObject* kwds)
{
if (TyTabType.tp_base->tp_init((PyObject*)self, args, kwds) < 0)
return -1;
RECT rect;
TyWidget_CalculateRect(self, &rect);
self->hWin = CreateWindowEx(0, WC_TABCONTROLW, L"",
WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_VISIBLE,// | TCS_BOTTOM,
rect.left, rect.top, rect.right, rect.bottom,
self->hwndParent, g->hMenu, g->hInstance, NULL);
if (self->hWin == NULL) {
PyErr_SetFromWindowsErr(0);
return -1;
}
self->pyChildren = PyDict_New();
SendMessage(self->hWin, WM_SETFONT, (WPARAM)g->hfDefaultFont, MAKELPARAM(FALSE, 0));
self->fnOldWinProcedure = (WNDPROC)SetWindowLongPtrW(self->hWin, GWLP_WNDPROC, (LONG_PTR)TyTabProc);
SetWindowLongPtr(self->hWin, GWLP_USERDATA, (LONG_PTR)self);
return 0;
}
static TyTabPageObject*
TyTab_GetPageByIndex(TyTabObject* self, int iIndex)
{
PyObject* pyKey, * pyValue, * pyPage = NULL;
Py_ssize_t nPos = 0;
while (PyDict_Next(self->pyChildren, &nPos, &pyKey, &pyValue) && !pyPage) {
if (((TyTabPageObject*)pyValue)->iIndex == iIndex)
pyPage = pyValue;
}
return pyPage;
}
static PyObject*
TyTab_getattro(TyTabObject* self, PyObject* pyAttributeName)
{
PyObject* pyResult, * pyAttribute;
pyResult = PyObject_GenericGetAttr((PyObject*)self, pyAttributeName);
if (pyResult == NULL && PyErr_ExceptionMatches(PyExc_AttributeError) && PyUnicode_Check(pyAttributeName)) {
if (PyDict_Contains(self->pyChildren, pyAttributeName)) {
PyErr_Clear();
pyAttribute = PyDict_GetItem(self->pyChildren, pyAttributeName);
Py_INCREF(pyAttribute);
return pyAttribute;
}
if (PyUnicode_CompareWithASCIIString(pyAttributeName, "children") == 0) {
PyErr_Clear();
Py_INCREF(self->pyChildren);
return self->pyChildren;
}
}
return TyTabType.tp_base->tp_getattro((PyObject*)self, pyAttributeName);
}
static void
TyTab_dealloc(TyTabObject* self)
{
Py_DECREF(self->pyChildren);
DestroyWindow(self->hWin);
TyTabType.tp_base->tp_dealloc((PyObject*)self);
}
static PyMemberDef TyTab_members[] = {
//{ "children", T_OBJECT, offsetof(TyTabObject, pyChildren), READONLY, "Tab pages" },
{ NULL }
};
static PyMethodDef TyTab_methods[] = {
{ NULL }
};
PyTypeObject TyTabType = {
PyVarObject_HEAD_INIT(NULL, 0)
"tymber.Tab", /* tp_name */
sizeof(TyTabObject), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)TyTab_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
TyTab_getattro, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
"Tab widget objects", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
TyTab_methods, /* tp_methods */
TyTab_members, /* tp_members */
0, /* tp_getset */
&TyWidgetType, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)TyTab_init, /* tp_init */
0, /* tp_alloc */
TyTab_new, /* tp_new */
PyObject_Free, /* tp_free */
};
static BOOL TyTabPage_GotSelected(TyTabPageObject* self);
static LRESULT CALLBACK
TyTabProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
TyTabObject* self = (TyTabObject*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
switch (msg)
{
case OCM_NOTIFY:
{
switch (((LPNMHDR)lParam)->code)
{
case TCN_SELCHANGING:
return FALSE;
case TCN_SELCHANGE:
{
int iIndex = TabCtrl_GetCurSel(self->hWin);
TyTabPage_GotSelected(TyTab_GetPageByIndex(self, iIndex));
break;
}
}
}
}
return CallWindowProcW(self->fnOldWinProcedure, hwnd, msg, wParam, lParam);
}
/* TabPage ----------------------------------------------------------------------- */
static PyObject*
TyTabPage_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
{
TyTabPageObject* self = (TyTabPageObject*)TyTabPageType.tp_base->tp_new(type, args, kwds);
if (self != NULL) {
self->iIndex = -1;
return (PyObject*)self;
}
else
return NULL;
}
static int
TyTabPage_init(TyTabPageObject* self, PyObject* args, PyObject* kwds)
{
if (TyTabPageType.tp_base->tp_init((PyObject*)self, args, kwds) < 0)
return -1;
if (!PyObject_TypeCheck(self->pyParent, &TyTabType)) {
PyErr_Format(PyExc_TypeError, "Argument 'parent' must be a Tab, not '%.200s'.", ((PyObject*)self->pyParent)->ob_type->tp_name);
return -1;
}
TCITEM tie;
tie.mask = TCIF_TEXT | TCIF_IMAGE;
tie.iImage = -1;
LPCSTR strText;
if (self->pyCaption != NULL) {
strText = PyUnicode_AsUTF8(self->pyCaption);
}
else
strText = "<New>";
tie.pszText = toW(strText);
self->iIndex = TabCtrl_InsertItem(self->pyParent->hWin, PyDict_Size(((TyTabObject*)self->pyParent)->pyChildren), &tie);
if (self->iIndex == -1) {
PyErr_SetFromWindowsErr(0);
return -1;
}
PyMem_RawFree(tie.pszText);
self->rc.left = 2;
self->rc.top = 24;
self->rc.right = -5;
self->rc.bottom = -5;
TyWidget_MoveWindow(self);
if (PyDict_Size(((TyTabObject*)self->pyParent)->pyChildren) == 1)
TyTabPage_GotSelected(self);
else
ShowWindow(self->hWin, SW_HIDE);
return 0;
}
static BOOL
TyTabPage_GotSelected(TyTabPageObject* self)
{
TyTabObject* pyTab = (TyTabObject*)self->pyParent;
if (pyTab->pyCurrentPage)
ShowWindow(pyTab->pyCurrentPage->hWin, SW_HIDE);
pyTab->pyCurrentPage = self;
ShowWindow(self->hWin, SW_SHOW);
return TRUE;
}
static PyObject*
TyTabPage_getattro(TyApplicationObject* self, PyObject* pyAttributeName)
{
PyObject* pyResult, * pyAttribute;
pyResult = PyObject_GenericGetAttr((PyObject*)self, pyAttributeName);
if (pyResult == NULL && PyErr_ExceptionMatches(PyExc_AttributeError) && PyUnicode_Check(pyAttributeName)) {
}
return TyTabPageType.tp_base->tp_getattro((PyObject*)self, pyAttributeName);
}
static void
TyTabPage_dealloc(TyTabPageObject* self)
{
TyTabPageType.tp_base->tp_dealloc((TyTabPageObject*)self);
}
static PyMemberDef TyTabPage_members[] = {
{ NULL }
};
static PyMethodDef TyTabPage_methods[] = {
{ NULL }
};
PyTypeObject TyTabPageType = {
PyVarObject_HEAD_INIT(NULL, 0)
"tymber.TabPage", /* tp_name */
sizeof(TyTabPageObject), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)TyTabPage_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
TyTabPage_getattro, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
"TabPage widget objects", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
TyTabPage_methods, /* tp_methods */
TyTabPage_members, /* tp_members */
0, /* tp_getset */
&TyBoxType, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)TyTabPage_init, /* tp_init */
0, /* tp_alloc */
TyTabPage_new, /* tp_new */
PyObject_Free, /* tp_free */
};