1 /* 2 * Copyright 2014-2016, 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 "AppMessageCodes.h" 13 14 15 ExpressionPromptWindow::ExpressionPromptWindow(BHandler* addTarget, 16 BHandler* closeTarget) 17 : 18 BWindow(BRect(), "Add Expression", B_FLOATING_WINDOW, 19 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 { 26 } 27 28 29 ExpressionPromptWindow::~ExpressionPromptWindow() 30 { 31 } 32 33 34 ExpressionPromptWindow* 35 ExpressionPromptWindow::Create(BHandler* addTarget, BHandler* closeTarget) 36 { 37 ExpressionPromptWindow* self = new ExpressionPromptWindow(addTarget, 38 closeTarget); 39 40 try { 41 self->_Init(); 42 } catch (...) { 43 delete self; 44 throw; 45 } 46 47 return self; 48 49 } 50 51 52 void 53 ExpressionPromptWindow::_Init() 54 { 55 fExpressionInput = new BTextControl("Expression:", NULL, NULL); 56 BLayoutItem* labelItem = fExpressionInput->CreateLabelLayoutItem(); 57 BLayoutItem* inputItem = fExpressionInput->CreateTextViewLayoutItem(); 58 inputItem->SetExplicitMinSize(BSize(200.0, B_SIZE_UNSET)); 59 inputItem->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET)); 60 labelItem->View()->SetViewUIColor(B_PANEL_BACKGROUND_COLOR); 61 62 BLayoutBuilder::Group<>(this, B_VERTICAL) 63 .SetInsets(B_USE_DEFAULT_SPACING) 64 .AddGroup(B_HORIZONTAL, 4.0f) 65 .Add(labelItem) 66 .Add(inputItem) 67 .End() 68 .AddGroup(B_HORIZONTAL, 4.0f) 69 .AddGlue() 70 .Add((fCancelButton = new BButton("Cancel", 71 new BMessage(B_QUIT_REQUESTED)))) 72 .Add((fAddButton = new BButton("Add", 73 new BMessage(MSG_ADD_NEW_EXPRESSION)))) 74 .End(); 75 76 fExpressionInput->SetTarget(this); 77 fCancelButton->SetTarget(this); 78 fAddButton->SetTarget(this); 79 fAddButton->MakeDefault(true); 80 fExpressionInput->TextView()->MakeFocus(true); 81 } 82 83 84 void 85 ExpressionPromptWindow::Show() 86 { 87 CenterOnScreen(); 88 BWindow::Show(); 89 } 90 91 92 bool 93 ExpressionPromptWindow::QuitRequested() 94 { 95 BMessenger messenger(fCloseTarget); 96 messenger.SendMessage(MSG_EXPRESSION_PROMPT_WINDOW_CLOSED); 97 98 return BWindow::QuitRequested(); 99 } 100 101 102 void 103 ExpressionPromptWindow::MessageReceived(BMessage* message) 104 { 105 switch (message->what) { 106 case MSG_ADD_NEW_EXPRESSION: 107 { 108 BMessage addMessage(MSG_ADD_NEW_EXPRESSION); 109 addMessage.AddString("expression", fExpressionInput->Text()); 110 addMessage.AddBool("persistent", true); 111 112 BMessenger(fAddTarget).SendMessage(&addMessage); 113 Quit(); 114 break; 115 } 116 117 default: 118 BWindow::MessageReceived(message); 119 break; 120 } 121 122 } 123