xref: /haiku/src/apps/debugger/user_interface/gui/teams_window/TeamsWindow.cpp (revision 25a7b01d15612846f332751841da3579db313082)
1 /*
2  * Copyright 2009-2010, Philippe Houdoin, phoudoin@haiku-os.org. All rights reserved.
3  * Copyright 2013, Rene Gollent, rene@gollent.com.
4  * Distributed under the terms of the MIT License.
5  */
6 
7 
8 #include <new>
9 
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdarg.h>
13 
14 #include <Application.h>
15 #include <Button.h>
16 #include <File.h>
17 #include <FindDirectory.h>
18 #include <LayoutBuilder.h>
19 #include <ListView.h>
20 #include <Path.h>
21 #include <ScrollView.h>
22 
23 #include "MessageCodes.h"
24 #include "SettingsManager.h"
25 #include "StartTeamWindow.h"
26 #include "TeamsWindow.h"
27 #include "TeamsListView.h"
28 
29 
30 enum {
31 	MSG_TEAM_SELECTION_CHANGED = 'tesc'
32 };
33 
34 
35 TeamsWindow::TeamsWindow(SettingsManager* settingsManager)
36 	:
37 	BWindow(BRect(100, 100, 500, 250), "Teams", B_DOCUMENT_WINDOW,
38 		B_ASYNCHRONOUS_CONTROLS),
39 	fTeamsListView(NULL),
40 	fStartTeamWindow(NULL),
41 	fAttachTeamButton(NULL),
42 	fCreateTeamButton(NULL),
43 	fSettingsManager(settingsManager)
44 {
45 	team_info info;
46 	get_team_info(B_CURRENT_TEAM, &info);
47 	fCurrentTeam = info.team;
48 }
49 
50 
51 TeamsWindow::~TeamsWindow()
52 {
53 	BMessenger(fStartTeamWindow).SendMessage(B_QUIT_REQUESTED);
54 }
55 
56 
57 /*static*/ TeamsWindow*
58 TeamsWindow::Create(SettingsManager* settingsManager)
59 {
60 	TeamsWindow* self = new TeamsWindow(settingsManager);
61 
62 	try {
63 		self->_Init();
64 	} catch (...) {
65 		delete self;
66 		throw;
67 	}
68 
69 	return self;
70 }
71 
72 
73 void
74 TeamsWindow::MessageReceived(BMessage* message)
75 {
76 	switch (message->what) {
77 		case MSG_START_NEW_TEAM:
78 		{
79 			BMessenger messenger(fStartTeamWindow);
80 			if (!messenger.IsValid()) {
81 				fStartTeamWindow = StartTeamWindow::Create();
82 				if (fStartTeamWindow == NULL)
83 					break;
84 			}
85 
86 			fStartTeamWindow->Show();
87 			break;
88 		}
89 
90 		case MSG_DEBUG_THIS_TEAM:
91 		{
92 			TeamRow* row = dynamic_cast<TeamRow*>(
93 				fTeamsListView->CurrentSelection());
94 			if (row != NULL) {
95 				BMessage message(MSG_DEBUG_THIS_TEAM);
96 				message.AddInt32("team", row->TeamID());
97 				be_app_messenger.SendMessage(&message);
98 			}
99 			break;
100 		}
101 
102 		case MSG_TEAM_SELECTION_CHANGED:
103 		{
104 			TeamRow* row = dynamic_cast<TeamRow*>(
105 				fTeamsListView->CurrentSelection());
106 			bool enabled = false;
107 			if (row != NULL) {
108 				team_id id = row->TeamID();
109 				if (id != fCurrentTeam && id != B_SYSTEM_TEAM)
110 					enabled = true;
111 			}
112 
113 			fAttachTeamButton->SetEnabled(enabled);
114 			break;
115 		}
116 
117 		default:
118 			BWindow::MessageReceived(message);
119 			break;
120 	}
121 }
122 
123 
124 bool
125 TeamsWindow::QuitRequested()
126 {
127 	_SaveSettings();
128 
129 	be_app_messenger.SendMessage(MSG_TEAMS_WINDOW_CLOSED);
130 	return true;
131 }
132 
133 
134 // #pragma mark --
135 
136 
137 void
138 TeamsWindow::_Init()
139 {
140 	BMessage settings;
141 	_LoadSettings(settings);
142 
143 	BRect frame;
144 	if (settings.FindRect("teams window frame", &frame) == B_OK) {
145 		MoveTo(frame.LeftTop());
146 		ResizeTo(frame.Width(), frame.Height());
147 	}
148 
149 	BLayoutBuilder::Group<>(this, B_VERTICAL)
150 		.Add(fTeamsListView = new TeamsListView("TeamsList", fCurrentTeam))
151 		.SetInsets(1.0f, 1.0f, 1.0f, 1.0f)
152 		.AddGroup(B_HORIZONTAL, 4.0f)
153 			.Add(fAttachTeamButton = new BButton("Attach", new BMessage(
154 						MSG_DEBUG_THIS_TEAM)))
155 			.Add(fCreateTeamButton = new BButton("Start new team"
156 					B_UTF8_ELLIPSIS, new BMessage(MSG_START_NEW_TEAM)))
157 			.End()
158 		.End();
159 
160 	fTeamsListView->SetInvocationMessage(new BMessage(MSG_DEBUG_THIS_TEAM));
161 	fTeamsListView->SetSelectionMessage(new BMessage(
162 			MSG_TEAM_SELECTION_CHANGED));
163 
164 	fAttachTeamButton->SetEnabled(false);
165 }
166 
167 
168 status_t
169 TeamsWindow::_OpenSettings(BFile& file, uint32 mode)
170 {
171 	BPath path;
172 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
173 		return B_ERROR;
174 
175 	path.Append("Debugger settings");
176 
177 	return file.SetTo(path.Path(), mode);
178 }
179 
180 
181 status_t
182 TeamsWindow::_LoadSettings(BMessage& settings)
183 {
184 	BFile file;
185 	status_t status = _OpenSettings(file, B_READ_ONLY);
186 	if (status < B_OK)
187 		return status;
188 
189 	return settings.Unflatten(&file);
190 }
191 
192 
193 status_t
194 TeamsWindow::_SaveSettings()
195 {
196 	BFile file;
197 	status_t status = _OpenSettings(file,
198 		B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
199 
200 	if (status < B_OK)
201 		return status;
202 
203 	BMessage settings('hdbg');
204 	status = settings.AddRect("teams window frame", Frame());
205 	if (status != B_OK)
206 		return status;
207 
208 	if (status == B_OK)
209 		status = settings.Flatten(&file);
210 
211 	return status;
212 }
213