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 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
|
#include "EditWidget.h"
#if 0
#include "SysFont.h"
#endif
#include "Font.h"
#include "WidgetManager.h"
#include "SexyAppBase.h"
#include "EditListener.h"
#include "SDL_keysym.h"
using namespace Sexy;
static int gEditWidgetColors[][3] =
{{255, 255, 255},
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
{255, 255, 255}};
EditWidget::EditWidget(int theId, EditListener* theEditListener)
{
mId = theId;
mEditListener = theEditListener;
mFont = NULL;
mHadDoubleClick = false;
mHilitePos = -1;
mLastModifyIdx = -1;
mLeftPos = 0;
mUndoCursor = 0;
mUndoHilitePos = 0;
mLastModifyIdx = 0;
mBlinkAcc = 0;
mCursorPos = 0;
mShowingCursor = false;
mDrawSelOverride = false;
mMaxChars = -1;
mMaxPixels = -1;
mPasswordChar = 0;
mBlinkDelay = 40;
SetColors(gEditWidgetColors, NUM_COLORS);
}
EditWidget::~EditWidget()
{
delete mFont;
ClearWidthCheckFonts();
}
void EditWidget::ClearWidthCheckFonts()
{
for (WidthCheckList::iterator anItr = mWidthCheckList.begin(); anItr != mWidthCheckList.end(); ++anItr)
delete anItr->mFont;
mWidthCheckList.clear();
}
void EditWidget::AddWidthCheckFont(Font *theFont, int theMaxPixels)
{
mWidthCheckList.push_back(WidthCheck());
WidthCheck &aCheck = mWidthCheckList.back();
aCheck.mWidth = theMaxPixels;
aCheck.mFont = theFont->Duplicate();
}
void EditWidget::SetText(const SexyString& theText, bool leftPosToZero)
{
mString = theText;
mCursorPos = mString.length();
mHilitePos = 0;
if (leftPosToZero)
mLeftPos = 0;
else
FocusCursor(true);
MarkDirty();
}
SexyString& EditWidget::GetDisplayString()
{
if (mPasswordChar==0)
return mString;
if (mPasswordDisplayString.size()!=mString.size())
{
mPasswordDisplayString = SexyString(mString.size(), mPasswordChar);
//mPasswordDisplayString.resize(mString.size());
//for (int i=0; i<(int)mPasswordDisplayString.length(); i++)
// mPasswordDisplayString[i] = mPasswordChar;
}
return mPasswordDisplayString;
}
bool EditWidget::WantsFocus()
{
return true;
}
void EditWidget::Resize(int theX, int theY, int theWidth, int theHeight)
{
Widget::Resize(theX, theY, theWidth, theHeight);
FocusCursor(false);
}
void EditWidget::SetFont(Font* theFont, Font* theWidthCheckFont)
{
delete mFont;
mFont = theFont->Duplicate();
ClearWidthCheckFonts();
if (theWidthCheckFont != NULL)
AddWidthCheckFont(theWidthCheckFont);
}
void EditWidget::Draw(Graphics* g) // Already translated
{
#if 0
if (mFont == NULL)
mFont = new SysFont(mWidgetManager->mApp, "Arial Unicode MS", 10, false);
#endif
SexyString &aString = GetDisplayString();
g->SetColor(mColors[COLOR_BKG]);
g->FillRect(0, 0, mWidth, mHeight);
for (int i = 0; i < 2; i++)
{
Graphics* aClipG = g->Create();
aClipG->SetFont(mFont);
if (i == 1)
{
int aCursorX = mFont->StringWidth(aString.substr(0, mCursorPos)) - mFont->StringWidth(aString.substr(0, mLeftPos));
int aHiliteX = aCursorX+2;
if ((mHilitePos != -1) && (mCursorPos != mHilitePos))
aHiliteX = mFont->StringWidth(aString.substr(0, mHilitePos)) - mFont->StringWidth(aString.substr(0, mLeftPos));
if (!mShowingCursor)
aCursorX += 2;
aCursorX = std::min(std::max(0, aCursorX), mWidth-8);
aHiliteX = std::min(std::max(0, aHiliteX), mWidth-8);
aClipG->ClipRect(4 + std::min(aCursorX, aHiliteX), (mHeight - mFont->GetHeight())/2, abs(aHiliteX - aCursorX), mFont->GetHeight());
}
else
aClipG->ClipRect(4, 0, mWidth-8, mHeight);
bool hasfocus = mHasFocus || mDrawSelOverride;
if (i == 1 && hasfocus)
{
aClipG->SetColor(mColors[COLOR_HILITE]);
aClipG->FillRect(0, 0, mWidth, mHeight);
}
if (i == 0 || !hasfocus)
aClipG->SetColor(mColors[COLOR_TEXT]);
else
aClipG->SetColor(mColors[COLOR_HILITE_TEXT]);
aClipG->DrawString(aString.substr(mLeftPos), 4, (mHeight - mFont->GetHeight())/2 + mFont->GetAscent());
delete aClipG;
}
g->SetColor(mColors[COLOR_OUTLINE]);
g->DrawRect(0, 0, mWidth-1, mHeight-1);
}
void EditWidget::UpdateCaretPos()
{
SexyAppBase *anApp = mWidgetManager->mApp;
Point aPoint = GetAbsPos();
if (aPoint.mX<10) aPoint.mX = 10;
else if (aPoint.mX>anApp->mWidth-10) aPoint.mX = anApp->mWidth-10;
if (aPoint.mY<10) aPoint.mY = 10;
else if (aPoint.mY>anApp->mHeight-10) aPoint.mY = anApp->mHeight-10;
#if 0
SetCaretPos(aPoint.mX,aPoint.mY);
#endif
}
void EditWidget::GotFocus()
{
Widget::GotFocus();
if (mWidgetManager && mWidgetManager->mApp->mTabletPC)
{
SexyAppBase *anApp = mWidgetManager->mApp;
#if 0
CreateCaret(anApp->mHWnd,NULL,0,0);
UpdateCaretPos();
ShowCaret(anApp->mHWnd);
#endif
}
mShowingCursor = true;
mBlinkAcc = 0;
MarkDirty();
}
void EditWidget::LostFocus()
{
Widget::LostFocus();
if (mWidgetManager && mWidgetManager->mApp->mTabletPC)
{
#if 0
HideCaret(mWidgetManager->mApp->mHWnd);
DestroyCaret();
#endif
}
mShowingCursor = false;
MarkDirty();
}
void EditWidget::Update()
{
Widget::Update();
if (mHasFocus)
{
if (mWidgetManager->mApp->mTabletPC)
{
UpdateCaretPos();
}
if (++mBlinkAcc > mBlinkDelay)
{
MarkDirty();
mBlinkAcc = 0;
mShowingCursor = !mShowingCursor;
}
}
}
void EditWidget::EnforceMaxPixels()
{
if (mMaxPixels<=0 && mWidthCheckList.empty()) // no width checking in effect
return;
if (mWidthCheckList.empty())
{
while (mFont->StringWidth(mString) > mMaxPixels)
mString = mString.substr(0, mString.length()-1);
return;
}
for (WidthCheckList::iterator anItr = mWidthCheckList.begin(); anItr != mWidthCheckList.end(); ++anItr)
{
int aWidth = anItr->mWidth;
if (aWidth<=0)
{
aWidth = mMaxPixels;
if (aWidth<=0)
continue;
}
while (anItr->mFont->StringWidth(mString) > aWidth)
mString = mString.substr(0,mString.length()-1);
}
}
bool EditWidget::IsPartOfWord(SexyChar theChar)
{
#if 0
return (((theChar >= _S('A')) && (theChar <= _S('Z'))) ||
((theChar >= _S('a')) && (theChar <= _S('z'))) ||
((theChar >= _S('0')) && (theChar <= _S('9'))) ||
(((unsigned int)theChar >= (unsigned int)(L'')) && ((unsigned int)theChar <= (unsigned int)(L''))) ||
(theChar == _S('_')));
#else
return false;
#endif
}
void EditWidget::ProcessKey(SDLKey theKey, SexyChar theChar)
{
if ((theKey == SDLK_LSHIFT) || (theKey == SDLK_RSHIFT) || (theKey == SDLK_LCTRL) || (theKey == SDLK_RCTRL))
return;
bool shiftDown = mWidgetManager->mKeyDown[SDLK_LSHIFT] || mWidgetManager->mKeyDown[SDLK_RSHIFT];
bool controlDown = mWidgetManager->mKeyDown[SDLK_LCTRL] || mWidgetManager->mKeyDown[SDLK_RCTRL];
bool bigChange = false;
bool removeHilite = !shiftDown;
if (shiftDown && (mHilitePos == -1))
mHilitePos = mCursorPos;
SexyString anOldString = mString;
int anOldCursorPos = mCursorPos;
int anOldHilitePos = mHilitePos;
if ((theChar == 3) || (theChar == 24))
{
// Copy selection
if ((mHilitePos != -1) && (mHilitePos != mCursorPos))
{
#if 0
if (mCursorPos < mHilitePos)
mWidgetManager->mApp->CopyToClipboard(SexyStringToString(GetDisplayString().substr(mCursorPos, mHilitePos)));
else
mWidgetManager->mApp->CopyToClipboard(SexyStringToString(GetDisplayString().substr(mHilitePos, mCursorPos)));
#endif
if (theChar == 3)
{
removeHilite = false;
}
else
{
mString = mString.substr(0, std::min(mCursorPos, mHilitePos)) + mString.substr(std::max(mCursorPos, mHilitePos));
mCursorPos = std::min(mCursorPos, mHilitePos);
mHilitePos = -1;
bigChange = true;
}
}
}
else if (theChar == 22)
{
// Paste selection
#if 0
SexyString aBaseString = StringToSexyString(mWidgetManager->mApp->GetClipboard());
#else
SexyString aBaseString;
#endif
if (aBaseString.length() > 0)
{
SexyString aString;
for (uint32_t i = 0; i < aBaseString.length(); i++)
{
if ((aBaseString[i] == '\r') || (aBaseString[i] == '\n'))
break;
if (mFont->CharWidth(aBaseString[i]) != 0 && mEditListener->AllowChar(mId, aBaseString[i]))
aString += aBaseString[i];
}
if (mHilitePos == -1)
{
// Insert string where cursor is
mString = mString.substr(0, mCursorPos) + aString + mString.substr(mCursorPos);
}
else
{
// Replace selection with new string
mString = mString.substr(0, std::min(mCursorPos, mHilitePos)) + aString + mString.substr(std::max(mCursorPos, mHilitePos));
mCursorPos = std::min(mCursorPos, mHilitePos);
mHilitePos = -1;
}
mCursorPos += aString.length();
bigChange = true;
}
}
else if (theChar == 26)
{
// Undo
mLastModifyIdx = -1;
SexyString aSwapString = mString;
int aSwapCursorPos = mCursorPos;
int aSwapHilitePos = mHilitePos;
mString = mUndoString;
mCursorPos = mUndoCursor;
mHilitePos = mUndoHilitePos;
mUndoString = aSwapString;
mUndoCursor = aSwapCursorPos;
mUndoHilitePos = aSwapHilitePos;
removeHilite = false;
}
else if (theKey == SDLK_LEFT)
{
if (controlDown)
{
// Get to a word
while ((mCursorPos > 0) && (!IsPartOfWord(mString[mCursorPos-1])))
mCursorPos--;
// Go beyond the word
while ((mCursorPos > 0) && (IsPartOfWord(mString[mCursorPos-1])))
mCursorPos--;
}
else if (shiftDown || (mHilitePos == -1))
mCursorPos--;
else
mCursorPos = std::min(mCursorPos, mHilitePos);
}
else if (theKey == SDLK_RIGHT)
{
if (controlDown)
{
// Get to whitespace
while ((mCursorPos < (int) mString.length()-1) && (IsPartOfWord(mString[mCursorPos+1])))
mCursorPos++;
// Go beyond the whitespace
while ((mCursorPos < (int) mString.length()-1) && (!IsPartOfWord(mString[mCursorPos+1])))
mCursorPos++;
}
if (shiftDown || (mHilitePos == -1))
mCursorPos++;
else
mCursorPos = std::max(mCursorPos, mHilitePos);
}
else if (theKey == SDLK_BACKSPACE)
{
if (mString.length() > 0)
{
if ((mHilitePos != -1) && (mHilitePos != mCursorPos))
{
// Delete selection
mString = mString.substr(0, std::min(mCursorPos, mHilitePos)) + mString.substr(std::max(mCursorPos, mHilitePos));
mCursorPos = std::min(mCursorPos, mHilitePos);
mHilitePos = -1;
bigChange = true;
}
else
{
// Delete char behind cursor
if (mCursorPos > 0)
mString = mString.substr(0, mCursorPos-1) + mString.substr(mCursorPos);
else
mString = mString.substr(mCursorPos);
mCursorPos--;
mHilitePos = -1;
if (mCursorPos != mLastModifyIdx)
bigChange = true;
mLastModifyIdx = mCursorPos-1;
}
}
}
else if (theKey == SDLK_DELETE)
{
if (mString.length() > 0)
{
if ((mHilitePos != -1) && (mHilitePos != mCursorPos))
{
// Delete selection
mString = mString.substr(0, std::min(mCursorPos, mHilitePos)) + mString.substr(std::max(mCursorPos, mHilitePos));
mCursorPos = std::min(mCursorPos, mHilitePos);
mHilitePos = -1;
bigChange = true;
}
else
{
// Delete char in front of cursor
if (mCursorPos < (int) mString.length())
mString = mString.substr(0, mCursorPos) + mString.substr(mCursorPos+1);
if (mCursorPos != mLastModifyIdx)
bigChange = true;
mLastModifyIdx = mCursorPos;
}
}
}
else if (theKey == SDLK_HOME)
{
mCursorPos = 0;
}
else if (theKey == SDLK_END)
{
mCursorPos = mString.length();
}
else if (theKey == SDLK_RETURN)
{
mEditListener->EditWidgetText(mId, mString);
}
else
{
SexyString aString = SexyString(1, theChar);
unsigned int uTheChar = (unsigned int)theChar;
unsigned int range = 127;
if (gSexyAppBase->mbAllowExtendedChars)
{
range = 255;
}
if ((uTheChar >= 32) && (uTheChar <= range) && (mFont->StringWidth(aString) > 0) && mEditListener->AllowChar(mId, theChar))
{
if ((mHilitePos != -1) && (mHilitePos != mCursorPos))
{
// Replace selection with new character
mString = mString.substr(0, std::min(mCursorPos, mHilitePos)) + SexyString(1, theChar) + mString.substr(std::max(mCursorPos, mHilitePos));
mCursorPos = std::min(mCursorPos, mHilitePos);
mHilitePos = -1;
bigChange = true;
}
else
{
// Insert character where cursor is
mString = mString.substr(0, mCursorPos) + SexyString(1, theChar) + mString.substr(mCursorPos);
if (mCursorPos != mLastModifyIdx+1)
bigChange = true;
mLastModifyIdx = mCursorPos;
mHilitePos = -1;
}
mCursorPos++;
FocusCursor(false);
}
else
removeHilite = false;
}
if ((mMaxChars != -1) && ((int) mString.length() > mMaxChars))
mString = mString.substr(0, mMaxChars);
EnforceMaxPixels();
if (mCursorPos < 0)
mCursorPos = 0;
else if (mCursorPos > (int) mString.length())
mCursorPos = mString.length();
if (anOldCursorPos != mCursorPos)
{
mBlinkAcc = 0;
mShowingCursor = true;
}
FocusCursor(true);
if (removeHilite || mHilitePos==mCursorPos)
mHilitePos = -1;
if (!mEditListener->AllowText(mId, mString))
{
mString = anOldString;
mCursorPos = anOldCursorPos;
mHilitePos = anOldHilitePos;
}
else if (bigChange)
{
mUndoString = anOldString;
mUndoCursor = anOldCursorPos;
mUndoHilitePos = anOldHilitePos;
}
MarkDirty();
}
//FIXME SDL takes care of modifying keys,etc so use it
void EditWidget::KeyDown(SDLKey theKey)
{
if (((theKey < 'A') || (theKey >= 'Z')) && mEditListener->AllowKey(mId, theKey))
ProcessKey(theKey, 0);
Widget::KeyDown(theKey);
}
void EditWidget::KeyChar(SexyChar theChar)
{
// if (mEditListener->AllowChar(mId, theChar))
ProcessKey(SDLK_UNKNOWN, theChar);
Widget::KeyChar(theChar);
}
int EditWidget::GetCharAt(int x, int y)
{
int aPos = 0;
SexyString &aString = GetDisplayString();
for (int i = mLeftPos; i < (int) aString.length(); i++)
{
SexyString aLoSubStr = aString.substr(mLeftPos, i-mLeftPos);
SexyString aHiSubStr = aString.substr(mLeftPos, i-mLeftPos+1);
int aLoLen = mFont->StringWidth(aLoSubStr);
int aHiLen = mFont->StringWidth(aHiSubStr);
if (x >= (aLoLen+aHiLen)/2 + 5)
aPos = i+1;
}
return aPos;
}
void EditWidget::FocusCursor(bool bigJump)
{
while (mCursorPos < mLeftPos)
{
if (bigJump)
mLeftPos = std::max(0, mLeftPos-10);
else
mLeftPos = std::max(0, mLeftPos-1);
MarkDirty();
}
if (mFont != NULL)
{
SexyString &aString = GetDisplayString();
while ((mWidth-8 > 0) && (mFont->StringWidth(aString.substr(0, mCursorPos)) - mFont->StringWidth(aString.substr(0, mLeftPos)) >= mWidth-8))
{
if (bigJump)
mLeftPos = std::min(mLeftPos + 10, (int) mString.length()-1);
else
mLeftPos = std::min(mLeftPos + 1, (int) mString.length()-1);
MarkDirty();
}
}
}
void EditWidget::MouseDown(int x, int y, int theBtnNum, int theClickCount)
{
Widget::MouseDown(x, y, theBtnNum, theClickCount);
mHilitePos = -1;
mCursorPos = GetCharAt(x, y);
if (theClickCount > 1)
{
mHadDoubleClick = true;
HiliteWord();
}
MarkDirty();
FocusCursor(false);
}
void EditWidget::MouseUp(int x, int y, int theBtnNum, int theClickCount)
{
Widget::MouseUp(x,y,theBtnNum,theClickCount);
if (mHilitePos==mCursorPos)
mHilitePos = -1;
if (mHadDoubleClick)
{
mHilitePos = -1;
mCursorPos = GetCharAt(x, y);
mHadDoubleClick = false;
HiliteWord();
}
MarkDirty();
}
void EditWidget::HiliteWord()
{
SexyString &aString = GetDisplayString();
if (mCursorPos < (int) aString.length())
{
// Find first space before word
mHilitePos = mCursorPos;
while ((mHilitePos > 0) && (IsPartOfWord(aString[mHilitePos-1])))
mHilitePos--;
// Find first space after word
while ((mCursorPos < (int) aString.length()-1) && (IsPartOfWord(aString[mCursorPos+1])))
mCursorPos++;
if (mCursorPos < (int) aString.length())
mCursorPos++;
}
}
void EditWidget::MouseDrag(int x, int y)
{
Widget::MouseDrag(x, y);
if (mHilitePos == -1)
mHilitePos = mCursorPos;
mCursorPos = GetCharAt(x, y);
MarkDirty();
FocusCursor(false);
}
void EditWidget::MouseEnter()
{
Widget::MouseEnter();
mWidgetManager->mApp->SetCursor(CURSOR_TEXT);
}
void EditWidget::MouseLeave()
{
Widget::MouseLeave();
mWidgetManager->mApp->SetCursor(CURSOR_POINTER);
}
void EditWidget::MarkDirty()
{
if (mColors[COLOR_BKG].mAlpha != 255)
Widget::MarkDirtyFull();
else
Widget::MarkDirty();
}
|