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(300.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->SetPanelDirectory(fTeamTextControl->TextView() 131 ->Text()); 132 133 fBrowseTeamPanel->Show(); 134 break; 135 } 136 case MSG_SET_TEAM_PATH: 137 { 138 entry_ref ref; 139 if (message->FindRef("refs", &ref) == B_OK) { 140 BPath path(&ref); 141 fTeamTextControl->TextView()->SetText(path.Path()); 142 } 143 break; 144 } 145 case MSG_START_NEW_TEAM: 146 { 147 BMessage appMessage(MSG_START_NEW_TEAM); 148 appMessage.AddString("path", fTeamTextControl->TextView()->Text()); 149 appMessage.AddString("arguments", fArgumentsTextControl->TextView() 150 ->Text()); 151 appMessage.AddPointer("interface", fTargetHostInterface); 152 BMessage reply; 153 be_app_messenger.SendMessage(&appMessage, &reply); 154 status_t error = reply.FindInt32("status"); 155 if (error != B_OK) { 156 BString messageString; 157 messageString.SetToFormat("Failed to start team: %s.", 158 strerror(error)); 159 BAlert* alert = new(std::nothrow) BAlert("Start team failed", 160 messageString.String(), "Close"); 161 if (alert != NULL) 162 alert->Go(); 163 } else { 164 be_app->PostMessage(MSG_START_TEAM_WINDOW_CLOSED); 165 PostMessage(B_QUIT_REQUESTED); 166 } 167 break; 168 } 169 default: 170 BWindow::MessageReceived(message); 171 break; 172 } 173 174 } 175