1 // Panel.cpp 2 3 #include <stdio.h> 4 5 #include <InterfaceDefs.h> 6 #include <Message.h> 7 #include <MessageFilter.h> 8 9 #include "Panel.h" 10 11 class EscapeFilter : public BMessageFilter { 12 public: 13 EscapeFilter(Panel* target) 14 : BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE), 15 fPanel(target) 16 { 17 } 18 virtual ~EscapeFilter() 19 { 20 } 21 virtual filter_result Filter(BMessage* message, BHandler** target) 22 { 23 filter_result result = B_DISPATCH_MESSAGE; 24 switch (message->what) { 25 case B_KEY_DOWN: 26 case B_UNMAPPED_KEY_DOWN: { 27 uint32 key; 28 if (message->FindInt32("raw_char", (int32*)&key) >= B_OK) { 29 if (key == B_ESCAPE) { 30 result = B_SKIP_MESSAGE; 31 fPanel->Cancel(); 32 } 33 } 34 break; 35 } 36 default: 37 break; 38 } 39 return result; 40 } 41 private: 42 Panel* fPanel; 43 }; 44 45 // constructor 46 Panel::Panel(BRect frame, const char* title, 47 window_type type, uint32 flags, 48 uint32 workspace) 49 : BWindow(frame, title, type, flags, workspace) 50 { 51 _InstallFilter(); 52 } 53 54 // constructor 55 Panel::Panel(BRect frame, const char* title, 56 window_look look, window_feel feel, 57 uint32 flags, uint32 workspace) 58 : BWindow(frame, title, look, feel, flags, workspace) 59 { 60 _InstallFilter(); 61 } 62 63 // destructor 64 Panel::~Panel() 65 { 66 } 67 68 // MessageReceived 69 void 70 Panel::Cancel() 71 { 72 PostMessage(B_QUIT_REQUESTED); 73 } 74 75 // _InstallFilter 76 void 77 Panel::_InstallFilter() 78 { 79 AddCommonFilter(new EscapeFilter(this)); 80 } 81