aboutsummaryrefslogtreecommitdiff
path: root/Src/Wasabi/api/skin/widgets/xuimenu.cpp
blob: 7113b8fbc50fb9517e23e0d0d1a0ec68cbe7fc48 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <precomp.h>
#include "xuimenu.h"
#include <api/service/svcs/svc_action.h>

#define MENU_TIMER_CHECK 0x100
#define DC_MENU_CHAIN    0x101
#define DC_MENU_REENABLE 0x102
#define DC_MENU_INIT     0x103
#define DC_MENU_NEXT     0x104
#define DC_MENU_PREV     0x105
#define DC_MENU_OPENACTION 0x106
#define MENU_TIMER_POPKBDLOCK 0x107
#define MENU_TIMER_DELAY 5
extern HINSTANCE hInstance;
const wchar_t MenuXuiObjectStr[] = L"Menu"; // This is the xml tag
char MenuXuiSvcName[] = "Menu xui object";

static XuiMenu *xuimenu_hookingMenu = NULL;
static HHOOK hhook=NULL;
static HHOOK hhook_menuselect=NULL;
static int hookusercount = 0;

XMLParamPair XuiMenu::params[] = {
	{MENU_DOWNID, L"DOWN"},
	{MENU_HOVERID, L"HOVER"},
	{MENU_MENU, L"MENU"},
	{MENU_MENUGROUP, L"MENUGROUP"},
	{MENU_NEXT, L"NEXT"},
	{MENU_NORMALID, L"NORMAL"},
	{MENU_PREV, L"PREV"},
	};

XuiMenu::XuiMenu() {
	timerset = 0;
	disablespawn = 0;
	nextinchain = NULL;
	normal = NULL;
	down = NULL;
	isspawned = 0;
	inarea = 0;
	kbdhook = 0;
	kbdlocktimer = 0;
	submenu_isselected = 0;
	submenu_selectedbymouse = 0;
	submenu_selected = NULL;
	first_hmenu = NULL;
	cur_hmenu = NULL;
	getScriptObject()->vcpu_setInterface(xuiMenuGuid, (void *)static_cast<XuiMenu *>(this));
	xuihandle = newXuiHandle();
	CreateXMLParameters(xuihandle);
	hover = NULL;
	orig_x = orig_y = 0;
	menu_parent = NULL;
}

void XuiMenu::CreateXMLParameters(int master_handle)
{
	//XUIMENU_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);
}

XuiMenu::~XuiMenu() {
  if (timerset) killTimer(MENU_TIMER_CHECK);
  if (kbdlocktimer) { killTimer(MENU_TIMER_POPKBDLOCK); WASABI_API_WND->popKeyboardLock(); } // do not set kbdlocktimer to 0, or stopKbdHook will start a timer
  stopKbdHook();
}

int XuiMenu::onInit() {
  XUIMENU_PARENT::onInit();
  postDeferredCallback(DC_MENU_INIT);
  return 1;
}

int XuiMenu::setXuiParam(int _xuihandle, int xmlattrid, const wchar_t *name, const wchar_t *value) {
  if (xuihandle == _xuihandle) {
    switch (xmlattrid) {
      case MENU_MENUGROUP: setMenuGroup(value); return 1;
      case MENU_MENU: setMenu(value); return 1;
      case MENU_NORMALID: setNormalId(value); return 1;
      case MENU_DOWNID: setDownId(value); return 1;
      case MENU_HOVERID: setHoverId(value); return 1;
      case MENU_NEXT: next = value; return 1;
      case MENU_PREV: prev = value; return 1;
    }
  }
  return XUIMENU_PARENT::setXuiParam(_xuihandle, xmlattrid, name, value);
}

void XuiMenu::setMenu(const wchar_t *m) 
{
  menuid = m;
  return;
}

void XuiMenu::setMenuGroup(const wchar_t *mg) {
  menugroup = mg;
  return;
}

void XuiMenu::setNormalId(const wchar_t *id) {
  normalid = id;
  updateObjects();
}

void XuiMenu::setDownId(const wchar_t *id) {
  downid = id;
  updateObjects();
}

void XuiMenu::setHoverId(const wchar_t *id) {
  hoverid = id;
  updateObjects();
}

int XuiMenu::onLeftButtonDown(int x, int y) {
  XUIMENU_PARENT::onLeftButtonDown(x, y);
  if (!disablespawn)
    spawnMenu(); 
  return 1;
}

void XuiMenu::timerCallback(int c) {
  if (c == MENU_TIMER_CHECK) {
    timerCheck();
  } else if (c == MENU_TIMER_POPKBDLOCK) {
    kbdlocktimer = 0;
    killTimer(MENU_TIMER_POPKBDLOCK);
    WASABI_API_WND->popKeyboardLock();
  }
  else XUIMENU_PARENT::timerCallback(c);
}

