1 /* 2 * Copyright 2009-2010, Philippe Houdoin, phoudoin@haiku-os.org. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include <new> 8 9 #include <stdio.h> 10 #include <string.h> 11 #include <stdarg.h> 12 13 #include <Application.h> 14 #include <ListView.h> 15 #include <ScrollView.h> 16 #include <File.h> 17 #include <FindDirectory.h> 18 #include <Path.h> 19 20 #include "MessageCodes.h" 21 #include "SettingsManager.h" 22 #include "TeamsWindow.h" 23 #include "TeamsListView.h" 24 25 26 TeamsWindow::TeamsWindow(SettingsManager* settingsManager) 27 : 28 BWindow(BRect(100, 100, 500, 250), "Teams", B_DOCUMENT_WINDOW, 29 B_ASYNCHRONOUS_CONTROLS), 30 fTeamsListView(NULL), 31 fSettingsManager(settingsManager) 32 { 33 } 34 35 36 TeamsWindow::~TeamsWindow() 37 { 38 } 39 40 41 /*static*/ TeamsWindow* 42 TeamsWindow::Create(SettingsManager* settingsManager) 43 { 44 TeamsWindow* self = new TeamsWindow(settingsManager); 45 46 try { 47 self->_Init(); 48 } catch (...) { 49 delete self; 50 throw; 51 } 52 53 return self; 54 } 55 56 57 void 58 TeamsWindow::MessageReceived(BMessage* message) 59 { 60 switch (message->what) { 61 case kMsgDebugThisTeam: 62 { 63 TeamListItem* item = dynamic_cast<TeamListItem*>(fTeamsListView->ItemAt( 64 fTeamsListView->CurrentSelection())); 65 66 if (item != NULL) { 67 BMessage message(MSG_DEBUG_THIS_TEAM); 68 message.AddInt32("team", item->TeamID()); 69 be_app_messenger.SendMessage(&message); 70 } 71 break; 72 } 73 74 default: 75 BWindow::MessageReceived(message); 76 break; 77 } 78 } 79 80 81 bool 82 TeamsWindow::QuitRequested() 83 { 84 _SaveSettings(); 85 86 be_app_messenger.SendMessage(MSG_TEAMS_WINDOW_CLOSED); 87 return true; 88 } 89 90 91 // #pragma mark -- 92 93 94 void 95 TeamsWindow::_Init() 96 { 97 BMessage settings; 98 _LoadSettings(settings); 99 100 BRect frame; 101 if (settings.FindRect("teams window frame", &frame) == B_OK) { 102 MoveTo(frame.LeftTop()); 103 ResizeTo(frame.Width(), frame.Height()); 104 } 105 106 // TODO: add button to start a team debugger 107 // TODO: add UI to setup arguments and environ, launch a program 108 // and start his team debugger 109 110 // Add a teams list view 111 frame = Bounds(); 112 frame.right -= B_V_SCROLL_BAR_WIDTH; 113 114 fTeamsListView = new TeamsListView(frame, "TeamsList"); 115 fTeamsListView->SetInvocationMessage(new BMessage(kMsgDebugThisTeam)); 116 117 BScrollView * teamsScroller = new BScrollView("TeamsListScroller", 118 fTeamsListView, B_FOLLOW_ALL_SIDES, 0, false, true, B_NO_BORDER); 119 120 AddChild(teamsScroller); 121 122 // small visual tweak 123 if (BScrollBar* scrollBar = teamsScroller->ScrollBar(B_VERTICAL)) { 124 scrollBar->MoveBy(0, -1); 125 scrollBar->ResizeBy(0, -(B_H_SCROLL_BAR_HEIGHT - 2)); 126 } 127 } 128 129 130 status_t 131 TeamsWindow::_OpenSettings(BFile& file, uint32 mode) 132 { 133 BPath path; 134 if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK) 135 return B_ERROR; 136 137 path.Append("Debugger settings"); 138 139 return file.SetTo(path.Path(), mode); 140 } 141 142 143 status_t 144 TeamsWindow::_LoadSettings(BMessage& settings) 145 { 146 BFile file; 147 status_t status = _OpenSettings(file, B_READ_ONLY); 148 if (status < B_OK) 149 return status; 150 151 return settings.Unflatten(&file); 152 } 153 154 155 status_t 156 TeamsWindow::_SaveSettings() 157 { 158 BFile file; 159 status_t status = _OpenSettings(file, 160 B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE); 161 162 if (status < B_OK) 163 return status; 164 165 BMessage settings('hdbg'); 166 status = settings.AddRect("teams window frame", Frame()); 167 if (status != B_OK) 168 return status; 169 170 if (status == B_OK) 171 status = settings.Flatten(&file); 172 173 return status; 174 } 175