// note that the pos < 0 check is /after/ the p->length - p->pagesize check
// it used to be /before/; this was actually a bug in Raymond Chen's original algorithm: if there are fewer than a page's worth of items, p->length - p->pagesize will be negative and our content draw at the bottom of the window
// this SHOULD have the same effect with that bug fixed and no others introduced... (thanks to devin on irc.badnik.net for confirming this logic)
// TODO verify this still holds with uiArea
if(pos>p->length-p->pagesize)
pos=p->length-p->pagesize;
if(pos<0)
pos=0;
// negative because ScrollWindowEx() is "backwards"
// TODO use scrollAmount == 3 (for both v and h) instead?
logLastError("error getting area wheel scroll amount in wheelscroll()");
if(scrollAmount==WHEEL_PAGESCROLL)
scrollAmount=p->pagesize;
if(scrollAmount==0)// no mouse wheel scrolling (or t->pagesize == 0)
return;
// the rest of this is basically http://blogs.msdn.com/b/oldnewthing/archive/2003/08/07/54615.aspx and http://blogs.msdn.com/b/oldnewthing/archive/2003/08/11/54624.aspx
// GetMessageTime() returns LONG and GetDoubleClckTime() returns UINT, which are int32 and uint32, respectively, but we don't need to worry about the signedness because for the same bit widths and two's complement arithmetic, s1-s2 == u1-u2 if bits(s1)==bits(s2) and bits(u1)==bits(u2) (and Windows requires two's complement: http://blogs.msdn.com/b/oldnewthing/archive/2005/05/27/422551.aspx)
// signedness isn't much of an issue for these calls anyway because http://stackoverflow.com/questions/24022225/what-are-the-sign-extension-rules-for-calling-windows-api-functions-stdcall-t and that we're only using unsigned values (think back to how you (didn't) handle signedness in assembly language) AND because of the above AND because the statistics below (time interval and width/height) really don't make sense if negative
me.Count=clickCounterClick(&(a->cc),me.Down,
me.X,me.Y,
GetMessageTime(),GetDoubleClickTime(),
GetSystemMetrics(SM_CXDOUBLECLK)/2,
GetSystemMetrics(SM_CYDOUBLECLK)/2);
// though wparam will contain control and shift state, let's just one function to get modifiers for both keyboard and mouse events; it'll work the same anyway since we have to do this for alt and windows key (super)
me.Modifiers=getModifiers();
button=me.Down;
if(button==0)
button=me.Up;
me.Held1To64=0;
if(button!=1&&(wParam&MK_LBUTTON)!=0)
me.Held1To64|=1<<0;
if(button!=2&&(wParam&MK_MBUTTON)!=0)
me.Held1To64|=1<<1;
if(button!=3&&(wParam&MK_RBUTTON)!=0)
me.Held1To64|=1<<2;
if(button!=4&&(wParam&MK_XBUTTON1)!=0)
me.Held1To64|=1<<3;
if(button!=5&&(wParam&MK_XBUTTON2)!=0)
me.Held1To64|=1<<4;
// on Windows, we have to capture on drag ourselves
if(me.Down!=0&&!a->capturing){
SetCapture(a->hwnd);
a->capturing=TRUE;
}
// only release capture when all buttons released
if(me.Up!=0&&a->capturing&&me.Held1To64==0){
// unset flag first as ReleaseCapture() sends WM_CAPTURECHANGED
a->capturing=FALSE;
if(ReleaseCapture()==0)
logLastError("error releasing capture on drag in areaMouseEvent()");
}
(*(a->ah->MouseEvent))(a->ah,a,&me);
}
// we use VK_SNAPSHOT as a sentinel because libui will never support the print screen key; that key belongs to the user
structextkeymap{
WPARAMvk;
uiExtKeyextkey;
};
// all mappings come from GLFW - https://github.com/glfw/glfw/blob/master/src/win32_window.c#L152
staticconststructextkeymapnumpadExtKeys[]={
{VK_HOME,uiExtKeyN7},
{VK_UP,uiExtKeyN8},
{VK_PRIOR,uiExtKeyN9},
{VK_LEFT,uiExtKeyN4},
{VK_CLEAR,uiExtKeyN5},
{VK_RIGHT,uiExtKeyN6},
{VK_END,uiExtKeyN1},
{VK_DOWN,uiExtKeyN2},
{VK_NEXT,uiExtKeyN3},
{VK_INSERT,uiExtKeyN0},
{VK_DELETE,uiExtKeyNDot},
{VK_SNAPSHOT,0},
};
staticconststructextkeymapextKeys[]={
{VK_ESCAPE,uiExtKeyEscape},
{VK_INSERT,uiExtKeyInsert},
{VK_DELETE,uiExtKeyDelete},
{VK_HOME,uiExtKeyHome},
{VK_END,uiExtKeyEnd},
{VK_PRIOR,uiExtKeyPageUp},
{VK_NEXT,uiExtKeyPageDown},
{VK_UP,uiExtKeyUp},
{VK_DOWN,uiExtKeyDown},
{VK_LEFT,uiExtKeyLeft},
{VK_RIGHT,uiExtKeyRight},
{VK_F1,uiExtKeyF1},
{VK_F2,uiExtKeyF2},
{VK_F3,uiExtKeyF3},
{VK_F4,uiExtKeyF4},
{VK_F5,uiExtKeyF5},
{VK_F6,uiExtKeyF6},
{VK_F7,uiExtKeyF7},
{VK_F8,uiExtKeyF8},
{VK_F9,uiExtKeyF9},
{VK_F10,uiExtKeyF10},
{VK_F11,uiExtKeyF11},
{VK_F12,uiExtKeyF12},
// numpad numeric keys and . are handled in events.c
// numpad enter is handled in code below
{VK_ADD,uiExtKeyNAdd},
{VK_SUBTRACT,uiExtKeyNSubtract},
{VK_MULTIPLY,uiExtKeyNMultiply},
{VK_DIVIDE,uiExtKeyNDivide},
{VK_SNAPSHOT,0},
};
staticconststruct{
WPARAMvk;
uiModifiersmod;
}modKeys[]={
// even if the separate left/right aren't necessary, have them here anyway, just to be safe
{VK_CONTROL,uiModifierCtrl},
{VK_LCONTROL,uiModifierCtrl},
{VK_RCONTROL,uiModifierCtrl},
{VK_MENU,uiModifierAlt},
{VK_LMENU,uiModifierAlt},
{VK_RMENU,uiModifierAlt},
{VK_SHIFT,uiModifierShift},
{VK_LSHIFT,uiModifierShift},
{VK_RSHIFT,uiModifierShift},
// there's no combined Windows key virtual-key code as there is with the others
// the numeric keypad keys when Num Lock is off are considered left-hand keys as the separate navigation buttons were added later
// the numeric keypad enter, however, is a right-hand key because it has the same virtual-key code as the typewriter enter
righthand=(lParam&0x01000000)!=0;
if(righthand){
if(wParam==VK_RETURN){
ke.ExtKey=uiExtKeyNEnter;
gotokeyFound;
}
}else
// this is special handling for numpad keys to ignore the state of Num Lock and Shift; see http://blogs.msdn.com/b/oldnewthing/archive/2004/09/06/226045.aspx and https://github.com/glfw/glfw/blob/master/src/win32_window.c#L152
for(i=0;numpadExtKeys[i].vk!=VK_SNAPSHOT;i++)
if(numpadExtKeys[i].vk==wParam){
ke.ExtKey=numpadExtKeys[i].extkey;
gotokeyFound;
}
// okay, those above cases didn't match anything
// first try the extended keys
for(i=0;extKeys[i].vk!=VK_SNAPSHOT;i++)
if(extKeys[i].vk==wParam){
ke.ExtKey=extKeys[i].extkey;
gotokeyFound;
}
// then try modifier keys
for(i=0;modKeys[i].vk!=VK_SNAPSHOT;i++)
if(modKeys[i].vk==wParam){
ke.Modifier=modKeys[i].mod;
// and don't include the key in Modifiers
ke.Modifiers&=~ke.Modifier;
gotokeyFound;
}
// and finally everything else
if(fromScancode((lParam>>16)&0xFF,&ke))
gotokeyFound;
// not a supported key, assume unhandled
// TODO the original code only did this if ke.Modifiers == 0 - why?