1 /* 2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Copyright 2011, Rene Gollent, rene@gollent.com. 4 * Distributed under the terms of the MIT License. 5 */ 6 7 8 #include "BreakpointsView.h" 9 10 #include <new> 11 12 #include <Button.h> 13 #include <LayoutBuilder.h> 14 15 #include <AutoLocker.h> 16 #include <ObjectList.h> 17 18 #include "MessageCodes.h" 19 #include "Team.h" 20 #include "UserBreakpoint.h" 21 22 23 // #pragma mark - BreakpointsView 24 25 26 BreakpointsView::BreakpointsView(Team* team, Listener* listener) 27 : 28 BGroupView(B_HORIZONTAL, 4.0f), 29 fTeam(team), 30 fBreakpoint(NULL), 31 fWatchpoint(NULL), 32 fListView(NULL), 33 fToggleBreakpointButton(NULL), 34 fRemoveBreakpointButton(NULL), 35 fListener(listener) 36 { 37 SetName("Breakpoints"); 38 } 39 40 41 BreakpointsView::~BreakpointsView() 42 { 43 if (fListView != NULL) 44 fListView->UnsetListener(); 45 } 46 47 48 /*static*/ BreakpointsView* 49 BreakpointsView::Create(Team* team, Listener* listener) 50 { 51 BreakpointsView* self = new BreakpointsView(team, listener); 52 53 try { 54 self->_Init(); 55 } catch (...) { 56 delete self; 57 throw; 58 } 59 60 return self; 61 } 62 63 64 void 65 BreakpointsView::UnsetListener() 66 { 67 fListener = NULL; 68 } 69 70 71 void 72 BreakpointsView::SetBreakpoint(UserBreakpoint* breakpoint, Watchpoint* watchpoint) 73 { 74 if (breakpoint == fBreakpoint && watchpoint == fWatchpoint) 75 return; 76 77 if (fWatchpoint != NULL) 78 fWatchpoint->ReleaseReference(); 79 80 if (fBreakpoint != NULL) 81 fBreakpoint->ReleaseReference(); 82 83 fWatchpoint = watchpoint; 84 fBreakpoint = breakpoint; 85 86 if (fBreakpoint != NULL) 87 fBreakpoint->AcquireReference(); 88 else if (fWatchpoint != NULL) 89 fWatchpoint->AcquireReference(); 90 91 fListView->SetBreakpoint(breakpoint, watchpoint); 92 93 _UpdateButtons(); 94 } 95 96 97 void 98 BreakpointsView::UserBreakpointChanged(UserBreakpoint* breakpoint) 99 { 100 fListView->UserBreakpointChanged(breakpoint); 101 102 _UpdateButtons(); 103 } 104 105 106 void 107 BreakpointsView::WatchpointChanged(Watchpoint* watchpoint) 108 { 109 fListView->WatchpointChanged(watchpoint); 110 111 _UpdateButtons(); 112 } 113 114 115 void 116 BreakpointsView::MessageReceived(BMessage* message) 117 { 118 switch (message->what) { 119 case MSG_ENABLE_BREAKPOINT: 120 if (fListener != NULL) { 121 if (fBreakpoint != NULL) { 122 fListener->SetBreakpointEnabledRequested(fBreakpoint, 123 true); 124 } else if (fWatchpoint != NULL) { 125 fListener->SetWatchpointEnabledRequested(fWatchpoint, 126 true); 127 } 128 } 129 break; 130 case MSG_DISABLE_BREAKPOINT: 131 if (fListener != NULL) { 132 if (fBreakpoint != NULL) { 133 fListener->SetBreakpointEnabledRequested(fBreakpoint, 134 false); 135 } else if (fWatchpoint != NULL) { 136 fListener->SetWatchpointEnabledRequested(fWatchpoint, 137 false); 138 } 139 } 140 break; 141 case MSG_CLEAR_BREAKPOINT: 142 if (fListener != NULL) { 143 if (fBreakpoint != NULL) { 144 fListener->ClearBreakpointRequested(fBreakpoint); 145 } else if (fWatchpoint != NULL) { 146 fListener->ClearWatchpointRequested(fWatchpoint); 147 } 148 } 149 break; 150 default: 151 BGroupView::MessageReceived(message); 152 break; 153 } 154 } 155 156 157 void 158 BreakpointsView::AttachedToWindow() 159 { 160 fToggleBreakpointButton->SetTarget(this); 161 fRemoveBreakpointButton->SetTarget(this); 162 } 163 164 165 void 166 BreakpointsView::LoadSettings(const BMessage& settings) 167 { 168 BMessage breakpointListSettings; 169 if (settings.FindMessage("breakpointList", &breakpointListSettings) 170 == B_OK) 171 fListView->LoadSettings(breakpointListSettings); 172 } 173 174 175 status_t 176 BreakpointsView::SaveSettings(BMessage& settings) 177 { 178 BMessage breakpointListSettings; 179 if (fListView->SaveSettings(breakpointListSettings) != B_OK) 180 return B_NO_MEMORY; 181 182 if (settings.AddMessage("breakpointList", &breakpointListSettings) != B_OK) 183 return B_NO_MEMORY; 184 185 return B_OK; 186 } 187 188 189 void 190 BreakpointsView::BreakpointSelectionChanged(UserBreakpoint* breakpoint) 191 { 192 if (fListener != NULL) 193 fListener->BreakpointSelectionChanged(breakpoint); 194 } 195 196 197 void 198 BreakpointsView::WatchpointSelectionChanged(Watchpoint* watchpoint) 199 { 200 if (fListener != NULL) 201 fListener->WatchpointSelectionChanged(watchpoint); 202 } 203 204 205 void 206 BreakpointsView::_Init() 207 { 208 BLayoutBuilder::Group<>(this) 209 .Add(fListView = BreakpointListView::Create(fTeam, this)) 210 .AddGroup(B_VERTICAL, 4.0f) 211 .Add(fToggleBreakpointButton = new BButton("Toggle")) 212 .Add(fRemoveBreakpointButton = new BButton("Remove")) 213 .AddGlue() 214 .End(); 215 216 fToggleBreakpointButton->SetMessage(new BMessage(MSG_ENABLE_BREAKPOINT)); 217 fRemoveBreakpointButton->SetMessage(new BMessage(MSG_CLEAR_BREAKPOINT)); 218 219 _UpdateButtons(); 220 } 221 222 223 void 224 BreakpointsView::_UpdateButtons() 225 { 226 AutoLocker<Team> teamLocker(fTeam); 227 228 bool enabled = false; 229 bool valid = false; 230 231 if (fBreakpoint != NULL && fBreakpoint->IsValid()) { 232 valid = true; 233 enabled = fBreakpoint->IsEnabled(); 234 } else if (fWatchpoint != NULL) { 235 valid = true; 236 enabled = fWatchpoint->IsEnabled(); 237 } 238 239 if (valid) { 240 if (enabled) { 241 fToggleBreakpointButton->SetLabel("Disable"); 242 fToggleBreakpointButton->SetMessage( 243 new BMessage(MSG_DISABLE_BREAKPOINT)); 244 } else { 245 fToggleBreakpointButton->SetLabel("Enable"); 246 fToggleBreakpointButton->SetMessage( 247 new BMessage(MSG_ENABLE_BREAKPOINT)); 248 } 249 250 fToggleBreakpointButton->SetEnabled(true); 251 fRemoveBreakpointButton->SetEnabled(true); 252 } else { 253 fToggleBreakpointButton->SetLabel("Enable"); 254 fToggleBreakpointButton->SetEnabled(false); 255 fRemoveBreakpointButton->SetEnabled(false); 256 } 257 } 258 259 260 // #pragma mark - Listener 261 262 263 BreakpointsView::Listener::~Listener() 264 { 265 } 266