1 #include "InlineEditor.h" 2 #include <MessageFilter.h> 3 #include <Handler.h> 4 5 class EditFilter : public BMessageFilter 6 { 7 public: 8 EditFilter(BTextControl *textbox) 9 : BMessageFilter(B_PROGRAMMED_DELIVERY, B_ANY_SOURCE,B_KEY_DOWN) 10 { 11 fTextBox = textbox; 12 } 13 14 ~EditFilter(void) 15 { 16 } 17 18 filter_result Filter(BMessage *msg, BHandler **target) 19 { 20 int32 rawchar; 21 msg->FindInt32("raw_char",&rawchar); 22 23 if (rawchar == B_ESCAPE) { 24 BLooper *loop = (*target)->Looper(); 25 if (loop) { 26 BMessenger msgr(loop); 27 msgr.SendMessage(B_QUIT_REQUESTED); 28 return B_SKIP_MESSAGE; 29 } 30 } else if (rawchar == B_ENTER) { 31 fTextBox->Invoke(); 32 return B_SKIP_MESSAGE; 33 } 34 return B_DISPATCH_MESSAGE; 35 } 36 37 private: 38 BTextControl *fTextBox; 39 40 }; 41 42 InlineEditor::InlineEditor(BMessenger target, const BRect &frame, 43 const char *text) 44 : BWindow(frame,"InlineEditor",B_NO_BORDER_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL, 45 B_ASYNCHRONOUS_CONTROLS), 46 fMessenger(target), 47 fCommand(M_INLINE_TEXT) 48 { 49 fTextBox = new BTextControl(BRect(0,0,1,1), "inlinebox",NULL, text, 50 new BMessage(fCommand), B_FOLLOW_ALL, 51 B_WILL_DRAW); 52 AddChild(fTextBox); 53 fTextBox->SetDivider(0); 54 fTextBox->MakeFocus(true); 55 56 fTextBox->ResizeToPreferred(); 57 fTextBox->ResizeTo(Bounds().Width(),fTextBox->Bounds().Height()); 58 ResizeTo(Bounds().Width(), fTextBox->Bounds().Height()); 59 60 AddCommonFilter(new EditFilter(fTextBox)); 61 } 62 63 64 bool 65 InlineEditor::QuitRequested(void) 66 { 67 return true; 68 } 69 70 71 void 72 InlineEditor::SetMessage(BMessage *msg) 73 { 74 fCommand = msg ? msg->what : 0; 75 fTextBox->SetMessage(msg); 76 } 77 78 79 void 80 InlineEditor::MessageReceived(BMessage *msg) 81 { 82 if (msg->what == fCommand) { 83 fMessenger.SendMessage(msg); 84 PostMessage(B_QUIT_REQUESTED); 85 } 86 } 87 88 void 89 InlineEditor::WindowActivated(bool active) 90 { 91 if (!active) 92 PostMessage(B_QUIT_REQUESTED); 93 } 94 95