Mercurial > xlib_App
changeset 8:c5c33bf5f1fb
Basic demo framework seems good
author | Eris Caffee <discordia@eldalin.com> |
---|---|
date | Sat, 21 Apr 2012 03:40:26 -0500 |
parents | 713e3c5db1e1 |
children | 8769ee69bd05 |
files | include/App.h src/App.cpp |
diffstat | 2 files changed, 77 insertions(+), 1 deletions(-) [+] |
line diff
1.1 --- a/include/App.h Wed Apr 18 00:53:10 2012 -0500 1.2 +++ b/include/App.h Sat Apr 21 03:40:26 2012 -0500 1.3 @@ -90,4 +90,5 @@ 1.4 long event_mask; 1.5 Atom wm_delete_window; // Need this in contructor and run(). 1.6 1.7 + std::string button_state_string(unsigned int state) const; 1.8 };
2.1 --- a/src/App.cpp Wed Apr 18 00:53:10 2012 -0500 2.2 +++ b/src/App.cpp Sat Apr 21 03:40:26 2012 -0500 2.3 @@ -87,7 +87,13 @@ 2.4 2.5 // Select the event types we want to receive. 2.6 //Other interesting events include KeyReleaseMask and ButtonReleaseMask 2.7 - event_mask = ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask ; 2.8 + event_mask = 2.9 + ExposureMask | StructureNotifyMask | 2.10 + KeyPressMask | KeyReleaseMask | 2.11 + FocusChangeMask | 2.12 + PointerMotionMask | 2.13 + EnterWindowMask | LeaveWindowMask | 2.14 + ButtonPressMask | ButtonReleaseMask ; 2.15 XSelectInput(dpy, wd->win, event_mask); 2.16 2.17 2.18 @@ -200,6 +206,9 @@ 2.19 { 2.20 switch (e.type) 2.21 { 2.22 + 2.23 + //////////////////////// 2.24 + // General window events 2.25 case Expose: 2.26 // We'll only redraw the entire window at a time, so unless this is 2.27 // the last contiguous expose, don't draw the window. 2.28 @@ -211,7 +220,46 @@ 2.29 if ((Atom)e.xclient.data.l[0] == wm_delete_window) 2.30 done = 1; 2.31 break; 2.32 + 2.33 + /////////////////////// 2.34 + // Mouse events 2.35 + case MotionNotify: 2.36 + std::cout << "Mouse at (" << e.xmotion.x << "," << e.xmotion.y << ")" << std::endl; 2.37 + break; 2.38 case ButtonPress: 2.39 + std::cout << "You pressed button " << e.xbutton.button << 2.40 + " at (" << e.xbutton.x << "," << e.xbutton.y << ")" << 2.41 + " state " << button_state_string(e.xbutton.state ) << std::endl; 2.42 + break; 2.43 + case ButtonRelease: 2.44 + std::cout << "You released button " << e.xbutton.button << 2.45 + " at (" << e.xbutton.x << "," << e.xbutton.y << ")" << 2.46 + " state " << button_state_string(e.xbutton.state ) << std::endl; 2.47 + break; 2.48 + case EnterNotify: 2.49 + std::cout << "Mouse has entered the window" << 2.50 + " at (" << e.xbutton.x << "," << e.xbutton.y << ")" << 2.51 + " state " << button_state_string(e.xbutton.state ) << std::endl; 2.52 + break; 2.53 + case LeaveNotify: 2.54 + std::cout << "Mouse has left the window" << 2.55 + " at (" << e.xbutton.x << "," << e.xbutton.y << ")" << 2.56 + " state " << button_state_string(e.xbutton.state ) << std::endl; 2.57 + break; 2.58 + 2.59 + ////////////////// 2.60 + // Keyboard Events 2.61 + case FocusIn: 2.62 + // Interesting note: When we regain focus due to a mouse click in the window, 2.63 + // we first get a LeaveNotify, then the FocusIn, then an EnterNotify, and then 2.64 + // the ButtonPress and ButtonRelease. So in a real app we might want to have 2.65 + // the FocusIn handler read ahead and handle (discard) the next ButtonPress, 2.66 + // and ButtonRelease so that the click does not have a real effect on the application. 2.67 + std::cout << "Got keyboard focus" << std::endl; 2.68 + break; 2.69 + case FocusOut: 2.70 + std::cout << "Lost keyboard focus" << std::endl; 2.71 + break; 2.72 case KeyPress: 2.73 // Doing the lookup like this, using XLookupString, means I get the modified 2.74 // character back. Thus type shift+a gives me XK_A instead of XK_a 2.75 @@ -232,6 +280,13 @@ 2.76 std::cout << "You typed " << key_buffer << std::endl; 2.77 break; 2.78 } 2.79 + case MappingNotify: 2.80 + // While it is uncommon for the user to change the keyboard mapping while 2.81 + // a program is running, handling this case is so trivial that there is 2.82 + // simply no excuse to leave it out. 2.83 + XRefreshKeyboardMapping((XMappingEvent *)&e); 2.84 + break; 2.85 + 2.86 default: 2.87 break; 2.88 } 2.89 @@ -243,6 +298,26 @@ 2.90 } 2.91 2.92 //////////////////////////////////////////////////////////////////////////////// 2.93 +std::string App::button_state_string(unsigned int state) const 2.94 + { 2.95 + std::string str = ""; 2.96 + if (state & Button1Mask) { str.append("Button1 "); } 2.97 + if (state & Button2Mask) { str.append("Button2 "); } 2.98 + if (state & Button3Mask) { str.append("Button3 "); } 2.99 + if (state & Button4Mask) { str.append("Button4 "); } 2.100 + if (state & Button5Mask) { str.append("Button5 "); } 2.101 + if (state & ShiftMask) { str.append("Shift "); } 2.102 + if (state & LockMask) { str.append("Lock "); } 2.103 + if (state & ControlMask) { str.append("Control "); } 2.104 + if (state & Mod1Mask) { str.append("Mod1 "); } 2.105 + if (state & Mod2Mask) { str.append("Mod2 "); } 2.106 + if (state & Mod3Mask) { str.append("Mod3 "); } 2.107 + if (state & Mod4Mask) { str.append("Mod4 "); } 2.108 + if (state & Mod5Mask) { str.append("Mod5 "); } 2.109 + return str; 2.110 + } 2.111 + 2.112 +//////////////////////////////////////////////////////////////////////////////// 2.113 // App::BitMap 2.114 //////////////////////////////////////////////////////////////////////////////// 2.115 App::BitMap::BitMap(const std::string & file)