void XuiMenu::timerCheck() {
  POINT pt;
  Wasabi::Std::getMousePos((long*)&pt.x, (long*)&pt.y);
  if (pt.x == orig_x && pt.y == orig_y) return;
  nextinchain = NULL;
  ifc_window *w = WASABI_API_WND->rootWndFromPoint(&pt);
  if (w != NULL) {
    XuiMenu *x = static_cast<XuiMenu *>(w->getInterface(xuiMenuGuid));
    switchToMenu(x);
  }
}

void XuiMenu::switchToMenu(XuiMenu *x) {
  if (x != NULL && x != this && x != nextinchain) 
	{
    if (WCSCASEEQLSAFE(x->getMenuGroup(), getMenuGroup())) 
		{
      killTimer(MENU_TIMER_CHECK); 
      timerset = 0; 
      nextinchain = x;
      stopKbdHook();
      cancelMenu();
    }
  }
}

void XuiMenu::spawnMenu(int monitor) {
  
    Wasabi::Std::getMousePos(&orig_x, &orig_y);

  isspawned = 1;
  onOpenMenu();

  if (monitor) { setTimer(MENU_TIMER_CHECK, MENU_TIMER_DELAY); timerset = 1; startKbdHook(); }

  StringW actionstr = StringPrintfW(L"MENU:%s", menuid);

  svc_action *act = ActionEnum(actionstr).getNext();
  if (act) {
    RECT r;
    getClientRect(&r);
    clientToScreen(&r);
    act->onAction(actionstr, NULL, r.left, r.bottom, NULL, 0, this);
    SvcEnum::release(act);
  }

  if (monitor) {
    if (timerset) { killTimer(MENU_TIMER_CHECK); timerset = 0; }
    stopKbdHook();
  }
  disablespawn = 1;
  if (nextinchain) {
    postDeferredCallback(DC_MENU_CHAIN);
  } 
  postDeferredCallback(DC_MENU_REENABLE);

  isspawned = 0;
  onCloseMenu();
}

int XuiMenu::onDeferredCallback(intptr_t p1, intptr_t p2) {
  if (p1 == DC_MENU_CHAIN) {
    XuiMenu *x = nextinchain;
    nextinchain = NULL;
    x->spawnMenu();
  } else if (p1 == DC_MENU_REENABLE) {
    disablespawn = 0;
  } else if (p1 == DC_MENU_INIT) {
    updateObjects();
  } else if (p1 == DC_MENU_NEXT) {
    _nextMenu();
  } else if (p1 == DC_MENU_PREV) {
    _previousMenu();
  } else if (p1 == DC_MENU_OPENACTION) {
    spawnMenu(); 
    WASABI_API_WND->kbdReset();
  } else return XUIMENU_PARENT::onDeferredCallback(p1, p2);
  return 1;
}

void XuiMenu::cancelMenu() {
  #ifdef WIN32

  PostMessage(gethWnd(), WM_LBUTTONDOWN, 0, 0xdeadc0de);
  PostMessage(gethWnd(), WM_LBUTTONUP, 0, 0xdeadc0de);

  #else
  
  #error port me! you should close that menu which is in its own message pump now (oh and btw, you don't know shit about the menu itself because it's spawned by an action). have fun!
  
  #endif
}

void XuiMenu::onOpenMenu() {
  updateObjects();
  script_onOpenMenu();
}

void XuiMenu::onCloseMenu() {
  updateObjects();
  WASABI_API_WND->kbdReset();
  script_onCloseMenu();
}

void XuiMenu::updateObjects() 
{
  normal = findObject(normalid);
  down = findObject(downid);
  hover = findObject(hoverid);
  if (normal) normal->guiobject_getRootWnd()->setVisible(!isspawned);
  if (down) down->guiobject_getRootWnd()->setVisible(isspawned);
  if (hover) hover->guiobject_getRootWnd()->setVisible(!isspawned && inarea);
}

void XuiMenu::onEnterArea() {
  XUIMENU_PARENT::onEnterArea();
  inarea = 1;
  updateObjects();
}

void XuiMenu::onLeaveArea() {
  XUIMENU_PARENT::onLeaveArea();
  inarea = 0;
  updateObjects();
}

int XuiMenu::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) 
{
  if (!_wcsicmp(action, L"open")) { openAction(); }
  //else if (STRCASEEQL(action, "preopen")) { inarea = 1; updateObjects(); startKbdHook(); }
  else if (!_wcsicmp(action, L"close")) cancelMenu();
  else return XUIMENU_PARENT::onAction(action, param, x, y, p1, p2, data, datalen, source);
  return 1;
}

