| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#include "tkWinInt.h" |
| 16 |
#include "tkSelect.h" |
| 17 |
|
| 18 |
static void UpdateClipboard _ANSI_ARGS_((HWND hwnd)); |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
int |
| 41 |
TkSelGetSelection(interp, tkwin, selection, target, proc, clientData) |
| 42 |
Tcl_Interp *interp; |
| 43 |
|
| 44 |
Tk_Window tkwin; |
| 45 |
|
| 46 |
|
| 47 |
Atom selection; |
| 48 |
Atom target; |
| 49 |
|
| 50 |
Tk_GetXSelProc *proc; |
| 51 |
|
| 52 |
ClientData clientData; |
| 53 |
{ |
| 54 |
char *data, *destPtr; |
| 55 |
Tcl_DString ds; |
| 56 |
HGLOBAL handle; |
| 57 |
Tcl_Encoding encoding; |
| 58 |
int result, locale; |
| 59 |
|
| 60 |
if ((selection != Tk_InternAtom(tkwin, "CLIPBOARD")) |
| 61 |
|| (target != XA_STRING) |
| 62 |
|| !OpenClipboard(NULL)) { |
| 63 |
goto error; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
result = TCL_ERROR; |
| 72 |
if (IsClipboardFormatAvailable(CF_UNICODETEXT)) { |
| 73 |
handle = GetClipboardData(CF_UNICODETEXT); |
| 74 |
if (!handle) { |
| 75 |
CloseClipboard(); |
| 76 |
goto error; |
| 77 |
} |
| 78 |
data = GlobalLock(handle); |
| 79 |
Tcl_DStringInit(&ds); |
| 80 |
Tcl_UniCharToUtfDString((Tcl_UniChar *)data, |
| 81 |
Tcl_UniCharLen((Tcl_UniChar *)data), &ds); |
| 82 |
GlobalUnlock(handle); |
| 83 |
} else if (IsClipboardFormatAvailable(CF_TEXT)) { |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
if (IsClipboardFormatAvailable(CF_LOCALE)) { |
| 89 |
handle = GetClipboardData(CF_LOCALE); |
| 90 |
if (!handle) { |
| 91 |
CloseClipboard(); |
| 92 |
goto error; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
Tcl_DStringInit(&ds); |
| 101 |
Tcl_DStringAppend(&ds, "cp######", -1); |
| 102 |
data = GlobalLock(handle); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
locale = LANGIDFROMLCID(*((int*)data)); |
| 112 |
GetLocaleInfo(locale, LOCALE_IDEFAULTANSICODEPAGE, |
| 113 |
Tcl_DStringValue(&ds)+2, Tcl_DStringLength(&ds)-2); |
| 114 |
GlobalUnlock(handle); |
| 115 |
|
| 116 |
encoding = Tcl_GetEncoding(NULL, Tcl_DStringValue(&ds)); |
| 117 |
Tcl_DStringFree(&ds); |
| 118 |
} else { |
| 119 |
encoding = NULL; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
handle = GetClipboardData(CF_TEXT); |
| 127 |
if (!handle) { |
| 128 |
if (encoding) { |
| 129 |
Tcl_FreeEncoding(encoding); |
| 130 |
} |
| 131 |
CloseClipboard(); |
| 132 |
goto error; |
| 133 |
} |
| 134 |
data = GlobalLock(handle); |
| 135 |
Tcl_ExternalToUtfDString(encoding, data, -1, &ds); |
| 136 |
GlobalUnlock(handle); |
| 137 |
if (encoding) { |
| 138 |
Tcl_FreeEncoding(encoding); |
| 139 |
} |
| 140 |
|
| 141 |
} else { |
| 142 |
CloseClipboard(); |
| 143 |
goto error; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
data = destPtr = Tcl_DStringValue(&ds); |
| 151 |
while (*data) { |
| 152 |
if (data[0] == '\r' && data[1] == '\n') { |
| 153 |
data++; |
| 154 |
} else { |
| 155 |
*destPtr++ = *data++; |
| 156 |
} |
| 157 |
} |
| 158 |
*destPtr = '\0'; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
result = (*proc)(clientData, interp, (long *) Tcl_DStringValue(&ds), |
| 165 |
Tcl_DStringLength(&ds),8,target,tkwin); |
| 166 |
Tcl_DStringFree(&ds); |
| 167 |
CloseClipboard(); |
| 168 |
return result; |
| 169 |
|
| 170 |
error: |
| 171 |
Tcl_AppendResult(interp, Tk_GetAtomName(tkwin, selection), |
| 172 |
" selection doesn't exist or form \"", |
| 173 |
Tk_GetAtomName(tkwin, target), |
| 174 |
"\" not defined", (char *) NULL); |
| 175 |
return TCL_ERROR; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
Window |
| 197 |
XGetSelectionOwner(Display *display, Atom selection) |
| 198 |
{ |
| 199 |
return None; |
| 200 |
} |
| 201 |
|
| 202 |
void |
| 203 |
XSetSelectionOwner(display, selection, owner, time) |
| 204 |
Display* display; |
| 205 |
Atom selection; |
| 206 |
Window owner; |
| 207 |
Time time; |
| 208 |
{ |
| 209 |
HWND hwnd = owner ? TkWinGetHWND(owner) : NULL; |
| 210 |
Tk_Window tkwin; |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
tkwin = (Tk_Window) TkGetMainInfoList()->winPtr; |
| 218 |
|
| 219 |
if (selection == Tk_InternAtom(tkwin, "CLIPBOARD")) { |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
if (GetClipboardOwner() != hwnd) { |
| 227 |
UpdateClipboard(hwnd); |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
void |
| 250 |
TkWinClipboardRender(dispPtr, format) |
| 251 |
TkDisplay *dispPtr; |
| 252 |
UINT format; |
| 253 |
{ |
| 254 |
TkClipboardTarget *targetPtr; |
| 255 |
TkClipboardBuffer *cbPtr; |
| 256 |
HGLOBAL handle; |
| 257 |
char *buffer, *p, *rawText, *endPtr; |
| 258 |
int length; |
| 259 |
Tcl_DString ds; |
| 260 |
|
| 261 |
for (targetPtr = dispPtr->clipTargetPtr; targetPtr != NULL; |
| 262 |
targetPtr = targetPtr->nextPtr) { |
| 263 |
if (targetPtr->type == XA_STRING) |
| 264 |
break; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
length = 0; |
| 273 |
if (targetPtr != NULL) { |
| 274 |
for (cbPtr = targetPtr->firstBufferPtr; cbPtr != NULL; |
| 275 |
cbPtr = cbPtr->nextPtr) { |
| 276 |
length += cbPtr->length; |
| 277 |
for (p = cbPtr->buffer, endPtr = p + cbPtr->length; |
| 278 |
p < endPtr; p++) { |
| 279 |
if (*p == '\n') { |
| 280 |
length++; |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
buffer = rawText = ckalloc(length + 1); |
| 291 |
if (targetPtr != NULL) { |
| 292 |
for (cbPtr = targetPtr->firstBufferPtr; cbPtr != NULL; |
| 293 |
cbPtr = cbPtr->nextPtr) { |
| 294 |
for (p = cbPtr->buffer, endPtr = p + cbPtr->length; |
| 295 |
p < endPtr; p++) { |
| 296 |
if (*p == '\n') { |
| 297 |
*buffer++ = '\r'; |
| 298 |
} |
| 299 |
*buffer++ = *p; |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
*buffer = '\0'; |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
if (TkWinGetPlatformId() == VER_PLATFORM_WIN32_NT) { |
| 311 |
Tcl_DStringInit(&ds); |
| 312 |
Tcl_UtfToUniCharDString(rawText, -1, &ds); |
| 313 |
ckfree(rawText); |
| 314 |
handle = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, |
| 315 |
Tcl_DStringLength(&ds)+2); |
| 316 |
if (!handle) { |
| 317 |
Tcl_DStringFree(&ds); |
| 318 |
return; |
| 319 |
} |
| 320 |
buffer = GlobalLock(handle); |
| 321 |
memcpy(buffer, Tcl_DStringValue(&ds), Tcl_DStringLength(&ds) + 2); |
| 322 |
GlobalUnlock(handle); |
| 323 |
Tcl_DStringFree(&ds); |
| 324 |
SetClipboardData(CF_UNICODETEXT, handle); |
| 325 |
} else { |
| 326 |
Tcl_UtfToExternalDString(NULL, rawText, -1, &ds); |
| 327 |
ckfree(rawText); |
| 328 |
handle = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, |
| 329 |
Tcl_DStringLength(&ds)+1); |
| 330 |
if (!handle) { |
| 331 |
Tcl_DStringFree(&ds); |
| 332 |
return; |
| 333 |
} |
| 334 |
buffer = GlobalLock(handle); |
| 335 |
memcpy(buffer, Tcl_DStringValue(&ds), Tcl_DStringLength(&ds) + 1); |
| 336 |
GlobalUnlock(handle); |
| 337 |
Tcl_DStringFree(&ds); |
| 338 |
SetClipboardData(CF_TEXT, handle); |
| 339 |
} |
| 340 |
|
| 341 |
return; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
void |
| 362 |
TkSelUpdateClipboard(winPtr, targetPtr) |
| 363 |
TkWindow *winPtr; |
| 364 |
TkClipboardTarget *targetPtr; |
| 365 |
{ |
| 366 |
HWND hwnd = TkWinGetHWND(winPtr->window); |
| 367 |
UpdateClipboard(hwnd); |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
static void |
| 388 |
UpdateClipboard(hwnd) |
| 389 |
HWND hwnd; |
| 390 |
{ |
| 391 |
TkWinUpdatingClipboard(TRUE); |
| 392 |
OpenClipboard(hwnd); |
| 393 |
EmptyClipboard(); |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
if (TkWinGetPlatformId() == VER_PLATFORM_WIN32_NT) { |
| 401 |
SetClipboardData(CF_UNICODETEXT, NULL); |
| 402 |
} else { |
| 403 |
SetClipboardData(CF_TEXT, NULL); |
| 404 |
} |
| 405 |
CloseClipboard(); |
| 406 |
TkWinUpdatingClipboard(FALSE); |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
void |
| 427 |
TkSelEventProc(tkwin, eventPtr) |
| 428 |
Tk_Window tkwin; |
| 429 |
|
| 430 |
register XEvent *eventPtr; |
| 431 |
|
| 432 |
|
| 433 |
{ |
| 434 |
if (eventPtr->type == SelectionClear) { |
| 435 |
TkSelClearSelection(tkwin, eventPtr); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
void |
| 458 |
TkSelPropProc(eventPtr) |
| 459 |
register XEvent *eventPtr; |
| 460 |
{ |
| 461 |
} |