guiConsoleEditCtrl.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "graphics/dgl.h"
  25. #include "gui/guiCanvas.h"
  26. #include "gui/guiConsoleEditCtrl.h"
  27. #include "memory/frameAllocator.h"
  28. IMPLEMENT_CONOBJECT(GuiConsoleEditCtrl);
  29. GuiConsoleEditCtrl::GuiConsoleEditCtrl()
  30. {
  31. mSinkAllKeyEvents = true;
  32. mSiblingScroller = NULL;
  33. mUseSiblingScroller = true;
  34. mHistorySize = 40;
  35. mReturnCausesTab = false;
  36. }
  37. void GuiConsoleEditCtrl::initPersistFields()
  38. {
  39. Parent::initPersistFields();
  40. addGroup("GuiConsoleEditCtrl");
  41. addField("useSiblingScroller", TypeBool, Offset(mUseSiblingScroller, GuiConsoleEditCtrl));
  42. endGroup("GuiConsoleEditCtrl");
  43. }
  44. bool GuiConsoleEditCtrl::onKeyDown(const GuiEvent &event)
  45. {
  46. setUpdate();
  47. if (event.keyCode == KEY_TAB)
  48. {
  49. // Get a buffer that can hold the completed text...
  50. FrameTemp<UTF8> tmpBuff(GuiTextEditCtrl::MAX_STRING_LENGTH);
  51. // And copy the text to be completed into it.
  52. mTextBuffer.getCopy8(tmpBuff, GuiTextEditCtrl::MAX_STRING_LENGTH);
  53. // perform the completion
  54. bool forward = event.modifier & SI_SHIFT;
  55. mCursorPos = Con::tabComplete(tmpBuff, mCursorPos, GuiTextEditCtrl::MAX_STRING_LENGTH, forward);
  56. // place results in our buffer.
  57. mTextBuffer.set(tmpBuff);
  58. return true;
  59. }
  60. else if ((event.keyCode == KEY_PAGE_UP) || (event.keyCode == KEY_PAGE_DOWN))
  61. {
  62. // See if there's some other widget that can scroll the console history.
  63. if (mUseSiblingScroller)
  64. {
  65. if (mSiblingScroller)
  66. {
  67. return mSiblingScroller->onKeyDown(event);
  68. }
  69. else
  70. {
  71. // Let's see if we can find it...
  72. SimGroup* pGroup = getGroup();
  73. if (pGroup)
  74. {
  75. // Find the first scroll control in the same group as us.
  76. for (SimSetIterator itr(pGroup); *itr; ++itr)
  77. {
  78. if ((mSiblingScroller = dynamic_cast<GuiScrollCtrl*>(*itr)))
  79. {
  80. return mSiblingScroller->onKeyDown(event);
  81. }
  82. }
  83. }
  84. // No luck... so don't try, next time.
  85. mUseSiblingScroller = false;
  86. }
  87. }
  88. }
  89. return Parent::onKeyDown(event);
  90. }