aEdit.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. #define AEDIT_CPP
  2. /*************************************************************************************************\
  3. aEdit.cpp : Implementation of the aEdit component of the GUI library.
  4. //---------------------------------------------------------------------------//
  5. // Copyright (C) Microsoft Corporation. All rights reserved. //
  6. //===========================================================================//
  7. \*************************************************************************************************/
  8. #include "aEdit.h"
  9. #include "userInput.h"
  10. #include "cType.h"
  11. #include "IniFile.h"
  12. #include "mclib.h"
  13. #include <windows.h>
  14. #include <mbstring.h>
  15. #include <winnls.h>
  16. #include "soundSys.h"
  17. #include "..\resource.h"
  18. #define ENTRY_MAX_CHARS 1000
  19. #define ENTRY_MARGIN 4.f
  20. #define HalfColorValue(color) (((color >> 1) & 0x007f0000) | ((color >> 1) & 0x00007f00) | ((color >> 1) & 0x0000007f))
  21. static long acp = 0;
  22. static bool g_bUseLangDll;
  23. static gosIME_Appearance g_ia;
  24. extern SoundSystem *sndSystem;
  25. aEdit::aEdit( )
  26. {
  27. if (acp == 0) {
  28. acp = GetACP();
  29. gos_SetIMELevel( 2 );
  30. gos_GetIMEAppearance( &g_ia );
  31. char szSetting[8];
  32. szSetting[0] = '1';
  33. cLoadString( IDS_IME_USE_LANGDLL_SETTING, szSetting, sizeof( szSetting ) );
  34. g_bUseLangDll = szSetting[0] == '1';
  35. if (!g_bUseLangDll) {
  36. // setup fixed colors
  37. g_ia.symbolColor = 0xff5c96c2;
  38. g_ia.symbolColorText = 0x7f000000;
  39. g_ia.symbolTranslucence = 0x7f;
  40. g_ia.symbolPlacement = 0;
  41. g_ia.candColorBase = 0xff43311c;
  42. g_ia.compColorTargetConv = 0xff5c96c2;
  43. g_ia.compColorTargetNotConv = 0xff002f55;
  44. g_ia.compColorInputErr = 0xffff00ff;
  45. g_ia.compTranslucence = 0x7f;
  46. if (acp != 949) { // other than Korean version, reduce color value by half so that composition string can be seen
  47. g_ia.compColorTargetConv = HalfColorValue(g_ia.compColorTargetConv);
  48. g_ia.compColorTargetNotConv = HalfColorValue(g_ia.compColorTargetNotConv);
  49. g_ia.compColorInputErr = HalfColorValue(g_ia.compColorInputErr);
  50. }
  51. }
  52. }
  53. nLimit = ENTRY_MAX_CHARS;
  54. nInsertion1 = nInsertion2 = 0;
  55. nLeftOffset = 0;
  56. bMouseDown=0;
  57. bCursorVisible=FALSE;
  58. bFocus = 0;
  59. dwStyleFlags = 0;
  60. cursorTime = 0.f;
  61. setColor( 0xff000000 );
  62. bAllowIME = true;
  63. bIMEInitialized = false;
  64. bWierdChars = true;
  65. //MarkForBackSurfaceUpdate();
  66. }
  67. aEdit& aEdit::operator =( const aEdit& src )
  68. {
  69. aObject::operator=( src );
  70. nLimit = src.nLimit;
  71. nInsertion1 = nInsertion2 = 0;
  72. nLeftOffset = src.nLeftOffset;
  73. bMouseDown = 0;
  74. bCursorVisible = FALSE;
  75. dwStyleFlags = src.dwStyleFlags;
  76. text = src.text;
  77. textColor = src.textColor;
  78. highlightColor = src.highlightColor;
  79. cursorColor = src.cursorColor;
  80. selectedColor = src.selectedColor; // selected text
  81. outlineColor = src.outlineColor;
  82. font = src.font;
  83. bAllowIME = src.bAllowIME;
  84. return *this;
  85. }
  86. //-------------------------------------------------------------------------------------------------
  87. aEdit::~aEdit()
  88. {
  89. setFocus( 0 );
  90. }
  91. void aEdit::update()
  92. {
  93. cursorTime += frameLength;
  94. if ( cursorTime > .5 )
  95. {
  96. bCursorVisible ^= 1;
  97. if ( !bFocus )
  98. bFocus = bCursorVisible = 0;
  99. cursorTime = 0.f;
  100. }
  101. handleMouse();
  102. handleKeyboard();
  103. }
  104. void aEdit::handleMouse()
  105. {
  106. int mouseX = userInput->getMouseX();
  107. int mouseY = userInput->getMouseY();
  108. bool bLeftClick = userInput->isLeftClick();
  109. bool bRightClick = userInput->isRightClick();
  110. int nOffset = mouseX - (globalX() + ENTRY_MARGIN);
  111. nOffset += nLeftOffset;
  112. // find char position of nOffset and set insertion point there
  113. int nChar = findChar(nOffset);
  114. if ( bMouseDown && userInput->isLeftDrag() && bFocus )
  115. {
  116. nInsertion2 = nChar;
  117. }
  118. if ( pointInside( mouseX, mouseY ) )
  119. {
  120. if ( bLeftClick && !( dwStyleFlags & ES_EDITREADONLY ) )
  121. {
  122. setFocus( true );
  123. bMouseDown = true;
  124. bCursorVisible=TRUE;
  125. nInsertion1 = nChar;
  126. if (gos_GetKeyStatus(KEY_LSHIFT) == KEY_FREE)
  127. nInsertion2 = nInsertion1;
  128. }
  129. else if ( bRightClick )
  130. {
  131. }
  132. }
  133. else if ( bLeftClick )
  134. setFocus( false );
  135. }
  136. void aEdit::handleKeyboard()
  137. {
  138. if ( !bFocus )
  139. return;
  140. while( true ) // keep getting keys until buffer is empty
  141. {
  142. DWORD key = gos_GetKey();
  143. if ( !key )
  144. return;
  145. if (!handleFormattingKeys(key))
  146. {
  147. // hack to eat control keys, must be a real way to do this
  148. if ( ((key & 0x00ff) + ((key & 0x00ff)<<8) + 0x4000) == key
  149. && key != 0x00006e2e )
  150. return;
  151. key &= 0x00ff;
  152. // this does happen when IME is enabled.
  153. if (key == 0)
  154. return;
  155. DWORD key2 = 0;
  156. if ( isleadbyte( key ) )
  157. {
  158. key2 = gos_GetKey( );
  159. key2 &= 0x00ff;
  160. }
  161. if (dwStyleFlags & ES_EDITNUM)
  162. {
  163. if (!isdigit(key) && key != ' ' && key != '.')
  164. return;
  165. }
  166. else if (dwStyleFlags & ES_EDITALPHA)
  167. {
  168. if (!isalpha(key) && key != ' ')
  169. return;
  170. }
  171. else if (dwStyleFlags & ES_EDITALPHANUM)
  172. {
  173. if (!isalpha(key) && !isdigit(key) && key != ' ')
  174. return;
  175. }
  176. if((dwStyleFlags & ES_EDITNOBLANK) && (key==' '))
  177. return;
  178. // if there is a selection range, bolt the text within it
  179. if (gos_GetKeyStatus(KEY_LMENU) != KEY_FREE )
  180. {
  181. return;
  182. }
  183. if ( key == '%'
  184. || key == '\\'
  185. || key == '/'
  186. || key == '\"') // eat these so andy doesn't try and sprintf on us
  187. return;
  188. if ( !bWierdChars )
  189. {
  190. if ( (key == '\\') ||
  191. (key == ':') ||
  192. (key == '*') ||
  193. (key == '\'') ||
  194. (key == '.') ||
  195. (key == ',') ||
  196. (key == '/') ||
  197. (key == '<') ||
  198. (key == '>') ||
  199. (key == '?') ||
  200. (key == '"') ||
  201. (key == '|') )
  202. return;
  203. }
  204. // is there space to insert a character?
  205. if (text.Length() - labs( nInsertion2 - nInsertion1 ) + (key2 ? 1 : 0) >= nLimit)
  206. {
  207. sndSystem->playDigitalSample( LOG_WRONGBUTTON );
  208. gos_FinalizeStringIME();
  209. gos_KeyboardFlush();
  210. return;
  211. }
  212. // insert the character
  213. if ( key )
  214. {
  215. clearSelection();
  216. char str[3];
  217. str[0] = key;
  218. int amountToAdd = 1;
  219. if ( key2 )
  220. {
  221. str[1] = key2;
  222. str[2] = NULL;
  223. amountToAdd = 2;
  224. }
  225. else
  226. str[1] = NULL;
  227. if (nInsertion1 < text.Length() )
  228. {
  229. text.Insert( nInsertion1, str );
  230. }
  231. else
  232. text += str;
  233. nInsertion1 += amountToAdd;
  234. nInsertion2 = nInsertion1;
  235. }
  236. }
  237. makeCursorVisible();
  238. }
  239. }
  240. void aEdit::renderWithDropShadow()
  241. {
  242. // do not do this for .ttf where we already are having performance issues
  243. if ( font.getSize() == 1 )
  244. {
  245. long realTextColor = textColor;
  246. textColor = 0xff000000;
  247. move( -1, 1 );
  248. render();
  249. move( 1, -1 );
  250. textColor = realTextColor;
  251. render();
  252. }
  253. else
  254. render();
  255. }
  256. void aEdit::render()
  257. {
  258. if ( !isShowing() )
  259. return;
  260. aObject::render();
  261. EString textToDraw = text;
  262. textToDraw += ' '; // have to do this for loc, some fonts are getting clipped in millenium
  263. // draw selection range
  264. if (nInsertion1 != nInsertion2)
  265. {
  266. int nMin = nInsertion1<nInsertion2?nInsertion1:nInsertion2;
  267. int nMax = nInsertion1<nInsertion2?nInsertion2:nInsertion1;
  268. if ( nMax > text.Length() )
  269. nMax = text.Length();
  270. if ( nMax > 1 )
  271. {
  272. if (isleadbyte( nMax -2 ) )
  273. nMax -= 2;
  274. }
  275. int nXSelStart = (int)(globalX() + charXPos(nMin) - nLeftOffset + ENTRY_MARGIN);
  276. int nXSelEnd = (int)(globalX() + charXPos(nMax) - nLeftOffset + ENTRY_MARGIN);
  277. if (nXSelStart < globalX())
  278. nXSelStart = globalX();
  279. if (nXSelEnd > globalX() + width())
  280. nXSelEnd = globalX() + width();
  281. GUI_RECT rect = { (int)nXSelStart,
  282. (int)globalY(),
  283. (int)nXSelEnd,
  284. (int)(globalY() + height()) };
  285. int startChar = 0;
  286. if ( nLeftOffset )
  287. {
  288. // need to find character where this position is
  289. startChar = findChar( nLeftOffset );
  290. }
  291. if ( text.Length() )
  292. {
  293. font.render( &textToDraw[startChar],
  294. (int)(globalX() + ENTRY_MARGIN),
  295. (int)globalY(), (int)width(), (int)height(),
  296. textColor, 0, 0 );
  297. }
  298. drawRect( rect, highlightColor);
  299. if ( text.Length() )
  300. {
  301. char tmp = textToDraw[nMax];
  302. textToDraw[nMax] = NULL;
  303. font.render( &textToDraw[nMin], (int)nXSelStart, (int)globalY(),
  304. nXSelEnd, (int)height(), selectedColor, 0, 0 );
  305. textToDraw[nMax] = tmp;
  306. }
  307. }
  308. else
  309. {
  310. int startChar = 0;
  311. if ( nLeftOffset )
  312. {
  313. // need to find character where this position is
  314. startChar = findChar( nLeftOffset );
  315. }
  316. if ( textToDraw.Length() )
  317. {
  318. font.render( &textToDraw[startChar],
  319. (int)(globalX() + ENTRY_MARGIN),
  320. (int)globalY(), (int)width(), (int)height(),
  321. textColor, 0, 0 );
  322. }
  323. else
  324. {
  325. font.render( "",
  326. (int)(globalX() + ENTRY_MARGIN),
  327. (int)globalY(), (int)width(), (int)height(),
  328. textColor, 0, 0 );
  329. }
  330. }
  331. if ( bFocus && bAllowIME )
  332. {
  333. gos_TextSetRegion( globalX() + ENTRY_MARGIN, globalY(), globalX() + width(), globalY() + height() - 1 );
  334. long pos = charXPos( nInsertion1 );
  335. if ( acp == 949 && nInsertion1 > nInsertion2 && gos_GetMachineInformation(gos_Info_GetIMEStatus))
  336. pos = charXPos( nInsertion2 );
  337. gos_PositionIME( globalX() + pos - nLeftOffset + ENTRY_MARGIN, globalY() );
  338. }
  339. drawCursor();
  340. }
  341. void aEdit::getEntry(EString& str)
  342. {
  343. str = text;
  344. }
  345. void aEdit::setEntry(const EString& str, BYTE byHighlight)
  346. {
  347. text = str;
  348. if (byHighlight)
  349. {
  350. nInsertion2 = text.Length();
  351. nInsertion1 = 0;
  352. }
  353. else
  354. {
  355. nInsertion2 = nInsertion1 = text.Length();
  356. flushCursorRight();
  357. }
  358. }
  359. void aEdit::setFocus(bool bHasFocus)
  360. {
  361. bool bFocusSave = bFocus;
  362. bFocus = bHasFocus;
  363. gos_KeyboardFlush();
  364. if ( bAllowIME && bHasFocus )
  365. {
  366. gos_EnableIME( true );
  367. bCursorVisible = bFocus;
  368. if (!g_bUseLangDll || g_ia.symbolPlacement == 0)
  369. g_ia.symbolHeight = height() - 2;
  370. if (!g_bUseLangDll) {
  371. // setup dynamic colors
  372. g_ia.candColorBorder = textColor;
  373. g_ia.candColorText = textColor;
  374. g_ia.compColorInput = textColor;
  375. g_ia.compColorConverted = textColor;
  376. g_ia.compColorText = textColor;
  377. if (acp != 949) { // other than Korean version, reduce color value by half
  378. g_ia.compColorInput = HalfColorValue(g_ia.compColorInput);
  379. g_ia.compColorConverted = HalfColorValue(g_ia.compColorConverted);
  380. g_ia.compColorInputErr = HalfColorValue(g_ia.compColorInputErr);
  381. }
  382. }
  383. gos_SetIMEAppearance( &g_ia );
  384. if ( !bIMEInitialized )
  385. {
  386. if ( acp == 932 || acp == 874 || acp == 936 || acp == 949 || acp == 950 )
  387. {
  388. // only want to do this if using IME
  389. resize( width() - height() - 2, height() );
  390. }
  391. bIMEInitialized = true;
  392. }
  393. }
  394. else {
  395. // bug #4862 - diable IME only when the edit box had the focus.
  396. if (bFocusSave)
  397. gos_EnableIME( false );
  398. }
  399. }
  400. bool aEdit::clearSelection()
  401. {
  402. if (nInsertion1 != nInsertion2)
  403. { // delete selection range
  404. int nMin = nInsertion1<nInsertion2?nInsertion1:nInsertion2;
  405. int nMax = nInsertion1<nInsertion2?nInsertion2:nInsertion1;
  406. int len = 1;
  407. text.Remove( nMin, nMax-len );
  408. nInsertion1 = nMin;
  409. nInsertion2 = nInsertion1;
  410. flushCursorRight();
  411. return true;
  412. }
  413. return false;
  414. }
  415. void aEdit:: backSpace(int nPosition)
  416. {
  417. int nCharCount = text.Length();
  418. if (nPosition>nCharCount)
  419. return;
  420. if (nPosition<=0)
  421. {
  422. nPosition = 0;
  423. return;
  424. }
  425. nCharCount = 1;
  426. if ( nPosition > 1 )
  427. {
  428. unsigned char* pPrev = _mbsdec( (const unsigned char*)(const char*)text, (const unsigned char*)(const char*)text + nPosition );
  429. nCharCount = (const unsigned char*)(const char*)text + nPosition - pPrev;
  430. }
  431. text.Remove( nPosition-nCharCount, nPosition-1 );
  432. nInsertion2 = nInsertion1 = nPosition-nCharCount;
  433. }
  434. void aEdit:: drawCursor()
  435. {
  436. if ( nInsertion1 != nInsertion2 || !bFocus || !gos_GetMachineInformation( gos_Info_GetIMECaretStatus ) )
  437. return;
  438. if (bCursorVisible && !( dwStyleFlags & ES_EDITREADONLY ))
  439. {
  440. gos_VERTEX v[2];
  441. memset( v, 0, sizeof( gos_VERTEX ) * 2 );
  442. v[0].rhw = v[1].rhw = .5;
  443. v[0].argb = v[1].argb = cursorColor;
  444. int nCursorX = charXPos(nInsertion1);
  445. nCursorX -= nLeftOffset;
  446. v[0].y = globalY();
  447. v[1].y = globalY() + height();
  448. v[0].x = globalX() + nCursorX + ENTRY_MARGIN;
  449. v[1].x = v[0].x;
  450. gos_DrawLines( v, 2 );
  451. }
  452. }
  453. void aEdit:: hideCursor()
  454. {
  455. bCursorVisible=FALSE;
  456. }
  457. bool aEdit::handleFormattingKeys(int keycode)
  458. {
  459. int key = (keycode & 0xFF00)>>8;
  460. bool bExtendedKey = true;
  461. switch (key)
  462. {
  463. case KEY_LCONTROL:
  464. return true;
  465. break;
  466. case KEY_RETURN:
  467. {
  468. if ( gos_GetKeyStatus(KEY_RETURN) == KEY_PRESSED ) // don't want this called more than 1 time if held
  469. setFocus( false );
  470. return true;
  471. }
  472. break;
  473. case KEY_TAB:
  474. return true;
  475. break;
  476. case KEY_GRAVE: // if there is a selection range, bolt the text within it
  477. if (gos_GetKeyStatus(KEY_LMENU) != KEY_FREE && bAllowIME )
  478. {
  479. gos_ToggleIME( true );
  480. return true;
  481. }
  482. break;
  483. case KEY_HOME:
  484. if (gos_GetKeyStatus(KEY_LSHIFT) != KEY_FREE)
  485. nInsertion2 = 0;
  486. else
  487. nInsertion2 = nInsertion1=0;
  488. nLeftOffset = 0;
  489. bCursorVisible=TRUE;
  490. return true;
  491. case KEY_END:
  492. if (gos_GetKeyStatus(KEY_LSHIFT) != KEY_FREE)
  493. nInsertion2 = text.Length();
  494. else
  495. nInsertion2 = nInsertion1=text.Length();
  496. makeCursorVisible();
  497. bCursorVisible=TRUE;
  498. return true;
  499. case KEY_LEFT:
  500. if (gos_GetKeyStatus(KEY_LSHIFT) != KEY_FREE)
  501. {
  502. if (nInsertion2)
  503. {
  504. int decrementCount = 1;
  505. if ( nInsertion2 > 1 )
  506. {
  507. unsigned char* pPrev = _mbsdec( (const unsigned char*)(const char*)text, (const unsigned char*)(const char*)text + nInsertion2 );
  508. decrementCount = (const unsigned char*)(const char*)text + nInsertion2 - pPrev;
  509. }
  510. nInsertion2-= decrementCount;
  511. }
  512. }
  513. else
  514. {
  515. if (nInsertion1 != nInsertion2)
  516. nInsertion1 = nInsertion1<nInsertion2?nInsertion1:nInsertion2;
  517. else if (nInsertion1)
  518. {
  519. int decrementCount = 1;
  520. if ( nInsertion1 > 1 )
  521. {
  522. unsigned char* pPrev = _mbsdec( (const unsigned char*)(const char*)text, (const unsigned char*)(const char*)text + nInsertion2 );
  523. decrementCount = (const unsigned char*)(const char*)text + nInsertion2 - pPrev;
  524. }
  525. nInsertion1 -= decrementCount;
  526. }
  527. nInsertion2 = nInsertion1;
  528. }
  529. makeCursorVisible();
  530. bCursorVisible=TRUE;
  531. return true;
  532. case KEY_RIGHT:
  533. if (gos_GetKeyStatus(KEY_LSHIFT) != KEY_FREE)
  534. {
  535. nInsertion2 += charLength(nInsertion2);
  536. }
  537. else
  538. {
  539. if (nInsertion1 != nInsertion2)
  540. nInsertion1 = nInsertion1>nInsertion2?nInsertion1:nInsertion2;
  541. else if (nInsertion1<text.Length())
  542. nInsertion1 += charLength( nInsertion1 );
  543. nInsertion2 = nInsertion1;
  544. }
  545. makeCursorVisible();
  546. bCursorVisible=TRUE;
  547. return true;
  548. case KEY_DELETE:
  549. if (bExtendedKey) // must check this, the period key will get mixed up with this
  550. {
  551. if (!clearSelection())
  552. backSpace(nInsertion1+charLength(nInsertion1));
  553. makeCursorVisible();
  554. bCursorVisible=TRUE;
  555. return true;
  556. }
  557. break;
  558. case KEY_BACK:
  559. if (!clearSelection())
  560. backSpace(nInsertion1);
  561. flushCursorRight();
  562. makeCursorVisible();
  563. bCursorVisible=TRUE;
  564. return true;
  565. break;
  566. case KEY_UP:
  567. case KEY_DOWN:
  568. return true;
  569. break;
  570. case KEY_8:
  571. if ( !bWierdChars && userInput->getKeyDown( KEY_LSHIFT ) )
  572. return true;
  573. break;
  574. // case \*/?:<>|
  575. }
  576. return false;
  577. }
  578. int aEdit::charXPos(int nCharIndex)
  579. {
  580. if (nCharIndex > text.Length() || !text.Length())
  581. return 0;
  582. int nXPos = 0;
  583. if ( nCharIndex != text.Length() )
  584. {
  585. int oldChar = text[nCharIndex];
  586. text[nCharIndex] = 0;
  587. nXPos = font.width( text );
  588. text[nCharIndex] = oldChar;
  589. }
  590. else
  591. {
  592. nXPos = font.width( text );
  593. }
  594. return nXPos;
  595. }
  596. void aEdit::makeCursorVisible()
  597. {
  598. int nXPos = charXPos(nInsertion2);
  599. if (nXPos < nLeftOffset)
  600. nLeftOffset = nXPos;
  601. if (nXPos > nLeftOffset + width() - (ENTRY_MARGIN*2.f))
  602. nLeftOffset = nXPos - (int)(width() - (ENTRY_MARGIN*2.f));
  603. }
  604. void aEdit::flushCursorRight()
  605. {
  606. int nXPos = charXPos(nInsertion2);
  607. if (nXPos < width() - ENTRY_MARGIN*2)
  608. nLeftOffset = 0;
  609. else
  610. nLeftOffset = nXPos - (int)(width()- ENTRY_MARGIN*2.f);
  611. }
  612. int aEdit::findChar(int nXPos)
  613. {
  614. int nLastPos,nNextPos,n;
  615. nNextPos = nLastPos = 0;
  616. n=0;
  617. unsigned char* pBegin = (unsigned char*)&text[0];
  618. unsigned char* pCur = pBegin;
  619. unsigned char* pNext = pBegin;
  620. if ( text.Length() )
  621. {
  622. do
  623. {
  624. pNext = _mbsinc( pCur );
  625. unsigned char tmp = *pNext;
  626. *pNext = NULL;
  627. nLastPos = nNextPos;
  628. nNextPos = font.width(text);
  629. *pNext = tmp;
  630. if ( nXPos > nNextPos )
  631. pCur = pNext;
  632. } while ( *pCur && nXPos > nNextPos);
  633. }
  634. if ( nXPos - nLastPos < nNextPos - nXPos )
  635. n = pCur - pBegin;
  636. else
  637. n = pNext - pBegin;
  638. if (n>text.Length())
  639. n=text.Length();
  640. return n;
  641. }
  642. void aEdit::init( FitIniFile* file, const char* header )
  643. {
  644. int result = file->seekBlock( header );
  645. if ( result != NO_ERR )
  646. {
  647. char errorStr[256];
  648. sprintf( errorStr, "couldn't find the text block%s", header );
  649. Assert( result == NO_ERR, 0, errorStr );
  650. return;
  651. }
  652. long left, top, width, height;
  653. file->readIdLong( "XLocation", left );
  654. file->readIdLong( "YLocation", top );
  655. file->readIdLong( "Width", width );
  656. file->readIdLong( "Height", height );
  657. aObject::init( left, top, width, height );
  658. file->readIdLong( "Color", textColor );
  659. long lfont;
  660. file->readIdLong( "Font", lfont );
  661. font.init( lfont );
  662. file->readIdLong( "CursorColor", cursorColor );
  663. file->readIdLong( "HighlightColor", highlightColor );
  664. file->readIdLong( "SelectedColor", selectedColor );
  665. file->readIdLong( "HelpCaption", helpHeader );
  666. file->readIdLong( "HelpDesc", helpID );
  667. }
  668. int aEdit::charLength( int index )
  669. {
  670. if ( index < 0 )
  671. return 0;
  672. if ( index > text.Length() )
  673. return 0;
  674. char tmp = text[index];
  675. if ( isleadbyte( tmp ) )
  676. return 2;
  677. return 1;
  678. }
  679. void aEdit::setFont( long resID )
  680. {
  681. font.init( resID );
  682. }
  683. //*************************************************************************************************
  684. // end of file ( aEdit.cpp )