1 /* 2 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include "awt_Toolkit.h" 27 #include "awt_TextArea.h" 28 #include "awt_TextComponent.h" 29 #include "awt_Canvas.h" 30 #include "awt_Window.h" 31 #include "awt_Frame.h" 32 33 /* IMPORTANT! Read the README.JNI file for notes on JNI converted AWT code. 34 */ 35 36 /***********************************************************************/ 37 // Struct for _ReplaceText() method 38 struct ReplaceTextStruct { 39 jobject textComponent; 40 jstring text; 41 int start, end; 42 }; 43 44 /************************************************************************ 45 * AwtTextArea fields 46 */ 47 48 jfieldID AwtTextArea::scrollbarVisibilityID; 49 50 /************************************************************************ 51 * AwtTextArea methods 52 */ 53 54 AwtTextArea::AwtTextArea() { 55 m_bCanUndo = FALSE; 56 m_lHDeltaAccum = 0; 57 m_lVDeltaAccum = 0; 58 } 59 60 AwtTextArea::~AwtTextArea() 61 { 62 } 63 64 void AwtTextArea::Dispose() 65 { 66 AwtTextComponent::Dispose(); 67 } 68 69 /* Create a new AwtTextArea object and window. */ 70 AwtTextArea* AwtTextArea::Create(jobject peer, jobject parent) 71 { 72 return (AwtTextArea*) AwtTextComponent::Create(peer, parent, true); 73 } 74 75 void AwtTextArea::EditSetSel(CHARRANGE &cr) { 76 SendMessage(EM_EXSETSEL, 0, reinterpret_cast<LPARAM>(&cr)); 77 // 6417581: force expected drawing 78 if (IS_WINVISTA && cr.cpMin == cr.cpMax) { 79 ::InvalidateRect(GetHWnd(), NULL, TRUE); 80 } 81 } 82 83 /* Count how many '\n's are there in jStr */ 84 size_t AwtTextArea::CountNewLines(JNIEnv *env, jstring jStr, size_t maxlen) 85 { 86 size_t nNewlines = 0; 87 88 if (jStr == NULL) { 89 return nNewlines; 90 } 91 /* 92 * Fix for BugTraq Id 4260109. 93 * Don't use TO_WSTRING since it allocates memory on the stack 94 * causing stack overflow when the text is very long. 95 */ 96 size_t length = env->GetStringLength(jStr) + 1; 97 WCHAR *string = new WCHAR[length]; 98 env->GetStringRegion(jStr, 0, static_cast<jsize>(length - 1), reinterpret_cast<jchar*>(string)); 99 string[length-1] = '\0'; 100 for (size_t i = 0; i < maxlen && i < length - 1; i++) { 101 if (string[i] == L'\n') { 102 nNewlines++; 103 } 104 } 105 delete[] string; 106 return nNewlines; 107 } 108 109 BOOL AwtTextArea::InheritsNativeMouseWheelBehavior() {return true;} 110 111 112 LRESULT 113 AwtTextArea::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) { 114 115 LRESULT retValue = 0; 116 MsgRouting mr = mrDoDefault; 117 118 switch (message) { 119 case EM_SETCHARFORMAT: 120 case WM_SETFONT: 121 SetIgnoreEnChange(TRUE); 122 break; 123 } 124 125 retValue = AwtTextComponent::WindowProc(message, wParam, lParam); 126 127 switch (message) { 128 case EM_SETCHARFORMAT: 129 case WM_SETFONT: 130 SetIgnoreEnChange(FALSE); 131 break; 132 } 133 134 return retValue; 135 } 136 137 MsgRouting 138 AwtTextArea::WmNcHitTest(UINT x, UINT y, LRESULT& retVal) 139 { 140 if (::IsWindow(AwtWindow::GetModalBlocker(AwtComponent::GetTopLevelParentForWindow(GetHWnd())))) { 141 retVal = HTCLIENT; 142 return mrConsume; 143 } 144 return AwtTextComponent::WmNcHitTest(x, y, retVal); 145 } 146 147 148 MsgRouting 149 AwtTextArea::HandleEvent(MSG *msg, BOOL synthetic) 150 { 151 /* 152 * RichEdit 1.0 control starts internal message loop if the 153 * left mouse button is pressed while the cursor is not over 154 * the current selection or the current selection is empty. 155 * Because of this we don't receive WM_MOUSEMOVE messages 156 * while the left mouse button is pressed. To work around 157 * this behavior we process the relevant mouse messages 158 * by ourselves. 159 * By consuming WM_MOUSEMOVE messages we also don't give 160 * the RichEdit control a chance to recognize a drag gesture 161 * and initiate its own drag-n-drop operation. 162 * 163 * The workaround also allows us to implement synthetic focus mechanism. 164 * 165 */ 166 if (IsFocusingMouseMessage(msg)) { 167 CHARRANGE cr; 168 169 LONG lCurPos = EditGetCharFromPos(msg->pt); 170 171 EditGetSel(cr); 172 /* 173 * NOTE: Plain EDIT control always clears selection on mouse 174 * button press. We are clearing the current selection only if 175 * the mouse pointer is not over the selected region. 176 * In this case we sacrifice backward compatibility 177 * to allow dnd of the current selection. 178 */ 179 if (lCurPos < cr.cpMin || cr.cpMax <= lCurPos) { 180 if (msg->message == WM_LBUTTONDBLCLK) { 181 SetStartSelectionPos(static_cast<LONG>(SendMessage( 182 EM_FINDWORDBREAK, WB_MOVEWORDLEFT, lCurPos))); 183 SetEndSelectionPos(static_cast<LONG>(SendMessage( 184 EM_FINDWORDBREAK, WB_MOVEWORDRIGHT, lCurPos))); 185 } else { 186 SetStartSelectionPos(lCurPos); 187 SetEndSelectionPos(lCurPos); 188 } 189 cr.cpMin = GetStartSelectionPos(); 190 cr.cpMax = GetEndSelectionPos(); 191 EditSetSel(cr); 192 } 193 194 delete msg; 195 return mrConsume; 196 } else if (msg->message == WM_LBUTTONUP) { 197 198 /* 199 * If the left mouse button is pressed on the selected region 200 * we don't clear the current selection. We clear it on button 201 * release instead. This is to allow dnd of the current selection. 202 */ 203 if (GetStartSelectionPos() == -1 && GetEndSelectionPos() == -1) { 204 CHARRANGE cr; 205 206 LONG lCurPos = EditGetCharFromPos(msg->pt); 207 208 cr.cpMin = lCurPos; 209 cr.cpMax = lCurPos; 210 EditSetSel(cr); 211 } 212 213 /* 214 * Cleanup the state variables when left mouse button is released. 215 * These state variables are designed to reflect the selection state 216 * while the left mouse button is pressed and be set to -1 otherwise. 217 */ 218 SetStartSelectionPos(-1); 219 SetEndSelectionPos(-1); 220 SetLastSelectionPos(-1); 221 222 delete msg; 223 return mrConsume; 224 } else if (msg->message == WM_MOUSEMOVE && (msg->wParam & MK_LBUTTON)) { 225 226 /* 227 * We consume WM_MOUSEMOVE while the left mouse button is pressed, 228 * so we have to simulate autoscrolling when mouse is moved outside 229 * of the client area. 230 */ 231 POINT p; 232 RECT r; 233 BOOL bScrollLeft = FALSE; 234 BOOL bScrollRight = FALSE; 235 BOOL bScrollUp = FALSE; 236 BOOL bScrollDown = FALSE; 237 238 p.x = msg->pt.x; 239 p.y = msg->pt.y; 240 VERIFY(::GetClientRect(GetHWnd(), &r)); 241 242 if (p.x < 0) { 243 bScrollLeft = TRUE; 244 p.x = 0; 245 } else if (p.x > r.right) { 246 bScrollRight = TRUE; 247 p.x = r.right - 1; 248 } 249 if (p.y < 0) { 250 bScrollUp = TRUE; 251 p.y = 0; 252 } else if (p.y > r.bottom) { 253 bScrollDown = TRUE; 254 p.y = r.bottom - 1; 255 } 256 257 LONG lCurPos = EditGetCharFromPos(p); 258 259 if (GetStartSelectionPos() != -1 && 260 GetEndSelectionPos() != -1 && 261 lCurPos != GetLastSelectionPos()) { 262 263 CHARRANGE cr; 264 265 SetLastSelectionPos(lCurPos); 266 267 cr.cpMin = GetStartSelectionPos(); 268 cr.cpMax = GetLastSelectionPos(); 269 270 EditSetSel(cr); 271 } 272 273 if (bScrollLeft == TRUE || bScrollRight == TRUE) { 274 SCROLLINFO si; 275 memset(&si, 0, sizeof(si)); 276 si.cbSize = sizeof(si); 277 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE; 278 279 VERIFY(::GetScrollInfo(GetHWnd(), SB_HORZ, &si)); 280 if (bScrollLeft == TRUE) { 281 si.nPos = si.nPos - si.nPage / 2; 282 si.nPos = max(si.nMin, si.nPos); 283 } else if (bScrollRight == TRUE) { 284 si.nPos = si.nPos + si.nPage / 2; 285 si.nPos = min(si.nPos, si.nMax); 286 } 287 /* 288 * Okay to use 16-bit position since RichEdit control adjusts 289 * its scrollbars so that their range is always 16-bit. 290 */ 291 DASSERT(abs(si.nPos) < 0x8000); 292 SendMessage(WM_HSCROLL, 293 MAKEWPARAM(SB_THUMBPOSITION, LOWORD(si.nPos))); 294 } 295 if (bScrollUp == TRUE) { 296 SendMessage(EM_LINESCROLL, 0, -1); 297 } else if (bScrollDown == TRUE) { 298 SendMessage(EM_LINESCROLL, 0, 1); 299 } 300 delete msg; 301 return mrConsume; 302 } else if (msg->message == WM_MOUSEWHEEL) { 303 // 4417236: If there is an old version of RichEd32.dll which 304 // does not provide the mouse wheel scrolling we have to 305 // interpret WM_MOUSEWHEEL as a sequence of scroll messages. 306 // kdm@sparc.spb.su 307 UINT platfScrollLines = 3; 308 // Retrieve a number of scroll lines. 309 ::SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, 310 &platfScrollLines, 0); 311 312 if (platfScrollLines > 0) { 313 HWND hWnd = GetHWnd(); 314 LONG styles = ::GetWindowLong(hWnd, GWL_STYLE); 315 316 RECT rect; 317 // rect.left and rect.top are zero. 318 // rect.right and rect.bottom contain the width and height 319 VERIFY(::GetClientRect(hWnd, &rect)); 320 321 // calculate a number of visible lines 322 TEXTMETRIC tm; 323 HDC hDC = ::GetDC(hWnd); 324 DASSERT(hDC != NULL); 325 VERIFY(::GetTextMetrics(hDC, &tm)); 326 VERIFY(::ReleaseDC(hWnd, hDC)); 327 LONG visibleLines = rect.bottom / tm.tmHeight + 1; 328 329 LONG lineCount = static_cast<LONG>(::SendMessage(hWnd, 330 EM_GETLINECOUNT, 0, 0)); 331 BOOL sb_vert_disabled = (styles & WS_VSCROLL) == 0 332 || (lineCount <= visibleLines); 333 334 LONG *delta_accum = &m_lVDeltaAccum; 335 UINT wm_msg = WM_VSCROLL; 336 int sb_type = SB_VERT; 337 338 if (sb_vert_disabled && (styles & WS_HSCROLL)) { 339 delta_accum = &m_lHDeltaAccum; 340 wm_msg = WM_HSCROLL; 341 sb_type = SB_HORZ; 342 } 343 344 int delta = (short) HIWORD(msg->wParam); 345 *delta_accum += delta; 346 if (abs(*delta_accum) >= WHEEL_DELTA) { 347 if (platfScrollLines == WHEEL_PAGESCROLL) { 348 // Synthesize a page down or a page up message. 349 ::SendMessage(hWnd, wm_msg, 350 (delta > 0) ? SB_PAGEUP : SB_PAGEDOWN, 0); 351 *delta_accum = 0; 352 } else { 353 // We provide a friendly behavior of text scrolling. 354 // During of scrolling the text can be out of the client 355 // area's boundary. Here we try to prevent this behavior. 356 SCROLLINFO si; 357 ::ZeroMemory(&si, sizeof(si)); 358 si.cbSize = sizeof(SCROLLINFO); 359 si.fMask = SIF_POS | SIF_RANGE | SIF_PAGE; 360 int actualScrollLines = 361 abs((int)(platfScrollLines * (*delta_accum / WHEEL_DELTA))); 362 for (int i = 0; i < actualScrollLines; i++) { 363 if (::GetScrollInfo(hWnd, sb_type, &si)) { 364 if ((wm_msg == WM_VSCROLL) 365 && ((*delta_accum < 0 366 && si.nPos >= (si.nMax - (int) si.nPage)) 367 || (*delta_accum > 0 368 && si.nPos <= si.nMin))) { 369 break; 370 } 371 } 372 // Here we don't send EM_LINESCROLL or EM_SCROLL 373 // messages to rich edit because it doesn't 374 // provide horizontal scrolling. 375 // So it's only possible to scroll by 1 line 376 // at a time to prevent scrolling when the 377 // scrollbar thumb reaches its boundary position. 378 ::SendMessage(hWnd, wm_msg, 379 (*delta_accum>0) ? SB_LINEUP : SB_LINEDOWN, 0); 380 } 381 *delta_accum %= WHEEL_DELTA; 382 } 383 } else { 384 *delta_accum = 0; 385 } 386 } 387 delete msg; 388 return mrConsume; 389 // 4417236: end of fix 390 } 391 392 return AwtTextComponent::HandleEvent(msg, synthetic); 393 } 394 395 396 /* Fix for 4776535, 4648702 397 * If width is 0 or 1 Windows hides the horizontal scroll bar even 398 * if the WS_HSCROLL style is set. It is a bug in Windows. 399 * As a workaround we should set an initial width to 2. 400 * kdm@sparc.spb.su 401 */ 402 void AwtTextArea::Reshape(int x, int y, int w, int h) 403 { 404 if (w < 2) { 405 w = 2; 406 } 407 AwtTextComponent::Reshape(x, y, w, h); 408 } 409 410 LONG AwtTextArea::getJavaSelPos(LONG orgPos) 411 { 412 long wlen; 413 long pos = orgPos; 414 long cur = 0; 415 long retval = 0; 416 LPTSTR wbuf; 417 418 if ((wlen = GetTextLength()) == 0) 419 return 0; 420 wbuf = new TCHAR[wlen + 1]; 421 GetText(wbuf, wlen + 1); 422 if (m_isLFonly == TRUE) { 423 wlen = RemoveCR(wbuf); 424 } 425 426 while (cur < pos && cur < wlen) { 427 if (wbuf[cur] == _T('\r') && wbuf[cur + 1] == _T('\n')) { 428 pos++; 429 } 430 cur++; 431 retval++; 432 } 433 delete[] wbuf; 434 return retval; 435 } 436 437 LONG AwtTextArea::getWin32SelPos(LONG orgPos) 438 { 439 if (GetTextLength() == 0) 440 return 0; 441 return orgPos; 442 } 443 444 void AwtTextArea::SetSelRange(LONG start, LONG end) 445 { 446 CHARRANGE cr; 447 cr.cpMin = getWin32SelPos(start); 448 cr.cpMax = getWin32SelPos(end); 449 EditSetSel(cr); 450 } 451 452 453 void AwtTextArea::_ReplaceText(void *param) 454 { 455 JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); 456 457 ReplaceTextStruct *rts = (ReplaceTextStruct *)param; 458 459 jobject textComponent = rts->textComponent; 460 jstring text = rts->text; 461 jint start = rts->start; 462 jint end = rts->end; 463 464 AwtTextComponent *c = NULL; 465 466 PDATA pData; 467 JNI_CHECK_PEER_GOTO(textComponent, done); 468 JNI_CHECK_NULL_GOTO(text, "null string", done); 469 470 c = (AwtTextComponent *)pData; 471 if (::IsWindow(c->GetHWnd())) 472 { 473 jsize length = env->GetStringLength(text) + 1; 474 // Bugid 4141477 - Can't use TO_WSTRING here because it uses alloca 475 // WCHAR* buffer = TO_WSTRING(text); 476 TCHAR *buffer = new TCHAR[length]; 477 env->GetStringRegion(text, 0, length-1, reinterpret_cast<jchar*>(buffer)); 478 buffer[length-1] = '\0'; 479 480 c->CheckLineSeparator(buffer); 481 c->RemoveCR(buffer); 482 // Fix for 5003402: added restoring/hiding selection to enable automatic scrolling 483 c->SendMessage(EM_HIDESELECTION, FALSE, TRUE); 484 c->SendMessage(EM_SETSEL, start, end); 485 c->SendMessage(EM_REPLACESEL, FALSE, (LPARAM)buffer); 486 c->SendMessage(EM_HIDESELECTION, TRUE, TRUE); 487 488 delete[] buffer; 489 } 490 491 done: 492 env->DeleteGlobalRef(textComponent); 493 env->DeleteGlobalRef(text); 494 495 delete rts; 496 } 497 498 499 /************************************************************************ 500 * TextArea native methods 501 */ 502 503 extern "C" { 504 505 /* 506 * Class: java_awt_TextArea 507 * Method: initIDs 508 * Signature: ()V 509 */ 510 JNIEXPORT void JNICALL 511 Java_java_awt_TextArea_initIDs(JNIEnv *env, jclass cls) 512 { 513 TRY; 514 515 AwtTextArea::scrollbarVisibilityID = 516 env->GetFieldID(cls, "scrollbarVisibility", "I"); 517 518 DASSERT(AwtTextArea::scrollbarVisibilityID != NULL); 519 520 CATCH_BAD_ALLOC; 521 } 522 523 } /* extern "C" */ 524 525 526 /************************************************************************ 527 * WTextAreaPeer native methods 528 */ 529 530 extern "C" { 531 532 /* 533 * Class: sun_awt_windows_WTextAreaPeer 534 * Method: create 535 * Signature: (Lsun/awt/windows/WComponentPeer;)V 536 */ 537 JNIEXPORT void JNICALL 538 Java_sun_awt_windows_WTextAreaPeer_create(JNIEnv *env, jobject self, 539 jobject parent) 540 { 541 TRY; 542 543 PDATA pData; 544 JNI_CHECK_PEER_RETURN(parent); 545 AwtToolkit::CreateComponent(self, parent, 546 (AwtToolkit::ComponentFactory) 547 AwtTextArea::Create); 548 JNI_CHECK_PEER_CREATION_RETURN(self); 549 550 CATCH_BAD_ALLOC; 551 } 552 553 /* 554 * Class: sun_awt_windows_WTextAreaPeer 555 * Method: replaceRange 556 * Signature: (Ljava/lang/String;II)V 557 */ 558 JNIEXPORT void JNICALL 559 Java_sun_awt_windows_WTextAreaPeer_replaceRange(JNIEnv *env, jobject self, 560 jstring text, 561 jint start, jint end) 562 { 563 TRY; 564 565 jobject selfGlobalRef = env->NewGlobalRef(self); 566 jstring textGlobalRef = (jstring)env->NewGlobalRef(text); 567 568 ReplaceTextStruct *rts = new ReplaceTextStruct; 569 rts->textComponent = selfGlobalRef; 570 rts->text = textGlobalRef; 571 rts->start = start; 572 rts->end = end; 573 574 AwtToolkit::GetInstance().SyncCall(AwtTextArea::_ReplaceText, rts); 575 // global refs and rts are deleted in _ReplaceText() 576 577 CATCH_BAD_ALLOC; 578 } 579 } /* extern "C" */