EditWindow.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "DeviceContext.h"
  23. #include "Window.h"
  24. #include "UserInterfaceLocal.h"
  25. #include "SliderWindow.h"
  26. #include "EditWindow.h"
  27. bool idEditWindow::ParseInternalVar( const char *_name, idParser *src ) {
  28. if ( idStr::Icmp( _name, "maxchars" ) == 0) {
  29. maxChars = src->ParseInt();
  30. return true;
  31. }
  32. if ( idStr::Icmp( _name, "numeric" ) == 0) {
  33. numeric = src->ParseBool();
  34. return true;
  35. }
  36. if ( idStr::Icmp( _name, "wrap" ) == 0) {
  37. wrap = src->ParseBool();
  38. return true;
  39. }
  40. if ( idStr::Icmp( _name, "readonly" ) == 0) {
  41. readonly = src->ParseBool();
  42. return true;
  43. }
  44. if ( idStr::Icmp( _name, "forceScroll" ) == 0) {
  45. forceScroll = src->ParseBool();
  46. return true;
  47. }
  48. if ( idStr::Icmp( _name, "source" ) == 0) {
  49. ParseString( src, sourceFile );
  50. return true;
  51. }
  52. if ( idStr::Icmp( _name, "password" ) == 0 ) {
  53. password = src->ParseBool();
  54. return true;
  55. }
  56. if ( idStr::Icmp( _name, "cvarMax" ) == 0) {
  57. cvarMax = src->ParseInt();
  58. return true;
  59. }
  60. return idWindow::ParseInternalVar( _name, src );
  61. }
  62. idWinVar *idEditWindow::GetWinVarByName( const char *_name, bool fixup, drawWin_t** owner ) {
  63. if ( idStr::Icmp( _name, "cvar" ) == 0 ) {
  64. return &cvarStr;
  65. }
  66. if ( idStr::Icmp( _name, "password" ) == 0 ) {
  67. return &password;
  68. }
  69. if ( idStr::Icmp( _name, "liveUpdate" ) == 0 ) {
  70. return &liveUpdate;
  71. }
  72. if ( idStr::Icmp( _name, "cvarGroup" ) == 0 ) {
  73. return &cvarGroup;
  74. }
  75. return idWindow::GetWinVarByName( _name, fixup, owner );
  76. }
  77. void idEditWindow::CommonInit() {
  78. maxChars = 128;
  79. numeric = false;
  80. paintOffset = 0;
  81. cursorPos = 0;
  82. cursorLine = 0;
  83. cvarMax = 0;
  84. wrap = false;
  85. sourceFile = "";
  86. scroller = NULL;
  87. sizeBias = 0;
  88. lastTextLength = 0;
  89. forceScroll = false;
  90. password = false;
  91. cvar = NULL;
  92. liveUpdate = true;
  93. readonly = false;
  94. scroller = new idSliderWindow(dc, gui);
  95. }
  96. idEditWindow::idEditWindow( idDeviceContext *d, idUserInterfaceLocal *g ) : idWindow(d, g) {
  97. dc = d;
  98. gui = g;
  99. CommonInit();
  100. }
  101. idEditWindow::idEditWindow( idUserInterfaceLocal *g ) : idWindow(g) {
  102. gui = g;
  103. CommonInit();
  104. }
  105. idEditWindow::~idEditWindow() {
  106. }
  107. void idEditWindow::GainFocus() {
  108. cursorPos = text.Length();
  109. EnsureCursorVisible();
  110. }
  111. void idEditWindow::Draw( int time, float x, float y ) {
  112. idVec4 color = foreColor;
  113. UpdateCvar( true );
  114. int len = text.Length();
  115. if ( len != lastTextLength ) {
  116. scroller->SetValue( 0.0f );
  117. EnsureCursorVisible();
  118. lastTextLength = len;
  119. }
  120. float scale = textScale;
  121. idStr pass;
  122. const char* buffer;
  123. if ( password ) {
  124. const char* temp = text;
  125. for ( ; *temp; temp++ ) {
  126. pass += "*";
  127. }
  128. buffer = pass;
  129. } else {
  130. buffer = text;
  131. }
  132. if ( cursorPos > len ) {
  133. cursorPos = len;
  134. }
  135. idRectangle rect = textRect;
  136. rect.x -= paintOffset;
  137. rect.w += paintOffset;
  138. if ( wrap && scroller->GetHigh() > 0.0f ) {
  139. float lineHeight = GetMaxCharHeight( ) + 5;
  140. rect.y -= scroller->GetValue() * lineHeight;
  141. rect.w -= sizeBias;
  142. rect.h = ( breaks.Num() + 1 ) * lineHeight;
  143. }
  144. if ( hover && !noEvents && Contains(gui->CursorX(), gui->CursorY()) ) {
  145. color = hoverColor;
  146. } else {
  147. hover = false;
  148. }
  149. if ( flags & WIN_FOCUS ) {
  150. color = hoverColor;
  151. }
  152. dc->DrawText( buffer, scale, 0, color, rect, wrap, (flags & WIN_FOCUS) ? cursorPos : -1);
  153. }
  154. /*
  155. =============
  156. idEditWindow::HandleEvent
  157. =============
  158. */
  159. const char *idEditWindow::HandleEvent(const sysEvent_t *event, bool *updateVisuals) {
  160. static char buffer[ MAX_EDITFIELD ];
  161. const char *ret = "";
  162. if ( wrap ) {
  163. // need to call this to allow proper focus and capturing on embedded children
  164. ret = idWindow::HandleEvent( event, updateVisuals );
  165. if ( ret && *ret ) {
  166. return ret;
  167. }
  168. }
  169. if ( ( event->evType != SE_CHAR && event->evType != SE_KEY ) ) {
  170. return ret;
  171. }
  172. idStr::Copynz( buffer, text.c_str(), sizeof( buffer ) );
  173. int key = event->evValue;
  174. int len = text.Length();
  175. if ( event->evType == SE_CHAR ) {
  176. if ( event->evValue == Sys_GetConsoleKey( false ) || event->evValue == Sys_GetConsoleKey( true ) ) {
  177. return "";
  178. }
  179. if ( updateVisuals ) {
  180. *updateVisuals = true;
  181. }
  182. if ( maxChars && len > maxChars ) {
  183. len = maxChars;
  184. }
  185. if ( ( key == K_ENTER || key == K_KP_ENTER ) && event->evValue2 ) {
  186. RunScript( ON_ACTION );
  187. RunScript( ON_ENTER );
  188. return cmd;
  189. }
  190. if ( key == K_ESCAPE ) {
  191. RunScript( ON_ESC );
  192. return cmd;
  193. }
  194. if ( readonly ) {
  195. return "";
  196. }
  197. if ( key == 'h' - 'a' + 1 || key == K_BACKSPACE ) { // ctrl-h is backspace
  198. if ( cursorPos > 0 ) {
  199. if ( cursorPos >= len ) {
  200. buffer[len - 1] = 0;
  201. cursorPos = len - 1;
  202. } else {
  203. memmove( &buffer[ cursorPos - 1 ], &buffer[ cursorPos ], len + 1 - cursorPos);
  204. cursorPos--;
  205. }
  206. text = buffer;
  207. UpdateCvar( false );
  208. RunScript( ON_ACTION );
  209. }
  210. return "";
  211. }
  212. //
  213. // ignore any non printable chars (except enter when wrap is enabled)
  214. //
  215. if ( wrap && (key == K_ENTER || key == K_KP_ENTER) ) {
  216. } else if ( !idStr::CharIsPrintable( key ) ) {
  217. return "";
  218. }
  219. if ( numeric ) {
  220. if ( ( key < '0' || key > '9' ) && key != '.' ) {
  221. return "";
  222. }
  223. }
  224. if ( dc->GetOverStrike() ) {
  225. if ( maxChars && cursorPos >= maxChars ) {
  226. return "";
  227. }
  228. } else {
  229. if ( ( len == MAX_EDITFIELD - 1 ) || ( maxChars && len >= maxChars ) ) {
  230. return "";
  231. }
  232. memmove( &buffer[ cursorPos + 1 ], &buffer[ cursorPos ], len + 1 - cursorPos );
  233. }
  234. buffer[ cursorPos ] = key;
  235. text = buffer;
  236. UpdateCvar( false );
  237. RunScript( ON_ACTION );
  238. if ( cursorPos < len + 1 ) {
  239. cursorPos++;
  240. }
  241. EnsureCursorVisible();
  242. } else if ( event->evType == SE_KEY && event->evValue2 ) {
  243. if ( updateVisuals ) {
  244. *updateVisuals = true;
  245. }
  246. if ( key == K_DEL ) {
  247. if ( readonly ) {
  248. return ret;
  249. }
  250. if ( cursorPos < len ) {
  251. memmove( &buffer[cursorPos], &buffer[cursorPos + 1], len - cursorPos);
  252. text = buffer;
  253. UpdateCvar( false );
  254. RunScript( ON_ACTION );
  255. }
  256. return ret;
  257. }
  258. if ( key == K_RIGHTARROW ) {
  259. if ( cursorPos < len ) {
  260. if ( idKeyInput::IsDown( K_CTRL ) ) {
  261. // skip to next word
  262. while( ( cursorPos < len ) && ( buffer[ cursorPos ] != ' ' ) ) {
  263. cursorPos++;
  264. }
  265. while( ( cursorPos < len ) && ( buffer[ cursorPos ] == ' ' ) ) {
  266. cursorPos++;
  267. }
  268. } else {
  269. if ( cursorPos < len ) {
  270. cursorPos++;
  271. }
  272. }
  273. }
  274. EnsureCursorVisible();
  275. return ret;
  276. }
  277. if ( key == K_LEFTARROW ) {
  278. if ( idKeyInput::IsDown( K_CTRL ) ) {
  279. // skip to previous word
  280. while( ( cursorPos > 0 ) && ( buffer[ cursorPos - 1 ] == ' ' ) ) {
  281. cursorPos--;
  282. }
  283. while( ( cursorPos > 0 ) && ( buffer[ cursorPos - 1 ] != ' ' ) ) {
  284. cursorPos--;
  285. }
  286. } else {
  287. if ( cursorPos > 0 ) {
  288. cursorPos--;
  289. }
  290. }
  291. EnsureCursorVisible();
  292. return ret;
  293. }
  294. if ( key == K_HOME ) {
  295. if ( idKeyInput::IsDown( K_CTRL ) || cursorLine <= 0 || ( cursorLine >= breaks.Num() ) ) {
  296. cursorPos = 0;
  297. } else {
  298. cursorPos = breaks[cursorLine];
  299. }
  300. EnsureCursorVisible();
  301. return ret;
  302. }
  303. if ( key == K_END ) {
  304. if ( idKeyInput::IsDown( K_CTRL ) || (cursorLine < -1) || ( cursorLine >= breaks.Num() - 1 ) ) {
  305. cursorPos = len;
  306. } else {
  307. cursorPos = breaks[cursorLine + 1] - 1;
  308. }
  309. EnsureCursorVisible();
  310. return ret;
  311. }
  312. if ( key == K_INS ) {
  313. if ( !readonly ) {
  314. dc->SetOverStrike( !dc->GetOverStrike() );
  315. }
  316. return ret;
  317. }
  318. if ( key == K_DOWNARROW ) {
  319. if ( idKeyInput::IsDown( K_CTRL ) ) {
  320. scroller->SetValue( scroller->GetValue() + 1.0f );
  321. } else {
  322. if ( cursorLine < breaks.Num() - 1 ) {
  323. int offset = cursorPos - breaks[cursorLine];
  324. cursorPos = breaks[cursorLine + 1] + offset;
  325. EnsureCursorVisible();
  326. }
  327. }
  328. }
  329. if (key == K_UPARROW ) {
  330. if ( idKeyInput::IsDown( K_CTRL ) ) {
  331. scroller->SetValue( scroller->GetValue() - 1.0f );
  332. } else {
  333. if ( cursorLine > 0 ) {
  334. int offset = cursorPos - breaks[cursorLine];
  335. cursorPos = breaks[cursorLine - 1] + offset;
  336. EnsureCursorVisible();
  337. }
  338. }
  339. }
  340. if ( key == K_ENTER || key == K_KP_ENTER ) {
  341. RunScript( ON_ACTION );
  342. RunScript( ON_ENTER );
  343. return cmd;
  344. }
  345. if ( key == K_ESCAPE ) {
  346. RunScript( ON_ESC );
  347. return cmd;
  348. }
  349. } else if ( event->evType == SE_KEY && !event->evValue2 ) {
  350. if ( key == K_ENTER || key == K_KP_ENTER ) {
  351. RunScript( ON_ENTERRELEASE );
  352. return cmd;
  353. } else {
  354. RunScript( ON_ACTIONRELEASE );
  355. }
  356. }
  357. return ret;
  358. }
  359. void idEditWindow::PostParse() {
  360. idWindow::PostParse();
  361. if ( maxChars == 0 ) {
  362. maxChars = 10;
  363. }
  364. if ( sourceFile.Length() ) {
  365. void *buffer;
  366. fileSystem->ReadFile( sourceFile, &buffer );
  367. text = (char *) buffer;
  368. fileSystem->FreeFile( buffer );
  369. }
  370. InitCvar();
  371. InitScroller(false);
  372. EnsureCursorVisible();
  373. flags |= WIN_CANFOCUS;
  374. }
  375. /*
  376. ================
  377. idEditWindow::InitScroller
  378. This is the same as in idListWindow
  379. ================
  380. */
  381. void idEditWindow::InitScroller( bool horizontal )
  382. {
  383. const char *thumbImage = "guis/assets/scrollbar_thumb.tga";
  384. const char *barImage = "guis/assets/scrollbarv.tga";
  385. const char *scrollerName = "_scrollerWinV";
  386. if (horizontal) {
  387. barImage = "guis/assets/scrollbarh.tga";
  388. scrollerName = "_scrollerWinH";
  389. }
  390. const idMaterial *mat = declManager->FindMaterial( barImage );
  391. mat->SetSort( SS_GUI );
  392. sizeBias = mat->GetImageWidth();
  393. idRectangle scrollRect;
  394. if (horizontal) {
  395. sizeBias = mat->GetImageHeight();
  396. scrollRect.x = 0;
  397. scrollRect.y = (clientRect.h - sizeBias);
  398. scrollRect.w = clientRect.w;
  399. scrollRect.h = sizeBias;
  400. } else {
  401. scrollRect.x = (clientRect.w - sizeBias);
  402. scrollRect.y = 0;
  403. scrollRect.w = sizeBias;
  404. scrollRect.h = clientRect.h;
  405. }
  406. scroller->InitWithDefaults(scrollerName, scrollRect, foreColor, matColor, mat->GetName(), thumbImage, !horizontal, true);
  407. InsertChild(scroller, NULL);
  408. scroller->SetBuddy(this);
  409. }
  410. void idEditWindow::HandleBuddyUpdate( idWindow *buddy ) {
  411. }
  412. void idEditWindow::EnsureCursorVisible()
  413. {
  414. if ( readonly ) {
  415. cursorPos = -1;
  416. } else if ( maxChars == 1 ) {
  417. cursorPos = 0;
  418. }
  419. if ( !dc ) {
  420. return;
  421. }
  422. SetFont();
  423. if ( !wrap ) {
  424. int cursorX = 0;
  425. if ( password ) {
  426. cursorX = cursorPos * dc->CharWidth( '*', textScale );
  427. } else {
  428. int i = 0;
  429. while ( i < text.Length() && i < cursorPos ) {
  430. if ( idStr::IsColor( &text[i] ) ) {
  431. i += 2;
  432. } else {
  433. cursorX += dc->CharWidth( text[i], textScale );
  434. i++;
  435. }
  436. }
  437. }
  438. int maxWidth = GetMaxCharWidth( );
  439. int left = cursorX - maxWidth;
  440. int right = ( cursorX - textRect.w ) + maxWidth;
  441. if ( paintOffset > left ) {
  442. // When we go past the left side, we want the text to jump 6 characters
  443. paintOffset = left - maxWidth * 6;
  444. }
  445. if ( paintOffset < right) {
  446. paintOffset = right;
  447. }
  448. if ( paintOffset < 0 ) {
  449. paintOffset = 0;
  450. }
  451. scroller->SetRange(0.0f, 0.0f, 1.0f);
  452. } else {
  453. // Word wrap
  454. breaks.Clear();
  455. idRectangle rect = textRect;
  456. rect.w -= sizeBias;
  457. dc->DrawText(text, textScale, textAlign, colorWhite, rect, true, (flags & WIN_FOCUS) ? cursorPos : -1, true, &breaks );
  458. int fit = textRect.h / (GetMaxCharHeight() + 5);
  459. if ( fit < breaks.Num() + 1 ) {
  460. scroller->SetRange(0, breaks.Num() + 1 - fit, 1);
  461. } else {
  462. // The text fits completely in the box
  463. scroller->SetRange(0.0f, 0.0f, 1.0f);
  464. }
  465. if ( forceScroll ) {
  466. scroller->SetValue( breaks.Num() - fit );
  467. } else if ( readonly ) {
  468. } else {
  469. cursorLine = 0;
  470. for ( int i = 1; i < breaks.Num(); i++ ) {
  471. if ( cursorPos >= breaks[i] ) {
  472. cursorLine = i;
  473. } else {
  474. break;
  475. }
  476. }
  477. int topLine = idMath::FtoiFast( scroller->GetValue() );
  478. if ( cursorLine < topLine ) {
  479. scroller->SetValue( cursorLine );
  480. } else if ( cursorLine >= topLine + fit) {
  481. scroller->SetValue( ( cursorLine - fit ) + 1 );
  482. }
  483. }
  484. }
  485. }
  486. void idEditWindow::Activate(bool activate, idStr &act) {
  487. idWindow::Activate(activate, act);
  488. if ( activate ) {
  489. UpdateCvar( true, true );
  490. EnsureCursorVisible();
  491. }
  492. }
  493. /*
  494. ============
  495. idEditWindow::InitCvar
  496. ============
  497. */
  498. void idEditWindow::InitCvar( ) {
  499. if ( cvarStr[0] == '\0' ) {
  500. if ( text.GetName() == NULL ) {
  501. common->Warning( "idEditWindow::InitCvar: gui '%s' window '%s' has an empty cvar string", gui->GetSourceFile(), name.c_str() );
  502. }
  503. cvar = NULL;
  504. return;
  505. }
  506. cvar = cvarSystem->Find( cvarStr );
  507. if ( !cvar ) {
  508. common->Warning( "idEditWindow::InitCvar: gui '%s' window '%s' references undefined cvar '%s'", gui->GetSourceFile(), name.c_str(), cvarStr.c_str() );
  509. return;
  510. }
  511. }
  512. /*
  513. ============
  514. idEditWindow::UpdateCvar
  515. ============
  516. */
  517. void idEditWindow::UpdateCvar( bool read, bool force ) {
  518. if ( force || liveUpdate ) {
  519. if ( cvar ) {
  520. if ( read ) {
  521. text = cvar->GetString();
  522. } else {
  523. cvar->SetString( text );
  524. if ( cvarMax && ( cvar->GetInteger() > cvarMax ) ) {
  525. cvar->SetInteger( cvarMax );
  526. }
  527. }
  528. }
  529. }
  530. }
  531. /*
  532. ============
  533. idEditWindow::RunNamedEvent
  534. ============
  535. */
  536. void idEditWindow::RunNamedEvent( const char* eventName ) {
  537. idStr event, group;
  538. if ( !idStr::Cmpn( eventName, "cvar read ", 10 ) ) {
  539. event = eventName;
  540. group = event.Mid( 10, event.Length() - 10 );
  541. if ( !group.Cmp( cvarGroup ) ) {
  542. UpdateCvar( true, true );
  543. }
  544. } else if ( !idStr::Cmpn( eventName, "cvar write ", 11 ) ) {
  545. event = eventName;
  546. group = event.Mid( 11, event.Length() - 11 );
  547. if ( !group.Cmp( cvarGroup ) ) {
  548. UpdateCvar( false, true );
  549. }
  550. }
  551. }