guiConsoleEditCtrl.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "console/consoleTypes.h"
  23. #include "console/console.h"
  24. #include "gui/core/guiCanvas.h"
  25. #include "gui/controls/guiConsoleEditCtrl.h"
  26. #include "core/frameAllocator.h"
  27. IMPLEMENT_CONOBJECT(GuiConsoleEditCtrl);
  28. ConsoleDocClass( GuiConsoleEditCtrl,
  29. "@brief Text entry element of a GuiConsole.\n\n"
  30. "@tsexample\n"
  31. "new GuiConsoleEditCtrl(ConsoleEntry)\n"
  32. "{\n"
  33. " profile = \"ConsoleTextEditProfile\";\n"
  34. " horizSizing = \"width\";\n"
  35. " vertSizing = \"top\";\n"
  36. " position = \"0 462\";\n"
  37. " extent = \"640 18\";\n"
  38. " minExtent = \"8 8\";\n"
  39. " visible = \"1\";\n"
  40. " altCommand = \"ConsoleEntry::eval();\";\n"
  41. " helpTag = \"0\";\n"
  42. " maxLength = \"255\";\n"
  43. " historySize = \"40\";\n"
  44. " password = \"0\";\n"
  45. " tabComplete = \"0\";\n"
  46. " sinkAllKeyEvents = \"1\";\n"
  47. " useSiblingScroller = \"1\";\n"
  48. "};\n"
  49. "@endtsexample\n\n"
  50. "@ingroup GuiCore"
  51. );
  52. GuiConsoleEditCtrl::GuiConsoleEditCtrl()
  53. {
  54. mSinkAllKeyEvents = true;
  55. mSiblingScroller = NULL;
  56. mUseSiblingScroller = true;
  57. }
  58. void GuiConsoleEditCtrl::initPersistFields()
  59. {
  60. docsURL;
  61. addGroup("GuiConsoleEditCtrl");
  62. addField("useSiblingScroller", TypeBool, Offset(mUseSiblingScroller, GuiConsoleEditCtrl));
  63. endGroup("GuiConsoleEditCtrl");
  64. Parent::initPersistFields();
  65. }
  66. bool GuiConsoleEditCtrl::onKeyDown(const GuiEvent &event)
  67. {
  68. setUpdate();
  69. if (event.keyCode == KEY_TAB)
  70. {
  71. // Get a buffer that can hold the completed text...
  72. FrameTemp<UTF8> tmpBuff(GuiTextCtrl::MAX_STRING_LENGTH);
  73. // And copy the text to be completed into it.
  74. mTextBuffer.getCopy8(tmpBuff, GuiTextCtrl::MAX_STRING_LENGTH);
  75. // perform the completion
  76. bool forward = (event.modifier & SI_SHIFT) == 0;
  77. mCursorPos = Con::tabComplete(tmpBuff, mCursorPos, GuiTextCtrl::MAX_STRING_LENGTH, forward);
  78. // place results in our buffer.
  79. mTextBuffer.set(tmpBuff);
  80. return true;
  81. }
  82. else if ((event.keyCode == KEY_PAGE_UP) || (event.keyCode == KEY_PAGE_DOWN))
  83. {
  84. // See if there's some other widget that can scroll the console history.
  85. if (mUseSiblingScroller)
  86. {
  87. if (mSiblingScroller)
  88. {
  89. return mSiblingScroller->onKeyDown(event);
  90. }
  91. else
  92. {
  93. // Let's see if we can find it...
  94. SimGroup* pGroup = getGroup();
  95. if (pGroup)
  96. {
  97. // Find the first scroll control in the same group as us.
  98. for (SimSetIterator itr(pGroup); *itr; ++itr)
  99. {
  100. mSiblingScroller = dynamic_cast<GuiScrollCtrl*>(*itr);
  101. if (mSiblingScroller != NULL)
  102. {
  103. return mSiblingScroller->onKeyDown(event);
  104. }
  105. }
  106. }
  107. // No luck... so don't try, next time.
  108. mUseSiblingScroller = false;
  109. }
  110. }
  111. }
  112. else if( event.keyCode == KEY_RETURN || event.keyCode == KEY_NUMPADENTER )
  113. {
  114. if ( event.modifier & SI_SHIFT &&
  115. mTextBuffer.length() + dStrlen("echo();") <= GuiTextCtrl::MAX_STRING_LENGTH )
  116. {
  117. // Wrap the text with echo( %s );
  118. char buf[GuiTextCtrl::MAX_STRING_LENGTH];
  119. getText( buf );
  120. String text( buf );
  121. text.replace( ";", "" );
  122. text = String::ToString( "echo(%s);", text.c_str() );
  123. setText( text.utf8() );
  124. }
  125. return Parent::dealWithEnter(false);
  126. }
  127. return Parent::onKeyDown(event);
  128. }