2
0

guiConsoleEditCtrl.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. addGroup("GuiConsoleEditCtrl");
  61. addField("useSiblingScroller", TypeBool, Offset(mUseSiblingScroller, GuiConsoleEditCtrl));
  62. endGroup("GuiConsoleEditCtrl");
  63. Parent::initPersistFields();
  64. }
  65. bool GuiConsoleEditCtrl::onKeyDown(const GuiEvent &event)
  66. {
  67. setUpdate();
  68. if (event.keyCode == KEY_TAB)
  69. {
  70. // Get a buffer that can hold the completed text...
  71. FrameTemp<UTF8> tmpBuff(GuiTextCtrl::MAX_STRING_LENGTH);
  72. // And copy the text to be completed into it.
  73. mTextBuffer.getCopy8(tmpBuff, GuiTextCtrl::MAX_STRING_LENGTH);
  74. // perform the completion
  75. bool forward = (event.modifier & SI_SHIFT) == 0;
  76. mCursorPos = Con::tabComplete(tmpBuff, mCursorPos, GuiTextCtrl::MAX_STRING_LENGTH, forward);
  77. // place results in our buffer.
  78. mTextBuffer.set(tmpBuff);
  79. return true;
  80. }
  81. else if ((event.keyCode == KEY_PAGE_UP) || (event.keyCode == KEY_PAGE_DOWN))
  82. {
  83. // See if there's some other widget that can scroll the console history.
  84. if (mUseSiblingScroller)
  85. {
  86. if (mSiblingScroller)
  87. {
  88. return mSiblingScroller->onKeyDown(event);
  89. }
  90. else
  91. {
  92. // Let's see if we can find it...
  93. SimGroup* pGroup = getGroup();
  94. if (pGroup)
  95. {
  96. // Find the first scroll control in the same group as us.
  97. for (SimSetIterator itr(pGroup); *itr; ++itr)
  98. {
  99. mSiblingScroller = dynamic_cast<GuiScrollCtrl*>(*itr);
  100. if (mSiblingScroller != NULL)
  101. {
  102. return mSiblingScroller->onKeyDown(event);
  103. }
  104. }
  105. }
  106. // No luck... so don't try, next time.
  107. mUseSiblingScroller = false;
  108. }
  109. }
  110. }
  111. else if( event.keyCode == KEY_RETURN || event.keyCode == KEY_NUMPADENTER )
  112. {
  113. if ( event.modifier & SI_SHIFT &&
  114. mTextBuffer.length() + dStrlen("echo();") <= GuiTextCtrl::MAX_STRING_LENGTH )
  115. {
  116. // Wrap the text with echo( %s );
  117. char buf[GuiTextCtrl::MAX_STRING_LENGTH];
  118. getText( buf );
  119. String text( buf );
  120. text.replace( ";", "" );
  121. text = String::ToString( "echo(%s);", text.c_str() );
  122. setText( text );
  123. }
  124. return Parent::dealWithEnter(false);
  125. }
  126. return Parent::onKeyDown(event);
  127. }