LRESULT CALLBACK xuimenu_KeyboardProc(int code, WPARAM wParam, LPARAM lParam) {
  if (code >= 0 && xuimenu_hookingMenu != NULL) {
    if (code != HC_NOREMOVE && !(lParam & (1 << 31))) {
      switch (wParam) {
        case VK_LEFT: xuimenu_hookingMenu->onTrappedLeft(); break;
        case VK_RIGHT: xuimenu_hookingMenu->onTrappedRight(); break;
        //case VK_ESCAPE: DebugString("Escape trapped!\n"); break;
      }
    }
  }
  if (hhook) return CallNextHookEx(hhook, code, wParam, lParam);
  return 1;
} 

LRESULT CALLBACK xuimenu_msgProc(int code, WPARAM wParam, LPARAM lParam) {
  if (code >= 0 && xuimenu_hookingMenu != NULL) {
    MSG *msg = (MSG *)lParam;
    if (msg->message == WM_MENUSELECT) {
      xuimenu_hookingMenu->onMenuSelect(msg->hwnd, (HMENU)msg->lParam, LOWORD(msg->wParam), HIWORD(msg->wParam));
    } 
  }
  if (hhook_menuselect) return CallNextHookEx(hhook_menuselect, code, wParam, lParam);
  return 1;
} 

void XuiMenu::startKbdHook() {
  if (kbdhook) return;
  if (kbdlocktimer) { killTimer(MENU_TIMER_POPKBDLOCK); kbdlocktimer = 0; }
  else WASABI_API_WND->pushKeyboardLock();
  xuimenu_hookingMenu = this;
  if (hhook == NULL) {
    hhook = SetWindowsHookEx(WH_KEYBOARD, xuimenu_KeyboardProc, 0, GetCurrentThreadId());
    hhook_menuselect = SetWindowsHookEx(WH_MSGFILTER, xuimenu_msgProc, 0, GetCurrentThreadId());
    hookusercount = 1;
  } else hookusercount++;
  kbdhook = 1;
}

void XuiMenu::stopKbdHook() {
  if (!kbdhook) return;
  if (nextinchain) {
    if (!kbdlocktimer) {
      kbdlocktimer = 1;
      setTimer(MENU_TIMER_POPKBDLOCK, 500);
    }
  } else WASABI_API_WND->popKeyboardLock();
  if (xuimenu_hookingMenu == this) xuimenu_hookingMenu = NULL;
  first_hmenu = NULL;
  submenu_isselected = 0;
  submenu_selectedbymouse = 0;
  submenu_selected = NULL;
  cur_hmenu = NULL;
  menu_parent = NULL;
  if (--hookusercount == 0) {
    UnhookWindowsHookEx(hhook_menuselect);
    UnhookWindowsHookEx(hhook);
    hhook = NULL;
    hhook_menuselect = NULL;
  }
  kbdhook = 0;
}

void XuiMenu::onMenuSelect(HWND hwnd, HMENU menu, int item, int flags) {
  if (first_hmenu == NULL) first_hmenu = menu;
  cur_hmenu = menu;
  menu_parent = hwnd;

//  DebugString("Menu Item Selected! HMENU = %X, item = %d, flags = %d, submenu = %X\n", menu, item, flags, flags & MF_POPUP);
  
  if (flags & MF_POPUP) { submenu_isselected = 1; submenu_selected = GetSubMenu(menu, item); submenu_selectedbymouse = flags & MF_MOUSESELECT; }
  else { submenu_isselected = 0; submenu_selected = NULL; submenu_selectedbymouse = 0; }
}

void XuiMenu::onTrappedLeft() {
  if (cur_hmenu == first_hmenu) 
    xuimenu_hookingMenu->previousMenu();
}

void XuiMenu::onTrappedRight() {
  if (!submenu_isselected) 
    nextMenu(); 
  else if (submenu_selectedbymouse)
    PostMessage(menu_parent, WM_KEYDOWN, VK_DOWN, 0);
}


void XuiMenu::openAction() {
  postDeferredCallback(DC_MENU_OPENACTION);
}

void XuiMenu::previousMenu() {
  postDeferredCallback(DC_MENU_PREV);
}

void XuiMenu::nextMenu() {
  postDeferredCallback(DC_MENU_NEXT);
}

void XuiMenu::_nextMenu() {
  GuiObject *o = findObject(next);
  if (o) {
    XuiMenu *menu = static_cast<XuiMenu*>(o->guiobject_getScriptObject()->vcpu_getInterface(xuiMenuGuid));
    if (menu) switchToMenu(menu);
  }
}

void XuiMenu::_previousMenu() {
  GuiObject *o = findObject(prev);
  if (o) {
    XuiMenu *menu = static_cast<XuiMenu*>(o->guiobject_getScriptObject()->vcpu_getInterface(xuiMenuGuid));
    if (menu) switchToMenu(menu);
  }
}