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