1 /* 2 * Copyright 2014, Rene Gollent, rene@gollent.com. 3 * Distributed under the terms of the MIT License. 4 */ 5 #include "ExpressionPromptWindow.h" 6 7 #include <Button.h> 8 #include <LayoutBuilder.h> 9 #include <String.h> 10 #include <TextControl.h> 11 12 #include "MessageCodes.h" 13 14 15 ExpressionPromptWindow::ExpressionPromptWindow(BHandler* addTarget, 16 BHandler* closeTarget, bool isPersistent) 17 : 18 BWindow(BRect(), isPersistent ? "Add Expression" : "Evaluate Expression", 19 B_FLOATING_WINDOW, B_AUTO_UPDATE_SIZE_LIMITS | B_CLOSE_ON_ESCAPE), 20 fExpressionInput(NULL), 21 fCancelButton(NULL), 22 fAddButton(NULL), 23 fAddTarget(addTarget), 24 fCloseTarget(closeTarget), 25 fPersistentExpression(isPersistent) 26 { 27 } 28 29 30 ExpressionPromptWindow::~ExpressionPromptWindow() 31 { 32 } 33 34 35 ExpressionPromptWindow* 36 ExpressionPromptWindow::Create(BHandler* addTarget, BHandler* closeTarget, 37 bool isPersistent) 38 { 39 ExpressionPromptWindow* self = new ExpressionPromptWindow(addTarget, 40 closeTarget, isPersistent); 41 42 try { 43 self->_Init(); 44 } catch (...) { 45 delete self; 46 throw; 47 } 48 49 return self; 50 51 } 52 53 54 void 55 ExpressionPromptWindow::_Init() 56 { 57 fExpressionInput = new BTextControl("Expression:", NULL, 58 new BMessage(MSG_EVALUATE_EXPRESSION)); 59 BLayoutItem* labelItem = fExpressionInput->CreateLabelLayoutItem(); 60 BLayoutItem* inputItem = fExpressionInput->CreateTextViewLayoutItem(); 61 inputItem->SetExplicitMinSize(BSize(200.0, B_SIZE_UNSET)); 62 inputItem->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET)); 63 labelItem->View()->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 64 65 BLayoutBuilder::Group<>(this, B_VERTICAL) 66 .SetInsets(B_USE_DEFAULT_SPACING) 67 .AddGroup(B_HORIZONTAL, 4.0f) 68 .Add(labelItem) 69 .Add(inputItem) 70 .End() 71 .AddGroup(B_HORIZONTAL, 4.0f) 72 .AddGlue() 73 .Add((fCancelButton = new BButton("Cancel", 74 new BMessage(B_QUIT_REQUESTED)))) 75 .Add((fAddButton = new BButton("Add", 76 new BMessage(MSG_ADD_NEW_EXPRESSION)))) 77 .End(); 78 79 fExpressionInput->SetTarget(this); 80 fCancelButton->SetTarget(this); 81 fAddButton->SetTarget(this); 82 fAddButton->MakeDefault(true); 83 fExpressionInput->TextView()->MakeFocus(true); 84 } 85 86 87 void 88 ExpressionPromptWindow::Show() 89 { 90 CenterOnScreen(); 91 BWindow::Show(); 92 } 93 94 95 bool 96 ExpressionPromptWindow::QuitRequested() 97 { 98 BMessenger messenger(fCloseTarget); 99 messenger.SendMessage(MSG_EXPRESSION_PROMPT_WINDOW_CLOSED); 100 101 return BWindow::QuitRequested(); 102 } 103 104 105 void 106 ExpressionPromptWindow::MessageReceived(BMessage* message) 107 { 108 switch (message->what) { 109 case MSG_ADD_NEW_EXPRESSION: 110 { 111 BMessage addMessage(MSG_EXPRESSION_PROMPT_WINDOW_CLOSED); 112 addMessage.AddString("expression", fExpressionInput->Text()); 113 addMessage.AddBool("persistent", fPersistentExpression); 114 addMessage.AddMessenger("target", BMessenger(fAddTarget)); 115 116 BMessenger(fCloseTarget).SendMessage(&addMessage); 117 Quit(); 118 break; 119 } 120 121 default: 122 BWindow::MessageReceived(message); 123 break; 124 } 125 126 } 127