aboutsummaryrefslogtreecommitdiff
path: root/Src/Wasabi/api/skin/widgets/historyeditbox.cpp
blob: bec191fc4c46e7da859d19c5142393bf90ef1d9c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <precomp.h>
#include "historyeditbox.h"

XMLParamPair HistoryEditBox::params[] = {
	  {HISTORYEDITBOX_SETNAVBUTTONS, L"NAVBUTTONS"}, // param is implemented by script
};
HistoryEditBox::HistoryEditBox() {
  history_pos = 0;
  xuihandle = newXuiHandle();
	CreateXMLParameters(xuihandle);

  setXmlParam(L"navbuttons", L"1"); // so we need to set a default value in the xml param list
}

void HistoryEditBox::CreateXMLParameters(int master_handle)
{
	//HISTORYEDITBOX_PARENT::CreateXMLParameters(master_handle);
	int numParams = sizeof(params) / sizeof(params[0]);
	hintNumberOfParams(xuihandle, numParams);
	for (int i = 0;i < numParams;i++)
		addParam(xuihandle, params[i], XUI_ATTRIBUTE_IMPLIED);
}

HistoryEditBox::~HistoryEditBox() {
  history.deleteAll();
}

void HistoryEditBox::history_forward() {
  if (history_pos > 0 && !isListOpen()) {
    history_pos--;
    if (history_pos > 0)
      setText(history.enumItem(history.getNumItems()-history_pos)->getValue(), 1);
  }
}

void HistoryEditBox::history_back() {
  if (!isListOpen() && history_pos < history.getNumItems()) {
    history_pos++;
    setText(history.enumItem(history.getNumItems()-history_pos)->getValue(), 1);
  }
}

void HistoryEditBox::onEditKeyDown(int vk) {
  HISTORYEDITBOX_PARENT::onEditKeyDown(vk);
  if (Std::keyDown(VK_CONTROL)) return;
  if (vk == VK_DOWN) {
    history_forward();
  } else if (vk == VK_UP) {
    history_back();
  }
}

int HistoryEditBox::onInit() {
  int r = HISTORYEDITBOX_PARENT::onInit();
#ifdef WASABI_COMPILE_CONFIG
  loadHistory();
#endif
  return r;
}

void HistoryEditBox::dropdownlist_onCloseList() {
  HISTORYEDITBOX_PARENT::dropdownlist_onCloseList();
  history_pos = 0;
}

void HistoryEditBox::onPreOpenList() 
{
  HISTORYEDITBOX_PARENT::onPreOpenList();
  addHistory(getText());
}

void HistoryEditBox::onEditEnter(const wchar_t *txt) 
{
  HISTORYEDITBOX_PARENT::onEditEnter(txt);
  if (Std::keyDown(VK_CONTROL)) return;
  addHistory(txt);
}

void HistoryEditBox::addHistory(const wchar_t *txt) 
{
  HISTORYEDITBOX_PARENT::onEditEnter(txt);
  history_pos = 0;
  
  if (!txt || !*txt) return;

  // yay multi-instances on unique history
#ifdef WASABI_COMPILE_CONFIG
  loadHistory(0);
#endif

  foreach(history) 
    StringW *s = history.getfor();
    if (!_wcsicmp(s->getValue(), txt)) {
      delete s;
      history.removeByPos(foreach_index);
      break;
    }
  endfor;

  history.addItem(new StringW(txt));

  while (history.getNumItems() > 64)
	{
    StringW *s = history.enumItem(0);
    delete s;
    history.removeByPos(1);
  }
#ifdef WASABI_COMPILE_CONFIG
  saveHistory();
  loadHistory(1);
#endif
}

#ifdef WASABI_COMPILE_CONFIG
void HistoryEditBox::loadHistory(int refill) {
  history.deleteAll();
  wchar_t d[256] = {0};
  wchar_t c[WA_MAX_PATH] = {0};
  int i;
  for (i=0;;i++) {
    StringCbPrintfW(d,sizeof(d),  L"%s_history_%d", getId(), i);
    WASABI_API_CONFIG->getStringPrivate(d, c, WA_MAX_PATH, L"");
    if (!*c)
      break;
    history.addItem(new StringW(c));
  }
  if (refill) {
    deleteAllItems();
    for (i=history.getNumItems()-1;i>=0;i--) {
      addItem(history.enumItem(i)->getValue());
    }
  }
}

void HistoryEditBox::saveHistory() {
  wchar_t d[256] = {0};
	int i;
  for (i=0;i<history.getNumItems();i++) {
    StringCbPrintfW(d, sizeof(d), L"%s_history_%d", getId(), i);
    WASABI_API_CONFIG->setStringPrivate(d, history.enumItem(i)->getValue());
  }
  StringCbPrintfW(d, sizeof(d), L"%s_history_%d", getId(), i);
  WASABI_API_CONFIG->setStringPrivate(d, L"");
}
#endif

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) {
  int r = HISTORYEDITBOX_PARENT::onAction(action, param, x, y, p1, p2, data, datalen, source);
  if (WCSCASEEQLSAFE(action, L"back")) history_back();
  if (WCSCASEEQLSAFE(action, L"forward")) history_forward();
  return r;
}