1 /* 2 * Copyright 2013-2016, Rene Gollent, rene@gollent.com. 3 * Distributed under the terms of the MIT License. 4 */ 5 #include "StartTeamWindow.h" 6 7 #include <Alert.h> 8 #include <Application.h> 9 #include <Button.h> 10 #include <FilePanel.h> 11 #include <LayoutBuilder.h> 12 #include <Path.h> 13 #include <String.h> 14 #include <StringView.h> 15 #include <TextControl.h> 16 17 #include "AppMessageCodes.h" 18 #include "UserInterface.h" 19 20 21 enum { 22 MSG_BROWSE_TEAM = 'brte', 23 MSG_SET_TEAM_PATH = 'setp' 24 }; 25 26 27 StartTeamWindow::StartTeamWindow(TargetHostInterface* hostInterface) 28 : 29 BWindow(BRect(), "Start new team", B_TITLED_WINDOW, 30 B_AUTO_UPDATE_SIZE_LIMITS | B_CLOSE_ON_ESCAPE), 31 fGuideText(NULL), 32 fTeamTextControl(NULL), 33 fArgumentsTextControl(NULL), 34 fBrowseTeamButton(NULL), 35 fBrowseTeamPanel(NULL), 36 fStartButton(NULL), 37 fCancelButton(NULL), 38 fTargetHostInterface(hostInterface) 39 { 40 } 41 42 43 StartTeamWindow::~StartTeamWindow() 44 { 45 delete fBrowseTeamPanel; 46 } 47 48 49 StartTeamWindow* 50 StartTeamWindow::Create(TargetHostInterface* hostInterface) 51 { 52 StartTeamWindow* self = new StartTeamWindow(hostInterface); 53 54 try { 55 self->_Init(); 56 } catch (...) { 57 delete self; 58 throw; 59 } 60 61 return self; 62 63 } 64 65 66 void 67 StartTeamWindow::_Init() 68 { 69 fGuideText = new BStringView("guide", "Set new team parameters below."); 70 fTeamTextControl = new BTextControl("Path: ", NULL, NULL); 71 fArgumentsTextControl = new BTextControl("Arguments: ", NULL, NULL); 72 fBrowseTeamButton = new BButton("Browse" B_UTF8_ELLIPSIS, new BMessage( 73 MSG_BROWSE_TEAM)); 74 fStartButton = new BButton("Start team", new BMessage(MSG_START_NEW_TEAM)); 75 fCancelButton = new BButton("Cancel", new BMessage(B_QUIT_REQUESTED)); 76 77 BLayoutBuilder::Group<>(this, B_VERTICAL) 78 .SetInsets(B_USE_DEFAULT_SPACING) 79 .Add(fGuideText) 80 .AddGroup(B_HORIZONTAL, 4.0f) 81 .Add(fTeamTextControl) 82 .Add(fBrowseTeamButton) 83 .End() 84 .AddGroup(B_HORIZONTAL, 4.0f) 85 .Add(fArgumentsTextControl) 86 .End() 87 .AddGroup(B_HORIZONTAL, 4.0f) 88 .AddGlue() 89 .Add(fCancelButton) 90 .Add(fStartButton) 91 .End(); 92 93 fTeamTextControl->SetExplicitMinSize(BSize(200.0, B_SIZE_UNSET)); 94 fGuideText->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET)); 95 96 fStartButton->SetTarget(this); 97 fCancelButton->SetTarget(this); 98 } 99 100 101 void 102 StartTeamWindow::Show() 103 { 104 CenterOnScreen(); 105 BWindow::Show(); 106 } 107 108 109 void 110 StartTeamWindow::MessageReceived(BMessage* message) 111 { 112 switch (message->what) { 113 case MSG_BROWSE_TEAM: 114 { 115 if (fBrowseTeamPanel == NULL) { 116 fBrowseTeamPanel = new(std::nothrow) BFilePanel(B_OPEN_PANEL, 117 new BMessenger(this)); 118 if (fBrowseTeamPanel == NULL) 119 break; 120 BMessage* message = new(std::nothrow) BMessage( 121 MSG_SET_TEAM_PATH); 122 if (message == NULL) { 123 delete fBrowseTeamPanel; 124 fBrowseTeamPanel = NULL; 125 break; 126 } 127 fBrowseTeamPanel->SetMessage(message); 128 } 129 130 fBrowseTeamPanel->Show(); 131 break; 132 } 133 case MSG_SET_TEAM_PATH: 134 { 135 entry_ref ref; 136 if (message->FindRef("refs", &ref) == B_OK) { 137 BPath path(&ref); 138 fTeamTextControl->TextView()->SetText(path.Path()); 139 } 140 break; 141 } 142 case MSG_START_NEW_TEAM: 143 { 144 BMessage appMessage(MSG_START_NEW_TEAM); 145 appMessage.AddString("path", fTeamTextControl->TextView()->Text()); 146 appMessage.AddString("arguments", fArgumentsTextControl->TextView() 147 ->Text()); 148 appMessage.AddPointer("interface", fTargetHostInterface); 149 BMessage reply; 150 be_app_messenger.SendMessage(&appMessage, &reply); 151 status_t error = reply.FindInt32("status"); 152 if (error != B_OK) { 153 BString messageString; 154 messageString.SetToFormat("Failed to start team: %s.", 155 strerror(error)); 156 BAlert* alert = new(std::nothrow) BAlert("Start team failed", 157 messageString.String(), "Close"); 158 if (alert != NULL) 159 alert->Go(); 160 } else { 161 be_app->PostMessage(MSG_START_TEAM_WINDOW_CLOSED); 162 PostMessage(B_QUIT_REQUESTED); 163 } 164 break; 165 } 166 default: 167 BWindow::MessageReceived(message); 168 break; 169 } 170 171 } 172