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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
/*
* tkWinTest.c --
*
* Contains commands for platform specific tests for the Windows
* platform.
*
* Copyright (c) 1997 Sun Microsystems, Inc.
* Copyright (c) 2000 by Scriptics Corporation.
* Copyright (c) 2001 by ActiveState Corporation.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tkWinTest.c,v 1.21 2008/11/08 18:44:40 dkf Exp $
*/
#include "tkWinInt.h"
HWND tkWinCurrentDialog;
/*
* Forward declarations of functions defined later in this file:
*/
static int TestclipboardObjCmd(ClientData clientData,
Tcl_Interp *interp, int objc,
Tcl_Obj *const objv[]);
static int TestwineventCmd(ClientData clientData,
Tcl_Interp *interp, int argc, const char **argv);
static int TestfindwindowObjCmd(ClientData clientData,
Tcl_Interp *interp, int objc,
Tcl_Obj *const objv[]);
static int TestgetwindowinfoObjCmd(ClientData clientData,
Tcl_Interp *interp, int objc,
Tcl_Obj *const objv[]);
MODULE_SCOPE int TkplatformtestInit(Tcl_Interp *interp);
static Tk_GetSelProc SetSelectionResult;
/*
*----------------------------------------------------------------------
*
* TkplatformtestInit --
*
* Defines commands that test platform specific functionality for Windows
* platforms.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* Defines new commands.
*
*----------------------------------------------------------------------
*/
int
TkplatformtestInit(
Tcl_Interp *interp) /* Interpreter to add commands to. */
{
/*
* Add commands for platform specific tests on MacOS here.
*/
Tcl_CreateObjCommand(interp, "testclipboard", TestclipboardObjCmd,
(ClientData) Tk_MainWindow(interp), NULL);
Tcl_CreateCommand(interp, "testwinevent", TestwineventCmd,
(ClientData) Tk_MainWindow(interp), NULL);
Tcl_CreateObjCommand(interp, "testfindwindow", TestfindwindowObjCmd,
(ClientData) Tk_MainWindow(interp), NULL);
Tcl_CreateObjCommand(interp, "testgetwindowinfo", TestgetwindowinfoObjCmd,
(ClientData) Tk_MainWindow(interp), NULL);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* AppendSystemError --
*
* This routine formats a Windows system error message and places it into
* the interpreter result. Originally from tclWinReg.c.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
AppendSystemError(
Tcl_Interp *interp, /* Current interpreter. */
DWORD error) /* Result code from error. */
{
int length;
WCHAR *wMsgPtr;
char *msg;
char id[TCL_INTEGER_SPACE], msgBuf[24 + TCL_INTEGER_SPACE];
Tcl_DString ds;
Tcl_Obj *resultPtr = Tcl_GetObjResult(interp);
length = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (WCHAR *) &wMsgPtr,
0, NULL);
if (length == 0) {
char *msgPtr;
length = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char *) &msgPtr,
0, NULL);
if (length > 0) {
wMsgPtr = (WCHAR *) LocalAlloc(LPTR, (length + 1) * sizeof(WCHAR));
MultiByteToWideChar(CP_ACP, 0, msgPtr, length + 1, wMsgPtr,
length + 1);
LocalFree(msgPtr);
}
}
if (length == 0) {
if (error == ERROR_CALL_NOT_IMPLEMENTED) {
msg = (char *)"function not supported under Win32s";
} else {
sprintf(msgBuf, "unknown error: %ld", error);
msg = msgBuf;
}
} else {
Tcl_Encoding encoding;
encoding = Tcl_GetEncoding(NULL, "unicode");
msg = Tcl_ExternalToUtfDString(encoding, (char *) wMsgPtr, -1, &ds);
Tcl_FreeEncoding(encoding);
LocalFree(wMsgPtr);
length = Tcl_DStringLength(&ds);
/*
* Trim the trailing CR/LF from the system message.
*/
if (msg[length-1] == '\n') {
msg[--length] = 0;
}
if (msg[length-1] == '\r') {
msg[--length] = 0;
}
}
sprintf(id, "%ld", error);
Tcl_SetErrorCode(interp, "WINDOWS", id, msg, NULL);
Tcl_AppendToObj(resultPtr, msg, length);
if (length != 0) {
Tcl_DStringFree(&ds);
}
}
/*
*----------------------------------------------------------------------
*
* TestclipboardObjCmd --
*
* This function implements the testclipboard command. It provides a way
* to determine the actual contents of the Windows clipboard.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
SetSelectionResult(
ClientData dummy,
Tcl_Interp *interp,
const char *selection)
{
Tcl_AppendResult(interp, selection, NULL);
return TCL_OK;
}
static int
TestclipboardObjCmd(
ClientData clientData, /* Main window for application. */
Tcl_Interp *interp, /* Current interpreter. */
int objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument values. */
{
Tk_Window tkwin = (Tk_Window) clientData;
if (objc != 1) {
Tcl_WrongNumArgs(interp, 1, objv, NULL);
return TCL_ERROR;
}
return TkSelGetSelection(interp, tkwin, Tk_InternAtom(tkwin, "CLIPBOARD"),
XA_STRING, SetSelectionResult, NULL);
}
/*
*----------------------------------------------------------------------
*
* TestwineventCmd --
*
* This function implements the testwinevent command. It provides a way
* to send messages to windows dialogs.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
TestwineventCmd(
ClientData clientData, /* Main window for application. */
Tcl_Interp *interp, /* Current interpreter. */
int argc, /* Number of arguments. */
const char **argv) /* Argument strings. */
{
HWND hwnd = 0;
HWND child = 0;
int id;
char *rest;
UINT message;
WPARAM wParam;
LPARAM lParam;
static const TkStateMap messageMap[] = {
{WM_LBUTTONDOWN, "WM_LBUTTONDOWN"},
{WM_LBUTTONUP, "WM_LBUTTONUP"},
{WM_CHAR, "WM_CHAR"},
{WM_GETTEXT, "WM_GETTEXT"},
{WM_SETTEXT, "WM_SETTEXT"},
{WM_COMMAND, "WM_COMMAND"},
{-1, NULL}
};
if ((argc == 3) && (strcmp(argv[1], "debug") == 0)) {
int b;
if (Tcl_GetBoolean(interp, argv[2], &b) != TCL_OK) {
return TCL_ERROR;
}
TkWinDialogDebug(b);
return TCL_OK;
}
if (argc < 4) {
return TCL_ERROR;
}
#if 0
TkpScanWindowId(interp, argv[1], &id);
if (
#ifdef _WIN64
(sscanf(string, "0x%p", &number) != 1) &&
#endif /* _WIN64 */
Tcl_GetInt(interp, string, (int *)&number) != TCL_OK) {
return TCL_ERROR;
}
#endif
hwnd = (HWND) strtol(argv[1], &rest, 0);
if (rest == argv[1]) {
hwnd = FindWindow(NULL, argv[1]);
if (hwnd == NULL) {
Tcl_SetResult(interp, "no such window", TCL_STATIC);
return TCL_ERROR;
}
}
UpdateWindow(hwnd);
id = strtol(argv[2], &rest, 0);
if (rest == argv[2]) {
char buf[256];
child = GetWindow(hwnd, GW_CHILD);
while (child != NULL) {
SendMessage(child, WM_GETTEXT, (WPARAM) sizeof(buf), (LPARAM) buf);
if (strcasecmp(buf, argv[2]) == 0) {
id = GetDlgCtrlID(child);
break;
}
child = GetWindow(child, GW_HWNDNEXT);
}
if (child == NULL) {
return TCL_ERROR;
}
}
message = TkFindStateNum(NULL, NULL, messageMap, argv[3]);
if (message < 0) {
message = strtol(argv[3], NULL, 0);
}
wParam = 0;
lParam = 0;
if (argc > 4) {
wParam = strtol(argv[4], NULL, 0);
}
if (argc > 5) {
lParam = strtol(argv[5], NULL, 0);
}
switch (message) {
case WM_GETTEXT: {
Tcl_DString ds;
char buf[256];
GetDlgItemText(hwnd, id, buf, 256);
Tcl_ExternalToUtfDString(NULL, buf, -1, &ds);
Tcl_AppendResult(interp, Tcl_DStringValue(&ds), NULL);
Tcl_DStringFree(&ds);
break;
}
case WM_SETTEXT: {
Tcl_DString ds;
Tcl_UtfToExternalDString(NULL, argv[4], -1, &ds);
SetDlgItemText(hwnd, id, Tcl_DStringValue(&ds));
Tcl_DStringFree(&ds);
break;
}
case WM_COMMAND: {
char buf[TCL_INTEGER_SPACE];
if (argc < 5) {
wParam = MAKEWPARAM(id, 0);
lParam = (LPARAM)child;
}
sprintf(buf, "%d", (int) SendMessage(hwnd, message, wParam, lParam));
Tcl_SetResult(interp, buf, TCL_VOLATILE);
break;
}
default: {
char buf[TCL_INTEGER_SPACE];
sprintf(buf, "%d",
(int)SendDlgItemMessage(hwnd, id, message, wParam, lParam));
Tcl_SetResult(interp, buf, TCL_VOLATILE);
break;
}
}
return TCL_OK;
}
/*
* testfindwindow title ?class?
* Find a Windows window using the FindWindow API call. This takes the window
* title and optionally the window class and if found returns the HWND and
* raises an error if the window is not found.
* eg: testfindwindow Console TkTopLevel
* Can find the console window if it is visible.
* eg: testfindwindow "TkTest #10201" "#32770"
* Can find a messagebox window with this title.
*/
static int
TestfindwindowObjCmd(
ClientData clientData, /* Main window for application. */
Tcl_Interp *interp, /* Current interpreter. */
int objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument values. */
{
const char *title = NULL, *class = NULL;
HWND hwnd = NULL;
int r = TCL_OK;
if (objc < 2 || objc > 3) {
Tcl_WrongNumArgs(interp, 1, objv, "title ?class?");
return TCL_ERROR;
}
title = Tcl_GetString(objv[1]);
if (objc == 3)
class = Tcl_GetString(objv[2]);
hwnd = FindWindowA(class, title);
if (hwnd == NULL) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("failed to find window: ", -1));
AppendSystemError(interp, GetLastError());
r = TCL_ERROR;
} else {
Tcl_SetObjResult(interp, Tcl_NewLongObj((long)hwnd));
}
return r;
}
static BOOL CALLBACK
EnumChildrenProc(
HWND hwnd,
LPARAM lParam)
{
Tcl_Obj *listObj = (Tcl_Obj *) lParam;
Tcl_ListObjAppendElement(NULL, listObj, Tcl_NewLongObj((long) hwnd));
return TRUE;
}
static int
TestgetwindowinfoObjCmd(
ClientData clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *const objv[])
{
HWND hwnd = NULL;
Tcl_Obj *resObj = NULL, *classObj = NULL, *textObj = NULL;
Tcl_Obj *childrenObj = NULL;
char buf[512];
int cch, cchBuf = tkWinProcs->useWide ? 256 : 512;
if (objc != 2) {
Tcl_WrongNumArgs(interp, 1, objv, "hwnd");
return TCL_ERROR;
}
if (Tcl_GetLongFromObj(interp, objv[1], (long *)&hwnd) != TCL_OK)
return TCL_ERROR;
if (tkWinProcs->useWide) {
cch = GetClassNameW(hwnd, (LPWSTR)buf, sizeof(buf)/sizeof(WCHAR));
classObj = Tcl_NewUnicodeObj((LPWSTR)buf, cch);
} else {
cch = GetClassNameA(hwnd, (LPSTR)buf, sizeof(buf));
classObj = Tcl_NewStringObj((LPSTR)buf, cch);
}
if (cch == 0) {
Tcl_SetResult(interp, "failed to get class name: ", TCL_STATIC);
AppendSystemError(interp, GetLastError());
return TCL_ERROR;
}
resObj = Tcl_NewListObj(0, NULL);
Tcl_ListObjAppendElement(interp, resObj, Tcl_NewStringObj("class", -1));
Tcl_ListObjAppendElement(interp, resObj, classObj);
Tcl_ListObjAppendElement(interp, resObj, Tcl_NewStringObj("id", -1));
Tcl_ListObjAppendElement(interp, resObj,
Tcl_NewLongObj(GetWindowLong(hwnd, GWL_ID)));
cch = tkWinProcs->getWindowText(hwnd, (LPTSTR)buf, cchBuf);
if (tkWinProcs->useWide) {
textObj = Tcl_NewUnicodeObj((LPCWSTR)buf, cch);
} else {
textObj = Tcl_NewStringObj((LPCSTR)buf, cch);
}
Tcl_ListObjAppendElement(interp, resObj, Tcl_NewStringObj("text", -1));
Tcl_ListObjAppendElement(interp, resObj, textObj);
Tcl_ListObjAppendElement(interp, resObj, Tcl_NewStringObj("parent", -1));
Tcl_ListObjAppendElement(interp, resObj,
Tcl_NewLongObj((long)GetParent(hwnd)));
childrenObj = Tcl_NewListObj(0, NULL);
EnumChildWindows(hwnd, EnumChildrenProc, (LPARAM)childrenObj);
Tcl_ListObjAppendElement(interp, resObj, Tcl_NewStringObj("children", -1));
Tcl_ListObjAppendElement(interp, resObj, childrenObj);
Tcl_SetObjResult(interp, resObj);
return TCL_OK;
}
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/
|