From 6a1d23cf994844a39e5a1860c97276780903fb34 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 9 Mar 2013 14:54:27 +0100 Subject: Implement somewhat functional arrow-key navigation in listbox widget. --- plugingui/eventhandler.cc | 242 +++++++++++----------------------------------- plugingui/guievent.h | 14 +-- plugingui/listbox.cc | 55 ++++++++++- plugingui/listbox.h | 1 + 4 files changed, 119 insertions(+), 193 deletions(-) diff --git a/plugingui/eventhandler.cc b/plugingui/eventhandler.cc index 34e0769..1dd629d 100644 --- a/plugingui/eventhandler.cc +++ b/plugingui/eventhandler.cc @@ -147,95 +147,46 @@ LRESULT CALLBACK dialogProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) break; case WM_LBUTTONUP: - { - GUI::ButtonEvent *e = new GUI::ButtonEvent(); - e->x = (int)(short) LOWORD(lp); - e->y = (int)(short) HIWORD(lp); - e->button = 0; - e->direction = -1; - handler->event = e; - } - // xPos = (int)(short) LOWORD(lp); - // yPos = (int)(short) HIWORD(lp); - // fwKeys = wp; - break; - case WM_LBUTTONDBLCLK: case WM_LBUTTONDOWN: - { - GUI::ButtonEvent *e = new GUI::ButtonEvent(); - e->x = (int)(short) LOWORD(lp); - e->y = (int)(short) HIWORD(lp); - e->button = 0; - e->direction = 1; - e->doubleclick = (msg == WM_LBUTTONDBLCLK); - handler->event = e; - } - //xPos = (int)(short) LOWORD(lp); - //yPos = (int)(short) HIWORD(lp); - //fwKeys = wp; - break; - + case WM_RBUTTONUP: + case WM_RBUTTONDBLCLK: + case WM_RBUTTONDOWN: case WM_MBUTTONUP: - { - GUI::ButtonEvent *e = new GUI::ButtonEvent(); - e->x = (int)(short) LOWORD(lp); - e->y = (int)(short) HIWORD(lp); - e->button = 3; - e->direction = -1; - e->doubleclick = 0; - handler->event = e; - } - //xPos = (int)(short) LOWORD(lp); - //yPos = (int)(short) HIWORD(lp); - //fwKeys = wp; - break; - - case WM_MBUTTONDBLCLK: + case WM_MBUTTONDBLCLK: case WM_MBUTTONDOWN: { GUI::ButtonEvent *e = new GUI::ButtonEvent(); e->x = (int)(short) LOWORD(lp); e->y = (int)(short) HIWORD(lp); - e->button = 3; - e->direction = 1; - e->doubleclick = (msg == WM_MBUTTONDBLCLK); - handler->event = e; - } - // xPos = (int)(short) LOWORD(lp); - // yPos = (int)(short) HIWORD(lp); - // fwKeys = wp; - break; - case WM_RBUTTONUP: - { - GUI::ButtonEvent *e = new GUI::ButtonEvent(); - e->x = (int)(short) LOWORD(lp); - e->y = (int)(short) HIWORD(lp); - e->button = 1; - e->direction = -1; - e->doubleclick = 0; - handler->event = e; - } - // xPos = (int)(short) LOWORD(lp); - // yPos = (int)(short) HIWORD(lp); - // fwKeys = wp; - break; + if(msg == WM_LBUTTONUP || + msg == WM_LBUTTONDBLCLK || + msg == WM_LBUTTONDOWN) e->button = 0; + + if(msg == WM_RBUTTONUP || + msg == WM_RBUTTONDBLCLK || + msg == WM_RBUTTONDOWN) e->button = 1; + + if(msg == WM_MBUTTONUP || + msg == WM_MBUTTONDBLCLK || + msg == WM_MBUTTONDOWN) e->button = 2; + + e->direction = 0; + if(msg == WM_LBUTTONUP || + msg == WM_RBUTTONUP || + msg == WM_MBUTTONUP) e->direction = -1; + + if(msg == WM_LBUTTONDOWN || + msg == WM_RBUTTONDOWN || + msg == WM_MBUTTONDOWN) e->direction = 1; + + e->doubleclick = (msg == WM_LBUTTONDBLCLK || + msg == WM_RBUTTONDBLCLK || + msg == WM_MBUTTONDBLCLK); - case WM_RBUTTONDBLCLK: - case WM_RBUTTONDOWN: - { - GUI::ButtonEvent *e = new GUI::ButtonEvent(); - e->x = (int)(short) LOWORD(lp); - e->y = (int)(short) HIWORD(lp); - e->button = 1; - e->direction = 1; - e->doubleclick = (msg == WM_RBUTTONDBLCLK); handler->event = e; } - // xPos = (int)(short) LOWORD(lp); - // yPos = (int)(short) HIWORD(lp); - // fwKeys = wp; break; case WM_KEYDOWN: @@ -254,9 +205,6 @@ LRESULT CALLBACK dialogProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) e->direction = -1; handler->event = e; } - // xPos = (int)(short) LOWORD(lp); - // yPos = (int)(short) HIWORD(lp); - // fwKeys = wp; break; case WM_CHAR: @@ -270,9 +218,6 @@ LRESULT CALLBACK dialogProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) handler->event = e; } } - // xPos = (int)(short) LOWORD(lp); - // yPos = (int)(short) HIWORD(lp); - // fwKeys = wp; break; case WM_PAINT: @@ -284,8 +229,6 @@ LRESULT CALLBACK dialogProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) e->height = 100; handler->event = e; - -#if 1 // Move to window.h (in class) HDC pDC; HBITMAP old; @@ -294,118 +237,47 @@ LRESULT CALLBACK dialogProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) GUI::PixelBuffer &px = gwindow->wpixbuf; { // Create bitmap (move to window.cc) - HDC hDC; BITMAPINFO bitmapinfo; - hDC=CreateCompatibleDC(NULL); - bitmapinfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); - bitmapinfo.bmiHeader.biWidth=px.width; - bitmapinfo.bmiHeader.biHeight=-px.height; /* top-down */ - bitmapinfo.bmiHeader.biPlanes=1; - bitmapinfo.bmiHeader.biBitCount=32; - bitmapinfo.bmiHeader.biCompression=BI_RGB; - bitmapinfo.bmiHeader.biSizeImage=0; - bitmapinfo.bmiHeader.biClrUsed=256; - bitmapinfo.bmiHeader.biClrImportant=256; - ourbitmap=CreateDIBSection(hDC,&bitmapinfo,DIB_RGB_COLORS,(void**)&framebuf,0,0); + hDC = CreateCompatibleDC(NULL); + bitmapinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); + bitmapinfo.bmiHeader.biWidth = px.width; + bitmapinfo.bmiHeader.biHeight = -px.height; /* top-down */ + bitmapinfo.bmiHeader.biPlanes = 1; + bitmapinfo.bmiHeader.biBitCount = 32; + bitmapinfo.bmiHeader.biCompression = BI_RGB; + bitmapinfo.bmiHeader.biSizeImage = 0; + bitmapinfo.bmiHeader.biClrUsed = 256; + bitmapinfo.bmiHeader.biClrImportant = 256; + ourbitmap=CreateDIBSection(hDC, &bitmapinfo, + DIB_RGB_COLORS, (void**)&framebuf, 0, 0); pDC=CreateCompatibleDC(NULL); - old=(HBITMAP__*)SelectObject(pDC,ourbitmap); + old = (HBITMAP__*)SelectObject(pDC, ourbitmap); DeleteDC(hDC); - } - { // Copy GUI::PixelBuffer to framebuffer (move to window.cc) - int i,j,k; - for (k=0,i=0;i<(int)px.height;i++) - for (j=0;j<(int)px.width;j++,k++) + for (k=0,i=0;i<(int)px.height;i++) { + for (j=0;j<(int)px.width;j++,k++) { *(framebuf+k)=RGB(px.buf[(j + i * px.width) * 3 + 2], px.buf[(j + i * px.width) * 3 + 1], px.buf[(j + i * px.width) * 3 + 0]); + } + } } - PAINTSTRUCT ps; - // RECT rect; - HDC hdc; - - // bool m_state = true; - // HBRUSH m_brush; - // HPEN m_penh, m_pens; //Outer - // HPEN m_penih, m_penis; //Inner - - hdc = BeginPaint(handler->gctx->m_hwnd, &ps); - BitBlt(hdc,0,0,px.width,px.height,pDC,0,0,SRCCOPY); - EndPaint(handler->gctx->m_hwnd, &ps); - - - - - { // Destroy bitmap (move to window.cc) - - SelectObject(pDC,old); - DeleteDC(pDC); - DeleteObject(ourbitmap); - - } - - - - - - - -#else - PAINTSTRUCT ps; - // RECT rect; - HDC hdc; - hdc = BeginPaint(handler->gctx->m_hwnd, &ps); - if(hdc) { - // GetClientRect(handler->gctx->m_hwnd, &rect); - - //Backgound - // FillRect(hdc, &rect, m_brush); - - /* - POINT p[2]; - p[0].x = p[0].y = 10; - p[1].x = p[1].y = 10; - SelectObject(hdc, m_penis); - Polyline(hdc, p, 2); - */ - GUI::PixelBuffer &px = gwindow->wpixbuf; - for(size_t y = 0; y < px.height; y++) { - for(size_t x = 0; x < px.width; x++) { - int col = *((int*)(&px.buf[(x + y * px.width) * 3])) & 0xffffff; - SetPixel(hdc, x, y, col); + PAINTSTRUCT ps; + HDC hdc = BeginPaint(handler->gctx->m_hwnd, &ps); + BitBlt(hdc,0,0,px.width,px.height,pDC,0,0,SRCCOPY); + EndPaint(handler->gctx->m_hwnd, &ps); + + { // Destroy bitmap (move to window.cc) + SelectObject(pDC,old); + DeleteDC(pDC); + DeleteObject(ourbitmap); + } - } - - /* - //Edges - drawHilight(hdc, m_state ? m_pens : m_penh, &rect); - drawShadow(hdc, m_state ? m_penh : m_pens, &rect); - - //Lav rect 1 mindre (shrink) - rect.left++; - rect.right--; - rect.top++; - rect.bottom--; - - drawHilight(hdc, m_state ? m_penis : m_penih, &rect); - drawShadow(hdc, m_state ? m_penih : m_penis, &rect); - */ - EndPaint(handler->gctx->m_hwnd, &ps); - } - //DeleteDC(hdc); -#endif - - - - - - - } return DefWindowProc(hwnd, msg, wp, lp); } @@ -520,13 +392,15 @@ GUI::Event *GUI::EventHandler::getNextEvent() } if(xe.type == KeyPress || xe.type == KeyRelease) { - // printf("key: %d\n", e.xkey.keycode); + // printf("key: %d\n", xe.xkey.keycode); KeyEvent *e = new KeyEvent(); e->window_id = xe.xkey.window; switch(xe.xkey.keycode) { case 113: e->keycode = KeyEvent::KEY_LEFT; break; case 114: e->keycode = KeyEvent::KEY_RIGHT; break; + case 111: e->keycode = KeyEvent::KEY_UP; break; + case 116: e->keycode = KeyEvent::KEY_DOWN; break; case 119: e->keycode = KeyEvent::KEY_DELETE; break; case 22: e->keycode = KeyEvent::KEY_BACKSPACE; break; case 110: e->keycode = KeyEvent::KEY_HOME; break; diff --git a/plugingui/guievent.h b/plugingui/guievent.h index db0328c..df2db26 100644 --- a/plugingui/guievent.h +++ b/plugingui/guievent.h @@ -108,12 +108,14 @@ public: enum { KEY_UNKNOWN =-1, - KEY_LEFT = 1,// if(e->keycode == 113) { // left key - KEY_RIGHT = 2,//} else if(e->keycode == 114) { // right key - KEY_DELETE = 3,//} else if(e->keycode == 119) { // delete - KEY_BACKSPACE = 4,//} else if(e->keycode == 22) { // backspace - KEY_HOME = 5, - KEY_END = 6, + KEY_LEFT = 1, + KEY_RIGHT = 2, + KEY_UP = 3, + KEY_DOWN = 4, + KEY_DELETE = 5, + KEY_BACKSPACE = 6, + KEY_HOME = 7, + KEY_END = 8, KEY_CHARACTER = 0xffff // character data is stored in 'text' }; }; diff --git a/plugingui/listbox.cc b/plugingui/listbox.cc index f917368..3dec8bc 100644 --- a/plugingui/listbox.cc +++ b/plugingui/listbox.cc @@ -134,9 +134,58 @@ void GUI::ListBox::scrollEvent(ScrollEvent *e) repaintEvent(NULL); } +void GUI::ListBox::keyEvent(GUI::KeyEvent *e) +{ + // printf("!\n"); + + if(e->direction == -1) { + switch(e->keycode) { + case GUI::KeyEvent::KEY_UP: + { + std::map::reverse_iterator i = items.rbegin(); + while(i != items.rend()) { + if(i->second == selected) break; + i++; + } + i++; + scroll_offset--; + if(scroll_offset < 0) scroll_offset = 0; + selected = i->second; + } + break; + case GUI::KeyEvent::KEY_DOWN: + { + std::map::iterator i = items.begin(); + while(i != items.end()) { + if(i->second == selected) break; + i++; + } + i++; + scroll_offset++; + if(scroll_offset > (items.size() - 1)) + scroll_offset = (items.size() - 1); + selected = i->second; + } + break; + case GUI::KeyEvent::KEY_HOME: + selected = items.begin()->second; + break; + case GUI::KeyEvent::KEY_END: + selected = items.rbegin()->second; + break; + default: + break; + } + + // printf("sel: %s\n", selected.c_str()); + + repaintEvent(NULL); + } + +} + void GUI::ListBox::buttonEvent(ButtonEvent *e) { - //printf("click %d %d [dc: %d]\n", e->x, e->y, e->doubleclick); if(e->direction == 1) { if(e->x > (width() - btn_size) && e->y < (width() - 1)) { if(e->y > 0 && e->y < btn_size) { @@ -173,7 +222,7 @@ void GUI::ListBox::buttonEvent(ButtonEvent *e) } repaintEvent(NULL); - - if(e->doubleclick && dblclk_handler) dblclk_handler(ptr); } + + if(e->doubleclick && dblclk_handler) dblclk_handler(ptr); } diff --git a/plugingui/listbox.h b/plugingui/listbox.h index 061a0eb..26d104b 100644 --- a/plugingui/listbox.h +++ b/plugingui/listbox.h @@ -54,6 +54,7 @@ public: virtual void repaintEvent(RepaintEvent *e); virtual void buttonEvent(ButtonEvent *e); virtual void scrollEvent(ScrollEvent *e); + virtual void keyEvent(KeyEvent *e); private: std::map items; -- cgit v1.2.3