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 }; 24 25 26 StartTeamWindow::StartTeamWindow(TargetHostInterface* hostInterface) 27 : 28 BWindow(BRect(), "Start new team", B_TITLED_WINDOW, 29 B_AUTO_UPDATE_SIZE_LIMITS | B_CLOSE_ON_ESCAPE), 30 fGuideText(NULL), 31 fTeamTextControl(NULL), 32 fArgumentsTextControl(NULL), 33 fBrowseTeamButton(NULL), 34 fBrowseTeamPanel(NULL), 35 fStartButton(NULL), 36 fCancelButton(NULL), 37 fTargetHostInterface(hostInterface) 38 { 39 } 40 41 42 StartTeamWindow::~StartTeamWindow() 43 { 44 delete fBrowseTeamPanel; 45 } 46 47 48 StartTeamWindow* 49 StartTeamWindow::Create(TargetHostInterface* hostInterface) 50 { 51 StartTeamWindow* self = new StartTeamWindow(hostInterface); 52 53 try { 54 self->_Init(); 55 } catch (...) { 56 delete self; 57 throw; 58 } 59 60 return self; 61 62 } 63 64 65 void 66 StartTeamWindow::_Init() 67 { 68 fGuideText = new BStringView("guide", "Set new team parameters below."); 69 fTeamTextControl = new BTextControl("Path: ", NULL, NULL); 70 fArgumentsTextControl = new BTextControl("Arguments: ", NULL, NULL); 71 fBrowseTeamButton = new BButton("Browse" B_UTF8_ELLIPSIS, new BMessage( 72 MSG_BROWSE_TEAM)); 73 fStartButton = new BButton("Start team", new BMessage(MSG_START_NEW_TEAM)); 74 fCancelButton = new BButton("Cancel", new BMessage(B_QUIT_REQUESTED)); 75 76 BLayoutBuilder::Group<>(this, B_VERTICAL) 77 .SetInsets(B_USE_DEFAULT_SPACING) 78 .Add(fGuideText) 79 .AddGroup(B_HORIZONTAL, 4.0f) 80 .Add(fTeamTextControl) 81 .Add(fBrowseTeamButton) 82 .End() 83 .AddGroup(B_HORIZONTAL, 4.0f) 84 .Add(fArgumentsTextControl) 85 .End() 86 .AddGroup(B_HORIZONTAL, 4.0f) 87 .AddGlue() 88 .Add(fCancelButton) 89 .Add(fStartButton) 90 .End(); 91 92 fTeamTextControl->SetExplicitMinSize(BSize(300.0, B_SIZE_UNSET)); 93 fGuideText->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET)); 94 95 fStartButton->SetTarget(this); 96 fCancelButton->SetTarget(this); 97 } 98 99 100 void 101 StartTeamWindow::Show() 102 { 103 CenterOnScreen(); 104 BWindow::Show(); 105 } 106 107 108 void 109 StartTeamWindow::MessageReceived(BMessage* message) 110 { 111 switch (message->what) { 112 case MSG_BROWSE_TEAM: 113 { 114 if (fBrowseTeamPanel == NULL) { 115 fBrowseTeamPanel = new(std::nothrow) BFilePanel(B_OPEN_PANEL, 116 new BMessenger(this)); 117 if (fBrowseTeamPanel == NULL) 118 break; 119 BMessage* message = new(std::nothrow) BMessage( 120 MSG_SET_TEAM_PATH); 121 if (message == NULL) { 122 delete fBrowseTeamPanel; 123 fBrowseTeamPanel = NULL; 124 break; 125 } 126 fBrowseTeamPanel->SetMessage(message); 127 } 128 129 fBrowseTeamPanel->SetPanelDirectory(fTeamTextControl->TextView() 130 ->Text()); 131 132 fBrowseTeamPanel->Show(); 133 break; 134 } 135 case MSG_SET_TEAM_PATH: 136 { 137 entry_ref ref; 138 if (message->FindRef("refs", &ref) == B_OK) { 139 BPath path(&ref); 140 fTeamTextControl->TextView()->SetText(path.Path()); 141 } 142 break; 143 } 144 case MSG_START_NEW_TEAM: 145 { 146 BMessage appMessage(MSG_START_NEW_TEAM); 147 appMessage.AddString("path", fTeamTextControl->TextView()->Text()); 148 appMessage.AddString("arguments", fArgumentsTextControl->TextView() 149 ->Text()); 150 appMessage.AddPointer("interface", fTargetHostInterface); 151 BMessage reply; 152 be_app_messenger.SendMessage(&appMessage, &reply); 153 status_t error = reply.FindInt32("status"); 154 if (error != B_OK) { 155 BString messageString; 156 messageString.SetToFormat("Failed to start team: %s.", 157 strerror(error)); 158 BAlert* alert = new(std::nothrow) BAlert("Start team failed", 159 messageString.String(), "Close"); 160 if (alert != NULL) 161 alert->Go(); 162 } else { 163 PostMessage(B_QUIT_REQUESTED); 164 } 165 break; 166 } 167 default: 168 BWindow::MessageReceived(message); 169 break; 170 } 171 172 } 173 174 175 bool 176 StartTeamWindow::QuitRequested() 177 { 178 be_app->PostMessage(MSG_START_TEAM_WINDOW_CLOSED); 179 return true; 180 } 181