| 1 |
/* |
| 2 |
* tkWinTest.c -- |
| 3 |
* |
| 4 |
* Contains commands for platform specific tests for |
| 5 |
* the Windows platform. |
| 6 |
* |
| 7 |
* Copyright (c) 1997 Sun Microsystems, Inc. |
| 8 |
* Copyright (c) 2000 by Scriptics Corporation. |
| 9 |
* Copyright (c) 2001 by ActiveState Corporation. |
| 10 |
* |
| 11 |
* See the file "license.terms" for information on usage and redistribution |
| 12 |
* of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 13 |
* |
| 14 |
* RCS: @(#) $Id: tkWinTest.c,v 1.7 2002/08/14 15:31:21 vincentdarley Exp $ |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "tkWinInt.h" |
| 18 |
|
| 19 |
HWND tkWinCurrentDialog; |
| 20 |
|
| 21 |
/* |
| 22 |
* Forward declarations of procedures defined later in this file: |
| 23 |
*/ |
| 24 |
|
| 25 |
int TkplatformtestInit(Tcl_Interp *interp); |
| 26 |
static int TestclipboardObjCmd(ClientData clientData, |
| 27 |
Tcl_Interp *interp, int objc, |
| 28 |
Tcl_Obj *CONST objv[]); |
| 29 |
static int TestwineventCmd(ClientData clientData, |
| 30 |
Tcl_Interp *interp, int argc, CONST char **argv); |
| 31 |
|
| 32 |
|
| 33 |
/* |
| 34 |
*---------------------------------------------------------------------- |
| 35 |
* |
| 36 |
* TkplatformtestInit -- |
| 37 |
* |
| 38 |
* Defines commands that test platform specific functionality for |
| 39 |
* Unix platforms. |
| 40 |
* |
| 41 |
* Results: |
| 42 |
* A standard Tcl result. |
| 43 |
* |
| 44 |
* Side effects: |
| 45 |
* Defines new commands. |
| 46 |
* |
| 47 |
*---------------------------------------------------------------------- |
| 48 |
*/ |
| 49 |
|
| 50 |
int |
| 51 |
TkplatformtestInit( |
| 52 |
Tcl_Interp *interp) /* Interpreter to add commands to. */ |
| 53 |
{ |
| 54 |
/* |
| 55 |
* Add commands for platform specific tests on MacOS here. |
| 56 |
*/ |
| 57 |
|
| 58 |
Tcl_CreateObjCommand(interp, "testclipboard", TestclipboardObjCmd, |
| 59 |
(ClientData) Tk_MainWindow(interp), (Tcl_CmdDeleteProc *) NULL); |
| 60 |
Tcl_CreateObjCommand(interp, "testwinevent", TestwineventCmd, |
| 61 |
(ClientData) Tk_MainWindow(interp), (Tcl_CmdDeleteProc *) NULL); |
| 62 |
|
| 63 |
return TCL_OK; |
| 64 |
} |
| 65 |
|
| 66 |
/* |
| 67 |
*---------------------------------------------------------------------- |
| 68 |
* |
| 69 |
* AppendSystemError -- |
| 70 |
* |
| 71 |
* This routine formats a Windows system error message and places |
| 72 |
* it into the interpreter result. Originally from tclWinReg.c. |
| 73 |
* |
| 74 |
* Results: |
| 75 |
* None. |
| 76 |
* |
| 77 |
* Side effects: |
| 78 |
* None. |
| 79 |
* |
| 80 |
*---------------------------------------------------------------------- |
| 81 |
*/ |
| 82 |
|
| 83 |
static void |
| 84 |
AppendSystemError( |
| 85 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 86 |
DWORD error) /* Result code from error. */ |
| 87 |
{ |
| 88 |
int length; |
| 89 |
WCHAR *wMsgPtr; |
| 90 |
char *msg; |
| 91 |
char id[TCL_INTEGER_SPACE], msgBuf[24 + TCL_INTEGER_SPACE]; |
| 92 |
Tcl_DString ds; |
| 93 |
Tcl_Obj *resultPtr = Tcl_GetObjResult(interp); |
| 94 |
|
| 95 |
length = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
| 96 |
| FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, error, |
| 97 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (WCHAR *) &wMsgPtr, |
| 98 |
0, NULL); |
| 99 |
if (length == 0) { |
| 100 |
char *msgPtr; |
| 101 |
|
| 102 |
length = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
| 103 |
| FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, error, |
| 104 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char *) &msgPtr, |
| 105 |
0, NULL); |
| 106 |
if (length > 0) { |
| 107 |
wMsgPtr = (WCHAR *) LocalAlloc(LPTR, (length + 1) * sizeof(WCHAR)); |
| 108 |
MultiByteToWideChar(CP_ACP, 0, msgPtr, length + 1, wMsgPtr, |
| 109 |
length + 1); |
| 110 |
LocalFree(msgPtr); |
| 111 |
} |
| 112 |
} |
| 113 |
if (length == 0) { |
| 114 |
if (error == ERROR_CALL_NOT_IMPLEMENTED) { |
| 115 |
msg = "function not supported under Win32s"; |
| 116 |
} else { |
| 117 |
sprintf(msgBuf, "unknown error: %ld", error); |
| 118 |
msg = msgBuf; |
| 119 |
} |
| 120 |
} else { |
| 121 |
Tcl_Encoding encoding; |
| 122 |
|
| 123 |
encoding = Tcl_GetEncoding(NULL, "unicode"); |
| 124 |
msg = Tcl_ExternalToUtfDString(encoding, (char *) wMsgPtr, -1, &ds); |
| 125 |
Tcl_FreeEncoding(encoding); |
| 126 |
LocalFree(wMsgPtr); |
| 127 |
|
| 128 |
length = Tcl_DStringLength(&ds); |
| 129 |
|
| 130 |
/* |
| 131 |
* Trim the trailing CR/LF from the system message. |
| 132 |
*/ |
| 133 |
if (msg[length-1] == '\n') { |
| 134 |
msg[--length] = 0; |
| 135 |
} |
| 136 |
if (msg[length-1] == '\r') { |
| 137 |
msg[--length] = 0; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
sprintf(id, "%ld", error); |
| 142 |
Tcl_SetErrorCode(interp, "WINDOWS", id, msg, (char *) NULL); |
| 143 |
Tcl_AppendToObj(resultPtr, msg, length); |
| 144 |
|
| 145 |
if (length != 0) { |
| 146 |
Tcl_DStringFree(&ds); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
/* |
| 151 |
*---------------------------------------------------------------------- |
| 152 |
* |
| 153 |
* TestclipboardObjCmd -- |
| 154 |
* |
| 155 |
* This procedure implements the testclipboard command. It provides |
| 156 |
* a way to determine the actual contents of the Windows clipboard. |
| 157 |
* |
| 158 |
* Results: |
| 159 |
* A standard Tcl result. |
| 160 |
* |
| 161 |
* Side effects: |
| 162 |
* None. |
| 163 |
* |
| 164 |
*---------------------------------------------------------------------- |
| 165 |
*/ |
| 166 |
|
| 167 |
static int |
| 168 |
TestclipboardObjCmd(clientData, interp, objc, objv) |
| 169 |
ClientData clientData; /* Main window for application. */ |
| 170 |
Tcl_Interp *interp; /* Current interpreter. */ |
| 171 |
int objc; /* Number of arguments. */ |
| 172 |
Tcl_Obj *CONST objv[]; /* Argument values. */ |
| 173 |
{ |
| 174 |
TkWindow *winPtr = (TkWindow *) clientData; |
| 175 |
HGLOBAL handle; |
| 176 |
char *data; |
| 177 |
int code = TCL_OK; |
| 178 |
|
| 179 |
if (objc != 1) { |
| 180 |
Tcl_WrongNumArgs(interp, 1, objv, (char *) NULL); |
| 181 |
return TCL_ERROR; |
| 182 |
} |
| 183 |
if (OpenClipboard(NULL)) { |
| 184 |
/* |
| 185 |
* We could consider using CF_UNICODETEXT on NT, but then we |
| 186 |
* would have to convert it from External. Instead we'll just |
| 187 |
* take this and do "bytestring" at the Tcl level for Unicode |
| 188 |
* inclusive text |
| 189 |
*/ |
| 190 |
handle = GetClipboardData(CF_TEXT); |
| 191 |
if (handle != NULL) { |
| 192 |
data = GlobalLock(handle); |
| 193 |
Tcl_AppendResult(interp, data, (char *) NULL); |
| 194 |
GlobalUnlock(handle); |
| 195 |
} else { |
| 196 |
Tcl_AppendResult(interp, "null clipboard handle", (char *) NULL); |
| 197 |
code = TCL_ERROR; |
| 198 |
} |
| 199 |
CloseClipboard(); |
| 200 |
return code; |
| 201 |
} else { |
| 202 |
Tcl_AppendResult(interp, "couldn't open clipboard: ", (char *) NULL); |
| 203 |
AppendSystemError(interp, GetLastError()); |
| 204 |
return TCL_ERROR; |
| 205 |
} |
| 206 |
return TCL_OK; |
| 207 |
} |
| 208 |
|
| 209 |
/* |
| 210 |
*---------------------------------------------------------------------- |
| 211 |
* |
| 212 |
* TestwineventCmd -- |
| 213 |
* |
| 214 |
* This procedure implements the testwinevent command. It provides |
| 215 |
* a way to send messages to windows dialogs. |
| 216 |
* |
| 217 |
* Results: |
| 218 |
* A standard Tcl result. |
| 219 |
* |
| 220 |
* Side effects: |
| 221 |
* None. |
| 222 |
* |
| 223 |
*---------------------------------------------------------------------- |
| 224 |
*/ |
| 225 |
|
| 226 |
static int |
| 227 |
TestwineventCmd(clientData, interp, argc, argv) |
| 228 |
ClientData clientData; /* Main window for application. */ |
| 229 |
Tcl_Interp *interp; /* Current interpreter. */ |
| 230 |
int argc; /* Number of arguments. */ |
| 231 |
CONST char **argv; /* Argument strings. */ |
| 232 |
{ |
| 233 |
HWND hwnd = 0; |
| 234 |
int id; |
| 235 |
char *rest; |
| 236 |
UINT message; |
| 237 |
WPARAM wParam; |
| 238 |
LPARAM lParam; |
| 239 |
static TkStateMap messageMap[] = { |
| 240 |
{WM_LBUTTONDOWN, "WM_LBUTTONDOWN"}, |
| 241 |
{WM_LBUTTONUP, "WM_LBUTTONUP"}, |
| 242 |
{WM_CHAR, "WM_CHAR"}, |
| 243 |
{WM_GETTEXT, "WM_GETTEXT"}, |
| 244 |
{WM_SETTEXT, "WM_SETTEXT"}, |
| 245 |
{-1, NULL} |
| 246 |
}; |
| 247 |
|
| 248 |
if ((argc == 3) && (strcmp(argv[1], "debug") == 0)) { |
| 249 |
int b; |
| 250 |
|
| 251 |
if (Tcl_GetBooleanFromObj(interp, argv[2], &b) != TCL_OK) { |
| 252 |
return TCL_ERROR; |
| 253 |
} |
| 254 |
TkWinDialogDebug(b); |
| 255 |
return TCL_OK; |
| 256 |
} |
| 257 |
|
| 258 |
if (argc < 4) { |
| 259 |
return TCL_ERROR; |
| 260 |
} |
| 261 |
|
| 262 |
#if 0 |
| 263 |
TkpScanWindowId(interp, argv[1], &id); |
| 264 |
if ( |
| 265 |
#ifdef _WIN64 |
| 266 |
(sscanf(string, "0x%p", &number) != 1) && |
| 267 |
#endif |
| 268 |
Tcl_GetInt(interp, string, (int *)&number) != TCL_OK) { |
| 269 |
return TCL_ERROR; |
| 270 |
} |
| 271 |
#endif |
| 272 |
hwnd = (HWND) strtol(argv[1], &rest, 0); |
| 273 |
if (rest == argv[1]) { |
| 274 |
hwnd = FindWindow(NULL, argv[1]); |
| 275 |
if (hwnd == NULL) { |
| 276 |
Tcl_SetResult(interp, "no such window", TCL_STATIC); |
| 277 |
return TCL_ERROR; |
| 278 |
} |
| 279 |
} |
| 280 |
UpdateWindow(hwnd); |
| 281 |
|
| 282 |
id = strtol(argv[2], &rest, 0); |
| 283 |
if (rest == argv[2]) { |
| 284 |
HWND child; |
| 285 |
char buf[256]; |
| 286 |
|
| 287 |
child = GetWindow(hwnd, GW_CHILD); |
| 288 |
while (child != NULL) { |
| 289 |
SendMessage(child, WM_GETTEXT, (WPARAM) sizeof(buf), (LPARAM) buf); |
| 290 |
if (strcasecmp(buf, argv[2]) == 0) { |
| 291 |
id = GetDlgCtrlID(child); |
| 292 |
break; |
| 293 |
} |
| 294 |
child = GetWindow(child, GW_HWNDNEXT); |
| 295 |
} |
| 296 |
if (child == NULL) { |
| 297 |
return TCL_ERROR; |
| 298 |
} |
| 299 |
} |
| 300 |
message = TkFindStateNum(NULL, NULL, messageMap, argv[3]); |
| 301 |
if (message < 0) { |
| 302 |
message = strtol(argv[3], NULL, 0); |
| 303 |
} |
| 304 |
wParam = 0; |
| 305 |
lParam = 0; |
| 306 |
|
| 307 |
if (argc > 4) { |
| 308 |
wParam = strtol(argv[4], NULL, 0); |
| 309 |
} |
| 310 |
if (argc > 5) { |
| 311 |
lParam = strtol(argv[5], NULL, 0); |
| 312 |
} |
| 313 |
|
| 314 |
switch (message) { |
| 315 |
case WM_GETTEXT: { |
| 316 |
Tcl_DString ds; |
| 317 |
char buf[256]; |
| 318 |
|
| 319 |
GetDlgItemText(hwnd, id, buf, 256); |
| 320 |
Tcl_ExternalToUtfDString(NULL, buf, -1, &ds); |
| 321 |
Tcl_AppendResult(interp, Tcl_DStringValue(&ds), NULL); |
| 322 |
Tcl_DStringFree(&ds); |
| 323 |
break; |
| 324 |
} |
| 325 |
case WM_SETTEXT: { |
| 326 |
Tcl_DString ds; |
| 327 |
|
| 328 |
Tcl_UtfToExternalDString(NULL, argv[4], -1, &ds); |
| 329 |
SetDlgItemText(hwnd, id, Tcl_DStringValue(&ds)); |
| 330 |
Tcl_DStringFree(&ds); |
| 331 |
break; |
| 332 |
} |
| 333 |
default: { |
| 334 |
char buf[TCL_INTEGER_SPACE]; |
| 335 |
|
| 336 |
sprintf(buf, "%d", |
| 337 |
SendDlgItemMessage(hwnd, id, message, wParam, lParam)); |
| 338 |
Tcl_SetResult(interp, buf, TCL_VOLATILE); |
| 339 |
break; |
| 340 |
} |
| 341 |
} |
| 342 |
return TCL_OK; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|