1
0

historyeditbox.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include <precomp.h>
  2. #include "historyeditbox.h"
  3. XMLParamPair HistoryEditBox::params[] = {
  4. {HISTORYEDITBOX_SETNAVBUTTONS, L"NAVBUTTONS"}, // param is implemented by script
  5. };
  6. HistoryEditBox::HistoryEditBox() {
  7. history_pos = 0;
  8. xuihandle = newXuiHandle();
  9. CreateXMLParameters(xuihandle);
  10. setXmlParam(L"navbuttons", L"1"); // so we need to set a default value in the xml param list
  11. }
  12. void HistoryEditBox::CreateXMLParameters(int master_handle)
  13. {
  14. //HISTORYEDITBOX_PARENT::CreateXMLParameters(master_handle);
  15. int numParams = sizeof(params) / sizeof(params[0]);
  16. hintNumberOfParams(xuihandle, numParams);
  17. for (int i = 0;i < numParams;i++)
  18. addParam(xuihandle, params[i], XUI_ATTRIBUTE_IMPLIED);
  19. }
  20. HistoryEditBox::~HistoryEditBox() {
  21. history.deleteAll();
  22. }
  23. void HistoryEditBox::history_forward() {
  24. if (history_pos > 0 && !isListOpen()) {
  25. history_pos--;
  26. if (history_pos > 0)
  27. setText(history.enumItem(history.getNumItems()-history_pos)->getValue(), 1);
  28. }
  29. }
  30. void HistoryEditBox::history_back() {
  31. if (!isListOpen() && history_pos < history.getNumItems()) {
  32. history_pos++;
  33. setText(history.enumItem(history.getNumItems()-history_pos)->getValue(), 1);
  34. }
  35. }
  36. void HistoryEditBox::onEditKeyDown(int vk) {
  37. HISTORYEDITBOX_PARENT::onEditKeyDown(vk);
  38. if (Std::keyDown(VK_CONTROL)) return;
  39. if (vk == VK_DOWN) {
  40. history_forward();
  41. } else if (vk == VK_UP) {
  42. history_back();
  43. }
  44. }
  45. int HistoryEditBox::onInit() {
  46. int r = HISTORYEDITBOX_PARENT::onInit();
  47. #ifdef WASABI_COMPILE_CONFIG
  48. loadHistory();
  49. #endif
  50. return r;
  51. }
  52. void HistoryEditBox::dropdownlist_onCloseList() {
  53. HISTORYEDITBOX_PARENT::dropdownlist_onCloseList();
  54. history_pos = 0;
  55. }
  56. void HistoryEditBox::onPreOpenList()
  57. {
  58. HISTORYEDITBOX_PARENT::onPreOpenList();
  59. addHistory(getText());
  60. }
  61. void HistoryEditBox::onEditEnter(const wchar_t *txt)
  62. {
  63. HISTORYEDITBOX_PARENT::onEditEnter(txt);
  64. if (Std::keyDown(VK_CONTROL)) return;
  65. addHistory(txt);
  66. }
  67. void HistoryEditBox::addHistory(const wchar_t *txt)
  68. {
  69. HISTORYEDITBOX_PARENT::onEditEnter(txt);
  70. history_pos = 0;
  71. if (!txt || !*txt) return;
  72. // yay multi-instances on unique history
  73. #ifdef WASABI_COMPILE_CONFIG
  74. loadHistory(0);
  75. #endif
  76. foreach(history)
  77. StringW *s = history.getfor();
  78. if (!_wcsicmp(s->getValue(), txt)) {
  79. delete s;
  80. history.removeByPos(foreach_index);
  81. break;
  82. }
  83. endfor;
  84. history.addItem(new StringW(txt));
  85. while (history.getNumItems() > 64)
  86. {
  87. StringW *s = history.enumItem(0);
  88. delete s;
  89. history.removeByPos(1);
  90. }
  91. #ifdef WASABI_COMPILE_CONFIG
  92. saveHistory();
  93. loadHistory(1);
  94. #endif
  95. }
  96. #ifdef WASABI_COMPILE_CONFIG
  97. void HistoryEditBox::loadHistory(int refill) {
  98. history.deleteAll();
  99. wchar_t d[256] = {0};
  100. wchar_t c[WA_MAX_PATH] = {0};
  101. int i;
  102. for (i=0;;i++) {
  103. StringCbPrintfW(d,sizeof(d), L"%s_history_%d", getId(), i);
  104. WASABI_API_CONFIG->getStringPrivate(d, c, WA_MAX_PATH, L"");
  105. if (!*c)
  106. break;
  107. history.addItem(new StringW(c));
  108. }
  109. if (refill) {
  110. deleteAllItems();
  111. for (i=history.getNumItems()-1;i>=0;i--) {
  112. addItem(history.enumItem(i)->getValue());
  113. }
  114. }
  115. }
  116. void HistoryEditBox::saveHistory() {
  117. wchar_t d[256] = {0};
  118. int i;
  119. for (i=0;i<history.getNumItems();i++) {
  120. StringCbPrintfW(d, sizeof(d), L"%s_history_%d", getId(), i);
  121. WASABI_API_CONFIG->setStringPrivate(d, history.enumItem(i)->getValue());
  122. }
  123. StringCbPrintfW(d, sizeof(d), L"%s_history_%d", getId(), i);
  124. WASABI_API_CONFIG->setStringPrivate(d, L"");
  125. }
  126. #endif
  127. int HistoryEditBox::onAction(const wchar_t *action, const wchar_t *param, int x, int y, intptr_t p1, intptr_t p2, void *data, size_t datalen, ifc_window *source) {
  128. int r = HISTORYEDITBOX_PARENT::onAction(action, param, x, y, p1, p2, data, datalen, source);
  129. if (WCSCASEEQLSAFE(action, L"back")) history_back();
  130. if (WCSCASEEQLSAFE(action, L"forward")) history_forward();
  131. return r;
  132. }